1 // Parser.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2003,2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <sys/types.h>
24 #include <netinet/in.h>
26 #include "StringTokenizer.H"
28 #include "UserCommands.H"
34 typedef void (*fptr) (ServerConnection *, Person *, String);
35 std::map < std::string, fptr, std::less < std::string > >Parser::functions;
41 Parser::functions["001"] = Parser::parse001; /* RPL_WELCOME */
42 Parser::functions["302"] = Parser::parse302; /* RPL_USERHOST */
43 Parser::functions["311"] = Parser::parse311; /* RPL_WHOISUSER */
44 Parser::functions["315"] = Parser::parse315; /* RPL_ENDOFWHO */
45 Parser::functions["324"] = Parser::parse324; /* RPL_CHANNELMODEIS */
46 Parser::functions["332"] = Parser::parse332; /* RPL_TOPIC */
47 Parser::functions["352"] = Parser::parse352; /* RPL_WHOREPLY */
48 Parser::functions["353"] = Parser::parse353; /* RPL_NAMESREPLY */
49 Parser::functions["366"] = Parser::parse366; /* RPL_ENDOFNAMES */
50 Parser::functions["367"] = Parser::parse367; /* RPL_BANLIST */
51 Parser::functions["401"] = Parser::parse401; /* ERR_NOSUCHNICK */
52 Parser::functions["433"] = Parser::parse433; /* ERR_NICKNAMEINUSE */
53 Parser::functions["437"] = Parser::parse433; /* ERR_UNAVAILRESOURCE */
54 Parser::functions["471"] = Parser::parse473; /* ERR_CHANNELISFULL */
55 Parser::functions["473"] = Parser::parse473; /* ERR_INVITEONLYCHAN */
56 Parser::functions["474"] = Parser::parse473; /* ERR_BANNEDFROMCHAN */
57 Parser::functions["475"] = Parser::parse473; /* ERR_BADCHANNELKEY */
58 Parser::functions["ERROR"] = Parser::parseError;
59 Parser::functions["INVITE"] = Parser::parseInvite;
60 Parser::functions["JOIN"] = Parser::parseJoin;
61 Parser::functions["KICK"] = Parser::parseKick;
62 Parser::functions["MODE"] = Parser::parseMode;
63 Parser::functions["NICK"] = Parser::parseNick;
64 Parser::functions["NOTICE"] = Parser::parseNotice;
65 Parser::functions["PART"] = Parser::parsePart;
66 Parser::functions["PING"] = Parser::parsePing;
67 Parser::functions["PONG"] = Parser::parsePong;
68 Parser::functions["PRIVMSG"] = Parser::parsePrivmsg;
69 Parser::functions["QUIT"] = Parser::parseQuit;
70 Parser::functions["TOPIC"] = Parser::parseTopic;
71 Parser::functions[""] = Parser::parseError;
76 Parser::parseLine (ServerConnection * cnx, String line)
78 StringTokenizer st (line);
81 cnx->bot->botInterp->RunHooks (Hook::RAW, line,
88 String fromMask = st.next_token ().substr (1);
89 if (fromMask.find ('!') != -1)
90 from = new Person (cnx->bot, fromMask);
93 String command = st.next_token ();
94 String rest = st.rest ();
96 // We must use map<>::find or else a new entry will be created in
97 // the map which will cause another lookup of the same invalid
98 // command to segfault the bot
99 std::map<std::string, fptr, std::less<std::string> >::const_iterator cit
100 = functions.find (command);
102 if (cit != functions.end ())
104 fptr temp_func = cit->second;
105 temp_func (cnx, from, rest);
112 Parser::parse001 (ServerConnection * cnx, Person * from, String rest)
115 StringTokenizer st (rest);
116 String realNick = st.next_token ();
117 if ((cnx->bot->nickName).toLower () != realNick.toLower ())
119 // Yes, this can happen, and it was a very subtle bug
120 cnx->bot->nickName = realNick;
121 cnx->bot->userList->removeFirst ();
122 cnx->bot->userList->addUserFirst (realNick + "!" +
123 cnx->bot->userHost, "*", 0,
125 cnx->bot->lastNickNameChange = time (0);
129 cnx->bot->connected = true;
130 cnx->queue->sendUserMode (cnx->bot->nickName, "+i");
131 cnx->queue->sendWhois (cnx->bot->nickName);
132 for (std::map < String, wantedChannel *,
133 std::less < String > >::iterator it =
134 cnx->bot->wantedChannels.begin ();
135 it != cnx->bot->wantedChannels.end (); ++it)
136 cnx->queue->sendJoin ((*it).first, (*it).second->key);
137 cnx->bot->logLine (String ("Connected to server ") +
138 cnx->bot->serverList->currentServer ()->
139 getHostName () + " (" +
140 String ((long) cnx->bot->serverList->
141 currentServer ()->getPort ()) + ").");
145 Parser::parse302 (ServerConnection * cnx, Person * from, String rest)
147 unsigned long num = cnx->bot->receivedUserhostID++;
148 StringTokenizer st (rest);
150 if (st.rest ().length ())
153 String parameters = st.rest ();
154 parameters = parameters.substr (1);
155 cnx->bot->userhostMap[num] = parameters;
158 cnx->bot->userhostMap[num] = "";
162 Parser::parse311 (ServerConnection * cnx, Person * from, String rest)
164 StringTokenizer st (rest);
166 String nuh = st.next_token () + "!";
167 String uh = st.next_token () + "@";
168 uh = uh + st.next_token ();
170 cnx->bot->userList->addUserFirst (nuh, "*", 0, 3, true, -1, "");
171 cnx->bot->userHost = uh;
175 Parser::parse315 (ServerConnection * cnx, Person * from, String rest)
177 StringTokenizer st (rest);
179 String channel = st.next_token ();
180 Channel *c = cnx->bot->channelList->getChannel (channel);
187 Parser::parse324 (ServerConnection * cnx, Person * from, String rest)
189 StringTokenizer st (rest);
191 String channel = st.next_token ();
192 if (Channel * c = cnx->bot->channelList->getChannel (channel))
194 c->parseMode (from, st.rest ());
198 Parser::parse332 (ServerConnection * cnx, Person * from, String rest)
200 StringTokenizer st (rest);
202 String channel = st.next_token ();
203 if (Channel * c = cnx->bot->channelList->getChannel (channel))
205 c->channelTopic = st.rest ().substr (1);
209 Parser::parse352 (ServerConnection * cnx, Person * from, String rest)
211 StringTokenizer st (rest);
213 String ch = st.next_token ();
214 String uh = st.next_token () + "@";
215 uh = uh + st.next_token ();
217 String n = st.next_token ();
218 String m = st.next_token ();
220 for (int i = 0; i < m.length (); i++)
226 mode |= User::AWAY_MODE;
229 mode |= User::IRCOP_MODE;
232 mode |= User::OP_MODE;
235 mode |= User::VOICE_MODE;
238 if (Channel * c = cnx->bot->channelList->getChannel (ch))
240 c->addNick (n, uh, mode, cnx->bot->userList);
244 Parser::parse353 (ServerConnection * cnx, Person * from, String rest)
248 StringTokenizer st (rest);
251 Channel *c = cnx->bot->channelList->getChannel (st.next_token ());
254 StringTokenizer st2 (st.next_token (':'));
255 while (st2.more_tokens_p ())
257 nick = st2.next_token ();
260 mode = User::OP_MODE;
261 nick = nick.substr (1);
263 else if (nick[0] == '+')
265 mode = User::VOICE_MODE;
266 nick = nick.substr (1);
268 c->addNick (nick, "", mode, 0, true);
273 Parser::parse366 (ServerConnection * cnx, Person * from, String rest)
275 StringTokenizer st (rest);
277 String ch = st.next_token ();
278 if (Channel * c = cnx->bot->channelList->getChannel (ch))
283 Parser::parse367 (ServerConnection * cnx, Person * from, String rest)
285 StringTokenizer st (rest);
287 String ch = st.next_token ();
288 if (Channel * c = cnx->bot->channelList->getChannel (ch))
289 c->addBan (st.next_token (), -1);
293 Parser::parse401 (ServerConnection * cnx, Person * from, String rest)
295 StringTokenizer st (rest);
297 String nick = st.next_token ();
298 if (cnx->bot->spyList.find (nick) != cnx->bot->spyList.end ())
300 delete cnx->bot->spyList[nick];
301 cnx->bot->spyList.erase (nick);
306 Parser::parse433 (ServerConnection * cnx, Person * from, String rest)
308 if (cnx->bot->connected)
310 if (cnx->bot->nickName.length () == 9)
314 i < cnx->bot->nickName.length ()
315 && cnx->bot->nickName[i] == '_'; i++);
316 if (i < cnx->bot->nickName.length ())
318 cnx->bot->nickName.substr (0,
320 cnx->bot->nickName.substr (i + 1);
322 cnx->bot->nickName = cnx->bot->nickName.substr (0, 4) +
323 String ((long) (rand () % 10000));
326 cnx->bot->nickName = cnx->bot->nickName + "_";
327 cnx->queue->sendNick (cnx->bot->nickName);
331 Parser::parse473 (ServerConnection * cnx, Person * from, String rest)
333 StringTokenizer st (rest);
335 cnx->bot->logLine (String ("Unable to join channel ") +
336 st.next_token () + ".");
340 Parser::parseError (ServerConnection * cnx, Person * from, String rest)
342 cnx->bot->logLine (String ("Error from server ") +
343 cnx->bot->serverList->currentServer ()->
344 getHostName () + " (" +
345 String ((long) cnx->bot->serverList->
346 currentServer ()->getPort ()) + ").");
347 cnx->bot->nextServer ();
351 Parser::parseInvite (ServerConnection * cnx, Person * from, String rest)
353 String nick = from->getNick ();
354 StringTokenizer st (rest);
356 String channel = st.rest ();
358 cnx->bot->botInterp->RunHooks (Hook::INVITE,
359 nick + " " + channel,
364 (channel), SCM_UNDEFINED));
366 if (cnx->bot->wantedChannels.find (channel) !=
367 cnx->bot->wantedChannels.end ())
368 cnx->queue->sendJoin (channel, cnx->bot->wantedChannels[channel]->key);
372 Parser::parseJoin (ServerConnection * cnx, Person * from, String rest)
374 StringTokenizer st (from->getAddress ());
375 String n = st.next_token ('!');
376 String uh = st.next_token ();
377 StringTokenizer st2 (rest);
378 String c = st2.next_token (':');
380 bool joinAndMode = false;
382 cnx->bot->botInterp->RunHooks (Hook::JOIN, n + " " + c,
386 str2scm (c), SCM_UNDEFINED));
388 // This part of code is for the combined JOIN & MODE of ircd 2.9
389 if (c.find ('\007') >= 0)
392 StringTokenizer st3 (c);
393 c = st3.next_token ('\007');
394 String m = st3.rest ();
396 for (int i = 0; i < m.length (); i++)
397 mode = mode + " " + n;
400 if (n == cnx->bot->nickName)
402 cnx->bot->logLine (String ("Joined channel ") + c + ".");
403 if (cnx->bot->wantedChannels.find (c) !=
404 cnx->bot->wantedChannels.end ())
405 cnx->bot->channelList->
406 addChannel (cnx, c, cnx->bot->wantedChannels[c]->keep);
408 cnx->bot->channelList->addChannel (cnx, c);
409 cnx->queue->sendWho (c);
410 cnx->queue->sendChannelMode (String ("MODE ") + c + " b");
411 cnx->queue->sendChannelMode (String ("MODE ") + c);
415 Channel *ch = cnx->bot->channelList->getChannel (c);
418 ShitEntry *se = cnx->bot->shitList->getShit (n + "!" + uh, c);
419 if (se && se->isStillValid () &&
420 se->getShitLevel () >= ShitEntry::SHIT_NOJOIN)
422 cnx->queue->sendChannelMode (c, "+b", se->getMask ());
423 cnx->queue->sendKick (c, n, se->getShitReason ());
426 ch->addNick (n, uh, 0, cnx->bot->userList);
427 if (ch->getUser (n)->getAop ()
428 && !(ch->getUser (n)->mode & User::OP_MODE) && cnx->bot->iAmOp (c))
430 // This is a part of the antispoof code
431 ch->getUser(n)->userkey = Utils::get_key ();
432 Commands::CTCP (cnx->bot, n, "PING",
433 ch->getUser(n)->userkey + " " + c);
438 parseMode (cnx, 0, mode);
442 Parser::parseKick (ServerConnection * cnx, Person * from, String rest)
444 StringTokenizer st (rest);
445 String channel = st.next_token ();
446 String target = st.next_token ();
447 String reason = st.rest ().substr (1);
449 cnx->bot->botInterp->RunHooks (Hook::KICK,
451 from->getNick () + " " +
452 channel + " " + reason,
465 (reason), SCM_UNDEFINED));
467 if (target == cnx->bot->nickName)
469 cnx->bot->logLine (from->getAddress () +
470 " kicked me out of channel " + channel +
471 " (" + reason + ").");
472 cnx->queue->sendJoin (channel,
473 cnx->bot->channelList->
474 getChannel (channel)->channelKey);
475 cnx->bot->channelList->delChannel (channel);
479 if (!cnx->bot->channelList->getChannel (channel))
481 User *u = cnx->bot->channelList->getChannel (channel)->getUser (target);
482 if (u && u->getProt () >= User::NO_KICK)
484 String fromNick = from->getNick ();
486 cnx->bot->channelList->getChannel (channel)->getUser (fromNick);
487 if (v->getProt () < User::NO_KICK)
489 cnx->bot->logLine (from->getAddress () + " kicked " + target +
490 " (protected) out of channel " + channel +
491 " (" + reason + ").");
492 cnx->queue->sendKick (channel, fromNick,
493 target + " \002is protected !\002");
496 cnx->bot->channelList->getChannel (channel)->delNick (target);
501 Parser::parseMode (ServerConnection * cnx, Person * from, String rest)
503 StringTokenizer st (rest);
504 String ch = st.next_token ();
505 String modes = st.rest ();
508 cnx->bot->botInterp->RunHooks (Hook::MODE,
509 from->getNick () + " " + ch +
521 if (Utils::channel_p (ch))
523 Channel *c = cnx->bot->channelList->getChannel (ch);
527 c->parseMode (from, modes);
529 c->parseMode (0, modes);
534 Parser::parseNick (ServerConnection * cnx, Person * from, String rest)
536 String on_orig = from->getNick ();
537 String on = on_orig.toLower ();
538 String nn = rest.substr (1);
539 String nn_lower = nn.toLower ();
541 cnx->bot->botInterp->RunHooks (Hook::NICKNAME,
550 if ((cnx->bot->nickName).toLower () == on)
552 cnx->bot->userList->removeFirst ();
553 cnx->bot->userList->addUserFirst (nn + "!" +
554 cnx->bot->userHost, "*", 0,
556 cnx->bot->lastNickNameChange = time (0);
557 cnx->bot->nickName = nn;
561 if (cnx->bot->spyList.find (on) != cnx->bot->spyList.end ())
563 cnx->bot->spyList[nn_lower] = cnx->bot->spyList[on];
564 cnx->bot->spyList.erase (on);
567 for (std::map < String, Channel *,
568 std::less < String > >::iterator it =
569 cnx->bot->channelList->begin ();
570 it != cnx->bot->channelList->end (); ++it)
571 if ((*it).second->hasNick (on))
572 (*it).second->changeNick (on, nn_lower);
576 Parser::parseNotice (ServerConnection * cnx, Person * from, String rest)
580 nick = from->getNick ();
581 StringTokenizer st (rest);
582 String to = st.next_token ();
583 rest = st.rest ().substr (1);
584 if (rest[0] != '\001')
587 if (Utils::channel_p (to))
588 cnx->bot->botInterp->RunHooks (Hook::PUBLIC_NOTICE,
589 nick + " " + to + " " + rest,
598 cnx->bot->botInterp->RunHooks (Hook::NOTICE, nick + " " + rest,
608 rest = rest.substr (1, rest.length () - 2);
609 StringTokenizer st2 (rest);
610 String command = st2.next_token ();
613 cnx->bot->botInterp->RunHooks (Hook::CTCP_REPLY,
614 nick + " " + command + " " +
625 if (command == "PING")
627 StringTokenizer st3 (rest);
628 rest = st3.next_token ();
629 String c = st3.rest ();
630 if (cnx->bot->channelList->getChannel (c) &&
631 cnx->bot->channelList->getChannel (c)->getUser (nick) &&
632 cnx->bot->channelList->getChannel (c)->getUser (nick)->
634 && !(cnx->bot->channelList->getChannel (c)->
635 getUser (nick)->mode & User::OP_MODE)
636 && cnx->bot->channelList->getChannel (c)->getUser (nick)->
638 cnx->queue->sendChannelMode (c, "+o", nick);
643 Parser::parsePrivmsg (ServerConnection * cnx, Person * from, String rest)
645 String nick = from->getNick ();
646 StringTokenizer st (rest);
647 String to = st.next_token ();
648 String fromUserhost = Utils::get_userhost (from->getAddress ());
649 rest = st.rest ().substr (1);
650 if (++(cnx->bot->ignoredUserhosts[fromUserhost]) > Bot::MAX_MESSAGES)
652 if (cnx->bot->ignoredUserhosts[fromUserhost] == Bot::MAX_MESSAGES + 1)
655 cnx->bot->botInterp->RunHooks (Hook::FLOOD, nick,
660 cnx->bot->ignoredUserhosts[fromUserhost] += Bot::IGNORE_DELAY;
661 cnx->bot->logLine (from->getAddress () +
662 " is flooding me. We will ignore him/her/it.");
663 if (!Utils::channel_p (to))
665 sendNotice (String ("\002You are now being ignored for ") +
666 String ((long) Bot::IGNORE_DELAY) +
669 // The following lines reset the counter if you use the
670 // command "!sorry" (if '!' is your command char).
671 // This is not documented, I know. But one probably does
672 // not want that every users can bypass the flood control
673 // Of course, if you want this feature to remain 'secret',
674 // do not use it in public.
675 if (rest.toUpper () == String (cnx->bot->commandChar) + "SORRY")
677 cnx->bot->ignoredUserhosts[fromUserhost] = 0;
678 from->sendNotice ("\002Don't do it again!\002");
683 if (rest[0] == '\001')
685 rest = rest.substr (1, rest.length () - 2);
686 if (!Utils::channel_p (to))
687 for (std::map < String, Person *,
688 std::less < String > >::iterator it =
689 cnx->bot->spyList.begin (); it != cnx->bot->spyList.end (); ++it)
690 (*it).second->sendNotice (String ("CTCP From ") +
692 Parser::parseCTCP (cnx, from, to, rest);
696 if ((rest.length () < 5 ||
697 rest.substr (1, 5).toUpper () != "IDENT") &&
698 (rest.length () < 8 ||
699 rest.substr (1, 8).toUpper () != "PASSWORD") &&
700 !Utils::channel_p (to))
701 for (std::map < String, Person *,
702 std::less < String > >::iterator it =
703 cnx->bot->spyList.begin (); it != cnx->bot->spyList.end (); ++it)
704 (*it).second->sendNotice (String ("*") + nick + "* " + rest);
705 Parser::parseMessage (cnx, from, to, rest);
710 Parser::parsePart (ServerConnection * cnx, Person * from, String rest)
712 String n = from->getNick ();
713 StringTokenizer st (rest);
714 String channel = st.next_token ();
716 cnx->bot->botInterp->RunHooks (Hook::LEAVE, n + " " + channel,
721 (channel), SCM_UNDEFINED));
723 if (n.toLower () == cnx->bot->nickName.toLower ())
725 cnx->bot->logLine (String ("Leaved channel ") + channel + ".");
726 cnx->bot->channelList->delChannel (channel);
730 Channel *c = cnx->bot->channelList->getChannel (channel);
734 if (c->countOp == 0 && c->count == 1)
736 cnx->queue->sendPart (channel);
737 cnx->queue->sendJoin (channel,
738 cnx->bot->wantedChannels[channel]->key);
744 Parser::parsePing (ServerConnection * cnx, Person * from, String rest)
746 cnx->queue->sendPong (rest);
750 Parser::parsePong (ServerConnection * cnx, Person * from, String rest)
752 cnx->lag = (cnx->lag + 2 * (time (NULL) - cnx->pingTime)) / 3;
753 cnx->bot->sentPing = false;
757 Parser::parseQuit (ServerConnection * cnx, Person * from, String rest)
759 String n = from->getNick ();
761 cnx->bot->botInterp->RunHooks (Hook::SIGNOFF, n + " " + rest,
768 if (n == cnx->bot->nickName)
769 cnx->bot->stop = true;
770 for (std::map < String, Channel *,
771 std::less < String > >::iterator it =
772 cnx->bot->channelList->begin ();
773 it != cnx->bot->channelList->end (); ++it)
774 (*it).second->delNick (n);
778 Parser::parseTopic (ServerConnection * cnx, Person * from, String rest)
780 StringTokenizer st (rest);
781 String channel = st.next_token ();
782 String newTopic = st.rest ().substr (1);
783 Channel *c = cnx->bot->channelList->getChannel (channel);
785 cnx->bot->botInterp->RunHooks (Hook::TOPIC,
786 from->getNick () + " " +
787 channel + " " + newTopic,
797 (newTopic), SCM_UNDEFINED));
801 if (c->lockedTopic && from->getNick () != cnx->bot->nickName)
802 cnx->queue->sendTopic (channel, c->channelTopic);
803 c->channelTopic = newTopic;
807 Parser::parseCTCP (ServerConnection * cnx,
808 Person * from, String to, String parameters)
810 StringTokenizer st (parameters);
811 String command = Utils::to_upper (st.next_token ());
812 String nick = from->getNick ();
814 if (st.more_tokens_p ())
819 cnx->bot->botInterp->RunHooks (Hook::CTCP,
820 nick + " " + to + " " +
821 command + " " + rest,
833 if (command == "PING")
835 Commands::CTCPReply (cnx->bot, nick, "PING", rest);
837 else if (command == "VERSION")
839 Commands::CTCPReply (cnx->bot, nick, "VERSION",
840 cnx->bot->versionString);
842 else if (command == "CLOCK")
844 time_t diff = time (NULL) - cnx->bot->startTime;
845 Commands::CTCPReply (cnx->bot, nick, "CLOCK",
846 String ("elapsed time: ") +
847 String ((long) (diff / 86400)) +
849 String ((long) (diff % 86400) /
851 String ((long) (diff % 3600) / 60) +
852 "m" + String ((long) (diff % 60)) + "s");
854 else if (command == "COMMAND")
856 Commands::CTCPReply (cnx->bot, nick,
857 "COMMAND", String (cnx->bot->commandChar));
859 else if (command == "LAG")
861 Commands::CTCPReply (cnx->bot, nick, "LAG",
862 String ((long) cnx->lag) + " second(s)");
864 else if (command == "DCC")
866 StringTokenizer st2 (rest);
867 command = Utils::to_upper (st2.next_token ());
868 if (command == "CHAT")
872 unsigned long address =
873 std::strtoul (st2.next_token ().c_str(), 0, 0);
874 int port = std::atoi (st2.next_token().c_str());
875 if (port >= 1024 && Utils::get_level (cnx->bot, from->getAddress ()))
876 cnx->bot->addDCC (from, address, port, Bot::CHAT);
878 cnx->bot->logLine ("DCC Chat Failed in Parser");
882 else if (command == "ACTION")
884 cnx->bot->botInterp->RunHooks (Hook::ACTION,
885 from->getNick () + " " + to +
901 Parser::parseMessage (ServerConnection * cnx,
902 Person * from, String to, String parameters)
905 if (Utils::channel_p (to))
906 cnx->bot->botInterp->RunHooks (Hook::PUBLIC,
907 from->getNick () + " " + to +
917 (parameters), SCM_UNDEFINED));
919 cnx->bot->botInterp->RunHooks (Hook::MESSAGE,
920 from->getNick () + " " +
928 (parameters), SCM_UNDEFINED));
930 if (parameters[0] != cnx->bot->commandChar)
933 StringTokenizer st (parameters);
934 String command = Utils::to_upper (st.next_token ().substr (1));
935 String rest = Utils::trim_str (st.rest ());
937 bool identified = false;
938 std::map<std::string, class userFunction*,
939 std::less<std::string> >::const_iterator uf_iter
940 = cnx->bot->userFunctions.find (command);
941 userFunction * f = 0;
943 if (uf_iter != cnx->bot->userFunctions.end ())
950 if (f->needsChannelName)
952 if (Utils::channel_p (rest))
954 StringTokenizer st2 (rest);
955 to = st.next_token ();
958 if (!Utils::channel_p (to))
960 from->sendNotice ("\002You need to supply a channel name"
961 " for this command\002");
964 if (!cnx->bot->channelList->getChannel (to))
966 from->sendNotice (String ("\002I am not on channel\002 ") +
970 level = Utils::get_level (cnx->bot, from->getAddress (), to);
972 if (Channel * c = cnx->bot->channelList->getChannel (to))
973 u = c->getUser (from->getNick ());
974 if (!u || !u->userListItem)
977 identified = u->userListItem->passwd == ""
978 || u->userListItem->identified > 0;
982 level = Utils::get_level (cnx->bot, from->getAddress ());
985 if (level >= f->minLevel)
987 cnx->bot->logLine (from->getAddress () + " did " + command +
990 if (f->argsCount != -1)
992 Parser::parseScriptFunction (cnx, to, f->needsChannelName,
993 f->scmFunc, f->argsCount,
998 f->function (cnx, from, to, rest);
1001 f->function (cnx, from, to, rest);
1009 ("\002You are not identified on channel\002 ") +
1017 Parser::parseScriptFunction (ServerConnection * cnx,
1019 bool needsChannelName,
1020 SCM scmFunc, int argsCount, String parameters)
1023 SCM args_list = scm_listify (SCM_UNDEFINED);
1024 if (needsChannelName)
1026 args_list = gh_append2 (args_list,
1027 scm_listify (Utils::
1033 StringTokenizer st (parameters);
1034 for (int i = argsCount; i > 0; i--)
1039 param = st.next_token ();
1040 args_list = gh_append2 (args_list,
1041 scm_listify (Utils::str2scm (param),
1045 struct wrapper_data wd;
1047 wd.args = args_list;
1048 scm_internal_catch (SCM_BOOL_T,
1049 (scm_t_catch_body) lazy_apply_wrapper, (void *) &wd,
1050 (scm_t_catch_handler) empty_handler, 0);