Add MATRIX_BLACKLIST and MATRIX_WHITELIST

This commit is contained in:
bertybuttface 2023-01-07 21:58:30 +00:00
parent aaa527bed3
commit c7b9cbf962
3 changed files with 18 additions and 2 deletions

View File

@ -54,6 +54,11 @@ MATRIX_BOT_PASSWORD=
MATRIX_DEFAULT_PREFIX=!chatgpt
MATRIX_DEFAULT_PREFIX_REPLY=false
# Matrix Access Control (optional)
# Can be set to user:homeserver or a wildcard like :anotherhomeserver.example
MATRIX_BLACKLIST=
MATRIX_WHITELIST=
# Matrix Feature Flags (optional)
MATRIX_AUTOJOIN=true
MATRIX_ENCRYPTION=true

View File

@ -15,6 +15,9 @@ export const {
MATRIX_ENCRYPTION,
MATRIX_THREADS,
MATRIX_PREFIX_DM,
/** Matrix Access Control */
MATRIX_BLACKLIST,
MATRIX_WHITELIST,
/** Matrix Bot Runtime Config */
MATRIX_DEFAULT_PREFIX,
MATRIX_DEFAULT_PREFIX_REPLY,
@ -35,6 +38,9 @@ export const {
MATRIX_ENCRYPTION: {schema: z.boolean().default(true)},
MATRIX_THREADS: {schema: z.boolean().default(true)},
MATRIX_PREFIX_DM: {schema: z.boolean().default(false)},
/** Matrix Access Control */
MATRIX_BLACKLIST: {schema: z.string().optional()},
MATRIX_WHITELIST: {schema: z.string().optional()},
/** Matrix Bot Runtime Config */
MATRIX_DEFAULT_PREFIX: {schema: z.string().default(""), description: "Set this to empty string if you don't want to use it. Trailing space matters."},
MATRIX_DEFAULT_PREFIX_REPLY:{schema: z.boolean().default(false)},

View File

@ -1,6 +1,6 @@
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk";
import { CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX} from "./env.js";
import { CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST} from "./env.js";
import { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js";
import { sendError, sendThreadReply } from "./utils.js";
@ -40,7 +40,12 @@ export default class CommandHandler {
if (Date.now() - event.origin_server_ts > 10000) return; // Ignore old messages
const relatesTo: RelatesTo | undefined = event.content["m.relates_to"];
if ((relatesTo !== undefined) && (relatesTo["rel_type"] === "m.replace")) return; // Ignore edits
if (MATRIX_BLACKLIST !== undefined){
if (MATRIX_BLACKLIST.split(" ").find(b => event.sender.endsWith(b))) return; // Ignore if on blacklist if set
}
if (MATRIX_WHITELIST !== undefined){
if (!MATRIX_WHITELIST.split(" ").find(w => event.sender.endsWith(w))) return; // Ignore if not on whitelist if set
}
const rootEventId: string = (relatesTo !== undefined && relatesTo.event_id !== undefined) ? relatesTo.event_id : event.event_id;
const storedValue: string = await this.client.storageProvider.readValue('gpt-' + rootEventId)
const storedConversation: StoredConversation = (storedValue !== undefined) ? JSON.parse(storedValue) : undefined;