dba9f85f0e4057ddcf46bbcdfb1b9e70235b0e14
[quassel.git] / src / core / ircserverhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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"
21
22 #include "util.h"
23
24 #include "coresession.h"
25 #include "networkconnection.h"
26 #include "network.h"
27 #include "identity.h"
28 #include "ctcphandler.h"
29
30 #include "ircuser.h"
31 #include "ircchannel.h"
32
33 #include <QDebug>
34
35 IrcServerHandler::IrcServerHandler(NetworkConnection *parent)
36   : BasicHandler(parent),
37     networkConnection(parent) {
38       _whois = false;
39 }
40
41 IrcServerHandler::~IrcServerHandler() {
42
43 }
44
45 QString IrcServerHandler::serverDecode(const QByteArray &string) {
46   return networkConnection->serverDecode(string);
47 }
48
49 QStringList IrcServerHandler::serverDecode(const QList<QByteArray> &stringlist) {
50   QStringList list;
51   foreach(QByteArray s, stringlist) list << networkConnection->serverDecode(s);
52   return list;
53 }
54
55 QString IrcServerHandler::bufferDecode(const QString &bufferName, const QByteArray &string) {
56   return networkConnection->bufferDecode(bufferName, string);
57 }
58
59 QStringList IrcServerHandler::bufferDecode(const QString &bufferName, const QList<QByteArray> &stringlist) {
60   QStringList list;
61   foreach(QByteArray s, stringlist) list << networkConnection->bufferDecode(bufferName, s);
62   return list;
63 }
64
65 QString IrcServerHandler::userDecode(const QString &userNick, const QByteArray &string) {
66   return networkConnection->userDecode(userNick, string);
67 }
68
69 QStringList IrcServerHandler::userDecode(const QString &userNick, const QList<QByteArray> &stringlist) {
70   QStringList list;
71   foreach(QByteArray s, stringlist) list << networkConnection->userDecode(userNick, s);
72   return list;
73 }
74
75 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
76 void IrcServerHandler::handleServerMsg(QByteArray msg) {
77   try {
78     if(msg.isEmpty()) {
79       qWarning() << "Received empty string from server!";
80       return;
81     }
82
83     // Now we split the raw message into its various parts...
84     QString prefix = "";
85     QByteArray trailing;
86     QString cmd;
87
88     // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg
89     // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level...
90     int idx = msg.indexOf(" :");
91     if(idx >= 0) {
92       if(msg.length() > idx + 2) trailing = msg.mid(idx + 2);
93       msg = msg.left(idx);
94     }
95     // OK, now it is safe to split...
96     QList<QByteArray> params = msg.split(' ');
97     if(!trailing.isEmpty()) params << trailing;
98     if(params.count() < 1) {
99       qWarning() << "Received invalid string from server!";
100       return;
101     }
102
103     QString foo = serverDecode(params.takeFirst());
104
105     // a colon as the first chars indicates the existence of a prefix
106     if(foo[0] == ':') {
107       foo.remove(0, 1);
108       prefix = foo;
109       if(params.count() < 1) {
110         qWarning() << "Received invalid string from server!";
111         return;
112       }
113       foo = serverDecode(params.takeFirst());
114     }
115
116     // next string without a whitespace is the command
117     cmd = foo.trimmed().toUpper();
118
119     // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this!
120     uint num = cmd.toUInt();
121     if(num > 0) {
122       if(params.count() == 0) {
123         qWarning() << "Message received from server violates RFC and is ignored!";
124         return;
125       }
126       params.removeFirst();
127     }
128
129     // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
130     handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList<QByteArray>, params));
131     //handle(cmd, Q_ARG(QString, prefix));
132   } catch(Exception e) {
133     emit displayMsg(Message::Error, "", e.msg());
134   }
135 }
136
137
138 void IrcServerHandler::defaultHandler(QString cmd, QString prefix, QList<QByteArray> rawparams) {
139   // we assume that all this happens in server encoding
140   QStringList params;
141   foreach(QByteArray r, rawparams) params << serverDecode(r);
142   uint num = cmd.toUInt();
143   if(num) {
144     // A lot of server messages don't really need their own handler because they don't do much.
145     // Catch and handle these here.
146     switch(num) {
147       // Welcome, status, info messages. Just display these.
148       case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
149         emit displayMsg(Message::Server, "", params.join(" "), prefix);
150         break;
151       // Server error messages without param, just display them
152       case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
153       case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
154       case 431: // ERR_NONICKNAMEGIVEN 
155         emit displayMsg(Message::Error, "", params.join(" "), prefix);
156         break;
157       // Server error messages, display them in red. First param will be appended.
158       case 401: case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442:
159       { QString channelName = params.takeFirst();
160       emit displayMsg(Message::Error, "", params.join(" ") + " " + channelName, prefix);
161       emit displayMsg(Message::Error, channelName, params.join(" ") + " " + channelName, prefix);
162       break;
163       }
164       // Server error messages which will be displayed with a colon between the first param and the rest
165       case 413: case 414: case 423: case 441: case 444: case 461:
166       case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
167       case 436: // ERR_NICKCOLLISION
168       { QString p = params.takeFirst();
169         emit displayMsg(Message::Error, "", p + ": " + params.join(" "));
170         break;
171       }
172       // Ignore these commands.
173       case 366: case 376:
174         break;
175
176       // Everything else will be marked in red, so we can add them somewhere.
177       default:
178         emit displayMsg(Message::Error, "", cmd + " " + params.join(" "), prefix);
179     }
180     //qDebug() << prefix <<":"<<cmd<<params;
181   } else {
182     emit displayMsg(Message::Error, "", QString("Unknown: ") + cmd + " " + params.join(" "), prefix);
183     //qDebug() << prefix <<":"<<cmd<<params;
184   }
185 }
186
187 //******************************/
188 // IRC SERVER HANDLER
189 //******************************/
190 void IrcServerHandler::handleJoin(QString prefix, QList<QByteArray> params) {
191   Q_ASSERT(params.count() == 1);
192   QString channel = serverDecode(params[0]);
193   IrcUser *ircuser = network()->updateNickFromMask(prefix);
194   emit displayMsg(Message::Join, channel, channel, prefix);
195   //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
196   ircuser->joinChannel(channel);
197 }
198
199 void IrcServerHandler::handleKick(QString prefix, QList<QByteArray> params) {
200   network()->updateNickFromMask(prefix);
201   IrcUser *victim = network()->ircUser(serverDecode(params[1]));
202   QString channel = serverDecode(params[0]);
203   Q_ASSERT(victim);
204
205   victim->partChannel(channel);
206
207   QString msg;
208   if(params.count() > 2) // someone got a reason!
209     msg = QString("%1 %2").arg(victim->nick()).arg(bufferDecode(channel, params[2]));
210   else
211     msg = victim->nick();
212
213   emit displayMsg(Message::Kick, channel, msg, prefix);
214 }
215
216 void IrcServerHandler::handleMode(QString prefix, QList<QByteArray> params) {
217   if(params.count() < 2) {
218     emit displayMsg(Message::Error, "", tr("Received invalid MODE from %s: %s").arg(prefix).arg(serverDecode(params).join(" ")));
219     return;
220   }
221
222   if(network()->isChannelName(params[0])) {
223     // Channel Modes
224     emit displayMsg(Message::Mode, serverDecode(params[0]), serverDecode(params).join(" "), prefix);
225
226     IrcChannel *channel = network()->ircChannel(params.takeFirst());
227     // FIXME: currently the IrcChannels only support PREFIX-Modes for users
228     // This cannot be fixed unless the SignalProxy() doesn't rely on methodIds anymore
229     QString modes = params.takeFirst();
230     bool add = true;
231     int modeIndex = 0;
232     for(int c = 0; c < modes.length(); c++) {
233       if(modes[c] == '+') {
234         add = true;
235         continue;
236       }
237       if(modes[c] == '-') {
238         add = false;
239         continue;
240       }
241
242       // this is the part where we restrict the mode changes to PREFIXES:
243       if(network()->prefixModes().contains(modes[c]) && modeIndex < params.count()) {
244         IrcUser *ircUser = network()->ircUser(params[modeIndex]);
245         if(add)
246           channel->addUserMode(ircUser, QString(modes[c]));
247         else
248           channel->removeUserMode(ircUser, QString(modes[c]));
249       }
250       modeIndex++;
251     }
252     
253   } else {
254     // pure User Modes
255     emit displayMsg(Message::Mode, "", serverDecode(params).join(" "), prefix);
256   }
257 }
258
259 void IrcServerHandler::handleNick(QString prefix, QList<QByteArray> params) {
260   IrcUser *ircuser = network()->updateNickFromMask(prefix);
261   Q_ASSERT(ircuser);
262   QString newnick = serverDecode(params[0]);
263   QString oldnick = ircuser->nick();
264
265   foreach(QString channel, ircuser->channels()) {
266     if(network()->isMyNick(oldnick)) {
267       emit displayMsg(Message::Nick, channel, newnick, newnick);
268     } else {
269       emit displayMsg(Message::Nick, channel, newnick, prefix);
270     }
271   }
272   ircuser->setNick(newnick);
273 }
274
275 void IrcServerHandler::handleNotice(QString prefix, QList<QByteArray> params) {
276   if(params.count() < 2) {
277     qWarning() << "IrcServerHandler::handleNotice(): not enoug Parameters:" << prefix << serverDecode(params);
278     return;
279   }
280
281   // check if it's only a Server Message or if it's a regular Notice
282   if(network()->currentServer().isEmpty() || network()->currentServer() == prefix) {
283     emit displayMsg(Message::Server, "", serverDecode(params[1]), prefix);
284     return;
285   }
286
287   QString target = serverDecode(params[0]);
288
289   // kick notices to the server buffer if they are directly addressed to us
290   if(network()->isMyNick(target))
291     target = QString("");
292
293   networkConnection->ctcpHandler()->parse(Message::Notice, prefix, target, userDecode(prefix, params[1]));
294 }
295
296 void IrcServerHandler::handlePart(QString prefix, QList<QByteArray> params) {
297   IrcUser *ircuser = network()->updateNickFromMask(prefix);
298   QString channel = serverDecode(params[0]);
299   Q_ASSERT(ircuser);
300
301   ircuser->partChannel(channel);
302
303   QString msg;
304   if(params.count() > 1)
305     msg = userDecode(ircuser->nick(), params[1]);
306
307   emit displayMsg(Message::Part, channel, msg, prefix);
308 }
309
310 void IrcServerHandler::handlePing(QString prefix, QList<QByteArray> params) {
311   Q_UNUSED(prefix);
312   emit putCmd("PONG", serverDecode(params));
313 }
314
315 void IrcServerHandler::handlePrivmsg(QString prefix, QList<QByteArray> params) {
316   IrcUser *ircuser = network()->updateNickFromMask(prefix);
317   Q_ASSERT(ircuser);
318   if(params.count() < 2)
319     params << QByteArray("");
320
321   QString target = serverDecode(params[0]);
322
323   // are we the target?
324   if(network()->isMyNick(target))
325     target = nickFromMask(ircuser->nick());
326
327   // it's possible to pack multiple privmsgs into one param using ctcp
328   // - > we let the ctcpHandler do the work
329   networkConnection->ctcpHandler()->parse(Message::Plain, prefix, target, userDecode(ircuser->nick(), params[1]));
330 }
331
332 void IrcServerHandler::handleQuit(QString prefix, QList<QByteArray> params) {
333   IrcUser *ircuser = network()->updateNickFromMask(prefix);
334   Q_ASSERT(ircuser);
335
336   QString msg;
337   if(params.count())
338     msg = userDecode(ircuser->nick(), params[0]);
339
340   foreach(QString channel, ircuser->channels())
341     emit displayMsg(Message::Quit, channel, msg, prefix);
342
343   network()->removeIrcUser(nickFromMask(prefix));
344 }
345
346 void IrcServerHandler::handleTopic(QString prefix, QList<QByteArray> params) {
347   IrcUser *ircuser = network()->updateNickFromMask(prefix);
348   QString channel = serverDecode(params[0]);
349   QString topic = bufferDecode(channel, params[1]);
350   Q_ASSERT(ircuser);
351
352   network()->ircChannel(channel)->setTopic(topic);
353
354   emit displayMsg(Message::Server, channel, tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel).arg(topic));
355 }
356
357 /* RPL_WELCOME */
358 void IrcServerHandler::handle001(QString prefix, QList<QByteArray> params) {
359   // there should be only one param: "Welcome to the Internet Relay Network <nick>!<user>@<host>"
360   QString param = serverDecode(params[0]);
361   QString myhostmask = param.section(' ', -1, -1);
362   network()->setCurrentServer(prefix);
363   network()->setMyNick(nickFromMask(myhostmask));
364
365   emit displayMsg(Message::Server, "", param, prefix);
366 }
367
368 /* RPL_ISUPPORT */
369 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
370 void IrcServerHandler::handle005(QString prefix, QList<QByteArray> params) {
371   Q_UNUSED(prefix)
372   QString rpl_isupport_suffix = serverDecode(params.takeLast());
373   if(rpl_isupport_suffix.toLower() != QString("are supported by this server")) {
374     qWarning() << "Received invalid RPL_ISUPPORT! Suffix is:" << rpl_isupport_suffix << "Excpected: are supported by this server";
375     return;
376   }
377   
378   foreach(QString param, serverDecode(params)) {
379     QString key = param.section("=", 0, 0);
380     QString value = param.section("=", 1);
381     network()->addSupport(key, value);
382   }
383 }
384
385
386 /* 
387 WHOIS-Message: 
388    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
389   and 301 (RPL_AWAY)
390               "<nick> :<away message>"
391 WHO-Message:
392    Replies 352 and 315 paired are used to answer a WHO message.
393
394 WHOWAS-Message:
395    Replies 314 and 369 are responses to a WHOWAS message.
396
397 */
398
399
400 /*   RPL_AWAY - "<nick> :<away message>" */
401 void IrcServerHandler::handle301(QString prefix, QList<QByteArray> params) {
402   Q_UNUSED(prefix);
403   QString nickName = serverDecode(params[0]);
404   QString awayMessage = serverDecode(params.last());
405
406   IrcUser *ircuser = network()->ircUser(nickName);
407   if(ircuser) {
408     ircuser->setAwayMessage(awayMessage);
409     ircuser->setAway(true);
410   }
411
412   if(_whois) {
413     emit displayMsg(Message::Server, "", tr("[Whois] %1 is away: \"%2\"").arg(nickName).arg(awayMessage));
414   } else {
415     int now = QDateTime::currentDateTime().toTime_t();
416     int silenceTime = 60;
417     if(ircuser && ircuser->lastAwayMessage() + silenceTime < now) {
418       emit displayMsg(Message::Server, params[0], tr("%1 is away: \"%2\"").arg(nickName).arg(awayMessage));
419     }
420     ircuser->setLastAwayMessage(now);
421   }
422 }
423
424 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
425 void IrcServerHandler::handle311(QString prefix, QList<QByteArray> params) {
426   Q_UNUSED(prefix)
427   _whois = true;
428   IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
429   if(ircuser) {
430     ircuser->setUser(serverDecode(params[1]));
431     ircuser->setHost(serverDecode(params[2]));
432     ircuser->setRealName(serverDecode(params.last()));
433   }
434   emit displayMsg(Message::Server, "", tr("[Whois] %1")
435       .arg(serverDecode(params).join(" ")));
436 }
437  
438 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
439 void IrcServerHandler::handle312(QString prefix, QList<QByteArray> params) {
440   Q_UNUSED(prefix)
441   IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
442   if(ircuser) {
443     ircuser->setServer(serverDecode(params[1]));
444   }
445   if(_whois) {
446     emit displayMsg(Message::Server, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
447   } else {
448     emit displayMsg(Message::Server, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
449   }
450 }
451
452 /*  RPL_WHOISOPERATOR - "<nick> :is an IRC operator" */
453 void IrcServerHandler::handle313(QString prefix, QList<QByteArray> params) {
454   Q_UNUSED(prefix)
455   IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
456   if(ircuser) {
457     ircuser->setIrcOperator(params.last());
458   }
459   emit displayMsg(Message::Server, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
460 }
461
462 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
463 void IrcServerHandler::handle314(QString prefix, QList<QByteArray> params) {
464   Q_UNUSED(prefix)
465   emit displayMsg(Message::Server, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
466 }
467
468 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
469 void IrcServerHandler::handle315(QString prefix, QList<QByteArray> params) {
470   Q_UNUSED(prefix)
471   emit displayMsg(Message::Server, "", tr("[Who] %1").arg(serverDecode(params).join(" ")));
472 }
473
474 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle" 
475    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
476    //TODO: parse real life message
477 void IrcServerHandler::handle317(QString prefix, QList<QByteArray> params) {
478   Q_UNUSED(prefix)
479   IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
480   if(ircuser) {
481     QDateTime now = QDateTime::currentDateTime();
482     int idleSecs = serverDecode(params[1]).toInt();
483     idleSecs *= -1;
484     ircuser->setIdleTime(now.addSecs(idleSecs));
485     emit displayMsg(Message::Server, "", tr("[Whois] %1 is idling for %2 seconds").arg(ircuser->nick()).arg(ircuser->idleTime().secsTo(now)));
486   } else {
487     emit displayMsg(Message::Server, "", tr("[Whois] idle message: %1").arg(serverDecode(params).join(" ")));
488   }
489 }
490
491 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
492 void IrcServerHandler::handle318(QString prefix, QList<QByteArray> params) {
493   Q_UNUSED(prefix)
494   _whois = false;
495   emit displayMsg(Message::Server, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
496 }
497
498 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
499 void IrcServerHandler::handle319(QString prefix, QList<QByteArray> params) {
500   Q_UNUSED(prefix)
501   emit displayMsg(Message::Server, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
502 }
503
504 /* RPL_NOTOPIC */
505 void IrcServerHandler::handle331(QString prefix, QList<QByteArray> params) {
506   Q_UNUSED(prefix);
507   QString channel = serverDecode(params[0]);
508   network()->ircChannel(channel)->setTopic(QString());
509   emit displayMsg(Message::Server, channel, tr("No topic is set for %1.").arg(channel));
510 }
511
512 /* RPL_TOPIC */
513 void IrcServerHandler::handle332(QString prefix, QList<QByteArray> params) {
514   Q_UNUSED(prefix);
515   QString channel = serverDecode(params[0]);
516   QString topic = bufferDecode(channel, params[1]);
517   network()->ircChannel(channel)->setTopic(topic);
518   emit displayMsg(Message::Server, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic));
519 }
520
521 /* Topic set by... */
522 void IrcServerHandler::handle333(QString prefix, QList<QByteArray> params) {
523   Q_UNUSED(prefix);
524   QString channel = serverDecode(params[0]);
525   emit displayMsg(Message::Server, channel, tr("Topic set by %1 on %2")
526       .arg(bufferDecode(channel, params[1]), QDateTime::fromTime_t(bufferDecode(channel, params[2]).toUInt()).toString()));
527 }
528
529 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick> 
530               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
531 void IrcServerHandler::handle352(QString prefix, QList<QByteArray> params) {
532   Q_UNUSED(prefix)
533   QString channel = serverDecode(params[0]);
534   IrcUser *ircuser = network()->ircUser(serverDecode(params[4]));
535   ircuser->setUser(serverDecode(params[1]));
536   ircuser->setHost(serverDecode(params[2]));
537
538   bool away = serverDecode(params[5]).startsWith("G") ? true : false;
539   ircuser->setAway(away);
540   ircuser->setServer(serverDecode(params[3])); 
541   ircuser->setRealName(serverDecode(params.last()).section(" ", 1));
542
543   emit displayMsg(Message::Server, "", tr("[Who] %1").arg(serverDecode(params).join(" ")));
544 }
545
546 /* RPL_NAMREPLY */
547 void IrcServerHandler::handle353(QString prefix, QList<QByteArray> params) {
548   Q_UNUSED(prefix)
549   params.removeFirst(); // either "=", "*" or "@" indicating a public, private or secret channel
550   QString channelname = serverDecode(params.takeFirst());
551
552   foreach(QString nick, serverDecode(params.takeFirst()).split(' ')) {
553     QString mode = QString();
554
555     if(network()->prefixes().contains(nick[0])) {
556       mode = network()->prefixToMode(nick[0]);
557       nick = nick.mid(1);
558     }
559
560     IrcUser *ircuser = network()->newIrcUser(nick);
561     ircuser->joinChannel(channelname);
562
563     if(!mode.isNull())
564       network()->ircChannel(channelname)->addUserMode(ircuser, mode);
565   }
566 }
567
568 /*  RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
569 void IrcServerHandler::handle369(QString prefix, QList<QByteArray> params) {
570   Q_UNUSED(prefix)
571   emit displayMsg(Message::Server, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
572 }
573
574 /* ERR_ERRONEUSNICKNAME */
575 void IrcServerHandler::handle432(QString prefix, QList<QByteArray> params) {
576   Q_UNUSED(prefix);
577
578   if(params.size() < 2) {
579     // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
580     // nick @@@
581     // :irc.scortum.moep.net 432  @@@ :Erroneous Nickname: Illegal characters
582     // correct server reply:
583     // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
584     emit displayMsg(Message::Error, "", tr("There is a nickname in your identity's nicklist which contains illegal characters"));
585     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"));
586     emit displayMsg(Message::Error, "", tr("Please use: /nick <othernick> to continue or clean up your nicklist"));
587   } else {
588     QString errnick = params[0];
589     emit displayMsg(Message::Error, "", tr("Nick %1 contains illegal characters").arg(errnick));
590     tryNextNick(errnick);
591   }
592 }
593
594 /* ERR_NICKNAMEINUSE */
595 void IrcServerHandler::handle433(QString prefix, QList<QByteArray> params) {
596   Q_UNUSED(prefix);
597
598   QString errnick = serverDecode(params[0]);
599   emit displayMsg(Message::Error, "", tr("Nick already in use: %1").arg(errnick));
600
601   // if there is a problem while connecting to the server -> we handle it
602   // but only if our connection has not been finished yet...
603   if(!networkConnection->network()->currentServer().isEmpty())
604     return;
605
606   tryNextNick(errnick);
607 }
608
609 void IrcServerHandler::tryNextNick(const QString &errnick) {
610   QStringList desiredNicks = networkConnection->coreSession()->identity(networkConnection->network()->identity())->nicks();
611   int nextNick = desiredNicks.indexOf(errnick) + 1;
612   if(desiredNicks.size() > nextNick) {
613     putCmd("NICK", QStringList(desiredNicks[nextNick]));
614   } else {
615     emit displayMsg(Message::Error, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
616   }
617 }
618
619
620 /***********************************************************************************/
621
622