diff --git a/config/default.yaml b/config/default.yaml index 17598d0..eca8f2d 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -21,14 +21,13 @@ pantalaimon: # The directory the bot should store various bits of information in dataPath: "/data/storage" -# Whether the bot should autojoin rooms it is invited to or not -autojoin: true +# If true (the default), only users in the `managementRoom` can invite the bot +# to new rooms. +autojoinOnlyIfManager: 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 +# If `autojoinOnlyIfManager` is false, only the members in this group can invite +# the bot to new rooms. +acceptInvitesFromGroup: '+example:example.org' # 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! diff --git a/src/config.ts b/src/config.ts index 768af67..b1a5668 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,7 +26,7 @@ interface IConfig { password: string; }; dataPath: string; - autojoin: boolean; + acceptInvitesFromGroup: string; autojoinOnlyIfManager: boolean; managementRoom: string; verboseLogging: boolean; @@ -66,7 +66,7 @@ const defaultConfig: IConfig = { password: "", }, dataPath: "/data/storage", - autojoin: false, + acceptInvitesFromGroup: '+example:example.org', autojoinOnlyIfManager: false, managementRoom: "!noop:example.org", verboseLogging: false, diff --git a/src/index.ts b/src/index.ts index 087c1ea..389cee3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,6 @@ limitations under the License. import * as path from "path"; import { - AutojoinRoomsMixin, LogLevel, LogService, MatrixClient, @@ -30,7 +29,7 @@ import BanList from "./models/BanList"; import { Mjolnir } from "./Mjolnir"; import { logMessage } from "./LogProxy"; import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent"; -import {BanListServer} from "./server/BanListServer"; +import { BanListServer } from "./server/BanListServer"; config.RUNTIME = {client: null}; @@ -52,18 +51,20 @@ LogService.info("index", "Starting bot..."); config.RUNTIME.client = client; - if (config.autojoin) { + client.on("room.invite", async (roomId: string, inviteEvent: any) => { + const membershipEvent = new MembershipEvent(inviteEvent); + 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); - }); + const managers = await client.getJoinedRoomMembers(config.managementRoom); + if (!managers.includes(membershipEvent.sender)) return; // ignore invite } else { - AutojoinRoomsMixin.setupOnClient(client); + const groupMembers = await client.unstableApis.getGroupUsers(config.acceptInvitesFromGroup); + const userIds = groupMembers.map(m => m.user_id); + if (!userIds.includes(membershipEvent.sender)) return; // ignore invite } - } + + return client.joinRoom(roomId); + }); const banLists: BanList[] = []; const protectedRooms: { [roomId: string]: string } = {};