/***************************************************************************
- * Copyright (C) 2005-09 by the Quassel Project *
+ * Copyright (C) 2005-10 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
#include <QDebug>
+#ifdef HAVE_QCA2
+# include "cipher.h"
+#endif
+
IrcServerHandler::IrcServerHandler(CoreNetwork *parent)
- : BasicHandler(parent),
+ : CoreBasicHandler(parent),
_whois(false)
{
+ connect(parent, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetsplits()));
}
IrcServerHandler::~IrcServerHandler() {
+ destroyNetsplits();
}
/*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
QString foo = serverDecode(params.takeFirst());
+ // with SASL, the command is 'AUTHENTICATE +' and we should check for this here.
+ /* obsolete because of events
+ if(foo == QString("AUTHENTICATE +")) {
+ handleAuthenticate();
+ return;
+ }
+ */
// a colon as the first chars indicates the existence of a prefix
if(foo[0] == ':') {
foo.remove(0, 1);
void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const QList<QByteArray> &rawparams) {
- // we assume that all this happens in server encoding
- QStringList params = serverDecode(rawparams);
- uint num = cmd.toUInt();
- if(num) {
- // A lot of server messages don't really need their own handler because they don't do much.
- // Catch and handle these here.
- switch(num) {
- // Welcome, status, info messages. Just display these.
- case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", params.join(" "), prefix);
- break;
- // 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
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", params.join(" "), prefix);
- break;
- // Server error messages, display them in red. First param will be appended.
- case 401: {
- QString target = params.takeFirst();
- emit displayMsg(Message::Error, target, params.join(" ") + " " + target, prefix, Message::Redirected);
- break;
- }
- case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
- QString channelName = params.takeFirst();
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", params.join(" ") + " " + channelName, prefix);
- break;
- }
- // Server error messages which will be displayed with a colon between the first param and the rest
- case 413: case 414: case 423: case 441: case 444: case 461: // FIXME see below for the 47x codes
- case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
- case 436: // ERR_NICKCOLLISION
- {
- QString p = params.takeFirst();
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", p + ": " + params.join(" "));
- break;
- }
- // Ignore these commands.
- case 321: case 366: case 376:
- break;
-
- // Everything else will be marked in red, so we can add them somewhere.
- default:
- if(_whois) {
- // 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(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
- coreSession()->ircListHelper()->reportError(params.join(" "));
- else
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", cmd + " " + params.join(" "), prefix);
- }
- }
- //qDebug() << prefix <<":"<<cmd<<params;
- } else {
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Unknown: ") + cmd + " " + params.join(" "), prefix);
- //qDebug() << prefix <<":"<<cmd<<params;
- }
+ // many commands are handled by the event system now
+ Q_UNUSED(cmd)
+ Q_UNUSED(prefix)
+ Q_UNUSED(rawparams)
}
//******************************/
// IRC SERVER HANDLER
//******************************/
+
void IrcServerHandler::handleJoin(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handleJoin()", params, 1))
return;
}
}
-void IrcServerHandler::handleKick(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handleKick()", params, 2))
- return;
-
- network()->updateNickFromMask(prefix);
- IrcUser *victim = network()->ircUser(params[1]);
- if(!victim)
- return;
-
- QString channel = serverDecode(params[0]);
- victim->partChannel(channel);
-
- QString msg;
- if(params.count() > 2) // someone got a reason!
- msg = QString("%1 %2").arg(victim->nick()).arg(channelDecode(channel, params[2]));
- else
- msg = victim->nick();
-
- emit displayMsg(Message::Kick, BufferInfo::ChannelBuffer, channel, msg, prefix);
- //if(network()->isMe(victim)) network()->setKickedFromChannel(channel);
-}
-
void IrcServerHandler::handleMode(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handleMode()", params, 2))
return;
// user channel modes (op, voice, etc...)
if(paramOffset < params.count()) {
IrcUser *ircUser = network()->ircUser(params[paramOffset]);
- if(add)
- channel->addUserMode(ircUser, QString(modes[c]));
- else
- channel->removeUserMode(ircUser, QString(modes[c]));
+ if(!ircUser) {
+ qWarning() << Q_FUNC_INFO << "Unknown IrcUser:" << params[paramOffset];
+ } else {
+ if(add) {
+ bool handledByNetsplit = false;
+ if(!_netsplits.empty()) {
+ foreach(Netsplit* n, _netsplits) {
+ handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
+ if(handledByNetsplit) {
+ n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
+ break;
+ }
+ }
+ }
+ if(!handledByNetsplit)
+ channel->addUserMode(ircUser, QString(modes[c]));
+ }
+ else
+ channel->removeUserMode(ircUser, QString(modes[c]));
+ }
} else {
qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
}
if(!removeModes.isEmpty())
ircUser->removeUserModes(removeModes);
+ if(network()->isMe(ircUser)) {
+ network()->updatePersistentModes(addModes, removeModes);
+ }
+
// FIXME: redirect
emit displayMsg(Message::Mode, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
}
}
-void IrcServerHandler::handleNick(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handleNick()", params, 1))
- return;
-
- IrcUser *ircuser = network()->updateNickFromMask(prefix);
- if(!ircuser) {
- qWarning() << "IrcServerHandler::handleNick(): Unknown IrcUser!";
- return;
- }
- QString newnick = serverDecode(params[0]);
- QString oldnick = ircuser->nick();
-
- QString sender = network()->isMyNick(oldnick)
- ? newnick
- : prefix;
-
-
- // 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);
-}
-
void IrcServerHandler::handleNotice(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handleNotice()", params, 2))
return;
}
-void IrcServerHandler::handlePart(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handlePart()", params, 1))
- return;
-
- IrcUser *ircuser = network()->updateNickFromMask(prefix);
- QString channel = serverDecode(params[0]);
- if(!ircuser) {
- qWarning() << "IrcServerHandler::handlePart(): Unknown IrcUser!";
- return;
- }
-
- ircuser->partChannel(channel);
-
- QString msg;
- if(params.count() > 1)
- msg = userDecode(ircuser->nick(), params[1]);
-
- emit displayMsg(Message::Part, BufferInfo::ChannelBuffer, channel, msg, prefix);
- if(network()->isMe(ircuser)) network()->setChannelParted(channel);
-}
-
void IrcServerHandler::handlePing(const QString &prefix, const QList<QByteArray> ¶ms) {
Q_UNUSED(prefix);
putCmd("PONG", params);
}
-void IrcServerHandler::handlePong(const QString &prefix, const QList<QByteArray> ¶ms) {
- Q_UNUSED(prefix);
- // the server is supposed to send back what we passed as param. and we send a timestamp
- // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
- if(params.count() < 2)
- return;
-
- QString timestamp = serverDecode(params[1]);
- QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
- if(!sendTime.isValid()) {
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", "PONG " + serverDecode(params).join(" "), prefix);
- return;
- }
-
- network()->setLatency(sendTime.msecsTo(QTime::currentTime()) / 2);
-}
-
void IrcServerHandler::handlePrivmsg(const QString &prefix, const QList<QByteArray> ¶ms) {
if(!checkParamCount("IrcServerHandler::handlePrivmsg()", params, 1))
return;
? *targetIter
: senderNick;
+#ifdef HAVE_QCA2
+ msg = decrypt(target, msg);
+#endif
// it's possible to pack multiple privmsgs into one param using ctcp
// - > we let the ctcpHandler do the work
network()->ctcpHandler()->parse(Message::Plain, prefix, target, msg);
if(!_netsplits.contains(msg)) {
n = new Netsplit();
connect(n, SIGNAL(finished()), this, SLOT(handleNetsplitFinished()));
- connect(n, SIGNAL(netsplitJoin(QString,QStringList,QString)), this, SLOT(handleNetsplitJoin(QString,QStringList,QString)));
- connect(n, SIGNAL(netsplitQuit(QString,QStringList,QString)), this, SLOT(handleNetsplitQuit(QString,QStringList,QString)));
+ connect(n, SIGNAL(netsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)),
+ this, SLOT(handleNetsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)));
+ connect(n, SIGNAL(netsplitQuit(const QString&, const QStringList&, const QString&)),
+ this, SLOT(handleNetsplitQuit(const QString&, const QStringList&, const QString&)));
+ connect(n, SIGNAL(earlyJoin(const QString&, const QStringList&, const QStringList&)),
+ this, SLOT(handleEarlyNetsplitJoin(const QString&, const QStringList&, const QStringList&)));
_netsplits.insert(msg, n);
}
else {
}
}
-void IrcServerHandler::handleTopic(const QString &prefix, const QList<QByteArray> ¶ms) {
- if(!checkParamCount("IrcServerHandler::handleTopic()", params, 1))
- return;
-
- IrcUser *ircuser = network()->updateNickFromMask(prefix);
- if(!ircuser)
- return;
-
- IrcChannel *channel = network()->ircChannel(serverDecode(params[0]));
- if(!channel)
- return;
-
- QString topic;
- if(params.count() > 1)
- topic = channelDecode(channel->name(), params[1]);
-
- channel->setTopic(topic);
-
- emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel->name()).arg(topic));
-}
-
-/* RPL_WELCOME */
-void IrcServerHandler::handle001(const QString &prefix, const QList<QByteArray> ¶ms) {
- network()->setCurrentServer(prefix);
-
- if(params.isEmpty()) {
- emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("%1 didn't supply a valid welcome message... expect some serious issues..."));
- }
- // there should be only one param: "Welcome to the Internet Relay Network <nick>!<user>@<host>"
- QString param = serverDecode(params[0]);
- QString myhostmask = param.section(' ', -1, -1);
-
- network()->setMyNick(nickFromMask(myhostmask));
-
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", param, prefix);
-}
-
/* RPL_ISUPPORT */
// TODO Complete 005 handling, also use sensible defaults for non-sent stuff
void IrcServerHandler::handle005(const QString &prefix, const QList<QByteArray> ¶ms) {
QString value = rawSupport.section("=", 1);
network()->addSupport(key, value);
}
+
+ /* determine our prefixes here to get an accurate result */
+ network()->determinePrefixes();
}
/* RPL_UMODEIS - "<user_modes> [<user_mode_params>]" */
ircuser->setRealName(serverDecode(params.last()));
emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(ircuser->nick()).arg(ircuser->hostmask()).arg(ircuser->realName()));
} else {
- emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(serverDecode(params[1])).arg(serverDecode(params[2])).arg(serverDecode(params.last())));
+ QString host = QString("%1!%2@%3").arg(serverDecode(params[0])).arg(serverDecode(params[1])).arg(serverDecode(params[2]));
+ emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(serverDecode(params[0])).arg(host).arg(serverDecode(params.last())));
}
}
QString nick = serverDecode(params[0]);
IrcUser *ircuser = network()->ircUser(nick);
+
+ QDateTime now = QDateTime::currentDateTime();
+ int idleSecs = serverDecode(params[1]).toInt();
+ idleSecs *= -1;
+
if(ircuser) {
- QDateTime now = QDateTime::currentDateTime();
- int idleSecs = serverDecode(params[1]).toInt();
- idleSecs *= -1;
ircuser->setIdleTime(now.addSecs(idleSecs));
if(params.size() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
int loginTime = serverDecode(params[2]).toInt();
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(" ")));
+ QDateTime idleSince = now.addSecs(idleSecs);
+ if (params.size() > 3) { // we have a signon time
+ int loginTime = serverDecode(params[2]).toInt();
+ QDateTime datetime = QDateTime::fromTime_t(loginTime);
+ emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is logged in since %2").arg(nick).arg(datetime.toString()));
+ }
+ emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is idling for %2 (%3)").arg(nick).arg(secondsToString(idleSince.secsTo(now))).arg(idleSince.toString()));
}
}
emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Channel %1 created on %2").arg(channel, time.toString()));
}
+/* RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
+void IrcServerHandler::handle330(const QString &prefix, const QList<QByteArray> ¶ms) {
+ Q_UNUSED(prefix);
+ if(!checkParamCount("IrcServerHandler::handle330()", params, 3))
+ return;
+
+ QString nick = serverDecode(params[0]);
+ QString account = serverDecode(params[1]);
+
+ emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is authed as %2").arg(nick).arg(account));
+}
+
/* RPL_NOTOPIC */
void IrcServerHandler::handle331(const QString &prefix, const QList<QByteArray> ¶ms) {
Q_UNUSED(prefix);
return;
QString channel = serverDecode(params[0]);
- QString topic = channelDecode(channel, params[1]);
+ QByteArray rawTopic = params[1];
+#ifdef HAVE_QCA2
+ rawTopic = decrypt(channel, rawTopic, true);
+#endif
+ QString topic = channelDecode(channel, rawTopic);
+
IrcChannel *chan = network()->ircChannel(channel);
if(chan)
chan->setTopic(topic);
tr("Topic set by %1 on %2") .arg(serverDecode(params[1]), QDateTime::fromTime_t(channelDecode(channel, params[2]).toUInt()).toString()));
}
+/* RPL_INVITING - "<nick> <channel>*/
+void IrcServerHandler::handle341(const QString &prefix, const QList<QByteArray> ¶ms) {
+ Q_UNUSED(prefix);
+ if(!checkParamCount("IrcServerHandler::handle341()", params, 2))
+ return;
+
+ QString nick = serverDecode(params[0]);
+
+ IrcChannel *channel = network()->ircChannel(serverDecode(params[1]));
+ if(!channel) {
+ qWarning() << "IrcServerHandler::handle341(): unknown channel:" << params[1];
+ return;
+ }
+
+ emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has been invited to %2").arg(nick).arg(channel->name()));
+}
+
/* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
void IrcServerHandler::handle352(const QString &prefix, const QList<QByteArray> ¶ms) {
tryNextNick(errnick);
}
+/* ERR_UNAVAILRESOURCE */
+void IrcServerHandler::handle437(const QString &prefix, const QList<QByteArray> ¶ms) {
+ Q_UNUSED(prefix);
+ if(!checkParamCount("IrcServerHandler::handle437()", params, 1))
+ return;
+
+ QString errnick = serverDecode(params[0]);
+ emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick/channel is temporarily unavailable: %1").arg(errnick));
+
+ // if there is a problem while connecting to the server -> we handle it
+ // but only if our connection has not been finished yet...
+ if(!network()->currentServer().isEmpty())
+ return;
+
+ if(!network()->isChannelName(errnick))
+ tryNextNick(errnick);
+}
+
/* Handle signals from Netsplit objects */
-void IrcServerHandler::handleNetsplitJoin(const QString &channel, const QStringList &users, const QString& quitMessage)
+void IrcServerHandler::handleNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes, const QString& quitMessage)
{
- QString msg = users.join(":").append(':').append(quitMessage);
- emit displayMsg(Message::NetsplitJoin, BufferInfo::ChannelBuffer, channel, msg);
+ IrcChannel *ircChannel = network()->ircChannel(channel);
+ if(!ircChannel) {
+ return;
+ }
+ QList<IrcUser *> ircUsers;
+ QStringList newModes = modes;
+ QStringList newUsers = users;
foreach(QString user, users) {
IrcUser *iu = network()->ircUser(nickFromMask(user));
if(iu)
- iu->joinChannel(channel);
+ ircUsers.append(iu);
+ else { // the user already quit
+ int idx = users.indexOf(user);
+ newUsers.removeAt(idx);
+ newModes.removeAt(idx);
+ }
}
+
+ QString msg = newUsers.join("#:#").append("#:#").append(quitMessage);
+ emit displayMsg(Message::NetsplitJoin, BufferInfo::ChannelBuffer, channel, msg);
+ ircChannel->joinIrcUsers(ircUsers, newModes);
}
void IrcServerHandler::handleNetsplitQuit(const QString &channel, const QStringList &users, const QString& quitMessage)
{
- QString msg = users.join(":").append(':').append(quitMessage);
+ QString msg = users.join("#:#").append("#:#").append(quitMessage);
emit displayMsg(Message::NetsplitQuit, BufferInfo::ChannelBuffer, channel, msg);
foreach(QString user, users) {
IrcUser *iu = network()->ircUser(nickFromMask(user));
}
}
+void IrcServerHandler::handleEarlyNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes) {
+ IrcChannel *ircChannel = network()->ircChannel(channel);
+ if(!ircChannel) {
+ qDebug() << "handleEarlyNetsplitJoin(): channel " << channel << " invalid";
+ return;
+ }
+ QList<IrcUser *> ircUsers;
+ QStringList newModes = modes;
+
+ foreach(QString user, users) {
+ IrcUser *iu = network()->updateNickFromMask(user);
+ if(iu) {
+ ircUsers.append(iu);
+ emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, user);
+ }
+ else {
+ newModes.removeAt(users.indexOf(user));
+ }
+ }
+ ircChannel->joinIrcUsers(ircUsers, newModes);
+}
void IrcServerHandler::handleNetsplitFinished()
{
Netsplit* n = qobject_cast<Netsplit*>(sender());
}
}
+void IrcServerHandler::destroyNetsplits() {
+ qDeleteAll(_netsplits);
+ _netsplits.clear();
+}
-/***********************************************************************************/
+#ifdef HAVE_QCA2
+QByteArray IrcServerHandler::decrypt(const QString &bufferName, const QByteArray &message_, bool isTopic) {
+ if(message_.isEmpty())
+ return message_;
+ Cipher *cipher = network()->cipher(bufferName);
+ if(!cipher)
+ return message_;
+ QByteArray message = message_;
+ message = isTopic? cipher->decryptTopic(message) : cipher->decrypt(message);
+ return message;
+}
+#endif