Bug 1210226 - Handle nicknames that contain spaces for msg and kick commands in XMPP. r=aleth chat/ patch on CLOSED TREE

--HG--
extra : amend_source : 4dec0001a9098c9a5df5109f8ce0ee185a39bde4
This commit is contained in:
Abdelrhman Ahmed 2015-11-03 05:13:00 +01:00
Родитель 3165ce3846
Коммит c9796db169
1 изменённых файлов: 44 добавлений и 16 удалений

Просмотреть файл

@ -51,6 +51,39 @@ function splitInput(aString) {
return splitParams; 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 = [ var commands = [
{ {
name: "join", name: "join",
@ -128,13 +161,14 @@ var commands = [
get helpString() { return _("command.kick", "kick"); }, get helpString() { return _("command.kick", "kick"); },
usageContext: Ci.imICommand.CMD_CONTEXT_CHAT, usageContext: Ci.imICommand.CMD_CONTEXT_CHAT,
run: function(aMsg, aConv) { run: function(aMsg, aConv) {
let params = splitInput(aMsg); let conv = getMUC(aConv);
if (!conv)
return true;
let params = splitByNick(aMsg, conv);
if (!params.length) if (!params.length)
return false; return false;
conv.kick(...params);
let conv = getMUC(aConv);
if (conv)
conv.kick(...params);
return true; return true;
} }
}, },
@ -202,21 +236,15 @@ var commands = [
get helpString() { return _("command.msg", "msg"); }, get helpString() { return _("command.msg", "msg"); },
usageContext: Ci.imICommand.CMD_CONTEXT_CHAT, usageContext: Ci.imICommand.CMD_CONTEXT_CHAT,
run: function(aMsg, aConv, aReturnedConv) { run: function(aMsg, aConv, aReturnedConv) {
let params = splitInput(aMsg);
if (params.length != 2)
return false;
let [nickName, msg] = params;
let conv = getMUC(aConv); let conv = getMUC(aConv);
if (!conv) if (!conv)
return true; return true;
if (!conv._participants.has(nickName)) { let params = splitByNick(aMsg, conv);
conv.writeMessage(conv.name, if (params.length != 2)
_("conversation.error.nickNotInRoom", nickName), return false;
{system: true}); let [nickName, msg] = params;
return true;
}
let account = getAccount(aConv); let account = getAccount(aConv);
let privateConv = account.createConversation(conv.name + "/" + nickName); let privateConv = account.createConversation(conv.name + "/" + nickName);
if (!privateConv) if (!privateConv)