Add optional reporting of invites that won't be accepted

This commit is contained in:
Travis Ralston 2020-03-05 15:38:09 -07:00
parent 7314df7379
commit ecc7674422
3 changed files with 24 additions and 2 deletions

View File

@ -29,6 +29,11 @@ autojoinOnlyIfManager: true
# the bot to new rooms.
acceptInvitesFromGroup: '+example:example.org'
# If the bot is invited to a room and it won't accept the invite (due to the
# conditions above), report it to the management room. Defaults to disabled (no
# reporting).
recordIgnoredInvites: 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

@ -28,6 +28,7 @@ interface IConfig {
dataPath: string;
acceptInvitesFromGroup: string;
autojoinOnlyIfManager: boolean;
recordIgnoredInvites: boolean;
managementRoom: string;
verboseLogging: boolean;
logLevel: "DEBUG" | "INFO" | "WARN" | "ERROR";
@ -68,6 +69,7 @@ const defaultConfig: IConfig = {
dataPath: "/data/storage",
acceptInvitesFromGroup: '+example:example.org',
autojoinOnlyIfManager: false,
recordIgnoredInvites: false,
managementRoom: "!noop:example.org",
verboseLogging: false,
logLevel: "INFO",

View File

@ -30,6 +30,7 @@ import { Mjolnir } from "./Mjolnir";
import { logMessage } from "./LogProxy";
import { MembershipEvent } from "matrix-bot-sdk/lib/models/events/MembershipEvent";
import { BanListServer } from "./server/BanListServer";
import * as htmlEscape from "escape-html";
config.RUNTIME = {client: null};
@ -54,13 +55,27 @@ LogService.info("index", "Starting bot...");
client.on("room.invite", async (roomId: string, inviteEvent: any) => {
const membershipEvent = new MembershipEvent(inviteEvent);
const reportInvite = async () => {
if (!config.recordIgnoredInvites) return; // Nothing to do
await client.sendMessage(config.managementRoom, {
msgtype: "m.text",
body: `${membershipEvent.sender} has invited me to ${roomId} but the config prevents me from accepting the invitation. `
+ `If you would like this room protected, use "!mjolnir rooms add ${roomId}" so I can accept the invite.`,
format: "org.matrix.custom.html",
formatted_body: `${htmlEscape(membershipEvent.sender)} has invited me to ${htmlEscape(roomId)} but the config prevents me from `
+ `accepting the invitation. If you would like this room protected, use <code>!mjolnir rooms add ${htmlEscape(roomId)}</code> `
+ `so I can accept the invite.`,
});
};
if (config.autojoinOnlyIfManager) {
const managers = await client.getJoinedRoomMembers(config.managementRoom);
if (!managers.includes(membershipEvent.sender)) return; // ignore invite
if (!managers.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
} else {
const groupMembers = await client.unstableApis.getGroupUsers(config.acceptInvitesFromGroup);
const userIds = groupMembers.map(m => m.user_id);
if (!userIds.includes(membershipEvent.sender)) return; // ignore invite
if (!userIds.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
}
return client.joinRoom(roomId);