diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 99523dab8..d399e53af 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -41,6 +41,7 @@ import { Snowflake, uploadFile, User, + stringGlobToRegexp, } from "@spacebar/util"; import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; @@ -453,8 +454,8 @@ router.post( if (rule.trigger_type == AutomodTriggerTypes.CUSTOM_WORDS) { const triggerMeta = rule.trigger_metadata as AutomodCustomWordsRule; - const regexes = triggerMeta.regex_patterns.map((x) => new RegExp(x, "i")).concat(triggerMeta.keyword_filter.map((k) => k.globToRegexp("i"))); - const allowedRegexes = triggerMeta.allow_list.map((k) => k.globToRegexp("i")); + const regexes = triggerMeta.regex_patterns.map((x) => new RegExp(x, "i")).concat(triggerMeta.keyword_filter.map((k) => stringGlobToRegexp(k, "i"))); + const allowedRegexes = triggerMeta.allow_list.map((k) => stringGlobToRegexp(k, "i")); const matches = regexes .map((r) => message.content!.match(r)) diff --git a/src/util/util/String.ts b/src/util/util/String.ts index 2d2e132a7..f79e73f20 100644 --- a/src/util/util/String.ts +++ b/src/util/util/String.ts @@ -37,4 +37,10 @@ export function centerString(str: string, len: number): string { const pad = len - str.length; const padLeft = Math.floor(pad / 2) + str.length; return str.padStart(padLeft).padEnd(len); -} \ No newline at end of file +} + +export function stringGlobToRegexp(str: string, flags?: string): RegExp { + // Convert simple wildcard patterns to regex + const escaped = str.replace(".", "\\.").replace("?", ".").replace("*", ".*"); + return new RegExp(escaped, flags); +} diff --git a/src/util/util/extensions/String.test.ts b/src/util/util/extensions/String.test.ts deleted file mode 100644 index d5cc9292e..000000000 --- a/src/util/util/extensions/String.test.ts +++ /dev/null @@ -1,15 +0,0 @@ -import moduleAlias from "module-alias"; -moduleAlias(); -import './String'; -import { describe, it } from 'node:test'; -import assert from 'node:assert/strict'; - -describe("String extensions", () => { - - it("globToRegexp", () => { - const pattern = "file-*.txt"; - const regex = pattern.globToRegexp(); - assert.ok(regex.test("file-123.txt")); - }); - -}); \ No newline at end of file diff --git a/src/util/util/extensions/String.ts b/src/util/util/extensions/String.ts deleted file mode 100644 index ce9b1a513..000000000 --- a/src/util/util/extensions/String.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2025 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -declare global { - interface String { - globToRegexp(flags?: string): RegExp; - } -} - -export function stringGlobToRegexp(str: string, flags?: string): RegExp { - // Convert simple wildcard patterns to regex - const escaped = str.replace(".", "\\.") - .replace("?", ".") - .replace("*", ".*") - return new RegExp(escaped, flags); -} - -// Register extensions -if (!String.prototype.globToRegexp) - String.prototype.globToRegexp = function (str: string, flags?: string) { - return stringGlobToRegexp.call(null, str, flags); - }; \ No newline at end of file diff --git a/src/util/util/extensions/index.ts b/src/util/util/extensions/index.ts index b7dcf4d53..e1946bb38 100644 --- a/src/util/util/extensions/index.ts +++ b/src/util/util/extensions/index.ts @@ -1,2 +1 @@ export * from "./Array"; -export * from "./String";