Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit fb46f6d

Browse files
committed
Add basic room ACL.
1 parent c7a9556 commit fb46f6d

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const {
2323
/** Matrix Access Control */
2424
MATRIX_BLACKLIST,
2525
MATRIX_WHITELIST,
26+
MATRIX_ROOM_BLACKLIST,
27+
MATRIX_ROOM_WHITELIST,
2628
/** Matrix Bot Runtime Config */
2729
MATRIX_DEFAULT_PREFIX,
2830
MATRIX_DEFAULT_PREFIX_REPLY,
@@ -53,6 +55,8 @@ export const {
5355
/** Matrix Access Control */
5456
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" },
5557
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" },
58+
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" },
59+
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" },
5660
/** Matrix Bot Runtime Config */
5761
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." },
5862
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" },

src/handlers.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ChatGPTClient from '@waylaidwanderer/chatgpt-api';
22
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk";
3-
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";
3+
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";
44
import { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js";
55
import { sendChatGPTMessage, sendError, sendReply } from "./utils.js";
66

@@ -29,13 +29,15 @@ export default class CommandHandler {
2929
}
3030
}
3131

32-
private shouldIgnore(event: MessageEvent): boolean {
33-
if (event.sender === this.userId) return true; // Ignore ourselves
34-
if (MATRIX_BLACKLIST && MATRIX_BLACKLIST.split(" ").find(b => event.sender.endsWith(b))) return true; // Ignore if on blacklist if set
35-
if (MATRIX_WHITELIST && !MATRIX_WHITELIST.split(" ").find(w => event.sender.endsWith(w))) return true; // Ignore if not on whitelist if set
36-
if (Date.now() - event.origin_server_ts > 10000) return true; // Ignore old messages
37-
if (event.content["m.relates_to"]?.["rel_type"] === "m.replace") return true; // Ignore edits
38-
if (CHATGPT_IGNORE_MEDIA && event.content.msgtype !== "m.text") return true; // Ignore everything which is not text if set
32+
private shouldIgnore(event: MessageEvent, roomId: string): boolean {
33+
if (event.sender === this.userId) return true; // Ignore ourselves
34+
if (MATRIX_BLACKLIST && MATRIX_BLACKLIST.split(" ").find(b => event.sender.endsWith(b))) return true; // Ignore if on blacklist if set
35+
if (MATRIX_WHITELIST && !MATRIX_WHITELIST.split(" ").find(w => event.sender.endsWith(w))) return true; // Ignore if not on whitelist if set
36+
if (MATRIX_ROOM_BLACKLIST && MATRIX_ROOM_BLACKLIST.split(" ").find(b => roomId.endsWith(b))) return true; // Ignore if on room blacklist if set
37+
if (MATRIX_ROOM_WHITELIST && !MATRIX_ROOM_WHITELIST.split(" ").find(w => roomId.endsWith(w))) return true; // Ignore if not on room whitelist if set
38+
if (Date.now() - event.origin_server_ts > 10000) return true; // Ignore old messages
39+
if (event.content["m.relates_to"]?.["rel_type"] === "m.replace") return true; // Ignore edits
40+
if (CHATGPT_IGNORE_MEDIA && event.content.msgtype !== "m.text") return true; // Ignore everything which is not text if set
3941
return false;
4042
}
4143

@@ -102,7 +104,7 @@ export default class CommandHandler {
102104
*/
103105
private async onMessage(roomId: string, event: MessageEvent) {
104106
try {
105-
if (this.shouldIgnore(event)) return;
107+
if (this.shouldIgnore(event, roomId)) return;
106108

107109
const storageKey = this.getStorageKey(event, roomId);
108110
const storedConversation = await this.getStoredConversation(storageKey, roomId);

0 commit comments

Comments
 (0)