1 // ScriptCommands.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002 Clinton Ebadi
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
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.
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.
27 #include "ServerList.H"
28 #include "ServerQueue.H"
29 #include "ScriptCommands.H"
33 #define VERIFY_STRING(par) if (!SCM_STRINGP((par))) \
34 return scm_long2num(-17)
36 #define VERIFY_NUMBER(par) if (!SCM_NUMBERP((par))) \
37 return scm_long2num(-17)
40 ScriptCommands::Action(SCM channel, SCM message)
42 VERIFY_STRING(channel);
43 VERIFY_STRING(message);
44 Message m = Commands::Action(Interp::bot, Utils::scm2String(channel),
45 Utils::scm2String(message));
46 return scm_long2num(m.getCode());
50 ScriptCommands::AddUser(SCM who, SCM maskChannel, SCM level,
51 SCM prot, SCM aop, SCM expire, SCM password)
53 // It segfaults when not online, but otherwise appears to work
55 VERIFY_STRING (maskChannel);
56 VERIFY_NUMBER (level);
58 String wwho = Utils::scm2String (who);
59 String mask = Utils::scm2String (maskChannel);
63 if (SCM_UNBNDP (password))
67 VERIFY_STRING (password);
68 passwd = Utils::scm2String (password);
70 if (SCM_UNBNDP (expire))
74 VERIFY_STRING (expire);
75 eexpire = Utils::strToTime (Utils::scm2String (expire));
76 if (!eexpire) eexpire = -1;
79 int protect = scm_num2int (prot, SCM_ARG1, "ScriptCommands::AddUser");
80 bool aaop = SCM_NFALSEP (aop);
81 int llevel = scm_num2int (level, SCM_ARG1,
82 "ScriptCommands::AddUser");
84 Message m = Commands::AddUser (Interp::bot, wwho, mask, llevel,
85 protect, aaop, eexpire, passwd);
87 return scm_long2num(m.getCode ());
91 ScriptCommands::AddServer(SCM servername, SCM port)
94 if (SCM_NUMBERP(port))
95 p = scm_num2long(port, SCM_ARG1, "ScriptCommands::AddServer");
96 Message m = Commands::AddServer(Interp::bot,
97 Utils::scm2String(servername),
99 return scm_long2num(m.getCode());
103 ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level,
104 SCM expiration, SCM reason)
106 // This appears to work...not much testing though
107 VERIFY_STRING (mask);
108 VERIFY_STRING (maskChannel);
109 VERIFY_NUMBER (level);
110 String mmask = Utils::scm2String (mask);
111 String mmaskChannel = Utils::scm2String (maskChannel);
112 int llevel = scm_num2int (level, SCM_ARG1, "ScriptCommands::AddShit");
116 if (SCM_UNBNDP (expiration))
120 VERIFY_STRING (expiration);
121 expire = Utils::strToTime (Utils::scm2String (expiration));
122 if (!expire) expire = -1;
124 if (SCM_UNBNDP (reason))
125 rreason = "You're on my shitlist, lamer";
128 VERIFY_STRING (reason);
129 rreason = Utils::scm2String (reason);
131 Message m = Commands::AddShit (Interp::bot, mmask, mmaskChannel,
132 llevel, expire, rreason);
134 return scm_long2num(m.getCode ());
138 ScriptCommands::Ban(SCM channel, SCM who)
140 VERIFY_STRING(channel);
142 Message m = Commands::Ban(Interp::bot, Utils::scm2String(channel),
143 Utils::scm2String(who));
144 return scm_long2num(m.getCode());
148 ScriptCommands::Cycle(SCM channel)
150 VERIFY_STRING(channel);
151 Message m = Commands::Cycle(Interp::bot, Utils::scm2String(channel));
152 return scm_long2num(m.getCode());
156 ScriptCommands::Deban(SCM channel, SCM who)
158 VERIFY_STRING(channel);
160 Message m = Commands::Deban(Interp::bot, Utils::scm2String(channel),
161 Utils::scm2String(who));
162 return scm_long2num(m.getCode());
166 ScriptCommands::DelServer(SCM number)
168 VERIFY_NUMBER(number);
169 Message m = Commands::DelServer(Interp::bot,
170 scm_num2long(number, SCM_ARG1,
171 "ScriptCommands::DelServer"));
172 return scm_long2num(m.getCode());
176 ScriptCommands::DelUser(SCM who, SCM maskChannel)
179 VERIFY_STRING(maskChannel);
180 Message m = Commands::DelUser(Interp::bot, Utils::scm2String(who),
181 Utils::scm2String(maskChannel));
182 return scm_long2num(m.getCode());
186 ScriptCommands::DelShit(SCM who, SCM maskChannel)
189 VERIFY_STRING(maskChannel);
190 Message m = Commands::DelShit(Interp::bot, Utils::scm2String(who),
191 Utils::scm2String(maskChannel));
192 return scm_long2num(m.getCode());
196 ScriptCommands::Deop(SCM channel, SCM who)
198 VERIFY_STRING(channel);
200 Message m = Commands::Deop(Interp::bot, Utils::scm2String(channel),
201 Utils::scm2String(who));
202 return scm_long2num(m.getCode());
206 ScriptCommands::Die(SCM reason)
208 String r = "Leaving";
209 if (SCM_STRINGP(reason))
210 r = Utils::scm2String(reason);
211 Message m = Commands::Die(Interp::bot, r);
212 return scm_long2num(m.getCode());
216 ScriptCommands::Do(SCM command)
218 VERIFY_STRING(command);
219 Message m = Commands::Do(Interp::bot, Utils::scm2String(command));
220 return scm_long2num(m.getCode());
224 ScriptCommands::Invite(SCM channel, SCM who)
226 VERIFY_STRING(channel);
228 Message m = Commands::Invite(Interp::bot, Utils::scm2String(channel),
229 Utils::scm2String(who));
230 return scm_long2num(m.getCode());
234 ScriptCommands::Join(SCM channel, SCM key)
236 VERIFY_STRING(channel);
238 if (SCM_STRINGP(key))
239 k = Utils::scm2String(key);
240 Message m = Commands::Join(Interp::bot, Utils::scm2String(channel),
242 return scm_long2num(m.getCode());
246 ScriptCommands::Keep(SCM channel, SCM modes)
248 VERIFY_STRING(channel);
249 VERIFY_STRING(modes);
250 Message m = Commands::Keep(Interp::bot, Utils::scm2String(channel),
251 Utils::scm2String(modes));
252 return scm_long2num(m.getCode());
256 ScriptCommands::Kick(SCM channel, SCM who, SCM reason)
258 VERIFY_STRING(channel);
262 if (SCM_STRINGP(reason))
263 r = Utils::scm2String(reason);
265 Message m = Commands::Kick(Interp::bot, Utils::scm2String(channel),
266 Utils::scm2String(who), r);
267 return scm_long2num(m.getCode());
271 ScriptCommands::KickBan(SCM channel, SCM who, SCM reason)
273 VERIFY_STRING(channel);
276 if (SCM_STRINGP(reason))
277 r = Utils::scm2String(reason);
278 Message m = Commands::KickBan(Interp::bot, Utils::scm2String(channel),
279 Utils::scm2String(who), r);
280 return scm_long2num(m.getCode());
284 ScriptCommands::Lock(SCM channel)
286 VERIFY_STRING(channel);
287 Message m = Commands::Lock(Interp::bot, Utils::scm2String(channel));
288 return scm_long2num(m.getCode());
292 ScriptCommands::LogPort(void)
294 return Interp::bot->botInterp->logPort;
298 ScriptCommands::Mode(SCM channel, SCM mode)
300 VERIFY_STRING(channel);
302 Message m = Commands::Mode(Interp::bot, Utils::scm2String(channel),
303 Utils::scm2String(mode));
304 return scm_long2num(m.getCode());
308 ScriptCommands::Msg(SCM nick, SCM message)
311 VERIFY_STRING(message);
312 Message m = Commands::Msg(Interp::bot, Utils::scm2String(nick),
313 Utils::scm2String(message));
314 return scm_long2num(m.getCode());
319 ScriptCommands::NextServer(void)
321 Message m = Commands::NextServer(Interp::bot);
322 return scm_long2num(m.getCode());
326 ScriptCommands::Nick(SCM nick)
329 Message m = Commands::Nick(Interp::bot, Utils::scm2String(nick));
330 return scm_long2num(m.getCode());
334 ScriptCommands::Op(SCM channel, SCM who)
336 VERIFY_STRING(channel);
338 Message m = Commands::Op(Interp::bot, Utils::scm2String(channel),
339 Utils::scm2String(who));
340 return scm_long2num(m.getCode());
344 ScriptCommands::Part(SCM channel)
346 VERIFY_STRING(channel);
347 Message m = Commands::Part(Interp::bot, Utils::scm2String(channel));
348 return scm_long2num(m.getCode());
352 ScriptCommands::Reconnect(void)
354 Message m = Commands::Reconnect(Interp::bot);
355 return scm_long2num(m.getCode());
359 ScriptCommands::Say(SCM channel, SCM message)
361 VERIFY_STRING(channel);
362 VERIFY_STRING(message);
363 Message m = Commands::Say(Interp::bot, Utils::scm2String(channel),
364 Utils::scm2String(message));
365 return scm_long2num(m.getCode());
369 ScriptCommands::Server(SCM number)
371 VERIFY_NUMBER(number);
372 Message m = Commands::Server(Interp::bot, gh_scm2long(number));
373 return scm_long2num(m.getCode());
377 ScriptCommands::SetVersion(SCM version)
379 Message m = Commands::SetVersion(Interp::bot, Utils::scm2String(version));
380 return scm_long2num(m.getCode());
384 ScriptCommands::TBan(SCM channel, SCM who, SCM seconds)
386 VERIFY_STRING(channel);
388 VERIFY_NUMBER(seconds);
389 Message m = Commands::TBan(Interp::bot, Utils::scm2String(channel),
390 Utils::scm2String(who), gh_scm2long(seconds));
391 return scm_long2num(m.getCode());
395 ScriptCommands::TKBan(SCM channel, SCM who, SCM seconds, SCM reason)
397 VERIFY_STRING(channel);
399 VERIFY_NUMBER(seconds);
401 if (SCM_STRINGP(reason))
402 r = Utils::scm2String(reason);
403 Message m = Commands::TKBan(Interp::bot, Utils::scm2String(channel),
404 Utils::scm2String(who),
405 gh_scm2long(seconds), r);
406 return scm_long2num(m.getCode());
410 ScriptCommands::Topic(SCM channel, SCM topic)
412 VERIFY_STRING(channel);
413 VERIFY_STRING(topic);
414 Message m = Commands::Topic(Interp::bot, Utils::scm2String(channel),
415 Utils::scm2String(topic));
416 return scm_long2num(m.getCode());
420 ScriptCommands::Unlock(SCM channel)
422 VERIFY_STRING(channel);
423 Message m = Commands::Unlock(Interp::bot, Utils::scm2String(channel));
424 return scm_long2num(m.getCode());
428 ScriptCommands::getNickname(void)
430 return Utils::string2SCM(Interp::bot->nickName);
434 ScriptCommands::getServer(void)
436 ::Server *serv = Interp::bot->serverList->currentServer();
437 int serverNumber = Interp::bot->serverList->currentNumber;
439 return gh_list(scm_long2num(serverNumber),
440 Utils::string2SCM(serv->getHostName()),
441 scm_long2num(serv->getPort()),
442 Utils::string2SCM(serv->getPassword()),
447 ScriptCommands::getServerList(void)
449 SCM res = gh_list(SCM_UNDEFINED);
452 std::vector<class Server *>::iterator it = Interp::bot->serverList->begin();
454 for ( ; it != Interp::bot->serverList->end(); ++it) {
456 res = gh_append2(res,
457 gh_list(gh_list(scm_long2num(i++),
458 Utils::string2SCM(s->getHostName()),
459 scm_long2num(s->getPort()),
460 Utils::string2SCM(s->getPassword()),
461 SCM_UNDEFINED), SCM_UNDEFINED));
467 ScriptCommands::flushQueue(void)
469 if (!Interp::bot->serverConnection)
471 Interp::bot->serverConnection->queue->flush();
476 ScriptCommands::flushPort(void)
478 scm_flush(Interp::bot->botInterp->logPort);
483 ScriptCommands::random(SCM scm_max)
487 if (SCM_NUMBERP(scm_max))
488 max = gh_scm2int(scm_max);
489 return scm_long2num(max ? rand() % max : 0);
493 ScriptCommands::addCommand(SCM scm_commandName, SCM scm_function,
494 SCM scm_needsChannel, SCM scm_args,
497 // We check that commandName is a string
498 if (!SCM_STRINGP(scm_commandName))
501 // We check that the command does not exist
502 String commandName = Utils::scm2String(scm_commandName).toUpper();
503 if (Interp::bot->userFunctions[commandName])
506 // Next we check that needsChannel is a boolean
507 if (!gh_boolean_p(scm_needsChannel))
509 bool needsChannel = gh_scm2bool(scm_needsChannel);
511 // We check that minLevel is an integer and that it's
513 if (!SCM_NUMBERP(scm_minLevel))
516 int minLevel = gh_scm2long(scm_minLevel);
517 if (minLevel < User::NONE || minLevel > User::MASTER)
520 // We check that "scm_function" is a Scheme procedure
521 if (!gh_procedure_p(scm_function))
524 // We check that args is an integer and is between 0 and 20 (arbitrary limit)
525 if (!SCM_NUMBERP(scm_args))
528 int args = gh_scm2long(scm_args);
529 if (args < 0 || args > 20)
532 // We add the command in the commands list
533 Interp::bot->userFunctions[commandName] =
534 new userFunction(0, minLevel, needsChannel, args, scm_function);
540 ScriptCommands::delCommand(SCM scm_commandName)
542 // We check that commandName is a string
543 if (!SCM_STRINGP(scm_commandName))
546 // We check that the command does exist
547 String commandName = Utils::scm2String(scm_commandName).toUpper();
548 if (!Interp::bot->userFunctions[commandName])
551 // We delete the command
552 Interp::bot->userFunctions.erase(commandName);
558 ScriptCommands::AddHook(SCM type, SCM regex, SCM function, SCM pri, SCM fall,
562 bool fallt = true; // does this hook fall through?
563 String rname = "DEFAULT";
565 if (!SCM_UNBNDP (pri))
566 priority = scm_num2int (pri, SCM_ARG1, "ScriptCommands::AddHook");
567 if (!SCM_UNBNDP (fall))
568 fallt = SCM_NFALSEP (fall);
569 if (!SCM_UNBNDP (name))
570 rname = Utils::scm2String (name);
571 return SCM_BOOL (Interp::bot->botInterp->AddHook(gh_scm2long(type),
573 priority, fallt, rname));
577 ScriptCommands::AddTimer(SCM when, SCM function)
579 return Interp::bot->botInterp->AddTimer(gh_scm2long(when), function);
583 ScriptCommands::DelTimer(SCM timer)
585 return SCM_BOOL (Interp::bot->botInterp->DelTimer(timer));
589 // FIXME: write these
591 #define IQUEUE Interp::bot->serverConnection->queue
594 ScriptCommands::sendCTCP(SCM to, SCM command , SCM message)
597 VERIFY_STRING(command);
598 VERIFY_STRING(message);
600 IQUEUE->sendCTCP (Utils::scm2String (to), Utils::scm2String (command),
601 Utils::scm2String (message));
602 return SCM_UNSPECIFIED;