Quiet annoying mutex debug logging messages
[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 extern "C" {
30 #include <libguile/hooks.h>
31 }
32
33 #include "Bot.H"
34 #include "BotInterp.H"
35 #include "Commands.H"
36 #include "Macros.H"
37 #include "ScriptCommands.H"
38 #include "ShitEntry.H"
39 #include "User.H"
40
41 // static class member initial definitions
42 Bot * Interp::bot = 0;
43 SCM Interp::bot_module = 0;
44
45 #ifdef MULTITHREAD
46 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
47 #endif
48
49 typedef scm_t_subr SCMFunc;
50
51 SCM
52 Interp::ScmApplyWrapper (void *data)
53 {
54 #ifdef MULTITHREAD
55 // pthread_mutex_lock (&Interp::mutex);
56 #endif
57
58 wrapper_data * wd = static_cast<wrapper_data *> (data);
59 scm_apply(wd->func, wd->args, SCM_EOL);
60
61 #ifdef MULTITHREAD
62 // pthread_mutex_unlock (&Interp::mutex);
63 #endif
64
65 return SCM_BOOL_T;
66 }
67
68 SCM
69 Interp::LazyHandler (void *data, SCM tag, SCM throw_args)
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
82 Interp::EmptyHandler(void *data, SCM tag, SCM args)
83 {
84 return SCM_UNSPECIFIED;
85 }
86
87
88 SCM
89 Interp::LazyApplyWrapper(void *data)
90 {
91 return scm_internal_catch(SCM_BOOL_T,
92 (scm_t_catch_body) Interp::ScmApplyWrapper,
93 data,
94 (scm_t_catch_handler) Interp::LazyHandler,
95 0);
96 }
97
98
99 static SCM
100 lazy_eval_file(char *filename)
101 {
102 return scm_internal_catch(SCM_BOOL_T,
103 (scm_t_catch_body) scm_c_primitive_load_path,
104 filename,
105 (scm_t_catch_handler) Interp::LazyHandler, 0);
106 }
107
108 static SCM
109 lazy_eval_string(char *str)
110 {
111 return scm_internal_catch(SCM_BOOL_T,
112 (scm_t_catch_body) scm_c_eval_string, str,
113 (scm_t_catch_handler) Interp::LazyHandler, 0);
114 }
115
116
117
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)
121
122 void
123 interp_init_helper (void* unused)
124 {
125 scm_c_use_module ("guile-user");
126 // Hooks
127 scm_c_define ("bot:exit-hook", scm_make_hook (scm_from_int (0)));
128
129 // Symbols
130 // bot:user-*
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));
136
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
143 // protection
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));
148
149 // auto-op
150 scm_c_define ("bot:aop/yes", scm_from_int (true));
151 scm_c_define ("bot:aop/no", scm_from_int (false));
152
153 // shit-list
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));
157 scm_c_define ("bot:shit/no-deban",
158 scm_from_int (ShitEntry::SHIT_NODEBAN));
159
160 // sys-dir
161 scm_c_define ("bot:sys-scripts-dir",
162 scm_from_locale_string ((String(PREFIX) +
163 "/share/bobotpp/scripts/").c_str ()));
164 // Hooks
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));
184 // send hooks
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));
191 // dcc hooks
192 scm_c_define ("hooks/dcc/chat-begin",
193 scm_from_int (Hook::DCC_CHAT_BEGIN));
194 scm_c_define ("hooks/dcc/chat-end",
195 scm_from_int (Hook::DCC_CHAT_END));
196 scm_c_define ("hooks/dcc/chat-message",
197 scm_from_int (Hook::DCC_CHAT_MESSAGE));
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);
208 bot_new_procedure ("bot:change-command-level",
209 (SCMFunc)ScriptCommands::ChangeCommandLevel,
210 2, 0, 0);
211 scm_c_define_gsubr ("bot:channel-users", 1, 0, 0,
212 (SCMFunc)ScriptCommands::ChannelUsers);
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);
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);
238
239 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
240 (SCMFunc)ScriptCommands::Notice);
241
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);
248 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
249 (SCMFunc)ScriptCommands::SetFloodRate);
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);
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);
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,
276 3, 3, 0);
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);
281
282 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
283 (SCMFunc)ScriptCommands::sendDCCChatMessage);
284 }
285
286 #undef bot_new_procedure
287 #undef scm_c_define_gsubr
288 #undef scm_c_define
289
290
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);
297
298 // load bobot-utils
299 return scm_primitive_load_path
300 (scm_from_locale_string ((String(PREFIX) +
301 "/share/bobotpp/scripts/bobot-utils.scm").c_str ()));
302 }
303
304 void
305 Interp::Startup()
306 {
307 bot_module = scm_c_define_module ("the-bot-module",
308 interp_init_helper, 0);
309
310 }
311
312 void Interp::Startup2 (Bot *b)
313 {
314 bot = b;
315
316 scm_c_call_with_current_module (bot_module,
317 interp_post_startup_helper,
318 bot_module);
319 }
320
321 void
322 Interp::Shutdown()
323 {
324 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
325 "bot:exit-hook")),
326 SCM_EOL);
327 }
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
336
337 bot = b;
338 scm_internal_catch(SCM_BOOL_T,
339 (scm_t_catch_body) lazy_eval_string, (void *) static_cast<const char *> (command.c_str ()),
340 (scm_t_catch_handler) Interp::EmptyHandler, 0);
341
342 #ifdef MULTITHREAD
343 // We release the lock
344 pthread_mutex_unlock(&mutex);
345 #endif
346 }
347
348
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;
357 scm_internal_catch(SCM_BOOL_T,
358 (scm_t_catch_body) lazy_eval_file,
359 (void *)static_cast<const char * >(filename.c_str ()),
360 (scm_t_catch_handler) Interp::EmptyHandler, 0);
361 #ifdef MULTITHREAD
362 // We release the lock
363 pthread_mutex_unlock(&mutex);
364 #endif
365 }
366
367 #endif