From fb46f6d9c52d571512542fa6812ecfa6bcd104af Mon Sep 17 00:00:00 2001 From: bertybuttface <110790513+bertybuttface@users.noreply.github.com> Date: Mon, 6 Feb 2023 13:33:03 +0000 Subject: [PATCH] Add basic room ACL. --- src/env.ts | 4 ++++ src/handlers.ts | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/env.ts b/src/env.ts index 513588b..98c1641 100644 --- a/src/env.ts +++ b/src/env.ts @@ -23,6 +23,8 @@ export const { /** Matrix Access Control */ MATRIX_BLACKLIST, MATRIX_WHITELIST, + MATRIX_ROOM_BLACKLIST, + MATRIX_ROOM_WHITELIST, /** Matrix Bot Runtime Config */ MATRIX_DEFAULT_PREFIX, MATRIX_DEFAULT_PREFIX_REPLY, @@ -53,6 +55,8 @@ export const { /** Matrix Access Control */ MATRIX_BLACKLIST: { schema: z.string().optional(), description: "Set to a spaces separated string of 'user:homeserver' or a wildcard like ':anotherhomeserver.example' to blacklist users or domains" }, MATRIX_WHITELIST: { schema: z.string().optional(), description: "Set to a spaces separated string of 'user:homeserver' or a wildcard like ':anotherhomeserver.example' to whitelist users or domains" }, + MATRIX_ROOM_BLACKLIST: { schema: z.string().optional(), description: "Set to a spaces separated string of 'user:homeserver' or a wildcard like ':anotherhomeserver.example' to blacklist rooms or domains" }, + MATRIX_ROOM_WHITELIST: { schema: z.string().optional(), description: "Set to a spaces separated string of 'user:homeserver' or a wildcard like ':anotherhomeserver.example' to whitelist rooms or domains" }, /** Matrix Bot Runtime Config */ MATRIX_DEFAULT_PREFIX: { schema: z.string().default(""), description: "Set to a string if you want the bot to respond only when messages start with this prefix. Trailing space matters. Empty for no prefix." }, MATRIX_DEFAULT_PREFIX_REPLY: { schema: z.boolean().default(false), description: "Set to false if you want the bot to answer to all messages in a thread/conversation" }, diff --git a/src/handlers.ts b/src/handlers.ts index ee16543..d418e0e 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -1,6 +1,6 @@ import ChatGPTClient from '@waylaidwanderer/chatgpt-api'; import { LogService, MatrixClient, UserID } from "matrix-bot-sdk"; -import { CHATGPT_CONTEXT, CHATGPT_TIMEOUT, CHATGPT_IGNORE_MEDIA, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST, MATRIX_RICH_TEXT, MATRIX_PREFIX_DM, MATRIX_THREADS } from "./env.js"; +import { CHATGPT_CONTEXT, CHATGPT_TIMEOUT, CHATGPT_IGNORE_MEDIA, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST, MATRIX_RICH_TEXT, MATRIX_PREFIX_DM, MATRIX_THREADS, MATRIX_ROOM_BLACKLIST, MATRIX_ROOM_WHITELIST } from "./env.js"; import { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js"; import { sendChatGPTMessage, sendError, sendReply } from "./utils.js"; @@ -29,13 +29,15 @@ export default class CommandHandler { } } - private shouldIgnore(event: MessageEvent): boolean { - if (event.sender === this.userId) return true; // Ignore ourselves - if (MATRIX_BLACKLIST && MATRIX_BLACKLIST.split(" ").find(b => event.sender.endsWith(b))) return true; // Ignore if on blacklist if set - if (MATRIX_WHITELIST && !MATRIX_WHITELIST.split(" ").find(w => event.sender.endsWith(w))) return true; // Ignore if not on whitelist if set - if (Date.now() - event.origin_server_ts > 10000) return true; // Ignore old messages - if (event.content["m.relates_to"]?.["rel_type"] === "m.replace") return true; // Ignore edits - if (CHATGPT_IGNORE_MEDIA && event.content.msgtype !== "m.text") return true; // Ignore everything which is not text if set + private shouldIgnore(event: MessageEvent, roomId: string): boolean { + if (event.sender === this.userId) return true; // Ignore ourselves + if (MATRIX_BLACKLIST && MATRIX_BLACKLIST.split(" ").find(b => event.sender.endsWith(b))) return true; // Ignore if on blacklist if set + if (MATRIX_WHITELIST && !MATRIX_WHITELIST.split(" ").find(w => event.sender.endsWith(w))) return true; // Ignore if not on whitelist if set + if (MATRIX_ROOM_BLACKLIST && MATRIX_ROOM_BLACKLIST.split(" ").find(b => roomId.endsWith(b))) return true; // Ignore if on room blacklist if set + if (MATRIX_ROOM_WHITELIST && !MATRIX_ROOM_WHITELIST.split(" ").find(w => roomId.endsWith(w))) return true; // Ignore if not on room whitelist if set + if (Date.now() - event.origin_server_ts > 10000) return true; // Ignore old messages + if (event.content["m.relates_to"]?.["rel_type"] === "m.replace") return true; // Ignore edits + if (CHATGPT_IGNORE_MEDIA && event.content.msgtype !== "m.text") return true; // Ignore everything which is not text if set return false; } @@ -102,7 +104,7 @@ export default class CommandHandler { */ private async onMessage(roomId: string, event: MessageEvent) { try { - if (this.shouldIgnore(event)) return; + if (this.shouldIgnore(event, roomId)) return; const storageKey = this.getStorageKey(event, roomId); const storedConversation = await this.getStoredConversation(storageKey, roomId);