aff75b8f7f50443e485d6d1936c9d5dc6f016392
[clinton/bobotpp.git] / source / Interp.C
1 // Interp.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002 Clinton Ebadi
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifdef USESCRIPTS
24
25 #include "Macros.H"
26 #include "Commands.H"
27 #include "Interp.H"
28 #include "BotInterp.H"
29
30 #include <libguile.h>
31
32 Bot * Interp::bot = 0;
33 #ifdef MULTITHREAD
34 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
35 #endif
36
37 typedef SCM (*SCMFunc)();
38
39 SCM
40 scm_apply_wrapper(void *data)
41 {
42 wrapper_data * wd = static_cast<wrapper_data *> (data);
43 scm_apply(wd->func, wd->args, SCM_EOL);
44 return SCM_BOOL_T;
45 }
46
47 #define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b)
48
49 void
50 Interp::Startup()
51 {
52 // scm_primitive_load_path(scm_makfrom0str("ice-9/boot-9.scm"));
53
54 bot_new_procedure ("bot-action", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
55 scm_c_define_gsubr ("bot-adduser", 5, 2, 0,
56 (SCMFunc)ScriptCommands::AddUser);
57 bot_new_procedure ("bot-addserver", (SCMFunc)ScriptCommands::Action,
58 3, 4, 0);
59 scm_c_define_gsubr ("bot-addshit", 3, 2, 0,
60 (SCMFunc)ScriptCommands::AddShit);
61 bot_new_procedure ("bot-ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
62 bot_new_procedure ("bot-cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0);
63 bot_new_procedure ("bot-deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0);
64 bot_new_procedure ("bot-delserver", (SCMFunc)ScriptCommands::DelServer,
65 1, 0, 0);
66 bot_new_procedure ("bot-deluser", (SCMFunc)ScriptCommands::DelUser, 2, 0, 0);
67 bot_new_procedure ("bot-delshit", (SCMFunc)ScriptCommands::DelShit, 2, 0, 0);
68 bot_new_procedure ("bot-deop", (SCMFunc)ScriptCommands::Deop, 2, 0, 0);
69 bot_new_procedure ("bot-die", (SCMFunc)ScriptCommands::Die, 1, 0, 0);
70 bot_new_procedure ("bot-do", (SCMFunc)ScriptCommands::Do, 1, 0, 0);
71 bot_new_procedure ("bot-invite", (SCMFunc)ScriptCommands::Invite, 2, 0, 0);
72 bot_new_procedure ("bot-join", (SCMFunc)ScriptCommands::Join, 1, 1, 0);
73 bot_new_procedure ("bot-keep", (SCMFunc)ScriptCommands::Keep, 2, 0, 0);
74 bot_new_procedure ("bot-kick", (SCMFunc)ScriptCommands::Kick, 2, 1, 0);
75 bot_new_procedure ("bot-kickban", (SCMFunc)ScriptCommands::KickBan, 2, 1, 0);
76 bot_new_procedure ("bot-lock", (SCMFunc)ScriptCommands::Lock, 1, 0, 0);
77 bot_new_procedure ("bot-logport", (SCMFunc)ScriptCommands::LogPort, 0, 0, 0);
78 bot_new_procedure ("bot-mode", (SCMFunc)ScriptCommands::Mode, 2, 0, 0);
79 bot_new_procedure ("bot-msg", (SCMFunc)ScriptCommands::Msg, 2, 0, 0);
80 bot_new_procedure ("bot-nextserver", (SCMFunc)ScriptCommands::NextServer,
81 0, 0, 0);
82 bot_new_procedure ("bot-nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0);
83 bot_new_procedure ("bot-op", (SCMFunc)ScriptCommands::Op, 2, 0, 0);
84 bot_new_procedure ("bot-part", (SCMFunc)ScriptCommands::Part, 1, 0, 0);
85 bot_new_procedure ("bot-reconnect", (SCMFunc)ScriptCommands::Reconnect,
86 0, 0, 0);
87 bot_new_procedure ("bot-say", (SCMFunc)ScriptCommands::Say, 2, 0, 0);
88 bot_new_procedure ("bot-server", (SCMFunc)ScriptCommands::Server, 1, 0, 0);
89 bot_new_procedure ("bot-setversion", (SCMFunc)ScriptCommands::SetVersion,
90 1, 0, 0);
91 bot_new_procedure ("bot-tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0);
92 bot_new_procedure ("bot-tkban", (SCMFunc)ScriptCommands::TKBan, 3, 1, 0);
93 bot_new_procedure ("bot-topic", (SCMFunc)ScriptCommands::Topic, 2, 0, 0);
94 bot_new_procedure ("bot-unlock", (SCMFunc)ScriptCommands::Unlock, 1, 0, 0);
95
96 bot_new_procedure ("bot-getnickname", (SCMFunc)ScriptCommands::getNickname,
97 0, 0, 0);
98 bot_new_procedure ("bot-getserver", (SCMFunc)ScriptCommands::getServer,
99 0, 0, 0);
100 bot_new_procedure ("bot-getserverlist",
101 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
102 bot_new_procedure ("bot-flush", (SCMFunc)ScriptCommands::flushQueue,
103 0, 0, 0);
104 bot_new_procedure ("bot-flushport", (SCMFunc)ScriptCommands::flushPort,
105 0, 0, 0);
106 bot_new_procedure ("bot-random", (SCMFunc)ScriptCommands::random,
107 1, 0, 0);
108 bot_new_procedure ("bot-addcommand", (SCMFunc)ScriptCommands::addCommand,
109 5, 0, 0);
110 bot_new_procedure ("bot-delcommand", (SCMFunc)ScriptCommands::delCommand,
111 1, 0, 0);
112 bot_new_procedure ("bot-addhook", (SCMFunc)ScriptCommands::AddHook,
113 3, 2, 0);
114 bot_new_procedure ("bot-addtimer", (SCMFunc)ScriptCommands::AddTimer,
115 2, 0, 0);
116 bot_new_procedure ("bot-deltimer", (SCMFunc)ScriptCommands::DelTimer,
117 1, 0, 0);
118 scm_c_define ("hooks/action", scm_long2num(Hook::ACTION));
119 scm_c_define ("hooks/nickname", scm_long2num(Hook::NICKNAME));
120 scm_c_define ("hooks/signoff", scm_long2num(Hook::SIGNOFF));
121 scm_c_define ("hooks/ctcp", scm_long2num(Hook::CTCP));
122 scm_c_define ("hooks/ctcp-reply", scm_long2num(Hook::CTCP_REPLY));
123 scm_c_define ("hooks/disconnect", scm_long2num(Hook::DISCONNECT));
124 scm_c_define ("hooks/flood", scm_long2num(Hook::FLOOD));
125 scm_c_define ("hooks/invite", scm_long2num(Hook::INVITE));
126 scm_c_define ("hooks/join", scm_long2num(Hook::JOIN));
127 scm_c_define ("hooks/kick", scm_long2num(Hook::KICK));
128 scm_c_define ("hooks/leave", scm_long2num(Hook::LEAVE));
129 scm_c_define ("hooks/part", scm_long2num(Hook::LEAVE));
130 scm_c_define ("hooks/mode", scm_long2num(Hook::MODE));
131 scm_c_define ("hooks/message", scm_long2num(Hook::MESSAGE));
132 // scm_c_define ("hooks/names", scm_long2num(Hook::NAMES));
133 scm_c_define ("hooks/notice", scm_long2num(Hook::NOTICE));
134 scm_c_define ("hooks/public", scm_long2num(Hook::PUBLIC));
135 scm_c_define ("hooks/public-notice", scm_long2num(Hook::PUBLIC_NOTICE));
136 scm_c_define ("hooks/raw", scm_long2num(Hook::RAW));
137 scm_c_define ("hooks/timer", scm_long2num(Hook::TIMER));
138 scm_c_define ("hooks/topic", scm_long2num(Hook::TOPIC));
139 }
140
141 void
142 Interp::Shutdown()
143 { }
144
145 void
146 Interp::Execute(Bot *b, String command)
147 {
148 #ifdef MULTITHREAD
149 // We get the lock
150 pthread_mutex_lock(&mutex);
151 #endif
152 bot = b;
153 gh_eval_str_with_catch(command, ErrorHandler);
154 #ifdef MULTITHREAD
155 // We release the lock
156 pthread_mutex_unlock(&mutex);
157 #endif
158 }
159
160 void
161 Interp::LoadScript(Bot *b, String filename)
162 {
163 #ifdef MULTITHREAD
164 // We get the lock
165 pthread_mutex_lock(&mutex);
166 #endif
167 bot = b;
168 gh_eval_file_with_catch(filename, ErrorHandler);
169 #ifdef MULTITHREAD
170 // We release the lock
171 pthread_mutex_unlock(&mutex);
172 #endif
173 }
174
175 SCM
176 Interp::ErrorHandler(void *data, SCM tag, SCM throw_args)
177 {
178 bot->botInterp->ScriptLog(throw_args);
179 return SCM_BOOL_F;
180 }
181
182 #endif