Check mail.domain.tld as alternative host

pull/156/head
M66B 6 years ago
parent c88cb380d3
commit 561c628817

@ -35,6 +35,9 @@ import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.Collator;
@ -70,6 +73,18 @@ public class EmailProvider {
this.name = name;
}
private EmailProvider(String name, String domain, String imap_prefix, String smtp_prefix) {
this.name = name;
this.imap_host = imap_prefix + "." + domain;
this.imap_port = 993;
this.imap_starttls = false;
this.smtp_host = smtp_prefix + "." + domain;
this.smtp_port = 587;
this.smtp_starttls = true;
}
static List<EmailProvider> loadProfiles(Context context) {
List<EmailProvider> result = null;
try {
@ -138,8 +153,13 @@ public class EmailProvider {
return addSpecials(context, fromDNS(domain));
} catch (UnknownHostException ex1) {
Log.w(ex1);
Log.i("Provider from template domain=" + domain);
return addSpecials(context, fromTemplate(domain));
try {
Log.i("Provider from template domain=" + domain);
return addSpecials(context, fromTemplate(domain));
} catch (UnknownHostException ex2) {
Log.w(ex2);
throw new UnknownHostException(context.getString(R.string.title_setup_no_settings, domain));
}
}
}
}
@ -339,17 +359,34 @@ public class EmailProvider {
return provider;
}
private static EmailProvider fromTemplate(String domain) {
EmailProvider provider = new EmailProvider(domain);
provider.imap_host = "imap." + domain;
provider.imap_port = 993;
provider.imap_starttls = false;
private static EmailProvider fromTemplate(String domain) throws UnknownHostException {
if (checkTemplate(domain, "imap", 993, "smtp", 587))
return new EmailProvider(domain, domain, "imap", "smtp");
provider.smtp_host = "smtp." + domain;
provider.smtp_port = 587;
provider.smtp_starttls = true;
else if (checkTemplate(domain, "mail", 993, "mail", 587))
return new EmailProvider(domain, domain, "mail", "mail");
return provider;
else
throw new UnknownHostException();
}
private static boolean checkTemplate(
String domain, String imap_prefix, int imap_port, String smtp_prefix, int smtp_port) {
return isHostReachable(imap_prefix + "." + domain, imap_port, 5000) &&
isHostReachable(smtp_prefix + "." + domain, smtp_port, 5000);
}
static boolean isHostReachable(String host, int port, int timeoutms) {
Log.i("Checking " + host + ":" + port);
try (Socket socket = new Socket()) {
InetAddress iaddr = InetAddress.getByName(host);
InetSocketAddress inetSocketAddress = new InetSocketAddress(iaddr, port);
socket.connect(inetSocketAddress, timeoutms);
return true;
} catch (IOException ex) {
Log.w(ex);
return false;
}
}
private static void addDocumentation(EmailProvider provider, String href, String title) {

@ -61,6 +61,7 @@ import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -439,7 +440,7 @@ public class FragmentQuickSetup extends FragmentBase {
tvInstructions.setVisibility(View.VISIBLE);
}
if (ex instanceof IllegalArgumentException)
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else {
tvError.setText(Helper.formatThrowable(ex));

@ -106,7 +106,7 @@
<string name="title_setup_go">Go</string>
<string name="title_setup_setting_gmail">Enable access for "less secure" apps</string>
<string name="title_setup_setting_yahoo">Enable access for "less secure" apps</string>
<string name="title_setup_no_settings">No settings found for \'%1$s\'</string>
<string name="title_setup_no_settings">No settings found for domain \'%1$s\'</string>
<string name="title_setup_quick_success">An account and an identity have successfully been added</string>
<string name="title_setup_quick_failed">You can try to configure an account and an identity below too</string>
<string name="title_setup_manage">Manage</string>

Loading…
Cancel
Save