From 97d02b3816efd8172f78d8d0fb49b813586fa974 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 21 Jan 2020 13:53:02 -0700 Subject: [PATCH] Add an option to only autojoin invites from bot managers --- config/default.yaml | 6 ++++++ src/config.ts | 1 + src/index.ts | 12 +++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/config/default.yaml b/config/default.yaml index 3f71520..c451cea 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -24,6 +24,12 @@ dataPath: "/data/storage" # Whether the bot should autojoin rooms it is invited to or not autojoin: true +# If `autojoin` is true, this defines whether anyone can invite the bot (the +# default), or only those in the `managementRoom` below. Generally this option +# should be set to true (only allow people in the management room to do invites) +# when using autojoin. +autojoinOnlyIfManager: false + # 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. diff --git a/src/config.ts b/src/config.ts index e09f971..aaa5a5a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -27,6 +27,7 @@ interface IConfig { }; dataPath: string; autojoin: boolean; + autojoinOnlyIfManager: boolean; managementRoom: string; verboseLogging: boolean; logLevel: "DEBUG" | "INFO" | "WARN" | "ERROR"; diff --git a/src/index.ts b/src/index.ts index 2557735..38bd4a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,7 @@ import config from "./config"; import BanList from "./models/BanList"; import { Mjolnir } from "./Mjolnir"; import { logMessage } from "./LogProxy"; +import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent"; config.RUNTIME = {client: null}; @@ -51,7 +52,16 @@ LogService.info("index", "Starting bot..."); config.RUNTIME.client = client; if (config.autojoin) { - AutojoinRoomsMixin.setupOnClient(client); + if (config.autojoinOnlyIfManager) { + client.on("room.invite", async (roomId: string, inviteEvent: any) => { + const membershipEvent = new MembershipEvent(inviteEvent); + const managers = await client.getJoinedRoomMembers(config.managementRoom); + if (!managers.includes(membershipEvent.sender)) return; // ignore invite + return client.joinRoom(roomId); + }); + } else { + AutojoinRoomsMixin.setupOnClient(client); + } } const banLists: BanList[] = [];