Refactoring

pull/159/head
M66B 6 years ago
parent 196354cf8c
commit 0d55e5880b

@ -65,7 +65,8 @@ public class EmailProvider {
enum UserType {LOCAL, EMAIL} enum UserType {LOCAL, EMAIL}
private static final int TIMEOUT = 20 * 1000; // milliseconds private static final int DNS_TIMEOUT = 5 * 1000; // milliseconds
private static final int ISPDB_TIMEOUT = 20 * 1000; // milliseconds
private EmailProvider() { private EmailProvider() {
} }
@ -80,18 +81,6 @@ public class EmailProvider {
throw new UnknownHostException(this.name + " invalid"); throw new UnknownHostException(this.name + " invalid");
} }
private EmailProvider(String name, String domain, String imap_prefix, String smtp_prefix) {
this.name = name;
this.imap_host = (imap_prefix == null ? "" : imap_prefix + ".") + domain;
this.imap_port = 993;
this.imap_starttls = false;
this.smtp_host = (smtp_prefix == null ? "" : smtp_prefix + ".") + domain;
this.smtp_port = 587;
this.smtp_starttls = true;
}
static List<EmailProvider> loadProfiles(Context context) { static List<EmailProvider> loadProfiles(Context context) {
List<EmailProvider> result = null; List<EmailProvider> result = null;
try { try {
@ -194,8 +183,8 @@ public class EmailProvider {
Log.i("Fetching " + url); Log.i("Fetching " + url);
HttpURLConnection request = (HttpURLConnection) url.openConnection(); HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setReadTimeout(TIMEOUT); request.setReadTimeout(ISPDB_TIMEOUT);
request.setConnectTimeout(TIMEOUT); request.setConnectTimeout(ISPDB_TIMEOUT);
request.setRequestMethod("GET"); request.setRequestMethod("GET");
request.setDoInput(true); request.setDoInput(true);
request.connect(); request.connect();
@ -384,27 +373,50 @@ public class EmailProvider {
} }
private static EmailProvider fromTemplate(Context context, String domain) throws UnknownHostException { private static EmailProvider fromTemplate(Context context, String domain) throws UnknownHostException {
if (checkTemplate(domain, null, 993, null, 587)) EmailProvider provider;
return new EmailProvider(domain, domain, null, null);
else if (checkTemplate(domain, "imap", 993, "smtp", 587)) provider = checkTemplate(domain, null, 993, null, 587);
return new EmailProvider(domain, domain, "imap", "smtp"); if (provider != null)
return provider;
else if (checkTemplate(domain, "mail", 993, "mail", 587)) provider = checkTemplate(domain, "imap", 993, "smtp", 587);
return new EmailProvider(domain, domain, "mail", "mail"); if (provider != null)
return provider;
provider = checkTemplate(domain, "mail", 993, "mail", 587);
if (provider != null)
return provider;
else
throw new UnknownHostException(domain + " template"); throw new UnknownHostException(domain + " template");
} }
private static boolean checkTemplate( private static EmailProvider checkTemplate(
String domain, String imap_prefix, int imap_port, String smtp_prefix, int smtp_port) { String domain,
return isHostReachable((imap_prefix == null ? "" : imap_prefix + ".") + domain, imap_port, 5000) && String imap_prefix, int imap_port,
isHostReachable((smtp_prefix == null ? "" : smtp_prefix + ".") + domain, smtp_port, 5000); String smtp_prefix, int smtp_port) {
String imap_host = (imap_prefix == null ? "" : imap_prefix + ".") + domain;
String smtp_host = (smtp_prefix == null ? "" : smtp_prefix + ".") + domain;
if (isHostReachable(imap_host, imap_port, DNS_TIMEOUT) &&
isHostReachable(smtp_host, smtp_port, DNS_TIMEOUT)) {
EmailProvider provider = new EmailProvider();
provider.name = domain;
provider.imap_host = imap_host;
provider.imap_port = imap_port;
provider.imap_starttls = (imap_port == 143);
provider.smtp_host = smtp_host;
provider.smtp_port = smtp_port;
provider.smtp_starttls = (smtp_port == 587);
return provider;
} else
return null;
} }
private static boolean isHostReachable(String host, int port, int timeoutms) { private static boolean isHostReachable(String host, int port, int timeoutms) {
Log.i("Checking " + host + ":" + port); Log.i("Checking reachable " + host + ":" + port);
try (Socket socket = new Socket()) { try (Socket socket = new Socket()) {
InetAddress iaddr = InetAddress.getByName(host); InetAddress iaddr = InetAddress.getByName(host);
InetSocketAddress inetSocketAddress = new InetSocketAddress(iaddr, port); InetSocketAddress inetSocketAddress = new InetSocketAddress(iaddr, port);

Loading…
Cancel
Save