From 66c4c10a78c36401aa90c7cf9357edf96cd942ac Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sun, 10 Jan 2021 08:03:15 -0500
Subject: [PATCH] Console: Improve parsing of email address (part 2)

---
 .../web/helpers/ConfigClientsHelper.java      | 21 ++++++++++++-------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHelper.java
index 72e182d694..76fe0c4f56 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigClientsHelper.java
@@ -9,6 +9,8 @@ import java.util.Locale;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import net.i2p.app.ClientApp;
 import net.i2p.app.ClientAppState;
@@ -37,6 +39,15 @@ public class ConfigClientsHelper extends HelperBase {
     public static final String PROP_ENABLE_CLIENT_CHANGE = "routerconsole.enableClientChange";
     public static final String PROP_ENABLE_PLUGIN_INSTALL = "routerconsole.enablePluginInstall";
 
+    /**
+     * simple regex from
+     * https://stackoverflow.com/questions/8204680/java-regex-email
+     * not the massive RFC 822 compliant one
+     * modified so .i2p will work
+     */
+    private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
+        Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z2]{2,6}", Pattern.CASE_INSENSITIVE);
+
     public ConfigClientsHelper() {}
 
     /** @since 0.9.14.1 */
@@ -303,14 +314,8 @@ public class ConfigClientsHelper extends HelperBase {
                 s = stripHTML(appProps, "author");
                 if (s != null) {
                     String[] authors = DataHelper.split(s, "[,; \r\n\t]");
-                    String author = null;
-                    for (int i = 0; i < authors.length; i++) {
-                        String a = authors[i];
-                        if (a.indexOf('@') > 0 && a.indexOf('.') > 0) {
-                            author = a;
-                            break;
-                        }
-                    }
+                    Matcher m = VALID_EMAIL_ADDRESS_REGEX.matcher(s);
+                    String author = m.find() ? m.group() : null;
                     desc.append("<tr><td><b>")
                         .append(_t("Author")).append("</b></td><td>");
                     if (author != null)
-- 
GitLab