X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fircserverhandler.cpp;h=bcdcf446630fe5003a1412319c8c86925a218c51;hp=5484d3b4e2ba4eefbe77e8967cca935a945aaf3f;hb=dc2aa39d20b60e7cd8e0ba66ca6c9ed729add008;hpb=e128a8ef50a0a3fce1d75d7d06121b2ea6c74d6a diff --git a/src/core/ircserverhandler.cpp b/src/core/ircserverhandler.cpp index 5484d3b4..bcdcf446 100644 --- a/src/core/ircserverhandler.cpp +++ b/src/core/ircserverhandler.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -23,87 +23,79 @@ #include "coresession.h" #include "coreirclisthelper.h" -#include "networkconnection.h" -#include "network.h" -#include "identity.h" +#include "coreidentity.h" #include "ctcphandler.h" #include "ircuser.h" -#include "ircchannel.h" +#include "coreircchannel.h" #include "logger.h" #include -IrcServerHandler::IrcServerHandler(NetworkConnection *parent) +IrcServerHandler::IrcServerHandler(CoreNetwork *parent) : BasicHandler(parent), _whois(false) { } IrcServerHandler::~IrcServerHandler() { - } /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */ void IrcServerHandler::handleServerMsg(QByteArray msg) { - try { - if(msg.isEmpty()) { - quWarning() << "Received empty string from server!"; - return; - } + if(msg.isEmpty()) { + qWarning() << "Received empty string from server!"; + return; + } - // Now we split the raw message into its various parts... - QString prefix = ""; - QByteArray trailing; - QString cmd; - - // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg - // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level... - int idx = msg.indexOf(" :"); - if(idx >= 0) { - if(msg.length() > idx + 2) trailing = msg.mid(idx + 2); - msg = msg.left(idx); - } - // OK, now it is safe to split... - QList params = msg.split(' '); - if(!trailing.isEmpty()) params << trailing; - if(params.count() < 1) { - quWarning() << "Received invalid string from server!"; - return; - } + // Now we split the raw message into its various parts... + QString prefix = ""; + QByteArray trailing; + QString cmd; - QString foo = serverDecode(params.takeFirst()); + // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg + // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level... + int idx = msg.indexOf(" :"); + if(idx >= 0) { + if(msg.length() > idx + 2) trailing = msg.mid(idx + 2); + msg = msg.left(idx); + } + // OK, now it is safe to split... + QList params = msg.split(' '); + if(!trailing.isEmpty()) params << trailing; + if(params.count() < 1) { + qWarning() << "Received invalid string from server!"; + return; + } - // a colon as the first chars indicates the existence of a prefix - if(foo[0] == ':') { - foo.remove(0, 1); - prefix = foo; - if(params.count() < 1) { - quWarning() << "Received invalid string from server!"; - return; - } - foo = serverDecode(params.takeFirst()); + QString foo = serverDecode(params.takeFirst()); + + // a colon as the first chars indicates the existence of a prefix + if(foo[0] == ':') { + foo.remove(0, 1); + prefix = foo; + if(params.count() < 1) { + qWarning() << "Received invalid string from server!"; + return; } + foo = serverDecode(params.takeFirst()); + } - // next string without a whitespace is the command - cmd = foo.trimmed().toUpper(); + // next string without a whitespace is the command + cmd = foo.trimmed().toUpper(); - // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this! - uint num = cmd.toUInt(); - if(num > 0) { - if(params.count() == 0) { - quWarning() << "Message received from server violates RFC and is ignored!"; - return; - } - params.removeFirst(); + // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this! + uint num = cmd.toUInt(); + if(num > 0) { + if(params.count() == 0) { + qWarning() << "Message received from server violates RFC and is ignored!"; + return; } - - // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-) - handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList, params)); - //handle(cmd, Q_ARG(QString, prefix)); - } catch(Exception e) { - emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", e.msg()); + params.removeFirst(); } + + // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-) + handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList, params)); } @@ -122,7 +114,7 @@ void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const // Server error messages without param, just display them case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462: case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502: - case 431: // ERR_NONICKNAMEGIVEN + case 431: // ERR_NONICKNAMEGIVEN emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", params.join(" "), prefix); break; // Server error messages, display them in red. First param will be appended. @@ -154,8 +146,8 @@ void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const // many nets define their own WHOIS fields. we fetch those not in need of special attention here: emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", "[Whois] " + params.join(" "), prefix); } else { - if(networkConnection()->coreSession()->ircListHelper()->requestInProgress(network()->networkId())) - networkConnection()->coreSession()->ircListHelper()->reportError(params.join(" ")); + if(coreSession()->ircListHelper()->requestInProgress(network()->networkId())) + coreSession()->ircListHelper()->reportError(params.join(" ")); else emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", cmd + " " + params.join(" "), prefix); } @@ -180,7 +172,7 @@ void IrcServerHandler::handleJoin(const QString &prefix, const QList //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params; ircuser->joinChannel(channel); if(network()->isMe(ircuser)) { - networkConnection()->setChannelJoined(channel); + network()->setChannelJoined(channel); putCmd("MODE", params[0]); // we want to know the modes of the channel we just joined, so we ask politely } } @@ -193,7 +185,7 @@ void IrcServerHandler::handleKick(const QString &prefix, const QList IrcUser *victim = network()->ircUser(params[1]); if(!victim) return; - + QString channel = serverDecode(params[0]); victim->partChannel(channel); @@ -204,7 +196,7 @@ void IrcServerHandler::handleKick(const QString &prefix, const QList msg = victim->nick(); emit displayMsg(Message::Kick, BufferInfo::ChannelBuffer, channel, msg, prefix); - //if(network()->isMe(victim)) networkConnection()->setKickedFromChannel(channel); + //if(network()->isMe(victim)) network()->setKickedFromChannel(channel); } void IrcServerHandler::handleMode(const QString &prefix, const QList ¶ms) { @@ -244,7 +236,7 @@ void IrcServerHandler::handleMode(const QString &prefix, const QList else channel->removeUserMode(ircUser, QString(modes[c])); } else { - quWarning() << "Received MODE with too few parameters:" << serverDecode(params); + qWarning() << "Received MODE with too few parameters:" << serverDecode(params); } paramOffset++; } else { @@ -255,18 +247,18 @@ void IrcServerHandler::handleMode(const QString &prefix, const QList if(paramOffset < params.count()) { value = params[paramOffset]; } else { - quWarning() << "Received MODE with too few parameters:" << serverDecode(params); + qWarning() << "Received MODE with too few parameters:" << serverDecode(params); } paramOffset++; } - + if(add) channel->addChannelMode(modes[c], value); else channel->removeChannelMode(modes[c], value); } } - + } else { // pure User Modes IrcUser *ircUser = network()->newIrcUser(params[0]); @@ -292,7 +284,7 @@ void IrcServerHandler::handleMode(const QString &prefix, const QList ircUser->addUserModes(addModes); if(!removeModes.isEmpty()) ircUser->removeUserModes(removeModes); - + // FIXME: redirect emit displayMsg(Message::Mode, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix); } @@ -304,7 +296,7 @@ void IrcServerHandler::handleNick(const QString &prefix, const QList IrcUser *ircuser = network()->updateNickFromMask(prefix); if(!ircuser) { - quWarning() << "IrcServerHandler::handleNick(): Unknown IrcUser!"; + qWarning() << "IrcServerHandler::handleNick(): Unknown IrcUser!"; return; } QString newnick = serverDecode(params[0]); @@ -314,11 +306,15 @@ void IrcServerHandler::handleNick(const QString &prefix, const QList ? newnick : prefix; - emit nickChanged(newnick, oldnick); + + // the order is cruicial + // otherwise the client would rename the buffer, see that the assigned ircuser doesn't match anymore + // and remove the ircuser from the querybuffer leading to a wrong on/offline state + ircuser->setNick(newnick); + coreSession()->renameBuffer(network()->networkId(), newnick, oldnick); + foreach(QString channel, ircuser->channels()) emit displayMsg(Message::Nick, BufferInfo::ChannelBuffer, channel, newnick, sender); - - ircuser->setNick(newnick); } void IrcServerHandler::handleNotice(const QString &prefix, const QList ¶ms) { @@ -326,12 +322,34 @@ void IrcServerHandler::handleNotice(const QString &prefix, const QListisChannelName(target)) { + QString msg = serverDecode(params[1]); + QRegExp welcomeRegExp("^\\[([^\\]]+)\\] "); + if(welcomeRegExp.indexIn(msg) != -1) { + QString channelname = welcomeRegExp.cap(1); + msg = msg.mid(welcomeRegExp.matchedLength()); + CoreIrcChannel *chan = static_cast(network()->ircChannel(channelname)); // we only have CoreIrcChannels in the core, so this cast is safe + if(chan && !chan->receivedWelcomeMsg()) { + chan->setReceivedWelcomeMsg(); + emit displayMsg(Message::Notice, BufferInfo::ChannelBuffer, channelname, msg, prefix); + return; + } + } + } + + if(prefix.isEmpty() || target == "AUTH") { target = ""; - else if(!network()->isChannelName(target)) - target = nickFromMask(prefix); + } else { + if(!target.isEmpty() && network()->prefixes().contains(target[0])) + target = target.mid(1); + if(!network()->isChannelName(target)) + target = nickFromMask(prefix); + } - networkConnection()->ctcpHandler()->parse(Message::Notice, prefix, target, params[1]); + network()->ctcpHandler()->parse(Message::Notice, prefix, target, params[1]); } void IrcServerHandler::handlePart(const QString &prefix, const QList ¶ms) { @@ -341,7 +359,7 @@ void IrcServerHandler::handlePart(const QString &prefix, const QList IrcUser *ircuser = network()->updateNickFromMask(prefix); QString channel = serverDecode(params[0]); if(!ircuser) { - quWarning() << "IrcServerHandler::handlePart(): Unknown IrcUser!"; + qWarning() << "IrcServerHandler::handlePart(): Unknown IrcUser!"; return; } @@ -352,7 +370,7 @@ void IrcServerHandler::handlePart(const QString &prefix, const QList msg = userDecode(ircuser->nick(), params[1]); emit displayMsg(Message::Part, BufferInfo::ChannelBuffer, channel, msg, prefix); - if(network()->isMe(ircuser)) networkConnection()->setChannelParted(channel); + if(network()->isMe(ircuser)) network()->setChannelParted(channel); } void IrcServerHandler::handlePing(const QString &prefix, const QList ¶ms) { @@ -367,6 +385,8 @@ void IrcServerHandler::handlePong(const QString &prefix, const QList if(params.count() < 2) return; + network()->resetPong(); + QString timestamp = serverDecode(params[1]); QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz"); if(!sendTime.isValid()) { @@ -383,28 +403,27 @@ void IrcServerHandler::handlePrivmsg(const QString &prefix, const QListupdateNickFromMask(prefix); if(!ircuser) { - quWarning() << "IrcServerHandler::handlePrivmsg(): Unknown IrcUser!"; + qWarning() << "IrcServerHandler::handlePrivmsg(): Unknown IrcUser!"; return; } if(params.isEmpty()) { - quWarning() << "IrcServerHandler::handlePrivmsg(): received PRIVMSG without target or message from:" << prefix; + qWarning() << "IrcServerHandler::handlePrivmsg(): received PRIVMSG without target or message from:" << prefix; return; } - + QString target = serverDecode(params[0]); QByteArray msg = params.count() < 2 ? QByteArray("") : params[1]; - // are we the target? - if(network()->isMyNick(target)) - target = nickFromMask(ircuser->nick()); + if(!network()->isChannelName(target)) + target = nickFromMask(prefix); // it's possible to pack multiple privmsgs into one param using ctcp // - > we let the ctcpHandler do the work - networkConnection()->ctcpHandler()->parse(Message::Plain, prefix, target, msg); + network()->ctcpHandler()->parse(Message::Plain, prefix, target, msg); } void IrcServerHandler::handleQuit(const QString &prefix, const QList ¶ms) { @@ -418,7 +437,7 @@ void IrcServerHandler::handleQuit(const QString &prefix, const QList foreach(QString channel, ircuser->channels()) emit displayMsg(Message::Quit, BufferInfo::ChannelBuffer, channel, msg, prefix); - network()->removeIrcUser(nickFromMask(prefix)); + ircuser->quit(); } void IrcServerHandler::handleTopic(const QString &prefix, const QList ¶ms) { @@ -432,7 +451,7 @@ void IrcServerHandler::handleTopic(const QString &prefix, const QListircChannel(serverDecode(params[0])); if(!channel) return; - + QString topic; if(params.count() > 1) topic = channelDecode(channel->name(), params[1]); @@ -513,8 +532,8 @@ void IrcServerHandler::handle266(const QString &prefix, const QList emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("%1").arg(serverDecode(params).join(" "))); } -/* -WHOIS-Message: +/* +WHOIS-Message: Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message. and 301 (RPL_AWAY) " :" @@ -569,8 +588,12 @@ void IrcServerHandler::handle305(const QString &prefix, const QList if(me) me->setAway(false); - if(!params.isEmpty()) - emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0])); + if(!network()->autoAwayActive()) { + if(!params.isEmpty()) + emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0])); + } else { + network()->setAutoAwayActive(false); + } } // 306 RPL_NOWAWAY @@ -581,7 +604,7 @@ void IrcServerHandler::handle306(const QString &prefix, const QList if(me) me->setAway(true); - if(!params.isEmpty()) + if(!params.isEmpty() && !network()->autoAwayActive()) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0])); } @@ -630,7 +653,7 @@ void IrcServerHandler::handle311(const QString &prefix, const QList emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(serverDecode(params[1])).arg(serverDecode(params[2])).arg(serverDecode(params.last()))); } } - + /* RPL_WHOISSERVER - " :" */ void IrcServerHandler::handle312(const QString &prefix, const QList ¶ms) { Q_UNUSED(prefix) @@ -682,14 +705,14 @@ void IrcServerHandler::handle315(const QString &prefix, const QList return; QStringList p = serverDecode(params); - if(networkConnection()->setAutoWhoDone(p[0])) { + if(network()->setAutoWhoDone(p[0])) { return; // stay silent } p.takeLast(); // should be "End of WHO list" emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] End of /WHO list for %1").arg(p.join(" "))); } -/* RPL_WHOISIDLE - " :seconds idle" +/* RPL_WHOISIDLE - " :seconds idle" (real life: " :seconds idle, signon time) */ void IrcServerHandler::handle317(const QString &prefix, const QList ¶ms) { Q_UNUSED(prefix); @@ -709,7 +732,7 @@ void IrcServerHandler::handle317(const QString &prefix, const QList emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is logged in since %2").arg(ircuser->nick()).arg(ircuser->loginTime().toString())); } 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())); - + } else { emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] idle message: %1").arg(userDecode(nick, params).join(" "))); } @@ -742,11 +765,11 @@ void IrcServerHandler::handle319(const QString &prefix, const QList else user.append(channel); } - if(!user.isEmpty()) + if(!user.isEmpty()) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is a user on channels: %2").arg(nick).arg(user.join(" "))); - if(!voice.isEmpty()) + if(!voice.isEmpty()) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 has voice on channels: %2").arg(nick).arg(voice.join(" "))); - if(!op.isEmpty()) + if(!op.isEmpty()) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is an operator on channels: %2").arg(nick).arg(op.join(" "))); } @@ -762,7 +785,7 @@ void IrcServerHandler::handle322(const QString &prefix, const QList QString channelName; quint32 userCount = 0; QString topic; - + int paramCount = params.count(); switch(paramCount) { case 3: @@ -774,7 +797,7 @@ void IrcServerHandler::handle322(const QString &prefix, const QList default: break; } - if(!networkConnection()->coreSession()->ircListHelper()->addChannel(network()->networkId(), channelName, userCount, topic)) + if(!coreSession()->ircListHelper()->addChannel(network()->networkId(), channelName, userCount, topic)) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Channel %1 has %2 users. Topic is: %3").arg(channelName).arg(userCount).arg(topic)); } @@ -783,10 +806,10 @@ void IrcServerHandler::handle323(const QString &prefix, const QList Q_UNUSED(prefix) Q_UNUSED(params) - if(!networkConnection()->coreSession()->ircListHelper()->endOfChannelList(network()->networkId())) + if(!coreSession()->ircListHelper()->endOfChannelList(network()->networkId())) emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("End of channel list")); } - + /* RPL_CHANNELMODEIS - " " */ void IrcServerHandler::handle324(const QString &prefix, const QList ¶ms) { Q_UNUSED(prefix); @@ -800,7 +823,7 @@ void IrcServerHandler::handle329(const QString &prefix, const QList #ifdef __GNUC__ # warning "Implement handle329 (Channel creation time)" #endif - // FIXME implement this... + // FIXME implement this... } /* RPL_NOTOPIC */ @@ -810,7 +833,10 @@ void IrcServerHandler::handle331(const QString &prefix, const QList return; QString channel = serverDecode(params[0]); - network()->ircChannel(channel)->setTopic(QString()); + IrcChannel *chan = network()->ircChannel(channel); + if(chan) + chan->setTopic(QString()); + emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("No topic is set for %1.").arg(channel)); } @@ -822,7 +848,10 @@ void IrcServerHandler::handle332(const QString &prefix, const QList QString channel = serverDecode(params[0]); QString topic = channelDecode(channel, params[1]); - network()->ircChannel(channel)->setTopic(topic); + IrcChannel *chan = network()->ircChannel(channel); + if(chan) + chan->setTopic(topic); + emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic)); } @@ -837,7 +866,7 @@ void IrcServerHandler::handle333(const QString &prefix, const QList tr("Topic set by %1 on %2") .arg(serverDecode(params[1]), QDateTime::fromTime_t(channelDecode(channel, params[2]).toUInt()).toString())); } -/* RPL_WHOREPLY: " +/* RPL_WHOREPLY: " ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] : " */ void IrcServerHandler::handle352(const QString &prefix, const QList ¶ms) { Q_UNUSED(prefix) @@ -852,11 +881,11 @@ void IrcServerHandler::handle352(const QString &prefix, const QList bool away = serverDecode(params[5]).startsWith("G") ? true : false; ircuser->setAway(away); - ircuser->setServer(serverDecode(params[3])); + ircuser->setServer(serverDecode(params[3])); ircuser->setRealName(serverDecode(params.last()).section(" ", 1)); } - if(!networkConnection()->isAutoWhoInProgress(channel)) { + if(!network()->isAutoWhoInProgress(channel)) { emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] %1").arg(serverDecode(params).join(" "))); } } @@ -864,22 +893,22 @@ void IrcServerHandler::handle352(const QString &prefix, const QList /* RPL_NAMREPLY */ void IrcServerHandler::handle353(const QString &prefix, const QList ¶ms) { Q_UNUSED(prefix); - if(!checkParamCount("IrcServerHandler::handle353()", params, 2)) + if(!checkParamCount("IrcServerHandler::handle353()", params, 3)) return; - + // param[0] is either "=", "*" or "@" indicating a public, private or secret channel // we don't use this information at the time beeing QString channelname = serverDecode(params[1]); IrcChannel *channel = network()->ircChannel(channelname); if(!channel) { - quWarning() << "IrcServerHandler::handle353(): received unknown target channel:" << channelname; + qWarning() << "IrcServerHandler::handle353(): received unknown target channel:" << channelname; return; } QStringList nicks; QStringList modes; - + foreach(QString nick, serverDecode(params[2]).split(' ')) { QString mode = QString(); @@ -891,7 +920,7 @@ void IrcServerHandler::handle353(const QString &prefix, const QList nicks << nick; modes << mode; } - + channel->joinIrcUsers(nicks, modes); } @@ -926,7 +955,7 @@ void IrcServerHandler::handle433(const QString &prefix, const QList Q_UNUSED(prefix); if(!checkParamCount("IrcServerHandler::handle433()", params, 1)) return; - + QString errnick = serverDecode(params[0]); emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick already in use: %1").arg(errnick)); @@ -946,7 +975,7 @@ void IrcServerHandler::handle433(const QString &prefix, const QList /* */ void IrcServerHandler::tryNextNick(const QString &errnick) { - QStringList desiredNicks = networkConnection()->coreSession()->identity(network()->identity())->nicks(); + QStringList desiredNicks = coreSession()->identity(network()->identity())->nicks(); int nextNick = desiredNicks.indexOf(errnick) + 1; if(desiredNicks.size() > nextNick) { putCmd("NICK", serverEncode(desiredNicks[nextNick])); @@ -957,7 +986,7 @@ void IrcServerHandler::tryNextNick(const QString &errnick) { bool IrcServerHandler::checkParamCount(const QString &methodName, const QList ¶ms, int minParams) { if(params.count() < minParams) { - quWarning() << qPrintable(methodName) << "requires" << minParams << "parameters but received only" << params.count() << serverDecode(params); + qWarning() << qPrintable(methodName) << "requires" << minParams << "parameters but received only" << params.count() << serverDecode(params); return false; } else { return true;