diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 45195bbc6c..9c9f78629f 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -4232,10 +4232,13 @@ public class AdapterMessage extends RecyclerView.Adapter 0) - sb.append('\n'); String domain = UriHelper.getEmailDomain(((InternetAddress) smtp_from).getAddress()); - sb.append(context.getString(R.string.title_via, UriHelper.getParentDomain(context, domain))); + String root = UriHelper.getRootDomain(context, domain); + if (root != null) { + if (sb.length() > 0) + sb.append('\n'); + sb.append(context.getString(R.string.title_via, root)); + } } if (Boolean.FALSE.equals(message.reply_domain)) { diff --git a/app/src/main/java/eu/faircode/email/Bimi.java b/app/src/main/java/eu/faircode/email/Bimi.java index a6abfc2ac5..88ed15d688 100644 --- a/app/src/main/java/eu/faircode/email/Bimi.java +++ b/app/src/main/java/eu/faircode/email/Bimi.java @@ -92,7 +92,7 @@ public class Bimi { DnsHelper.DnsRecord record = lookupBimi(context, selector, domain); if (record == null) { String parent = UriHelper.getParentDomain(context, domain); - if (domain.equals(parent)) + if (parent == null) return null; domain = parent; record = lookupBimi(context, selector, domain); @@ -328,7 +328,7 @@ public class Bimi { DnsHelper.DnsRecord[] records = DnsHelper.lookup(context, txt, "txt"); if (records.length == 0) { String parent = UriHelper.getParentDomain(context, domain); - if (!domain.equals(parent)) { + if (parent != null) { txt = "_dmarc." + parent; records = DnsHelper.lookup(context, txt, "txt"); } diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index 5a7df9ebd6..e982889e63 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -413,9 +413,9 @@ public class EmailProvider implements Parcelable { break; } - String mxparent = UriHelper.getParentDomain(context, target); - String pdomain = UriHelper.getParentDomain(context, provider.imap.host); - if (mxparent.equalsIgnoreCase(pdomain)) { + String mxroot = UriHelper.getRootDomain(context, target); + String proot = UriHelper.getRootDomain(context, provider.imap.host); + if (mxroot != null && mxroot.equalsIgnoreCase(proot)) { EntityLog.log(context, "From MX host=" + provider.imap.host); candidates.add(provider); break; diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index 1de21ddaf3..4d1c7c6580 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -337,11 +337,11 @@ public class EntityMessage implements Serializable { } String[] checkFromDomain(Context context) { - return MessageHelper.equalDomain(context, from, smtp_from); + return MessageHelper.equalRootDomain(context, from, smtp_from); } String[] checkReplyDomain(Context context) { - return MessageHelper.equalDomain(context, reply, from); + return MessageHelper.equalRootDomain(context, reply, from); } static String getSubject(Context context, String language, String subject, boolean forward) { diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index beaac620d8..204509225f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -1167,8 +1167,8 @@ public class FragmentAccount extends FragmentBase { db.beginTransaction(); if (account != null && !account.password.equals(password)) { - String domain = UriHelper.getParentDomain(context, account.host); - String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain); + String root = UriHelper.getRootDomain(context, account.host); + String match = (root == null || root.equals(account.host) ? account.host : "%." + root); int count = db.identity().setIdentityPassword(account.id, account.user, password, auth, match); Log.i("Updated passwords=" + count + " match=" + match); } diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java index ee90d65e27..f5255bf53a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java @@ -119,7 +119,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { String t = title.replaceAll("\\s+", ""); Uri u = Uri.parse(title.contains("://") ? t : "http://" + t); String host = u.getHost(); // Capture1.PNG - uriTitle = (UriHelper.hasParentDomain(context, host) ? u : null); + uriTitle = (UriHelper.hasTld(context, host) ? u : null); } else uriTitle = null; diff --git a/app/src/main/java/eu/faircode/email/FragmentPop.java b/app/src/main/java/eu/faircode/email/FragmentPop.java index 05c673f980..53ea2a5507 100644 --- a/app/src/main/java/eu/faircode/email/FragmentPop.java +++ b/app/src/main/java/eu/faircode/email/FragmentPop.java @@ -524,8 +524,8 @@ public class FragmentPop extends FragmentBase { db.beginTransaction(); if (account != null && !account.password.equals(password)) { - String domain = UriHelper.getParentDomain(context, account.host); - String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain); + String root = UriHelper.getRootDomain(context, account.host); + String match = (root == null || root.equals(account.host) ? account.host : "%." + root); int count = db.identity().setIdentityPassword(account.id, account.user, password, auth, match); Log.i("Updated passwords=" + count + " match=" + match); } diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 3eaa2724ae..8f7d005b15 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -4440,7 +4440,7 @@ public class MessageHelper { return true; } - static String[] equalDomain(Context context, Address[] a1, Address[] a2) { + static String[] equalRootDomain(Context context, Address[] a1, Address[] a2) { if (a1 == null || a1.length == 0) return null; if (a2 == null || a2.length == 0) @@ -4450,13 +4450,17 @@ public class MessageHelper { String r = UriHelper.getEmailDomain(((InternetAddress) _a1).getAddress()); if (r == null) continue; - String d1 = UriHelper.getParentDomain(context, r); + String d1 = UriHelper.getRootDomain(context, r); + if (d1 == null) + continue; for (Address _a2 : a2) { String f = UriHelper.getEmailDomain(((InternetAddress) _a2).getAddress()); if (f == null) continue; - String d2 = UriHelper.getParentDomain(context, f); + String d2 = UriHelper.getRootDomain(context, f); + if (d2 == null) + continue; if (!d1.equalsIgnoreCase(d2)) return new String[]{d2, d1}; diff --git a/app/src/main/java/eu/faircode/email/UriHelper.java b/app/src/main/java/eu/faircode/email/UriHelper.java index 08b878ce74..b1abe0f334 100644 --- a/app/src/main/java/eu/faircode/email/UriHelper.java +++ b/app/src/main/java/eu/faircode/email/UriHelper.java @@ -86,49 +86,64 @@ public class UriHelper { static String getParentDomain(Context context, String host) { if (host == null) return null; - String parent = _getSuffix(context, host); - return (parent == null ? host : parent); + int dot = host.indexOf('.'); + if (dot < 0) + return null; + String parent = host.substring(dot + 1); + String tld = getTld(context, host); + if (tld == null || tld.equals(parent) || parent.length() < tld.length()) + return null; + return parent; } - static boolean hasParentDomain(Context context, String host) { - return (host != null && _getSuffix(context, host) != null); + static String getRootDomain(Context context, String host) { + if (host == null) + return null; + String tld = getTld(context, host); + if (tld == null) + return null; + if (tld.equals(host)) + return null; + int dot = host.substring(0, host.length() - tld.length() - 1).lastIndexOf('.'); + if (dot < 0) + return host; + return host.substring(dot + 1); } static boolean isTld(Context context, String host) { - ensureSuffixList(context); + if (host == null) + return false; + String tld = getTld(context, host); + return (tld != null && tld.equals(host)); + } - synchronized (suffixList) { - int d = host.indexOf('.'); - String w = (d < 0 ? null : '*' + host.substring(d)); - return (!suffixList.contains('!' + host) && - !suffixList.contains(w) && - suffixList.contains(host)); - } + static boolean hasTld(Context context, String host) { + return (getTld(context, host) != null); } - private static String _getSuffix(Context context, @NonNull String host) { + static String getTld(Context context, @NonNull String host) { ensureSuffixList(context); - String h = host.toLowerCase(Locale.ROOT); + String eval = host.toLowerCase(Locale.ROOT); while (true) { - int dot = h.indexOf('.'); - if (dot < 0) - return null; - - String prefix = h.substring(0, dot); - h = h.substring(dot + 1); - - int d = h.indexOf('.'); - String w = (d < 0 ? null : '*' + h.substring(d)); + int d = eval.indexOf('.'); + String w = (d < 0 ? null : '*' + eval.substring(d)); synchronized (suffixList) { - if (!suffixList.contains('!' + h) && - (suffixList.contains(h) || suffixList.contains(w))) { - String parent = prefix + "." + h; - Log.d("Host=" + host + " parent=" + parent); - return parent; - } + if (suffixList.contains(eval)) + return eval; + if (suffixList.contains(w)) + if (suffixList.contains('!' + eval)) + return eval.substring(d + 1); + else + return eval; } + + int dot = eval.indexOf('.'); + if (dot < 0) + return null; + + eval = eval.substring(dot + 1); } }