[project @ 2004-04-28 03:33:52 by unknown_lamer]
[clinton/bobotpp.git] / source / Interp.C
CommitLineData
cb21075d 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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
ce02032f 32// static class member initial definitions
cb21075d 33Bot * Interp::bot = 0;
ce02032f 34SCM Interp::bot_module = 0;
cb21075d 35#ifdef MULTITHREAD
36pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
37#endif
38
39typedef SCM (*SCMFunc)();
40
41SCM
42scm_apply_wrapper(void *data)
43{
c3ecc559 44#ifdef MULTITHREAD
45 // pthread_mutex_lock (&Interp::mutex);
46#endif
47
cb21075d 48 wrapper_data * wd = static_cast<wrapper_data *> (data);
49 scm_apply(wd->func, wd->args, SCM_EOL);
c3ecc559 50
51#ifdef MULTITHREAD
52 // pthread_mutex_unlock (&Interp::mutex);
53#endif
54
cb21075d 55 return SCM_BOOL_T;
56}
57
ce02032f 58#define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b); scm_c_export (a, 0)
59#define scm_c_define_gsubr(a, b, c, d, e) scm_c_define_gsubr (a, b, c, d, e); scm_c_export (a, 0)
60#define scm_c_define(a, b) scm_c_define (a, b); scm_c_export (a, 0)
cb21075d 61
62void
ce02032f 63interp_init_helper (void* unused)
cb21075d 64{
ce02032f 65 scm_c_use_module ("guile-user");
91dddabd 66 // Hooks
67 scm_c_define ("bot:exit-hook", scm_make_hook (scm_long2num (0)));
cb21075d 68
439869bf 69 // Symbols
70 // bot:user-*
71 scm_c_define ("bot:user-none", scm_long2num (User::NONE));
72 scm_c_define ("bot:user-user", scm_long2num (User::USER));
73 scm_c_define ("bot:user-trusted", scm_long2num (User::TRUSTED_USER));
74 scm_c_define ("bot:user-friend", scm_long2num (User::FRIEND));
75 scm_c_define ("bot:user-master", scm_long2num (User::MASTER));
76 // sys-dir
77 scm_c_define ("bot:sys-scripts-dir",
ad529fde 78 scm_makfrom0str (String(PREFIX) +
79 "/share/bobotpp/scripts/"));
439869bf 80 // Hooks
cb21075d 81 scm_c_define ("hooks/action", scm_long2num(Hook::ACTION));
82 scm_c_define ("hooks/nickname", scm_long2num(Hook::NICKNAME));
83 scm_c_define ("hooks/signoff", scm_long2num(Hook::SIGNOFF));
84 scm_c_define ("hooks/ctcp", scm_long2num(Hook::CTCP));
85 scm_c_define ("hooks/ctcp-reply", scm_long2num(Hook::CTCP_REPLY));
86 scm_c_define ("hooks/disconnect", scm_long2num(Hook::DISCONNECT));
87 scm_c_define ("hooks/flood", scm_long2num(Hook::FLOOD));
88 scm_c_define ("hooks/invite", scm_long2num(Hook::INVITE));
89 scm_c_define ("hooks/join", scm_long2num(Hook::JOIN));
90 scm_c_define ("hooks/kick", scm_long2num(Hook::KICK));
91 scm_c_define ("hooks/leave", scm_long2num(Hook::LEAVE));
92 scm_c_define ("hooks/part", scm_long2num(Hook::LEAVE));
93 scm_c_define ("hooks/mode", scm_long2num(Hook::MODE));
94 scm_c_define ("hooks/message", scm_long2num(Hook::MESSAGE));
cb21075d 95 scm_c_define ("hooks/notice", scm_long2num(Hook::NOTICE));
96 scm_c_define ("hooks/public", scm_long2num(Hook::PUBLIC));
97 scm_c_define ("hooks/public-notice", scm_long2num(Hook::PUBLIC_NOTICE));
98 scm_c_define ("hooks/raw", scm_long2num(Hook::RAW));
99 scm_c_define ("hooks/timer", scm_long2num(Hook::TIMER));
100 scm_c_define ("hooks/topic", scm_long2num(Hook::TOPIC));
6530edbf 101 // send hooks
fed59248 102 scm_c_define ("hooks/send/public", scm_long2num (Hook::SEND_PUBLIC));
103 scm_c_define ("hooks/send/message", scm_long2num (Hook::SEND_MESSAGE));
104 scm_c_define ("hooks/send/action", scm_long2num (Hook::SEND_ACTION));
105 scm_c_define ("hooks/send/ctcp", scm_long2num (Hook::SEND_CTCP));
6530edbf 106 // dcc hooks
107 scm_c_define ("hooks/dcc/chat-begin",
108 scm_long2num (Hook::DCC_CHAT_BEGIN));
109 scm_c_define ("hooks/dcc/chat-message",
110 scm_long2num (Hook::DCC_CHAT_MESSAGE));
439869bf 111
112 // procedures
113 bot_new_procedure ("bot:action", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
114 scm_c_define_gsubr ("bot:adduser", 5, 2, 0,
115 (SCMFunc)ScriptCommands::AddUser);
116 bot_new_procedure ("bot:addserver", (SCMFunc)ScriptCommands::Action,
117 3, 4, 0);
118 scm_c_define_gsubr ("bot:addshit", 3, 2, 0,
119 (SCMFunc)ScriptCommands::AddShit);
120 bot_new_procedure ("bot:ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
4edefeb6 121 bot_new_procedure ("bot:change-command-level", (SCMFunc)ScriptCommands::ChangeCommandLevel,
122 2, 0, 0);
439869bf 123 bot_new_procedure ("bot:cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0);
124 bot_new_procedure ("bot:deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0);
125 bot_new_procedure ("bot:delserver", (SCMFunc)ScriptCommands::DelServer,
126 1, 0, 0);
127 bot_new_procedure ("bot:deluser", (SCMFunc)ScriptCommands::DelUser, 2, 0, 0);
128 bot_new_procedure ("bot:delshit", (SCMFunc)ScriptCommands::DelShit, 2, 0, 0);
129 bot_new_procedure ("bot:deop", (SCMFunc)ScriptCommands::Deop, 2, 0, 0);
130 bot_new_procedure ("bot:die", (SCMFunc)ScriptCommands::Die, 1, 0, 0);
131 bot_new_procedure ("bot:do", (SCMFunc)ScriptCommands::Do, 1, 0, 0);
132 bot_new_procedure ("bot:invite", (SCMFunc)ScriptCommands::Invite, 2, 0, 0);
133 bot_new_procedure ("bot:join", (SCMFunc)ScriptCommands::Join, 1, 1, 0);
134 bot_new_procedure ("bot:keep", (SCMFunc)ScriptCommands::Keep, 2, 0, 0);
135 bot_new_procedure ("bot:kick", (SCMFunc)ScriptCommands::Kick, 2, 1, 0);
136 bot_new_procedure ("bot:kickban", (SCMFunc)ScriptCommands::KickBan, 2, 1, 0);
137 bot_new_procedure ("bot:lock", (SCMFunc)ScriptCommands::Lock, 1, 0, 0);
138 bot_new_procedure ("bot:logport", (SCMFunc)ScriptCommands::LogPort, 0, 0, 0);
139 bot_new_procedure ("bot:mode", (SCMFunc)ScriptCommands::Mode, 2, 0, 0);
140 bot_new_procedure ("bot:msg", (SCMFunc)ScriptCommands::Msg, 2, 0, 0);
141 bot_new_procedure ("bot:nextserver", (SCMFunc)ScriptCommands::NextServer,
142 0, 0, 0);
143 bot_new_procedure ("bot:nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0);
144 bot_new_procedure ("bot:op", (SCMFunc)ScriptCommands::Op, 2, 0, 0);
145 bot_new_procedure ("bot:part", (SCMFunc)ScriptCommands::Part, 1, 0, 0);
146 bot_new_procedure ("bot:reconnect", (SCMFunc)ScriptCommands::Reconnect,
147 0, 0, 0);
148 bot_new_procedure ("bot:say", (SCMFunc)ScriptCommands::Say, 2, 0, 0);
149 bot_new_procedure ("bot:server", (SCMFunc)ScriptCommands::Server, 1, 0, 0);
e171dcce 150 scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
151 (SCMFunc)ScriptCommands::SetFloodRate);
439869bf 152 bot_new_procedure ("bot:setversion", (SCMFunc)ScriptCommands::SetVersion,
153 1, 0, 0);
154 bot_new_procedure ("bot:tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0);
155 bot_new_procedure ("bot:tkban", (SCMFunc)ScriptCommands::TKBan, 3, 1, 0);
156 bot_new_procedure ("bot:topic", (SCMFunc)ScriptCommands::Topic, 2, 0, 0);
157 bot_new_procedure ("bot:unlock", (SCMFunc)ScriptCommands::Unlock, 1, 0, 0);
158
159 bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname,
160 0, 0, 0);
161 bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer,
162 0, 0, 0);
163 bot_new_procedure ("bot:getserverlist",
164 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
165 bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue,
166 0, 0, 0);
167 bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort,
168 0, 0, 0);
169 bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random,
170 1, 0, 0);
171 bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand,
172 5, 0, 0);
173 bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand,
174 1, 0, 0);
175 bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook,
fd7440f1 176 3, 3, 0);
439869bf 177 bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer,
178 2, 0, 0);
179 bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer,
180 1, 0, 0);
e07b6b46 181
b93600f6 182 scm_c_define_gsubr ("bot:dcc-chat-send", 2, 0, 0,
0b7a49e2 183 (SCMFunc)ScriptCommands::sendDCCChatMessage);
184
e07b6b46 185 // "Low Level" Message functuions
186 scm_c_define_gsubr ("bot:send-CTCP", 3, 0, 0,
187 (SCMFunc)ScriptCommands::sendCTCP);
ce02032f 188}
189
190#undef bot_new_procedure
191#undef scm_c_define_gsubr
192#undef scm_c_define
193
e07b6b46 194
ce02032f 195SCM
196interp_post_startup_helper (void *bot_module)
197{
198 SCM module = static_cast<SCM> (bot_module);
199 scm_c_define ("the-bot-module", module);
200 scm_c_export ("the-bot-module", 0);
439869bf 201 // load bobot-utils
202 scm_primitive_load
203 (scm_makfrom0str (String(PREFIX) +
204 "/share/bobotpp/scripts/bobot-utils.scm"));
ce02032f 205 return 0;
206}
439869bf 207
ce02032f 208void
209Interp::Startup()
210{
211 bot_module = scm_c_define_module ("the-bot-module",
212 interp_init_helper, 0);
213 scm_c_call_with_current_module (bot_module,
214 interp_post_startup_helper,
215 bot_module);
cb21075d 216}
217
218void
219Interp::Shutdown()
91dddabd 220{
221 scm_c_run_hook (gh_lookup ("bot:exit-hook"), SCM_EOL);
222}
cb21075d 223
224void
225Interp::Execute(Bot *b, String command)
226{
227#ifdef MULTITHREAD
228 // We get the lock
229 pthread_mutex_lock(&mutex);
230#endif
231 bot = b;
232 gh_eval_str_with_catch(command, ErrorHandler);
233#ifdef MULTITHREAD
234 // We release the lock
235 pthread_mutex_unlock(&mutex);
236#endif
237}
238
ce02032f 239void load_script_helper (void* file)
240{
241 String filename = *static_cast<String*> (file);
242 scm_c_use_module ("guile-user");
243 gh_eval_file_with_catch(filename, Interp::ErrorHandler);
244}
245
cb21075d 246void
247Interp::LoadScript(Bot *b, String filename)
248{
249#ifdef MULTITHREAD
250 // We get the lock
251 pthread_mutex_lock(&mutex);
252#endif
253 bot = b;
ce02032f 254 //scm_c_define_module ("", load_script_helper, &filename);
cb21075d 255 gh_eval_file_with_catch(filename, ErrorHandler);
256#ifdef MULTITHREAD
257 // We release the lock
258 pthread_mutex_unlock(&mutex);
259#endif
260}
261
262SCM
263Interp::ErrorHandler(void *data, SCM tag, SCM throw_args)
264{
265 bot->botInterp->ScriptLog(throw_args);
266 return SCM_BOOL_F;
267}
268
269#endif