Undo change for scm_primitive_load_path arity change
[clinton/bobotpp.git] / source / Interp.C
CommitLineData
cb21075d 1// Interp.C -*- C++ -*-
2// Copyright (c) 1998 Etienne BERNARD
46af1667 3// Copyright (C) 2002,2005,2008 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
cfa82921 20#include "Interp.H"
21
22#ifdef USESCRIPTS
23
cb21075d 24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
cfa82921 28#include <libguile.h>
cb21075d 29
cfa82921 30#include "Bot.H"
cb21075d 31#include "BotInterp.H"
cfa82921 32#include "Commands.H"
33#include "Macros.H"
34#include "ScriptCommands.H"
528799bd 35#include "ShitEntry.H"
cfa82921 36#include "User.H"
cb21075d 37
ce02032f 38// static class member initial definitions
cb21075d 39Bot * Interp::bot = 0;
ce02032f 40SCM Interp::bot_module = 0;
9efc3706 41
cb21075d 42#ifdef MULTITHREAD
43pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
44#endif
45
46typedef SCM (*SCMFunc)();
47
48SCM
ae97d6ec 49Interp::ScmApplyWrapper (void *data)
cb21075d 50{
c3ecc559 51#ifdef MULTITHREAD
52 // pthread_mutex_lock (&Interp::mutex);
53#endif
54
cb21075d 55 wrapper_data * wd = static_cast<wrapper_data *> (data);
56 scm_apply(wd->func, wd->args, SCM_EOL);
c3ecc559 57
58#ifdef MULTITHREAD
59 // pthread_mutex_unlock (&Interp::mutex);
60#endif
61
cb21075d 62 return SCM_BOOL_T;
63}
64
ae97d6ec 65SCM
66Interp::LazyHandler (void *data, SCM tag, SCM throw_args)
d56bdd22 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
78SCM
ae97d6ec 79Interp::EmptyHandler(void *data, SCM tag, SCM args)
d56bdd22 80{
81 return SCM_UNSPECIFIED;
82}
83
84
85SCM
ae97d6ec 86Interp::LazyApplyWrapper(void *data)
d56bdd22 87{
88 return scm_internal_lazy_catch(SCM_BOOL_T,
ae97d6ec 89 (scm_t_catch_body) Interp::ScmApplyWrapper,
90 data,
91 (scm_t_catch_handler) Interp::LazyHandler,
92 0);
d56bdd22 93}
94
95
96static SCM
97lazy_eval_file(char *filename)
98{
99 return scm_internal_lazy_catch(SCM_BOOL_T,
a1cf3b8a 100 (scm_t_catch_body) scm_c_primitive_load_path,
ae97d6ec 101 filename,
102 (scm_t_catch_handler) Interp::LazyHandler, 0);
d56bdd22 103}
104
105static SCM
106lazy_eval_string(char *str)
107{
108 return scm_internal_lazy_catch(SCM_BOOL_T,
109 (scm_t_catch_body) scm_c_eval_string, str,
ae97d6ec 110 (scm_t_catch_handler) Interp::LazyHandler, 0);
d56bdd22 111}
112
113
114
ce02032f 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)
cb21075d 118
119void
ce02032f 120interp_init_helper (void* unused)
cb21075d 121{
ce02032f 122 scm_c_use_module ("guile-user");
91dddabd 123 // Hooks
46af1667 124 scm_c_define ("bot:exit-hook", scm_make_hook (scm_from_int (0)));
cb21075d 125
439869bf 126 // Symbols
127 // bot:user-*
46af1667 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));
af8c61fe 133
c1ea3a86 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
af8c61fe 140 // protection
46af1667 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));
af8c61fe 145
146 // auto-op
46af1667 147 scm_c_define ("bot:aop/yes", scm_from_int (true));
148 scm_c_define ("bot:aop/no", scm_from_int (false));
528799bd 149
150 // shit-list
46af1667 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));
528799bd 154 scm_c_define ("bot:shit/no-deban",
46af1667 155 scm_from_int (ShitEntry::SHIT_NODEBAN));
af8c61fe 156
439869bf 157 // sys-dir
158 scm_c_define ("bot:sys-scripts-dir",
815a1816 159 scm_from_locale_string ((String(PREFIX) +
160 "/share/bobotpp/scripts/").c_str ()));
439869bf 161 // Hooks
46af1667 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));
6530edbf 181 // send hooks
46af1667 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));
6530edbf 188 // dcc hooks
189 scm_c_define ("hooks/dcc/chat-begin",
46af1667 190 scm_from_int (Hook::DCC_CHAT_BEGIN));
133eff7a 191 scm_c_define ("hooks/dcc/chat-end",
46af1667 192 scm_from_int (Hook::DCC_CHAT_END));
6530edbf 193 scm_c_define ("hooks/dcc/chat-message",
46af1667 194 scm_from_int (Hook::DCC_CHAT_MESSAGE));
439869bf 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);
af8c61fe 205 bot_new_procedure ("bot:change-command-level",
206 (SCMFunc)ScriptCommands::ChangeCommandLevel,
4edefeb6 207 2, 0, 0);
c1ea3a86 208 scm_c_define_gsubr ("bot:channel-users", 1, 0, 0,
209 (SCMFunc)ScriptCommands::ChannelUsers);
c6e7af05 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);
439869bf 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);
5aec4622 235
236 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
c99c411a 237 (SCMFunc)ScriptCommands::Notice);
5aec4622 238
439869bf 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);
e171dcce 245 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
246 (SCMFunc)ScriptCommands::SetFloodRate);
439869bf 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);
c6e7af05 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);
439869bf 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,
fd7440f1 273 3, 3, 0);
439869bf 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);
e07b6b46 278
b93600f6 279 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
0b7a49e2 280 (SCMFunc)ScriptCommands::sendDCCChatMessage);
ce02032f 281}
282
283#undef bot_new_procedure
284#undef scm_c_define_gsubr
285#undef scm_c_define
286
e07b6b46 287
ce02032f 288SCM
289interp_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);
9efc3706 294
439869bf 295 // load bobot-utils
a1cf3b8a 296 return scm_primitive_load_path
815a1816 297 (scm_from_locale_string ((String(PREFIX) +
8ab8c3fc 298 "/share/bobotpp/scripts/bobot-utils.scm").c_str ()));
ce02032f 299}
439869bf 300
ce02032f 301void
302Interp::Startup()
303{
304 bot_module = scm_c_define_module ("the-bot-module",
0316e2c1 305 interp_init_helper, 0);
d695ede4 306
b9e62f2c 307}
308
db098a03 309void Interp::Startup2 (Bot *b)
b9e62f2c 310{
db098a03 311 bot = b;
312
ce02032f 313 scm_c_call_with_current_module (bot_module,
314 interp_post_startup_helper,
315 bot_module);
cb21075d 316}
317
318void
319Interp::Shutdown()
91dddabd 320{
46af1667 321 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
322 "bot:exit-hook")),
323 SCM_EOL);
91dddabd 324}
cb21075d 325
326void
327Interp::Execute(Bot *b, String command)
328{
329#ifdef MULTITHREAD
330 // We get the lock
331 pthread_mutex_lock(&mutex);
332#endif
0316e2c1 333
cb21075d 334 bot = b;
d56bdd22 335 scm_internal_catch(SCM_BOOL_T,
815a1816 336 (scm_t_catch_body) lazy_eval_string, (void *) static_cast<const char *> (command.c_str ()),
ae97d6ec 337 (scm_t_catch_handler) Interp::EmptyHandler, 0);
0316e2c1 338
cb21075d 339#ifdef MULTITHREAD
340 // We release the lock
341 pthread_mutex_unlock(&mutex);
342#endif
343}
344
ce02032f 345
cb21075d 346void
347Interp::LoadScript(Bot *b, String filename)
348{
349#ifdef MULTITHREAD
350 // We get the lock
351 pthread_mutex_lock(&mutex);
352#endif
353 bot = b;
d56bdd22 354 scm_internal_catch(SCM_BOOL_T,
ae97d6ec 355 (scm_t_catch_body) lazy_eval_file,
815a1816 356 (void *)static_cast<const char * >(filename.c_str ()),
ae97d6ec 357 (scm_t_catch_handler) Interp::EmptyHandler, 0);
cb21075d 358#ifdef MULTITHREAD
359 // We release the lock
360 pthread_mutex_unlock(&mutex);
361#endif
362}
363
cb21075d 364#endif