aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
index dc0c722b9..47b827677 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
@@ -2,6 +2,9 @@ package org.sufficientlysecure.keychain.pgp;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/** An abstract KeyRing.
*
* This is an abstract class for all KeyRing constructs. It serves as a common
@@ -19,6 +22,10 @@ public abstract class KeyRing {
abstract public String getPrimaryUserId() throws PgpGeneralException;
+ public String[] getSplitPrimaryUserId() throws PgpGeneralException {
+ return splitUserId(getPrimaryUserId());
+ }
+
abstract public boolean isRevoked() throws PgpGeneralException;
abstract public boolean canCertify() throws PgpGeneralException;
@@ -33,4 +40,39 @@ public abstract class KeyRing {
abstract public int getVerified() throws PgpGeneralException;
+ private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
+
+ /**
+ * Splits userId string into naming part, email part, and comment part
+ *
+ * @param userId
+ * @return array with naming (0), email (1), comment (2)
+ */
+ public static String[] splitUserId(String userId) {
+ String[] result = new String[]{null, null, null};
+
+ if (userId == null || userId.equals("")) {
+ return result;
+ }
+
+ /*
+ * User ID matching:
+ * http://fiddle.re/t4p6f
+ *
+ * test cases:
+ * "Max Mustermann (this is a comment) <max@example.com>"
+ * "Max Mustermann <max@example.com>"
+ * "Max Mustermann (this is a comment)"
+ * "Max Mustermann [this is nothing]"
+ */
+ Matcher matcher = USER_ID_PATTERN.matcher(userId);
+ if (matcher.matches()) {
+ result[0] = matcher.group(1);
+ result[1] = matcher.group(3);
+ result[2] = matcher.group(2);
+ }
+
+ return result;
+ }
+
}