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