From b45305a5e495b95ecbc5fc59c576dfce4b935f06 Mon Sep 17 00:00:00 2001 From: martgil <46025304+martgil@users.noreply.github.com> Date: Thu, 12 Jun 2025 10:24:31 +0800 Subject: [PATCH] refactor: Simplify User ID parsing --- src/packet/userid.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/packet/userid.js b/src/packet/userid.js index 4a092d8c..8d90ea08 100644 --- a/src/packet/userid.js +++ b/src/packet/userid.js @@ -87,15 +87,36 @@ class UserIDPacket { * The `name` and `comment` parts can include any letters, whitespace, and symbols, except for `(` and `)`, * since they interfere with `comment` parsing. */ - const re = /^(?[^()]+\s+)?(?\([^()]+\)\s+)?(?<\S+@\S+>)$/; - const matches = re.exec(userID); - if (matches !== null) { - const { name, comment, email } = matches.groups; - this.comment = comment?.replace(/^\(|\)|\s$/g, '').trim() || ''; // remove parenthesis and separating whiltespace - this.name = name?.trim() || ''; - this.email = email.substring(1, email.length - 1); // remove brackets - } else if (/^[^\s@]+@[^\s@]+$/.test(userID)) { // unbracketed email: enforce single @ and no whitespace - this.email = userID; + + const isValidEmail = str => /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(str); + const firstBracket = userID.indexOf('<'); + const lastBracket = userID.lastIndexOf('>'); + if ( + firstBracket !== -1 && + lastBracket !== -1 && + lastBracket > firstBracket + ) { + const potentialEmail = userID.substring(firstBracket + 1, lastBracket); + if (isValidEmail(potentialEmail)) { + this.email = potentialEmail; + const beforeEmail = userID.substring(0, firstBracket).trim(); + const firstParen = beforeEmail.indexOf('('); + const lastParen = beforeEmail.lastIndexOf(')'); + if (firstParen !== -1 && lastParen !== -1 && lastParen > firstParen) { + this.comment = beforeEmail + .substring(firstParen + 1, lastParen) + .trim(); + this.name = beforeEmail.substring(0, firstParen).trim(); + } else { + this.name = beforeEmail; + this.comment = ''; + } + } + } else if (isValidEmail(userID.trim())) { + // unbracketed email case + this.email = userID.trim(); + this.name = ''; + this.comment = ''; } this.userID = userID;