SASL support
[quassel.git] / src / core / ircserverhandler.cpp
index 6dfb838..445ca7c 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-10 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include <QDebug>
 
 IrcServerHandler::IrcServerHandler(CoreNetwork *parent)
-  : BasicHandler(parent),
+  : CoreBasicHandler(parent),
     _whois(false)
 {
+  connect(parent, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetsplits()));
 }
 
 IrcServerHandler::~IrcServerHandler() {
-  if(!_netsplits.empty())
-    qDeleteAll(_netsplits);
+  destroyNetsplits();
 }
 
 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
@@ -84,6 +84,12 @@ void IrcServerHandler::handleServerMsg(QByteArray msg) {
 
   QString foo = serverDecode(params.takeFirst());
 
+  // with SASL, the command is 'AUTHENTICATE +' and we should check for this here.
+  if(foo == QString("AUTHENTICATE +")) {
+    handleAuthenticate();
+    return;
+  }
+
   // a colon as the first chars indicates the existence of a prefix
   if(foo[0] == ':') {
     foo.remove(0, 1);
@@ -160,6 +166,11 @@ void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const
       case 321: case 366: case 376:
         break;
 
+      case 903: case 904: case 905: case 906: case 907:
+      {
+        network()->putRawLine("CAP END");
+        emit displayMsg(Message::Info, BufferInfo::StatusBuffer, "", "CAP: " + params.join(""));
+      }
       // Everything else will be marked in red, so we can add them somewhere.
       default:
         if(_whois) {
@@ -265,22 +276,26 @@ void IrcServerHandler::handleMode(const QString &prefix, const QList<QByteArray>
         // user channel modes (op, voice, etc...)
         if(paramOffset < params.count()) {
           IrcUser *ircUser = network()->ircUser(params[paramOffset]);
-          if(add) {
-            bool handledByNetsplit = false;
-            if(!_netsplits.empty()) {
-              foreach(Netsplit* n, _netsplits) {
-                handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
-                if(handledByNetsplit) {
-                  n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
-                  break;
+          if(!ircUser) {
+            qWarning() << Q_FUNC_INFO << "Unknown IrcUser:" << params[paramOffset];
+          } else {
+            if(add) {
+              bool handledByNetsplit = false;
+              if(!_netsplits.empty()) {
+                foreach(Netsplit* n, _netsplits) {
+                  handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
+                  if(handledByNetsplit) {
+                    n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
+                    break;
+                  }
                 }
               }
+              if(!handledByNetsplit)
+                channel->addUserMode(ircUser, QString(modes[c]));
             }
-            if(!handledByNetsplit)
-              channel->addUserMode(ircUser, QString(modes[c]));
+            else
+              channel->removeUserMode(ircUser, QString(modes[c]));
           }
-          else
-            channel->removeUserMode(ircUser, QString(modes[c]));
         } else {
           qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
         }
@@ -538,6 +553,31 @@ void IrcServerHandler::handleTopic(const QString &prefix, const QList<QByteArray
   emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has changed topic for %2 to: \"%3\"").arg(ircuser->nick()).arg(channel->name()).arg(topic));
 }
 
+void IrcServerHandler::handleCap(const QString &prefix, const QList<QByteArray> &params) {
+    // for SASL, there will only be a single param of 'sasl', however you can check here for
+    // additional CAP messages (ls, multi-prefix, et cetera).
+
+    Q_UNUSED(prefix);
+
+    if(params.size() == 3) {
+        QString param = serverDecode(params[2]);
+        if(param == QString("sasl")) {  // SASL Ready
+            network()->putRawLine(serverEncode("AUTHENTICATE PLAIN"));  // Only working with PLAIN atm, blowfish later
+        }
+    }
+}
+
+void IrcServerHandler::handleAuthenticate() {
+    QString construct = network()->saslAccount();
+    construct.append(QChar(QChar::Null));
+    construct.append(network()->saslAccount());
+    construct.append(QChar(QChar::Null));
+    construct.append(network()->saslPassword());
+    QByteArray saslData = QByteArray(construct.toAscii().toBase64());
+    saslData.prepend(QString("AUTHENTICATE ").toAscii());
+    network()->putRawLine(saslData);
+}
+
 /* RPL_WELCOME */
 void IrcServerHandler::handle001(const QString &prefix, const QList<QByteArray> &params) {
   network()->setCurrentServer(prefix);
@@ -1073,17 +1113,20 @@ void IrcServerHandler::handleNetsplitJoin(const QString &channel, const QStringL
   }
   QList<IrcUser *> ircUsers;
   QStringList newModes = modes;
+  QStringList newUsers = users;
 
   foreach(QString user, users) {
-    IrcUser *iu = network()->updateNickFromMask(user);
+    IrcUser *iu = network()->ircUser(nickFromMask(user));
     if(iu)
       ircUsers.append(iu);
-    else {
-      newModes.removeAt(users.indexOf(user));
+    else { // the user already quit
+      int idx = users.indexOf(user);
+      newUsers.removeAt(idx);
+      newModes.removeAt(idx);
     }
   }
 
-  QString msg = users.join("#:#").append("#:#").append(quitMessage);
+  QString msg = newUsers.join("#:#").append("#:#").append(quitMessage);
   emit displayMsg(Message::NetsplitJoin, BufferInfo::ChannelBuffer, channel, msg);
   ircChannel->joinIrcUsers(ircUsers, newModes);
 }
@@ -1160,6 +1203,10 @@ bool IrcServerHandler::checkParamCount(const QString &methodName, const QList<QB
   }
 }
 
+void IrcServerHandler::destroyNetsplits() {
+  qDeleteAll(_netsplits);
+  _netsplits.clear();
+}
 
 /***********************************************************************************/