diff --git a/src/commands/UnbanBanCommand.ts b/src/commands/UnbanBanCommand.ts index 1231f68..831ee3d 100644 --- a/src/commands/UnbanBanCommand.ts +++ b/src/commands/UnbanBanCommand.ts @@ -70,14 +70,19 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni if (entity) break; } - if (!list) { - list = mjolnir.lists.find(b => b.listShortcode.toLowerCase() === defaultShortcode); + if (!entity) { + // It'll be a server at this point - figure out which positional argument is the server + // name and where the reason starts. + let serverIndex = 2; + if (ruleType) serverIndex++; + if (list) serverIndex++; + entity = parts[serverIndex]; + if (!ruleType) ruleType = RULE_SERVER; + argumentIndex = serverIndex + 1; } - if (!entity) { - entity = parts[argumentIndex - 1]; - if (!ruleType) ruleType = RULE_SERVER; // due to the conditions above, it can't be anything else - console.log(parts); + if (!list) { + list = mjolnir.lists.find(b => b.listShortcode.toLowerCase() === defaultShortcode); } let replyMessage = null; diff --git a/test/commands/UnbanBanCommandTest.ts b/test/commands/UnbanBanCommandTest.ts index 3d66f35..073ebe1 100644 --- a/test/commands/UnbanBanCommandTest.ts +++ b/test/commands/UnbanBanCommandTest.ts @@ -62,6 +62,23 @@ describe("UnbanBanCommand", () => { expect(bits.list.listShortcode).toBe("test"); }); + it("should be able to detect servers with ban reasons", async () => { + const mjolnir = createTestMjolnir(); + (mjolnir).lists = [{listShortcode: "test"}]; + mjolnir.client.sendMessage = (roomId: string, content: any): Promise => { + throw new Error("sendMessage should not have been called: " + JSON.stringify(content)); + }; + + const command = "!mjolnir ban test example.org reason here"; + const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' ')); + expect(bits).toBeTruthy(); + expect(bits.reason).toBe("reason here"); + expect(bits.ruleType).toBe(RULE_SERVER); + expect(bits.entity).toBe("example.org"); + expect(bits.list).toBeDefined(); + expect(bits.list.listShortcode).toBe("test"); + }); + it("should be able to detect servers with globs", async () => { const mjolnir = createTestMjolnir(); (mjolnir).lists = [{listShortcode: "test"}]; @@ -113,6 +130,23 @@ describe("UnbanBanCommand", () => { expect(bits.list.listShortcode).toBe("test"); }); + it("should be able to detect room IDs with ban reasons", async () => { + const mjolnir = createTestMjolnir(); + (mjolnir).lists = [{listShortcode: "test"}]; + mjolnir.client.sendMessage = (roomId: string, content: any): Promise => { + throw new Error("sendMessage should not have been called: " + JSON.stringify(content)); + }; + + const command = "!mjolnir ban test !example.org reason here"; + const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' ')); + expect(bits).toBeTruthy(); + expect(bits.reason).toBe("reason here"); + expect(bits.ruleType).toBe(RULE_ROOM); + expect(bits.entity).toBe("!example.org"); + expect(bits.list).toBeDefined(); + expect(bits.list.listShortcode).toBe("test"); + }); + it("should be able to detect room IDs with globs", async () => { const mjolnir = createTestMjolnir(); (mjolnir).lists = [{listShortcode: "test"}]; @@ -147,6 +181,23 @@ describe("UnbanBanCommand", () => { expect(bits.list.listShortcode).toBe("test"); }); + it("should be able to detect room aliases with ban reasons", async () => { + const mjolnir = createTestMjolnir(); + (mjolnir).lists = [{listShortcode: "test"}]; + mjolnir.client.sendMessage = (roomId: string, content: any): Promise => { + throw new Error("sendMessage should not have been called: " + JSON.stringify(content)); + }; + + const command = "!mjolnir ban test #example.org reason here"; + const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' ')); + expect(bits).toBeTruthy(); + expect(bits.reason).toBe("reason here"); + expect(bits.ruleType).toBe(RULE_ROOM); + expect(bits.entity).toBe("#example.org"); + expect(bits.list).toBeDefined(); + expect(bits.list.listShortcode).toBe("test"); + }); + it("should be able to detect room aliases with globs", async () => { const mjolnir = createTestMjolnir(); (mjolnir).lists = [{listShortcode: "test"}]; @@ -198,6 +249,23 @@ describe("UnbanBanCommand", () => { expect(bits.list.listShortcode).toBe("test"); }); + it("should be able to detect user IDs with ban reasons", async () => { + const mjolnir = createTestMjolnir(); + (mjolnir).lists = [{listShortcode: "test"}]; + mjolnir.client.sendMessage = (roomId: string, content: any): Promise => { + throw new Error("sendMessage should not have been called: " + JSON.stringify(content)); + }; + + const command = "!mjolnir ban test @example.org reason here"; + const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' ')); + expect(bits).toBeTruthy(); + expect(bits.reason).toBe("reason here"); + expect(bits.ruleType).toBe(RULE_USER); + expect(bits.entity).toBe("@example.org"); + expect(bits.list).toBeDefined(); + expect(bits.list.listShortcode).toBe("test"); + }); + it("should be able to detect user IDs with globs", async () => { const mjolnir = createTestMjolnir(); (mjolnir).lists = [{listShortcode: "test"}];