From ef2172f7fb8a8a149f62ca99752e04c04862a8a5 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 29 Dec 2018 17:23:27 +0000 Subject: [PATCH] Get user name type --- .../java/eu/faircode/email/FragmentSetup.java | 6 ++-- .../main/java/eu/faircode/email/Provider.java | 36 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index f85392af0d..bbe32f658b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -222,10 +222,10 @@ public class FragmentSetup extends FragmentEx { if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) throw new IllegalArgumentException(context.getString(R.string.title_email_invalid)); - String domain = email.split("@")[1]; - Provider provider = Provider.fromDomain(context, domain); + String[] dparts = email.split("@"); + Provider provider = Provider.fromDomain(context, dparts[1]); - String user = email; // TODO + String user = (provider.user == Provider.UserType.EMAIL ? email : dparts[0]); Character separator; long now = new Date().getTime(); diff --git a/app/src/main/java/eu/faircode/email/Provider.java b/app/src/main/java/eu/faircode/email/Provider.java index 0a9222a95a..7d5d3152bc 100644 --- a/app/src/main/java/eu/faircode/email/Provider.java +++ b/app/src/main/java/eu/faircode/email/Provider.java @@ -56,6 +56,9 @@ public class Provider { public String smtp_host; public int smtp_port; public boolean smtp_starttls; + public UserType user = UserType.EMAIL; + + enum UserType {LOCAL, EMAIL} private Provider() { } @@ -120,19 +123,16 @@ public class Provider { return result; } - static Provider fromDomain(Context context, String domain) throws IOException, XmlPullParserException { + static Provider fromDomain(Context context, String domain) throws IOException { try { - return Provider.fromDNS(context, domain); - } catch (TextParseException ex) { - Log.w(ex); - throw new UnknownHostException(domain); - } catch (UnknownHostException ex) { + return Provider.fromISPDB(domain); + } catch (Throwable ex) { Log.w(ex); - return Provider.fromConfig(context, domain); + return Provider.fromDNS(domain); } } - private static Provider fromConfig(Context context, String domain) throws IOException, XmlPullParserException { + private static Provider fromISPDB(String domain) throws IOException, XmlPullParserException { // https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat URL url = new URL("https://autoconfig.thunderbird.net/v1.1/" + domain); Log.i("Fetching " + url); @@ -203,11 +203,27 @@ public class Provider { provider.imap_starttls = true; else if (smtp) provider.smtp_starttls = true; - } + } else + Log.w("Unknown socket type=" + socket); } + continue; + } else if ("username".equals(xml.getName())) { + eventType = xml.next(); + if (eventType == XmlPullParser.TEXT) { + String username = xml.getText(); + Log.i("Username=" + username); + if ("%EMAILADDRESS%".equals(username)) + provider.user = UserType.EMAIL; + else if ("%EMAILLOCALPART%".equals(username)) + provider.user = UserType.LOCAL; + else + Log.w("Unknown username type=" + username); + } continue; + } + } else if (eventType == XmlPullParser.END_TAG) { if ("incomingServer".equals(xml.getName())) imap = false; @@ -225,7 +241,7 @@ public class Provider { return provider; } - private static Provider fromDNS(Context context, String domain) throws TextParseException, UnknownHostException { + private static Provider fromDNS(String domain) throws TextParseException, UnknownHostException { // https://tools.ietf.org/html/rfc6186 SRVRecord imap = lookup("_imaps._tcp." + domain); SRVRecord smtp = lookup("_submission._tcp." + domain);