fixes #525 - using 'away' as ultimate fallback if no away reason is specified
[quassel.git] / src / core / userinputhandler.cpp
index 64e71ea..8fe87b0 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 
 #include "util.h"
 
-#include "networkconnection.h"
-#include "network.h"
 #include "ctcphandler.h"
+#include "coreidentity.h"
 #include "ircuser.h"
 
 #include <QDebug>
 #include <QRegExp>
 
-UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
+UserInputHandler::UserInputHandler(CoreNetwork *parent)
+  : BasicHandler(parent)
+{
 }
 
 void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
-  try {
-    if(msg_.isEmpty())
-      return;
-    QString cmd;
-    QString msg = msg_;
-    // leading slashes indicate there's a command to call unless there is anothere one in the first section (like a path /proc/cpuinfo)
-    int secondSlashPos = msg.indexOf('/', 1);
-    int firstSpacePos = msg.indexOf(' ');
-    if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
-      if(msg.startsWith("//"))
-       msg.remove(0, 1); // //asdf is transformed to /asdf
-      cmd = QString("SAY");
-    } else {
-      cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
-      msg = msg.section(' ', 1);
-    }
-    handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
-  } catch(Exception e) {
-    emit displayMsg(Message::Error, bufferInfo.type(), "", e.msg());
+  if(msg_.isEmpty())
+    return;
+  QString cmd;
+  QString msg = msg_;
+  // leading slashes indicate there's a command to call unless there is another one in the first section (like a path /proc/cpuinfo)
+  int secondSlashPos = msg.indexOf('/', 1);
+  int firstSpacePos = msg.indexOf(' ');
+  if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
+    if(msg.startsWith("//"))
+      msg.remove(0, 1); // //asdf is transformed to /asdf
+    cmd = QString("SAY");
+  } else {
+    cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
+    msg = msg.section(' ', 1);
   }
+  handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
 }
 
 // ====================
 //  Public Slots
 // ====================
-
 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
 
@@ -67,16 +63,28 @@ void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &m
 
   // if there is no message supplied we have to check if we are already away or not
   if(msg.isEmpty()) {
-    if(me && !me->isAway())
-      awayMsg = networkConnection()->identity()->awayReason();
+    if(me && !me->isAway()) {
+      awayMsg = network()->identityPtr()->awayReason();
+      if(awayMsg.isEmpty()) {
+       awayMsg = tr("away");
+      }
+    }
   }
   if(me)
     me->setAwayMessage(awayMsg);
-  
+
   putCmd("AWAY", serverEncode(awayMsg));
 }
 
 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
+  banOrUnban(bufferInfo, msg, true);
+}
+
+void UserInputHandler::handleUnban(const BufferInfo &bufferInfo, const QString &msg) {
+  banOrUnban(bufferInfo, msg, false);
+}
+
+void UserInputHandler::banOrUnban(const BufferInfo &bufferInfo, const QString &msg, bool ban) {
   QString banChannel;
   QString banUser;
 
@@ -100,24 +108,32 @@ void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &ms
       return;
     }
 
-    if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
+    static QRegExp ipAddress("\\d+\\.\\d+\\.\\d+\\.\\d+");
+    if(ipAddress.exactMatch(generalizedHost))    {
+        int lastDotPos = generalizedHost.lastIndexOf('.') + 1;
+        generalizedHost.replace(lastDotPos, generalizedHost.length() - lastDotPos, '*');
+    } else if(generalizedHost.lastIndexOf(".") != -1 && generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1) != -1) {
       int secondLastPeriodPosition = generalizedHost.lastIndexOf(".", generalizedHost.lastIndexOf(".")-1);
       generalizedHost.replace(0, secondLastPeriodPosition, "*");
     }
-    banUser = QString("*!%1@%2").arg(ircuser->user()).arg(generalizedHost);
+    banUser = QString("*!%1@%2").arg(ircuser->user()generalizedHost);
   } else {
     banUser = params.join(" ");
   }
 
-  QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
+  QString banMode = ban ? "+b" : "-b";
+  QString banMsg = QString("MODE %1 %2 %3").arg(banChannel, banMode, banUser);
   emit putRawLine(serverEncode(banMsg));
 }
 
 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
+
   QString nick = msg.section(' ', 0, 0);
   QString ctcpTag = msg.section(' ', 1, 1).toUpper();
-  if (ctcpTag.isEmpty()) return;
+  if(ctcpTag.isEmpty())
+    return;
+
   QString message = "";
   QString verboseMessage = tr("sending CTCP-%1 request").arg(ctcpTag);
 
@@ -126,7 +142,7 @@ void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &m
     message = QString::number(now);
   }
 
-  networkConnection()->ctcpHandler()->query(nick, ctcpTag, message);
+  network()->ctcpHandler()->query(nick, ctcpTag, message);
   emit displayMsg(Message::Action, BufferInfo::StatusBuffer, "", verboseMessage, network()->myNick());
 }
 
@@ -172,10 +188,10 @@ void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &m
   i = 0;
   for(; i < keys.count(); i++) {
     if(i >= chans.count()) break;
-    networkConnection()->addChannelKey(chans[i], keys[i]);
+    network()->addChannelKey(chans[i], keys[i]);
   }
   for(; i < chans.count(); i++) {
-    networkConnection()->removeChannelKey(chans[i]);
+    network()->removeChannelKey(chans[i]);
   }
 }
 
@@ -183,7 +199,7 @@ void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &m
   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
   QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
   if(reason.isEmpty())
-    reason = networkConnection()->identity()->kickReason();
+    reason = network()->identityPtr()->kickReason();
 
   QList<QByteArray> params;
   params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
@@ -207,7 +223,7 @@ void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &m
 
 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
   if(bufferInfo.bufferName().isEmpty()) return; // server buffer
-  networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
+  network()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
   emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
 }
 
@@ -218,7 +234,7 @@ void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &m
   // if the first argument is neither a channel nor us (user modes are only to oneself) the current buffer is assumed to be the target
   if(!params.isEmpty() && !network()->isChannelName(params[0]) && !network()->isMyNick(params[0]))
     params.prepend(bufferInfo.bufferName());
-  
+
   // TODO handle correct encoding for buffer modes (channelEncode())
   emit putCmd("MODE", serverEncode(params));
 }
@@ -229,11 +245,8 @@ void UserInputHandler::handleMsg(const BufferInfo &bufferInfo, const QString &ms
   if(!msg.contains(' '))
     return;
 
-  QList<QByteArray> params;
-  params << serverEncode(msg.section(' ', 0, 0));
-  params << userEncode(params[0], msg.section(' ', 1));
-
-  emit putCmd("PRIVMSG", params);
+  QByteArray target = serverEncode(msg.section(' ', 0, 0));
+  putPrivmsg(target, userEncode(target, msg.section(' ', 1)));
 }
 
 void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &msg) {
@@ -242,6 +255,15 @@ void UserInputHandler::handleNick(const BufferInfo &bufferInfo, const QString &m
   emit putCmd("NICK", serverEncode(nick));
 }
 
+void UserInputHandler::handleNotice(const BufferInfo &bufferInfo, const QString &msg) {
+  QString bufferName = msg.section(' ', 0, 0);
+  QString payload = msg.section(' ', 1);
+  QList<QByteArray> params;
+  params << serverEncode(bufferName) << channelEncode(bufferInfo.bufferName(), payload);
+  emit putCmd("NOTICE", params);
+  emit displayMsg(Message::Notice, bufferName, payload, network()->myNick(), Message::Self);
+}
+
 void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg) {
   QStringList nicks = msg.split(' ', QString::SkipEmptyParts);
   QString m = "+"; for(int i = 0; i < nicks.count(); i++) m += 'o';
@@ -267,9 +289,9 @@ void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &m
   } else {
     partReason = msg.mid(channelName.length() + 1);
   }
-  
+
   if(partReason.isEmpty())
-    partReason = networkConnection()->identity()->partReason();
+    partReason = network()->identityPtr()->partReason();
 
   params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
   emit putCmd("PART", params);
@@ -299,15 +321,11 @@ void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &
 
 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
+  network()->disconnectFromIrc(true, msg);
+}
 
-  QString quitReason;
-  if(msg.isEmpty())
-    quitReason = networkConnection()->identity()->quitReason();
-  else
-    quitReason = msg;
-
-  emit putCmd("QUIT", serverEncode(quitReason));
-  networkConnection()->disconnectFromIrc();
+void UserInputHandler::issueQuit(const QString &reason) {
+  emit putCmd("QUIT", serverEncode(reason));
 }
 
 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
@@ -316,22 +334,19 @@ void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &
 }
 
 void UserInputHandler::handleSay(const BufferInfo &bufferInfo, const QString &msg) {
-  if(bufferInfo.bufferName().isEmpty()) return;  // server buffer
-  QList<QByteArray> params;
-  params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
-  emit putCmd("PRIVMSG", params);
+  if(bufferInfo.bufferName().isEmpty())
+    return;  // server buffer
+  putPrivmsg(serverEncode(bufferInfo.bufferName()), channelEncode(bufferInfo.bufferName(), msg));
   emit displayMsg(Message::Plain, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick(), Message::Self);
 }
 
 void UserInputHandler::handleTopic(const BufferInfo &bufferInfo, const QString &msg) {
   if(bufferInfo.bufferName().isEmpty()) return;
-  if(!msg.isEmpty()) {
-    QList<QByteArray> params;
-    params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
-    emit putCmd("TOPIC", params);
-  } else {
-    emit networkConnection()->putRawLine("TOPIC " + serverEncode(bufferInfo.bufferName()) + " :");
-  }
+  QList<QByteArray> params;
+  params << serverEncode(bufferInfo.bufferName());
+  if(!msg.isEmpty())
+    params << channelEncode(bufferInfo.bufferName(), msg);
+  emit putCmd("TOPIC", params);
 }
 
 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
@@ -342,6 +357,25 @@ void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &
   emit putCmd("MODE", serverEncode(params));
 }
 
+void UserInputHandler::handleWait(const BufferInfo &bufferInfo, const QString &msg) {
+  int splitPos = msg.indexOf(';');
+  if(splitPos <= 0)
+    return;
+
+  bool ok;
+  int delay = msg.left(splitPos).trimmed().toInt(&ok);
+  if(!ok)
+    return;
+
+  delay *= 1000;
+
+  QString command = msg.mid(splitPos + 1).trimmed();
+  if(command.isEmpty())
+    return;
+
+  _delayedCommands[startTimer(delay)] = Command(bufferInfo, command);
+}
+
 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
   emit putCmd("WHO", serverEncode(msg.split(' ')));
@@ -364,14 +398,35 @@ void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo,
       return;
     }
   }
-  emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
+  emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmdmsg));
 }
 
 void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
+  QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
   QStringList commands = alias.split(QRegExp("; ?"));
   QStringList params = msg.split(' ');
+  QStringList expandedCommands;
   for(int i = 0; i < commands.count(); i++) {
     QString command = commands[i];
+
+    // replace ranges like $1..3
+    if(!params.isEmpty()) {
+      int pos;
+      while((pos = paramRangeR.indexIn(command)) != -1) {
+       int start = paramRangeR.cap(1).toInt();
+       bool ok;
+       int end = paramRangeR.cap(2).toInt(&ok);
+       if(!ok) {
+         end = params.count();
+       }
+       if(end < start)
+         command = command.replace(pos, paramRangeR.matchedLength(), QString());
+       else {
+         command = command.replace(pos, paramRangeR.matchedLength(), QStringList(params.mid(start - 1, end - start + 1)).join(" "));
+       }
+      }
+    }
+
     for(int j = params.count(); j > 0; j--) {
       IrcUser *ircUser = network()->ircUser(params[j - 1]);
       command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
@@ -380,9 +435,86 @@ void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo
     command = command.replace("$0", msg);
     command = command.replace("$channelname", bufferInfo.bufferName());
     command = command.replace("$currentnick", network()->myNick());
+    expandedCommands << command;
+  }
+
+  while(!expandedCommands.isEmpty()) {
+    QString command;
+    if(expandedCommands[0].trimmed().toLower().startsWith("/wait")) {
+      command = expandedCommands.join("; ");
+      expandedCommands.clear();
+    } else {
+      command = expandedCommands.takeFirst();
+    }
     handleUserInput(bufferInfo, command);
   }
 }
 
 
+void UserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message) {
+  static const char *cmd = "PRIVMSG";
+  int overrun = lastParamOverrun(cmd, QList<QByteArray>() << message);
+  if(overrun) {
+    static const char *splitter = " .,-";
+    int maxSplitPos = message.count() - overrun;
+    int splitPos = -1;
+    for(const char *splitChar = splitter; *splitChar != 0; splitChar++) {
+      splitPos = qMax(splitPos, message.lastIndexOf(*splitChar, maxSplitPos));
+    }
+    if(splitPos <= 0) {
+      splitPos = maxSplitPos;
+    }
+    putCmd(cmd, QList<QByteArray>() << target << message.left(splitPos));
+    putPrivmsg(target, message.mid(splitPos));
+    return;
+  } else {
+    putCmd(cmd, QList<QByteArray>() << target << message);
+  }
+}
+
+// returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long
+int UserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArray> &params) {
+  // the server will pass our message trunkated 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:
+  // 512 - nicklen - userlen - hostlen - commandlen - sum(param[0]..param[n-1])) - 2 (for CRLF) - 4 (":!@" + 1space between prefix and command) - max(paramcount - 1, 0) (space for simple params) - 2 (space and colon for last param)
+  IrcUser *me = network()->me();
+  int maxLen = 480 - cmd.toAscii().count(); // educated guess in case we don't know us (yet?)
+
+  if(me)
+    maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toAscii().count() - 6;
+
+  if(!params.isEmpty()) {
+    for(int i = 0; i < params.count() - 1; i++) {
+      maxLen -= (params[i].count() + 1);
+    }
+    maxLen -= 2; // " :" last param separator;
 
+    if(params.last().count() > maxLen) {
+      return params.last().count() - maxLen;
+    } else {
+      return 0;
+    }
+  } else {
+    return 0;
+  }
+}
+
+
+void UserInputHandler::timerEvent(QTimerEvent *event) {
+  if(!_delayedCommands.contains(event->timerId())) {
+    QObject::timerEvent(event);
+    return;
+  }
+  BufferInfo bufferInfo = _delayedCommands[event->timerId()].bufferInfo;
+  QString rawCommand = _delayedCommands[event->timerId()].command;
+  _delayedCommands.remove(event->timerId());
+  event->accept();
+
+  // the stored command might be the result of an alias expansion, so we need to split it up again
+  QStringList commands = rawCommand.split(QRegExp("; ?"));
+  foreach(QString command, commands) {
+    handleUserInput(bufferInfo, command);
+  }
+}