[project @ 2005-06-29 20:19:45 by unknown_lamer]
[clinton/bobotpp.git] / source / Parser.C
1 // Parser.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2003,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 02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <sys/types.h>
24 #include <netinet/in.h>
25
26 #include "StringTokenizer.H"
27 #include "Parser.H"
28 #include "UserCommands.H"
29 #include "Commands.H"
30 #include "Macros.H"
31 #include "Utils.H"
32 #include "ShitList.H"
33
34 typedef void (*fptr) (ServerConnection *, Person *, String);
35 std::map < std::string, fptr, std::less < std::string > >Parser::functions;
36
37 void
38 Parser::init ()
39 {
40 // 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;
72 }
73
74
75 void
76 Parser::parseLine (ServerConnection * cnx, String line)
77 {
78 StringTokenizer st (line);
79 Person *from = 0;
80 #ifdef USESCRIPTS
81 cnx->bot->botInterp->RunHooks (Hook::RAW, line,
82 scm_listify (Utils::
83 str2scm (line),
84 SCM_UNDEFINED));
85 #endif
86 if (line[0] == ':')
87 {
88 String fromMask = st.next_token ().substr (1);
89 if (fromMask.find ('!') != -1)
90 from = new Person (cnx->bot, fromMask);
91 }
92
93 String command = st.next_token ();
94 String rest = st.rest ();
95
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);
101
102 if (cit != functions.end ())
103 {
104 fptr temp_func = cit->second;
105 temp_func (cnx, from, rest);
106 }
107
108 delete from;
109 }
110
111 void
112 Parser::parse001 (ServerConnection * cnx, Person * from, String rest)
113 {
114 String temp = "";
115 StringTokenizer st (rest);
116 String realNick = st.next_token ();
117 if ((cnx->bot->nickName).toLower () != realNick.toLower ())
118 {
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,
124 3, true, -1, "");
125 cnx->bot->lastNickNameChange = time (0);
126 cnx->bot->rehash ();
127 }
128
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 ()) + ").");
142 }
143
144 void
145 Parser::parse302 (ServerConnection * cnx, Person * from, String rest)
146 {
147 unsigned long num = cnx->bot->receivedUserhostID++;
148 StringTokenizer st (rest);
149 st.next_token (':');
150 if (st.rest ().length ())
151 {
152 st.next_token ('=');
153 String parameters = st.rest ();
154 parameters = parameters.substr (1);
155 cnx->bot->userhostMap[num] = parameters;
156 }
157 else
158 cnx->bot->userhostMap[num] = "";
159 }
160
161 void
162 Parser::parse311 (ServerConnection * cnx, Person * from, String rest)
163 {
164 StringTokenizer st (rest);
165 st.next_token ();
166 String nuh = st.next_token () + "!";
167 String uh = st.next_token () + "@";
168 uh = uh + st.next_token ();
169 nuh = nuh + uh;
170 cnx->bot->userList->addUserFirst (nuh, "*", 0, 3, true, -1, "");
171 cnx->bot->userHost = uh;
172 }
173
174 void
175 Parser::parse315 (ServerConnection * cnx, Person * from, String rest)
176 {
177 StringTokenizer st (rest);
178 st.next_token ();
179 String channel = st.next_token ();
180 Channel *c = cnx->bot->channelList->getChannel (channel);
181 if (!c)
182 return;
183 c->gotWho = true;
184 }
185
186 void
187 Parser::parse324 (ServerConnection * cnx, Person * from, String rest)
188 {
189 StringTokenizer st (rest);
190 st.next_token ();
191 String channel = st.next_token ();
192 if (Channel * c = cnx->bot->channelList->getChannel (channel))
193 if (c)
194 c->parseMode (from, st.rest ());
195 }
196
197 void
198 Parser::parse332 (ServerConnection * cnx, Person * from, String rest)
199 {
200 StringTokenizer st (rest);
201 st.next_token ();
202 String channel = st.next_token ();
203 if (Channel * c = cnx->bot->channelList->getChannel (channel))
204 if (c)
205 c->channelTopic = st.rest ().substr (1);
206 }
207
208 void
209 Parser::parse352 (ServerConnection * cnx, Person * from, String rest)
210 {
211 StringTokenizer st (rest);
212 st.next_token ();
213 String ch = st.next_token ();
214 String uh = st.next_token () + "@";
215 uh = uh + st.next_token ();
216 st.next_token ();
217 String n = st.next_token ();
218 String m = st.next_token ();
219 int mode = 0;
220 for (int i = 0; i < m.length (); i++)
221 switch (m[i])
222 {
223 case 'H':
224 break;
225 case 'G':
226 mode |= User::AWAY_MODE;
227 break;
228 case '*':
229 mode |= User::IRCOP_MODE;
230 break;
231 case '@':
232 mode |= User::OP_MODE;
233 break;
234 case '+':
235 mode |= User::VOICE_MODE;
236 break;
237 }
238 if (Channel * c = cnx->bot->channelList->getChannel (ch))
239 if (c)
240 c->addNick (n, uh, mode, cnx->bot->userList);
241 }
242
243 void
244 Parser::parse353 (ServerConnection * cnx, Person * from, String rest)
245 {
246 int mode = 0;
247 String nick;
248 StringTokenizer st (rest);
249 st.next_token ();
250 st.next_token ();
251 Channel *c = cnx->bot->channelList->getChannel (st.next_token ());
252 if (!c)
253 return;
254 StringTokenizer st2 (st.next_token (':'));
255 while (st2.more_tokens_p ())
256 {
257 nick = st2.next_token ();
258 if (nick[0] == '@')
259 {
260 mode = User::OP_MODE;
261 nick = nick.substr (1);
262 }
263 else if (nick[0] == '+')
264 {
265 mode = User::VOICE_MODE;
266 nick = nick.substr (1);
267 }
268 c->addNick (nick, "", mode, 0, true);
269 }
270 }
271
272 void
273 Parser::parse366 (ServerConnection * cnx, Person * from, String rest)
274 {
275 StringTokenizer st (rest);
276 st.next_token ();
277 String ch = st.next_token ();
278 if (Channel * c = cnx->bot->channelList->getChannel (ch))
279 c->joined = true;
280 }
281
282 void
283 Parser::parse367 (ServerConnection * cnx, Person * from, String rest)
284 {
285 StringTokenizer st (rest);
286 st.next_token ();
287 String ch = st.next_token ();
288 if (Channel * c = cnx->bot->channelList->getChannel (ch))
289 c->addBan (st.next_token (), -1);
290 }
291
292 void
293 Parser::parse401 (ServerConnection * cnx, Person * from, String rest)
294 {
295 StringTokenizer st (rest);
296 st.next_token ();
297 String nick = st.next_token ();
298 if (cnx->bot->spyList.find (nick) != cnx->bot->spyList.end ())
299 {
300 delete cnx->bot->spyList[nick];
301 cnx->bot->spyList.erase (nick);
302 }
303 }
304
305 void
306 Parser::parse433 (ServerConnection * cnx, Person * from, String rest)
307 {
308 if (cnx->bot->connected)
309 return;
310 if (cnx->bot->nickName.length () == 9)
311 {
312 int i;
313 for (i = 0;
314 i < cnx->bot->nickName.length ()
315 && cnx->bot->nickName[i] == '_'; i++);
316 if (i < cnx->bot->nickName.length ())
317 cnx->bot->nickName =
318 cnx->bot->nickName.substr (0,
319 i - 1) + "_" +
320 cnx->bot->nickName.substr (i + 1);
321 else
322 cnx->bot->nickName = cnx->bot->nickName.substr (0, 4) +
323 String ((long) (rand () % 10000));
324 }
325 else
326 cnx->bot->nickName = cnx->bot->nickName + "_";
327 cnx->queue->sendNick (cnx->bot->nickName);
328 }
329
330 void
331 Parser::parse473 (ServerConnection * cnx, Person * from, String rest)
332 {
333 StringTokenizer st (rest);
334 st.next_token ();
335 cnx->bot->logLine (String ("Unable to join channel ") +
336 st.next_token () + ".");
337 }
338
339 void
340 Parser::parseError (ServerConnection * cnx, Person * from, String rest)
341 {
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 ();
348 }
349
350 void
351 Parser::parseInvite (ServerConnection * cnx, Person * from, String rest)
352 {
353 String nick = from->getNick ();
354 StringTokenizer st (rest);
355 st.next_token (':');
356 String channel = st.rest ();
357 #ifdef USESCRIPTS
358 cnx->bot->botInterp->RunHooks (Hook::INVITE,
359 nick + " " + channel,
360 scm_listify (Utils::
361 str2scm (nick),
362 Utils::
363 str2scm
364 (channel), SCM_UNDEFINED));
365 #endif
366 if (cnx->bot->wantedChannels.find (channel) !=
367 cnx->bot->wantedChannels.end ())
368 cnx->queue->sendJoin (channel, cnx->bot->wantedChannels[channel]->key);
369 }
370
371 void
372 Parser::parseJoin (ServerConnection * cnx, Person * from, String rest)
373 {
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 (':');
379 String mode;
380 bool joinAndMode = false;
381 #ifdef USESCRIPTS
382 cnx->bot->botInterp->RunHooks (Hook::JOIN, n + " " + c,
383 scm_listify (Utils::
384 str2scm (n),
385 Utils::
386 str2scm (c), SCM_UNDEFINED));
387 #endif
388 // This part of code is for the combined JOIN & MODE of ircd 2.9
389 if (c.find ('\007') >= 0)
390 {
391 joinAndMode = true;
392 StringTokenizer st3 (c);
393 c = st3.next_token ('\007');
394 String m = st3.rest ();
395 mode = c + " +" + m;
396 for (int i = 0; i < m.length (); i++)
397 mode = mode + " " + n;
398 }
399
400 if (n == cnx->bot->nickName)
401 {
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);
407 else
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);
412 }
413 else
414 {
415 Channel *ch = cnx->bot->channelList->getChannel (c);
416 if (!ch)
417 return;
418 ShitEntry *se = cnx->bot->shitList->getShit (n + "!" + uh, c);
419 if (se && se->isStillValid () &&
420 se->getShitLevel () >= ShitEntry::SHIT_NOJOIN)
421 {
422 cnx->queue->sendChannelMode (c, "+b", se->getMask ());
423 cnx->queue->sendKick (c, n, se->getShitReason ());
424 return;
425 }
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))
429 {
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);
434 }
435 }
436
437 if (joinAndMode)
438 parseMode (cnx, 0, mode);
439 }
440
441 void
442 Parser::parseKick (ServerConnection * cnx, Person * from, String rest)
443 {
444 StringTokenizer st (rest);
445 String channel = st.next_token ();
446 String target = st.next_token ();
447 String reason = st.rest ().substr (1);
448 #ifdef USESCRIPTS
449 cnx->bot->botInterp->RunHooks (Hook::KICK,
450 target + " " +
451 from->getNick () + " " +
452 channel + " " + reason,
453 scm_listify (Utils::
454 str2scm
455 (target),
456 Utils::
457 str2scm (from->
458 getNick
459 ()),
460 Utils::
461 str2scm
462 (channel),
463 Utils::
464 str2scm
465 (reason), SCM_UNDEFINED));
466 #endif
467 if (target == cnx->bot->nickName)
468 {
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);
476 }
477 else
478 {
479 if (!cnx->bot->channelList->getChannel (channel))
480 return;
481 User *u = cnx->bot->channelList->getChannel (channel)->getUser (target);
482 if (u && u->getProt () >= User::NO_KICK)
483 {
484 String fromNick = from->getNick ();
485 User *v =
486 cnx->bot->channelList->getChannel (channel)->getUser (fromNick);
487 if (v->getProt () < User::NO_KICK)
488 {
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");
494 }
495 }
496 cnx->bot->channelList->getChannel (channel)->delNick (target);
497 }
498 }
499
500 void
501 Parser::parseMode (ServerConnection * cnx, Person * from, String rest)
502 {
503 StringTokenizer st (rest);
504 String ch = st.next_token ();
505 String modes = st.rest ();
506 #ifdef USESCRIPTS
507 if (from)
508 cnx->bot->botInterp->RunHooks (Hook::MODE,
509 from->getNick () + " " + ch +
510 " " + modes,
511 scm_listify (Utils::
512 str2scm (from->
513 getNick
514 ()),
515 Utils::
516 str2scm (ch),
517 Utils::
518 str2scm (modes),
519 SCM_UNDEFINED));
520 #endif
521 if (Utils::channel_p (ch))
522 {
523 Channel *c = cnx->bot->channelList->getChannel (ch);
524 if (!c)
525 return;
526 if (from)
527 c->parseMode (from, modes);
528 else
529 c->parseMode (0, modes);
530 }
531 }
532
533 void
534 Parser::parseNick (ServerConnection * cnx, Person * from, String rest)
535 {
536 String on_orig = from->getNick ();
537 String on = on_orig.toLower ();
538 String nn = rest.substr (1);
539 String nn_lower = nn.toLower ();
540 #ifdef USESCRIPTS
541 cnx->bot->botInterp->RunHooks (Hook::NICKNAME,
542 on_orig + " " + nn,
543 scm_listify (Utils::
544 str2scm
545 (on_orig),
546 Utils::
547 str2scm (nn),
548 SCM_UNDEFINED));
549 #endif
550 if ((cnx->bot->nickName).toLower () == on)
551 {
552 cnx->bot->userList->removeFirst ();
553 cnx->bot->userList->addUserFirst (nn + "!" +
554 cnx->bot->userHost, "*", 0,
555 3, true, -1, "");
556 cnx->bot->lastNickNameChange = time (0);
557 cnx->bot->nickName = nn;
558 cnx->bot->rehash ();
559 }
560
561 if (cnx->bot->spyList.find (on) != cnx->bot->spyList.end ())
562 {
563 cnx->bot->spyList[nn_lower] = cnx->bot->spyList[on];
564 cnx->bot->spyList.erase (on);
565 }
566
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);
573 }
574
575 void
576 Parser::parseNotice (ServerConnection * cnx, Person * from, String rest)
577 {
578 String nick = "";
579 if (from)
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')
585 {
586 #ifdef USESCRIPTS
587 if (Utils::channel_p (to))
588 cnx->bot->botInterp->RunHooks (Hook::PUBLIC_NOTICE,
589 nick + " " + to + " " + rest,
590 scm_listify (Utils::
591 str2scm (nick),
592 Utils::
593 str2scm (to),
594 Utils::
595 str2scm (rest),
596 SCM_UNDEFINED));
597 else
598 cnx->bot->botInterp->RunHooks (Hook::NOTICE, nick + " " + rest,
599 scm_listify (Utils::
600 str2scm (nick),
601 Utils::
602 str2scm (rest),
603 SCM_UNDEFINED));
604 #endif
605 return;
606 }
607
608 rest = rest.substr (1, rest.length () - 2);
609 StringTokenizer st2 (rest);
610 String command = st2.next_token ();
611 rest = st2.rest ();
612 #ifdef USESCRIPTS
613 cnx->bot->botInterp->RunHooks (Hook::CTCP_REPLY,
614 nick + " " + command + " " +
615 rest,
616 scm_listify (Utils::
617 str2scm (nick),
618 Utils::
619 str2scm
620 (command),
621 Utils::
622 str2scm (rest),
623 SCM_UNDEFINED));
624 #endif
625 if (command == "PING")
626 {
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)->
633 getAop ()
634 && !(cnx->bot->channelList->getChannel (c)->
635 getUser (nick)->mode & User::OP_MODE)
636 && cnx->bot->channelList->getChannel (c)->getUser (nick)->
637 userkey == rest)
638 cnx->queue->sendChannelMode (c, "+o", nick);
639 }
640 }
641
642 void
643 Parser::parsePrivmsg (ServerConnection * cnx, Person * from, String rest)
644 {
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)
651 {
652 if (cnx->bot->ignoredUserhosts[fromUserhost] == Bot::MAX_MESSAGES + 1)
653 {
654 #ifdef USESCRIPTS
655 cnx->bot->botInterp->RunHooks (Hook::FLOOD, nick,
656 scm_listify (Utils::
657 str2scm (nick),
658 SCM_UNDEFINED));
659 #endif
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))
664 from->
665 sendNotice (String ("\002You are now being ignored for ") +
666 String ((long) Bot::IGNORE_DELAY) +
667 " seconds.\002");
668 }
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")
676 {
677 cnx->bot->ignoredUserhosts[fromUserhost] = 0;
678 from->sendNotice ("\002Don't do it again!\002");
679 }
680 return;
681 }
682
683 if (rest[0] == '\001')
684 {
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 ") +
691 nick + ": " + rest);
692 Parser::parseCTCP (cnx, from, to, rest);
693 }
694 else
695 {
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);
706 }
707 }
708
709 void
710 Parser::parsePart (ServerConnection * cnx, Person * from, String rest)
711 {
712 String n = from->getNick ();
713 StringTokenizer st (rest);
714 String channel = st.next_token ();
715 #ifdef USESCRIPTS
716 cnx->bot->botInterp->RunHooks (Hook::LEAVE, n + " " + channel,
717 scm_listify (Utils::
718 str2scm (n),
719 Utils::
720 str2scm
721 (channel), SCM_UNDEFINED));
722 #endif
723 if (n.toLower () == cnx->bot->nickName.toLower ())
724 {
725 cnx->bot->logLine (String ("Leaved channel ") + channel + ".");
726 cnx->bot->channelList->delChannel (channel);
727 }
728 else
729 {
730 Channel *c = cnx->bot->channelList->getChannel (channel);
731 if (!c)
732 return;
733 c->delNick (n);
734 if (c->countOp == 0 && c->count == 1)
735 {
736 cnx->queue->sendPart (channel);
737 cnx->queue->sendJoin (channel,
738 cnx->bot->wantedChannels[channel]->key);
739 }
740 }
741 }
742
743 void
744 Parser::parsePing (ServerConnection * cnx, Person * from, String rest)
745 {
746 cnx->queue->sendPong (rest);
747 }
748
749 void
750 Parser::parsePong (ServerConnection * cnx, Person * from, String rest)
751 {
752 cnx->lag = (cnx->lag + 2 * (time (NULL) - cnx->pingTime)) / 3;
753 cnx->bot->sentPing = false;
754 }
755
756 void
757 Parser::parseQuit (ServerConnection * cnx, Person * from, String rest)
758 {
759 String n = from->getNick ();
760 #ifdef USESCRIPTS
761 cnx->bot->botInterp->RunHooks (Hook::SIGNOFF, n + " " + rest,
762 scm_listify (Utils::
763 str2scm (n),
764 Utils::
765 str2scm (rest),
766 SCM_UNDEFINED));
767 #endif
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);
775 }
776
777 void
778 Parser::parseTopic (ServerConnection * cnx, Person * from, String rest)
779 {
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);
784 #ifdef USESCRIPTS
785 cnx->bot->botInterp->RunHooks (Hook::TOPIC,
786 from->getNick () + " " +
787 channel + " " + newTopic,
788 scm_listify (Utils::
789 str2scm (from->
790 getNick
791 ()),
792 Utils::
793 str2scm
794 (channel),
795 Utils::
796 str2scm
797 (newTopic), SCM_UNDEFINED));
798 #endif
799 if (!c)
800 return;
801 if (c->lockedTopic && from->getNick () != cnx->bot->nickName)
802 cnx->queue->sendTopic (channel, c->channelTopic);
803 c->channelTopic = newTopic;
804 }
805
806 void
807 Parser::parseCTCP (ServerConnection * cnx,
808 Person * from, String to, String parameters)
809 {
810 StringTokenizer st (parameters);
811 String command = Utils::to_upper (st.next_token ());
812 String nick = from->getNick ();
813 String rest;
814 if (st.more_tokens_p ())
815 rest = st.rest ();
816 else
817 rest = "";
818 #ifdef USESCRIPTS
819 cnx->bot->botInterp->RunHooks (Hook::CTCP,
820 nick + " " + to + " " +
821 command + " " + rest,
822 scm_listify (Utils::
823 str2scm (nick),
824 Utils::
825 str2scm (to),
826 Utils::
827 str2scm
828 (command),
829 Utils::
830 str2scm (rest),
831 SCM_UNDEFINED));
832 #endif
833 if (command == "PING")
834 {
835 Commands::CTCPReply (cnx->bot, nick, "PING", rest);
836 }
837 else if (command == "VERSION")
838 {
839 Commands::CTCPReply (cnx->bot, nick, "VERSION",
840 cnx->bot->versionString);
841 }
842 else if (command == "CLOCK")
843 {
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)) +
848 "d" +
849 String ((long) (diff % 86400) /
850 3600) + "h" +
851 String ((long) (diff % 3600) / 60) +
852 "m" + String ((long) (diff % 60)) + "s");
853 }
854 else if (command == "COMMAND")
855 {
856 Commands::CTCPReply (cnx->bot, nick,
857 "COMMAND", String (cnx->bot->commandChar));
858 }
859 else if (command == "LAG")
860 {
861 Commands::CTCPReply (cnx->bot, nick, "LAG",
862 String ((long) cnx->lag) + " second(s)");
863 }
864 else if (command == "DCC")
865 {
866 StringTokenizer st2 (rest);
867 command = Utils::to_upper (st2.next_token ());
868 if (command == "CHAT")
869 {
870 // FIXME: debug DCC
871 st2.next_token ();
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);
877 else
878 cnx->bot->logLine ("DCC Chat Failed in Parser");
879 }
880 }
881 #ifdef USESCRIPTS
882 else if (command == "ACTION")
883 {
884 cnx->bot->botInterp->RunHooks (Hook::ACTION,
885 from->getNick () + " " + to +
886 " " + rest,
887 scm_listify (Utils::
888 str2scm (from->
889 getNick
890 ()),
891 Utils::
892 str2scm (to),
893 Utils::
894 str2scm (rest),
895 SCM_UNDEFINED));
896 }
897 #endif
898 }
899
900 void
901 Parser::parseMessage (ServerConnection * cnx,
902 Person * from, String to, String parameters)
903 {
904 #ifdef USESCRIPTS
905 if (Utils::channel_p (to))
906 cnx->bot->botInterp->RunHooks (Hook::PUBLIC,
907 from->getNick () + " " + to +
908 " " + parameters,
909 scm_listify (Utils::
910 str2scm (from->
911 getNick
912 ()),
913 Utils::
914 str2scm (to),
915 Utils::
916 str2scm
917 (parameters), SCM_UNDEFINED));
918 else
919 cnx->bot->botInterp->RunHooks (Hook::MESSAGE,
920 from->getNick () + " " +
921 parameters,
922 scm_listify (Utils::
923 str2scm (from->
924 getNick
925 ()),
926 Utils::
927 str2scm
928 (parameters), SCM_UNDEFINED));
929 #endif
930 if (parameters[0] != cnx->bot->commandChar)
931 return;
932
933 StringTokenizer st (parameters);
934 String command = Utils::to_upper (st.next_token ().substr (1));
935 String rest = Utils::trim_str (st.rest ());
936 int level;
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;
942
943 if (uf_iter != cnx->bot->userFunctions.end ())
944 f = uf_iter->second;
945 else
946 return;
947
948 if (f)
949 {
950 if (f->needsChannelName)
951 {
952 if (Utils::channel_p (rest))
953 {
954 StringTokenizer st2 (rest);
955 to = st.next_token ();
956 rest = st.rest ();
957 }
958 if (!Utils::channel_p (to))
959 {
960 from->sendNotice ("\002You need to supply a channel name"
961 " for this command\002");
962 return;
963 }
964 if (!cnx->bot->channelList->getChannel (to))
965 {
966 from->sendNotice (String ("\002I am not on channel\002 ") +
967 to);
968 return;
969 }
970 level = Utils::get_level (cnx->bot, from->getAddress (), to);
971 User *u = 0;
972 if (Channel * c = cnx->bot->channelList->getChannel (to))
973 u = c->getUser (from->getNick ());
974 if (!u || !u->userListItem)
975 identified = true;
976 else
977 identified = u->userListItem->passwd == ""
978 || u->userListItem->identified > 0;
979 }
980 else
981 {
982 level = Utils::get_level (cnx->bot, from->getAddress ());
983 identified = true;
984 }
985 if (level >= f->minLevel)
986 {
987 cnx->bot->logLine (from->getAddress () + " did " + command +
988 " " + rest);
989 #ifdef USESCRIPTS
990 if (f->argsCount != -1)
991 {
992 Parser::parseScriptFunction (cnx, to, f->needsChannelName,
993 f->scmFunc, f->argsCount,
994 rest);
995 }
996 else
997 {
998 f->function (cnx, from, to, rest);
999 }
1000 #else
1001 f->function (cnx, from, to, rest);
1002 #endif
1003 }
1004 else
1005 {
1006 if (!identified)
1007 from->
1008 sendNotice (String
1009 ("\002You are not identified on channel\002 ") +
1010 to);
1011 }
1012 }
1013 }
1014
1015 #ifdef USESCRIPTS
1016 void
1017 Parser::parseScriptFunction (ServerConnection * cnx,
1018 String channel,
1019 bool needsChannelName,
1020 SCM scmFunc, int argsCount, String parameters)
1021 {
1022 String param;
1023 SCM args_list = scm_listify (SCM_UNDEFINED);
1024 if (needsChannelName)
1025 {
1026 args_list = gh_append2 (args_list,
1027 scm_listify (Utils::
1028 str2scm (channel),
1029 SCM_UNDEFINED));
1030 argsCount--;
1031 }
1032
1033 StringTokenizer st (parameters);
1034 for (int i = argsCount; i > 0; i--)
1035 {
1036 if (i == 1)
1037 param = st.rest ();
1038 else
1039 param = st.next_token ();
1040 args_list = gh_append2 (args_list,
1041 scm_listify (Utils::str2scm (param),
1042 SCM_UNDEFINED));
1043 }
1044
1045 struct wrapper_data wd;
1046 wd.func = scmFunc;
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);
1051 }
1052 #endif