911bad2d5782fb80d2720df6a8e7de5f0ca657b7
[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,
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 // protection
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));
139
140 // auto-op
141 scm_c_define ("bot:aop/yes", scm_from_int (true));
142 scm_c_define ("bot:aop/no", scm_from_int (false));
143
144 // shit-list
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));
148 scm_c_define ("bot:shit/no-deban",
149 scm_from_int (ShitEntry::SHIT_NODEBAN));
150
151 // sys-dir
152 scm_c_define ("bot:sys-scripts-dir",
153 scm_from_locale_string (String(PREFIX) +
154 "/share/bobotpp/scripts/"));
155 // Hooks
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));
175 // send hooks
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));
182 // dcc hooks
183 scm_c_define ("hooks/dcc/chat-begin",
184 scm_from_int (Hook::DCC_CHAT_BEGIN));
185 scm_c_define ("hooks/dcc/chat-end",
186 scm_from_int (Hook::DCC_CHAT_END));
187 scm_c_define ("hooks/dcc/chat-message",
188 scm_from_int (Hook::DCC_CHAT_MESSAGE));
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);
199 bot_new_procedure ("bot:change-command-level",
200 (SCMFunc)ScriptCommands::ChangeCommandLevel,
201 2, 0, 0);
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);
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);
227
228 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
229 (SCMFunc)ScriptCommands::Notice);
230
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);
237 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
238 (SCMFunc)ScriptCommands::SetFloodRate);
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);
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);
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,
265 3, 3, 0);
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);
270
271 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
272 (SCMFunc)ScriptCommands::sendDCCChatMessage);
273 }
274
275 #undef bot_new_procedure
276 #undef scm_c_define_gsubr
277 #undef scm_c_define
278
279
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);
286
287 // load bobot-utils
288 scm_primitive_load
289 (scm_from_locale_string (String(PREFIX) +
290 "/share/bobotpp/scripts/bobot-utils.scm"));
291 return SCM_UNSPECIFIED;
292 }
293
294 void
295 Interp::Startup()
296 {
297 bot_module = scm_c_define_module ("the-bot-module",
298 interp_init_helper, 0);
299
300 }
301
302 void Interp::Startup2 (Bot *b)
303 {
304 bot = b;
305
306 scm_c_call_with_current_module (bot_module,
307 interp_post_startup_helper,
308 bot_module);
309 }
310
311 void
312 Interp::Shutdown()
313 {
314 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
315 "bot:exit-hook")),
316 SCM_EOL);
317 }
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
326
327 bot = b;
328 scm_internal_catch(SCM_BOOL_T,
329 (scm_t_catch_body) lazy_eval_string, (void *) static_cast<const char *> (command),
330 (scm_t_catch_handler) Interp::EmptyHandler, 0);
331
332 #ifdef MULTITHREAD
333 // We release the lock
334 pthread_mutex_unlock(&mutex);
335 #endif
336 }
337
338
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;
347 scm_internal_catch(SCM_BOOL_T,
348 (scm_t_catch_body) lazy_eval_file,
349 (void *)static_cast<const char * >(filename),
350 (scm_t_catch_handler) Interp::EmptyHandler, 0);
351 #ifdef MULTITHREAD
352 // We release the lock
353 pthread_mutex_unlock(&mutex);
354 #endif
355 }
356
357 #endif