| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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 | cnx->queue->sendCTCP (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 | cnx->queue->sendCTCPReply (nick, "PING", rest); |
| 834 | else if (command == "VERSION") |
| 835 | cnx->queue->sendCTCPReply (nick, "VERSION", cnx->bot->versionString); |
| 836 | else if (command == "CLOCK") |
| 837 | { |
| 838 | time_t diff = time (NULL) - cnx->bot->startTime; |
| 839 | cnx->queue->sendCTCPReply (nick, "CLOCK", |
| 840 | String ("elapsed time: ") + |
| 841 | String ((long) (diff / 86400)) + |
| 842 | "d" + |
| 843 | String ((long) (diff % 86400) / |
| 844 | 3600) + "h" + |
| 845 | String ((long) (diff % 3600) / 60) + |
| 846 | "m" + String ((long) (diff % 60)) + "s"); |
| 847 | } |
| 848 | else if (command == "COMMAND") |
| 849 | cnx->queue->sendCTCPReply (nick, |
| 850 | "COMMAND", String (cnx->bot->commandChar)); |
| 851 | else if (command == "LAG") |
| 852 | cnx->queue->sendCTCPReply (nick, "LAG", |
| 853 | String ((long) cnx->lag) + " second(s)"); |
| 854 | else if (command == "DCC") |
| 855 | { |
| 856 | StringTokenizer st2 (rest); |
| 857 | command = Utils::to_upper (st2.next_token ()); |
| 858 | if (command == "CHAT") |
| 859 | { |
| 860 | // FIXME: debug DCC |
| 861 | st2.next_token (); |
| 862 | unsigned long address = |
| 863 | std::strtoul (st2.next_token ().c_str(), 0, 0); |
| 864 | int port = std::atoi (st2.next_token().c_str()); |
| 865 | if (port >= 1024 && Utils::get_level (cnx->bot, from->getAddress ())) |
| 866 | cnx->bot->addDCC (from, address, port, Bot::CHAT); |
| 867 | else |
| 868 | cnx->bot->logLine ("DCC Chat Failed in Parser"); |
| 869 | } |
| 870 | } |
| 871 | #ifdef USESCRIPTS |
| 872 | else if (command == "ACTION") |
| 873 | { |
| 874 | cnx->bot->botInterp->RunHooks (Hook::ACTION, |
| 875 | from->getAddress () + " " + to + |
| 876 | " " + rest, |
| 877 | scm_listify (Utils:: |
| 878 | str2scm (from-> |
| 879 | getAddress |
| 880 | ()), |
| 881 | Utils:: |
| 882 | str2scm (to), |
| 883 | Utils:: |
| 884 | str2scm (rest), |
| 885 | SCM_UNDEFINED)); |
| 886 | } |
| 887 | #endif |
| 888 | } |
| 889 | |
| 890 | void |
| 891 | Parser::parseMessage (ServerConnection * cnx, |
| 892 | Person * from, String to, String parameters) |
| 893 | { |
| 894 | #ifdef USESCRIPTS |
| 895 | if (Utils::channel_p (to)) |
| 896 | cnx->bot->botInterp->RunHooks (Hook::PUBLIC, |
| 897 | from->getNick () + " " + to + |
| 898 | " " + parameters, |
| 899 | scm_listify (Utils:: |
| 900 | str2scm (from-> |
| 901 | getNick |
| 902 | ()), |
| 903 | Utils:: |
| 904 | str2scm (to), |
| 905 | Utils:: |
| 906 | str2scm |
| 907 | (parameters), SCM_UNDEFINED)); |
| 908 | else |
| 909 | cnx->bot->botInterp->RunHooks (Hook::MESSAGE, |
| 910 | from->getNick () + " " + |
| 911 | parameters, |
| 912 | scm_listify (Utils:: |
| 913 | str2scm (from-> |
| 914 | getNick |
| 915 | ()), |
| 916 | Utils:: |
| 917 | str2scm |
| 918 | (parameters), SCM_UNDEFINED)); |
| 919 | #endif |
| 920 | if (parameters[0] != cnx->bot->commandChar) |
| 921 | return; |
| 922 | |
| 923 | StringTokenizer st (parameters); |
| 924 | String command = Utils::to_upper (st.next_token ().substr (1)); |
| 925 | String rest = Utils::trim_str (st.rest ()); |
| 926 | int level; |
| 927 | bool identified = false; |
| 928 | std::map<std::string, class userFunction*, |
| 929 | std::less<std::string> >::const_iterator uf_iter |
| 930 | = cnx->bot->userFunctions.find (command); |
| 931 | userFunction * f = 0; |
| 932 | |
| 933 | if (uf_iter != cnx->bot->userFunctions.end ()) |
| 934 | f = uf_iter->second; |
| 935 | else |
| 936 | return; |
| 937 | |
| 938 | if (f) |
| 939 | { |
| 940 | if (f->needsChannelName) |
| 941 | { |
| 942 | if (Utils::channel_p (rest)) |
| 943 | { |
| 944 | StringTokenizer st2 (rest); |
| 945 | to = st.next_token (); |
| 946 | rest = st.rest (); |
| 947 | } |
| 948 | if (!Utils::channel_p (to)) |
| 949 | { |
| 950 | from->sendNotice ("\002You need to supply a channel name" |
| 951 | " for this command\002"); |
| 952 | return; |
| 953 | } |
| 954 | if (!cnx->bot->channelList->getChannel (to)) |
| 955 | { |
| 956 | from->sendNotice (String ("\002I am not on channel\002 ") + |
| 957 | to); |
| 958 | return; |
| 959 | } |
| 960 | level = Utils::get_level (cnx->bot, from->getAddress (), to); |
| 961 | User *u = 0; |
| 962 | if (Channel * c = cnx->bot->channelList->getChannel (to)) |
| 963 | u = c->getUser (from->getNick ()); |
| 964 | if (!u || !u->userListItem) |
| 965 | identified = true; |
| 966 | else |
| 967 | identified = u->userListItem->passwd == "" |
| 968 | || u->userListItem->identified > 0; |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | level = Utils::get_level (cnx->bot, from->getAddress ()); |
| 973 | identified = true; |
| 974 | } |
| 975 | if (level >= f->minLevel) |
| 976 | { |
| 977 | cnx->bot->logLine (from->getAddress () + " did " + command + |
| 978 | " " + rest); |
| 979 | #ifdef USESCRIPTS |
| 980 | if (f->argsCount != -1) |
| 981 | { |
| 982 | Parser::parseScriptFunction (cnx, to, f->needsChannelName, |
| 983 | f->scmFunc, f->argsCount, |
| 984 | rest); |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | f->function (cnx, from, to, rest); |
| 989 | } |
| 990 | #else |
| 991 | f->function (cnx, from, to, rest); |
| 992 | #endif |
| 993 | } |
| 994 | else |
| 995 | { |
| 996 | if (!identified) |
| 997 | from-> |
| 998 | sendNotice (String |
| 999 | ("\002You are not identified on channel\002 ") + |
| 1000 | to); |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | #ifdef USESCRIPTS |
| 1006 | void |
| 1007 | Parser::parseScriptFunction (ServerConnection * cnx, |
| 1008 | String channel, |
| 1009 | bool needsChannelName, |
| 1010 | SCM scmFunc, int argsCount, String parameters) |
| 1011 | { |
| 1012 | String param; |
| 1013 | SCM args_list = scm_listify (SCM_UNDEFINED); |
| 1014 | if (needsChannelName) |
| 1015 | { |
| 1016 | args_list = gh_append2 (args_list, |
| 1017 | scm_listify (Utils:: |
| 1018 | str2scm (channel), |
| 1019 | SCM_UNDEFINED)); |
| 1020 | argsCount--; |
| 1021 | } |
| 1022 | |
| 1023 | StringTokenizer st (parameters); |
| 1024 | for (int i = argsCount; i > 0; i--) |
| 1025 | { |
| 1026 | if (i == 1) |
| 1027 | param = st.rest (); |
| 1028 | else |
| 1029 | param = st.next_token (); |
| 1030 | args_list = gh_append2 (args_list, |
| 1031 | scm_listify (Utils::str2scm (param), |
| 1032 | SCM_UNDEFINED)); |
| 1033 | } |
| 1034 | |
| 1035 | struct wrapper_data wd; |
| 1036 | wd.func = scmFunc; |
| 1037 | wd.args = args_list; |
| 1038 | gh_catch (SCM_BOOL_T, (scm_t_catch_body) scm_apply_wrapper, |
| 1039 | (void *) &wd, (scm_t_catch_handler) Interp::ErrorHandler, 0); |
| 1040 | } |
| 1041 | #endif |