cb21075d |
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 |
ae97d6ec |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | // 02110-1301, USA. |
cb21075d |
19 | |
20 | #ifdef HAVE_CONFIG_H |
21 | #include "config.h" |
22 | #endif |
23 | |
24 | #ifdef USESCRIPTS |
25 | |
26 | #include "Macros.H" |
27 | #include "Commands.H" |
28 | #include "Interp.H" |
29 | #include "BotInterp.H" |
30 | |
31 | #include <libguile.h> |
32 | |
ce02032f |
33 | // static class member initial definitions |
cb21075d |
34 | Bot * Interp::bot = 0; |
ce02032f |
35 | SCM Interp::bot_module = 0; |
9efc3706 |
36 | |
cb21075d |
37 | #ifdef MULTITHREAD |
38 | pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER; |
39 | #endif |
40 | |
41 | typedef SCM (*SCMFunc)(); |
42 | |
43 | SCM |
ae97d6ec |
44 | Interp::ScmApplyWrapper (void *data) |
cb21075d |
45 | { |
c3ecc559 |
46 | #ifdef MULTITHREAD |
47 | // pthread_mutex_lock (&Interp::mutex); |
48 | #endif |
49 | |
cb21075d |
50 | wrapper_data * wd = static_cast<wrapper_data *> (data); |
51 | scm_apply(wd->func, wd->args, SCM_EOL); |
c3ecc559 |
52 | |
53 | #ifdef MULTITHREAD |
54 | // pthread_mutex_unlock (&Interp::mutex); |
55 | #endif |
56 | |
cb21075d |
57 | return SCM_BOOL_T; |
58 | } |
59 | |
ae97d6ec |
60 | SCM |
61 | Interp::LazyHandler (void *data, SCM tag, SCM throw_args) |
d56bdd22 |
62 | { |
63 | SCM log_port = Interp::bot->botInterp->ScriptLog(); |
64 | SCM eport = scm_set_current_error_port(log_port); |
65 | |
66 | scm_handle_by_message_noexit((void *)"bobot++", tag, throw_args); |
67 | scm_force_output(log_port); |
68 | scm_set_current_error_port(eport); |
69 | scm_ithrow(tag, throw_args, 1); |
70 | return SCM_UNSPECIFIED; /* never returns */ |
71 | } |
72 | |
73 | SCM |
ae97d6ec |
74 | Interp::EmptyHandler(void *data, SCM tag, SCM args) |
d56bdd22 |
75 | { |
76 | return SCM_UNSPECIFIED; |
77 | } |
78 | |
79 | |
80 | SCM |
ae97d6ec |
81 | Interp::LazyApplyWrapper(void *data) |
d56bdd22 |
82 | { |
83 | return scm_internal_lazy_catch(SCM_BOOL_T, |
ae97d6ec |
84 | (scm_t_catch_body) Interp::ScmApplyWrapper, |
85 | data, |
86 | (scm_t_catch_handler) Interp::LazyHandler, |
87 | 0); |
d56bdd22 |
88 | } |
89 | |
90 | |
91 | static SCM |
92 | lazy_eval_file(char *filename) |
93 | { |
94 | return scm_internal_lazy_catch(SCM_BOOL_T, |
ae97d6ec |
95 | (scm_t_catch_body) scm_c_primitive_load, |
96 | filename, |
97 | (scm_t_catch_handler) Interp::LazyHandler, 0); |
d56bdd22 |
98 | } |
99 | |
100 | static SCM |
101 | lazy_eval_string(char *str) |
102 | { |
103 | return scm_internal_lazy_catch(SCM_BOOL_T, |
104 | (scm_t_catch_body) scm_c_eval_string, str, |
ae97d6ec |
105 | (scm_t_catch_handler) Interp::LazyHandler, 0); |
d56bdd22 |
106 | } |
107 | |
108 | |
109 | |
ce02032f |
110 | #define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b); scm_c_export (a, 0) |
111 | #define scm_c_define_gsubr(a, b, c, d, e) scm_c_define_gsubr (a, b, c, d, e); scm_c_export (a, 0) |
112 | #define scm_c_define(a, b) scm_c_define (a, b); scm_c_export (a, 0) |
cb21075d |
113 | |
114 | void |
ce02032f |
115 | interp_init_helper (void* unused) |
cb21075d |
116 | { |
ce02032f |
117 | scm_c_use_module ("guile-user"); |
91dddabd |
118 | // Hooks |
119 | scm_c_define ("bot:exit-hook", scm_make_hook (scm_long2num (0))); |
cb21075d |
120 | |
439869bf |
121 | // Symbols |
122 | // bot:user-* |
123 | scm_c_define ("bot:user-none", scm_long2num (User::NONE)); |
124 | scm_c_define ("bot:user-user", scm_long2num (User::USER)); |
125 | scm_c_define ("bot:user-trusted", scm_long2num (User::TRUSTED_USER)); |
126 | scm_c_define ("bot:user-friend", scm_long2num (User::FRIEND)); |
127 | scm_c_define ("bot:user-master", scm_long2num (User::MASTER)); |
af8c61fe |
128 | |
129 | // protection |
130 | scm_c_define ("bot:protection/none", scm_long2num (User::NO_PROT)); |
131 | scm_c_define ("bot:protection/no-ban", scm_long2num (User::NO_BAN)); |
132 | scm_c_define ("bot:protection/no-kick", scm_long2num (User::NO_KICK)); |
133 | scm_c_define ("bot:protection/no-deop", scm_long2num (User::NO_DEOP)); |
134 | |
135 | // auto-op |
136 | scm_c_define ("bot:aop/yes", scm_long2num (true)); |
137 | scm_c_define ("bot:aop/no", scm_long2num (false)); |
138 | |
439869bf |
139 | // sys-dir |
140 | scm_c_define ("bot:sys-scripts-dir", |
ad529fde |
141 | scm_makfrom0str (String(PREFIX) + |
142 | "/share/bobotpp/scripts/")); |
439869bf |
143 | // Hooks |
cb21075d |
144 | scm_c_define ("hooks/action", scm_long2num(Hook::ACTION)); |
145 | scm_c_define ("hooks/nickname", scm_long2num(Hook::NICKNAME)); |
146 | scm_c_define ("hooks/signoff", scm_long2num(Hook::SIGNOFF)); |
147 | scm_c_define ("hooks/ctcp", scm_long2num(Hook::CTCP)); |
148 | scm_c_define ("hooks/ctcp-reply", scm_long2num(Hook::CTCP_REPLY)); |
149 | scm_c_define ("hooks/disconnect", scm_long2num(Hook::DISCONNECT)); |
150 | scm_c_define ("hooks/flood", scm_long2num(Hook::FLOOD)); |
151 | scm_c_define ("hooks/invite", scm_long2num(Hook::INVITE)); |
152 | scm_c_define ("hooks/join", scm_long2num(Hook::JOIN)); |
153 | scm_c_define ("hooks/kick", scm_long2num(Hook::KICK)); |
cb21075d |
154 | scm_c_define ("hooks/mode", scm_long2num(Hook::MODE)); |
155 | scm_c_define ("hooks/message", scm_long2num(Hook::MESSAGE)); |
cb21075d |
156 | scm_c_define ("hooks/notice", scm_long2num(Hook::NOTICE)); |
ae97d6ec |
157 | scm_c_define ("hooks/part", scm_long2num(Hook::PART)); |
cb21075d |
158 | scm_c_define ("hooks/public", scm_long2num(Hook::PUBLIC)); |
159 | scm_c_define ("hooks/public-notice", scm_long2num(Hook::PUBLIC_NOTICE)); |
160 | scm_c_define ("hooks/raw", scm_long2num(Hook::RAW)); |
161 | scm_c_define ("hooks/timer", scm_long2num(Hook::TIMER)); |
162 | scm_c_define ("hooks/topic", scm_long2num(Hook::TOPIC)); |
6530edbf |
163 | // send hooks |
fed59248 |
164 | scm_c_define ("hooks/send/public", scm_long2num (Hook::SEND_PUBLIC)); |
165 | scm_c_define ("hooks/send/message", scm_long2num (Hook::SEND_MESSAGE)); |
166 | scm_c_define ("hooks/send/action", scm_long2num (Hook::SEND_ACTION)); |
167 | scm_c_define ("hooks/send/ctcp", scm_long2num (Hook::SEND_CTCP)); |
6530edbf |
168 | // dcc hooks |
169 | scm_c_define ("hooks/dcc/chat-begin", |
170 | scm_long2num (Hook::DCC_CHAT_BEGIN)); |
171 | scm_c_define ("hooks/dcc/chat-message", |
172 | scm_long2num (Hook::DCC_CHAT_MESSAGE)); |
439869bf |
173 | |
174 | // procedures |
175 | bot_new_procedure ("bot:action", (SCMFunc)ScriptCommands::Action, 2, 0, 0); |
176 | scm_c_define_gsubr ("bot:adduser", 5, 2, 0, |
177 | (SCMFunc)ScriptCommands::AddUser); |
178 | bot_new_procedure ("bot:addserver", (SCMFunc)ScriptCommands::Action, |
179 | 3, 4, 0); |
180 | scm_c_define_gsubr ("bot:addshit", 3, 2, 0, |
181 | (SCMFunc)ScriptCommands::AddShit); |
182 | bot_new_procedure ("bot:ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0); |
af8c61fe |
183 | bot_new_procedure ("bot:change-command-level", |
184 | (SCMFunc)ScriptCommands::ChangeCommandLevel, |
4edefeb6 |
185 | 2, 0, 0); |
439869bf |
186 | bot_new_procedure ("bot:cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0); |
187 | bot_new_procedure ("bot:deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0); |
188 | bot_new_procedure ("bot:delserver", (SCMFunc)ScriptCommands::DelServer, |
189 | 1, 0, 0); |
190 | bot_new_procedure ("bot:deluser", (SCMFunc)ScriptCommands::DelUser, 2, 0, 0); |
191 | bot_new_procedure ("bot:delshit", (SCMFunc)ScriptCommands::DelShit, 2, 0, 0); |
192 | bot_new_procedure ("bot:deop", (SCMFunc)ScriptCommands::Deop, 2, 0, 0); |
193 | bot_new_procedure ("bot:die", (SCMFunc)ScriptCommands::Die, 1, 0, 0); |
194 | bot_new_procedure ("bot:do", (SCMFunc)ScriptCommands::Do, 1, 0, 0); |
195 | bot_new_procedure ("bot:invite", (SCMFunc)ScriptCommands::Invite, 2, 0, 0); |
196 | bot_new_procedure ("bot:join", (SCMFunc)ScriptCommands::Join, 1, 1, 0); |
197 | bot_new_procedure ("bot:keep", (SCMFunc)ScriptCommands::Keep, 2, 0, 0); |
198 | bot_new_procedure ("bot:kick", (SCMFunc)ScriptCommands::Kick, 2, 1, 0); |
199 | bot_new_procedure ("bot:kickban", (SCMFunc)ScriptCommands::KickBan, 2, 1, 0); |
200 | bot_new_procedure ("bot:lock", (SCMFunc)ScriptCommands::Lock, 1, 0, 0); |
201 | bot_new_procedure ("bot:logport", (SCMFunc)ScriptCommands::LogPort, 0, 0, 0); |
202 | bot_new_procedure ("bot:mode", (SCMFunc)ScriptCommands::Mode, 2, 0, 0); |
203 | bot_new_procedure ("bot:msg", (SCMFunc)ScriptCommands::Msg, 2, 0, 0); |
204 | bot_new_procedure ("bot:nextserver", (SCMFunc)ScriptCommands::NextServer, |
205 | 0, 0, 0); |
206 | bot_new_procedure ("bot:nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0); |
5aec4622 |
207 | |
208 | scm_c_define_gsubr ("bot:notice", 2, 0, 0, |
209 | (SCMFunc)ScriptCommands::sendNotice); |
210 | |
439869bf |
211 | bot_new_procedure ("bot:op", (SCMFunc)ScriptCommands::Op, 2, 0, 0); |
212 | bot_new_procedure ("bot:part", (SCMFunc)ScriptCommands::Part, 1, 0, 0); |
213 | bot_new_procedure ("bot:reconnect", (SCMFunc)ScriptCommands::Reconnect, |
214 | 0, 0, 0); |
215 | bot_new_procedure ("bot:say", (SCMFunc)ScriptCommands::Say, 2, 0, 0); |
216 | bot_new_procedure ("bot:server", (SCMFunc)ScriptCommands::Server, 1, 0, 0); |
e171dcce |
217 | scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0, |
218 | (SCMFunc)ScriptCommands::SetFloodRate); |
439869bf |
219 | bot_new_procedure ("bot:setversion", (SCMFunc)ScriptCommands::SetVersion, |
220 | 1, 0, 0); |
221 | bot_new_procedure ("bot:tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0); |
222 | bot_new_procedure ("bot:tkban", (SCMFunc)ScriptCommands::TKBan, 3, 1, 0); |
223 | bot_new_procedure ("bot:topic", (SCMFunc)ScriptCommands::Topic, 2, 0, 0); |
224 | bot_new_procedure ("bot:unlock", (SCMFunc)ScriptCommands::Unlock, 1, 0, 0); |
225 | |
226 | bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname, |
227 | 0, 0, 0); |
228 | bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer, |
229 | 0, 0, 0); |
230 | bot_new_procedure ("bot:getserverlist", |
231 | (SCMFunc)ScriptCommands::getServerList, 0, 0, 0); |
232 | bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue, |
233 | 0, 0, 0); |
234 | bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort, |
235 | 0, 0, 0); |
236 | bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random, |
237 | 1, 0, 0); |
238 | bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand, |
239 | 5, 0, 0); |
240 | bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand, |
241 | 1, 0, 0); |
242 | bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook, |
fd7440f1 |
243 | 3, 3, 0); |
439869bf |
244 | bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer, |
245 | 2, 0, 0); |
246 | bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer, |
247 | 1, 0, 0); |
e07b6b46 |
248 | |
b93600f6 |
249 | scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0, |
0b7a49e2 |
250 | (SCMFunc)ScriptCommands::sendDCCChatMessage); |
251 | |
e07b6b46 |
252 | // "Low Level" Message functuions |
672b7d4e |
253 | scm_c_define_gsubr ("bot:send-ctcp", 3, 0, 0, |
e07b6b46 |
254 | (SCMFunc)ScriptCommands::sendCTCP); |
672b7d4e |
255 | scm_c_define_gsubr ("bot:send-ctcp-reply", 3, 0, 0, |
256 | (SCMFunc)ScriptCommands::sendCTCPReply); |
ce02032f |
257 | } |
258 | |
259 | #undef bot_new_procedure |
260 | #undef scm_c_define_gsubr |
261 | #undef scm_c_define |
262 | |
e07b6b46 |
263 | |
ce02032f |
264 | SCM |
265 | interp_post_startup_helper (void *bot_module) |
266 | { |
267 | SCM module = static_cast<SCM> (bot_module); |
268 | scm_c_define ("the-bot-module", module); |
269 | scm_c_export ("the-bot-module", 0); |
9efc3706 |
270 | |
439869bf |
271 | // load bobot-utils |
272 | scm_primitive_load |
273 | (scm_makfrom0str (String(PREFIX) + |
274 | "/share/bobotpp/scripts/bobot-utils.scm")); |
606a8eec |
275 | return SCM_UNSPECIFIED; |
ce02032f |
276 | } |
439869bf |
277 | |
ce02032f |
278 | void |
279 | Interp::Startup() |
280 | { |
281 | bot_module = scm_c_define_module ("the-bot-module", |
0316e2c1 |
282 | interp_init_helper, 0); |
d695ede4 |
283 | |
ce02032f |
284 | scm_c_call_with_current_module (bot_module, |
285 | interp_post_startup_helper, |
286 | bot_module); |
cb21075d |
287 | } |
288 | |
289 | void |
290 | Interp::Shutdown() |
91dddabd |
291 | { |
292 | scm_c_run_hook (gh_lookup ("bot:exit-hook"), SCM_EOL); |
293 | } |
cb21075d |
294 | |
295 | void |
296 | Interp::Execute(Bot *b, String command) |
297 | { |
298 | #ifdef MULTITHREAD |
299 | // We get the lock |
300 | pthread_mutex_lock(&mutex); |
301 | #endif |
0316e2c1 |
302 | |
cb21075d |
303 | bot = b; |
d56bdd22 |
304 | scm_internal_catch(SCM_BOOL_T, |
305 | (scm_t_catch_body) lazy_eval_string, (void *) static_cast<const char *> (command), |
ae97d6ec |
306 | (scm_t_catch_handler) Interp::EmptyHandler, 0); |
0316e2c1 |
307 | |
cb21075d |
308 | #ifdef MULTITHREAD |
309 | // We release the lock |
310 | pthread_mutex_unlock(&mutex); |
311 | #endif |
312 | } |
313 | |
ce02032f |
314 | |
cb21075d |
315 | void |
316 | Interp::LoadScript(Bot *b, String filename) |
317 | { |
318 | #ifdef MULTITHREAD |
319 | // We get the lock |
320 | pthread_mutex_lock(&mutex); |
321 | #endif |
322 | bot = b; |
d56bdd22 |
323 | scm_internal_catch(SCM_BOOL_T, |
ae97d6ec |
324 | (scm_t_catch_body) lazy_eval_file, |
325 | (void *)static_cast<const char * >(filename), |
326 | (scm_t_catch_handler) Interp::EmptyHandler, 0); |
cb21075d |
327 | #ifdef MULTITHREAD |
328 | // We release the lock |
329 | pthread_mutex_unlock(&mutex); |
330 | #endif |
331 | } |
332 | |
cb21075d |
333 | #endif |