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