added missing macros
[quassel.git] / src / core / userinputhandler.cpp
index 4b4c079..31ab7a3 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  *
@@ -33,23 +33,17 @@ UserInputHandler::UserInputHandler(CoreNetwork *parent)
 {
 }
 
-void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &msg_) {
-  if(msg_.isEmpty())
+void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QString &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);
+
+  AliasManager::CommandList list = coreSession()->aliasManager().processInput(bufferInfo, msg);
+
+  for(int i = 0; i < list.count(); i++) {
+    QString cmd = list.at(i).second.section(' ', 0, 0).remove(0, 1).toUpper();
+    QString payload = list.at(i).second.section(' ', 1);
+    handle(cmd, Q_ARG(BufferInfo, list.at(i).first), Q_ARG(QString, payload));
   }
-  handle(cmd, Q_ARG(BufferInfo, bufferInfo), Q_ARG(QString, msg));
 }
 
 // ====================
@@ -63,8 +57,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 +102,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 +169,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 +319,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) {
@@ -389,68 +386,13 @@ void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString
 }
 
 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
-  for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
-    if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
-      expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
-      return;
-    }
-  }
+  Q_UNUSED(bufferInfo);
   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd, msg));
 }
 
-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("*"));
-      command = command.replace(QString("$%1").arg(j), params[j - 1]);
-    }
-    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);
+  int overrun = lastParamOverrun(cmd, QList<QByteArray>() << target << message);
   if(overrun) {
     static const char *splitter = " .,-";
     int maxSplitPos = message.count() - overrun;
@@ -498,13 +440,19 @@ 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());
+  BufferInfo bufferInfo = _delayedCommands[event->timerId()].bufferInfo;
+  QString rawCommand = _delayedCommands[event->timerId()].command;
+  _delayedCommands.remove(event->timerId());
   event->accept();
-  handleUserInput(command.bufferInfo, command.command);
+
+  // 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);
+  }
 }