Wait for it...
authorMarcus Eggenberger <egs@quassel-irc.org>
Tue, 30 Dec 2008 13:17:46 +0000 (14:17 +0100)
committerMarcus Eggenberger <egs@quassel-irc.org>
Tue, 30 Dec 2008 13:17:46 +0000 (14:17 +0100)
introducing /wait. Usage: "/wait 5; hi there" Will send the message "hi there" to the current buffer after 5 seconds.
wait is a non-blocking call.

src/core/userinputhandler.cpp
src/core/userinputhandler.h

index 70db434..4b4c079 100644 (file)
@@ -354,6 +354,25 @@ void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &
   emit putCmd("MODE", serverEncode(params));
 }
 
   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(' ')));
 void UserInputHandler::handleWho(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
   emit putCmd("WHO", serverEncode(msg.split(' ')));
@@ -383,6 +402,7 @@ void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo
   QRegExp paramRangeR("\\$(\\d+)\\.\\.(\\d*)");
   QStringList commands = alias.split(QRegExp("; ?"));
   QStringList params = msg.split(' ');
   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];
 
   for(int i = 0; i < commands.count(); i++) {
     QString command = commands[i];
 
@@ -412,6 +432,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());
     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);
   }
 }
     handleUserInput(bufferInfo, command);
   }
 }
@@ -467,3 +498,13 @@ int UserInputHandler::lastParamOverrun(const QString &cmd, const QList<QByteArra
   }
 }
 
   }
 }
 
+
+void UserInputHandler::timerEvent(QTimerEvent *event) {
+  if(!_delayedCommands.contains(event->timerId())) {
+    QObject::timerEvent(event);
+    return;
+  }
+  Command command = _delayedCommands.take(event->timerId());
+  event->accept();
+  handleUserInput(command.bufferInfo, command.command);
+}
index f7bd01d..fe0c3c6 100644 (file)
@@ -60,6 +60,7 @@ public slots:
   void handleSay(const BufferInfo &bufferInfo, const QString &text);
   void handleTopic(const BufferInfo &bufferInfo, const QString &text);
   void handleVoice(const BufferInfo &bufferInfo, const QString &text);
   void handleSay(const BufferInfo &bufferInfo, const QString &text);
   void handleTopic(const BufferInfo &bufferInfo, const QString &text);
   void handleVoice(const BufferInfo &bufferInfo, const QString &text);
+  void handleWait(const BufferInfo &bufferInfo, const QString &text);
   void handleWho(const BufferInfo &bufferInfo, const QString &text);
   void handleWhois(const BufferInfo &bufferInfo, const QString &text);
   void handleWhowas(const BufferInfo &bufferInfo, const QString &text);
   void handleWho(const BufferInfo &bufferInfo, const QString &text);
   void handleWhois(const BufferInfo &bufferInfo, const QString &text);
   void handleWhowas(const BufferInfo &bufferInfo, const QString &text);
@@ -68,11 +69,23 @@ public slots:
 
   void issueQuit(const QString &reason);
 
 
   void issueQuit(const QString &reason);
 
+protected:
+  void timerEvent(QTimerEvent *event);
+
 private:
   void expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg);
   void banOrUnban(const BufferInfo &bufferInfo, const QString &text, bool ban);
   void putPrivmsg(const QByteArray &target, const QByteArray &message);
   int lastParamOverrun(const QString &cmd, const QList<QByteArray> &params);
 private:
   void expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg);
   void banOrUnban(const BufferInfo &bufferInfo, const QString &text, bool ban);
   void putPrivmsg(const QByteArray &target, const QByteArray &message);
   int lastParamOverrun(const QString &cmd, const QList<QByteArray> &params);
+
+  struct Command {
+    BufferInfo bufferInfo;
+    QString command;
+    Command(const BufferInfo &info, const QString &command) : bufferInfo(info), command(command) {}
+    Command() {}
+  };
+
+  QHash<int, Command> _delayedCommands;
 };
 
 
 };