[project @ 2005-06-28 10:57:28 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 "Macros.H"
30 #include "Utils.H"
31 #include "ShitList.H"
32
33 typedef void (*fptr) (ServerConnection *, Person *, String);
34 std::map < std::string, fptr, std::less < std::string > >Parser::functions;
35
36 void
37 Parser::init ()
38 {
39 // Parser functions
40 Parser::functions["001"] = Parser::parse001; /* RPL_WELCOME */
41 Parser::functions["302"] = Parser::parse302; /* RPL_USERHOST */
42 Parser::functions["311"] = Parser::parse311; /* RPL_WHOISUSER */
43 Parser::functions["315"] = Parser::parse315; /* RPL_ENDOFWHO */
44 Parser::functions["324"] = Parser::parse324; /* RPL_CHANNELMODEIS */
45 Parser::functions["332"] = Parser::parse332; /* RPL_TOPIC */
46 Parser::functions["352"] = Parser::parse352; /* RPL_WHOREPLY */
47 Parser::functions["353"] = Parser::parse353; /* RPL_NAMESREPLY */
48 Parser::functions["366"] = Parser::parse366; /* RPL_ENDOFNAMES */
49 Parser::functions["367"] = Parser::parse367; /* RPL_BANLIST */
50 Parser::functions["401"] = Parser::parse401; /* ERR_NOSUCHNICK */
51 Parser::functions["433"] = Parser::parse433; /* ERR_NICKNAMEINUSE */
52 Parser::functions["437"] = Parser::parse433; /* ERR_UNAVAILRESOURCE */
53 Parser::functions["471"] = Parser::parse473; /* ERR_CHANNELISFULL */
54 Parser::functions["473"] = Parser::parse473; /* ERR_INVITEONLYCHAN */
55 Parser::functions["474"] = Parser::parse473; /* ERR_BANNEDFROMCHAN */
56 Parser::functions["475"] = Parser::parse473; /* ERR_BADCHANNELKEY */
57 Parser::functions["ERROR"] = Parser::parseError;
58 Parser::functions["INVITE"] = Parser::parseInvite;
59 Parser::functions["JOIN"] = Parser::parseJoin;
60 Parser::functions["KICK"] = Parser::parseKick;
61 Parser::functions["MODE"] = Parser::parseMode;
62 Parser::functions["NICK"] = Parser::parseNick;
63 Parser::functions["NOTICE"] = Parser::parseNotice;
64 Parser::functions["PART"] = Parser::parsePart;
65 Parser::functions["PING"] = Parser::parsePing;
66 Parser::functions["PONG"] = Parser::parsePong;
67 Parser::functions["PRIVMSG"] = Parser::parsePrivmsg;
68 Parser::functions["QUIT"] = Parser::parseQuit;
69 Parser::functions["TOPIC"] = Parser::parseTopic;
70 Parser::functions[""] = Parser::parseError;
71 }
72
73
74 void
75 Parser::parseLine (ServerConnection * cnx, String line)
76 {
77 StringTokenizer st (line);
78 Person *from = 0;
79 #ifdef USESCRIPTS
80 cnx->bot->botInterp->RunHooks (Hook::RAW, line,
81 scm_listify (Utils::
82 str2scm (line),
83 SCM_UNDEFINED));
84 #endif
85 if (line[0] == ':')
86 {
87 String fromMask = st.next_token ().substr (1);
88 if (fromMask.find ('!') != -1)
89 from = new Person (cnx->bot, fromMask);
90 }
91
92 String command = st.next_token ();
93 String rest = st.rest ();
94
95 // We must use map<>::find or else a new entry will be created in
96 // the map which will cause another lookup of the same invalid
97 // command to segfault the bot
98 std::map<std::string, fptr, std::less<std::string> >::const_iterator cit
99 = functions.find (command);
100
101 if (cit != functions.end ())
102 {
103 fptr temp_func = cit->second;
104 temp_func (cnx, from, rest);
105 }
106
107 delete from;
108 }
109
110 void
111 Parser::parse001 (ServerConnection * cnx, Person * from, String rest)
112 {
113 String temp = "";
114 StringTokenizer st (rest);
115 String realNick = st.next_token ();
116 if ((cnx->bot->nickName).toLower () != realNick.toLower ())
117 {
118 // Yes, this can happen, and it was a very subtle bug
119 cnx->bot->nickName = realNick;
120 cnx->bot->userList->removeFirst ();
121 cnx->bot->userList->addUserFirst (realNick + "!" +
122 cnx->bot->userHost, "*", 0,
123 3, true, -1, "");
124 cnx->bot->lastNickNameChange = time (0);
125 cnx->bot->rehash ();
126 }
127
128 cnx->bot->connected = true;
129 cnx->queue->sendUserMode (cnx->bot->nickName, "+i");
130 cnx->queue->sendWhois (cnx->bot->nickName);
131 for (std::map < String, wantedChannel *,
132 std::less < String > >::iterator it =
133 cnx->bot->wantedChannels.begin ();
134 it != cnx->bot->wantedChannels.end (); ++it)
135 cnx->queue->sendJoin ((*it).first, (*it).second->key);
136 cnx->bot->logLine (String ("Connected to server ") +
137 cnx->bot->serverList->currentServer ()->
138 getHostName () + " (" +
139 String ((long) cnx->bot->serverList->
140 currentServer ()->getPort ()) + ").");
141 }
142
143 void
144 Parser::parse302 (ServerConnection * cnx, Person * from, String rest)
145 {
146 unsigned long num = cnx->bot->receivedUserhostID++;
147 StringTokenizer st (rest);
148 st.next_token (':');
149 if (st.rest ().length ())
150 {
151 st.next_token ('=');
152 String parameters = st.rest ();
153 parameters = parameters.substr (1);
154 cnx->bot->userhostMap[num] = parameters;
155 }
156 else
157 cnx->bot->userhostMap[num] = "";
158 }
159
160 void
161 Parser::parse311 (ServerConnection * cnx, Person * from, String rest)
162 {
163 StringTokenizer st (rest);
164 st.next_token ();
165 String nuh = st.next_token () + "!";
166 String uh = st.next_token () + "@";
167 uh = uh + st.next_token ();
168 nuh = nuh + uh;
169 cnx->bot->userList->addUserFirst (nuh, "*", 0, 3, true, -1, "");
170 cnx->bot->userHost = uh;
171 }
172
173 void
174 Parser::parse315 (ServerConnection * cnx, Person * from, String rest)
175 {
176 StringTokenizer st (rest);
177 st.next_token ();
178 String channel = st.next_token ();
179 Channel *c = cnx->bot->channelList->getChannel (channel);
180 if (!c)
181 return;
182 c->gotWho = true;
183 }
184
185 void
186 Parser::parse324 (ServerConnection * cnx, Person * from, String rest)
187 {
188 StringTokenizer st (rest);
189 st.next_token ();
190 String channel = st.next_token ();
191 if (Channel * c = cnx->bot->channelList->getChannel (channel))
192 if (c)
193 c->parseMode (from, st.rest ());
194 }
195
196 void
197 Parser::parse332 (ServerConnection * cnx, Person * from, String rest)
198 {
199 StringTokenizer st (rest);
200 st.next_token ();
201 String channel = st.next_token ();
202 if (Channel * c = cnx->bot->channelList->getChannel (channel))
203 if (c)
204 c->channelTopic = st.rest ().substr (1);
205 }
206
207 void
208 Parser::parse352 (ServerConnection * cnx, Person * from, String rest)
209 {
210 StringTokenizer st (rest);
211 st.next_token ();
212 String ch = st.next_token ();
213 String uh = st.next_token () + "@";
214 uh = uh + st.next_token ();
215 st.next_token ();
216 String n = st.next_token ();
217 String m = st.next_token ();
218 int mode = 0;
219 for (int i = 0; i < m.length (); i++)
220 switch (m[i])
221 {
222 case 'H':
223 break;
224 case 'G':
225 mode |= User::AWAY_MODE;
226 break;
227 case '*':
228 mode |= User::IRCOP_MODE;
229 break;
230 case '@':
231 mode |= User::OP_MODE;
232 break;
233 case '+':
234 mode |= User::VOICE_MODE;
235 break;
236 }
237 if (Channel * c = cnx->bot->channelList->getChannel (ch))
238 if (c)
239 c->addNick (n, uh, mode, cnx->bot->userList);
240 }
241
242 void
243 Parser::parse353 (ServerConnection * cnx, Person * from, String rest)
244 {
245 int mode = 0;
246 String nick;
247 StringTokenizer st (rest);
248 st.next_token ();
249 st.next_token ();
250 Channel *c = cnx->bot->channelList->getChannel (st.next_token ());
251 if (!c)
252 return;
253 StringTokenizer st2 (st.next_token (':'));
254 while (st2.more_tokens_p ())
255 {
256 nick = st2.next_token ();
257 if (nick[0] == '@')
258 {
259 mode = User::OP_MODE;
260 nick = nick.substr (1);
261 }
262 else if (nick[0] == '+')
263 {
264 mode = User::VOICE_MODE;
265 nick = nick.substr (1);
266 }
267 c->addNick (nick, "", mode, 0, true);
268 }
269 }
270
271 void
272 Parser::parse366 (ServerConnection * cnx, Person * from, String rest)
273 {
274 StringTokenizer st (rest);
275 st.next_token ();
276 String ch = st.next_token ();
277 if (Channel * c = cnx->bot->channelList->getChannel (ch))
278 c->joined = true;
279 }
280
281 void
282 Parser::parse367 (ServerConnection * cnx, Person * from, String rest)
283 {
284 StringTokenizer st (rest);
285 st.next_token ();
286 String ch = st.next_token ();
287 if (Channel * c = cnx->bot->channelList->getChannel (ch))
288 c->addBan (st.next_token (), -1);
289 }
290
291 void
292 Parser::parse401 (ServerConnection * cnx, Person * from, String rest)
293 {
294 StringTokenizer st (rest);
295 st.next_token ();
296 String nick = st.next_token ();
297 if (cnx->bot->spyList.find (nick) != cnx->bot->spyList.end ())
298 {
299 delete cnx->bot->spyList[nick];
300 cnx->bot->spyList.erase (nick);
301 }
302 }
303
304 void
305 Parser::parse433 (ServerConnection * cnx, Person * from, String rest)
306 {
307 if (cnx->bot->connected)
308 return;
309 if (cnx->bot->nickName.length () == 9)
310 {
311 int i;
312 for (i = 0;
313 i < cnx->bot->nickName.length ()
314 && cnx->bot->nickName[i] == '_'; i++);
315 if (i < cnx->bot->nickName.length ())
316 cnx->bot->nickName =
317 cnx->bot->nickName.substr (0,
318 i - 1) + "_" +
319 cnx->bot->nickName.substr (i + 1);
320 else
321 cnx->bot->nickName = cnx->bot->nickName.substr (0, 4) +
322 String ((long) (rand () % 10000));
323 }
324 else
325 cnx->bot->nickName = cnx->bot->nickName + "_";
326 cnx->queue->sendNick (cnx->bot->nickName);
327 }
328
329 void
330 Parser::parse473 (ServerConnection * cnx, Person * from, String rest)
331 {
332 StringTokenizer st (rest);
333 st.next_token ();
334 cnx->bot->logLine (String ("Unable to join channel ") +
335 st.next_token () + ".");
336 }
337
338 void
339 Parser::parseError (ServerConnection * cnx, Person * from, String rest)
340 {
341 cnx->bot->logLine (String ("Error from server ") +
342 cnx->bot->serverList->currentServer ()->
343 getHostName () + " (" +
344 String ((long) cnx->bot->serverList->
345 currentServer ()->getPort ()) + ").");
346 cnx->bot->nextServer ();
347 }
348
349 void
350 Parser::parseInvite (ServerConnection * cnx, Person * from, String rest)
351 {
352 String nick = from->getNick ();
353 StringTokenizer st (rest);
354 st.next_token (':');
355 String channel = st.rest ();
356 #ifdef USESCRIPTS
357 cnx->bot->botInterp->RunHooks (Hook::INVITE,
358 nick + " " + channel,
359 scm_listify (Utils::
360 str2scm (nick),
361 Utils::
362 str2scm
363 (channel), SCM_UNDEFINED));
364 #endif
365 if (cnx->bot->wantedChannels.find (channel) !=
366 cnx->bot->wantedChannels.end ())
367 cnx->queue->sendJoin (channel, cnx->bot->wantedChannels[channel]->key);
368 }
369
370 void
371 Parser::parseJoin (ServerConnection * cnx, Person * from, String rest)
372 {
373 StringTokenizer st (from->getAddress ());
374 String n = st.next_token ('!');
375 String uh = st.next_token ();
376 StringTokenizer st2 (rest);
377 String c = st2.next_token (':');
378 String mode;
379 bool joinAndMode = false;
380 #ifdef USESCRIPTS
381 cnx->bot->botInterp->RunHooks (Hook::JOIN, n + " " + c,
382 scm_listify (Utils::
383 str2scm (n),
384 Utils::
385 str2scm (c), SCM_UNDEFINED));
386 #endif
387 // This part of code is for the combined JOIN & MODE of ircd 2.9
388 if (c.find ('\007') >= 0)
389 {
390 joinAndMode = true;
391 StringTokenizer st3 (c);
392 c = st3.next_token ('\007');
393 String m = st3.rest ();
394 mode = c + " +" + m;
395 for (int i = 0; i < m.length (); i++)
396 mode = mode + " " + n;
397 }
398
399 if (n == cnx->bot->nickName)
400 {
401 cnx->bot->logLine (String ("Joined channel ") + c + ".");
402 if (cnx->bot->wantedChannels.find (c) !=
403 cnx->bot->wantedChannels.end ())
404 cnx->bot->channelList->
405 addChannel (cnx, c, cnx->bot->wantedChannels[c]->keep);
406 else
407 cnx->bot->channelList->addChannel (cnx, c);
408 cnx->queue->sendWho (c);
409 cnx->queue->sendChannelMode (String ("MODE ") + c + " b");
410 cnx->queue->sendChannelMode (String ("MODE ") + c);
411 }
412 else
413 {
414 Channel *ch = cnx->bot->channelList->getChannel (c);
415 if (!ch)
416 return;
417 ShitEntry *se = cnx->bot->shitList->getShit (n + "!" + uh, c);
418 if (se && se->isStillValid () &&
419 se->getShitLevel () >= ShitEntry::SHIT_NOJOIN)
420 {
421 cnx->queue->sendChannelMode (c, "+b", se->getMask ());
422 cnx->queue->sendKick (c, n, se->getShitReason ());
423 return;
424 }
425 ch->addNick (n, uh, 0, cnx->bot->userList);
426 if (ch->getUser (n)->getAop ()
427 && !(ch->getUser (n)->mode & User::OP_MODE) && cnx->bot->iAmOp (c))
428 {
429 // This is a part of the antispoof code
430 ch->getUser (n)->userkey = Utils::get_key ();
431 Commands::CTCP (cnx->bot, n, "PING",
432 ch->getUser (n)->userkey + " " + c);
433 }
434 }
435
436 if (joinAndMode)
437 parseMode (cnx, 0, mode);
438 }
439
440 void
441 Parser::parseKick (ServerConnection * cnx, Person * from, String rest)
442 {
443 StringTokenizer st (rest);
444 String channel = st.next_token ();
445 String target = st.next_token ();
446 String reason = st.rest ().substr (1);
447 #ifdef USESCRIPTS
448 cnx->bot->botInterp->RunHooks (Hook::KICK,
449 target + " " +
450 from->getNick () + " " +
451 channel + " " + reason,
452 scm_listify (Utils::
453 str2scm
454 (target),
455 Utils::
456 str2scm (from->
457 getNick
458 ()),
459 Utils::
460 str2scm
461 (channel),
462 Utils::
463 str2scm
464 (reason), SCM_UNDEFINED));
465 #endif
466 if (target == cnx->bot->nickName)
467 {
468 cnx->bot->logLine (from->getAddress () +
469 " kicked me out of channel " + channel +
470 " (" + reason + ").");
471 cnx->queue->sendJoin (channel,
472 cnx->bot->channelList->
473 getChannel (channel)->channelKey);
474 cnx->bot->channelList->delChannel (channel);
475 }
476 else
477 {
478 if (!cnx->bot->channelList->getChannel (channel))
479 return;
480 User *u = cnx->bot->channelList->getChannel (channel)->getUser (target);
481 if (u && u->getProt () >= User::NO_KICK)
482 {
483 String fromNick = from->getNick ();
484 User *v =
485 cnx->bot->channelList->getChannel (channel)->getUser (fromNick);
486 if (v->getProt () < User::NO_KICK)
487 {
488 cnx->bot->logLine (from->getAddress () + " kicked " + target +
489 " (protected) out of channel " + channel +
490 " (" + reason + ").");
491 cnx->queue->sendKick (channel, fromNick,
492 target + " \002is protected !\002");
493 }
494 }
495 cnx->bot->channelList->getChannel (channel)->delNick (target);
496 }
497 }
498
499 void
500 Parser::parseMode (ServerConnection * cnx, Person * from, String rest)
501 {
502 StringTokenizer st (rest);
503 String ch = st.next_token ();
504 String modes = st.rest ();
505 #ifdef USESCRIPTS
506 if (from)
507 cnx->bot->botInterp->RunHooks (Hook::MODE,
508 from->getNick () + " " + ch +
509 " " + modes,
510 scm_listify (Utils::
511 str2scm (from->
512 getNick
513 ()),
514 Utils::
515 str2scm (ch),
516 Utils::
517 str2scm (modes),
518 SCM_UNDEFINED));
519 #endif
520 if (Utils::channel_p (ch))
521 {
522 Channel *c = cnx->bot->channelList->getChannel (ch);
523 if (!c)
524 return;
525 if (from)
526 c->parseMode (from, modes);
527 else
528 c->parseMode (0, modes);
529 }
530 }
531
532 void
533 Parser::parseNick (ServerConnection * cnx, Person * from, String rest)
534 {
535 String on_orig = from->getNick ();
536 String on = on_orig.toLower ();
537 String nn = rest.substr (1);
538 String nn_lower = nn.toLower ();
539 #ifdef USESCRIPTS
540 cnx->bot->botInterp->RunHooks (Hook::NICKNAME,
541 on_orig + " " + nn,
542 scm_listify (Utils::
543 str2scm
544 (on_orig),
545 Utils::
546 str2scm (nn),
547 SCM_UNDEFINED));
548 #endif
549 if ((cnx->bot->nickName).toLower () == on)
550 {
551 cnx->bot->userList->removeFirst ();
552 cnx->bot->userList->addUserFirst (nn + "!" +
553 cnx->bot->userHost, "*", 0,
554 3, true, -1, "");
555 cnx->bot->lastNickNameChange = time (0);
556 cnx->bot->nickName = nn;
557 cnx->bot->rehash ();
558 }
559
560 if (cnx->bot->spyList.find (on) != cnx->bot->spyList.end ())
561 {
562 cnx->bot->spyList[nn_lower] = cnx->bot->spyList[on];
563 cnx->bot->spyList.erase (on);
564 }
565
566 for (std::map < String, Channel *,
567 std::less < String > >::iterator it =
568 cnx->bot->channelList->begin ();
569 it != cnx->bot->channelList->end (); ++it)
570 if ((*it).second->hasNick (on))
571 (*it).second->changeNick (on, nn_lower);
572 }
573
574 void
575 Parser::parseNotice (ServerConnection * cnx, Person * from, String rest)
576 {
577 String nick = "";
578 if (from)
579 nick = from->getNick ();
580 StringTokenizer st (rest);
581 String to = st.next_token ();
582 rest = st.rest ().substr (1);
583 if (rest[0] != '\001')
584 {
585 #ifdef USESCRIPTS
586 if (Utils::channel_p (to))
587 cnx->bot->botInterp->RunHooks (Hook::PUBLIC_NOTICE,
588 nick + " " + to + " " + rest,
589 scm_listify (Utils::
590 str2scm (nick),
591 Utils::
592 str2scm (to),
593 Utils::
594 str2scm (rest),
595 SCM_UNDEFINED));
596 else
597 cnx->bot->botInterp->RunHooks (Hook::NOTICE, nick + " " + rest,
598 scm_listify (Utils::
599 str2scm (nick),
600 Utils::
601 str2scm (rest),
602 SCM_UNDEFINED));
603 #endif
604 return;
605 }
606
607 rest = rest.substr (1, rest.length () - 2);
608 StringTokenizer st2 (rest);
609 String command = st2.next_token ();
610 rest = st2.rest ();
611 #ifdef USESCRIPTS
612 cnx->bot->botInterp->RunHooks (Hook::CTCP_REPLY,
613 nick + " " + command + " " +
614 rest,
615 scm_listify (Utils::
616 str2scm (nick),
617 Utils::
618 str2scm
619 (command),
620 Utils::
621 str2scm (rest),
622 SCM_UNDEFINED));
623 #endif
624 if (command == "PING")
625 {
626 StringTokenizer st3 (rest);
627 rest = st3.next_token ();
628 String c = st3.rest ();
629 if (cnx->bot->channelList->getChannel (c) &&
630 cnx->bot->channelList->getChannel (c)->getUser (nick) &&
631 cnx->bot->channelList->getChannel (c)->getUser (nick)->
632 getAop ()
633 && !(cnx->bot->channelList->getChannel (c)->
634 getUser (nick)->mode & User::OP_MODE)
635 && cnx->bot->channelList->getChannel (c)->getUser (nick)->
636 userkey == rest)
637 cnx->queue->sendChannelMode (c, "+o", nick);
638 }
639 }
640
641 void
642 Parser::parsePrivmsg (ServerConnection * cnx, Person * from, String rest)
643 {
644 String nick = from->getNick ();
645 StringTokenizer st (rest);
646 String to = st.next_token ();
647 String fromUserhost = Utils::get_userhost (from->getAddress ());
648 rest = st.rest ().substr (1);
649 if (++(cnx->bot->ignoredUserhosts[fromUserhost]) > Bot::MAX_MESSAGES)
650 {
651 if (cnx->bot->ignoredUserhosts[fromUserhost] == Bot::MAX_MESSAGES + 1)
652 {
653 #ifdef USESCRIPTS
654 cnx->bot->botInterp->RunHooks (Hook::FLOOD, nick,
655 scm_listify (Utils::
656 str2scm (nick),
657 SCM_UNDEFINED));
658 #endif
659 cnx->bot->ignoredUserhosts[fromUserhost] += Bot::IGNORE_DELAY;
660 cnx->bot->logLine (from->getAddress () +
661 " is flooding me. We will ignore him/her/it.");
662 if (!Utils::channel_p (to))
663 from->
664 sendNotice (String ("\002You are now being ignored for ") +
665 String ((long) Bot::IGNORE_DELAY) +
666 " seconds.\002");
667 }
668 // The following lines reset the counter if you use the
669 // command "!sorry" (if '!' is your command char).
670 // This is not documented, I know. But one probably does
671 // not want that every users can bypass the flood control
672 // Of course, if you want this feature to remain 'secret',
673 // do not use it in public.
674 if (rest.toUpper () == String (cnx->bot->commandChar) + "SORRY")
675 {
676 cnx->bot->ignoredUserhosts[fromUserhost] = 0;
677 from->sendNotice ("\002Don't do it again!\002");
678 }
679 return;
680 }
681
682 if (rest[0] == '\001')
683 {
684 rest = rest.substr (1, rest.length () - 2);
685 if (!Utils::channel_p (to))
686 for (std::map < String, Person *,
687 std::less < String > >::iterator it =
688 cnx->bot->spyList.begin (); it != cnx->bot->spyList.end (); ++it)
689 (*it).second->sendNotice (String ("CTCP From ") +
690 nick + ": " + rest);
691 Parser::parseCTCP (cnx, from, to, rest);
692 }
693 else
694 {
695 if ((rest.length () < 5 ||
696 rest.substr (1, 5).toUpper () != "IDENT") &&
697 (rest.length () < 8 ||
698 rest.substr (1, 8).toUpper () != "PASSWORD") &&
699 !Utils::channel_p (to))
700 for (std::map < String, Person *,
701 std::less < String > >::iterator it =
702 cnx->bot->spyList.begin (); it != cnx->bot->spyList.end (); ++it)
703 (*it).second->sendNotice (String ("*") + nick + "* " + rest);
704 Parser::parseMessage (cnx, from, to, rest);
705 }
706 }
707
708 void
709 Parser::parsePart (ServerConnection * cnx, Person * from, String rest)
710 {
711 String n = from->getNick ();
712 StringTokenizer st (rest);
713 String channel = st.next_token ();
714 #ifdef USESCRIPTS
715 cnx->bot->botInterp->RunHooks (Hook::LEAVE, n + " " + channel,
716 scm_listify (Utils::
717 str2scm (n),
718 Utils::
719 str2scm
720 (channel), SCM_UNDEFINED));
721 #endif
722 if (n.toLower () == cnx->bot->nickName.toLower ())
723 {
724 cnx->bot->logLine (String ("Leaved channel ") + channel + ".");
725 cnx->bot->channelList->delChannel (channel);
726 }
727 else
728 {
729 Channel *c = cnx->bot->channelList->getChannel (channel);
730 if (!c)
731 return;
732 c->delNick (n);
733 if (c->countOp == 0 && c->count == 1)
734 {
735 cnx->queue->sendPart (channel);
736 cnx->queue->sendJoin (channel,
737 cnx->bot->wantedChannels[channel]->key);
738 }
739 }
740 }
741
742 void
743 Parser::parsePing (ServerConnection * cnx, Person * from, String rest)
744 {
745 cnx->queue->sendPong (rest);
746 }
747
748 void
749 Parser::parsePong (ServerConnection * cnx, Person * from, String rest)
750 {
751 cnx->lag = (cnx->lag + 2 * (time (NULL) - cnx->pingTime)) / 3;
752 cnx->bot->sentPing = false;
753 }
754
755 void
756 Parser::parseQuit (ServerConnection * cnx, Person * from, String rest)
757 {
758 String n = from->getNick ();
759 #ifdef USESCRIPTS
760 cnx->bot->botInterp->RunHooks (Hook::SIGNOFF, n + " " + rest,
761 scm_listify (Utils::
762 str2scm (n),
763 Utils::
764 str2scm (rest),
765 SCM_UNDEFINED));
766 #endif
767 if (n == cnx->bot->nickName)
768 cnx->bot->stop = true;
769 for (std::map < String, Channel *,
770 std::less < String > >::iterator it =
771 cnx->bot->channelList->begin ();
772 it != cnx->bot->channelList->end (); ++it)
773 (*it).second->delNick (n);
774 }
775
776 void
777 Parser::parseTopic (ServerConnection * cnx, Person * from, String rest)
778 {
779 StringTokenizer st (rest);
780 String channel = st.next_token ();
781 String newTopic = st.rest ().substr (1);
782 Channel *c = cnx->bot->channelList->getChannel (channel);
783 #ifdef USESCRIPTS
784 cnx->bot->botInterp->RunHooks (Hook::TOPIC,
785 from->getNick () + " " +
786 channel + " " + newTopic,
787 scm_listify (Utils::
788 str2scm (from->
789 getNick
790 ()),
791 Utils::
792 str2scm
793 (channel),
794 Utils::
795 str2scm
796 (newTopic), SCM_UNDEFINED));
797 #endif
798 if (!c)
799 return;
800 if (c->lockedTopic && from->getNick () != cnx->bot->nickName)
801 cnx->queue->sendTopic (channel, c->channelTopic);
802 c->channelTopic = newTopic;
803 }
804
805 void
806 Parser::parseCTCP (ServerConnection * cnx,
807 Person * from, String to, String parameters)
808 {
809 StringTokenizer st (parameters);
810 String command = Utils::to_upper (st.next_token ());
811 String nick = from->getNick ();
812 String rest;
813 if (st.more_tokens_p ())
814 rest = st.rest ();
815 else
816 rest = "";
817 #ifdef USESCRIPTS
818 cnx->bot->botInterp->RunHooks (Hook::CTCP,
819 nick + " " + to + " " +
820 command + " " + rest,
821 scm_listify (Utils::
822 str2scm (nick),
823 Utils::
824 str2scm (to),
825 Utils::
826 str2scm
827 (command),
828 Utils::
829 str2scm (rest),
830 SCM_UNDEFINED));
831 #endif
832 if (command == "PING")
833 {
834 Commands::CTCPReply (cnx->bot, nick, "PING", rest);
835 }
836 else if (command == "VERSION")
837 {
838 Commands::CTCPReply (cnx->bot, nick, "VERSION",
839 cnx->bot->versionString);
840 }
841 else if (command == "CLOCK")
842 {
843 time_t diff = time (NULL) - cnx->bot->startTime;
844 Commands::CTCPReply (cnx->bot, nick, "CLOCK",
845 String ("elapsed time: ") +
846 String ((long) (diff / 86400)) +
847 "d" +
848 String ((long) (diff % 86400) /
849 3600) + "h" +
850 String ((long) (diff % 3600) / 60) +
851 "m" + String ((long) (diff % 60)) + "s");
852 }
853 else if (command == "COMMAND")
854 {
855 Commands::CTCPReply (cnx->bot, nick,
856 "COMMAND", String (cnx->bot->commandChar));
857 }
858 else if (command == "LAG")
859 {
860 Commands::CTCPReply (cnx->bot, nick, "LAG",
861 String ((long) cnx->lag) + " second(s)");
862 }
863 else if (command == "DCC")
864 {
865 StringTokenizer st2 (rest);
866 command = Utils::to_upper (st2.next_token ());
867 if (command == "CHAT")
868 {
869 // FIXME: debug DCC
870 st2.next_token ();
871 unsigned long address =
872 std::strtoul (st2.next_token ().c_str(), 0, 0);
873 int port = std::atoi (st2.next_token().c_str());
874 if (port >= 1024 && Utils::get_level (cnx->bot, from->getAddress ()))
875 cnx->bot->addDCC (from, address, port, Bot::CHAT);
876 else
877 cnx->bot->logLine ("DCC Chat Failed in Parser");
878 }
879 }
880 #ifdef USESCRIPTS
881 else if (command == "ACTION")
882 {
883 cnx->bot->botInterp->RunHooks (Hook::ACTION,
884 from->getNick () + " " + to +
885 " " + rest,
886 scm_listify (Utils::
887 str2scm (from->
888 getNick
889 ()),
890 Utils::
891 str2scm (to),
892 Utils::
893 str2scm (rest),
894 SCM_UNDEFINED));
895 }
896 #endif
897 }
898
899 void
900 Parser::parseMessage (ServerConnection * cnx,
901 Person * from, String to, String parameters)
902 {
903 #ifdef USESCRIPTS
904 if (Utils::channel_p (to))
905 cnx->bot->botInterp->RunHooks (Hook::PUBLIC,
906 from->getNick () + " " + to +
907 " " + parameters,
908 scm_listify (Utils::
909 str2scm (from->
910 getNick
911 ()),
912 Utils::
913 str2scm (to),
914 Utils::
915 str2scm
916 (parameters), SCM_UNDEFINED));
917 else
918 cnx->bot->botInterp->RunHooks (Hook::MESSAGE,
919 from->getNick () + " " +
920 parameters,
921 scm_listify (Utils::
922 str2scm (from->
923 getNick
924 ()),
925 Utils::
926 str2scm
927 (parameters), SCM_UNDEFINED));
928 #endif
929 if (parameters[0] != cnx->bot->commandChar)
930 return;
931
932 StringTokenizer st (parameters);
933 String command = Utils::to_upper (st.next_token ().substr (1));
934 String rest = Utils::trim_str (st.rest ());
935 int level;
936 bool identified = false;
937 std::map<std::string, class userFunction*,
938 std::less<std::string> >::const_iterator uf_iter
939 = cnx->bot->userFunctions.find (command);
940 userFunction * f = 0;
941
942 if (uf_iter != cnx->bot->userFunctions.end ())
943 f = uf_iter->second;
944 else
945 return;
946
947 if (f)
948 {
949 if (f->needsChannelName)
950 {
951 if (Utils::channel_p (rest))
952 {
953 StringTokenizer st2 (rest);
954 to = st.next_token ();
955 rest = st.rest ();
956 }
957 if (!Utils::channel_p (to))
958 {
959 from->sendNotice ("\002You need to supply a channel name"
960 " for this command\002");
961 return;
962 }
963 if (!cnx->bot->channelList->getChannel (to))
964 {
965 from->sendNotice (String ("\002I am not on channel\002 ") +
966 to);
967 return;
968 }
969 level = Utils::get_level (cnx->bot, from->getAddress (), to);
970 User *u = 0;
971 if (Channel * c = cnx->bot->channelList->getChannel (to))
972 u = c->getUser (from->getNick ());
973 if (!u || !u->userListItem)
974 identified = true;
975 else
976 identified = u->userListItem->passwd == ""
977 || u->userListItem->identified > 0;
978 }
979 else
980 {
981 level = Utils::get_level (cnx->bot, from->getAddress ());
982 identified = true;
983 }
984 if (level >= f->minLevel)
985 {
986 cnx->bot->logLine (from->getAddress () + " did " + command +
987 " " + rest);
988 #ifdef USESCRIPTS
989 if (f->argsCount != -1)
990 {
991 Parser::parseScriptFunction (cnx, to, f->needsChannelName,
992 f->scmFunc, f->argsCount,
993 rest);
994 }
995 else
996 {
997 f->function (cnx, from, to, rest);
998 }
999 #else
1000 f->function (cnx, from, to, rest);
1001 #endif
1002 }
1003 else
1004 {
1005 if (!identified)
1006 from->
1007 sendNotice (String
1008 ("\002You are not identified on channel\002 ") +
1009 to);
1010 }
1011 }
1012 }
1013
1014 #ifdef USESCRIPTS
1015 void
1016 Parser::parseScriptFunction (ServerConnection * cnx,
1017 String channel,
1018 bool needsChannelName,
1019 SCM scmFunc, int argsCount, String parameters)
1020 {
1021 String param;
1022 SCM args_list = scm_listify (SCM_UNDEFINED);
1023 if (needsChannelName)
1024 {
1025 args_list = gh_append2 (args_list,
1026 scm_listify (Utils::
1027 str2scm (channel),
1028 SCM_UNDEFINED));
1029 argsCount--;
1030 }
1031
1032 StringTokenizer st (parameters);
1033 for (int i = argsCount; i > 0; i--)
1034 {
1035 if (i == 1)
1036 param = st.rest ();
1037 else
1038 param = st.next_token ();
1039 args_list = gh_append2 (args_list,
1040 scm_listify (Utils::str2scm (param),
1041 SCM_UNDEFINED));
1042 }
1043
1044 struct wrapper_data wd;
1045 wd.func = scmFunc;
1046 wd.args = args_list;
1047 scm_internal_catch (SCM_BOOL_T,
1048 (scm_t_catch_body) lazy_apply_wrapper, (void *) &wd,
1049 (scm_t_catch_handler) empty_handler, 0);
1050 }
1051 #endif