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