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