Add an option to only autojoin invites from bot managers

This commit is contained in:
Travis Ralston 2020-01-21 13:53:02 -07:00
parent 38e22ee155
commit 97d02b3816
3 changed files with 18 additions and 1 deletions

View File

@ -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.

View File

@ -27,6 +27,7 @@ interface IConfig {
};
dataPath: string;
autojoin: boolean;
autojoinOnlyIfManager: boolean;
managementRoom: string;
verboseLogging: boolean;
logLevel: "DEBUG" | "INFO" | "WARN" | "ERROR";

View File

@ -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[] = [];