From a98c9988e3e1cf94fff48a37fbd4f0b3fb0de715 Mon Sep 17 00:00:00 2001 From: xenofem Date: Sat, 27 Mar 2021 03:50:17 -0400 Subject: [PATCH] apply rot13 to discord spoilers in unformatted matrix message body --- src/discordmessageparser.ts | 2 +- src/util.ts | 16 ++++++++++++++++ test/test_discordmessageparser.ts | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/discordmessageparser.ts b/src/discordmessageparser.ts index 9797add..3d6de29 100644 --- a/src/discordmessageparser.ts +++ b/src/discordmessageparser.ts @@ -274,7 +274,7 @@ export class DiscordMessageParser { // matrix spoilers are still in MSC stage // see https://github.com/matrix-org/matrix-doc/pull/2010 if (!html) { - return `(Spoiler: ${node.content})`; + return `(Spoiler (rot13): ${Util.Rot13(node.content)})`; } return `${node.content}`; } diff --git a/src/util.ts b/src/util.ts index d869b1c..b11c291 100644 --- a/src/util.ts +++ b/src/util.ts @@ -30,6 +30,22 @@ export class Util { return htmlColor; } + public static Rot13(plaintext: string): string { + const UPPER_Z = 90; + const LOWER_Z = 122; + const CAESAR_SHIFT = 13; + const ALPHABET_LENGTH = 26; + return plaintext.replace(/[a-zA-Z]/g, (c) => { + const code = c.charCodeAt(0); + let shiftedCode = code + CAESAR_SHIFT; + if ((code <= UPPER_Z && shiftedCode > UPPER_Z) + || (code <= LOWER_Z && shiftedCode > LOWER_Z)) { + shiftedCode = shiftedCode - ALPHABET_LENGTH; + } + return String.fromCharCode(shiftedCode); + }); + } + public static async AsyncForEach(arr, callback) { for (let i = 0; i < arr.length; i++) { await callback(arr[i], i, arr); diff --git a/test/test_discordmessageparser.ts b/test/test_discordmessageparser.ts index 3265d66..35927a8 100644 --- a/test/test_discordmessageparser.ts +++ b/test/test_discordmessageparser.ts @@ -350,7 +350,7 @@ describe("DiscordMessageParser", () => { const mp = new DiscordMessageParser(); const msg = getMessage("||foxies||"); const result = await mp.FormatMessage(defaultOpts, msg); - expect(result.body).is.equal("(Spoiler: foxies)"); + expect(result.body).is.equal("(Spoiler (rot13): sbkvrf)"); expect(result.formattedBody).is.equal("foxies"); }); it("processes unknown emoji correctly", async () => {