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