Commit | Line | Data |
---|---|---|
cb21075d | 1 | // ScriptCommands.C -*- C++ -*- |
2 | // Copyright (c) 1998 Etienne BERNARD | |
c1ea3a86 | 3 | // Copyright (C) 2002,2005,2008,2009 Clinton Ebadi |
cb21075d | 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 | // 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 | |
ae97d6ec | 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | // 02110-1301, USA. | |
cb21075d | 19 | |
20 | #ifdef HAVE_CONFIG_H | |
21 | #include "config.h" | |
22 | #endif | |
23 | ||
24 | #ifdef USESCRIPTS | |
25 | ||
cb21075d | 26 | #include "ScriptCommands.H" |
cfa82921 | 27 | |
28 | #include <libguile.h> | |
29 | ||
30 | #include "Bot.H" | |
31 | #include "BotInterp.H" | |
c1ea3a86 | 32 | #include "ChannelList.H" |
5aec4622 | 33 | #include "Commands.H" |
cfa82921 | 34 | #include "DCCManager.H" |
35 | #include "DCCPerson.H" | |
36 | #include "Interp.H" | |
5aec4622 | 37 | #include "Message.H" |
cfa82921 | 38 | #include "Parser.H" |
39 | #include "Server.H" | |
40 | #include "ServerConnection.H" | |
41 | #include "ServerList.H" | |
42 | #include "ServerQueue.H" | |
43 | #include "User.H" | |
44 | #include "Utils.H" | |
cb21075d | 45 | |
46af1667 | 46 | #define VERIFY_STRING(par) if (!scm_is_string((par))) \ |
47 | return scm_from_int(-17) | |
cb21075d | 48 | |
49 | #define VERIFY_NUMBER(par) if (!SCM_NUMBERP((par))) \ | |
46af1667 | 50 | return scm_from_int(-17) |
cb21075d | 51 | |
52 | SCM | |
53 | ScriptCommands::Action(SCM channel, SCM message) | |
54 | { | |
55 | VERIFY_STRING(channel); | |
56 | VERIFY_STRING(message); | |
a6339323 | 57 | Message m = Commands::Action(Interp::bot, Utils::scm2str(channel), |
58 | Utils::scm2str(message)); | |
46af1667 | 59 | return scm_from_int(m.getCode()); |
cb21075d | 60 | } |
61 | ||
62 | SCM | |
63 | ScriptCommands::AddUser(SCM who, SCM maskChannel, SCM level, | |
64 | SCM prot, SCM aop, SCM expire, SCM password) | |
65 | { | |
66 | // It segfaults when not online, but otherwise appears to work | |
67 | VERIFY_STRING (who); | |
68 | VERIFY_STRING (maskChannel); | |
69 | VERIFY_NUMBER (level); | |
70 | ||
a6339323 | 71 | String wwho = Utils::scm2str (who); |
72 | String mask = Utils::scm2str (maskChannel); | |
cb21075d | 73 | String passwd; |
74 | std::time_t eexpire; | |
75 | ||
76 | if (SCM_UNBNDP (password)) | |
77 | passwd = ""; | |
78 | else | |
79 | { | |
80 | VERIFY_STRING (password); | |
a6339323 | 81 | passwd = Utils::scm2str (password); |
cb21075d | 82 | } |
83 | if (SCM_UNBNDP (expire)) | |
84 | eexpire = -1; | |
85 | else | |
86 | { | |
87 | VERIFY_STRING (expire); | |
a6339323 | 88 | eexpire = Utils::str2time (Utils::scm2str (expire)); |
cb21075d | 89 | if (!eexpire) eexpire = -1; |
90 | } | |
91 | ||
46af1667 | 92 | int protect = scm_to_int (prot); |
93 | bool aaop = scm_is_true (aop); | |
94 | int llevel = scm_to_int (level); | |
cb21075d | 95 | |
96 | Message m = Commands::AddUser (Interp::bot, wwho, mask, llevel, | |
97 | protect, aaop, eexpire, passwd); | |
98 | ||
46af1667 | 99 | return scm_from_int(m.getCode ()); |
cb21075d | 100 | } |
101 | ||
102 | SCM | |
103 | ScriptCommands::AddServer(SCM servername, SCM port) | |
104 | { | |
105 | int p = 6667; | |
106 | if (SCM_NUMBERP(port)) | |
46af1667 | 107 | p = scm_to_int (port); |
cb21075d | 108 | Message m = Commands::AddServer(Interp::bot, |
a6339323 | 109 | Utils::scm2str(servername), |
cb21075d | 110 | p); |
46af1667 | 111 | return scm_from_int(m.getCode()); |
cb21075d | 112 | } |
113 | ||
114 | SCM | |
115 | ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level, | |
116 | SCM expiration, SCM reason) | |
117 | { | |
118 | // This appears to work...not much testing though | |
119 | VERIFY_STRING (mask); | |
120 | VERIFY_STRING (maskChannel); | |
121 | VERIFY_NUMBER (level); | |
a6339323 | 122 | String mmask = Utils::scm2str (mask); |
123 | String mmaskChannel = Utils::scm2str (maskChannel); | |
46af1667 | 124 | int llevel = scm_to_int (level); |
cb21075d | 125 | std::time_t expire; |
126 | String rreason; | |
127 | ||
128 | if (SCM_UNBNDP (expiration)) | |
129 | expire = -1; | |
130 | else | |
131 | { | |
132 | VERIFY_STRING (expiration); | |
a6339323 | 133 | expire = Utils::str2time (Utils::scm2str (expiration)); |
cb21075d | 134 | if (!expire) expire = -1; |
135 | } | |
136 | if (SCM_UNBNDP (reason)) | |
137 | rreason = "You're on my shitlist, lamer"; | |
138 | else | |
139 | { | |
140 | VERIFY_STRING (reason); | |
a6339323 | 141 | rreason = Utils::scm2str (reason); |
cb21075d | 142 | } |
143 | Message m = Commands::AddShit (Interp::bot, mmask, mmaskChannel, | |
144 | llevel, expire, rreason); | |
145 | ||
46af1667 | 146 | return scm_from_int(m.getCode ()); |
cb21075d | 147 | } |
148 | ||
149 | SCM | |
150 | ScriptCommands::Ban(SCM channel, SCM who) | |
151 | { | |
152 | VERIFY_STRING(channel); | |
153 | VERIFY_STRING(who); | |
a6339323 | 154 | Message m = Commands::Ban(Interp::bot, Utils::scm2str(channel), |
155 | Utils::scm2str(who)); | |
46af1667 | 156 | return scm_from_int(m.getCode()); |
cb21075d | 157 | } |
158 | ||
4edefeb6 | 159 | SCM |
160 | ScriptCommands::ChangeCommandLevel(SCM command, SCM level) | |
161 | { | |
162 | VERIFY_STRING (command); | |
163 | VERIFY_NUMBER (level); | |
164 | ||
46af1667 | 165 | std::string ccommand = scm_to_locale_string (command); |
166 | unsigned int llevel = scm_to_uint (level); | |
4edefeb6 | 167 | |
168 | if (llevel > 4) | |
169 | return SCM_BOOL_F; | |
170 | ||
171 | std::map<std::string, class userFunction*, | |
172 | std::less<std::string> >::const_iterator uf_iter | |
173 | = Interp::bot->userFunctions.find (ccommand); | |
174 | userFunction * f = 0; | |
175 | ||
176 | if (uf_iter != Interp::bot->userFunctions.end ()) | |
177 | f = uf_iter->second; | |
178 | else | |
179 | return SCM_BOOL_F; | |
180 | ||
181 | f->minLevel = llevel; | |
182 | return SCM_BOOL_T; | |
183 | } | |
184 | ||
c1ea3a86 | 185 | |
186 | void | |
187 | ScriptCommands::collect_channel_users::operator() (SCM *list, User user) | |
188 | const | |
189 | { | |
190 | *list = scm_cons (scm_list_n (Utils::str2scm (user.get_nick()), | |
191 | Utils::str2scm (user.get_userhost ()), | |
192 | scm_from_int (user.get_mode ()), | |
193 | SCM_UNDEFINED), | |
194 | *list); | |
195 | } | |
196 | ||
197 | SCM | |
198 | ScriptCommands::ChannelUsers (SCM channel_name) | |
199 | { | |
200 | VERIFY_STRING (channel_name); | |
201 | ||
202 | Channel *channel = Interp::bot->channelList->getChannel (Utils::scm2str (channel_name)); | |
203 | ||
204 | if (!channel) | |
205 | return SCM_BOOL_F; | |
206 | ||
207 | SCM* list = (SCM*)scm_gc_malloc (sizeof (SCM), "ScriptCommands::ChannelUsers"); | |
208 | *list = SCM_EOL; | |
209 | ||
210 | collect_channel_users c; | |
211 | ||
212 | channel->for_each_channel_users (std::bind1st (c, list)); | |
213 | ||
214 | scm_remember_upto_here_1 (list); | |
215 | return scm_reverse_x (*list, SCM_UNDEFINED); | |
216 | } | |
217 | ||
c6e7af05 | 218 | SCM |
219 | ScriptCommands::CTCP(SCM to, SCM command , SCM message) | |
220 | { | |
221 | VERIFY_STRING(to); | |
222 | VERIFY_STRING(command); | |
223 | VERIFY_STRING(message); | |
224 | ||
225 | Commands::CTCP (Interp::bot, Utils::scm2str (to), | |
226 | Utils::scm2str (command), | |
227 | Utils::scm2str (message)); | |
228 | ||
229 | return SCM_UNSPECIFIED; | |
230 | } | |
231 | ||
232 | SCM | |
233 | ScriptCommands::CTCPReply (SCM to, SCM command , SCM message) | |
234 | { | |
235 | VERIFY_STRING(to); | |
236 | VERIFY_STRING(command); | |
237 | VERIFY_STRING(message); | |
238 | ||
239 | Commands::CTCPReply (Interp::bot, Utils::scm2str (to), | |
240 | Utils::scm2str (command), | |
241 | Utils::scm2str (message)); | |
242 | ||
243 | return SCM_UNSPECIFIED; | |
244 | } | |
245 | ||
cb21075d | 246 | SCM |
247 | ScriptCommands::Cycle(SCM channel) | |
248 | { | |
249 | VERIFY_STRING(channel); | |
a6339323 | 250 | Message m = Commands::Cycle(Interp::bot, Utils::scm2str(channel)); |
46af1667 | 251 | return scm_from_int(m.getCode()); |
cb21075d | 252 | } |
253 | ||
254 | SCM | |
255 | ScriptCommands::Deban(SCM channel, SCM who) | |
256 | { | |
257 | VERIFY_STRING(channel); | |
258 | VERIFY_STRING(who); | |
a6339323 | 259 | Message m = Commands::Deban(Interp::bot, Utils::scm2str(channel), |
260 | Utils::scm2str(who)); | |
46af1667 | 261 | return scm_from_int(m.getCode()); |
cb21075d | 262 | } |
263 | ||
264 | SCM | |
265 | ScriptCommands::DelServer(SCM number) | |
266 | { | |
267 | VERIFY_NUMBER(number); | |
46af1667 | 268 | Message m = Commands::DelServer(Interp::bot, scm_to_int (number)); |
269 | return scm_from_int(m.getCode()); | |
cb21075d | 270 | } |
271 | ||
272 | SCM | |
273 | ScriptCommands::DelUser(SCM who, SCM maskChannel) | |
274 | { | |
275 | VERIFY_STRING(who); | |
276 | VERIFY_STRING(maskChannel); | |
a6339323 | 277 | Message m = Commands::DelUser(Interp::bot, Utils::scm2str(who), |
278 | Utils::scm2str(maskChannel)); | |
46af1667 | 279 | return scm_from_int(m.getCode()); |
cb21075d | 280 | } |
281 | ||
282 | SCM | |
283 | ScriptCommands::DelShit(SCM who, SCM maskChannel) | |
284 | { | |
285 | VERIFY_STRING(who); | |
286 | VERIFY_STRING(maskChannel); | |
a6339323 | 287 | Message m = Commands::DelShit(Interp::bot, Utils::scm2str(who), |
288 | Utils::scm2str(maskChannel)); | |
46af1667 | 289 | return scm_from_int(m.getCode()); |
cb21075d | 290 | } |
291 | ||
292 | SCM | |
293 | ScriptCommands::Deop(SCM channel, SCM who) | |
294 | { | |
295 | VERIFY_STRING(channel); | |
296 | VERIFY_STRING(who); | |
a6339323 | 297 | Message m = Commands::Deop(Interp::bot, Utils::scm2str(channel), |
298 | Utils::scm2str(who)); | |
46af1667 | 299 | return scm_from_int(m.getCode()); |
cb21075d | 300 | } |
301 | ||
302 | SCM | |
303 | ScriptCommands::Die(SCM reason) | |
304 | { | |
305 | String r = "Leaving"; | |
46af1667 | 306 | if (scm_is_string(reason)) |
a6339323 | 307 | r = Utils::scm2str(reason); |
cb21075d | 308 | Message m = Commands::Die(Interp::bot, r); |
46af1667 | 309 | return scm_from_int(m.getCode()); |
cb21075d | 310 | } |
311 | ||
312 | SCM | |
313 | ScriptCommands::Do(SCM command) | |
314 | { | |
315 | VERIFY_STRING(command); | |
a6339323 | 316 | Message m = Commands::Do(Interp::bot, Utils::scm2str(command)); |
46af1667 | 317 | return scm_from_int(m.getCode()); |
cb21075d | 318 | } |
319 | ||
320 | SCM | |
321 | ScriptCommands::Invite(SCM channel, SCM who) | |
322 | { | |
323 | VERIFY_STRING(channel); | |
324 | VERIFY_STRING(who); | |
a6339323 | 325 | Message m = Commands::Invite(Interp::bot, Utils::scm2str(channel), |
326 | Utils::scm2str(who)); | |
46af1667 | 327 | return scm_from_int(m.getCode()); |
cb21075d | 328 | } |
329 | ||
330 | SCM | |
331 | ScriptCommands::Join(SCM channel, SCM key) | |
332 | { | |
333 | VERIFY_STRING(channel); | |
334 | String k = ""; | |
46af1667 | 335 | if (scm_is_string(key)) |
a6339323 | 336 | k = Utils::scm2str(key); |
337 | Message m = Commands::Join(Interp::bot, Utils::scm2str(channel), | |
cb21075d | 338 | k); |
46af1667 | 339 | return scm_from_int(m.getCode()); |
cb21075d | 340 | } |
341 | ||
342 | SCM | |
343 | ScriptCommands::Keep(SCM channel, SCM modes) | |
344 | { | |
345 | VERIFY_STRING(channel); | |
346 | VERIFY_STRING(modes); | |
a6339323 | 347 | Message m = Commands::Keep(Interp::bot, Utils::scm2str(channel), |
348 | Utils::scm2str(modes)); | |
46af1667 | 349 | return scm_from_int(m.getCode()); |
cb21075d | 350 | } |
351 | ||
352 | SCM | |
353 | ScriptCommands::Kick(SCM channel, SCM who, SCM reason) | |
354 | { | |
355 | VERIFY_STRING(channel); | |
356 | VERIFY_STRING(who); | |
357 | ||
358 | String r = ""; | |
46af1667 | 359 | if (scm_is_string(reason)) |
a6339323 | 360 | r = Utils::scm2str(reason); |
cb21075d | 361 | |
a6339323 | 362 | Message m = Commands::Kick(Interp::bot, Utils::scm2str(channel), |
363 | Utils::scm2str(who), r); | |
46af1667 | 364 | return scm_from_int(m.getCode()); |
cb21075d | 365 | } |
366 | ||
367 | SCM | |
368 | ScriptCommands::KickBan(SCM channel, SCM who, SCM reason) | |
369 | { | |
370 | VERIFY_STRING(channel); | |
371 | VERIFY_STRING(who); | |
372 | String r = ""; | |
46af1667 | 373 | if (scm_is_string(reason)) |
a6339323 | 374 | r = Utils::scm2str(reason); |
375 | Message m = Commands::KickBan(Interp::bot, Utils::scm2str(channel), | |
376 | Utils::scm2str(who), r); | |
46af1667 | 377 | return scm_from_int(m.getCode()); |
cb21075d | 378 | } |
379 | ||
380 | SCM | |
381 | ScriptCommands::Lock(SCM channel) | |
382 | { | |
383 | VERIFY_STRING(channel); | |
a6339323 | 384 | Message m = Commands::Lock(Interp::bot, Utils::scm2str(channel)); |
46af1667 | 385 | return scm_from_int(m.getCode()); |
cb21075d | 386 | } |
387 | ||
388 | SCM | |
389 | ScriptCommands::LogPort(void) | |
390 | { | |
391 | return Interp::bot->botInterp->logPort; | |
392 | } | |
393 | ||
394 | SCM | |
395 | ScriptCommands::Mode(SCM channel, SCM mode) | |
396 | { | |
397 | VERIFY_STRING(channel); | |
398 | VERIFY_STRING(mode); | |
a6339323 | 399 | Message m = Commands::Mode(Interp::bot, Utils::scm2str(channel), |
400 | Utils::scm2str(mode)); | |
46af1667 | 401 | return scm_from_int(m.getCode()); |
cb21075d | 402 | } |
403 | ||
404 | SCM | |
405 | ScriptCommands::Msg(SCM nick, SCM message) | |
406 | { | |
407 | VERIFY_STRING(nick); | |
408 | VERIFY_STRING(message); | |
a6339323 | 409 | Message m = Commands::Msg(Interp::bot, Utils::scm2str(nick), |
410 | Utils::scm2str(message)); | |
46af1667 | 411 | return scm_from_int(m.getCode()); |
cb21075d | 412 | |
413 | } | |
414 | ||
415 | SCM | |
416 | ScriptCommands::NextServer(void) | |
417 | { | |
418 | Message m = Commands::NextServer(Interp::bot); | |
46af1667 | 419 | return scm_from_int(m.getCode()); |
cb21075d | 420 | } |
421 | ||
422 | SCM | |
423 | ScriptCommands::Nick(SCM nick) | |
424 | { | |
425 | VERIFY_STRING(nick); | |
a6339323 | 426 | Message m = Commands::Nick(Interp::bot, Utils::scm2str(nick)); |
46af1667 | 427 | return scm_from_int(m.getCode()); |
cb21075d | 428 | } |
429 | ||
c99c411a | 430 | SCM |
431 | ScriptCommands::Notice (SCM to, SCM message) | |
432 | { | |
433 | VERIFY_STRING (to); | |
434 | VERIFY_STRING (message); | |
435 | ||
46af1667 | 436 | return (scm_from_int |
c99c411a | 437 | (Commands::Notice (Interp::bot, |
438 | Utils::scm2str (to), | |
439 | Utils::scm2str (message)).getCode ())); | |
440 | ||
441 | } | |
442 | ||
cb21075d | 443 | SCM |
444 | ScriptCommands::Op(SCM channel, SCM who) | |
445 | { | |
446 | VERIFY_STRING(channel); | |
447 | VERIFY_STRING(who); | |
a6339323 | 448 | Message m = Commands::Op(Interp::bot, Utils::scm2str(channel), |
449 | Utils::scm2str(who)); | |
46af1667 | 450 | return scm_from_int(m.getCode()); |
cb21075d | 451 | } |
452 | ||
453 | SCM | |
454 | ScriptCommands::Part(SCM channel) | |
455 | { | |
456 | VERIFY_STRING(channel); | |
a6339323 | 457 | Message m = Commands::Part(Interp::bot, Utils::scm2str(channel)); |
46af1667 | 458 | return scm_from_int(m.getCode()); |
cb21075d | 459 | } |
460 | ||
461 | SCM | |
462 | ScriptCommands::Reconnect(void) | |
463 | { | |
464 | Message m = Commands::Reconnect(Interp::bot); | |
46af1667 | 465 | return scm_from_int(m.getCode()); |
cb21075d | 466 | } |
467 | ||
468 | SCM | |
469 | ScriptCommands::Say(SCM channel, SCM message) | |
470 | { | |
471 | VERIFY_STRING(channel); | |
472 | VERIFY_STRING(message); | |
a6339323 | 473 | Message m = Commands::Say(Interp::bot, Utils::scm2str(channel), |
474 | Utils::scm2str(message)); | |
46af1667 | 475 | return scm_from_int(m.getCode()); |
cb21075d | 476 | } |
477 | ||
478 | SCM | |
479 | ScriptCommands::Server(SCM number) | |
480 | { | |
481 | VERIFY_NUMBER(number); | |
46af1667 | 482 | Message m = Commands::Server(Interp::bot, scm_to_int(number)); |
483 | return scm_from_int(m.getCode()); | |
cb21075d | 484 | } |
485 | ||
e171dcce | 486 | SCM |
487 | ScriptCommands::SetFloodRate(SCM rate) | |
488 | { | |
489 | VERIFY_NUMBER(rate); | |
46af1667 | 490 | Message m = Commands::SetFloodRate(Interp::bot, scm_to_uint (rate)); |
491 | return scm_from_int(m.getCode()); | |
e171dcce | 492 | } |
493 | ||
cb21075d | 494 | SCM |
495 | ScriptCommands::SetVersion(SCM version) | |
496 | { | |
a6339323 | 497 | Message m = Commands::SetVersion(Interp::bot, Utils::scm2str(version)); |
46af1667 | 498 | return scm_from_int(m.getCode()); |
cb21075d | 499 | } |
500 | ||
501 | SCM | |
502 | ScriptCommands::TBan(SCM channel, SCM who, SCM seconds) | |
503 | { | |
504 | VERIFY_STRING(channel); | |
505 | VERIFY_STRING(who); | |
506 | VERIFY_NUMBER(seconds); | |
a6339323 | 507 | Message m = Commands::TBan(Interp::bot, Utils::scm2str(channel), |
46af1667 | 508 | Utils::scm2str(who), scm_to_int(seconds)); |
509 | return scm_from_int(m.getCode()); | |
cb21075d | 510 | } |
511 | ||
512 | SCM | |
513 | ScriptCommands::TKBan(SCM channel, SCM who, SCM seconds, SCM reason) | |
514 | { | |
515 | VERIFY_STRING(channel); | |
516 | VERIFY_STRING(who); | |
517 | VERIFY_NUMBER(seconds); | |
518 | String r = ""; | |
46af1667 | 519 | if (scm_is_string(reason)) |
a6339323 | 520 | r = Utils::scm2str(reason); |
521 | Message m = Commands::TKBan(Interp::bot, Utils::scm2str(channel), | |
522 | Utils::scm2str(who), | |
46af1667 | 523 | scm_to_int(seconds), r); |
524 | return scm_from_int(m.getCode()); | |
cb21075d | 525 | } |
526 | ||
527 | SCM | |
528 | ScriptCommands::Topic(SCM channel, SCM topic) | |
529 | { | |
530 | VERIFY_STRING(channel); | |
531 | VERIFY_STRING(topic); | |
a6339323 | 532 | Message m = Commands::Topic(Interp::bot, Utils::scm2str(channel), |
533 | Utils::scm2str(topic)); | |
46af1667 | 534 | return scm_from_int(m.getCode()); |
cb21075d | 535 | } |
536 | ||
537 | SCM | |
538 | ScriptCommands::Unlock(SCM channel) | |
539 | { | |
540 | VERIFY_STRING(channel); | |
a6339323 | 541 | Message m = Commands::Unlock(Interp::bot, Utils::scm2str(channel)); |
46af1667 | 542 | return scm_from_int(m.getCode()); |
cb21075d | 543 | } |
544 | ||
c6e7af05 | 545 | SCM |
546 | ScriptCommands::Who (SCM target) | |
547 | { | |
548 | VERIFY_STRING (target); | |
549 | ||
550 | Message m = Commands::Who (Interp::bot, Utils::scm2str (target)); | |
551 | ||
46af1667 | 552 | return scm_from_int (m.getCode ()); |
c6e7af05 | 553 | } |
554 | ||
555 | SCM | |
556 | ScriptCommands::Whois (SCM nick) | |
557 | { | |
558 | VERIFY_STRING (nick); | |
559 | ||
560 | Message m = Commands::Whois (Interp::bot, Utils::scm2str (nick)); | |
561 | ||
46af1667 | 562 | return scm_from_int (m.getCode ()); |
c6e7af05 | 563 | } |
564 | ||
cb21075d | 565 | SCM |
566 | ScriptCommands::getNickname(void) | |
567 | { | |
a6339323 | 568 | return Utils::str2scm(Interp::bot->nickName); |
cb21075d | 569 | } |
570 | ||
571 | SCM | |
572 | ScriptCommands::getServer(void) | |
573 | { | |
574 | ::Server *serv = Interp::bot->serverList->currentServer(); | |
575 | int serverNumber = Interp::bot->serverList->currentNumber; | |
576 | ||
46af1667 | 577 | return scm_list_n(scm_from_int(serverNumber), |
a6339323 | 578 | Utils::str2scm(serv->getHostName()), |
46af1667 | 579 | scm_from_int(serv->getPort()), |
a6339323 | 580 | Utils::str2scm(serv->getPassword()), |
cb21075d | 581 | SCM_UNDEFINED); |
582 | } | |
583 | ||
584 | SCM | |
585 | ScriptCommands::getServerList(void) | |
586 | { | |
46af1667 | 587 | SCM res = scm_list_n(SCM_UNDEFINED); |
cb21075d | 588 | ::Server *s; |
589 | int i = 0; | |
528799bd | 590 | std::vector<class Server *>::iterator it = |
591 | Interp::bot->serverList->begin(); | |
cb21075d | 592 | |
593 | for ( ; it != Interp::bot->serverList->end(); ++it) { | |
594 | s = (*it); | |
46af1667 | 595 | res = |
596 | scm_append | |
597 | (scm_list_2 (res, | |
598 | scm_list_n(scm_list_n(scm_from_int(i++), | |
599 | Utils::str2scm(s->getHostName()), | |
600 | scm_from_int(s->getPort()), | |
601 | Utils::str2scm(s->getPassword()), | |
602 | SCM_UNDEFINED), SCM_UNDEFINED))); | |
603 | } | |
cb21075d | 604 | return res; |
605 | } | |
606 | ||
17f13c7a CE |
607 | SCM ScriptCommands::getVersion(void) |
608 | { | |
609 | return Utils::str2scm (Interp::bot->get_version ()); | |
610 | } | |
611 | ||
612 | SCM ScriptCommands::getReleaseRevision(void) | |
613 | { | |
614 | return Utils::str2scm (Interp::bot->release_revision); | |
615 | } | |
616 | ||
cb21075d | 617 | SCM |
618 | ScriptCommands::flushQueue(void) | |
619 | { | |
620 | if (!Interp::bot->serverConnection) | |
621 | return SCM_BOOL_F; | |
622 | Interp::bot->serverConnection->queue->flush(); | |
623 | return SCM_BOOL_T; | |
624 | } | |
625 | ||
626 | SCM | |
627 | ScriptCommands::flushPort(void) | |
628 | { | |
629 | scm_flush(Interp::bot->botInterp->logPort); | |
630 | return SCM_BOOL_T; | |
631 | } | |
632 | ||
633 | SCM | |
634 | ScriptCommands::random(SCM scm_max) | |
635 | { | |
636 | int max = 0; | |
637 | //srand(time(NULL)); | |
638 | if (SCM_NUMBERP(scm_max)) | |
46af1667 | 639 | max = scm_to_int(scm_max); |
640 | return scm_from_int(max ? rand() % max : 0); | |
cb21075d | 641 | } |
642 | ||
643 | SCM | |
644 | ScriptCommands::addCommand(SCM scm_commandName, SCM scm_function, | |
645 | SCM scm_needsChannel, SCM scm_args, | |
646 | SCM scm_minLevel) | |
647 | { | |
648 | // We check that commandName is a string | |
46af1667 | 649 | if (!scm_is_string(scm_commandName)) |
cb21075d | 650 | return SCM_BOOL_F; |
651 | ||
652 | // We check that the command does not exist | |
a6339323 | 653 | String commandName = Utils::to_upper (Utils::scm2str(scm_commandName)); |
e07b6b46 | 654 | if (Interp::bot->userFunctions[commandName]) |
655 | return SCM_BOOL_F; | |
cb21075d | 656 | |
657 | // Next we check that needsChannel is a boolean | |
46af1667 | 658 | if (!scm_is_bool (scm_needsChannel)) |
cb21075d | 659 | return SCM_BOOL_F; |
46af1667 | 660 | bool needsChannel = scm_to_bool(scm_needsChannel); |
cb21075d | 661 | |
662 | // We check that minLevel is an integer and that it's | |
663 | // a valid level | |
664 | if (!SCM_NUMBERP(scm_minLevel)) | |
665 | return SCM_BOOL_F; | |
666 | ||
46af1667 | 667 | int minLevel = scm_to_int(scm_minLevel); |
cb21075d | 668 | if (minLevel < User::NONE || minLevel > User::MASTER) |
669 | return SCM_BOOL_F; | |
670 | ||
671 | // We check that "scm_function" is a Scheme procedure | |
46af1667 | 672 | if (!scm_is_true (scm_procedure_p(scm_function))) |
cb21075d | 673 | return SCM_BOOL_F; |
674 | ||
675 | // We check that args is an integer and is between 0 and 20 (arbitrary limit) | |
676 | if (!SCM_NUMBERP(scm_args)) | |
677 | return SCM_BOOL_F; | |
678 | ||
46af1667 | 679 | int args = scm_to_int(scm_args); |
cb21075d | 680 | if (args < 0 || args > 20) |
681 | return SCM_BOOL_F; | |
682 | ||
683 | // We add the command in the commands list | |
e07b6b46 | 684 | Interp::bot->userFunctions[commandName] = |
685 | new userFunction(0, minLevel, needsChannel, args, scm_function); | |
686 | ||
cb21075d | 687 | return SCM_BOOL_T; |
688 | } | |
689 | ||
690 | SCM | |
691 | ScriptCommands::delCommand(SCM scm_commandName) | |
692 | { | |
693 | // We check that commandName is a string | |
46af1667 | 694 | if (!scm_is_string(scm_commandName)) |
cb21075d | 695 | return SCM_BOOL_F; |
696 | ||
697 | // We check that the command does exist | |
a6339323 | 698 | String commandName = Utils::to_upper (Utils::scm2str(scm_commandName)); |
e07b6b46 | 699 | if (!Interp::bot->userFunctions[commandName]) |
cb21075d | 700 | return SCM_BOOL_F; |
701 | ||
702 | // We delete the command | |
e07b6b46 | 703 | Interp::bot->userFunctions.erase(commandName); |
cb21075d | 704 | |
705 | return SCM_BOOL_T; | |
706 | } | |
707 | ||
708 | SCM | |
fd7440f1 | 709 | ScriptCommands::AddHook(SCM type, SCM regex, SCM function, SCM pri, SCM fall, |
710 | SCM name) | |
cb21075d | 711 | { |
712 | int priority = 0; | |
439869bf | 713 | bool fallt = true; // does this hook fall through? |
fd7440f1 | 714 | String rname = "DEFAULT"; |
cb21075d | 715 | |
716 | if (!SCM_UNBNDP (pri)) | |
46af1667 | 717 | priority = scm_to_int (pri); |
cb21075d | 718 | if (!SCM_UNBNDP (fall)) |
46af1667 | 719 | fallt = scm_is_true (fall); |
fd7440f1 | 720 | if (!SCM_UNBNDP (name)) |
a6339323 | 721 | rname = Utils::scm2str (name); |
46af1667 | 722 | return scm_from_bool (Interp::bot->botInterp->AddHook(scm_to_int(type), |
cb21075d | 723 | regex, function, |
fd7440f1 | 724 | priority, fallt, rname)); |
cb21075d | 725 | } |
726 | ||
727 | SCM | |
728 | ScriptCommands::AddTimer(SCM when, SCM function) | |
729 | { | |
46af1667 | 730 | return Interp::bot->botInterp->AddTimer(scm_to_int(when), function); |
cb21075d | 731 | } |
732 | ||
733 | SCM | |
734 | ScriptCommands::DelTimer(SCM timer) | |
735 | { | |
46af1667 | 736 | return scm_from_bool (Interp::bot->botInterp->DelTimer(timer)); |
e07b6b46 | 737 | } |
738 | ||
0b7a49e2 | 739 | SCM |
740 | ScriptCommands::sendDCCChatMessage (SCM to, SCM message) | |
741 | { | |
742 | ||
46af1667 | 743 | return scm_from_bool (Interp::bot->dccConnections->sendMessage |
a6339323 | 744 | (Utils::scm2str (to), |
745 | Utils::scm2str (message))); | |
0b7a49e2 | 746 | } |
747 | ||
e07b6b46 | 748 | |
e07b6b46 | 749 | |
6b7614a8 | 750 | |
672b7d4e | 751 | |
752 | ||
cb21075d | 753 | #endif |