diff --git a/config/default.yaml b/config/default.yaml index e8a81643..8201a389 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -170,6 +170,15 @@ 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. + # + # 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/ + 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. diff --git a/src/config.ts b/src/config.ts index 38057a10..9d7b26e5 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..aca1617c 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);