1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "ircserverhandler.h"
24 #include "coresession.h"
25 #include "coreirclisthelper.h"
26 #include "coreidentity.h"
27 #include "ctcphandler.h"
30 #include "coreircchannel.h"
35 IrcServerHandler::IrcServerHandler(CoreNetwork *parent)
36 : BasicHandler(parent),
41 IrcServerHandler::~IrcServerHandler() {
44 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
45 void IrcServerHandler::handleServerMsg(QByteArray msg) {
47 qWarning() << "Received empty string from server!";
51 // Now we split the raw message into its various parts...
56 // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg
57 // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level...
58 int idx = msg.indexOf(" :");
60 if(msg.length() > idx + 2) trailing = msg.mid(idx + 2);
63 // OK, now it is safe to split...
64 QList<QByteArray> params = msg.split(' ');
65 if(!trailing.isEmpty()) params << trailing;
66 if(params.count() < 1) {
67 qWarning() << "Received invalid string from server!";
71 QString foo = serverDecode(params.takeFirst());
73 // a colon as the first chars indicates the existence of a prefix
77 if(params.count() < 1) {
78 qWarning() << "Received invalid string from server!";
81 foo = serverDecode(params.takeFirst());
84 // next string without a whitespace is the command
85 cmd = foo.trimmed().toUpper();
87 // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this!
88 uint num = cmd.toUInt();
90 if(params.count() == 0) {
91 qWarning() << "Message received from server violates RFC and is ignored!" << msg;
94 _target = serverDecode(params.takeFirst());
99 // note that the IRC server is still alive
100 network()->resetPingTimeout();
102 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
103 handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList<QByteArray>, params));
107 void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const QList<QByteArray> &rawparams) {
108 // we assume that all this happens in server encoding
109 QStringList params = serverDecode(rawparams);
110 uint num = cmd.toUInt();
112 // A lot of server messages don't really need their own handler because they don't do much.
113 // Catch and handle these here.
115 // Welcome, status, info messages. Just display these.
116 case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
117 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", params.join(" "), prefix);
119 // Server error messages without param, just display them
120 case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
121 case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
122 case 431: // ERR_NONICKNAMEGIVEN
123 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", params.join(" "), prefix);
125 // Server error messages, display them in red. First param will be appended.
127 QString target = params.takeFirst();
128 displayMsg(Message::Error, target, params.join(" ") + " " + target, prefix, Message::Redirected);
131 case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
132 QString channelName = params.takeFirst();
133 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", params.join(" ") + " " + channelName, prefix);
136 // Server error messages which will be displayed with a colon between the first param and the rest
137 case 413: case 414: case 423: case 441: case 444: case 461: // FIXME see below for the 47x codes
138 case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
139 case 436: // ERR_NICKCOLLISION
140 { QString p = params.takeFirst();
141 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", p + ": " + params.join(" "));
144 // Ignore these commands.
145 case 321: case 366: case 376:
148 // Everything else will be marked in red, so we can add them somewhere.
151 // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
152 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", "[Whois] " + params.join(" "), prefix);
154 if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
155 coreSession()->ircListHelper()->reportError(params.join(" "));
157 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", cmd + " " + params.join(" "), prefix);
160 //qDebug() << prefix <<":"<<cmd<<params;
162 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Unknown: ") + cmd + " " + params.join(" "), prefix);
163 //qDebug() << prefix <<":"<<cmd<<params;
167 //******************************/
168 // IRC SERVER HANDLER
169 //******************************/
170 void IrcServerHandler::handleJoin(const QString &prefix, const QList<QByteArray> ¶ms) {
171 if(!checkParamCount("IrcServerHandler::handleJoin()", params, 1))
174 QString channel = serverDecode(params[0]);
175 IrcUser *ircuser = network()->updateNickFromMask(prefix);
176 emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, prefix);
177 //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
178 ircuser->joinChannel(channel);
179 if(network()->isMe(ircuser)) {
180 network()->setChannelJoined(channel);
181 putCmd("MODE", params[0]); // we want to know the modes of the channel we just joined, so we ask politely
185 void IrcServerHandler::handleKick(const QString &prefix, const QList<QByteArray> ¶ms) {
186 if(!checkParamCount("IrcServerHandler::handleKick()", params, 2))
189 network()->updateNickFromMask(prefix);
190 IrcUser *victim = network()->ircUser(params[1]);
194 QString channel = serverDecode(params[0]);
195 victim->partChannel(channel);
198 if(params.count() > 2) // someone got a reason!
199 msg = QString("%1 %2").arg(victim->nick()).arg(channelDecode(channel, params[2]));
201 msg = victim->nick();
203 emit displayMsg(Message::Kick, BufferInfo::ChannelBuffer, channel, msg, prefix);
204 //if(network()->isMe(victim)) network()->setKickedFromChannel(channel);
207 void IrcServerHandler::handleMode(const QString &prefix, const QList<QByteArray> ¶ms) {
208 if(!checkParamCount("IrcServerHandler::handleMode()", params, 2))
211 if(network()->isChannelName(serverDecode(params[0]))) {
213 emit displayMsg(Message::Mode, BufferInfo::ChannelBuffer, serverDecode(params[0]), serverDecode(params).join(" "), prefix);
215 IrcChannel *channel = network()->ircChannel(params[0]);
217 // we received mode information for a channel we're not in. that means probably we've just been kicked out or something like that
218 // anyways: we don't have a place to store the data --> discard the info.
222 QString modes = params[1];
225 for(int c = 0; c < modes.length(); c++) {
226 if(modes[c] == '+') {
230 if(modes[c] == '-') {
235 if(network()->prefixModes().contains(modes[c])) {
236 // user channel modes (op, voice, etc...)
237 if(paramOffset < params.count()) {
238 IrcUser *ircUser = network()->ircUser(params[paramOffset]);
240 channel->addUserMode(ircUser, QString(modes[c]));
242 channel->removeUserMode(ircUser, QString(modes[c]));
244 qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
248 // regular channel modes
250 Network::ChannelModeType modeType = network()->channelModeType(modes[c]);
251 if(modeType == Network::A_CHANMODE || modeType == Network::B_CHANMODE || (modeType == Network::C_CHANMODE && add)) {
252 if(paramOffset < params.count()) {
253 value = params[paramOffset];
255 qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
261 channel->addChannelMode(modes[c], value);
263 channel->removeChannelMode(modes[c], value);
269 IrcUser *ircUser = network()->newIrcUser(params[0]);
270 QString modeString(serverDecode(params[1]));
274 for(int c = 0; c < modeString.count(); c++) {
275 if(modeString[c] == '+') {
279 if(modeString[c] == '-') {
284 addModes += modeString[c];
286 removeModes += modeString[c];
288 if(!addModes.isEmpty())
289 ircUser->addUserModes(addModes);
290 if(!removeModes.isEmpty())
291 ircUser->removeUserModes(removeModes);
294 emit displayMsg(Message::Mode, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
298 void IrcServerHandler::handleNick(const QString &prefix, const QList<QByteArray> ¶ms) {
299 if(!checkParamCount("IrcServerHandler::handleNick()", params, 1))
302 IrcUser *ircuser = network()->updateNickFromMask(prefix);
304 qWarning() << "IrcServerHandler::handleNick(): Unknown IrcUser!";
307 QString newnick = serverDecode(params[0]);
308 QString oldnick = ircuser->nick();
310 QString sender = network()->isMyNick(oldnick)
315 // the order is cruicial
316 // otherwise the client would rename the buffer, see that the assigned ircuser doesn't match anymore
317 // and remove the ircuser from the querybuffer leading to a wrong on/offline state
318 ircuser->setNick(newnick);
319 coreSession()->renameBuffer(network()->networkId(), newnick, oldnick);
321 foreach(QString channel, ircuser->channels())
322 emit displayMsg(Message::Nick, BufferInfo::ChannelBuffer, channel, newnick, sender);
325 void IrcServerHandler::handleNotice(const QString &prefix, const QList<QByteArray> ¶ms) {
326 if(!checkParamCount("IrcServerHandler::handleNotice()", params, 2))
329 QString target = serverDecode(params[0]);
331 // special treatment for welcome messages like:
332 // :ChanServ!ChanServ@services. NOTICE egst :[#apache] Welcome, this is #apache. Please read the in-channel topic message. This channel is being logged by IRSeekBot. If you have any question please see http://blog.freenode.net/?p=68
333 if(!network()->isChannelName(target)) {
334 QString msg = serverDecode(params[1]);
335 QRegExp welcomeRegExp("^\\[([^\\]]+)\\] ");
336 if(welcomeRegExp.indexIn(msg) != -1) {
337 QString channelname = welcomeRegExp.cap(1);
338 msg = msg.mid(welcomeRegExp.matchedLength());
339 CoreIrcChannel *chan = static_cast<CoreIrcChannel *>(network()->ircChannel(channelname)); // we only have CoreIrcChannels in the core, so this cast is safe
340 if(chan && !chan->receivedWelcomeMsg()) {
341 chan->setReceivedWelcomeMsg();
342 emit displayMsg(Message::Notice, BufferInfo::ChannelBuffer, channelname, msg, prefix);
348 if(prefix.isEmpty() || target == "AUTH") {
351 if(!target.isEmpty() && network()->prefixes().contains(target[0]))
352 target = target.mid(1);
353 if(!network()->isChannelName(target))
354 target = nickFromMask(prefix);
357 network()->ctcpHandler()->parse(Message::Notice, prefix, target, params[1]);
360 void IrcServerHandler::handlePart(const QString &prefix, const QList<QByteArray> ¶ms) {
361 if(!checkParamCount("IrcServerHandler::handlePart()", params, 1))
364 IrcUser *ircuser = network()->updateNickFromMask(prefix);
365 QString channel = serverDecode(params[0]);
367 qWarning() << "IrcServerHandler::handlePart(): Unknown IrcUser!";
371 ircuser->partChannel(channel);
374 if(params.count() > 1)
375 msg = userDecode(ircuser->nick(), params[1]);
377 emit displayMsg(Message::Part, BufferInfo::ChannelBuffer, channel, msg, prefix);
378 if(network()->isMe(ircuser)) network()->setChannelParted(channel);
381 void IrcServerHandler::handlePing(const QString &prefix, const QList<QByteArray> ¶ms) {
383 putCmd("PONG", params);
386 void IrcServerHandler::handlePong(const QString &prefix, const QList<QByteArray> ¶ms) {
388 // the server is supposed to send back what we passed as param. and we send a timestamp
389 // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
390 if(params.count() < 2)
393 QString timestamp = serverDecode(params[1]);
394 QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
395 if(!sendTime.isValid()) {
396 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", "PONG " + serverDecode(params).join(" "), prefix);
400 network()->setLatency(sendTime.msecsTo(QTime::currentTime()) / 2);
403 void IrcServerHandler::handlePrivmsg(const QString &prefix, const QList<QByteArray> ¶ms) {
404 if(!checkParamCount("IrcServerHandler::handlePrivmsg()", params, 1))
407 IrcUser *ircuser = network()->updateNickFromMask(prefix);
409 qWarning() << "IrcServerHandler::handlePrivmsg(): Unknown IrcUser!";
413 if(params.isEmpty()) {
414 qWarning() << "IrcServerHandler::handlePrivmsg(): received PRIVMSG without target or message from:" << prefix;
418 QString target = serverDecode(params[0]);
420 QByteArray msg = params.count() < 2
424 if(!network()->isChannelName(target))
425 target = nickFromMask(prefix);
427 // it's possible to pack multiple privmsgs into one param using ctcp
428 // - > we let the ctcpHandler do the work
429 network()->ctcpHandler()->parse(Message::Plain, prefix, target, msg);
432 void IrcServerHandler::handleQuit(const QString &prefix, const QList<QByteArray> ¶ms) {
433 IrcUser *ircuser = network()->updateNickFromMask(prefix);
437 if(params.count() > 0)
438 msg = userDecode(ircuser->nick(), params[0]);
440 foreach(QString channel, ircuser->channels())
441 emit displayMsg(Message::Quit, BufferInfo::ChannelBuffer, channel, msg, prefix);
446 void IrcServerHandler::handleTopic(const QString &prefix, const QList<QByteArray> ¶ms) {
447 if(!checkParamCount("IrcServerHandler::handleTopic()", params, 1))
450 IrcUser *ircuser = network()->updateNickFromMask(prefix);
454 IrcChannel *channel = network()->ircChannel(serverDecode(params[0]));
459 if(params.count() > 1)
460 topic = channelDecode(channel->name(), params[1]);
462 channel->setTopic(topic);
464 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel->name()).arg(topic));
468 void IrcServerHandler::handle001(const QString &prefix, const QList<QByteArray> ¶ms) {
469 network()->setCurrentServer(prefix);
471 if(params.isEmpty()) {
472 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("%1 didn't supply a valid welcome message... expect some serious issues..."));
474 // there should be only one param: "Welcome to the Internet Relay Network <nick>!<user>@<host>"
475 QString param = serverDecode(params[0]);
476 QString myhostmask = param.section(' ', -1, -1);
478 network()->setMyNick(nickFromMask(myhostmask));
480 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", param, prefix);
484 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
485 void IrcServerHandler::handle005(const QString &prefix, const QList<QByteArray> ¶ms) {
487 const int numParams = params.size();
489 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received RPL_ISUPPORT (005) without parameters!"), prefix);
493 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
495 QString rpl_isupport_suffix = serverDecode(params.last());
496 if(!rpl_isupport_suffix.toLower().contains("are supported by this server")) {
497 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received non RFC compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), prefix);
502 for(int i = 0; i < numParams - 1; i++) {
503 QString rawSupport = serverDecode(params[i]);
504 QString key = rawSupport.section("=", 0, 0);
505 QString value = rawSupport.section("=", 1);
506 network()->addSupport(key, value);
510 /* RPL_UMODEIS - "<user_modes> [<user_mode_params>]" */
511 void IrcServerHandler::handle221(const QString &prefix, const QList<QByteArray> ¶ms) {
513 //TODO: save information in network object
514 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("%1").arg(serverDecode(params).join(" ")));
517 /* RPL_STATSCONN - "Highest connection cout: 8000 (7999 clients)" */
518 void IrcServerHandler::handle250(const QString &prefix, const QList<QByteArray> ¶ms) {
520 //TODO: save information in network object
521 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("%1").arg(serverDecode(params).join(" ")));
524 /* RPL_LOCALUSERS - "Current local user: 5024 Max: 7999 */
525 void IrcServerHandler::handle265(const QString &prefix, const QList<QByteArray> ¶ms) {
527 //TODO: save information in network object
528 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("%1").arg(serverDecode(params).join(" ")));
531 /* RPL_GLOBALUSERS - "Current global users: 46093 Max: 47650" */
532 void IrcServerHandler::handle266(const QString &prefix, const QList<QByteArray> ¶ms) {
534 //TODO: save information in network object
535 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("%1").arg(serverDecode(params).join(" ")));
540 Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
542 "<nick> :<away message>"
544 Replies 352 and 315 paired are used to answer a WHO message.
547 Replies 314 and 369 are responses to a WHOWAS message.
552 /* RPL_AWAY - "<nick> :<away message>" */
553 void IrcServerHandler::handle301(const QString &prefix, const QList<QByteArray> ¶ms) {
555 if(!checkParamCount("IrcServerHandler::handle301()", params, 2))
559 QString nickName = serverDecode(params[0]);
560 QString awayMessage = userDecode(nickName, params[1]);
562 IrcUser *ircuser = network()->ircUser(nickName);
564 ircuser->setAwayMessage(awayMessage);
565 ircuser->setAway(true);
568 // FIXME: proper redirection needed
570 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is away: \"%2\"").arg(nickName).arg(awayMessage));
573 int now = QDateTime::currentDateTime().toTime_t();
574 int silenceTime = 60;
575 if(ircuser->lastAwayMessage() + silenceTime < now) {
576 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, params[0], tr("%1 is away: \"%2\"").arg(nickName).arg(awayMessage));
578 ircuser->setLastAwayMessage(now);
580 // probably should not happen
581 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, params[0], tr("%1 is away: \"%2\"").arg(nickName).arg(awayMessage));
587 // ":You are no longer marked as being away"
588 void IrcServerHandler::handle305(const QString &prefix, const QList<QByteArray> ¶ms) {
590 IrcUser *me = network()->me();
594 if(!network()->autoAwayActive()) {
595 if(!params.isEmpty())
596 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0]));
598 network()->setAutoAwayActive(false);
603 // ":You have been marked as being away"
604 void IrcServerHandler::handle306(const QString &prefix, const QList<QByteArray> ¶ms) {
606 IrcUser *me = network()->me();
610 if(!params.isEmpty() && !network()->autoAwayActive())
611 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0]));
614 /* RPL_WHOISSERVICE - "<user> is registered nick" */
615 void IrcServerHandler::handle307(const QString &prefix, const QList<QByteArray> ¶ms) {
617 if(!checkParamCount("IrcServerHandler::handle307()", params, 1))
620 QString whoisServiceReply = serverDecode(params).join(" ");
621 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
623 ircuser->setWhoisServiceReply(whoisServiceReply);
625 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(whoisServiceReply));
628 /* RPL_SUSERHOST - "<user> is available for help." */
629 void IrcServerHandler::handle310(const QString &prefix, const QList<QByteArray> ¶ms) {
631 if(!checkParamCount("IrcServerHandler::handle310()", params, 1))
634 QString suserHost = serverDecode(params).join(" ");
635 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
637 ircuser->setSuserHost(suserHost);
639 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(suserHost));
642 /* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
643 void IrcServerHandler::handle311(const QString &prefix, const QList<QByteArray> ¶ms) {
645 if(!checkParamCount("IrcServerHandler::handle311()", params, 3))
649 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
651 ircuser->setUser(serverDecode(params[1]));
652 ircuser->setHost(serverDecode(params[2]));
653 ircuser->setRealName(serverDecode(params.last()));
654 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(ircuser->nick()).arg(ircuser->hostmask()).arg(ircuser->realName()));
656 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(serverDecode(params[1])).arg(serverDecode(params[2])).arg(serverDecode(params.last())));
660 /* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
661 void IrcServerHandler::handle312(const QString &prefix, const QList<QByteArray> ¶ms) {
663 if(!checkParamCount("IrcServerHandler::handle312()", params, 2))
666 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
668 ircuser->setServer(serverDecode(params[1]));
671 QString returnString = tr("%1 is online via %2 (%3)").arg(serverDecode(params[0])).arg(serverDecode(params[1])).arg(serverDecode(params.last()));
673 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(returnString));
675 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1").arg(returnString));
679 /* RPL_WHOISOPERATOR - "<nick> :is an IRC operator" */
680 void IrcServerHandler::handle313(const QString &prefix, const QList<QByteArray> ¶ms) {
682 if(!checkParamCount("IrcServerHandler::handle313()", params, 1))
685 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
687 ircuser->setIrcOperator(params.last());
689 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
692 /* RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
693 void IrcServerHandler::handle314(const QString &prefix, const QList<QByteArray> ¶ms) {
695 if(!checkParamCount("IrcServerHandler::handle314()", params, 3))
698 QString nick = serverDecode(params[0]);
699 QString hostmask = QString("%1@%2").arg(serverDecode(params[1])).arg(serverDecode(params[2]));
700 QString realName = serverDecode(params.last());
701 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1 was %2 (%3)").arg(nick).arg(hostmask).arg(realName));
704 /* RPL_ENDOFWHO: "<name> :End of WHO list" */
705 void IrcServerHandler::handle315(const QString &prefix, const QList<QByteArray> ¶ms) {
707 if(!checkParamCount("IrcServerHandler::handle315()", params, 1))
710 QStringList p = serverDecode(params);
711 if(network()->setAutoWhoDone(p[0])) {
712 return; // stay silent
714 p.takeLast(); // should be "End of WHO list"
715 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
718 /* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
719 (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
720 void IrcServerHandler::handle317(const QString &prefix, const QList<QByteArray> ¶ms) {
722 if(!checkParamCount("IrcServerHandler::handle317()", params, 2))
725 QString nick = serverDecode(params[0]);
726 IrcUser *ircuser = network()->ircUser(nick);
728 QDateTime now = QDateTime::currentDateTime();
729 int idleSecs = serverDecode(params[1]).toInt();
731 ircuser->setIdleTime(now.addSecs(idleSecs));
732 if(params.size() > 3) { // if we have more then 3 params we have the obove mentioned "real life" situation
733 int loginTime = serverDecode(params[2]).toInt();
734 ircuser->setLoginTime(QDateTime::fromTime_t(loginTime));
735 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is logged in since %2").arg(ircuser->nick()).arg(ircuser->loginTime().toString()));
737 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is idling for %2 (%3)").arg(ircuser->nick()).arg(secondsToString(ircuser->idleTime().secsTo(now))).arg(ircuser->idleTime().toString()));
740 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] idle message: %1").arg(userDecode(nick, params).join(" ")));
744 /* RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
745 void IrcServerHandler::handle318(const QString &prefix, const QList<QByteArray> ¶ms) {
748 QStringList parameter = serverDecode(params);
749 parameter.removeFirst();
750 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(parameter.join(" ")));
753 /* RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
754 void IrcServerHandler::handle319(const QString &prefix, const QList<QByteArray> ¶ms) {
756 if(!checkParamCount("IrcServerHandler::handle319()", params, 2))
759 QString nick = serverDecode(params.first());
763 foreach (QString channel, serverDecode(params.last()).split(" ")) {
764 if(channel.startsWith("@"))
765 op.append(channel.remove(0,1));
766 else if(channel.startsWith("+"))
767 voice.append(channel.remove(0,1));
769 user.append(channel);
772 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is a user on channels: %2").arg(nick).arg(user.join(" ")));
774 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 has voice on channels: %2").arg(nick).arg(voice.join(" ")));
776 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is an operator on channels: %2").arg(nick).arg(op.join(" ")));
779 /* RPL_WHOISVIRT - "<nick> is identified to services" */
780 void IrcServerHandler::handle320(const QString &prefix, const QList<QByteArray> ¶ms) {
782 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
785 /* RPL_LIST - "<channel> <# visible> :<topic>" */
786 void IrcServerHandler::handle322(const QString &prefix, const QList<QByteArray> ¶ms) {
789 quint32 userCount = 0;
792 int paramCount = params.count();
795 topic = serverDecode(params[2]);
797 userCount = serverDecode(params[1]).toUInt();
799 channelName = serverDecode(params[0]);
803 if(!coreSession()->ircListHelper()->addChannel(network()->networkId(), channelName, userCount, topic))
804 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Channel %1 has %2 users. Topic is: %3").arg(channelName).arg(userCount).arg(topic));
807 /* RPL_LISTEND ":End of LIST" */
808 void IrcServerHandler::handle323(const QString &prefix, const QList<QByteArray> ¶ms) {
812 if(!coreSession()->ircListHelper()->endOfChannelList(network()->networkId()))
813 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("End of channel list"));
816 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
817 void IrcServerHandler::handle324(const QString &prefix, const QList<QByteArray> ¶ms) {
819 handleMode(prefix, params);
822 /* RPL_??? - "<channel> <creation time (unix)>" */
823 void IrcServerHandler::handle329(const QString &prefix, const QList<QByteArray> ¶ms) {
827 # warning "Implement handle329 (Channel creation time)"
829 // FIXME implement this...
833 void IrcServerHandler::handle331(const QString &prefix, const QList<QByteArray> ¶ms) {
835 if(!checkParamCount("IrcServerHandler::handle331()", params, 1))
838 QString channel = serverDecode(params[0]);
839 IrcChannel *chan = network()->ircChannel(channel);
841 chan->setTopic(QString());
843 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("No topic is set for %1.").arg(channel));
847 void IrcServerHandler::handle332(const QString &prefix, const QList<QByteArray> ¶ms) {
849 if(!checkParamCount("IrcServerHandler::handle332()", params, 2))
852 QString channel = serverDecode(params[0]);
853 QString topic = channelDecode(channel, params[1]);
854 IrcChannel *chan = network()->ircChannel(channel);
856 chan->setTopic(topic);
858 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic));
861 /* Topic set by... */
862 void IrcServerHandler::handle333(const QString &prefix, const QList<QByteArray> ¶ms) {
864 if(!checkParamCount("IrcServerHandler::handle333()", params, 3))
867 QString channel = serverDecode(params[0]);
868 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel,
869 tr("Topic set by %1 on %2") .arg(serverDecode(params[1]), QDateTime::fromTime_t(channelDecode(channel, params[2]).toUInt()).toString()));
872 /* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
873 ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
874 void IrcServerHandler::handle352(const QString &prefix, const QList<QByteArray> ¶ms) {
876 if(!checkParamCount("IrcServerHandler::handle352()", params, 6))
879 QString channel = serverDecode(params[0]);
880 IrcUser *ircuser = network()->ircUser(serverDecode(params[4]));
882 ircuser->setUser(serverDecode(params[1]));
883 ircuser->setHost(serverDecode(params[2]));
885 bool away = serverDecode(params[5]).startsWith("G") ? true : false;
886 ircuser->setAway(away);
887 ircuser->setServer(serverDecode(params[3]));
888 ircuser->setRealName(serverDecode(params.last()).section(" ", 1));
891 if(!network()->isAutoWhoInProgress(channel)) {
892 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] %1").arg(serverDecode(params).join(" ")));
897 void IrcServerHandler::handle353(const QString &prefix, const QList<QByteArray> ¶ms) {
899 if(!checkParamCount("IrcServerHandler::handle353()", params, 3))
902 // param[0] is either "=", "*" or "@" indicating a public, private or secret channel
903 // we don't use this information at the time beeing
904 QString channelname = serverDecode(params[1]);
906 IrcChannel *channel = network()->ircChannel(channelname);
908 qWarning() << "IrcServerHandler::handle353(): received unknown target channel:" << channelname;
915 foreach(QString nick, serverDecode(params[2]).split(' ')) {
916 QString mode = QString();
918 if(network()->prefixes().contains(nick[0])) {
919 mode = network()->prefixToMode(nick[0]);
927 channel->joinIrcUsers(nicks, modes);
930 /* RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
931 void IrcServerHandler::handle369(const QString &prefix, const QList<QByteArray> ¶ms) {
933 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
936 /* ERR_ERRONEUSNICKNAME */
937 void IrcServerHandler::handle432(const QString &prefix, const QList<QByteArray> ¶ms) {
941 if(params.size() < 2) {
942 // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
944 // :irc.scortum.moep.net 432 @@@ :Erroneous Nickname: Illegal characters
945 // correct server reply:
946 // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
951 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick %1 contains illegal characters").arg(errnick));
952 tryNextNick(errnick, true /* erroneus */);
955 /* ERR_NICKNAMEINUSE */
956 void IrcServerHandler::handle433(const QString &prefix, const QList<QByteArray> ¶ms) {
958 if(!checkParamCount("IrcServerHandler::handle433()", params, 1))
961 QString errnick = serverDecode(params[0]);
962 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick already in use: %1").arg(errnick));
964 // if there is a problem while connecting to the server -> we handle it
965 // but only if our connection has not been finished yet...
966 if(!network()->currentServer().isEmpty())
969 tryNextNick(errnick);
974 // FIXME networkConnection()->setChannelKey("") for all ERR replies indicating that a JOIN went wrong
975 // mostly, these are codes in the 47x range
979 void IrcServerHandler::tryNextNick(const QString &errnick, bool erroneus) {
980 QStringList desiredNicks = coreSession()->identity(network()->identity())->nicks();
981 int nextNickIdx = desiredNicks.indexOf(errnick) + 1;
983 if(nextNickIdx > 0 && desiredNicks.size() > nextNickIdx) {
984 nextNick = desiredNicks[nextNickIdx];
987 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
990 nextNick = errnick + "_";
993 putCmd("NICK", serverEncode(nextNick));
996 bool IrcServerHandler::checkParamCount(const QString &methodName, const QList<QByteArray> ¶ms, int minParams) {
997 if(params.count() < minParams) {
998 qWarning() << qPrintable(methodName) << "requires" << minParams << "parameters but received only" << params.count() << serverDecode(params);
1006 /***********************************************************************************/