1 // Interp.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
28 #include "BotInterp.H"
32 // static class member initial definitions
33 Bot * Interp::bot = 0;
34 SCM Interp::bot_module = 0;
37 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
40 typedef SCM (*SCMFunc)();
43 scm_apply_wrapper(void *data)
46 // pthread_mutex_lock (&Interp::mutex);
49 wrapper_data * wd = static_cast<wrapper_data *> (data);
50 scm_apply(wd->func, wd->args, SCM_EOL);
53 // pthread_mutex_unlock (&Interp::mutex);
59 #define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b); scm_c_export (a, 0)
60 #define scm_c_define_gsubr(a, b, c, d, e) scm_c_define_gsubr (a, b, c, d, e); scm_c_export (a, 0)
61 #define scm_c_define(a, b) scm_c_define (a, b); scm_c_export (a, 0)
64 interp_init_helper (void* unused)
66 scm_c_use_module ("guile-user");
68 scm_c_define ("bot:exit-hook", scm_make_hook (scm_long2num (0)));
72 scm_c_define ("bot:user-none", scm_long2num (User::NONE));
73 scm_c_define ("bot:user-user", scm_long2num (User::USER));
74 scm_c_define ("bot:user-trusted", scm_long2num (User::TRUSTED_USER));
75 scm_c_define ("bot:user-friend", scm_long2num (User::FRIEND));
76 scm_c_define ("bot:user-master", scm_long2num (User::MASTER));
79 scm_c_define ("bot:protection/none", scm_long2num (User::NO_PROT));
80 scm_c_define ("bot:protection/no-ban", scm_long2num (User::NO_BAN));
81 scm_c_define ("bot:protection/no-kick", scm_long2num (User::NO_KICK));
82 scm_c_define ("bot:protection/no-deop", scm_long2num (User::NO_DEOP));
85 scm_c_define ("bot:aop/yes", scm_long2num (true));
86 scm_c_define ("bot:aop/no", scm_long2num (false));
89 scm_c_define ("bot:sys-scripts-dir",
90 scm_makfrom0str (String(PREFIX) +
91 "/share/bobotpp/scripts/"));
93 scm_c_define ("hooks/action", scm_long2num(Hook::ACTION));
94 scm_c_define ("hooks/nickname", scm_long2num(Hook::NICKNAME));
95 scm_c_define ("hooks/signoff", scm_long2num(Hook::SIGNOFF));
96 scm_c_define ("hooks/ctcp", scm_long2num(Hook::CTCP));
97 scm_c_define ("hooks/ctcp-reply", scm_long2num(Hook::CTCP_REPLY));
98 scm_c_define ("hooks/disconnect", scm_long2num(Hook::DISCONNECT));
99 scm_c_define ("hooks/flood", scm_long2num(Hook::FLOOD));
100 scm_c_define ("hooks/invite", scm_long2num(Hook::INVITE));
101 scm_c_define ("hooks/join", scm_long2num(Hook::JOIN));
102 scm_c_define ("hooks/kick", scm_long2num(Hook::KICK));
103 scm_c_define ("hooks/leave", scm_long2num(Hook::LEAVE));
104 scm_c_define ("hooks/part", scm_long2num(Hook::LEAVE));
105 scm_c_define ("hooks/mode", scm_long2num(Hook::MODE));
106 scm_c_define ("hooks/message", scm_long2num(Hook::MESSAGE));
107 scm_c_define ("hooks/notice", scm_long2num(Hook::NOTICE));
108 scm_c_define ("hooks/public", scm_long2num(Hook::PUBLIC));
109 scm_c_define ("hooks/public-notice", scm_long2num(Hook::PUBLIC_NOTICE));
110 scm_c_define ("hooks/raw", scm_long2num(Hook::RAW));
111 scm_c_define ("hooks/timer", scm_long2num(Hook::TIMER));
112 scm_c_define ("hooks/topic", scm_long2num(Hook::TOPIC));
114 scm_c_define ("hooks/send/public", scm_long2num (Hook::SEND_PUBLIC));
115 scm_c_define ("hooks/send/message", scm_long2num (Hook::SEND_MESSAGE));
116 scm_c_define ("hooks/send/action", scm_long2num (Hook::SEND_ACTION));
117 scm_c_define ("hooks/send/ctcp", scm_long2num (Hook::SEND_CTCP));
119 scm_c_define ("hooks/dcc/chat-begin",
120 scm_long2num (Hook::DCC_CHAT_BEGIN));
121 scm_c_define ("hooks/dcc/chat-message",
122 scm_long2num (Hook::DCC_CHAT_MESSAGE));
125 bot_new_procedure ("bot:action", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
126 scm_c_define_gsubr ("bot:adduser", 5, 2, 0,
127 (SCMFunc)ScriptCommands::AddUser);
128 bot_new_procedure ("bot:addserver", (SCMFunc)ScriptCommands::Action,
130 scm_c_define_gsubr ("bot:addshit", 3, 2, 0,
131 (SCMFunc)ScriptCommands::AddShit);
132 bot_new_procedure ("bot:ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
133 bot_new_procedure ("bot:change-command-level",
134 (SCMFunc)ScriptCommands::ChangeCommandLevel,
136 bot_new_procedure ("bot:cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0);
137 bot_new_procedure ("bot:deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0);
138 bot_new_procedure ("bot:delserver", (SCMFunc)ScriptCommands::DelServer,
140 bot_new_procedure ("bot:deluser", (SCMFunc)ScriptCommands::DelUser, 2, 0, 0);
141 bot_new_procedure ("bot:delshit", (SCMFunc)ScriptCommands::DelShit, 2, 0, 0);
142 bot_new_procedure ("bot:deop", (SCMFunc)ScriptCommands::Deop, 2, 0, 0);
143 bot_new_procedure ("bot:die", (SCMFunc)ScriptCommands::Die, 1, 0, 0);
144 bot_new_procedure ("bot:do", (SCMFunc)ScriptCommands::Do, 1, 0, 0);
145 bot_new_procedure ("bot:invite", (SCMFunc)ScriptCommands::Invite, 2, 0, 0);
146 bot_new_procedure ("bot:join", (SCMFunc)ScriptCommands::Join, 1, 1, 0);
147 bot_new_procedure ("bot:keep", (SCMFunc)ScriptCommands::Keep, 2, 0, 0);
148 bot_new_procedure ("bot:kick", (SCMFunc)ScriptCommands::Kick, 2, 1, 0);
149 bot_new_procedure ("bot:kickban", (SCMFunc)ScriptCommands::KickBan, 2, 1, 0);
150 bot_new_procedure ("bot:lock", (SCMFunc)ScriptCommands::Lock, 1, 0, 0);
151 bot_new_procedure ("bot:logport", (SCMFunc)ScriptCommands::LogPort, 0, 0, 0);
152 bot_new_procedure ("bot:mode", (SCMFunc)ScriptCommands::Mode, 2, 0, 0);
153 bot_new_procedure ("bot:msg", (SCMFunc)ScriptCommands::Msg, 2, 0, 0);
154 bot_new_procedure ("bot:nextserver", (SCMFunc)ScriptCommands::NextServer,
156 bot_new_procedure ("bot:nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0);
158 scm_c_define_gsubr ("bot:notice", 2, 0, 0,
159 (SCMFunc)ScriptCommands::sendNotice);
161 bot_new_procedure ("bot:op", (SCMFunc)ScriptCommands::Op, 2, 0, 0);
162 bot_new_procedure ("bot:part", (SCMFunc)ScriptCommands::Part, 1, 0, 0);
163 bot_new_procedure ("bot:reconnect", (SCMFunc)ScriptCommands::Reconnect,
165 bot_new_procedure ("bot:say", (SCMFunc)ScriptCommands::Say, 2, 0, 0);
166 bot_new_procedure ("bot:server", (SCMFunc)ScriptCommands::Server, 1, 0, 0);
167 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
168 (SCMFunc)ScriptCommands::SetFloodRate);
169 bot_new_procedure ("bot:setversion", (SCMFunc)ScriptCommands::SetVersion,
171 bot_new_procedure ("bot:tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0);
172 bot_new_procedure ("bot:tkban", (SCMFunc)ScriptCommands::TKBan, 3, 1, 0);
173 bot_new_procedure ("bot:topic", (SCMFunc)ScriptCommands::Topic, 2, 0, 0);
174 bot_new_procedure ("bot:unlock", (SCMFunc)ScriptCommands::Unlock, 1, 0, 0);
176 bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname,
178 bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer,
180 bot_new_procedure ("bot:getserverlist",
181 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
182 bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue,
184 bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort,
186 bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random,
188 bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand,
190 bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand,
192 bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook,
194 bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer,
196 bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer,
199 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
200 (SCMFunc)ScriptCommands::sendDCCChatMessage);
202 // "Low Level" Message functuions
203 scm_c_define_gsubr ("bot:send-CTCP", 3, 0, 0,
204 (SCMFunc)ScriptCommands::sendCTCP);
207 #undef bot_new_procedure
208 #undef scm_c_define_gsubr
213 interp_post_startup_helper (void *bot_module)
215 SCM module = static_cast<SCM> (bot_module);
216 scm_c_define ("the-bot-module", module);
217 scm_c_export ("the-bot-module", 0);
221 (scm_makfrom0str (String(PREFIX) +
222 "/share/bobotpp/scripts/bobot-utils.scm"));
229 bot_module = scm_c_define_module ("the-bot-module",
230 interp_init_helper, 0);
232 scm_c_call_with_current_module (bot_module,
233 interp_post_startup_helper,
240 scm_c_run_hook (gh_lookup ("bot:exit-hook"), SCM_EOL);
244 Interp::Execute(Bot *b, String command)
248 pthread_mutex_lock(&mutex);
252 gh_eval_str_with_catch (command, ErrorHandler);
255 // We release the lock
256 pthread_mutex_unlock(&mutex);
260 void load_script_helper (void* file)
262 String filename = *static_cast<String*> (file);
263 scm_c_use_module ("guile-user");
264 gh_eval_file_with_catch(filename, Interp::ErrorHandler);
268 Interp::LoadScript(Bot *b, String filename)
272 pthread_mutex_lock(&mutex);
275 //scm_c_define_module ("", load_script_helper, &filename);
276 gh_eval_file_with_catch(filename, ErrorHandler);
278 // We release the lock
279 pthread_mutex_unlock(&mutex);
284 Interp::ErrorHandler(void *data, SCM tag, SCM throw_args)
286 bot->botInterp->ScriptLog(throw_args);