mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Add optional reporting of invites that won't be accepted
This commit is contained in:
parent
7314df7379
commit
ecc7674422
@ -29,6 +29,11 @@ autojoinOnlyIfManager: true
|
|||||||
# the bot to new rooms.
|
# the bot to new rooms.
|
||||||
acceptInvitesFromGroup: '+example:example.org'
|
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
|
# 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!
|
||||||
# This should be a room alias or room ID - not a matrix.to URL.
|
# This should be a room alias or room ID - not a matrix.to URL.
|
||||||
|
@ -28,6 +28,7 @@ interface IConfig {
|
|||||||
dataPath: string;
|
dataPath: string;
|
||||||
acceptInvitesFromGroup: string;
|
acceptInvitesFromGroup: string;
|
||||||
autojoinOnlyIfManager: boolean;
|
autojoinOnlyIfManager: boolean;
|
||||||
|
recordIgnoredInvites: boolean;
|
||||||
managementRoom: string;
|
managementRoom: string;
|
||||||
verboseLogging: boolean;
|
verboseLogging: boolean;
|
||||||
logLevel: "DEBUG" | "INFO" | "WARN" | "ERROR";
|
logLevel: "DEBUG" | "INFO" | "WARN" | "ERROR";
|
||||||
@ -68,6 +69,7 @@ const defaultConfig: IConfig = {
|
|||||||
dataPath: "/data/storage",
|
dataPath: "/data/storage",
|
||||||
acceptInvitesFromGroup: '+example:example.org',
|
acceptInvitesFromGroup: '+example:example.org',
|
||||||
autojoinOnlyIfManager: false,
|
autojoinOnlyIfManager: false,
|
||||||
|
recordIgnoredInvites: false,
|
||||||
managementRoom: "!noop:example.org",
|
managementRoom: "!noop:example.org",
|
||||||
verboseLogging: false,
|
verboseLogging: false,
|
||||||
logLevel: "INFO",
|
logLevel: "INFO",
|
||||||
|
19
src/index.ts
19
src/index.ts
@ -30,6 +30,7 @@ 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";
|
||||||
|
import * as htmlEscape from "escape-html";
|
||||||
|
|
||||||
config.RUNTIME = {client: null};
|
config.RUNTIME = {client: null};
|
||||||
|
|
||||||
@ -54,13 +55,27 @@ LogService.info("index", "Starting bot...");
|
|||||||
client.on("room.invite", async (roomId: string, inviteEvent: any) => {
|
client.on("room.invite", async (roomId: string, inviteEvent: any) => {
|
||||||
const membershipEvent = new MembershipEvent(inviteEvent);
|
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) {
|
if (config.autojoinOnlyIfManager) {
|
||||||
const managers = await client.getJoinedRoomMembers(config.managementRoom);
|
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 {
|
} else {
|
||||||
const groupMembers = await client.unstableApis.getGroupUsers(config.acceptInvitesFromGroup);
|
const groupMembers = await client.unstableApis.getGroupUsers(config.acceptInvitesFromGroup);
|
||||||
const userIds = groupMembers.map(m => m.user_id);
|
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);
|
return client.joinRoom(roomId);
|
||||||
|
Loading…
Reference in New Issue
Block a user