Fixed/improved email provider

pull/162/head
M66B 5 years ago
parent 6b03106bc3
commit 98885428e8

@ -215,7 +215,7 @@ public class EmailProvider {
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration // https://wiki.mozilla.org/Thunderbird:Autoconfiguration
HttpURLConnection request; HttpURLConnection request;
try { try {
URL url = new URL(" https://autoconfig." + domain + "/mail/config-v1.1.xml?emailaddress=someone@" + domain); URL url = new URL("https://autoconfig." + domain + "/mail/config-v1.1.xml?emailaddress=someone@" + domain);
Log.i("Fetching " + url); Log.i("Fetching " + url);
request = (HttpURLConnection) url.openConnection(); request = (HttpURLConnection) url.openConnection();
@ -252,164 +252,166 @@ public class EmailProvider {
} }
} }
// https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser
XmlPullParser xml = factory.newPullParser(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
xml.setInput(new InputStreamReader(request.getInputStream())); XmlPullParser xml = factory.newPullParser();
xml.setInput(new InputStreamReader(request.getInputStream()));
boolean imap = false;
boolean smtp = false; boolean imap = false;
String href = null; boolean smtp = false;
String title = null; String href = null;
int eventType = xml.getEventType(); String title = null;
while (eventType != XmlPullParser.END_DOCUMENT) { int eventType = xml.getEventType();
if (eventType == XmlPullParser.START_TAG) { while (eventType != XmlPullParser.END_DOCUMENT) {
String name = xml.getName(); if (eventType == XmlPullParser.START_TAG) {
if ("displayShortName".equals(name)) { String name = xml.getName();
// <displayShortName>GMail</displayShortName> if ("displayShortName".equals(name)) {
eventType = xml.next(); // <displayShortName>GMail</displayShortName>
if (eventType == XmlPullParser.TEXT) { eventType = xml.next();
String display = xml.getText(); if (eventType == XmlPullParser.TEXT) {
Log.i("Name=" + display); String display = xml.getText();
provider.name = display; Log.i("Name=" + display);
} provider.name = display;
continue;
} else if ("incomingServer".equals(name)) {
// <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>
imap = "imap".equals(xml.getAttributeValue(null, "type"));
} 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>
smtp = "smtp".equals(xml.getAttributeValue(null, "type"));
} else if ("hostname".equals(name)) {
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;
} else if ("port".equals(name)) {
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;
continue;
} else if ("incomingServer".equals(name)) {
} else if ("socketType".equals(name)) { // <incomingServer type="imap">
eventType = xml.next(); // <hostname>imap.gmail.com</hostname>
if (eventType == XmlPullParser.TEXT) { // <port>993</port>
String socket = xml.getText(); // <socketType>SSL</socketType>
Log.i("Socket=" + socket); // <username>%EMAILADDRESS%</username>
if ("SSL".equals(socket)) { // <authentication>OAuth2</authentication>
if (imap) // <authentication>password-cleartext</authentication>
provider.imap.starttls = false; // </incomingServer>
else if (smtp) imap = "imap".equals(xml.getAttributeValue(null, "type"));
provider.smtp.starttls = false;
} else if ("STARTTLS".equals(socket)) { } 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>
smtp = "smtp".equals(xml.getAttributeValue(null, "type"));
} else if ("hostname".equals(name)) {
eventType = xml.next();
if (eventType == XmlPullParser.TEXT) {
String host = xml.getText();
Log.i("Host=" + host);
if (imap) if (imap)
provider.imap.starttls = true; provider.imap.host = host;
else if (smtp) else if (smtp)
provider.smtp.starttls = true; provider.smtp.host = host;
} else }
Log.w("Unknown socket type=" + socket); continue;
}
continue; } else if ("port".equals(name)) {
} else if ("username".equals(name)) {
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 ("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(); eventType = xml.next();
if (eventType == XmlPullParser.TEXT) { if (eventType == XmlPullParser.TEXT) {
if (title == null) String port = xml.getText();
title = ""; 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;
} else if ("socketType".equals(name)) {
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;
} else
Log.w("Unknown socket type=" + socket);
}
continue;
} else if ("username".equals(name)) {
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 else
title += "<br>"; Log.w("Unknown username type=" + username);
title += xml.getText();
} }
continue; continue;
} 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;
}
} }
}
} else if (eventType == XmlPullParser.END_TAG) { } else if (eventType == XmlPullParser.END_TAG) {
String name = xml.getName(); String name = xml.getName();
if ("incomingServer".equals(name)) if ("incomingServer".equals(name))
imap = false; imap = false;
else if ("outgoingServer".equals(name)) else if ("outgoingServer".equals(name))
smtp = false; smtp = false;
else if ("enable".equals(name) || "documentation".equals(name)) { else if ("enable".equals(name) || "documentation".equals(name)) {
if (href != null) { if (href != null) {
if (title == null) if (title == null)
title = href; title = href;
addDocumentation(provider, href, title); addDocumentation(provider, href, title);
href = null; href = null;
title = null; title = null;
}
} }
} }
}
eventType = xml.next(); eventType = xml.next();
}
} finally {
request.disconnect();
} }
request.disconnect();
Log.i("imap=" + provider.imap.host + ":" + provider.imap.port + ":" + provider.imap.starttls); Log.i("imap=" + provider.imap.host + ":" + provider.imap.port + ":" + provider.imap.starttls);
Log.i("smtp=" + provider.smtp.host + ":" + provider.smtp.port + ":" + provider.smtp.starttls); Log.i("smtp=" + provider.smtp.host + ":" + provider.smtp.port + ":" + provider.smtp.starttls);

Loading…
Cancel
Save