Re-work invite settings to better support practical use cases

This commit is contained in:
Travis Ralston 2020-03-05 15:29:30 -07:00
parent f5763803d9
commit 7314df7379
3 changed files with 20 additions and 20 deletions

View File

@ -21,14 +21,13 @@ pantalaimon:
# The directory the bot should store various bits of information in # The directory the bot should store various bits of information in
dataPath: "/data/storage" dataPath: "/data/storage"
# Whether the bot should autojoin rooms it is invited to or not # If true (the default), only users in the `managementRoom` can invite the bot
autojoin: true # to new rooms.
autojoinOnlyIfManager: true
# If `autojoin` is true, this defines whether anyone can invite the bot (the # If `autojoinOnlyIfManager` is false, only the members in this group can invite
# default), or only those in the `managementRoom` below. Generally this option # the bot to new rooms.
# should be set to true (only allow people in the management room to do invites) acceptInvitesFromGroup: '+example:example.org'
# when using autojoin.
autojoinOnlyIfManager: false
# The room ID where people can use the bot. The bot has no access controls, so # 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! # anyone in this room can use the bot - secure your room!

View File

@ -26,7 +26,7 @@ interface IConfig {
password: string; password: string;
}; };
dataPath: string; dataPath: string;
autojoin: boolean; acceptInvitesFromGroup: string;
autojoinOnlyIfManager: boolean; autojoinOnlyIfManager: boolean;
managementRoom: string; managementRoom: string;
verboseLogging: boolean; verboseLogging: boolean;
@ -66,7 +66,7 @@ const defaultConfig: IConfig = {
password: "", password: "",
}, },
dataPath: "/data/storage", dataPath: "/data/storage",
autojoin: false, acceptInvitesFromGroup: '+example:example.org',
autojoinOnlyIfManager: false, autojoinOnlyIfManager: false,
managementRoom: "!noop:example.org", managementRoom: "!noop:example.org",
verboseLogging: false, verboseLogging: false,

View File

@ -16,7 +16,6 @@ limitations under the License.
import * as path from "path"; import * as path from "path";
import { import {
AutojoinRoomsMixin,
LogLevel, LogLevel,
LogService, LogService,
MatrixClient, MatrixClient,
@ -30,7 +29,7 @@ import BanList from "./models/BanList";
import { Mjolnir } from "./Mjolnir"; import { Mjolnir } from "./Mjolnir";
import { logMessage } from "./LogProxy"; import { logMessage } from "./LogProxy";
import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent"; import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent";
import {BanListServer} from "./server/BanListServer"; import { BanListServer } from "./server/BanListServer";
config.RUNTIME = {client: null}; config.RUNTIME = {client: null};
@ -52,18 +51,20 @@ LogService.info("index", "Starting bot...");
config.RUNTIME.client = client; config.RUNTIME.client = client;
if (config.autojoin) { client.on("room.invite", async (roomId: string, inviteEvent: any) => {
const membershipEvent = new MembershipEvent(inviteEvent);
if (config.autojoinOnlyIfManager) { if (config.autojoinOnlyIfManager) {
client.on("room.invite", async (roomId: string, inviteEvent: any) => { const managers = await client.getJoinedRoomMembers(config.managementRoom);
const membershipEvent = new MembershipEvent(inviteEvent); if (!managers.includes(membershipEvent.sender)) return; // ignore invite
const managers = await client.getJoinedRoomMembers(config.managementRoom);
if (!managers.includes(membershipEvent.sender)) return; // ignore invite
return client.joinRoom(roomId);
});
} else { } 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 banLists: BanList[] = [];
const protectedRooms: { [roomId: string]: string } = {}; const protectedRooms: { [roomId: string]: string } = {};