PSL refactoring

This commit is contained in:
M66B 2022-05-02 09:55:27 +02:00
parent 682acc879b
commit 65bf57094d
9 changed files with 70 additions and 48 deletions

View File

@ -4232,10 +4232,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
if (Boolean.FALSE.equals(message.from_domain) && message.smtp_from != null)
for (Address smtp_from : message.smtp_from) {
if (sb.length() > 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)) {

View File

@ -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");
}

View File

@ -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;

View File

@ -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) {

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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};

View File

@ -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);
synchronized (suffixList) {
int d = host.indexOf('.');
String w = (d < 0 ? null : '*' + host.substring(d));
return (!suffixList.contains('!' + host) &&
!suffixList.contains(w) &&
suffixList.contains(host));
}
if (host == null)
return false;
String tld = getTld(context, host);
return (tld != null && tld.equals(host));
}
private static String _getSuffix(Context context, @NonNull String host) {
static boolean hasTld(Context context, String host) {
return (getTld(context, host) != null);
}
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('.');
int d = eval.indexOf('.');
String w = (d < 0 ? null : '*' + eval.substring(d));
synchronized (suffixList) {
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;
String prefix = h.substring(0, dot);
h = h.substring(dot + 1);
int d = h.indexOf('.');
String w = (d < 0 ? null : '*' + h.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;
}
}
eval = eval.substring(dot + 1);
}
}