the alias engine can now expand nicks to their hostnames
[quassel.git] / src / core / userinputhandler.cpp
index a431f3d..6167170 100644 (file)
@@ -24,6 +24,7 @@
 #include "networkconnection.h"
 #include "network.h"
 #include "ctcphandler.h"
+#include "ircuser.h"
 
 #include <QDebug>
 
@@ -36,7 +37,12 @@ void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QStri
       return;
     QString cmd;
     QString msg = msg_;
-    if(!msg.startsWith('/')) {
+    // 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();
@@ -54,17 +60,56 @@ void UserInputHandler::handleUserInput(const BufferInfo &bufferInfo, const QStri
 
 void UserInputHandler::handleAway(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
-  putCmd("AWAY", serverEncode(msg));
+
+  QString awayMsg = msg;
+  IrcUser *me = network()->me();
+
+  // 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->setAwayMessage(awayMsg);
+  
+  putCmd("AWAY", serverEncode(awayMsg));
 }
 
 void UserInputHandler::handleBan(const BufferInfo &bufferInfo, const QString &msg) {
-  if(bufferInfo.type() != BufferInfo::ChannelBuffer)
+  QString banChannel;
+  QString banUser;
+
+  QStringList params = msg.split(" ");
+
+  if(!params.isEmpty() && isChannelName(params[0])) {
+    banChannel = params.takeFirst();
+  } else if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
+    banChannel = bufferInfo.bufferName();
+  } else {
+    emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: channel unknown in command: /BAN %1").arg(msg));
     return;
-  
-  //TODO: find suitable default hostmask if msg gives only nickname 
-  // Example: MODE &oulu +b *!*@*
-  QByteArray banMsg = serverEncode(bufferInfo.bufferName()) + " +b " + bufferEncode(bufferInfo.bufferName(), msg);
-  emit putCmd("MODE", banMsg);
+  }
+
+  if(!params.isEmpty() && !params.contains("!") && network()->ircUser(params[0])) {
+    IrcUser *ircuser = network()->ircUser(params[0]);
+    // generalizedHost changes <nick> to  *!ident@*.sld.tld.
+    QString generalizedHost = ircuser->host();
+    if(generalizedHost.isEmpty()) {
+      emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: host unknown in command: /BAN %1").arg(msg));
+      return;
+    }
+
+    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);
+  } else {
+    banUser = params.join(" ");
+  }
+
+  QString banMsg = QString("MODE %1 +b %2").arg(banChannel).arg(banUser);
+  emit putRawLine(serverEncode(banMsg));
 }
 
 void UserInputHandler::handleCtcp(const BufferInfo &bufferInfo, const QString &msg) {
@@ -106,46 +151,71 @@ void UserInputHandler::handleInvite(const BufferInfo &bufferInfo, const QString
   emit putCmd("INVITE", serverEncode(params));
 }
 
-void UserInputHandler::handleJ(const BufferInfo &bufferInfo, const QString &msg) {
-  Q_UNUSED(bufferInfo)
-  QStringList params = msg.split(" ");
-  if(params.size() > 0 && !params[0].startsWith("#")) {
-    params[0] = QString("#%1").arg(params[0]);
-  }
-  emit putCmd("JOIN", serverEncode(params));
-}
-
 void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
-  QStringList params = msg.split(" ");
-  emit putCmd("JOIN", serverEncode(params));
+  QStringList params = msg.trimmed().split(" ");
+  QStringList chans = params[0].split(",");
+  QStringList keys;
+  int i;
+  for(i = 0; i < chans.count(); i++) {
+    if (chans.at(i)[0].isLetterOrNumber())
+      chans[i].prepend(QChar('#'));
+  }
+  params[0] = chans.join(",");
+  if(params.count() > 1) keys = params[1].split(",");
+  emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
+  i = 0;
+  for(; i < keys.count(); i++) {
+    if(i >= chans.count()) break;
+    networkConnection()->addChannelKey(chans[i], keys[i]);
+  }
+  for(; i < chans.count(); i++) {
+    networkConnection()->removeChannelKey(chans[i]);
+  }
 }
 
 void UserInputHandler::handleKick(const BufferInfo &bufferInfo, const QString &msg) {
   QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
   QString reason = msg.section(' ', 1, -1, QString::SectionSkipEmpty).trimmed();
-  if(reason.isEmpty()) reason = networkConnection()->identity()->kickReason();
+  if(reason.isEmpty())
+    reason = networkConnection()->identity()->kickReason();
+
   QList<QByteArray> params;
-  params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << bufferEncode(bufferInfo.bufferName(), reason);
+  params << serverEncode(bufferInfo.bufferName()) << serverEncode(nick) << channelEncode(bufferInfo.bufferName(), reason);
   emit putCmd("KICK", params);
 }
 
+void UserInputHandler::handleKill(const BufferInfo &bufferInfo, const QString &msg) {
+  Q_UNUSED(bufferInfo)
+  QString nick = msg.section(' ', 0, 0, QString::SectionSkipEmpty);
+  QString pass = msg.section(' ', 1, -1, QString::SectionSkipEmpty);
+  QList<QByteArray> params;
+  params << serverEncode(nick) << serverEncode(pass);
+  emit putCmd("KILL", params);
+}
+
+
 void UserInputHandler::handleList(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
   emit putCmd("LIST", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
 }
 
-
 void UserInputHandler::handleMe(const BufferInfo &bufferInfo, const QString &msg) {
   if(bufferInfo.bufferName().isEmpty()) return; // server buffer
-  networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", bufferEncode(bufferInfo.bufferName(), msg));
+  networkConnection()->ctcpHandler()->query(bufferInfo.bufferName(), "ACTION", msg);
   emit displayMsg(Message::Action, bufferInfo.type(), bufferInfo.bufferName(), msg, network()->myNick());
 }
 
 void UserInputHandler::handleMode(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
-  // TODO handle correct encoding for buffer modes (bufferEncode())
-  emit putCmd("MODE", serverEncode(msg.split(' ', QString::SkipEmptyParts)));
+
+  QStringList params = msg.split(' ', QString::SkipEmptyParts);
+  // 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));
 }
 
 // TODO: show privmsgs
@@ -175,12 +245,41 @@ void UserInputHandler::handleOp(const BufferInfo &bufferInfo, const QString &msg
   emit putCmd("MODE", serverEncode(params));
 }
 
+void UserInputHandler::handleOper(const BufferInfo &bufferInfo, const QString &msg) {
+  Q_UNUSED(bufferInfo)
+  emit putRawLine(serverEncode(QString("OPER %1").arg(msg)));
+}
+
 void UserInputHandler::handlePart(const BufferInfo &bufferInfo, const QString &msg) {
   QList<QByteArray> params;
-  params << serverEncode(bufferInfo.bufferName()) << bufferEncode(bufferInfo.bufferName(), msg);
+  QString partReason;
+
+  // msg might contain either a channel name and/or a reaon, so we have to check if the first word is a known channel
+  QString channelName = msg.section(' ', 0, 0);
+  if(channelName.isEmpty() || !network()->ircChannel(channelName)) {
+    channelName = bufferInfo.bufferName();
+    partReason = msg;
+  } else {
+    partReason = msg.mid(channelName.length() + 1);
+  }
+  
+  if(partReason.isEmpty())
+    partReason = networkConnection()->identity()->partReason();
+
+  params << serverEncode(channelName) << channelEncode(bufferInfo.bufferName(), partReason);
   emit putCmd("PART", params);
 }
 
+void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
+  Q_UNUSED(bufferInfo)
+
+  QString param = msg;
+  if(param.isEmpty())
+    param = QTime::currentTime().toString("hh:mm:ss.zzz");
+
+  putCmd("PING", serverEncode(param));
+}
+
 // TODO: implement queries
 void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
@@ -195,7 +294,15 @@ void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &
 
 void UserInputHandler::handleQuit(const BufferInfo &bufferInfo, const QString &msg) {
   Q_UNUSED(bufferInfo)
-  emit putCmd("QUIT", serverEncode(msg));
+
+  QString quitReason;
+  if(msg.isEmpty())
+    quitReason = networkConnection()->identity()->quitReason();
+  else
+    quitReason = msg;
+
+  emit putCmd("QUIT", serverEncode(quitReason));
+  networkConnection()->disconnectFromIrc();
 }
 
 void UserInputHandler::handleQuote(const BufferInfo &bufferInfo, const QString &msg) {
@@ -206,16 +313,20 @@ 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()) << bufferEncode(bufferInfo.bufferName(), msg);
+  params << serverEncode(bufferInfo.bufferName()) << channelEncode(bufferInfo.bufferName(), msg);
   emit putCmd("PRIVMSG", params);
   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;
-  QList<QByteArray> params;
-  params << serverEncode(bufferInfo.bufferName()) << bufferEncode(bufferInfo.bufferName(), msg);
-  emit putCmd("TOPIC", params);
+  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()) + " :");
+  }
 }
 
 void UserInputHandler::handleVoice(const BufferInfo &bufferInfo, const QString &msg) {
@@ -242,8 +353,31 @@ void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString
 }
 
 void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
-  Q_UNUSED(bufferInfo)
+  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;
+    }
+  }
   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
 }
 
+void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
+  QStringList commands = alias.split(QRegExp("; ?"));
+  QStringList params = msg.split(' ');
+  for(int i = 0; i < commands.count(); i++) {
+    QString command = commands[i];
+    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());
+    handleUserInput(bufferInfo, command);
+  }
+}
+
+