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