1 /***************************************************************************
2 * Copyright (C) 2005-08 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 "networkconnection.h"
28 #include "ctcphandler.h"
31 #include "ircchannel.h"
35 IrcServerHandler::IrcServerHandler(NetworkConnection *parent)
36 : BasicHandler(parent),
37 networkConnection(parent) {
40 IrcServerHandler::~IrcServerHandler() {
44 QString IrcServerHandler::serverDecode(const QByteArray &string) {
45 return networkConnection->serverDecode(string);
48 QStringList IrcServerHandler::serverDecode(const QList<QByteArray> &stringlist) {
50 foreach(QByteArray s, stringlist) list << networkConnection->serverDecode(s);
54 QString IrcServerHandler::bufferDecode(const QString &bufferName, const QByteArray &string) {
55 return networkConnection->bufferDecode(bufferName, string);
58 QStringList IrcServerHandler::bufferDecode(const QString &bufferName, const QList<QByteArray> &stringlist) {
60 foreach(QByteArray s, stringlist) list << networkConnection->bufferDecode(bufferName, s);
64 QString IrcServerHandler::userDecode(const QString &userNick, const QByteArray &string) {
65 return networkConnection->userDecode(userNick, string);
68 QStringList IrcServerHandler::userDecode(const QString &userNick, const QList<QByteArray> &stringlist) {
70 foreach(QByteArray s, stringlist) list << networkConnection->userDecode(userNick, s);
74 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
75 void IrcServerHandler::handleServerMsg(QByteArray msg) {
78 qWarning() << "Received empty string from server!";
82 // Now we split the raw message into its various parts...
87 // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg
88 // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level...
89 int idx = msg.indexOf(" :");
91 if(msg.length() > idx + 2) trailing = msg.mid(idx + 2);
94 // OK, now it is safe to split...
95 QList<QByteArray> params = msg.split(' ');
96 if(!trailing.isEmpty()) params << trailing;
97 if(params.count() < 1) {
98 qWarning() << "Received invalid string from server!";
102 QString foo = serverDecode(params.takeFirst());
104 // a colon as the first chars indicates the existence of a prefix
108 if(params.count() < 1) {
109 qWarning() << "Received invalid string from server!";
112 foo = serverDecode(params.takeFirst());
115 // next string without a whitespace is the command
116 cmd = foo.trimmed().toUpper();
118 // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this!
119 uint num = cmd.toUInt();
121 if(params.count() == 0) {
122 qWarning() << "Message received from server violates RFC and is ignored!";
125 params.removeFirst();
128 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
129 handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList<QByteArray>, params));
130 //handle(cmd, Q_ARG(QString, prefix));
131 } catch(Exception e) {
132 emit displayMsg(Message::Error, "", e.msg());
137 void IrcServerHandler::defaultHandler(QString cmd, QString prefix, QList<QByteArray> rawparams) {
138 // we assume that all this happens in server encoding
140 foreach(QByteArray r, rawparams) params << serverDecode(r);
141 uint num = cmd.toUInt();
143 // A lot of server messages don't really need their own handler because they don't do much.
144 // Catch and handle these here.
146 // Welcome, status, info messages. Just display these.
147 case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
148 emit displayMsg(Message::Server, "", params.join(" "), prefix);
150 // Server error messages without param, just display them
151 case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
152 case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
153 case 431: // ERR_NONICKNAMEGIVEN
154 emit displayMsg(Message::Error, "", params.join(" "), prefix);
156 // Server error messages, display them in red. First param will be appended.
157 case 401: case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442:
158 { QString p = params.takeFirst();
159 emit displayMsg(Message::Error, "", params.join(" ") + " " + p, prefix);
162 // Server error messages which will be displayed with a colon between the first param and the rest
163 case 413: case 414: case 423: case 441: case 444: case 461:
164 case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
165 case 436: // ERR_NICKCOLLISION
166 { QString p = params.takeFirst();
167 emit displayMsg(Message::Error, "", p + ": " + params.join(" "));
170 // Ignore these commands.
174 // Everything else will be marked in red, so we can add them somewhere.
176 emit displayMsg(Message::Error, "", cmd + " " + params.join(" "), prefix);
178 //qDebug() << prefix <<":"<<cmd<<params;
180 emit displayMsg(Message::Error, "", QString("Unknown: ") + cmd + " " + params.join(" "), prefix);
181 //qDebug() << prefix <<":"<<cmd<<params;
185 //******************************/
186 // IRC SERVER HANDLER
187 //******************************/
188 void IrcServerHandler::handleJoin(QString prefix, QList<QByteArray> params) {
189 Q_ASSERT(params.count() == 1);
190 QString channel = serverDecode(params[0]);
191 IrcUser *ircuser = network()->updateNickFromMask(prefix);
192 emit displayMsg(Message::Join, channel, channel, prefix);
193 //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
194 ircuser->joinChannel(channel);
197 void IrcServerHandler::handleKick(QString prefix, QList<QByteArray> params) {
198 network()->updateNickFromMask(prefix);
199 IrcUser *victim = network()->ircUser(serverDecode(params[1]));
200 QString channel = serverDecode(params[0]);
203 victim->partChannel(channel);
206 if(params.count() > 2) // someone got a reason!
207 msg = QString("%1 %2").arg(victim->nick()).arg(bufferDecode(channel, params[2]));
209 msg = victim->nick();
211 emit displayMsg(Message::Kick, channel, msg, prefix);
214 void IrcServerHandler::handleMode(QString prefix, QList<QByteArray> params) {
215 if(params.count() < 2) {
216 emit displayMsg(Message::Error, "", tr("Received invalid MODE from %s: %s").arg(prefix).arg(serverDecode(params).join(" ")));
220 if(network()->isChannelName(params[0])) {
222 emit displayMsg(Message::Mode, serverDecode(params[0]), serverDecode(params).join(" "), prefix);
224 IrcChannel *channel = network()->ircChannel(params.takeFirst());
225 // FIXME: currently the IrcChannels only support PREFIX-Modes for users
226 // This cannot be fixed unless the SignalProxy() doesn't rely on methodIds anymore
227 QString modes = params.takeFirst();
230 for(int c = 0; c < modes.length(); c++) {
231 if(modes[c] == '+') {
235 if(modes[c] == '-') {
240 // this is the part where we restrict the mode changes to PREFIXES:
241 if(network()->prefixModes().contains(modes[c]) && modeIndex < params.count()) {
242 IrcUser *ircUser = network()->ircUser(params[modeIndex]);
244 channel->addUserMode(ircUser, QString(modes[c]));
246 channel->removeUserMode(ircUser, QString(modes[c]));
253 emit displayMsg(Message::Mode, "", serverDecode(params).join(" "), prefix);
257 void IrcServerHandler::handleNick(QString prefix, QList<QByteArray> params) {
258 IrcUser *ircuser = network()->updateNickFromMask(prefix);
260 QString newnick = serverDecode(params[0]);
261 QString oldnick = ircuser->nick();
263 foreach(QString channel, ircuser->channels()) {
264 if(network()->isMyNick(oldnick)) {
265 emit displayMsg(Message::Nick, channel, newnick, newnick);
267 emit displayMsg(Message::Nick, channel, newnick, prefix);
270 ircuser->setNick(newnick);
273 void IrcServerHandler::handleNotice(QString prefix, QList<QByteArray> params) {
274 if(network()->currentServer().isEmpty() || network()->currentServer() == prefix)
275 emit displayMsg(Message::Server, "", serverDecode(params[1]), prefix);
277 emit displayMsg(Message::Notice, "", userDecode(prefix, params[1]), prefix);
280 void IrcServerHandler::handlePart(QString prefix, QList<QByteArray> params) {
281 IrcUser *ircuser = network()->updateNickFromMask(prefix);
282 QString channel = serverDecode(params[0]);
285 ircuser->partChannel(channel);
288 if(params.count() > 1)
289 msg = userDecode(ircuser->nick(), params[1]);
291 emit displayMsg(Message::Part, channel, msg, prefix);
294 void IrcServerHandler::handlePing(QString prefix, QList<QByteArray> params) {
296 emit putCmd("PONG", serverDecode(params));
299 void IrcServerHandler::handlePrivmsg(QString prefix, QList<QByteArray> params) {
300 IrcUser *ircuser = network()->updateNickFromMask(prefix);
302 if(params.count() < 2)
303 params << QByteArray("");
305 QString target = serverDecode(params[0]);
307 // are we the target or is it a channel?
308 if(network()->isMyNick(target)) {
309 // it's possible to pack multiple privmsgs into one param using ctcp
310 QStringList messages = networkConnection->ctcpHandler()->parse(CtcpHandler::CtcpQuery, prefix, target, userDecode(ircuser->nick(), params[1]));
311 foreach(QString message, messages) {
312 if(!message.isEmpty()) {
313 emit displayMsg(Message::Plain, "", message, prefix, Message::PrivMsg);
317 // so it's probably a channel..
318 if(!isChannelName(target)) {
319 qWarning() << "received PRIVMSG with target" << target << "which is neither us nor a channel!";
323 QStringList messages = networkConnection->ctcpHandler()->parse(CtcpHandler::CtcpQuery, prefix, target, bufferDecode(target, params[1]));
324 foreach(QString message, messages)
325 emit displayMsg(Message::Plain, target, message, prefix);
330 void IrcServerHandler::handleQuit(QString prefix, QList<QByteArray> params) {
331 IrcUser *ircuser = network()->updateNickFromMask(prefix);
336 msg = userDecode(ircuser->nick(), params[0]);
338 foreach(QString channel, ircuser->channels())
339 emit displayMsg(Message::Quit, channel, msg, prefix);
341 network()->removeIrcUser(nickFromMask(prefix));
344 void IrcServerHandler::handleTopic(QString prefix, QList<QByteArray> params) {
345 IrcUser *ircuser = network()->updateNickFromMask(prefix);
346 QString channel = serverDecode(params[0]);
347 QString topic = bufferDecode(channel, params[1]);
350 network()->ircChannel(channel)->setTopic(topic);
352 emit displayMsg(Message::Server, channel, tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel).arg(topic));
356 void IrcServerHandler::handle001(QString prefix, QList<QByteArray> params) {
357 // there should be only one param: "Welcome to the Internet Relay Network <nick>!<user>@<host>"
358 QString param = serverDecode(params[0]);
359 QString myhostmask = param.section(' ', -1, -1);
360 network()->setCurrentServer(prefix);
361 network()->setMyNick(nickFromMask(myhostmask));
363 emit displayMsg(Message::Server, "", param, prefix);
367 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
368 void IrcServerHandler::handle005(QString prefix, QList<QByteArray> params) {
370 QString rpl_isupport_suffix = serverDecode(params.takeLast());
371 if(rpl_isupport_suffix.toLower() != QString("are supported by this server")) {
372 qWarning() << "Received invalid RPL_ISUPPORT! Suffix is:" << rpl_isupport_suffix << "Excpected: are supported by this server";
376 foreach(QString param, serverDecode(params)) {
377 QString key = param.section("=", 0, 0);
378 QString value = param.section("=", 1);
379 network()->addSupport(key, value);
385 void IrcServerHandler::handle331(QString prefix, QList<QByteArray> params) {
387 QString channel = serverDecode(params[0]);
388 network()->ircChannel(channel)->setTopic(QString());
389 emit displayMsg(Message::Server, channel, tr("No topic is set for %1.").arg(channel));
393 void IrcServerHandler::handle332(QString prefix, QList<QByteArray> params) {
395 QString channel = serverDecode(params[0]);
396 QString topic = bufferDecode(channel, params[1]);
397 network()->ircChannel(channel)->setTopic(topic);
398 emit displayMsg(Message::Server, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic));
401 /* Topic set by... */
402 void IrcServerHandler::handle333(QString prefix, QList<QByteArray> params) {
404 QString channel = serverDecode(params[0]);
405 emit displayMsg(Message::Server, channel, tr("Topic set by %1 on %2")
406 .arg(bufferDecode(channel, params[1]), QDateTime::fromTime_t(bufferDecode(channel, params[2]).toUInt()).toString()));
410 void IrcServerHandler::handle353(QString prefix, QList<QByteArray> params) {
412 params.removeFirst(); // either "=", "*" or "@" indicating a public, private or secret channel
413 QString channelname = serverDecode(params.takeFirst());
415 foreach(QString nick, serverDecode(params.takeFirst()).split(' ')) {
416 QString mode = QString();
418 if(network()->prefixes().contains(nick[0])) {
419 mode = network()->prefixToMode(nick[0]);
423 IrcUser *ircuser = network()->newIrcUser(nick);
424 ircuser->joinChannel(channelname);
427 network()->ircChannel(channelname)->addUserMode(ircuser, mode);
431 /* ERR_ERRONEUSNICKNAME */
432 void IrcServerHandler::handle432(QString prefix, QList<QByteArray> params) {
435 if(params.size() < 2) {
436 // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
438 // :irc.scortum.moep.net 432 @@@ :Erroneous Nickname: Illegal characters
439 // correct server reply:
440 // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
441 emit displayMsg(Message::Error, "", tr("There is a nickname in your identity's nicklist which contains illegal characters"));
442 emit displayMsg(Message::Error, "", tr("Due to a bug in Unreal IRCd (and maybe other irc-servers too) we're unable to determine the erroneous nick"));
443 emit displayMsg(Message::Error, "", tr("Please use: /nick <othernick> to continue or clean up your nicklist"));
445 QString errnick = params[0];
446 emit displayMsg(Message::Error, "", tr("Nick %1 contains illegal characters").arg(errnick));
447 tryNextNick(errnick);
451 /* ERR_NICKNAMEINUSE */
452 void IrcServerHandler::handle433(QString prefix, QList<QByteArray> params) {
455 QString errnick = serverDecode(params[0]);
456 emit displayMsg(Message::Error, "", tr("Nick already in use: %1").arg(errnick));
458 // if there is a problem while connecting to the server -> we handle it
459 // but only if our connection has not been finished yet...
460 if(!networkConnection->network()->currentServer().isEmpty())
463 tryNextNick(errnick);
466 void IrcServerHandler::tryNextNick(const QString &errnick) {
467 QStringList desiredNicks = networkConnection->coreSession()->identity(networkConnection->network()->identity())->nicks();
468 int nextNick = desiredNicks.indexOf(errnick) + 1;
469 if(desiredNicks.size() > nextNick) {
470 putCmd("NICK", QStringList(desiredNicks[nextNick]));
472 emit displayMsg(Message::Error, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
477 /***********************************************************************************/