mirror of
https://github.com/M66B/FairEmail.git
synced 2025-12-26 15:40:59 +01:00
Refactoring
This commit is contained in:
@@ -11,38 +11,22 @@ import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.bugsnag.android.BreadcrumbType;
|
||||
import com.bugsnag.android.Bugsnag;
|
||||
import com.sun.mail.imap.IMAPStore;
|
||||
import com.sun.mail.smtp.SMTPTransport;
|
||||
import com.sun.mail.util.MailConnectException;
|
||||
|
||||
import org.xbill.DNS.Lookup;
|
||||
import org.xbill.DNS.SimpleResolver;
|
||||
import org.xbill.DNS.Type;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.NoSuchProviderException;
|
||||
import javax.mail.Service;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
||||
public class ConnectionHelper {
|
||||
private static final String any4 = "0.0.0.0";
|
||||
// https://dns.watch/
|
||||
private static final String DEFAULT_DNS = "84.200.69.80";
|
||||
|
||||
@@ -262,48 +246,6 @@ public class ConnectionHelper {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void connect(Context context, @NonNull ServiceHolder istore, EntityAccount account) throws MessagingException {
|
||||
connect(context, istore, account.host, account.port, account.user, account.password);
|
||||
}
|
||||
|
||||
static void connect(Context context, ServiceHolder iservice, EntityIdentity identity) throws MessagingException {
|
||||
connect(context, iservice, identity.host, identity.port, identity.user, identity.password);
|
||||
}
|
||||
|
||||
static void connect(Context context, ServiceHolder iservice, String host, int port, String user, String password) throws MessagingException {
|
||||
try {
|
||||
iservice.connect(context, host, port, user, password);
|
||||
} catch (MailConnectException ex) {
|
||||
if (!hasIPv6(host))
|
||||
throw ex;
|
||||
|
||||
try {
|
||||
Log.i("Binding to " + any4);
|
||||
iservice.getSession().getProperties().put("mail.imap.localaddress", any4);
|
||||
iservice.getSession().getProperties().put("mail.imaps.localaddress", any4);
|
||||
iservice.getSession().getProperties().put("mail.smtp.localaddress", any4);
|
||||
iservice.getSession().getProperties().put("mail.smtps.localaddress", any4);
|
||||
iservice.connect(context, host, port, user, password);
|
||||
} catch (Throwable ex1) {
|
||||
Log.w(ex1);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasIPv6(String host) {
|
||||
boolean has = false;
|
||||
try {
|
||||
for (InetAddress iaddr : InetAddress.getAllByName(host)) {
|
||||
Log.i(host + " resolves to " + iaddr);
|
||||
if (iaddr instanceof Inet6Address)
|
||||
has = true;
|
||||
}
|
||||
} catch (UnknownHostException ignored) {
|
||||
}
|
||||
return has;
|
||||
}
|
||||
|
||||
static boolean airplaneMode(Context context) {
|
||||
return Settings.System.getInt(context.getContentResolver(),
|
||||
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
|
||||
@@ -374,66 +316,4 @@ public class ConnectionHelper {
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
static class ServiceHolder implements AutoCloseable {
|
||||
private String protocol;
|
||||
private Session issesion;
|
||||
private Service iservice;
|
||||
|
||||
private ServiceHolder() {
|
||||
}
|
||||
|
||||
ServiceHolder(String protocol, Session issesion) {
|
||||
this.protocol = protocol;
|
||||
this.issesion = issesion;
|
||||
}
|
||||
|
||||
void connect(Context context, String host, int port, String user, String password) throws MessagingException {
|
||||
if ("imap".equals(protocol) || "imaps".equals(protocol)) {
|
||||
iservice = issesion.getStore(protocol);
|
||||
iservice.connect(host, port, user, password);
|
||||
|
||||
// https://www.ietf.org/rfc/rfc2971.txt
|
||||
if (getStore().hasCapability("ID"))
|
||||
try {
|
||||
Map<String, String> id = new LinkedHashMap<>();
|
||||
id.put("name", context.getString(R.string.app_name));
|
||||
id.put("version", BuildConfig.VERSION_NAME);
|
||||
Map<String, String> sid = getStore().id(id);
|
||||
if (sid != null) {
|
||||
Map<String, String> crumb = new HashMap<>();
|
||||
for (String key : sid.keySet()) {
|
||||
crumb.put(key, sid.get(key));
|
||||
EntityLog.log(context, "Server " + key + "=" + sid.get(key));
|
||||
}
|
||||
Bugsnag.leaveBreadcrumb("server", BreadcrumbType.LOG, crumb);
|
||||
}
|
||||
} catch (MessagingException ex) {
|
||||
Log.w(ex);
|
||||
}
|
||||
|
||||
} else if ("smtp".equals(protocol) || "smtps".equals(protocol)) {
|
||||
iservice = issesion.getTransport(protocol);
|
||||
iservice.connect(host, port, user, password);
|
||||
} else
|
||||
throw new NoSuchProviderException(protocol);
|
||||
}
|
||||
|
||||
Session getSession() {
|
||||
return this.issesion;
|
||||
}
|
||||
|
||||
IMAPStore getStore() {
|
||||
return (IMAPStore) this.iservice;
|
||||
}
|
||||
|
||||
SMTPTransport getTransport() {
|
||||
return (SMTPTransport) this.iservice;
|
||||
}
|
||||
|
||||
public void close() throws MessagingException {
|
||||
if (iservice != null)
|
||||
iservice.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user