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