Fixes #742 - JOIN without parameters
[quassel.git] / src / core / userinputhandler.cpp
index 70db434..4a36f66 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  *
@@ -63,8 +63,12 @@ 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())
+    if(me && !me->isAway()) {
       awayMsg = network()->identityPtr()->awayReason();
+      if(awayMsg.isEmpty()) {
+       awayMsg = tr("away");
+      }
+    }
   }
   if(me)
     me->setAwayMessage(awayMsg);
@@ -104,7 +108,11 @@ void UserInputHandler::banOrUnban(const BufferInfo &bufferInfo, const QString &m
       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, "*");
     }
@@ -167,7 +175,7 @@ void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &m
   QString sane_msg = msg;
   sane_msg.replace(QRegExp(", +"), ",");
   QStringList params = sane_msg.trimmed().split(" ");
-  QStringList chans = params[0].split(",");
+  QStringList chans = params[0].split(",", QString::SkipEmptyParts);
   QStringList keys;
   int i;
   for(i = 0; i < chans.count(); i++) {
@@ -317,12 +325,7 @@ void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &m
 }
 
 void UserInputHandler::issueQuit(const QString &reason) {
-  QString quitReason;
-  if(reason.isEmpty())
-    quitReason = network()->identityPtr()->quitReason();
-  else
-    quitReason = reason;
-  emit putCmd("QUIT", serverEncode(quitReason));
+  emit putCmd("QUIT", serverEncode(reason));
 }
 
 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
@@ -354,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(' ')));
@@ -383,6 +405,7 @@ void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo
   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];
 
@@ -412,6 +435,17 @@ 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);
   }
 }
@@ -419,7 +453,7 @@ void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo
 
 void UserInputHandler::putPrivmsg(const QByteArray &target, const QByteArray &message) {
   static const char *cmd = "PRIVMSG";
-  int overrun = lastParamOverrun(cmd, QList<QByteArray>() << message);
+  int overrun = lastParamOverrun(cmd, QList<QByteArray>() << target << message);
   if(overrun) {
     static const char *splitter = " .,-";
     int maxSplitPos = message.count() - overrun;
@@ -467,3 +501,19 @@ int UserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArra
   }
 }
 
+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);
+  }
+}