Allow sending unknown commands to server with >1 params
[quassel.git] / src / core / coreuserinputhandler.cpp
index 47b9b89..ecff7ef 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  *
@@ -29,7 +29,7 @@
 #include <QRegExp>
 
 CoreUserInputHandler::CoreUserInputHandler(CoreNetwork *parent)
-  : BasicHandler(parent)
+  : CoreBasicHandler(parent)
 {
 }
 
@@ -128,8 +128,8 @@ void CoreUserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QStrin
   if(ctcpTag.isEmpty())
     return;
 
-  QString message = "";
-  QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
+  QString message = msg.section(' ', 2);
+  QString verboseMessage = tr("sending CTCP-%1 request to %2").arg(ctcpTag).arg(nick);
 
   if(ctcpTag == "PING") {
     uint now = QDateTime::currentDateTime().toTime_t();
@@ -169,23 +169,54 @@ void CoreUserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QStrin
   QString sane_msg = msg;
   sane_msg.replace(QRegExp(", +"), ",");
   QStringList params = sane_msg.trimmed().split(" ");
+
   QStringList chans = params[0].split(",", QString::SkipEmptyParts);
   QStringList keys;
+  if(params.count() > 1)
+    keys = params[1].split(",");
+
   int i;
   for(i = 0; i < chans.count(); i++) {
     if(!network()->isChannelName(chans[i]))
       chans[i].prepend('#');
+
+    if(i < keys.count()) {
+      network()->addChannelKey(chans[i], keys[i]);
+    } else {
+      network()->removeChannelKey(chans[i]);
+    }
   }
-  params[0] = chans.join(",");
-  if(params.count() > 1) keys = params[1].split(",");
-  emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
+
+  static const char *cmd = "JOIN";
   i = 0;
-  for(; i < keys.count(); i++) {
-    if(i >= chans.count()) break;
-    network()->addChannelKey(chans[i], keys[i]);
-  }
-  for(; i < chans.count(); i++) {
-    network()->removeChannelKey(chans[i]);
+  QStringList joinChans, joinKeys;
+  int slicesize = chans.count();
+  QList<QByteArray> encodedParams;
+
+  // go through all to-be-joined channels and (re)build the join list
+  while(i < chans.count()) {
+    joinChans.append(chans.at(i));
+    if(i < keys.count())
+      joinKeys.append(keys.at(i));
+
+    // if the channel list we built so far either contains all requested channels or exceeds
+    // the desired amount of channels in this slice, try to send what we have so far
+    if(++i == chans.count() || joinChans.count() >= slicesize) {
+      params.clear();
+      params.append(joinChans.join(","));
+      params.append(joinKeys.join(","));
+      encodedParams = serverEncode(params);
+      // check if it fits in one command
+      if(lastParamOverrun(cmd, encodedParams) == 0) {
+        emit putCmd(cmd, encodedParams);
+      } else if(slicesize > 1) {
+        // back to start of slice, try again with half the amount of channels
+        i -= slicesize;
+        slicesize /= 2;
+      }
+      joinChans.clear();
+      joinKeys.clear();
+    }
   }
 }
 
@@ -387,7 +418,7 @@ void CoreUserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QStr
 
 void CoreUserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo);
-  emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd, msg));
+  emit putCmd(serverEncode(cmd.toUpper()), serverEncode(msg.split(" ")));
 }
 
 void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message) {
@@ -413,7 +444,7 @@ void CoreUserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray
 
 // returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
 int CoreUserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> &params) {
-  // the server will pass our message trunkated to 512 bytes including CRLF with the following format:
+  // the server will pass our message truncated to 512 bytes including CRLF with the following format:
   // ":prefix COMMAND param0 param1 :lastparam"
   // where prefix = "nickname!user@host"
   // that means that the last message can be as long as: