Fix includes
[quassel.git] / src / core / coresessioneventprocessor.cpp
index bb2c211..1082617 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2010 by the Quassel Project                        *
+ *   Copyright (C) 2005-2012 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "coreirclisthelper.h"
 #include "corenetwork.h"
 #include "coresession.h"
+#include "ctcpevent.h"
 #include "ircevent.h"
 #include "ircuser.h"
 #include "messageevent.h"
 #include "netsplit.h"
+#include "quassel.h"
 
 CoreSessionEventProcessor::CoreSessionEventProcessor(CoreSession *session)
-  : QObject(session),
+  : BasicHandler("handleCtcp", session),
   _coreSession(session)
 {
   connect(coreSession(), SIGNAL(networkDisconnected(NetworkId)), this, SLOT(destroyNetsplits(NetworkId)));
+  connect(this, SIGNAL(newEvent(Event *)), coreSession()->eventManager(), SLOT(postEvent(Event *)));
 }
 
 bool CoreSessionEventProcessor::checkParamCount(IrcEvent *e, int minParams) {
@@ -61,7 +64,7 @@ void CoreSessionEventProcessor::tryNextNick(NetworkEvent *e, const QString &errn
       MessageEvent *msgEvent = new MessageEvent(Message::Error, e->network(),
                                                 tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"),
                                                 QString(), QString(), Message::None, e->timestamp());
-      coreSession()->eventManager()->sendEvent(msgEvent);
+      emit newEvent(msgEvent);
       return;
     } else {
       nextNick = errnick + "_";
@@ -153,7 +156,7 @@ void CoreSessionEventProcessor::processIrcEventJoin(IrcEvent *e) {
   }
 }
 
-void CoreSessionEventProcessor::processIrcEventKick(IrcEvent *e) {
+void CoreSessionEventProcessor::lateProcessIrcEventKick(IrcEvent *e) {
   if(checkParamCount(e, 2)) {
     e->network()->updateNickFromMask(e->prefix());
     IrcUser *victim = e->network()->ircUser(e->params().at(1));
@@ -270,7 +273,7 @@ void CoreSessionEventProcessor::processIrcEventMode(IrcEvent *e) {
   }
 }
 
-void CoreSessionEventProcessor::processIrcEventNick(IrcEvent *e) {
+void CoreSessionEventProcessor::lateProcessIrcEventNick(IrcEvent *e) {
   if(checkParamCount(e, 1)) {
     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
     if(!ircuser) {
@@ -288,7 +291,7 @@ void CoreSessionEventProcessor::processIrcEventNick(IrcEvent *e) {
   }
 }
 
-void CoreSessionEventProcessor::processIrcEventPart(IrcEvent *e) {
+void CoreSessionEventProcessor::lateProcessIrcEventPart(IrcEvent *e) {
   if(checkParamCount(e, 1)) {
     IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
     if(!ircuser) {
@@ -302,6 +305,12 @@ void CoreSessionEventProcessor::processIrcEventPart(IrcEvent *e) {
   }
 }
 
+void CoreSessionEventProcessor::processIrcEventPing(IrcEvent *e) {
+  QString param = e->params().count()? e->params().first() : QString();
+  // FIXME use events
+  coreNetwork(e)->putRawLine("PONG " + coreNetwork(e)->serverEncode(param));
+}
+
 void CoreSessionEventProcessor::processIrcEventPong(IrcEvent *e) {
   // the server is supposed to send back what we passed as param. and we send a timestamp
   // but using quote and whatnought one can send arbitrary pings, so we have to do some sanity checks
@@ -656,6 +665,9 @@ void CoreSessionEventProcessor::processIrcEvent353(IrcEvent *e) {
 
 /* ERR_ERRONEUSNICKNAME */
 void CoreSessionEventProcessor::processIrcEvent432(IrcEventNumeric *e) {
+  if(!checkParamCount(e, 1))
+    return;
+
   QString errnick;
   if(e->params().count() < 2) {
     // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
@@ -739,12 +751,12 @@ void CoreSessionEventProcessor::handleNetsplitJoin(Network *net,
 
   ircChannel->joinIrcUsers(ircUsers, newModes);
   NetworkSplitEvent *event = new NetworkSplitEvent(EventManager::NetworkSplitJoin, net, channel, newUsers, quitMessage);
-  coreSession()->eventManager()->sendEvent(event);
+  emit newEvent(event);
 }
 
 void CoreSessionEventProcessor::handleNetsplitQuit(Network *net, const QString &channel, const QStringList &users, const QString& quitMessage) {
   NetworkSplitEvent *event = new NetworkSplitEvent(EventManager::NetworkSplitQuit, net, channel, users, quitMessage);
-  coreSession()->eventManager()->sendEvent(event);
+  emit newEvent(event);
   foreach(QString user, users) {
     IrcUser *iu = net->ircUser(nickFromMask(user));
     if(iu)
@@ -776,7 +788,7 @@ void CoreSessionEventProcessor::handleEarlyNetsplitJoin(Network *net, const QStr
   ircChannel->joinIrcUsers(ircUsers, newModes);
   foreach(NetworkEvent *event, events) {
     event->setFlag(EventManager::Fake); // ignore this in here!
-    coreSession()->eventManager()->sendEvent(event);
+    emit newEvent(event);
   }
 }
 
@@ -798,3 +810,49 @@ void CoreSessionEventProcessor::destroyNetsplits(NetworkId netId) {
   QHash<QString, Netsplit *> splits = _netsplits.take(net);
   qDeleteAll(splits);
 }
+
+/*******************************/
+/******** CTCP HANDLING ********/
+/*******************************/
+
+void CoreSessionEventProcessor::processCtcpEvent(CtcpEvent *e) {
+  if(e->testFlag(EventManager::Self))
+    return; // ignore ctcp events generated by user input
+
+  if(e->type() != EventManager::CtcpEvent || e->ctcpType() != CtcpEvent::Query)
+    return;
+
+  handle(e->ctcpCmd(), Q_ARG(CtcpEvent *, e));
+}
+
+void CoreSessionEventProcessor::defaultHandler(const QString &ctcpCmd, CtcpEvent *e) {
+  // This handler is only there to avoid warnings for unknown CTCPs
+  Q_UNUSED(e);
+  Q_UNUSED(ctcpCmd);
+}
+
+void CoreSessionEventProcessor::handleCtcpAction(CtcpEvent *e) {
+  // This handler is only there to feed CLIENTINFO
+  Q_UNUSED(e);
+}
+
+void CoreSessionEventProcessor::handleCtcpClientinfo(CtcpEvent *e) {
+  QStringList supportedHandlers;
+  foreach(QString handler, providesHandlers())
+    supportedHandlers << handler.toUpper();
+  qSort(supportedHandlers);
+  e->setReply(supportedHandlers.join(" "));
+}
+
+void CoreSessionEventProcessor::handleCtcpPing(CtcpEvent *e) {
+  e->setReply(e->param());
+}
+
+void CoreSessionEventProcessor::handleCtcpTime(CtcpEvent *e) {
+  e->setReply(QDateTime::currentDateTime().toString());
+}
+
+void CoreSessionEventProcessor::handleCtcpVersion(CtcpEvent *e) {
+  e->setReply(QString("Quassel IRC %1 (built on %2) -- http://www.quassel-irc.org")
+              .arg(Quassel::buildInfo().plainVersionString).arg(Quassel::buildInfo().buildDate));
+}