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 "ScriptCommands.H"
32 #define VERIFY_STRING(par) if (!SCM_STRINGP((par))) \
33 return scm_long2num(-17)
35 #define VERIFY_NUMBER(par) if (!SCM_NUMBERP((par))) \
36 return scm_long2num(-17)
39 ScriptCommands::Action(SCM channel, SCM message)
41 VERIFY_STRING(channel);
42 VERIFY_STRING(message);
43 Message m = Commands::Action(Interp::bot, Utils::scm2String(channel),
44 Utils::scm2String(message));
45 return scm_long2num(m.getCode());
49 ScriptCommands::AddUser(SCM who, SCM maskChannel, SCM level,
50 SCM prot, SCM aop, SCM expire, SCM password)
52 // It segfaults when not online, but otherwise appears to work
54 VERIFY_STRING (maskChannel);
55 VERIFY_NUMBER (level);
57 String wwho = Utils::scm2String (who);
58 String mask = Utils::scm2String (maskChannel);
62 if (SCM_UNBNDP (password))
66 VERIFY_STRING (password);
67 passwd = Utils::scm2String (password);
69 if (SCM_UNBNDP (expire))
73 VERIFY_STRING (expire);
74 eexpire = Utils::strToTime (Utils::scm2String (expire));
75 if (!eexpire) eexpire = -1;
78 int protect = scm_num2int (prot, SCM_ARG1, "ScriptCommands::AddUser");
79 bool aaop = SCM_NFALSEP (aop);
80 int llevel = scm_num2int (level, SCM_ARG1,
81 "ScriptCommands::AddUser");
83 Message m = Commands::AddUser (Interp::bot, wwho, mask, llevel,
84 protect, aaop, eexpire, passwd);
86 return scm_long2num(m.getCode ());
90 ScriptCommands::AddServer(SCM servername, SCM port)
93 if (SCM_NUMBERP(port))
94 p = scm_num2long(port, SCM_ARG1, "ScriptCommands::AddServer");
95 Message m = Commands::AddServer(Interp::bot,
96 Utils::scm2String(servername),
98 return scm_long2num(m.getCode());
102 ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level,
103 SCM expiration, SCM reason)
105 // This appears to work...not much testing though
106 VERIFY_STRING (mask);
107 VERIFY_STRING (maskChannel);
108 VERIFY_NUMBER (level);
109 String mmask = Utils::scm2String (mask);
110 String mmaskChannel = Utils::scm2String (maskChannel);
111 int llevel = scm_num2int (level, SCM_ARG1, "ScriptCommands::AddShit");
115 if (SCM_UNBNDP (expiration))
119 VERIFY_STRING (expiration);
120 expire = Utils::strToTime (Utils::scm2String (expiration));
121 if (!expire) expire = -1;
123 if (SCM_UNBNDP (reason))
124 rreason = "You're on my shitlist, lamer";
127 VERIFY_STRING (reason);
128 rreason = Utils::scm2String (reason);
130 Message m = Commands::AddShit (Interp::bot, mmask, mmaskChannel,
131 llevel, expire, rreason);
133 return scm_long2num(m.getCode ());
137 ScriptCommands::Ban(SCM channel, SCM who)
139 VERIFY_STRING(channel);
141 Message m = Commands::Ban(Interp::bot, Utils::scm2String(channel),
142 Utils::scm2String(who));
143 return scm_long2num(m.getCode());
147 ScriptCommands::Cycle(SCM channel)
149 VERIFY_STRING(channel);
150 Message m = Commands::Cycle(Interp::bot, Utils::scm2String(channel));
151 return scm_long2num(m.getCode());
155 ScriptCommands::Deban(SCM channel, SCM who)
157 VERIFY_STRING(channel);
159 Message m = Commands::Deban(Interp::bot, Utils::scm2String(channel),
160 Utils::scm2String(who));
161 return scm_long2num(m.getCode());
165 ScriptCommands::DelServer(SCM number)
167 VERIFY_NUMBER(number);
168 Message m = Commands::DelServer(Interp::bot,
169 scm_num2long(number, SCM_ARG1,
170 "ScriptCommands::DelServer"));
171 return scm_long2num(m.getCode());
175 ScriptCommands::DelUser(SCM who, SCM maskChannel)
178 VERIFY_STRING(maskChannel);
179 Message m = Commands::DelUser(Interp::bot, Utils::scm2String(who),
180 Utils::scm2String(maskChannel));
181 return scm_long2num(m.getCode());
185 ScriptCommands::DelShit(SCM who, SCM maskChannel)
188 VERIFY_STRING(maskChannel);
189 Message m = Commands::DelShit(Interp::bot, Utils::scm2String(who),
190 Utils::scm2String(maskChannel));
191 return scm_long2num(m.getCode());
195 ScriptCommands::Deop(SCM channel, SCM who)
197 VERIFY_STRING(channel);
199 Message m = Commands::Deop(Interp::bot, Utils::scm2String(channel),
200 Utils::scm2String(who));
201 return scm_long2num(m.getCode());
205 ScriptCommands::Die(SCM reason)
207 String r = "Leaving";
208 if (SCM_STRINGP(reason))
209 r = Utils::scm2String(reason);
210 Message m = Commands::Die(Interp::bot, r);
211 return scm_long2num(m.getCode());
215 ScriptCommands::Do(SCM command)
217 VERIFY_STRING(command);
218 Message m = Commands::Do(Interp::bot, Utils::scm2String(command));
219 return scm_long2num(m.getCode());
223 ScriptCommands::Invite(SCM channel, SCM who)
225 VERIFY_STRING(channel);
227 Message m = Commands::Invite(Interp::bot, Utils::scm2String(channel),
228 Utils::scm2String(who));
229 return scm_long2num(m.getCode());
233 ScriptCommands::Join(SCM channel, SCM key)
235 VERIFY_STRING(channel);
237 if (SCM_STRINGP(key))
238 k = Utils::scm2String(key);
239 Message m = Commands::Join(Interp::bot, Utils::scm2String(channel),
241 return scm_long2num(m.getCode());
245 ScriptCommands::Keep(SCM channel, SCM modes)
247 VERIFY_STRING(channel);
248 VERIFY_STRING(modes);
249 Message m = Commands::Keep(Interp::bot, Utils::scm2String(channel),
250 Utils::scm2String(modes));
251 return scm_long2num(m.getCode());
255 ScriptCommands::Kick(SCM channel, SCM who, SCM reason)
257 VERIFY_STRING(channel);
261 if (SCM_STRINGP(reason))
262 r = Utils::scm2String(reason);
264 Message m = Commands::Kick(Interp::bot, Utils::scm2String(channel),
265 Utils::scm2String(who), r);
266 return scm_long2num(m.getCode());
270 ScriptCommands::KickBan(SCM channel, SCM who, SCM reason)
272 VERIFY_STRING(channel);
275 if (SCM_STRINGP(reason))
276 r = Utils::scm2String(reason);
277 Message m = Commands::KickBan(Interp::bot, Utils::scm2String(channel),
278 Utils::scm2String(who), r);
279 return scm_long2num(m.getCode());
283 ScriptCommands::Lock(SCM channel)
285 VERIFY_STRING(channel);
286 Message m = Commands::Lock(Interp::bot, Utils::scm2String(channel));
287 return scm_long2num(m.getCode());
291 ScriptCommands::LogPort(void)
293 return Interp::bot->botInterp->logPort;
297 ScriptCommands::Mode(SCM channel, SCM mode)
299 VERIFY_STRING(channel);
301 Message m = Commands::Mode(Interp::bot, Utils::scm2String(channel),
302 Utils::scm2String(mode));
303 return scm_long2num(m.getCode());
307 ScriptCommands::Msg(SCM nick, SCM message)
310 VERIFY_STRING(message);
311 Message m = Commands::Msg(Interp::bot, Utils::scm2String(nick),
312 Utils::scm2String(message));
313 return scm_long2num(m.getCode());
318 ScriptCommands::NextServer(void)
320 Message m = Commands::NextServer(Interp::bot);
321 return scm_long2num(m.getCode());
325 ScriptCommands::Nick(SCM nick)
328 Message m = Commands::Nick(Interp::bot, Utils::scm2String(nick));
329 return scm_long2num(m.getCode());
333 ScriptCommands::Op(SCM channel, SCM who)
335 VERIFY_STRING(channel);
337 Message m = Commands::Op(Interp::bot, Utils::scm2String(channel),
338 Utils::scm2String(who));
339 return scm_long2num(m.getCode());
343 ScriptCommands::Part(SCM channel)
345 VERIFY_STRING(channel);
346 Message m = Commands::Part(Interp::bot, Utils::scm2String(channel));
347 return scm_long2num(m.getCode());
351 ScriptCommands::Reconnect(void)
353 Message m = Commands::Reconnect(Interp::bot);
354 return scm_long2num(m.getCode());
358 ScriptCommands::Say(SCM channel, SCM message)
360 VERIFY_STRING(channel);
361 VERIFY_STRING(message);
362 Message m = Commands::Say(Interp::bot, Utils::scm2String(channel),
363 Utils::scm2String(message));
364 return scm_long2num(m.getCode());
368 ScriptCommands::Server(SCM number)
370 VERIFY_NUMBER(number);
371 Message m = Commands::Server(Interp::bot, gh_scm2long(number));
372 return scm_long2num(m.getCode());
376 ScriptCommands::SetVersion(SCM version)
378 Message m = Commands::SetVersion(Interp::bot, Utils::scm2String(version));
379 return scm_long2num(m.getCode());
383 ScriptCommands::TBan(SCM channel, SCM who, SCM seconds)
385 VERIFY_STRING(channel);
387 VERIFY_NUMBER(seconds);
388 Message m = Commands::TBan(Interp::bot, Utils::scm2String(channel),
389 Utils::scm2String(who), gh_scm2long(seconds));
390 return scm_long2num(m.getCode());
394 ScriptCommands::TKBan(SCM channel, SCM who, SCM seconds, SCM reason)
396 VERIFY_STRING(channel);
398 VERIFY_NUMBER(seconds);
400 if (SCM_STRINGP(reason))
401 r = Utils::scm2String(reason);
402 Message m = Commands::TKBan(Interp::bot, Utils::scm2String(channel),
403 Utils::scm2String(who),
404 gh_scm2long(seconds), r);
405 return scm_long2num(m.getCode());
409 ScriptCommands::Topic(SCM channel, SCM topic)
411 VERIFY_STRING(channel);
412 VERIFY_STRING(topic);
413 Message m = Commands::Topic(Interp::bot, Utils::scm2String(channel),
414 Utils::scm2String(topic));
415 return scm_long2num(m.getCode());
419 ScriptCommands::Unlock(SCM channel)
421 VERIFY_STRING(channel);
422 Message m = Commands::Unlock(Interp::bot, Utils::scm2String(channel));
423 return scm_long2num(m.getCode());
430 ScriptCommands::getNickname(void)
432 return Utils::string2SCM(Interp::bot->nickName);
436 ScriptCommands::getServer(void)
438 ::Server *serv = Interp::bot->serverList->currentServer();
439 int serverNumber = Interp::bot->serverList->currentNumber;
441 return gh_list(scm_long2num(serverNumber),
442 Utils::string2SCM(serv->getHostName()),
443 scm_long2num(serv->getPort()),
444 Utils::string2SCM(serv->getPassword()),
449 ScriptCommands::getServerList(void)
451 SCM res = gh_list(SCM_UNDEFINED);
454 std::vector<class Server *>::iterator it = Interp::bot->serverList->begin();
456 for ( ; it != Interp::bot->serverList->end(); ++it) {
458 res = gh_append2(res,
459 gh_list(gh_list(scm_long2num(i++),
460 Utils::string2SCM(s->getHostName()),
461 scm_long2num(s->getPort()),
462 Utils::string2SCM(s->getPassword()),
463 SCM_UNDEFINED), SCM_UNDEFINED));
469 ScriptCommands::flushQueue(void)
471 if (!Interp::bot->serverConnection)
473 Interp::bot->serverConnection->queue->flush();
478 ScriptCommands::flushPort(void)
480 scm_flush(Interp::bot->botInterp->logPort);
485 ScriptCommands::random(SCM scm_max)
489 if (SCM_NUMBERP(scm_max))
490 max = gh_scm2int(scm_max);
491 return scm_long2num(max ? rand() % max : 0);
495 ScriptCommands::addCommand(SCM scm_commandName, SCM scm_function,
496 SCM scm_needsChannel, SCM scm_args,
499 // We check that commandName is a string
500 if (!SCM_STRINGP(scm_commandName))
503 // We check that the command does not exist
504 String commandName = Utils::scm2String(scm_commandName).toUpper();
505 std::list<class userFunction *>::iterator it =
506 Interp::bot->userFunctions.begin();
507 for ( ; it != Interp::bot->userFunctions.end(); ++it) {
508 if ((*it)->name == commandName)
512 // Next we check that needsChannel is a boolean
513 if (!gh_boolean_p(scm_needsChannel))
515 bool needsChannel = (bool)gh_scm2bool(scm_needsChannel);
517 // We check that minLevel is an integer and that it's
519 if (!SCM_NUMBERP(scm_minLevel))
522 int minLevel = gh_scm2long(scm_minLevel);
523 if (minLevel < User::NONE || minLevel > User::MASTER)
526 // We check that "scm_function" is a Scheme procedure
527 if (!gh_procedure_p(scm_function))
530 // We check that args is an integer and is between 0 and 20 (arbitrary limit)
531 if (!SCM_NUMBERP(scm_args))
534 int args = gh_scm2long(scm_args);
535 if (args < 0 || args > 20)
538 // We add the command in the commands list
539 Interp::bot->userFunctions.push_back(new userFunction(commandName,
540 (void (*)(ServerConnection *, Person *, String, String))0, minLevel, needsChannel, args, scm_function));
546 ScriptCommands::delCommand(SCM scm_commandName)
548 // We check that commandName is a string
549 if (!SCM_STRINGP(scm_commandName))
552 // We check that the command does exist
553 String commandName = Utils::scm2String(scm_commandName).toUpper();
554 std::list<class userFunction *>::iterator it =
555 Interp::bot->userFunctions.begin();
556 for ( ; it != Interp::bot->userFunctions.end(); ++it) {
557 if ((*it)->name == commandName)
561 if (it == Interp::bot->userFunctions.end())
564 // We delete the command
565 Interp::bot->userFunctions.erase(it);
572 ScriptCommands::AddHook(SCM type, SCM regex, SCM function, SCM pri, SCM fall)
575 bool fallt = true; // does this hook fall through?
577 if (!SCM_UNBNDP (pri))
578 priority = scm_num2int (pri, SCM_ARG1, "ScriptCommands::AddHook");
579 if (!SCM_UNBNDP (fall))
580 fallt = SCM_NFALSEP (fall);
581 return SCM_BOOL (Interp::bot->botInterp->AddHook(gh_scm2long(type),
587 ScriptCommands::AddTimer(SCM when, SCM function)
589 return Interp::bot->botInterp->AddTimer(gh_scm2long(when), function);
593 ScriptCommands::DelTimer(SCM timer)
595 return gh_bool2scm(Interp::bot->botInterp->DelTimer(timer));