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