From f25dfda09c75c128e824b37770754be572bd9afa Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Fri, 9 Jun 2023 22:06:31 +0200
Subject: [PATCH] Properly clean discord webhooks username

---
 src/usersyncroniser.ts |  2 +-
 src/util.ts            | 17 ++---------------
 tsconfig.json          |  2 +-
 3 files changed, 4 insertions(+), 17 deletions(-)

diff --git a/src/usersyncroniser.ts b/src/usersyncroniser.ts
index b82722d..51ac3fc 100644
--- a/src/usersyncroniser.ts
+++ b/src/usersyncroniser.ts
@@ -317,7 +317,7 @@ export class UserSyncroniser {
             // for webhooks we append the username to the mxid, as webhooks with the same
             // id can have multiple different usernames set. This way we don't spam
             // userstate changes
-            mxidExtra = "_" + Util.ParseMxid(`@${user.username}`, false).localpart;
+            mxidExtra = "_" + Util.ParseMxid(`@${user.username}`).localpart;
         }
         const guildState: IGuildMemberState = Object.assign({}, DEFAULT_GUILD_STATE, {
             bot: user.bot,
diff --git a/src/util.ts b/src/util.ts
index f09190e..b5345c8 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -365,10 +365,7 @@ export class Util {
             const badChars = new Set(localpart.replace(/([a-z0-9]|-|\.|=|_)+/g, ""));
             badChars.forEach((c) => {
                 const hex = c.charCodeAt(0).toString(RADIX).toLowerCase();
-                localpart = localpart.replace(
-                    new RegExp(`\\${c}`, "g"),
-                    `=${hex}`,
-                );
+		localpart = localpart.replaceAll(c, `=${hex}`);
             });
         }
         return {
@@ -380,17 +377,7 @@ export class Util {
 
     // Taken from https://github.com/matrix-org/matrix-appservice-bridge/blob/master/lib/models/users/matrix.js
     public static EscapeStringForUserId(localpart: string) {
-        // NOTE: Currently Matrix accepts / in the userId, although going forward it will be removed.
-        const badChars = new Set(localpart.replace(/([a-z]|[0-9]|-|\.|=|_)+/g, ""));
-        let res = localpart;
-        badChars.forEach((c) => {
-            const hex = c.charCodeAt(0).toString(16).toLowerCase();
-            res = res.replace(
-                new RegExp(`\\x${hex}`, "g"),
-                `=${hex}`,
-            );
-        });
-        return res;
+	return localpart.replace(/[^a-z0-9-._]/g, a => `=${a.codePointAt(0)!.toString(16).toLowerCase()}`)
     }
 }
 
diff --git a/tsconfig.json b/tsconfig.json
index d8273e2..b2d7054 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -2,7 +2,7 @@
     "compilerOptions": {
         "module": "commonjs",
         "moduleResolution": "node",
-        "target": "es2016",
+        "target": "es2021",
         "noImplicitAny": false,
         "inlineSourceMap": true,
         "outDir": "./build",
-- 
GitLab