Add basic room ACL.

This commit is contained in:
bertybuttface 2023-02-06 13:33:03 +00:00
parent c7a9556e18
commit fb46f6d9c5
2 changed files with 15 additions and 9 deletions

View File

@ -23,6 +23,8 @@ export const {
/** Matrix Access Control */ /** Matrix Access Control */
MATRIX_BLACKLIST, MATRIX_BLACKLIST,
MATRIX_WHITELIST, MATRIX_WHITELIST,
MATRIX_ROOM_BLACKLIST,
MATRIX_ROOM_WHITELIST,
/** Matrix Bot Runtime Config */ /** Matrix Bot Runtime Config */
MATRIX_DEFAULT_PREFIX, MATRIX_DEFAULT_PREFIX,
MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX_REPLY,
@ -53,6 +55,8 @@ export const {
/** Matrix Access Control */ /** 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_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_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 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: { 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" }, 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" },

View File

@ -1,6 +1,6 @@
import ChatGPTClient from '@waylaidwanderer/chatgpt-api'; import ChatGPTClient from '@waylaidwanderer/chatgpt-api';
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk"; 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 { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js";
import { sendChatGPTMessage, sendError, sendReply } from "./utils.js"; import { sendChatGPTMessage, sendError, sendReply } from "./utils.js";
@ -29,13 +29,15 @@ export default class CommandHandler {
} }
} }
private shouldIgnore(event: MessageEvent): boolean { private shouldIgnore(event: MessageEvent, roomId: string): boolean {
if (event.sender === this.userId) return true; // Ignore ourselves 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_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_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 (MATRIX_ROOM_BLACKLIST && MATRIX_ROOM_BLACKLIST.split(" ").find(b => roomId.endsWith(b))) return true; // Ignore if on room blacklist if set
if (event.content["m.relates_to"]?.["rel_type"] === "m.replace") return true; // Ignore edits if (MATRIX_ROOM_WHITELIST && !MATRIX_ROOM_WHITELIST.split(" ").find(w => roomId.endsWith(w))) return true; // Ignore if not on room whitelist if set
if (CHATGPT_IGNORE_MEDIA && event.content.msgtype !== "m.text") return true; // Ignore everything which is not text 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; return false;
} }
@ -102,7 +104,7 @@ export default class CommandHandler {
*/ */
private async onMessage(roomId: string, event: MessageEvent) { private async onMessage(roomId: string, event: MessageEvent) {
try { try {
if (this.shouldIgnore(event)) return; if (this.shouldIgnore(event, roomId)) return;
const storageKey = this.getStorageKey(event, roomId); const storageKey = this.getStorageKey(event, roomId);
const storedConversation = await this.getStoredConversation(storageKey, roomId); const storedConversation = await this.getStoredConversation(storageKey, roomId);