Merge pull request #679 from thelounge/xpaw/fix-mode-command
Fix /mode command to correctly assume target
This commit is contained in:
commit
04f23704b8
@ -2,30 +2,60 @@
|
|||||||
|
|
||||||
exports.commands = ["mode", "op", "voice", "deop", "devoice"];
|
exports.commands = ["mode", "op", "voice", "deop", "devoice"];
|
||||||
|
|
||||||
|
var Chan = require("../../models/chan");
|
||||||
|
var Msg = require("../../models/msg");
|
||||||
|
|
||||||
|
exports.commands = [
|
||||||
|
"mode",
|
||||||
|
"op",
|
||||||
|
"deop",
|
||||||
|
"hop",
|
||||||
|
"dehop",
|
||||||
|
"voice",
|
||||||
|
"devoice",
|
||||||
|
];
|
||||||
|
|
||||||
exports.input = function(network, chan, cmd, args) {
|
exports.input = function(network, chan, cmd, args) {
|
||||||
if (args.length === 0) {
|
if (cmd !== "mode") {
|
||||||
|
if (chan.type !== Chan.Type.CHANNEL) {
|
||||||
|
chan.pushMessage(this, new Msg({
|
||||||
|
type: Msg.Type.ERROR,
|
||||||
|
text: `${cmd} command can only be used in channels.`
|
||||||
|
}));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var mode;
|
if (args.length === 0) {
|
||||||
var user;
|
chan.pushMessage(this, new Msg({
|
||||||
if (cmd !== "mode") {
|
type: Msg.Type.ERROR,
|
||||||
user = args[0];
|
text: `Usage: /${cmd} <nick> [...nick]`
|
||||||
mode = {
|
}));
|
||||||
op: "+o",
|
|
||||||
voice: "+v",
|
return;
|
||||||
deop: "-o",
|
|
||||||
devoice: "-v"
|
|
||||||
}[cmd];
|
|
||||||
} else if (args.length === 1) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
mode = args[0];
|
|
||||||
user = args[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var irc = network.irc;
|
const mode = {
|
||||||
irc.raw("MODE", chan.name, mode, user);
|
op: "+o",
|
||||||
|
hop: "+h",
|
||||||
|
voice: "+v",
|
||||||
|
deop: "-o",
|
||||||
|
dehop: "-h",
|
||||||
|
devoice: "-v"
|
||||||
|
}[cmd];
|
||||||
|
|
||||||
return true;
|
args.forEach(function(target) {
|
||||||
|
network.irc.raw("MODE", chan.name, mode, target);
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length === 0 || args[0][0] === "+" || args[0][0] === "-") {
|
||||||
|
args.unshift(chan.type === Chan.Type.CHANNEL || chan.type === Chan.Type.QUERY ? chan.name : network.nick);
|
||||||
|
}
|
||||||
|
|
||||||
|
args.unshift("MODE");
|
||||||
|
|
||||||
|
network.irc.raw.apply(network.irc, args);
|
||||||
};
|
};
|
||||||
|
90
test/commands/mode.js
Normal file
90
test/commands/mode.js
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
|
||||||
|
var Chan = require("../../src/models/chan");
|
||||||
|
var ModeCommand = require("../../src/plugins/inputs/mode");
|
||||||
|
|
||||||
|
describe("Commands", function() {
|
||||||
|
describe("/mode", function() {
|
||||||
|
const channel = new Chan({
|
||||||
|
name: "#thelounge"
|
||||||
|
});
|
||||||
|
|
||||||
|
const lobby = new Chan({
|
||||||
|
name: "Network Lobby",
|
||||||
|
type: Chan.Type.LOBBY
|
||||||
|
});
|
||||||
|
|
||||||
|
const testableNetwork = {
|
||||||
|
lastCommand: null,
|
||||||
|
nick: "xPaw",
|
||||||
|
irc: {
|
||||||
|
raw: function() {
|
||||||
|
testableNetwork.lastCommand = Array.prototype.join.call(arguments, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
it("should not mess with the given target", function() {
|
||||||
|
const test = function(expected, args) {
|
||||||
|
ModeCommand.input(testableNetwork, channel, "mode", Array.from(args));
|
||||||
|
expect(testableNetwork.lastCommand).to.equal(expected);
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, lobby, "mode", Array.from(args));
|
||||||
|
expect(testableNetwork.lastCommand).to.equal(expected);
|
||||||
|
};
|
||||||
|
|
||||||
|
test("MODE xPaw +i", ["xPaw", "+i"]);
|
||||||
|
test("MODE xPaw -w", ["xPaw", "-w"]);
|
||||||
|
test("MODE #thelounge +o xPaw", ["#thelounge", "+o", "xPaw"]);
|
||||||
|
test("MODE #thelounge -v xPaw", ["#thelounge", "-v", "xPaw"]);
|
||||||
|
test("MODE #thelounge +o-o xPaw Max-P", ["#thelounge", "+o-o", "xPaw", "Max-P"]);
|
||||||
|
test("MODE #thelounge", ["#thelounge"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should assume target if none given", function() {
|
||||||
|
ModeCommand.input(testableNetwork, channel, "mode", []);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, lobby, "mode", []);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "mode", ["+b"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +b");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, lobby, "mode", ["+b"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE xPaw +b");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "mode", ["-o", "hey"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -o hey");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, lobby, "mode", ["-i", "idk"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE xPaw -i idk");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should support shorthand commands", function() {
|
||||||
|
ModeCommand.input(testableNetwork, channel, "op", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +o xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "deop", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -o xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "hop", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +h xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "dehop", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -h xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "voice", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +v xPaw");
|
||||||
|
|
||||||
|
ModeCommand.input(testableNetwork, channel, "devoice", ["xPaw"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v xPaw");
|
||||||
|
|
||||||
|
// Multiple arguments are supported, sent as separate commands
|
||||||
|
ModeCommand.input(testableNetwork, channel, "devoice", ["xPaw", "Max-P"]);
|
||||||
|
expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v Max-P");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user