07362b60fe980714be6fb4ed292ab1106763cdef
[quassel.git] / src / core / coresessioneventprocessor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coresessioneventprocessor.h"
22
23 #include "coreirclisthelper.h"
24 #include "corenetwork.h"
25 #include "coresession.h"
26 #include "ctcpevent.h"
27 #include "ircevent.h"
28 #include "ircuser.h"
29 #include "messageevent.h"
30 #include "netsplit.h"
31 #include "quassel.h"
32
33 CoreSessionEventProcessor::CoreSessionEventProcessor(CoreSession *session)
34     : BasicHandler("handleCtcp", session),
35     _coreSession(session)
36 {
37     connect(coreSession(), SIGNAL(networkDisconnected(NetworkId)), this, SLOT(destroyNetsplits(NetworkId)));
38     connect(this, SIGNAL(newEvent(Event *)), coreSession()->eventManager(), SLOT(postEvent(Event *)));
39 }
40
41
42 bool CoreSessionEventProcessor::checkParamCount(IrcEvent *e, int minParams)
43 {
44     if (e->params().count() < minParams) {
45         if (e->type() == EventManager::IrcEventNumeric) {
46             qWarning() << "Command " << static_cast<IrcEventNumeric *>(e)->number() << " requires " << minParams << "params, got: " << e->params();
47         }
48         else {
49             QString name = coreSession()->eventManager()->enumName(e->type());
50             qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
51         }
52         e->stop();
53         return false;
54     }
55     return true;
56 }
57
58
59 void CoreSessionEventProcessor::tryNextNick(NetworkEvent *e, const QString &errnick, bool erroneus)
60 {
61     QStringList desiredNicks = coreSession()->identity(e->network()->identity())->nicks();
62     int nextNickIdx = desiredNicks.indexOf(errnick) + 1;
63     QString nextNick;
64     if (nextNickIdx > 0 && desiredNicks.size() > nextNickIdx) {
65         nextNick = desiredNicks[nextNickIdx];
66     }
67     else {
68         if (erroneus) {
69             // FIXME Make this an ErrorEvent or something like that, so it's translated in the client
70             MessageEvent *msgEvent = new MessageEvent(Message::Error, e->network(),
71                 tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"),
72                 QString(), QString(), Message::None, e->timestamp());
73             emit newEvent(msgEvent);
74             return;
75         }
76         else {
77             nextNick = errnick + "_";
78         }
79     }
80     // FIXME Use a proper output event for this
81     coreNetwork(e)->putRawLine("NICK " + coreNetwork(e)->encodeServerString(nextNick));
82 }
83
84
85 void CoreSessionEventProcessor::processIrcEventNumeric(IrcEventNumeric *e)
86 {
87     switch (e->number()) {
88     // CAP stuff
89     case 903:
90     case 904:
91     case 905:
92     case 906:
93     case 907:
94         qobject_cast<CoreNetwork *>(e->network())->putRawLine("CAP END");
95         break;
96
97     default:
98         break;
99     }
100 }
101
102
103 void CoreSessionEventProcessor::processIrcEventAuthenticate(IrcEvent *e)
104 {
105     if (!checkParamCount(e, 1))
106         return;
107
108     if (e->params().at(0) != "+") {
109         qWarning() << "Invalid AUTHENTICATE" << e;
110         return;
111     }
112
113     CoreNetwork *net = coreNetwork(e);
114
115     QString construct = net->saslAccount();
116     construct.append(QChar(QChar::Null));
117     construct.append(net->saslAccount());
118     construct.append(QChar(QChar::Null));
119     construct.append(net->saslPassword());
120     QByteArray saslData = QByteArray(construct.toAscii().toBase64());
121     saslData.prepend("AUTHENTICATE ");
122     net->putRawLine(saslData);
123 }
124
125
126 void CoreSessionEventProcessor::processIrcEventCap(IrcEvent *e)
127 {
128     // for SASL, there will only be a single param of 'sasl', however you can check here for
129     // additional CAP messages (ls, multi-prefix, et cetera).
130
131     if (e->params().count() == 3) {
132         if (e->params().at(2).startsWith("sasl")) { // Freenode (at least) sends "sasl " with a trailing space for some reason!
133             // FIXME use event
134             coreNetwork(e)->putRawLine(coreNetwork(e)->serverEncode("AUTHENTICATE PLAIN")); // Only working with PLAIN atm, blowfish later
135         }
136     }
137 }
138
139
140 void CoreSessionEventProcessor::processIrcEventInvite(IrcEvent *e)
141 {
142     if (checkParamCount(e, 2)) {
143         e->network()->updateNickFromMask(e->prefix());
144     }
145 }
146
147
148 void CoreSessionEventProcessor::processIrcEventJoin(IrcEvent *e)
149 {
150     if (e->testFlag(EventManager::Fake)) // generated by handleEarlyNetsplitJoin
151         return;
152
153     if (!checkParamCount(e, 1))
154         return;
155
156     CoreNetwork *net = coreNetwork(e);
157     QString channel = e->params()[0];
158     IrcUser *ircuser = net->updateNickFromMask(e->prefix());
159
160     bool handledByNetsplit = false;
161     foreach(Netsplit* n, _netsplits.value(e->network())) {
162         handledByNetsplit = n->userJoined(e->prefix(), channel);
163         if (handledByNetsplit)
164             break;
165     }
166
167     if (!handledByNetsplit)
168         ircuser->joinChannel(channel);
169     else
170         e->setFlag(EventManager::Netsplit);
171
172     if (net->isMe(ircuser)) {
173         net->setChannelJoined(channel);
174         // FIXME use event
175         net->putRawLine(net->serverEncode("MODE " + channel)); // we want to know the modes of the channel we just joined, so we ask politely
176     }
177 }
178
179
180 void CoreSessionEventProcessor::lateProcessIrcEventKick(IrcEvent *e)
181 {
182     if (checkParamCount(e, 2)) {
183         e->network()->updateNickFromMask(e->prefix());
184         IrcUser *victim = e->network()->ircUser(e->params().at(1));
185         if (victim) {
186             victim->partChannel(e->params().at(0));
187             //if(e->network()->isMe(victim)) e->network()->setKickedFromChannel(channel);
188         }
189     }
190 }
191
192
193 void CoreSessionEventProcessor::processIrcEventMode(IrcEvent *e)
194 {
195     if (!checkParamCount(e, 2))
196         return;
197
198     if (e->network()->isChannelName(e->params().first())) {
199         // Channel Modes
200
201         IrcChannel *channel = e->network()->ircChannel(e->params()[0]);
202         if (!channel) {
203             // we received mode information for a channel we're not in. that means probably we've just been kicked out or something like that
204             // anyways: we don't have a place to store the data --> discard the info.
205             return;
206         }
207
208         QString modes = e->params()[1];
209         bool add = true;
210         int paramOffset = 2;
211         for (int c = 0; c < modes.length(); c++) {
212             if (modes[c] == '+') {
213                 add = true;
214                 continue;
215             }
216             if (modes[c] == '-') {
217                 add = false;
218                 continue;
219             }
220
221             if (e->network()->prefixModes().contains(modes[c])) {
222                 // user channel modes (op, voice, etc...)
223                 if (paramOffset < e->params().count()) {
224                     IrcUser *ircUser = e->network()->ircUser(e->params()[paramOffset]);
225                     if (!ircUser) {
226                         qWarning() << Q_FUNC_INFO << "Unknown IrcUser:" << e->params()[paramOffset];
227                     }
228                     else {
229                         if (add) {
230                             bool handledByNetsplit = false;
231                             QHash<QString, Netsplit *> splits = _netsplits.value(e->network());
232                             foreach(Netsplit* n, _netsplits.value(e->network())) {
233                                 handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
234                                 if (handledByNetsplit) {
235                                     n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
236                                     break;
237                                 }
238                             }
239                             if (!handledByNetsplit)
240                                 channel->addUserMode(ircUser, QString(modes[c]));
241                         }
242                         else
243                             channel->removeUserMode(ircUser, QString(modes[c]));
244                     }
245                 }
246                 else {
247                     qWarning() << "Received MODE with too few parameters:" << e->params();
248                 }
249                 ++paramOffset;
250             }
251             else {
252                 // regular channel modes
253                 QString value;
254                 Network::ChannelModeType modeType = e->network()->channelModeType(modes[c]);
255                 if (modeType == Network::A_CHANMODE || modeType == Network::B_CHANMODE || (modeType == Network::C_CHANMODE && add)) {
256                     if (paramOffset < e->params().count()) {
257                         value = e->params()[paramOffset];
258                     }
259                     else {
260                         qWarning() << "Received MODE with too few parameters:" << e->params();
261                     }
262                     ++paramOffset;
263                 }
264
265                 if (add)
266                     channel->addChannelMode(modes[c], value);
267                 else
268                     channel->removeChannelMode(modes[c], value);
269             }
270         }
271     }
272     else {
273         // pure User Modes
274         IrcUser *ircUser = e->network()->newIrcUser(e->params().first());
275         QString modeString(e->params()[1]);
276         QString addModes;
277         QString removeModes;
278         bool add = false;
279         for (int c = 0; c < modeString.count(); c++) {
280             if (modeString[c] == '+') {
281                 add = true;
282                 continue;
283             }
284             if (modeString[c] == '-') {
285                 add = false;
286                 continue;
287             }
288             if (add)
289                 addModes += modeString[c];
290             else
291                 removeModes += modeString[c];
292         }
293         if (!addModes.isEmpty())
294             ircUser->addUserModes(addModes);
295         if (!removeModes.isEmpty())
296             ircUser->removeUserModes(removeModes);
297
298         if (e->network()->isMe(ircUser)) {
299             coreNetwork(e)->updatePersistentModes(addModes, removeModes);
300         }
301     }
302 }
303
304
305 void CoreSessionEventProcessor::lateProcessIrcEventNick(IrcEvent *e)
306 {
307     if (checkParamCount(e, 1)) {
308         IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
309         if (!ircuser) {
310             qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
311             return;
312         }
313         QString newnick = e->params().at(0);
314         QString oldnick = ircuser->nick();
315
316         // the order is cruicial
317         // otherwise the client would rename the buffer, see that the assigned ircuser doesn't match anymore
318         // and remove the ircuser from the querybuffer leading to a wrong on/offline state
319         ircuser->setNick(newnick);
320         coreSession()->renameBuffer(e->networkId(), newnick, oldnick);
321     }
322 }
323
324
325 void CoreSessionEventProcessor::lateProcessIrcEventPart(IrcEvent *e)
326 {
327     if (checkParamCount(e, 1)) {
328         IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
329         if (!ircuser) {
330             qWarning() << Q_FUNC_INFO<< "Unknown IrcUser!";
331             return;
332         }
333         QString channel = e->params().at(0);
334         ircuser->partChannel(channel);
335         if (e->network()->isMe(ircuser))
336             qobject_cast<CoreNetwork *>(e->network())->setChannelParted(channel);
337     }
338 }
339
340
341 void CoreSessionEventProcessor::processIrcEventPing(IrcEvent *e)
342 {
343     QString param = e->params().count() ? e->params().first() : QString();
344     // FIXME use events
345     coreNetwork(e)->putRawLine("PONG " + coreNetwork(e)->serverEncode(param));
346 }
347
348
349 void CoreSessionEventProcessor::processIrcEventPong(IrcEvent *e)
350 {
351     // the server is supposed to send back what we passed as param. and we send a timestamp
352     // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
353     if (checkParamCount(e, 2)) {
354         QString timestamp = e->params().at(1);
355         QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
356         if (sendTime.isValid())
357             e->network()->setLatency(sendTime.msecsTo(QTime::currentTime()) / 2);
358     }
359 }
360
361
362 void CoreSessionEventProcessor::processIrcEventQuit(IrcEvent *e)
363 {
364     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
365     if (!ircuser)
366         return;
367
368     QString msg;
369     if (e->params().count() > 0)
370         msg = e->params()[0];
371
372     // check if netsplit
373     if (Netsplit::isNetsplit(msg)) {
374         Netsplit *n;
375         if (!_netsplits[e->network()].contains(msg)) {
376             n = new Netsplit(e->network(), this);
377             connect(n, SIGNAL(finished()), this, SLOT(handleNetsplitFinished()));
378             connect(n, SIGNAL(netsplitJoin(Network*, QString, QStringList, QStringList, QString)),
379                 this, SLOT(handleNetsplitJoin(Network*, QString, QStringList, QStringList, QString)));
380             connect(n, SIGNAL(netsplitQuit(Network*, QString, QStringList, QString)),
381                 this, SLOT(handleNetsplitQuit(Network*, QString, QStringList, QString)));
382             connect(n, SIGNAL(earlyJoin(Network*, QString, QStringList, QStringList)),
383                 this, SLOT(handleEarlyNetsplitJoin(Network*, QString, QStringList, QStringList)));
384             _netsplits[e->network()].insert(msg, n);
385         }
386         else {
387             n = _netsplits[e->network()][msg];
388         }
389         // add this user to the netsplit
390         n->userQuit(e->prefix(), ircuser->channels(), msg);
391         e->setFlag(EventManager::Netsplit);
392     }
393     // normal quit is handled in lateProcessIrcEventQuit()
394 }
395
396
397 void CoreSessionEventProcessor::lateProcessIrcEventQuit(IrcEvent *e)
398 {
399     if (e->testFlag(EventManager::Netsplit))
400         return;
401
402     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
403     if (!ircuser)
404         return;
405
406     ircuser->quit();
407 }
408
409
410 void CoreSessionEventProcessor::processIrcEventTopic(IrcEvent *e)
411 {
412     if (checkParamCount(e, 2)) {
413         e->network()->updateNickFromMask(e->prefix());
414         IrcChannel *channel = e->network()->ircChannel(e->params().at(0));
415         if (channel)
416             channel->setTopic(e->params().at(1));
417     }
418 }
419
420
421 /* RPL_WELCOME */
422 void CoreSessionEventProcessor::processIrcEvent001(IrcEvent *e)
423 {
424     if (!checkParamCount(e, 1))
425         return;
426
427     QString myhostmask = e->params().at(0).section(' ', -1, -1);
428     e->network()->setCurrentServer(e->prefix());
429     e->network()->setMyNick(nickFromMask(myhostmask));
430 }
431
432
433 /* RPL_ISUPPORT */
434 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
435 void CoreSessionEventProcessor::processIrcEvent005(IrcEvent *e)
436 {
437     if (!checkParamCount(e, 1))
438         return;
439
440     QString key, value;
441     for (int i = 0; i < e->params().count() - 1; i++) {
442         QString key = e->params()[i].section("=", 0, 0);
443         QString value = e->params()[i].section("=", 1);
444         e->network()->addSupport(key, value);
445     }
446
447     /* determine our prefixes here to get an accurate result */
448     e->network()->determinePrefixes();
449 }
450
451
452 /* RPL_UMODEIS - "<user_modes> [<user_mode_params>]" */
453 void CoreSessionEventProcessor::processIrcEvent221(IrcEvent *)
454 {
455     // TODO: save information in network object
456 }
457
458
459 /* RPL_STATSCONN - "Highest connection cout: 8000 (7999 clients)" */
460 void CoreSessionEventProcessor::processIrcEvent250(IrcEvent *)
461 {
462     // TODO: save information in network object
463 }
464
465
466 /* RPL_LOCALUSERS - "Current local user: 5024  Max: 7999 */
467 void CoreSessionEventProcessor::processIrcEvent265(IrcEvent *)
468 {
469     // TODO: save information in network object
470 }
471
472
473 /* RPL_GLOBALUSERS - "Current global users: 46093  Max: 47650" */
474 void CoreSessionEventProcessor::processIrcEvent266(IrcEvent *)
475 {
476     // TODO: save information in network object
477 }
478
479
480 /*
481 WHOIS-Message:
482    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
483   and 301 (RPL_AWAY)
484               "<nick> :<away message>"
485 WHO-Message:
486    Replies 352 and 315 paired are used to answer a WHO message.
487
488 WHOWAS-Message:
489    Replies 314 and 369 are responses to a WHOWAS message.
490
491 */
492
493 /* RPL_AWAY - "<nick> :<away message>" */
494 void CoreSessionEventProcessor::processIrcEvent301(IrcEvent *e)
495 {
496     if (!checkParamCount(e, 2))
497         return;
498
499     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
500     if (ircuser) {
501         ircuser->setAway(true);
502         ircuser->setAwayMessage(e->params().at(1));
503         //ircuser->setLastAwayMessage(now);
504     }
505 }
506
507
508 /* RPL_UNAWAY - ":You are no longer marked as being away" */
509 void CoreSessionEventProcessor::processIrcEvent305(IrcEvent *e)
510 {
511     IrcUser *me = e->network()->me();
512     if (me)
513         me->setAway(false);
514
515     if (e->network()->autoAwayActive()) {
516         e->network()->setAutoAwayActive(false);
517         e->setFlag(EventManager::Silent);
518     }
519 }
520
521
522 /* RPL_NOWAWAY - ":You have been marked as being away" */
523 void CoreSessionEventProcessor::processIrcEvent306(IrcEvent *e)
524 {
525     IrcUser *me = e->network()->me();
526     if (me)
527         me->setAway(true);
528 }
529
530
531 /* RPL_WHOISSERVICE - "<user> is registered nick" */
532 void CoreSessionEventProcessor::processIrcEvent307(IrcEvent *e)
533 {
534     if (!checkParamCount(e, 1))
535         return;
536
537     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
538     if (ircuser)
539         ircuser->setWhoisServiceReply(e->params().join(" "));
540 }
541
542
543 /* RPL_SUSERHOST - "<user> is available for help." */
544 void CoreSessionEventProcessor::processIrcEvent310(IrcEvent *e)
545 {
546     if (!checkParamCount(e, 1))
547         return;
548
549     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
550     if (ircuser)
551         ircuser->setSuserHost(e->params().join(" "));
552 }
553
554
555 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
556 void CoreSessionEventProcessor::processIrcEvent311(IrcEvent *e)
557 {
558     if (!checkParamCount(e, 3))
559         return;
560
561     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
562     if (ircuser) {
563         ircuser->setUser(e->params().at(1));
564         ircuser->setHost(e->params().at(2));
565         ircuser->setRealName(e->params().last());
566     }
567 }
568
569
570 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
571 void CoreSessionEventProcessor::processIrcEvent312(IrcEvent *e)
572 {
573     if (!checkParamCount(e, 2))
574         return;
575
576     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
577     if (ircuser)
578         ircuser->setServer(e->params().at(1));
579 }
580
581
582 /*  RPL_WHOISOPERATOR - "<nick> :is an IRC operator" */
583 void CoreSessionEventProcessor::processIrcEvent313(IrcEvent *e)
584 {
585     if (!checkParamCount(e, 1))
586         return;
587
588     IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
589     if (ircuser)
590         ircuser->setIrcOperator(e->params().last());
591 }
592
593
594 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
595 void CoreSessionEventProcessor::processIrcEvent315(IrcEvent *e)
596 {
597     if (!checkParamCount(e, 1))
598         return;
599
600     if (coreNetwork(e)->setAutoWhoDone(e->params()[0]))
601         e->setFlag(EventManager::Silent);
602 }
603
604
605 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
606    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
607 void CoreSessionEventProcessor::processIrcEvent317(IrcEvent *e)
608 {
609     if (!checkParamCount(e, 2))
610         return;
611
612     QDateTime loginTime;
613
614     int idleSecs = e->params()[1].toInt();
615     if (e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
616         int logintime = e->params()[2].toInt();
617         loginTime = QDateTime::fromTime_t(logintime);
618     }
619
620     IrcUser *ircuser = e->network()->ircUser(e->params()[0]);
621     if (ircuser) {
622         ircuser->setIdleTime(e->timestamp().addSecs(-idleSecs));
623         if (loginTime.isValid())
624             ircuser->setLoginTime(loginTime);
625     }
626 }
627
628
629 /* RPL_LIST -  "<channel> <# visible> :<topic>" */
630 void CoreSessionEventProcessor::processIrcEvent322(IrcEvent *e)
631 {
632     if (!checkParamCount(e, 1))
633         return;
634
635     QString channelName;
636     quint32 userCount = 0;
637     QString topic;
638
639     switch (e->params().count()) {
640     case 3:
641         topic = e->params()[2];
642     case 2:
643         userCount = e->params()[1].toUInt();
644     case 1:
645         channelName = e->params()[0];
646     default:
647         break;
648     }
649     if (coreSession()->ircListHelper()->addChannel(e->networkId(), channelName, userCount, topic))
650         e->stop();  // consumed by IrcListHelper, so don't further process/show this event
651 }
652
653
654 /* RPL_LISTEND ":End of LIST" */
655 void CoreSessionEventProcessor::processIrcEvent323(IrcEvent *e)
656 {
657     if (!checkParamCount(e, 1))
658         return;
659
660     if (coreSession()->ircListHelper()->endOfChannelList(e->networkId()))
661         e->stop();  // consumed by IrcListHelper, so don't further process/show this event
662 }
663
664
665 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
666 void CoreSessionEventProcessor::processIrcEvent324(IrcEvent *e)
667 {
668     processIrcEventMode(e);
669 }
670
671
672 /* RPL_NOTOPIC */
673 void CoreSessionEventProcessor::processIrcEvent331(IrcEvent *e)
674 {
675     if (!checkParamCount(e, 1))
676         return;
677
678     IrcChannel *chan = e->network()->ircChannel(e->params()[0]);
679     if (chan)
680         chan->setTopic(QString());
681 }
682
683
684 /* RPL_TOPIC */
685 void CoreSessionEventProcessor::processIrcEvent332(IrcEvent *e)
686 {
687     if (!checkParamCount(e, 2))
688         return;
689
690     IrcChannel *chan = e->network()->ircChannel(e->params()[0]);
691     if (chan)
692         chan->setTopic(e->params()[1]);
693 }
694
695
696 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
697               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
698 void CoreSessionEventProcessor::processIrcEvent352(IrcEvent *e)
699 {
700     if (!checkParamCount(e, 6))
701         return;
702
703     QString channel = e->params()[0];
704     IrcUser *ircuser = e->network()->ircUser(e->params()[4]);
705     if (ircuser) {
706         ircuser->setUser(e->params()[1]);
707         ircuser->setHost(e->params()[2]);
708
709         bool away = e->params()[5].startsWith("G");
710         ircuser->setAway(away);
711         ircuser->setServer(e->params()[3]);
712         ircuser->setRealName(e->params().last().section(" ", 1));
713     }
714
715     if (coreNetwork(e)->isAutoWhoInProgress(channel))
716         e->setFlag(EventManager::Silent);
717 }
718
719
720 /* RPL_NAMREPLY */
721 void CoreSessionEventProcessor::processIrcEvent353(IrcEvent *e)
722 {
723     if (!checkParamCount(e, 3))
724         return;
725
726     // param[0] is either "=", "*" or "@" indicating a public, private or secret channel
727     // we don't use this information at the time beeing
728     QString channelname = e->params()[1];
729
730     IrcChannel *channel = e->network()->ircChannel(channelname);
731     if (!channel) {
732         qWarning() << Q_FUNC_INFO << "Received unknown target channel:" << channelname;
733         return;
734     }
735
736     QStringList nicks;
737     QStringList modes;
738
739     foreach(QString nick, e->params()[2].split(' ')) {
740         QString mode;
741
742         if (e->network()->prefixes().contains(nick[0])) {
743             mode = e->network()->prefixToMode(nick[0]);
744             nick = nick.mid(1);
745         }
746
747         nicks << nick;
748         modes << mode;
749     }
750
751     channel->joinIrcUsers(nicks, modes);
752 }
753
754
755 /* ERR_ERRONEUSNICKNAME */
756 void CoreSessionEventProcessor::processIrcEvent432(IrcEventNumeric *e)
757 {
758     if (!checkParamCount(e, 1))
759         return;
760
761     QString errnick;
762     if (e->params().count() < 2) {
763         // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
764         // nick @@@
765         // :irc.scortum.moep.net 432  @@@ :Erroneous Nickname: Illegal characters
766         // correct server reply:
767         // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
768         e->params().prepend(e->target());
769         e->setTarget("*");
770     }
771     errnick = e->params()[0];
772
773     tryNextNick(e, errnick, true /* erroneus */);
774 }
775
776
777 /* ERR_NICKNAMEINUSE */
778 void CoreSessionEventProcessor::processIrcEvent433(IrcEventNumeric *e)
779 {
780     if (!checkParamCount(e, 1))
781         return;
782
783     QString errnick = e->params().first();
784
785     // if there is a problem while connecting to the server -> we handle it
786     // but only if our connection has not been finished yet...
787     if (!e->network()->currentServer().isEmpty())
788         return;
789
790     tryNextNick(e, errnick);
791 }
792
793
794 /* ERR_UNAVAILRESOURCE */
795 void CoreSessionEventProcessor::processIrcEvent437(IrcEventNumeric *e)
796 {
797     if (!checkParamCount(e, 1))
798         return;
799
800     QString errnick = e->params().first();
801
802     // if there is a problem while connecting to the server -> we handle it
803     // but only if our connection has not been finished yet...
804     if (!e->network()->currentServer().isEmpty())
805         return;
806
807     if (!e->network()->isChannelName(errnick))
808         tryNextNick(e, errnick);
809 }
810
811
812 /* template
813 void CoreSessionEventProcessor::processIrcEvent(IrcEvent *e) {
814   if(!checkParamCount(e, 1))
815     return;
816
817 }
818 */
819
820 /* Handle signals from Netsplit objects  */
821
822 void CoreSessionEventProcessor::handleNetsplitJoin(Network *net,
823     const QString &channel,
824     const QStringList &users,
825     const QStringList &modes,
826     const QString &quitMessage)
827 {
828     IrcChannel *ircChannel = net->ircChannel(channel);
829     if (!ircChannel) {
830         return;
831     }
832     QList<IrcUser *> ircUsers;
833     QStringList newModes = modes;
834     QStringList newUsers = users;
835
836     foreach(const QString &user, users) {
837         IrcUser *iu = net->ircUser(nickFromMask(user));
838         if (iu)
839             ircUsers.append(iu);
840         else { // the user already quit
841             int idx = users.indexOf(user);
842             newUsers.removeAt(idx);
843             newModes.removeAt(idx);
844         }
845     }
846
847     ircChannel->joinIrcUsers(ircUsers, newModes);
848     NetworkSplitEvent *event = new NetworkSplitEvent(EventManager::NetworkSplitJoin, net, channel, newUsers, quitMessage);
849     emit newEvent(event);
850 }
851
852
853 void CoreSessionEventProcessor::handleNetsplitQuit(Network *net, const QString &channel, const QStringList &users, const QString &quitMessage)
854 {
855     NetworkSplitEvent *event = new NetworkSplitEvent(EventManager::NetworkSplitQuit, net, channel, users, quitMessage);
856     emit newEvent(event);
857     foreach(QString user, users) {
858         IrcUser *iu = net->ircUser(nickFromMask(user));
859         if (iu)
860             iu->quit();
861     }
862 }
863
864
865 void CoreSessionEventProcessor::handleEarlyNetsplitJoin(Network *net, const QString &channel, const QStringList &users, const QStringList &modes)
866 {
867     IrcChannel *ircChannel = net->ircChannel(channel);
868     if (!ircChannel) {
869         qDebug() << "handleEarlyNetsplitJoin(): channel " << channel << " invalid";
870         return;
871     }
872     QList<NetworkEvent *> events;
873     QList<IrcUser *> ircUsers;
874     QStringList newModes = modes;
875
876     foreach(QString user, users) {
877         IrcUser *iu = net->updateNickFromMask(user);
878         if (iu) {
879             ircUsers.append(iu);
880             // fake event for scripts that consume join events
881             events << new IrcEvent(EventManager::IrcEventJoin, net, iu->hostmask(), QStringList() << channel);
882         }
883         else {
884             newModes.removeAt(users.indexOf(user));
885         }
886     }
887     ircChannel->joinIrcUsers(ircUsers, newModes);
888     foreach(NetworkEvent *event, events) {
889         event->setFlag(EventManager::Fake); // ignore this in here!
890         emit newEvent(event);
891     }
892 }
893
894
895 void CoreSessionEventProcessor::handleNetsplitFinished()
896 {
897     Netsplit *n = qobject_cast<Netsplit *>(sender());
898     Q_ASSERT(n);
899     QHash<QString, Netsplit *> splithash  = _netsplits.take(n->network());
900     splithash.remove(splithash.key(n));
901     if (splithash.count())
902         _netsplits[n->network()] = splithash;
903     n->deleteLater();
904 }
905
906
907 void CoreSessionEventProcessor::destroyNetsplits(NetworkId netId)
908 {
909     Network *net = coreSession()->network(netId);
910     if (!net)
911         return;
912
913     QHash<QString, Netsplit *> splits = _netsplits.take(net);
914     qDeleteAll(splits);
915 }
916
917
918 /*******************************/
919 /******** CTCP HANDLING ********/
920 /*******************************/
921
922 void CoreSessionEventProcessor::processCtcpEvent(CtcpEvent *e)
923 {
924     if (e->testFlag(EventManager::Self))
925         return;  // ignore ctcp events generated by user input
926
927     if (e->type() != EventManager::CtcpEvent || e->ctcpType() != CtcpEvent::Query)
928         return;
929
930     handle(e->ctcpCmd(), Q_ARG(CtcpEvent *, e));
931 }
932
933
934 void CoreSessionEventProcessor::defaultHandler(const QString &ctcpCmd, CtcpEvent *e)
935 {
936     // This handler is only there to avoid warnings for unknown CTCPs
937     Q_UNUSED(e);
938     Q_UNUSED(ctcpCmd);
939 }
940
941
942 void CoreSessionEventProcessor::handleCtcpAction(CtcpEvent *e)
943 {
944     // This handler is only there to feed CLIENTINFO
945     Q_UNUSED(e);
946 }
947
948
949 void CoreSessionEventProcessor::handleCtcpClientinfo(CtcpEvent *e)
950 {
951     QStringList supportedHandlers;
952     foreach(QString handler, providesHandlers())
953     supportedHandlers << handler.toUpper();
954     qSort(supportedHandlers);
955     e->setReply(supportedHandlers.join(" "));
956 }
957
958
959 void CoreSessionEventProcessor::handleCtcpPing(CtcpEvent *e)
960 {
961     e->setReply(e->param());
962 }
963
964
965 void CoreSessionEventProcessor::handleCtcpTime(CtcpEvent *e)
966 {
967     e->setReply(QDateTime::currentDateTime().toString());
968 }
969
970
971 void CoreSessionEventProcessor::handleCtcpVersion(CtcpEvent *e)
972 {
973     e->setReply(QString("Quassel IRC %1 (built on %2) -- http://www.quassel-irc.org")
974         .arg(Quassel::buildInfo().plainVersionString).arg(Quassel::buildInfo().buildDate));
975 }