From 4dacaa46f37ac5a0b46076cf68e193e7eb5f2b39 Mon Sep 17 00:00:00 2001 From: JeDaYoshi Date: Sat, 3 Jul 2021 03:50:22 +0000 Subject: [PATCH 1/5] Optimise modes based on ISUPPORT This will see the maximum allowed of modes that are allowed at once as sent in RPL_ISUPPORT and will send multiple batches while using /op, /voice, etc. This also fixes a minor issue where it would try sending an empty voice if it had an extra space on arguments (such as using '/voice ') --- src/plugins/inputs/mode.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/plugins/inputs/mode.js b/src/plugins/inputs/mode.js index 4598a566..a86f3e26 100644 --- a/src/plugins/inputs/mode.js +++ b/src/plugins/inputs/mode.js @@ -19,7 +19,9 @@ exports.input = function ({irc, nick}, chan, cmd, args) { return; } - if (args.length === 0) { + const target = args.filter((arg) => arg !== ""); + + if (target.length === 0) { chan.pushMessage( this, new Msg({ @@ -40,9 +42,13 @@ exports.input = function ({irc, nick}, chan, cmd, args) { devoice: "-v", }[cmd]; - args.forEach(function (target) { - irc.raw("MODE", chan.name, mode, target); - }); + const limit = parseInt(irc.network.supports("modes")) || 1; + + for (let i = 0; i < target.length; i += limit) { + const targets = target.slice(i, i + limit); + const amode = `${mode[0]}${mode[1].repeat(targets.length)}`; + irc.raw("MODE", chan.name, amode, ...targets); + } return; } From e0e12c196049e6609a5ddd40f0b47edd627a8a66 Mon Sep 17 00:00:00 2001 From: JeDaYoshi Date: Sat, 3 Jul 2021 21:20:28 +0000 Subject: [PATCH 2/5] Fix tests for mode shorthand commands --- src/plugins/inputs/mode.js | 2 +- test/commands/mode.js | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/plugins/inputs/mode.js b/src/plugins/inputs/mode.js index a86f3e26..9af07079 100644 --- a/src/plugins/inputs/mode.js +++ b/src/plugins/inputs/mode.js @@ -42,7 +42,7 @@ exports.input = function ({irc, nick}, chan, cmd, args) { devoice: "-v", }[cmd]; - const limit = parseInt(irc.network.supports("modes")) || 1; + const limit = parseInt(irc.network.supports("MODES")) || 1; for (let i = 0; i < target.length; i += limit) { const targets = target.slice(i, i + limit); diff --git a/test/commands/mode.js b/test/commands/mode.js index ab27a9a4..c85cafaf 100644 --- a/test/commands/mode.js +++ b/test/commands/mode.js @@ -20,6 +20,13 @@ describe("Commands", function () { lastCommand: null, nick: "xPaw", irc: { + network: { + supports(type) { + if (type.toUpperCase() === "MODES") { + return "4"; + } + }, + }, raw(...args) { testableNetwork.lastCommand = args.join(" "); }, @@ -82,9 +89,18 @@ describe("Commands", function () { 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"); + ModeCommand.input(testableNetwork, channel, "voice", ["xPaw", "Max-P"]); + expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +vv xPaw Max-P"); + + // since the limit for modes on tests is 4, it should send two commands + ModeCommand.input(testableNetwork, channel, "devoice", [ + "xPaw", + "Max-P", + "hey", + "idk", + "thelounge", + ]); + expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge"); }); }); }); From 23f6886cc1a3b1932c9f9102a8a94c0fb3b674c3 Mon Sep 17 00:00:00 2001 From: JeDaYoshi Date: Sun, 4 Jul 2021 01:01:45 +0000 Subject: [PATCH 3/5] Add test for ISUPPORT-less networks on /mode shorthands --- test/commands/mode.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/commands/mode.js b/test/commands/mode.js index c85cafaf..731dcc5e 100644 --- a/test/commands/mode.js +++ b/test/commands/mode.js @@ -33,6 +33,19 @@ describe("Commands", function () { }, }; + const testableNetworkNoSupports = Object.assign({}, testableNetwork, { + irc: { + network: { + supports() { + return null; + }, + }, + raw(...args) { + testableNetworkNoSupports.lastCommand = args.join(" "); + }, + }, + }); + it("should not mess with the given target", function () { const test = function (expected, args) { ModeCommand.input(testableNetwork, channel, "mode", Array.from(args)); @@ -88,7 +101,9 @@ describe("Commands", function () { ModeCommand.input(testableNetwork, channel, "devoice", ["xPaw"]); expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v xPaw"); + }); + it("should use ISUPPORT MODES on shorthand commands", function () { ModeCommand.input(testableNetwork, channel, "voice", ["xPaw", "Max-P"]); expect(testableNetwork.lastCommand).to.equal("MODE #thelounge +vv xPaw Max-P"); @@ -102,5 +117,13 @@ describe("Commands", function () { ]); expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge"); }); + + it("should fallback to default limit of 1 mode for shorthand commands", function () { + ModeCommand.input(testableNetworkNoSupports, channel, "voice", ["xPaw"]); + expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge +v xPaw"); + + ModeCommand.input(testableNetworkNoSupports, channel, "devoice", ["xPaw", "Max-P"]); + expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge -v Max-P"); + }); }); }); From d96704835a31407affefc4b1eed28302dc80b8a0 Mon Sep 17 00:00:00 2001 From: JeDaYoshi Date: Tue, 6 Jul 2021 15:48:01 +0000 Subject: [PATCH 4/5] Send all modes in case of no ISUPPORT --- src/plugins/inputs/mode.js | 2 +- test/commands/mode.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugins/inputs/mode.js b/src/plugins/inputs/mode.js index 9af07079..30f90531 100644 --- a/src/plugins/inputs/mode.js +++ b/src/plugins/inputs/mode.js @@ -42,7 +42,7 @@ exports.input = function ({irc, nick}, chan, cmd, args) { devoice: "-v", }[cmd]; - const limit = parseInt(irc.network.supports("MODES")) || 1; + const limit = parseInt(irc.network.supports("MODES")) || target.length; for (let i = 0; i < target.length; i += limit) { const targets = target.slice(i, i + limit); diff --git a/test/commands/mode.js b/test/commands/mode.js index 731dcc5e..a818135a 100644 --- a/test/commands/mode.js +++ b/test/commands/mode.js @@ -118,12 +118,14 @@ describe("Commands", function () { expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge"); }); - it("should fallback to default limit of 1 mode for shorthand commands", function () { + it("should fallback to all modes at once for shorthand commands", function () { ModeCommand.input(testableNetworkNoSupports, channel, "voice", ["xPaw"]); expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge +v xPaw"); ModeCommand.input(testableNetworkNoSupports, channel, "devoice", ["xPaw", "Max-P"]); - expect(testableNetworkNoSupports.lastCommand).to.equal("MODE #thelounge -v Max-P"); + expect(testableNetworkNoSupports.lastCommand).to.equal( + "MODE #thelounge -vv xPaw Max-P" + ); }); }); }); From 35fcacb7675fbb35551a4122adfe670ecca40870 Mon Sep 17 00:00:00 2001 From: JeDaYoshi Date: Tue, 6 Jul 2021 18:15:37 +0000 Subject: [PATCH 5/5] Add firstCommand and do further checks on mode tests --- test/commands/mode.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/commands/mode.js b/test/commands/mode.js index a818135a..a6ea9bad 100644 --- a/test/commands/mode.js +++ b/test/commands/mode.js @@ -17,6 +17,7 @@ describe("Commands", function () { }); const testableNetwork = { + firstCommand: null, lastCommand: null, nick: "xPaw", irc: { @@ -28,6 +29,7 @@ describe("Commands", function () { }, }, raw(...args) { + testableNetwork.firstCommand = testableNetwork.lastCommand; testableNetwork.lastCommand = args.join(" "); }, }, @@ -41,6 +43,7 @@ describe("Commands", function () { }, }, raw(...args) { + testableNetworkNoSupports.firstCommand = testableNetworkNoSupports.lastCommand; testableNetworkNoSupports.lastCommand = args.join(" "); }, }, @@ -115,6 +118,9 @@ describe("Commands", function () { "idk", "thelounge", ]); + expect(testableNetwork.firstCommand).to.equal( + "MODE #thelounge -vvvv xPaw Max-P hey idk" + ); expect(testableNetwork.lastCommand).to.equal("MODE #thelounge -v thelounge"); });