Highlight mail-do domain name, refactoring

pull/199/head
M66B 4 years ago
parent 9dc7795320
commit 3449815e68

@ -227,15 +227,6 @@ public class DnsHelper {
} }
} }
static String getParentDomain(String host) {
if (host != null) {
String[] h = host.split("\\.");
if (h.length >= 2)
return h[h.length - 2] + "." + h[h.length - 1];
}
return host;
}
private static String getDnsServer(Context context) { private static String getDnsServer(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) if (cm == null)

@ -315,14 +315,14 @@ public class EntityMessage implements Serializable {
int rat = (r == null ? -1 : r.indexOf('@')); int rat = (r == null ? -1 : r.indexOf('@'));
if (rat < 0) if (rat < 0)
continue; continue;
String rdomain = DnsHelper.getParentDomain(r.substring(rat + 1)); String rdomain = UriHelper.getParentDomain(r.substring(rat + 1));
for (Address _from : from) { for (Address _from : from) {
String f = ((InternetAddress) _from).getAddress(); String f = ((InternetAddress) _from).getAddress();
int fat = (f == null ? -1 : f.indexOf('@')); int fat = (f == null ? -1 : f.indexOf('@'));
if (fat < 0) if (fat < 0)
continue; continue;
String fdomain = DnsHelper.getParentDomain(f.substring(fat + 1)); String fdomain = UriHelper.getParentDomain(f.substring(fat + 1));
if (!rdomain.equalsIgnoreCase(fdomain)) if (!rdomain.equalsIgnoreCase(fdomain))
return context.getString(R.string.title_reply_domain, fdomain, rdomain); return context.getString(R.string.title_reply_domain, fdomain, rdomain);

@ -860,7 +860,7 @@ public class EntityRule {
int at = sender.indexOf('@'); int at = sender.indexOf('@');
if (at > 0) { if (at > 0) {
boolean whitelisted = false; boolean whitelisted = false;
String domain = DnsHelper.getParentDomain(sender.substring(at + 1)); String domain = UriHelper.getParentDomain(sender.substring(at + 1));
for (String d : whitelist) for (String d : whitelist)
if (domain.matches(d)) { if (domain.matches(d)) {
whitelisted = true; whitelisted = true;

@ -1077,7 +1077,7 @@ public class FragmentAccount extends FragmentBase {
db.beginTransaction(); db.beginTransaction();
if (account != null && !account.password.equals(password)) { if (account != null && !account.password.equals(password)) {
String domain = DnsHelper.getParentDomain(account.host); String domain = UriHelper.getParentDomain(account.host);
String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain); String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain);
int count = db.identity().setIdentityPassword(account.id, account.user, password, match); int count = db.identity().setIdentityPassword(account.id, account.user, password, match);
Log.i("Updated passwords=" + count + " match=" + match); Log.i("Updated passwords=" + count + " match=" + match);

@ -54,6 +54,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.net.MailTo;
import androidx.core.util.PatternsCompat; import androidx.core.util.PatternsCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -502,7 +503,15 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
private Spanned format(Uri uri, Context context) { private Spanned format(Uri uri, Context context) {
String text = uri.toString(); String text = uri.toString();
SpannableStringBuilder ssb = new SpannableStringBuilder(text); SpannableStringBuilder ssb = new SpannableStringBuilder(text);
try {
String host = uri.getHost(); String host = uri.getHost();
if (host == null && "mailto".equals(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString());
host = UriHelper.getEmailDomain(email.getTo());
}
if (host != null) { if (host != null) {
int textColorLink = Helper.resolveColor(context, android.R.attr.textColorLink); int textColorLink = Helper.resolveColor(context, android.R.attr.textColorLink);
int index = text.indexOf(host); int index = text.indexOf(host);
@ -510,6 +519,10 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
ssb.setSpan(new ForegroundColorSpan(textColorLink), ssb.setSpan(new ForegroundColorSpan(textColorLink),
index, index + host.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); index, index + host.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
} catch (Throwable ex) {
Log.e(ex);
}
return ssb; return ssb;
} }
} }

@ -488,7 +488,7 @@ public class FragmentPop extends FragmentBase {
db.beginTransaction(); db.beginTransaction();
if (account != null && !account.password.equals(password)) { if (account != null && !account.password.equals(password)) {
String domain = DnsHelper.getParentDomain(account.host); String domain = UriHelper.getParentDomain(account.host);
String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain); String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain);
int count = db.identity().setIdentityPassword(account.id, account.user, password, match); int count = db.identity().setIdentityPassword(account.id, account.user, password, match);
Log.i("Updated passwords=" + count + " match=" + match); Log.i("Updated passwords=" + count + " match=" + match);

@ -45,10 +45,9 @@ public class IPInfo {
static Pair<String, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException { static Pair<String, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException {
if ("mailto".equalsIgnoreCase(uri.getScheme())) { if ("mailto".equalsIgnoreCase(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString()); MailTo email = MailTo.parse(uri.toString());
String to = email.getTo(); String domain = UriHelper.getEmailDomain(email.getTo());
if (to == null || !to.contains("@")) if (domain == null)
throw new UnknownHostException(); throw new UnknownHostException();
String domain = to.substring(to.indexOf('@') + 1);
InetAddress address = DnsHelper.lookupMx(context, domain); InetAddress address = DnsHelper.lookupMx(context, domain);
if (address == null) if (address == null)
throw new UnknownHostException(); throw new UnknownHostException();

@ -0,0 +1,44 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/
public class UriHelper {
static String getParentDomain(String host) {
if (host == null)
return null;
String[] h = host.split("\\.");
if (h.length >= 2)
return h[h.length - 2] + "." + h[h.length - 1];
return host;
}
static String getEmailDomain(String address) {
if (address == null)
return null;
int at = address.indexOf('@');
if (at > 0)
return address.substring(at + 1);
return address;
}
}
Loading…
Cancel
Save