3ea994734baba4d91b3c85550382cf0ed7845564
[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., 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
32 Bot * Interp::bot = 0;
33 #ifdef MULTITHREAD
34 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
35 #endif
36
37 typedef SCM (*SCMFunc)();
38
39 SCM
40 scm_apply_wrapper(void *data)
41 {
42 wrapper_data * wd = static_cast<wrapper_data *> (data);
43 scm_apply(wd->func, wd->args, SCM_EOL);
44 return SCM_BOOL_T;
45 }
46
47 #define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b)
48
49 void
50 Interp::Startup()
51 {
52 // Hooks
53 scm_c_define ("bot:exit-hook", scm_make_hook (scm_long2num (0)));
54
55 // Symbols
56 // bot:user-*
57 scm_c_define ("bot:user-none", scm_long2num (User::NONE));
58 scm_c_define ("bot:user-user", scm_long2num (User::USER));
59 scm_c_define ("bot:user-trusted", scm_long2num (User::TRUSTED_USER));
60 scm_c_define ("bot:user-friend", scm_long2num (User::FRIEND));
61 scm_c_define ("bot:user-master", scm_long2num (User::MASTER));
62 // sys-dir
63 scm_c_define ("bot:sys-scripts-dir",
64 scm_makfrom0str (String(PREFIX) +
65 "/share/bobotpp/scripts/"));
66 // Hooks
67 scm_c_define ("hooks/action", scm_long2num(Hook::ACTION));
68 scm_c_define ("hooks/nickname", scm_long2num(Hook::NICKNAME));
69 scm_c_define ("hooks/signoff", scm_long2num(Hook::SIGNOFF));
70 scm_c_define ("hooks/ctcp", scm_long2num(Hook::CTCP));
71 scm_c_define ("hooks/ctcp-reply", scm_long2num(Hook::CTCP_REPLY));
72 scm_c_define ("hooks/disconnect", scm_long2num(Hook::DISCONNECT));
73 scm_c_define ("hooks/flood", scm_long2num(Hook::FLOOD));
74 scm_c_define ("hooks/invite", scm_long2num(Hook::INVITE));
75 scm_c_define ("hooks/join", scm_long2num(Hook::JOIN));
76 scm_c_define ("hooks/kick", scm_long2num(Hook::KICK));
77 scm_c_define ("hooks/leave", scm_long2num(Hook::LEAVE));
78 scm_c_define ("hooks/part", scm_long2num(Hook::LEAVE));
79 scm_c_define ("hooks/mode", scm_long2num(Hook::MODE));
80 scm_c_define ("hooks/message", scm_long2num(Hook::MESSAGE));
81 // scm_c_define ("hooks/names", scm_long2num(Hook::NAMES));
82 scm_c_define ("hooks/notice", scm_long2num(Hook::NOTICE));
83 scm_c_define ("hooks/public", scm_long2num(Hook::PUBLIC));
84 scm_c_define ("hooks/public-notice", scm_long2num(Hook::PUBLIC_NOTICE));
85 scm_c_define ("hooks/raw", scm_long2num(Hook::RAW));
86 scm_c_define ("hooks/timer", scm_long2num(Hook::TIMER));
87 scm_c_define ("hooks/topic", scm_long2num(Hook::TOPIC));
88 scm_c_define ("hooks/send/public", scm_long2num (Hook::SEND_PUBLIC));
89 scm_c_define ("hooks/send/message", scm_long2num (Hook::SEND_MESSAGE));
90 scm_c_define ("hooks/send/action", scm_long2num (Hook::SEND_ACTION));
91 scm_c_define ("hooks/send/ctcp", scm_long2num (Hook::SEND_CTCP));
92
93
94 // procedures
95 bot_new_procedure ("bot:action", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
96 scm_c_define_gsubr ("bot:adduser", 5, 2, 0,
97 (SCMFunc)ScriptCommands::AddUser);
98 bot_new_procedure ("bot:addserver", (SCMFunc)ScriptCommands::Action,
99 3, 4, 0);
100 scm_c_define_gsubr ("bot:addshit", 3, 2, 0,
101 (SCMFunc)ScriptCommands::AddShit);
102 bot_new_procedure ("bot:ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
103 bot_new_procedure ("bot:cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0);
104 bot_new_procedure ("bot:deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0);
105 bot_new_procedure ("bot:delserver", (SCMFunc)ScriptCommands::DelServer,
106 1, 0, 0);
107 bot_new_procedure ("bot:deluser", (SCMFunc)ScriptCommands::DelUser, 2, 0, 0);
108 bot_new_procedure ("bot:delshit", (SCMFunc)ScriptCommands::DelShit, 2, 0, 0);
109 bot_new_procedure ("bot:deop", (SCMFunc)ScriptCommands::Deop, 2, 0, 0);
110 bot_new_procedure ("bot:die", (SCMFunc)ScriptCommands::Die, 1, 0, 0);
111 bot_new_procedure ("bot:do", (SCMFunc)ScriptCommands::Do, 1, 0, 0);
112 bot_new_procedure ("bot:invite", (SCMFunc)ScriptCommands::Invite, 2, 0, 0);
113 bot_new_procedure ("bot:join", (SCMFunc)ScriptCommands::Join, 1, 1, 0);
114 bot_new_procedure ("bot:keep", (SCMFunc)ScriptCommands::Keep, 2, 0, 0);
115 bot_new_procedure ("bot:kick", (SCMFunc)ScriptCommands::Kick, 2, 1, 0);
116 bot_new_procedure ("bot:kickban", (SCMFunc)ScriptCommands::KickBan, 2, 1, 0);
117 bot_new_procedure ("bot:lock", (SCMFunc)ScriptCommands::Lock, 1, 0, 0);
118 bot_new_procedure ("bot:logport", (SCMFunc)ScriptCommands::LogPort, 0, 0, 0);
119 bot_new_procedure ("bot:mode", (SCMFunc)ScriptCommands::Mode, 2, 0, 0);
120 bot_new_procedure ("bot:msg", (SCMFunc)ScriptCommands::Msg, 2, 0, 0);
121 bot_new_procedure ("bot:nextserver", (SCMFunc)ScriptCommands::NextServer,
122 0, 0, 0);
123 bot_new_procedure ("bot:nick", (SCMFunc)ScriptCommands::Nick, 1, 0, 0);
124 bot_new_procedure ("bot:op", (SCMFunc)ScriptCommands::Op, 2, 0, 0);
125 bot_new_procedure ("bot:part", (SCMFunc)ScriptCommands::Part, 1, 0, 0);
126 bot_new_procedure ("bot:reconnect", (SCMFunc)ScriptCommands::Reconnect,
127 0, 0, 0);
128 bot_new_procedure ("bot:say", (SCMFunc)ScriptCommands::Say, 2, 0, 0);
129 bot_new_procedure ("bot:server", (SCMFunc)ScriptCommands::Server, 1, 0, 0);
130 bot_new_procedure ("bot:setversion", (SCMFunc)ScriptCommands::SetVersion,
131 1, 0, 0);
132 bot_new_procedure ("bot:tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0);
133 bot_new_procedure ("bot:tkban", (SCMFunc)ScriptCommands::TKBan, 3, 1, 0);
134 bot_new_procedure ("bot:topic", (SCMFunc)ScriptCommands::Topic, 2, 0, 0);
135 bot_new_procedure ("bot:unlock", (SCMFunc)ScriptCommands::Unlock, 1, 0, 0);
136
137 bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname,
138 0, 0, 0);
139 bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer,
140 0, 0, 0);
141 bot_new_procedure ("bot:getserverlist",
142 (SCMFunc)ScriptCommands::getServerList, 0, 0, 0);
143 bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue,
144 0, 0, 0);
145 bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort,
146 0, 0, 0);
147 bot_new_procedure ("bot:random", (SCMFunc)ScriptCommands::random,
148 1, 0, 0);
149 bot_new_procedure ("bot:addcommand", (SCMFunc)ScriptCommands::addCommand,
150 5, 0, 0);
151 bot_new_procedure ("bot:delcommand", (SCMFunc)ScriptCommands::delCommand,
152 1, 0, 0);
153 bot_new_procedure ("bot:addhook", (SCMFunc)ScriptCommands::AddHook,
154 3, 3, 0);
155 bot_new_procedure ("bot:addtimer", (SCMFunc)ScriptCommands::AddTimer,
156 2, 0, 0);
157 bot_new_procedure ("bot:deltimer", (SCMFunc)ScriptCommands::DelTimer,
158 1, 0, 0);
159
160 // "Low Level" Message functuions
161 scm_c_define_gsubr ("bot:send-CTCP", 3, 0, 0,
162 (SCMFunc)ScriptCommands::sendCTCP);
163
164 // load bobot-utils
165 scm_primitive_load
166 (scm_makfrom0str (String(PREFIX) +
167 "/share/bobotpp/scripts/bobot-utils.scm"));
168
169
170 }
171
172 void
173 Interp::Shutdown()
174 {
175 scm_c_run_hook (gh_lookup ("bot:exit-hook"), SCM_EOL);
176 }
177
178 void
179 Interp::Execute(Bot *b, String command)
180 {
181 #ifdef MULTITHREAD
182 // We get the lock
183 pthread_mutex_lock(&mutex);
184 #endif
185 bot = b;
186 gh_eval_str_with_catch(command, ErrorHandler);
187 #ifdef MULTITHREAD
188 // We release the lock
189 pthread_mutex_unlock(&mutex);
190 #endif
191 }
192
193 void
194 Interp::LoadScript(Bot *b, String filename)
195 {
196 #ifdef MULTITHREAD
197 // We get the lock
198 pthread_mutex_lock(&mutex);
199 #endif
200 bot = b;
201 gh_eval_file_with_catch(filename, ErrorHandler);
202 #ifdef MULTITHREAD
203 // We release the lock
204 pthread_mutex_unlock(&mutex);
205 #endif
206 }
207
208 SCM
209 Interp::ErrorHandler(void *data, SCM tag, SCM throw_args)
210 {
211 bot->botInterp->ScriptLog(throw_args);
212 return SCM_BOOL_F;
213 }
214
215 #endif