1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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"
25 #include "networkinfo.h"
26 #include "ctcphandler.h"
29 #include "ircchannel.h"
33 IrcServerHandler::IrcServerHandler(Server *parent)
34 : BasicHandler(parent) {
37 IrcServerHandler::~IrcServerHandler() {
40 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
41 void IrcServerHandler::handleServerMsg(QByteArray rawmsg) {
43 if(rawmsg.isEmpty()) {
44 qWarning() << "Received empty string from server!";
47 // TODO Implement encoding conversion
48 /* At this point, we have a raw message as a byte array. This needs to be converted to a QString somewhere.
49 * Problem is, that at this point we don't know which encoding to use for the various parts of the message.
50 * This is something the command handler needs to take care of (e.g. PRIVMSG needs to first parse for CTCP,
51 * and then convert the raw strings into the correct encoding.
52 * We _can_ safely assume Server encoding for prefix and cmd, but not for the params. Therefore, we need to
53 * change from a QStringList to a QList<QByteArray> in all the handlers, and have the handlers call decodeString
56 QString msg = QString::fromLatin1(rawmsg);
58 // OK, first we split the raw message into its various parts...
63 // a colon as the first chars indicates the existance of a prefix
66 prefix = msg.section(' ', 0, 0);
67 msg = msg.section(' ', 1);
70 // next string without a whitespace is the command
71 cmd = msg.section(' ', 0, 0).toUpper();
72 msg = msg.mid(cmd.length());
75 QString trailing = "";
76 if(msg.contains(" :")) {
77 trailing = msg.section(" :", 1);
78 msg = msg.section(" :", 0, 0);
81 params << msg.split(' ', QString::SkipEmptyParts);
83 if(!trailing.isEmpty()) {
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 Q_ASSERT(params.count() > 0); // Violation to RFC
94 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
95 handle(cmd, Q_ARG(QString, prefix), Q_ARG(QStringList, params));
96 //handle(cmd, Q_ARG(QString, prefix));
97 } catch(Exception e) {
98 emit displayMsg(Message::Error, "", e.msg());
103 void IrcServerHandler::defaultHandler(QString cmd, QString prefix, QStringList params) {
104 uint num = cmd.toUInt();
106 // A lot of server messages don't really need their own handler because they don't do much.
107 // Catch and handle these here.
109 // Welcome, status, info messages. Just display these.
110 case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
111 emit displayMsg(Message::Server, "", params.join(" "), prefix);
113 // Server error messages without param, just display them
114 case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
115 case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
116 case 431: // ERR_NONICKNAMEGIVEN
117 emit displayMsg(Message::Error, "", params.join(" "), prefix);
119 // Server error messages, display them in red. First param will be appended.
120 case 401: case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442:
121 { QString p = params.takeFirst();
122 emit displayMsg(Message::Error, "", params.join(" ") + " " + p, prefix);
125 // Server error messages which will be displayed with a colon between the first param and the rest
126 case 413: case 414: case 423: case 441: case 444: case 461:
127 case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
128 case 436: // ERR_NICKCOLLISION
129 { QString p = params.takeFirst();
130 emit displayMsg(Message::Error, "", p + ": " + params.join(" "));
133 // Ignore these commands.
137 // Everything else will be marked in red, so we can add them somewhere.
139 emit displayMsg(Message::Error, "", cmd + " " + params.join(" "), prefix);
141 //qDebug() << prefix <<":"<<cmd<<params;
143 emit displayMsg(Message::Error, "", QString("Unknown: ") + cmd + " " + params.join(" "), prefix);
144 //qDebug() << prefix <<":"<<cmd<<params;
148 //******************************/
149 // IRC SERVER HANDLER
150 //******************************/
151 void IrcServerHandler::handleJoin(QString prefix, QStringList params) {
152 Q_ASSERT(params.count() == 1);
153 QString channel = params[0];
154 IrcUser *ircuser = networkInfo()->updateNickFromMask(prefix);
155 emit displayMsg(Message::Join, channel, channel, prefix);
156 //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
157 ircuser->joinChannel(channel);
160 void IrcServerHandler::handleKick(QString prefix, QStringList params) {
161 networkInfo()->updateNickFromMask(prefix);
162 IrcUser *victim = networkInfo()->ircUser(params[1]);
163 QString channel = params[0];
166 victim->partChannel(channel);
169 if(params.count() > 2) // someone got a reason!
170 msg = QString("%1 %2").arg(victim->nick()).arg(params[2]);
172 msg = victim->nick();
174 emit displayMsg(Message::Kick, params[0], msg, prefix);
177 void IrcServerHandler::handleMode(QString prefix, QStringList params) {
181 // if(isChannelName(params[0])) {
182 // // TODO only channel-user modes supported by now
183 // QString prefixes = serverSupports["PrefixModes"].toString();
184 // QString modes = params[1];
188 // while(m < modes.length()) {
189 // if(modes[m] == '+') { add = true; m++; continue; }
190 // if(modes[m] == '-') { add = false; m++; continue; }
191 // if(prefixes.contains(modes[m])) { // it's a user channel mode
192 // Q_ASSERT(params.count() > m);
193 // QString nick = params[p++];
194 // if(nicks.contains(nick)) { // sometimes, a server might try to set a MODE on a nick that is no longer there
195 // QVariantMap n = nicks[nick]; QVariantMap clist = n["Channels"].toMap(); QVariantMap chan = clist[params[0]].toMap();
196 // QString mstr = chan["Mode"].toString();
197 // add ? mstr += modes[m] : mstr.remove(modes[m]);
198 // chan["Mode"] = mstr; clist[params[0]] = chan; n["Channels"] = clist; nicks[nick] = n;
199 // emit nickUpdated(network, nick, n);
203 // // TODO add more modes
207 // emit displayMsg(Message::Mode, params[0], params.join(" "), prefix);
209 // //Q_ASSERT(nicks.contains(params[0]));
210 // //QVariantMap n = nicks[params[0]].toMap();
211 // //QString mode = n["Mode"].toString();
212 // emit displayMsg(Message::Mode, "", params.join(" "));
216 void IrcServerHandler::handleNick(QString prefix, QStringList params) {
217 IrcUser *ircuser = networkInfo()->updateNickFromMask(prefix);
219 QString newnick = params[0];
220 QString oldnick = ircuser->nick();
222 foreach(QString channel, ircuser->channels()) {
223 if(networkInfo()->isMyNick(oldnick)) {
224 emit displayMsg(Message::Nick, channel, newnick, newnick);
226 emit displayMsg(Message::Nick, channel, newnick, prefix);
229 ircuser->setNick(newnick);
232 void IrcServerHandler::handleNotice(QString prefix, QStringList params) {
233 if(networkInfo()->currentServer().isEmpty() || networkInfo()->currentServer() == prefix)
234 emit displayMsg(Message::Server, "", params[1], prefix);
236 emit displayMsg(Message::Notice, "", params[1], prefix);
239 void IrcServerHandler::handlePart(QString prefix, QStringList params) {
240 IrcUser *ircuser = networkInfo()->updateNickFromMask(prefix);
241 QString channel = params[0];
244 ircuser->partChannel(channel);
247 if(params.count() > 1)
250 emit displayMsg(Message::Part, params[0], msg, prefix);
253 void IrcServerHandler::handlePing(QString prefix, QStringList params) {
255 emit putCmd("PONG", params);
258 void IrcServerHandler::handlePrivmsg(QString prefix, QStringList params) {
259 networkInfo()->updateNickFromMask(prefix);
261 params << QString("");
263 // it's possible to pack multiple privmsgs into one param using ctcp
264 QStringList messages = server->ctcpHandler()->parse(CtcpHandler::CtcpQuery, prefix, params[0], params[1]);
266 // are we the target or is it a channel?
267 if(networkInfo()->isMyNick(params[0])) {
268 foreach(QString message, messages) {
269 if(!message.isEmpty()) {
270 emit displayMsg(Message::Plain, "", message, prefix, Message::PrivMsg);
275 Q_ASSERT(isChannelName(params[0])); // should be channel!
276 foreach(QString message, messages) {
277 if(!message.isEmpty()) {
278 emit displayMsg(Message::Plain, params[0], message, prefix);
285 void IrcServerHandler::handleQuit(QString prefix, QStringList params) {
286 IrcUser *ircuser = networkInfo()->updateNickFromMask(prefix);
288 //qDebug() << "IrcServerHandler:handleQuit" << prefix << params;
294 foreach(QString channel, ircuser->channels())
295 emit displayMsg(Message::Quit, channel, msg, prefix);
297 networkInfo()->removeIrcUser(nickFromMask(prefix));
300 void IrcServerHandler::handleTopic(QString prefix, QStringList params) {
301 IrcUser *ircuser = networkInfo()->updateNickFromMask(prefix);
302 QString channel = params[0];
303 QString topic = params[1];
306 networkInfo()->ircChannel(channel)->setTopic(topic);
308 emit displayMsg(Message::Server, params[0], tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel).arg(topic));
312 void IrcServerHandler::handle001(QString prefix, QStringList params) {
313 // there should be only one param: "Welcome to the Internet Relay Network <nick>!<user>@<host>"
314 QString myhostmask = params[0].section(' ', -1, -1);
315 networkInfo()->setCurrentServer(prefix);
316 networkInfo()->setMyNick(nickFromMask(myhostmask));
318 emit displayMsg(Message::Server, "", params[0], prefix);
321 // TODO: reimplement perform List!
322 //// send performlist
323 //QStringList performList = networkSettings["Perform"].toString().split( "\n" );
324 //int count = performList.count();
325 //for(int a = 0; a < count; a++) {
326 // if(!performList[a].isEmpty() ) {
327 // userInput(network, "", performList[a]);
333 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
334 void IrcServerHandler::handle005(QString prefix, QStringList params) {
336 QString rpl_isupport_suffix = params.takeLast();
337 if(rpl_isupport_suffix.toLower() != QString("are supported by this server")) {
338 qWarning() << "Received invalid RPL_ISUPPORT! Suffix is:" << rpl_isupport_suffix << "Excpected: are supported by this server";
342 foreach(QString param, params) {
343 QString key = param.section("=", 0, 0);
344 QString value = param.section("=", 1);
345 networkInfo()->addSupport(key, value);
351 void IrcServerHandler::handle331(QString prefix, QStringList params) {
353 networkInfo()->ircChannel(params[0])->setTopic(QString());
354 emit displayMsg(Message::Server, params[0], tr("No topic is set for %1.").arg(params[0]));
358 void IrcServerHandler::handle332(QString prefix, QStringList params) {
360 networkInfo()->ircChannel(params[0])->setTopic(params[1]);
361 emit displayMsg(Message::Server, params[0], tr("Topic for %1 is \"%2\"").arg(params[0]).arg(params[1]));
364 /* Topic set by... */
365 void IrcServerHandler::handle333(QString prefix, QStringList params) {
367 emit displayMsg(Message::Server, params[0], tr("Topic set by %1 on %2").arg(params[1]).arg(QDateTime::fromTime_t(params[2].toUInt()).toString()));
371 void IrcServerHandler::handle353(QString prefix, QStringList params) {
373 params.removeFirst(); // either "=", "*" or "@" indicating a public, private or secret channel
374 QString channelname = params.takeFirst();
376 foreach(QString nick, params.takeFirst().split(' ')) {
377 QString mode = QString();
379 if(networkInfo()->prefixes().contains(nick[0])) {
380 mode = networkInfo()->prefixToMode(nick[0]);
384 IrcUser *ircuser = networkInfo()->newIrcUser(nick);
385 ircuser->joinChannel(channelname);
388 networkInfo()->ircChannel(channelname)->addUserMode(ircuser, mode);
392 /* ERR_ERRONEUSNICKNAME */
393 void IrcServerHandler::handle432(QString prefix, QStringList params) {
396 emit displayMsg(Message::Error, "", tr("Your desired nickname contains illegal characters!"));
397 emit displayMsg(Message::Error, "", tr("Please use /nick <othernick> to continue your IRC-Session!"));
400 // if(params.size() < 2) {
401 // // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
403 // // :irc.scortum.moep.net 432 @@@ :Erroneous Nickname: Illegal characters
404 // // correct server reply:
405 // // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
406 // emit displayMsg(Message::Error, "", tr("There is a nickname in your identity's nicklist which contains illegal characters"));
407 // 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"));
408 // emit displayMsg(Message::Error, "", tr("Please use: /nick <othernick> to continue or clean up your nicklist"));
410 // QString errnick = params[0];
411 // emit displayMsg(Message::Error, "", tr("Nick %1 contains illegal characters").arg(errnick));
412 // // if there is a problem while connecting to the server -> we handle it
413 // // TODO rely on another source...
414 // if(currentServer.isEmpty()) {
415 // QStringList desiredNicks = identity["NickList"].toStringList();
416 // int nextNick = desiredNicks.indexOf(errnick) + 1;
417 // if (desiredNicks.size() > nextNick) {
418 // putCmd("NICK", QStringList(desiredNicks[nextNick]));
420 // emit displayMsg(Message::Error, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
426 /* ERR_NICKNAMEINUSE */
427 void IrcServerHandler::handle433(QString prefix, QStringList params) {
429 QString errnick = params[0];
430 emit displayMsg(Message::Error, "", tr("Nick %1 is already taken").arg(errnick));
431 emit displayMsg(Message::Error, "", tr("Please use /nick <othernick> to continue your IRC-Session!"));
434 // // if there is a problem while connecting to the server -> we handle it
435 // // TODO rely on another source...
436 // if(currentServer.isEmpty()) {
437 // QStringList desiredNicks = identity["NickList"].toStringList();
438 // int nextNick = desiredNicks.indexOf(errnick) + 1;
439 // if (desiredNicks.size() > nextNick) {
440 // putCmd("NICK", QStringList(desiredNicks[nextNick]));
442 // emit displayMsg(Message::Error, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
447 /***********************************************************************************/