Update all deprecated/discouraged Guile calls
[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 #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"
30 #include "ShitEntry.H"
31
32 #include <libguile.h>
33
34 // static class member initial definitions
35 Bot * Interp::bot = 0;
36 SCM Interp::bot_module = 0;
37
38 #ifdef MULTITHREAD
39 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
40 #endif
41
42 typedef SCM (*SCMFunc)();
43
44 SCM
45 Interp::ScmApplyWrapper (void *data)
46 {
47 #ifdef MULTITHREAD
48 // pthread_mutex_lock (&Interp::mutex);
49 #endif
50
51 wrapper_data * wd = static_cast<wrapper_data *> (data);
52 scm_apply(wd->func, wd->args, SCM_EOL);
53
54 #ifdef MULTITHREAD
55 // pthread_mutex_unlock (&Interp::mutex);
56 #endif
57
58 return SCM_BOOL_T;
59 }
60
61 SCM
62 Interp::LazyHandler (void *data, SCM tag, SCM throw_args)
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
74 SCM
75 Interp::EmptyHandler(void *data, SCM tag, SCM args)
76 {
77 return SCM_UNSPECIFIED;
78 }
79
80
81 SCM
82 Interp::LazyApplyWrapper(void *data)
83 {
84 return scm_internal_lazy_catch(SCM_BOOL_T,
85 (scm_t_catch_body) Interp::ScmApplyWrapper,
86 data,
87 (scm_t_catch_handler) Interp::LazyHandler,
88 0);
89 }
90
91
92 static SCM
93 lazy_eval_file(char *filename)
94 {
95 return scm_internal_lazy_catch(SCM_BOOL_T,
96 (scm_t_catch_body) scm_c_primitive_load,
97 filename,
98 (scm_t_catch_handler) Interp::LazyHandler, 0);
99 }
100
101 static SCM
102 lazy_eval_string(char *str)
103 {
104 return scm_internal_lazy_catch(SCM_BOOL_T,
105 (scm_t_catch_body) scm_c_eval_string, str,
106 (scm_t_catch_handler) Interp::LazyHandler, 0);
107 }
108
109
110
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)
114
115 void
116 interp_init_helper (void* unused)
117 {
118 scm_c_use_module ("guile-user");
119 // Hooks
120 scm_c_define ("bot:exit-hook", scm_make_hook (scm_from_int (0)));
121
122 // Symbols
123 // bot:user-*
124 scm_c_define ("bot:user-none", scm_from_int (User::NONE));
125 scm_c_define ("bot:user-user", scm_from_int (User::USER));
126 scm_c_define ("bot:user-trusted", scm_from_int (User::TRUSTED_USER));
127 scm_c_define ("bot:user-friend", scm_from_int (User::FRIEND));
128 scm_c_define ("bot:user-master", scm_from_int (User::MASTER));
129
130 // protection
131 scm_c_define ("bot:protection/none", scm_from_int (User::NO_PROT));
132 scm_c_define ("bot:protection/no-ban", scm_from_int (User::NO_BAN));
133 scm_c_define ("bot:protection/no-kick", scm_from_int (User::NO_KICK));
134 scm_c_define ("bot:protection/no-deop", scm_from_int (User::NO_DEOP));
135
136 // auto-op
137 scm_c_define ("bot:aop/yes", scm_from_int (true));
138 scm_c_define ("bot:aop/no", scm_from_int (false));
139
140 // shit-list
141 scm_c_define ("bot:shit/none", scm_from_int (ShitEntry::SHIT_NOSHIT));
142 scm_c_define ("bot:shit/no-op", scm_from_int (ShitEntry::SHIT_NOOP));
143 scm_c_define ("bot:shit/no-join", scm_from_int (ShitEntry::SHIT_NOJOIN));
144 scm_c_define ("bot:shit/no-deban",
145 scm_from_int (ShitEntry::SHIT_NODEBAN));
146
147 // sys-dir
148 scm_c_define ("bot:sys-scripts-dir",
149 scm_from_locale_string (String(PREFIX) +
150 "/share/bobotpp/scripts/"));
151 // Hooks
152 scm_c_define ("hooks/action", scm_from_int(Hook::ACTION));
153 scm_c_define ("hooks/nickname", scm_from_int(Hook::NICKNAME));
154 scm_c_define ("hooks/signoff", scm_from_int(Hook::SIGNOFF));
155 scm_c_define ("hooks/ctcp", scm_from_int(Hook::CTCP));
156 scm_c_define ("hooks/ctcp-reply", scm_from_int(Hook::CTCP_REPLY));
157 scm_c_define ("hooks/disconnect", scm_from_int(Hook::DISCONNECT));
158 scm_c_define ("hooks/flood", scm_from_int(Hook::FLOOD));
159 scm_c_define ("hooks/invite", scm_from_int(Hook::INVITE));
160 scm_c_define ("hooks/join", scm_from_int(Hook::JOIN));
161 scm_c_define ("hooks/kick", scm_from_int(Hook::KICK));
162 scm_c_define ("hooks/mode", scm_from_int(Hook::MODE));
163 scm_c_define ("hooks/message", scm_from_int(Hook::MESSAGE));
164 scm_c_define ("hooks/notice", scm_from_int(Hook::NOTICE));
165 scm_c_define ("hooks/part", scm_from_int(Hook::PART));
166 scm_c_define ("hooks/public", scm_from_int(Hook::PUBLIC));
167 scm_c_define ("hooks/public-notice", scm_from_int(Hook::PUBLIC_NOTICE));
168 scm_c_define ("hooks/raw", scm_from_int(Hook::RAW));
169 scm_c_define ("hooks/timer", scm_from_int(Hook::TIMER));
170 scm_c_define ("hooks/topic", scm_from_int(Hook::TOPIC));
171 // send hooks
172 scm_c_define ("hooks/send/public", scm_from_int (Hook::SEND_PUBLIC));
173 scm_c_define ("hooks/send/message", scm_from_int (Hook::SEND_MESSAGE));
174 scm_c_define ("hooks/send/action", scm_from_int (Hook::SEND_ACTION));
175 scm_c_define ("hooks/send/ctcp", scm_from_int (Hook::SEND_CTCP));
176 scm_c_define ("hooks/send/who", scm_from_int (Hook::SEND_WHO));
177 scm_c_define ("hooks/send/whois", scm_from_int (Hook::SEND_WHOIS));
178 // dcc hooks
179 scm_c_define ("hooks/dcc/chat-begin",
180 scm_from_int (Hook::DCC_CHAT_BEGIN));
181 scm_c_define ("hooks/dcc/chat-end",
182 scm_from_int (Hook::DCC_CHAT_END));
183 scm_c_define ("hooks/dcc/chat-message",
184 scm_from_int (Hook::DCC_CHAT_MESSAGE));
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);
195 bot_new_procedure ("bot:change-command-level",
196 (SCMFunc)ScriptCommands::ChangeCommandLevel,
197 2, 0, 0);
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);
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);
223
224 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
225 (SCMFunc)ScriptCommands::Notice);
226
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);
233 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
234 (SCMFunc)ScriptCommands::SetFloodRate);
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);
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);
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,
261 3, 3, 0);
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);
266
267 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
268 (SCMFunc)ScriptCommands::sendDCCChatMessage);
269 }
270
271 #undef bot_new_procedure
272 #undef scm_c_define_gsubr
273 #undef scm_c_define
274
275
276 SCM
277 interp_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);
282
283 // load bobot-utils
284 scm_primitive_load
285 (scm_from_locale_string (String(PREFIX) +
286 "/share/bobotpp/scripts/bobot-utils.scm"));
287 return SCM_UNSPECIFIED;
288 }
289
290 void
291 Interp::Startup()
292 {
293 bot_module = scm_c_define_module ("the-bot-module",
294 interp_init_helper, 0);
295
296 }
297
298 void Interp::Startup2 (Bot *b)
299 {
300 bot = b;
301
302 scm_c_call_with_current_module (bot_module,
303 interp_post_startup_helper,
304 bot_module);
305 }
306
307 void
308 Interp::Shutdown()
309 {
310 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
311 "bot:exit-hook")),
312 SCM_EOL);
313 }
314
315 void
316 Interp::Execute(Bot *b, String command)
317 {
318 #ifdef MULTITHREAD
319 // We get the lock
320 pthread_mutex_lock(&mutex);
321 #endif
322
323 bot = b;
324 scm_internal_catch(SCM_BOOL_T,
325 (scm_t_catch_body) lazy_eval_string, (void *) static_cast<const char *> (command),
326 (scm_t_catch_handler) Interp::EmptyHandler, 0);
327
328 #ifdef MULTITHREAD
329 // We release the lock
330 pthread_mutex_unlock(&mutex);
331 #endif
332 }
333
334
335 void
336 Interp::LoadScript(Bot *b, String filename)
337 {
338 #ifdef MULTITHREAD
339 // We get the lock
340 pthread_mutex_lock(&mutex);
341 #endif
342 bot = b;
343 scm_internal_catch(SCM_BOOL_T,
344 (scm_t_catch_body) lazy_eval_file,
345 (void *)static_cast<const char * >(filename),
346 (scm_t_catch_handler) Interp::EmptyHandler, 0);
347 #ifdef MULTITHREAD
348 // We release the lock
349 pthread_mutex_unlock(&mutex);
350 #endif
351 }
352
353 #endif