1 // Interp.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005,2008 Clinton Ebadi
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.
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.
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
30 #include <libguile/hooks.h>
34 #include "BotInterp.H"
37 #include "ScriptCommands.H"
38 #include "ShitEntry.H"
41 // static class member initial definitions
42 Bot * Interp::bot = 0;
43 SCM Interp::bot_module = 0;
46 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
49 typedef scm_t_subr SCMFunc;
52 Interp::ScmApplyWrapper (void *data)
55 // pthread_mutex_lock (&Interp::mutex);
58 wrapper_data * wd = static_cast<wrapper_data *> (data);
59 scm_apply(wd->func, wd->args, SCM_EOL);
62 // pthread_mutex_unlock (&Interp::mutex);
69 Interp::LazyHandler (void *data, SCM tag, SCM throw_args)
71 SCM log_port = Interp::bot->botInterp->ScriptLog();
72 SCM eport = scm_set_current_error_port(log_port);
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 */
82 Interp::EmptyHandler(void *data, SCM tag, SCM args)
84 return SCM_UNSPECIFIED;
89 Interp::LazyApplyWrapper(void *data)
91 return scm_c_with_throw_handler(SCM_BOOL_T,
92 (scm_t_catch_body) Interp::ScmApplyWrapper,
94 (scm_t_catch_handler) Interp::LazyHandler,
101 lazy_eval_file(char *filename)
103 return scm_c_with_throw_handler(SCM_BOOL_T,
104 (scm_t_catch_body) scm_c_primitive_load_path,
106 (scm_t_catch_handler) Interp::LazyHandler,
112 lazy_eval_string(char *str)
114 return scm_c_with_throw_handler(SCM_BOOL_T,
115 (scm_t_catch_body) scm_c_eval_string,
117 (scm_t_catch_handler) Interp::LazyHandler,
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)
129 interp_init_helper (void* unused)
131 scm_c_use_module ("guile-user");
133 scm_c_define ("bot:exit-hook", scm_make_hook (scm_from_int (0)));
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));
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));
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));
156 scm_c_define ("bot:aop/yes", scm_from_int (true));
157 scm_c_define ("bot:aop/no", scm_from_int (false));
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));
167 scm_c_define ("bot:sys-scripts-dir",
168 scm_from_locale_string ((String(PREFIX) +
169 "/share/bobotpp/scripts/").c_str ()));
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));
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));
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));
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,
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,
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,
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,
243 bot_new_procedure ("bot:nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0);
245 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
246 (SCMFunc)ScriptCommands::Notice);
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,
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,
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);
265 bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname,
267 bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer,
269 bot_new_procedure ("bot:getserverlist",
270 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
271 bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue,
273 bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort,
275 bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random,
277 bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand,
279 bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand,
281 bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook,
283 bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer,
285 bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer,
288 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
289 (SCMFunc)ScriptCommands::sendDCCChatMessage);
292 #undef bot_new_procedure
293 #undef scm_c_define_gsubr
298 interp_post_startup_helper (void *bot_module)
300 SCM module = static_cast<SCM> (bot_module);
301 scm_c_define ("the-bot-module", module);
302 scm_c_export ("the-bot-module", 0);
305 return scm_primitive_load_path
306 (scm_from_locale_string ((String(PREFIX) +
307 "/share/bobotpp/scripts/bobot-utils.scm").c_str ()));
313 bot_module = scm_c_define_module ("the-bot-module",
314 interp_init_helper, 0);
318 void Interp::Startup2 (Bot *b)
322 scm_c_call_with_current_module (bot_module,
323 interp_post_startup_helper,
330 scm_c_run_hook (scm_variable_ref (scm_c_module_lookup (bot_module,
336 Interp::Execute(Bot *b, String command)
340 pthread_mutex_lock(&mutex);
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,
352 // We release the lock
353 pthread_mutex_unlock(&mutex);
359 Interp::LoadScript(Bot *b, String filename)
363 pthread_mutex_lock(&mutex);
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,
373 // We release the lock
374 pthread_mutex_unlock(&mutex);