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