diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index 122e2ba6..cf59fc80 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -58,7 +58,7 @@ export class Mjolnir { */ private unlistedUserRedactionQueue = new UnlistedUserRedactionQueue(); - private protectedRoomsConfig: ProtectedRoomsConfig; + public readonly protectedRoomsConfig: ProtectedRoomsConfig; public readonly protectedRoomsTracker: ProtectedRoomsSet; private webapis: WebAPIs; private openMetrics: OpenMetrics; diff --git a/src/commands/ShutdownRoomCommand.ts b/src/commands/ShutdownRoomCommand.ts index 7e7b0ebf..880275db 100644 --- a/src/commands/ShutdownRoomCommand.ts +++ b/src/commands/ShutdownRoomCommand.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import { RichReply } from "@vector-im/matrix-bot-sdk"; +import { LogLevel, RichReply } from "@vector-im/matrix-bot-sdk"; // !mjolnir shutdown room [] export async function execShutdownRoomCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { @@ -31,6 +31,21 @@ export async function execShutdownRoomCommand(roomId: string, event: any, mjolni return; } + let protectedRooms; + if (mjolnir.config.protectAllJoinedRooms) { + protectedRooms = await mjolnir.client.getJoinedRooms(); + } else { + protectedRooms = mjolnir.protectedRoomsConfig.getExplicitlyProtectedRooms(); + } + if (protectedRooms.includes(target)) { + await mjolnir.managementRoomOutput.logMessage( + LogLevel.INFO, + "ShutdownRoomCommand", + "You are attempting to shutdown a room that mjolnir currently protects, aborting.", + ); + return; + } + await mjolnir.shutdownSynapseRoom(await mjolnir.client.resolveRoom(target), reason); await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅"); } diff --git a/test/integration/commands/shutdownCommandTest.ts b/test/integration/commands/shutdownCommandTest.ts index 7c79234f..cdb661f7 100644 --- a/test/integration/commands/shutdownCommandTest.ts +++ b/test/integration/commands/shutdownCommandTest.ts @@ -1,9 +1,11 @@ import { strict as assert } from "assert"; import { newTestUser } from "../clientHelper"; +import { getFirstReaction } from "./commandUtils"; +import { MatrixClient } from "@vector-im/matrix-bot-sdk"; describe("Test: shutdown command", function () { - let client; + let client: MatrixClient; this.beforeEach(async function () { client = await newTestUser(this.config.homeserverUrl, { name: { contains: "shutdown-command" } }); await client.start(); @@ -34,7 +36,7 @@ describe("Test: shutdown command", function () { }); const reply2 = new Promise((resolve, reject) => { - this.mjolnir.client.on("room.event", (roomId, event) => { + this.mjolnir.client.on("room.event", (roomId: string, event: any) => { if ( roomId !== this.mjolnir.managementRoomId && roomId !== badRoom && @@ -54,4 +56,42 @@ describe("Test: shutdown command", function () { return e.message.endsWith('{"errcode":"M_UNKNOWN","error":"This room has been blocked on this server"}'); }); }); + it("Mjolnir will not shutdown a room it is protecting.", async function () { + this.timeout(20000); + const targetRoom = await client.createRoom({ preset: "public_chat" }); + await client.joinRoom(this.mjolnir.managementRoomId); + const otherUser = await newTestUser(this.config.homeserverUrl, { + name: { contains: "shutdown-command-extra" }, + }); + + await getFirstReaction(client, this.mjolnir.managementRoomId, "✅", async () => { + return await client.sendMessage(this.mjolnir.managementRoomId, { + msgtype: "m.text", + body: `!mjolnir rooms add ${targetRoom}`, + }); + }); + + await client.sendMessage(this.mjolnir.managementRoomId, { + msgtype: "m.text", + body: `!mjolnir shutdown room ${targetRoom}`, + }); + + let reply = new Promise((resolve, reject) => { + client.on("room.message", (roomId: string, event: any) => { + console.log(JSON.stringify(event)); + if ( + roomId === this.mjolnir.managementRoomId && + event.content?.body.includes( + "You are attempting to shutdown a room that mjolnir currently protects, aborting", + ) + ) { + resolve(event); + } + }); + }); + await reply; + // room should not be shutdown and available to join + const joined = await otherUser.joinRoom(targetRoom); + await otherUser.sendMessage(joined, { msgtype: "m.text", body: "it's fine to interact with this room" }); + }); });