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