Files
FairEmail/app/src/main/java/eu/faircode/email/EmailProvider.java

414 lines
17 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.content.Context;
import android.content.res.XmlResourceParser;
2018-12-29 15:04:22 +00:00
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
2018-08-02 13:33:06 +00:00
import org.xmlpull.v1.XmlPullParser;
2018-12-29 15:04:22 +00:00
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
2018-08-02 13:33:06 +00:00
2018-12-29 15:04:22 +00:00
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
2018-08-03 04:34:02 +00:00
import java.text.Collator;
2018-08-02 13:33:06 +00:00
import java.util.ArrayList;
2018-08-03 04:34:02 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-08-03 04:34:02 +00:00
import java.util.Locale;
2018-08-02 13:33:06 +00:00
2019-01-13 15:23:04 +00:00
public class EmailProvider {
2018-08-02 13:33:06 +00:00
public String name;
public int order;
2018-08-24 16:41:22 +00:00
public String link;
2018-08-27 14:31:45 +00:00
public String type;
public String prefix;
2018-08-02 13:33:06 +00:00
public String imap_host;
2018-11-09 14:48:21 +00:00
public boolean imap_starttls;
2018-08-02 13:33:06 +00:00
public int imap_port;
public String smtp_host;
public int smtp_port;
2018-11-09 14:48:21 +00:00
public boolean smtp_starttls;
2018-12-29 17:23:27 +00:00
public UserType user = UserType.EMAIL;
2018-12-29 18:39:02 +00:00
public StringBuilder documentation = null; // html
2018-12-29 17:23:27 +00:00
enum UserType {LOCAL, EMAIL}
2018-08-02 13:33:06 +00:00
2018-12-29 18:40:14 +00:00
private static final int TIMEOUT = 20 * 1000; // milliseconds
2019-01-13 15:23:04 +00:00
private EmailProvider() {
2018-08-02 13:33:06 +00:00
}
2019-01-13 15:23:04 +00:00
EmailProvider(String name) {
2018-08-02 13:33:06 +00:00
this.name = name;
}
2019-01-13 15:23:04 +00:00
static List<EmailProvider> loadProfiles(Context context) {
List<EmailProvider> result = null;
2018-08-02 13:33:06 +00:00
try {
2019-01-13 15:23:04 +00:00
EmailProvider provider = null;
2018-08-02 13:33:06 +00:00
XmlResourceParser xml = context.getResources().getXml(R.xml.providers);
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
2018-12-29 18:39:02 +00:00
String name = xml.getName();
if ("providers".equals(name))
2018-08-02 13:33:06 +00:00
result = new ArrayList<>();
2018-12-29 18:39:02 +00:00
else if ("provider".equals(name)) {
2019-01-13 15:23:04 +00:00
provider = new EmailProvider();
2018-08-02 13:33:06 +00:00
provider.name = xml.getAttributeValue(null, "name");
provider.order = xml.getAttributeIntValue(null, "order", Integer.MAX_VALUE);
2018-08-24 16:41:22 +00:00
provider.link = xml.getAttributeValue(null, "link");
2018-08-27 14:31:45 +00:00
provider.type = xml.getAttributeValue(null, "type");
provider.prefix = xml.getAttributeValue(null, "prefix");
2018-12-29 18:39:02 +00:00
} else if ("imap".equals(name)) {
2018-08-02 13:33:06 +00:00
provider.imap_host = xml.getAttributeValue(null, "host");
2018-08-08 06:55:47 +00:00
provider.imap_port = xml.getAttributeIntValue(null, "port", 0);
2018-11-09 14:48:21 +00:00
provider.imap_starttls = xml.getAttributeBooleanValue(null, "starttls", false);
2018-12-29 18:39:02 +00:00
} else if ("smtp".equals(name)) {
2018-08-02 13:33:06 +00:00
provider.smtp_host = xml.getAttributeValue(null, "host");
2018-08-08 06:55:47 +00:00
provider.smtp_port = xml.getAttributeIntValue(null, "port", 0);
2018-11-09 14:48:21 +00:00
provider.smtp_starttls = xml.getAttributeBooleanValue(null, "starttls", false);
2018-08-02 13:33:06 +00:00
} else
2018-12-29 18:39:02 +00:00
throw new IllegalAccessException(name);
2018-08-02 13:33:06 +00:00
} else if (eventType == XmlPullParser.END_TAG) {
if ("provider".equals(xml.getName())) {
result.add(provider);
provider = null;
}
}
eventType = xml.next();
}
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-08-02 13:33:06 +00:00
}
2018-08-03 04:34:02 +00:00
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
2019-01-13 15:23:04 +00:00
Collections.sort(result, new Comparator<EmailProvider>() {
2018-08-03 04:34:02 +00:00
@Override
2019-01-13 15:23:04 +00:00
public int compare(EmailProvider p1, EmailProvider p2) {
int o = Integer.compare(p1.order, p2.order);
if (o == 0)
return collator.compare(p1.name, p2.name);
else
return o;
2018-08-03 04:34:02 +00:00
}
});
2018-08-02 13:33:06 +00:00
return result;
}
2019-01-13 15:23:04 +00:00
static EmailProvider fromDomain(Context context, String domain) throws IOException {
2018-12-29 15:04:22 +00:00
try {
2019-04-12 08:38:15 +02:00
Log.i("Provider from ISPDB domain=" + domain);
2019-04-12 08:28:23 +02:00
return addSpecials(context, fromISPDB(domain));
2018-12-29 17:23:27 +00:00
} catch (Throwable ex) {
2018-12-29 15:04:22 +00:00
Log.w(ex);
2018-12-29 18:44:18 +00:00
try {
2019-04-12 08:38:15 +02:00
Log.i("Provider from DNS domain=" + domain);
2019-04-12 08:28:23 +02:00
return addSpecials(context, fromDNS(domain));
2018-12-29 18:44:18 +00:00
} catch (UnknownHostException ex1) {
Log.w(ex1);
2019-04-12 08:38:15 +02:00
Log.i("Provider from template domain=" + domain);
return addSpecials(context, fromTemplate(domain));
2018-12-29 18:44:18 +00:00
}
2018-12-29 15:04:22 +00:00
}
}
2019-04-12 08:28:23 +02:00
private static EmailProvider fromISPDB(String domain) throws IOException, XmlPullParserException {
2019-01-13 15:23:04 +00:00
EmailProvider provider = new EmailProvider(domain);
2018-12-30 12:52:25 +00:00
2018-12-29 15:04:22 +00:00
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
URL url = new URL("https://autoconfig.thunderbird.net/v1.1/" + domain);
Log.i("Fetching " + url);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
2018-12-29 18:40:14 +00:00
request.setReadTimeout(TIMEOUT);
request.setConnectTimeout(TIMEOUT);
2018-12-29 15:04:22 +00:00
request.setRequestMethod("GET");
request.setDoInput(true);
request.connect();
// https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xml = factory.newPullParser();
xml.setInput(new InputStreamReader(request.getInputStream()));
boolean imap = false;
boolean smtp = false;
2018-12-29 18:39:02 +00:00
String href = null;
String title = null;
2018-12-29 15:04:22 +00:00
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
2018-12-29 18:39:02 +00:00
String name = xml.getName();
2018-12-30 15:50:22 +00:00
if ("displayShortName".equals(name)) {
// <displayShortName>GMail</displayShortName>
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
String display = xml.getText();
Log.i("Name=" + display);
provider.name = display;
}
continue;
} else if ("incomingServer".equals(name)) {
2018-12-29 18:39:02 +00:00
// <incomingServer type="imap">
// <hostname>imap.gmail.com</hostname>
// <port>993</port>
// <socketType>SSL</socketType>
// <username>%EMAILADDRESS%</username>
// <authentication>OAuth2</authentication>
// <authentication>password-cleartext</authentication>
// </incomingServer>
2018-12-29 15:04:22 +00:00
imap = "imap".equals(xml.getAttributeValue(null, "type"));
2018-12-29 18:39:02 +00:00
} else if ("outgoingServer".equals(name)) {
// <outgoingServer type="smtp">
// <hostname>smtp.gmail.com</hostname>
// <port>465</port>
// <socketType>SSL</socketType>
// <username>%EMAILADDRESS%</username>
// <authentication>OAuth2</authentication>
// <authentication>password-cleartext</authentication>
// </outgoingServer>
2018-12-29 15:04:22 +00:00
smtp = "smtp".equals(xml.getAttributeValue(null, "type"));
2018-12-29 18:39:02 +00:00
} else if ("hostname".equals(name)) {
2018-12-29 15:04:22 +00:00
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
String host = xml.getText();
Log.i("Host=" + host);
if (imap)
provider.imap_host = host;
else if (smtp)
provider.smtp_host = host;
}
continue;
2018-12-29 18:39:02 +00:00
} else if ("port".equals(name)) {
2018-12-29 15:04:22 +00:00
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
String port = xml.getText();
Log.i("Port=" + port);
if (imap) {
provider.imap_port = Integer.parseInt(port);
provider.imap_starttls = (provider.imap_port == 143);
} else if (smtp) {
provider.smtp_port = Integer.parseInt(port);
provider.smtp_starttls = (provider.smtp_port == 587);
}
}
continue;
2018-12-29 18:39:02 +00:00
} else if ("socketType".equals(name)) {
2018-12-29 15:04:22 +00:00
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
String socket = xml.getText();
Log.i("Socket=" + socket);
if ("SSL".equals(socket)) {
if (imap)
provider.imap_starttls = false;
else if (smtp)
provider.smtp_starttls = false;
} else if ("STARTTLS".equals(socket)) {
if (imap)
provider.imap_starttls = true;
else if (smtp)
provider.smtp_starttls = true;
2018-12-29 17:23:27 +00:00
} else
Log.w("Unknown socket type=" + socket);
2018-12-29 15:04:22 +00:00
}
2018-12-29 17:23:27 +00:00
continue;
2018-12-29 15:04:22 +00:00
2018-12-29 18:39:02 +00:00
} else if ("username".equals(name)) {
2018-12-29 17:23:27 +00:00
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);
}
2018-12-29 15:04:22 +00:00
continue;
2018-12-29 17:23:27 +00:00
2018-12-29 18:39:02 +00:00
} else if ("enable".equals(name)) {
// <enable visiturl="https://mail.google.com/mail/?ui=2&shva=1#settings/fwdandpop">
// <instruction>You need to enable IMAP access</instruction>
// </enable>
href = xml.getAttributeValue(null, "visiturl");
title = null;
} else if ("documentation".equals(name)) {
// <documentation url="http://mail.google.com/support/bin/answer.py?answer=13273">
// <descr>How to enable IMAP/POP3 in GMail</descr>
// </documentation>
href = xml.getAttributeValue(null, "url");
title = null;
} else if ("instruction".equals(name) || "descr".equals(name)) {
if (href != null) {
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
if (title == null)
title = "";
else
title += "<br />";
title += xml.getText();
}
continue;
}
2018-12-29 15:04:22 +00:00
}
2018-12-29 17:23:27 +00:00
2018-12-29 15:04:22 +00:00
} else if (eventType == XmlPullParser.END_TAG) {
2018-12-29 18:39:02 +00:00
String name = xml.getName();
if ("incomingServer".equals(name))
2018-12-29 15:04:22 +00:00
imap = false;
2018-12-29 18:39:02 +00:00
else if ("outgoingServer".equals(name))
2018-12-29 15:04:22 +00:00
smtp = false;
2018-12-29 18:39:02 +00:00
else if ("enable".equals(name) || "documentation".equals(name)) {
if (href != null) {
if (title == null)
title = href;
2019-01-06 08:16:44 +00:00
addDocumentation(provider, href, title);
2018-12-29 18:39:02 +00:00
href = null;
title = null;
}
}
2018-12-29 15:04:22 +00:00
}
eventType = xml.next();
}
request.disconnect();
Log.i("imap=" + provider.imap_host + ":" + provider.imap_port + ":" + provider.imap_starttls);
Log.i("smtp=" + provider.smtp_host + ":" + provider.smtp_port + ":" + provider.smtp_starttls);
2019-01-06 08:16:44 +00:00
2019-04-12 08:28:23 +02:00
return provider;
2018-12-29 15:04:22 +00:00
}
2019-04-12 08:28:23 +02:00
private static EmailProvider fromDNS(String domain) throws TextParseException, UnknownHostException {
2018-12-29 15:04:22 +00:00
// https://tools.ietf.org/html/rfc6186
SRVRecord imap = lookup("_imaps._tcp." + domain);
SRVRecord smtp = lookup("_submission._tcp." + domain);
2019-01-13 15:23:04 +00:00
EmailProvider provider = new EmailProvider(domain);
2018-12-29 15:04:22 +00:00
provider.imap_host = imap.getTarget().toString(true);
provider.imap_port = imap.getPort();
provider.imap_starttls = (provider.imap_port == 143);
provider.smtp_host = smtp.getTarget().toString(true);
provider.smtp_port = smtp.getPort();
provider.smtp_starttls = (provider.smtp_port == 587);
2019-04-12 08:28:23 +02:00
return provider;
2019-01-06 08:16:44 +00:00
}
2019-04-12 08:38:15 +02:00
private static EmailProvider fromTemplate(String domain) {
EmailProvider provider = new EmailProvider(domain);
provider.imap_host = "imap." + domain;
provider.imap_port = 993;
provider.imap_starttls = false;
provider.smtp_host = "smtp." + domain;
provider.smtp_port = 587;
provider.smtp_starttls = true;
return provider;
}
2019-01-06 08:16:44 +00:00
2019-01-13 15:23:04 +00:00
private static void addDocumentation(EmailProvider provider, String href, String title) {
2019-01-06 08:16:44 +00:00
if (provider.documentation == null)
provider.documentation = new StringBuilder();
else
provider.documentation.append("<br /><br />");
provider.documentation.append("<a href=\"").append(href).append("\">").append(title).append("</a>");
}
2019-01-13 15:23:04 +00:00
private static EmailProvider addSpecials(Context context, EmailProvider provider) {
for (EmailProvider predefined : loadProfiles(context))
2019-01-06 08:16:44 +00:00
if (provider.imap_host.equals(predefined.imap_host)) {
provider.prefix = predefined.prefix;
break;
}
if ("imap.gmail.com".equals(provider.imap_host))
addDocumentation(provider,
"https://www.google.com/settings/security/lesssecureapps",
context.getString(R.string.title_setup_setting_gmail));
2019-04-12 08:50:46 +02:00
if (provider.imap_host.endsWith("yahoo.com"))
addDocumentation(provider,
"https://login.yahoo.com/account/security#less-secure-apps",
context.getString(R.string.title_setup_setting_yahoo));
2018-12-29 15:04:22 +00:00
return provider;
}
private static SRVRecord lookup(String dns) throws TextParseException, UnknownHostException {
Lookup lookup = new Lookup(dns, Type.SRV);
2018-12-30 09:55:14 +00:00
// https://dns.watch/ 84.200.69.80
SimpleResolver resolver = new SimpleResolver("8.8.8.8");
2018-12-29 15:04:22 +00:00
lookup.setResolver(resolver);
Log.i("Lookup dns=" + dns + " @" + resolver.getAddress());
Record[] records = lookup.run();
2019-01-06 08:16:44 +00:00
2018-12-29 15:04:22 +00:00
if (lookup.getResult() != Lookup.SUCCESSFUL)
if (lookup.getResult() == Lookup.HOST_NOT_FOUND)
throw new UnknownHostException(dns);
else
throw new IllegalArgumentException(lookup.getErrorString());
2019-01-06 08:16:44 +00:00
2018-12-29 15:04:22 +00:00
Log.i("Found dns=" + (records == null ? -1 : records.length));
return (records == null || records.length == 0 ? null : (SRVRecord) records[0]);
}
2019-01-06 08:16:44 +00:00
int getAuthType() {
if ("com.google".equals(type))
return Helper.AUTH_TYPE_GMAIL;
return Helper.AUTH_TYPE_PASSWORD;
}
2018-08-02 13:33:06 +00:00
@Override
public String toString() {
return name;
}
}