Bug 1210226 - Handle nicknames that contain spaces for msg and kick commands in XMPP. r=aleth chat/ patch on CLOSED TREE
authorAbdelrhman Ahmed <a.ahmed1026@gmail.com>
Tue, 03 Nov 2015 05:13:00 +0100
changeset 18539 6fe88dd01169a34338efc6092ccc0d674af7bc3c
parent 18538 f29ebb12e30c276087cd5f8405ae817c15849f8f
child 18540 2435da3ea8b4e7e16b47bcfecc7dec83cc9d276c
push id11352
push useraleth@instantbird.org
push dateTue, 03 Nov 2015 21:26:21 +0000
treeherdercomm-central@6fe88dd01169 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleth
bugs1210226
Bug 1210226 - Handle nicknames that contain spaces for msg and kick commands in XMPP. r=aleth chat/ patch on CLOSED TREE
chat/protocols/xmpp/xmpp-commands.jsm
--- a/chat/protocols/xmpp/xmpp-commands.jsm
+++ b/chat/protocols/xmpp/xmpp-commands.jsm
@@ -46,16 +46,49 @@ function splitInput(aString) {
     splitParams.push(params.slice(0, offset));
     splitParams.push(params.slice(offset + 1).trimLeft());
   }
   else
     splitParams.push(params);
   return splitParams;
 }
 
+// Trims the string and splits it in two parts (The first part is a nickname
+// and the second part is the rest of string) based on nicknames of current
+// participants. Returns the non-empty parts in an array.
+function splitByNick(aString, aConv) {
+  let params = aString.trim();
+  if (!params)
+    return [];
+
+  // Match trimmed-string with the longest prefix of participant's nickname.
+  let nickName = "";
+  for (let participant of aConv._participants.keys()) {
+    if (params.startsWith(participant + " ") &&
+        participant.length > nickName.length)
+      nickName = participant;
+  }
+  if (!nickName) {
+    let offset = params.indexOf(" ");
+    let expectedNickName = offset != -1 ? params.slice(0, offset) : params;
+    aConv.writeMessage(aConv.name,
+                      _("conversation.error.nickNotInRoom", expectedNickName),
+                      {system: true});
+    return [];
+  }
+
+  let splitParams = [];
+  splitParams.push(nickName);
+
+  let msg = params.substring(nickName.length);
+  if (msg)
+    splitParams.push(msg.trimLeft());
+  return splitParams;
+}
+
 var commands = [
   {
     name: "join",
     get helpString() { return _("command.join3", "join"); },
     run: function(aMsg, aConv, aReturnedConv) {
       let account = getAccount(aConv);
       let params = aMsg.trim();
       let conv;
@@ -123,23 +156,24 @@ var commands = [
       return true;
     }
   },
   {
     name: "kick",
     get helpString() { return _("command.kick", "kick"); },
     usageContext: Ci.imICommand.CMD_CONTEXT_CHAT,
     run: function(aMsg, aConv) {
-      let params = splitInput(aMsg);
+      let conv = getMUC(aConv);
+      if (!conv)
+        return true;
+
+      let params = splitByNick(aMsg, conv);
       if (!params.length)
         return false;
-
-      let conv = getMUC(aConv);
-      if (conv)
-        conv.kick(...params);
+      conv.kick(...params);
       return true;
     }
   },
   {
     name: "invite",
     get helpString() { return _("command.invite", "invite"); },
     usageContext: Ci.imICommand.CMD_CONTEXT_CHAT,
     run: function(aMsg, aConv) {
@@ -197,31 +231,25 @@ var commands = [
       return true;
     }
   },
   {
     name: "msg",
     get helpString() { return _("command.msg", "msg"); },
     usageContext: Ci.imICommand.CMD_CONTEXT_CHAT,
     run: function(aMsg, aConv, aReturnedConv) {
-      let params = splitInput(aMsg);
+      let conv = getMUC(aConv);
+      if (!conv)
+        return true;
+
+      let params = splitByNick(aMsg, conv);
       if (params.length != 2)
         return false;
       let [nickName, msg] = params;
 
-      let conv = getMUC(aConv);
-      if (!conv)
-        return true;
-
-      if (!conv._participants.has(nickName)) {
-        conv.writeMessage(conv.name,
-                          _("conversation.error.nickNotInRoom", nickName),
-                          {system: true});
-        return true;
-      }
       let account = getAccount(aConv);
       let privateConv = account.createConversation(conv.name + "/" + nickName);
       if (!privateConv)
         return true;
       privateConv.sendMsg(msg.trim());
 
       if (aReturnedConv)
         aReturnedConv.value = privateConv;