From 5815157190a27e7ef499f3436b1a3687abf0b485 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 21 Oct 2020 22:33:15 +0300 Subject: [PATCH] Refuse to send replies with an unknown inline snippet This behavior can be disabled by setting the following option: errorOnUnknownInlineSnippet = off --- CHANGELOG.md | 1 + docs/configuration.md | 5 +++++ src/data/Thread.js | 10 ++++++++++ src/data/cfg.jsdoc.js | 1 + src/data/cfg.schema.json | 5 +++++ 5 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9eb77..ee90434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Please report any bugs you encounter by [creating a GitHub issue](https://github * E.g. `!r Hello! {{rules}}` * The symbols used can be changed with the `inlineSnippetStart` and `inlineSnippetEnd` options * This feature can be disabled by setting `allowInlineSnippets = off` in your config + * By default, the bot will refuse to send a reply with an unknown inline snippet. To disable this behavior, set `errorOnUnknownInlineSnippet = off`. * Plugins can now also be installed from NPM modules * Example: `plugins[] = npm:some-plugin-package` * Fix occasional bug with expiring blocks where the bot would send the expiry message multiple times diff --git a/docs/configuration.md b/docs/configuration.md index 800771c..5d46811 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -168,6 +168,11 @@ commandAliases.x = close **Default:** `off` When enabled, the bot will send a greeting DM to users that join the main server. +#### errorOnUnknownInlineSnippet +**Default:** `on` +When enabled, the bot will refuse to send any reply with an unknown inline snippet. +See [allowInlineSnippets](#allowInlineSnippets) for more details. + #### greetingAttachment **Default:** *None* Path to an image or other attachment to send as a greeting. Requires `enableGreeting` to be enabled. diff --git a/src/data/Thread.js b/src/data/Thread.js index 75ebfc9..fd09a63 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -206,14 +206,24 @@ class Thread { return _map; }, {}); + let unknownSnippets = new Set(); text = text.replace( new RegExp(`${config.inlineSnippetStart}(\\s*\\S+?\\s*)${config.inlineSnippetEnd}`, "i"), (orig, trigger) => { trigger = trigger.trim(); const snippet = snippetMap[trigger.toLowerCase()]; + if (snippet == null) { + unknownSnippets.add(trigger); + } + return snippet != null ? snippet.body : orig; } ); + + if (config.errorOnUnknownInlineSnippet && unknownSnippets.size > 0) { + this.postSystemMessage(`The following snippets used in the reply do not exist:\n${Array.from(unknownSnippets).join(", ")}`); + return false; + } } // Prepare attachments, if any diff --git a/src/data/cfg.jsdoc.js b/src/data/cfg.jsdoc.js index 1acae72..c23e3c1 100644 --- a/src/data/cfg.jsdoc.js +++ b/src/data/cfg.jsdoc.js @@ -59,6 +59,7 @@ * @property {boolean} [allowInlineSnippets=true] * @property {string} [inlineSnippetStart="{{"] * @property {string} [inlineSnippetEnd="}}"] + * @property {boolean} [errorOnUnknownInlineSnippet=true] * @property {string} [logStorage="local"] * @property {object} [logOptions] * @property {string} logOptions.attachmentDirectory diff --git a/src/data/cfg.schema.json b/src/data/cfg.schema.json index d042dab..8c42ab0 100644 --- a/src/data/cfg.schema.json +++ b/src/data/cfg.schema.json @@ -331,6 +331,11 @@ "default": "}}" }, + "errorOnUnknownInlineSnippet": { + "$ref": "#/definitions/customBoolean", + "default": true + }, + "logStorage": { "type": "string", "default": "local"