From 3a567edc38de803f878303d71521b2e3e341e05a Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 27 Sep 2019 14:36:23 -0600 Subject: [PATCH] Add a config option to pick the protected rooms --- config/default.yaml | 5 +++++ src/config.ts | 1 + src/index.ts | 26 +++++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 40aa582..829eab5 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -12,8 +12,13 @@ autojoin: true # The room ID where people can use the bot. The bot has no access controls, so # anyone in this room can use the bot - secure your room! +# This should be a room alias or room ID - not a matrix.to URL. managementRoom: "#moderators:example.org" +# A list of rooms to protect (matrix.to URLs) +protectedRooms: + - "https://matrix.to/#/#yourroom:example.org" + # A list of ban lists to follow (matrix.to URLs) banLists: - "https://matrix.to/#/#sample-ban-list:t2bot.io" # S.A.M.P.L.E. diff --git a/src/config.ts b/src/config.ts index 8dd4fde..40abe3a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,6 +22,7 @@ interface IConfig { dataPath: string; autojoin: boolean; managementRoom: string; + protectedRooms: string[]; // matrix.to urls banLists: string[]; // matrix.to urls } diff --git a/src/index.ts b/src/index.ts index 70f14ca..b09f442 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ const storage = new SimpleFsStorageProvider(path.join(config.dataPath, "bot.json const client = new MatrixClient(config.homeserverUrl, config.accessToken, storage); const lists: BanList[] = []; let managementRoomId = ""; +const protectedRooms: { [roomId: string]: string } = {}; if (config.autojoin) { AutojoinRoomsMixin.setupOnClient(client); @@ -48,7 +49,7 @@ client.on("room.event", async (roomId, event) => { await list.updateList(); } - const errors = await applyServerAcls(lists, await client.getJoinedRooms(), client); + const errors = await applyServerAcls(lists, Object.keys(protectedRooms), client); return printActionResult(errors); } else if (event['type'] === "m.room.member") { // TODO: Check membership against ban lists @@ -56,6 +57,7 @@ client.on("room.event", async (roomId, event) => { }); client.on("room.message", async (roomId, event) => { + if (roomId !== managementRoomId) return; if (!event['content']) return; const content = event['content']; @@ -83,6 +85,19 @@ client.on("room.message", async (roomId, event) => { lists.push(list); } + // Ensure we're also joined to the rooms we're protecting + for (const roomRef of config.protectedRooms) { + const permalink = Permalinks.parseUrl(roomRef); + if (!permalink.roomIdOrAlias) continue; + + let roomId = await client.resolveRoom(permalink.roomIdOrAlias); + if (!joinedRooms.includes(roomId)) { + roomId = await client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers); + } + + protectedRooms[roomId] = roomRef; + } + // Ensure we're also in the management room managementRoomId = await client.joinRoom(config.managementRoom); await client.sendNotice(managementRoomId, "Mjolnir is starting up. Use !mjolnir to query status."); @@ -103,7 +118,7 @@ async function printStatus(roomId: string) { // Append header information first html += "Running:
"; text += "Running: ✅\n"; - html += `Protected rooms: ${rooms.length}
`; + html += `Protected rooms: ${Object.keys(protectedRooms).length}
`; text += `Protected rooms: ${rooms.length}\n`; // Append list information @@ -133,12 +148,13 @@ async function printActionResult(errors: RoomUpdateError[]) { html += `${errors.length} errors updating protected rooms!
"; } else { - html += `Updated all protected rooms with new rules successfully.`; + html += `Updated all protected rooms with new rules successfully.`; text += "Updated all protected rooms with new rules successfully"; }