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