Add config dir to default load path
[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_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);
97 }
98
99
100 static SCM
101 lazy_eval_file(char *filename)
102 {
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);
109 }
110
111 static SCM
112 lazy_eval_string(char *str)
113 {
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);
120 }
121
122
123
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)
127
128 void
129 interp_init_helper (void* unused)
130 {
131 scm_c_use_module ("guile-user");
132 // Hooks
133 scm_c_define ("bot:exit-hook", scm_make_hook (scm_from_int (0)));
134
135 // Symbols
136 // bot:user-*
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));
142
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
149 // protection
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));
154
155 // auto-op
156 scm_c_define ("bot:aop/yes", scm_from_int (true));
157 scm_c_define ("bot:aop/no", scm_from_int (false));
158
159 // shit-list
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));
163 scm_c_define ("bot:shit/no-deban",
164 scm_from_int (ShitEntry::SHIT_NODEBAN));
165
166 // sys-dir
167 scm_c_define ("bot:sys-scripts-dir",
168 scm_from_locale_string ((String(PREFIX) +
169 "/share/bobotpp/scripts/").c_str ()));
170 // Hooks
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));
190 // send hooks
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));
197 // dcc hooks
198 scm_c_define ("hooks/dcc/chat-begin",
199 scm_from_int (Hook::DCC_CHAT_BEGIN));
200 scm_c_define ("hooks/dcc/chat-end",
201 scm_from_int (Hook::DCC_CHAT_END));
202 scm_c_define ("hooks/dcc/chat-message",
203 scm_from_int (Hook::DCC_CHAT_MESSAGE));
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);
214 bot_new_procedure ("bot:change-command-level",
215 (SCMFunc)ScriptCommands::ChangeCommandLevel,
216 2, 0, 0);
217 scm_c_define_gsubr ("bot:channel-users", 1, 0, 0,
218 (SCMFunc)ScriptCommands::ChannelUsers);
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);
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);
244
245 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
246 (SCMFunc)ScriptCommands::Notice);
247
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);
254 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
255 (SCMFunc)ScriptCommands::SetFloodRate);
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);
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);
264
265 bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname,
266 0, 0, 0);
267 bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer,
268 0, 0, 0);
269 bot_new_procedure ("bot:getserverlist",
270 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
271 bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue,
272 0, 0, 0);
273 bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort,
274 0, 0, 0);
275 bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random,
276 1, 0, 0);
277 bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand,
278 5, 0, 0);
279 bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand,
280 1, 0, 0);
281 bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook,
282 3, 3, 0);
283 bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer,
284 2, 0, 0);
285 bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer,
286 1, 0, 0);
287
288 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
289 (SCMFunc)ScriptCommands::sendDCCChatMessage);
290 }
291
292 #undef bot_new_procedure
293 #undef scm_c_define_gsubr
294 #undef scm_c_define
295
296
297 SCM
298 interp_post_startup_helper (void *bot_module)
299 {
300 SCM module = static_cast<SCM> (bot_module);
301 scm_c_define ("the-bot-module", module);
302 scm_c_export ("the-bot-module", 0);
303
304 // load bobot-utils
305 return scm_primitive_load_path
306 (scm_from_locale_string ((String(PREFIX) +
307 "/share/bobotpp/scripts/bobot-utils.scm").c_str ()));
308 }
309
310 void
311 Interp::Startup()
312 {
313 bot_module = scm_c_define_module ("the-bot-module",
314 interp_init_helper, 0);
315
316 }
317
318 void Interp::Startup2 (Bot *b)
319 {
320 bot = b;
321
322 scm_c_call_with_current_module (bot_module,
323 interp_post_startup_helper,
324 bot_module);
325 }
326
327 void
328 Interp::Shutdown()
329 {
330 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
331 "bot:exit-hook")),
332 SCM_EOL);
333 }
334
335 void
336 Interp::Execute(Bot *b, String command)
337 {
338 #ifdef MULTITHREAD
339 // We get the lock
340 pthread_mutex_lock(&mutex);
341 #endif
342
343 bot = b;
344 scm_c_with_throw_handler(SCM_BOOL_T,
345 (scm_t_catch_body) lazy_eval_string,
346 (void *) static_cast<const char *> (command.c_str ()),
347 (scm_t_catch_handler) Interp::EmptyHandler,
348 0,
349 0);
350
351 #ifdef MULTITHREAD
352 // We release the lock
353 pthread_mutex_unlock(&mutex);
354 #endif
355 }
356
357
358 void
359 Interp::LoadScript(Bot *b, String filename)
360 {
361 #ifdef MULTITHREAD
362 // We get the lock
363 pthread_mutex_lock(&mutex);
364 #endif
365 bot = b;
366 scm_c_with_throw_handler(SCM_BOOL_T,
367 (scm_t_catch_body) lazy_eval_file,
368 (void *)static_cast<const char * >(filename.c_str ()),
369 (scm_t_catch_handler) Interp::EmptyHandler,
370 0,
371 0);
372 #ifdef MULTITHREAD
373 // We release the lock
374 pthread_mutex_unlock(&mutex);
375 #endif
376 }
377
378 #endif