From 4d16296d4c77ed2cf2c4527ac1a266b0e436c086 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Sun, 13 Oct 2024 09:53:17 +0800 Subject: [PATCH 1/4] add regex functionality to Mjolnir's WordList feature. --- src/config.ts | 2 ++ src/protections/WordList.ts | 26 ++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index 38057a10..72596e38 100644 --- a/src/config.ts +++ b/src/config.ts @@ -120,6 +120,7 @@ export interface IConfig { }; protections: { wordlist: { + enableRegexps: boolean; words: string[]; minutesBeforeTrusting: number; }; @@ -228,6 +229,7 @@ const defaultConfig: IConfig = { }, protections: { wordlist: { + enableRegexps: false, words: [], minutesBeforeTrusting: 20, }, diff --git a/src/protections/WordList.ts b/src/protections/WordList.ts index 1d54e887..243abd1e 100644 --- a/src/protections/WordList.ts +++ b/src/protections/WordList.ts @@ -88,15 +88,9 @@ export class WordList extends Protection { } } if (!this.badWords) { - // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping - const escapeRegExp = (string: string) => { - return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - }; - - // Create a mega-regex from all the tiny words. - const words = mjolnir.config.protections.wordlist.words - .filter((word) => word.length !== 0) - .map(escapeRegExp); + const enableRegexps = mjolnir.config.protections.wordlist.enableRegexps || false; + const words = mjolnir.config.protections.wordlist.words.filter((word) => word.length !== 0); + if (words.length === 0) { mjolnir.managementRoomOutput.logMessage( LogLevel.ERROR, @@ -106,7 +100,19 @@ export class WordList extends Protection { this.enabled = false; return; } - this.badWords = new RegExp(words.join("|"), "i"); + + if (enableRegexps) { + this.badWords = new RegExp(words.join("|"), "i"); + } else { + // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping + const escapeRegExp = (string: string) => { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + }; + + // Create a mega-regex from all the tiny words. + const escapedWords = words.map(escapeRegExp); + this.badWords = new RegExp(escapedWords.join("|"), "i"); + } } const match = this.badWords!.exec(message); From a9875d575a59c7fb4f238ae5b0cf224985a54a10 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Sun, 13 Oct 2024 13:24:49 +0800 Subject: [PATCH 2/4] enableRegExps > enableRegexps --- src/config.ts | 4 ++-- src/protections/WordList.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config.ts b/src/config.ts index 72596e38..9d7b26e5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -120,7 +120,7 @@ export interface IConfig { }; protections: { wordlist: { - enableRegexps: boolean; + enableRegExps: boolean; words: string[]; minutesBeforeTrusting: number; }; @@ -229,7 +229,7 @@ const defaultConfig: IConfig = { }, protections: { wordlist: { - enableRegexps: false, + enableRegExps: false, words: [], minutesBeforeTrusting: 20, }, diff --git a/src/protections/WordList.ts b/src/protections/WordList.ts index 243abd1e..aca1617c 100644 --- a/src/protections/WordList.ts +++ b/src/protections/WordList.ts @@ -88,7 +88,7 @@ export class WordList extends Protection { } } if (!this.badWords) { - const enableRegexps = mjolnir.config.protections.wordlist.enableRegexps || false; + const enableRegExps = mjolnir.config.protections.wordlist.enableRegExps || false; const words = mjolnir.config.protections.wordlist.words.filter((word) => word.length !== 0); if (words.length === 0) { @@ -101,7 +101,7 @@ export class WordList extends Protection { return; } - if (enableRegexps) { + if (enableRegExps) { this.badWords = new RegExp(words.join("|"), "i"); } else { // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping From 310f50959733fa4e66b7e48bfa6fda74c70b29fc Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Sun, 13 Oct 2024 13:30:21 +0800 Subject: [PATCH 3/4] add comments and an example variable about regexp support to the default.yaml config. --- config/default.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config/default.yaml b/config/default.yaml index e8a81643..14637e2e 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -170,6 +170,14 @@ protections: # Configuration for the wordlist plugin, which can ban users based if they say certain # blocked words shortly after joining. wordlist: + # For advanced usage, regex can also be enabled. If left disabled Mjolnir will interpret words literally. + # + # See the following links for more information; + # - https://www.digitalocean.com/community/tutorials/an-introduction-to-regular-expressions + # - https://regexr.com/ + # - https://regexone.com/ + enableRegExps: false + # A list of case-insensitive keywords that the WordList protection will watch for from new users. # # WordList will ban users who use these words when first joining a room, so take caution when selecting them. From bd351e4e002bae1bbe38bae7cb3eb8524fd71f0d Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Sun, 13 Oct 2024 13:36:19 +0800 Subject: [PATCH 4/4] add an example for blocking t.me links. --- config/default.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/default.yaml b/config/default.yaml index 14637e2e..8201a389 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -172,10 +172,11 @@ protections: wordlist: # For advanced usage, regex can also be enabled. If left disabled Mjolnir will interpret words literally. # + # For example, if enabled this 'word' would match all t.me links: (https?:\/\/)?t\.me\/(\+)?[a-zA-Z0-9_-]+ + # # See the following links for more information; # - https://www.digitalocean.com/community/tutorials/an-introduction-to-regular-expressions # - https://regexr.com/ - # - https://regexone.com/ enableRegExps: false # A list of case-insensitive keywords that the WordList protection will watch for from new users.