From b8af2f1a68c883908180393278a85ed6f2e3f9ba Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 14 Apr 2022 11:52:27 +0600 Subject: [PATCH 1/3] update leakCanary version --- app/k9mail/build.gradle | 2 +- build.gradle | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/k9mail/build.gradle b/app/k9mail/build.gradle index 940597d77a..f6c923edfb 100644 --- a/app/k9mail/build.gradle +++ b/app/k9mail/build.gradle @@ -26,7 +26,7 @@ dependencies { annotationProcessor "com.github.bumptech.glide:compiler:${versions.glide}" if (project.hasProperty('k9mail.enableLeakCanary') && project.property('k9mail.enableLeakCanary') == "true") { - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4' + debugImplementation "com.squareup.leakcanary:leakcanary-android:${versions.leakcanary}" } // Required for DependencyInjectionTest to be able to resolve OpenPgpApiManager diff --git a/build.gradle b/build.gradle index 0f209cbcd2..073834c7b9 100644 --- a/build.gradle +++ b/build.gradle @@ -49,8 +49,8 @@ buildscript { 'mockitoKotlin': '4.0.0', 'truth': '1.1.3', 'turbine': '0.7.0', - - 'ktlint': '0.40.0' + 'ktlint': '0.40.0', + 'leakcanary': '2.8.1' ] javaVersion = JavaVersion.VERSION_11 -- GitLab From 5b6b9e2a6a38188db874a667fa64dfe55ace1456 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 15 Apr 2022 23:24:39 +0600 Subject: [PATCH 2/3] 4235-Auto_discover_ecloud_accounts issue: https://gitlab.e.foundation/e/backlog/-/issues/4235 On MailList activity onCreate, we retrieve accounts from accountManager & configure the mails by predefine settings on provider.xml file. For /e/ cloud account, if the mail's is missing on th provider.xml file, the app will try to retrieve the info from the api & set up. --- app/ui/legacy/build.gradle | 3 + .../accountmanager/EeloAccountCreator.java | 137 ++++++++++++++++++ .../accountmanager/EeloMailAutoConfigApi.java | 16 ++ .../EeloMailAutoConfigBaseServer.java | 75 ++++++++++ .../EeloMailAutoConfigEmailProvider.java | 44 ++++++ .../EeloMailAutoConfigIncomingServer.java | 9 ++ .../EeloMailAutoConfigOutgoingServer.java | 22 +++ .../EeloMailAutoConfigResponse.java | 33 +++++ 8 files changed, 339 insertions(+) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigApi.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigBaseServer.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigEmailProvider.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigIncomingServer.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigOutgoingServer.java create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigResponse.java diff --git a/app/ui/legacy/build.gradle b/app/ui/legacy/build.gradle index a7504055da..eafb08efa1 100644 --- a/app/ui/legacy/build.gradle +++ b/app/ui/legacy/build.gradle @@ -65,6 +65,9 @@ dependencies { testImplementation "io.insert-koin:koin-test-junit4:${versions.koin}" testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.kotlinCoroutines}" testImplementation "app.cash.turbine:turbine:${versions.turbine}" + + implementation "com.squareup.retrofit2:retrofit:${versions.retrofit}" + implementation "com.squareup.retrofit2:converter-simplexml:${versions.retrofit}" } android { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java index 7249b0d629..bb9b23a8ec 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java @@ -1,6 +1,8 @@ package com.fsck.k9.activity.setup.accountmanager; +import java.io.IOException; + import android.content.Context; import com.fsck.k9.Account; @@ -13,9 +15,17 @@ import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; import com.fsck.k9.autodiscovery.api.DiscoveryResults; import com.fsck.k9.autodiscovery.api.DiscoveryTarget; import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery; +import com.fsck.k9.helper.EmailHelper; +import com.fsck.k9.mail.AuthType; +import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mailstore.SpecialLocalFoldersCreator; +import com.fsck.k9.preferences.Protocols; import com.fsck.k9.ui.ConnectionSettings; +import retrofit2.Call; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.simplexml.SimpleXmlConverterFactory; import timber.log.Timber; @@ -33,6 +43,10 @@ public class EeloAccountCreator { account.setName(emailId); ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(emailId); + if (connectionSettings == null) { + // connection details not predefined in the xml. Try to load from the api + connectionSettings = retrieveConfigFromApi(emailId); + } if (connectionSettings == null) { Timber.e("Error while trying to initialise account configuration."); return; @@ -52,6 +66,129 @@ public class EeloAccountCreator { Core.setServicesEnabled(context); } + /** + * Try to retrieve /e/ cloud mail info from the api + * @param email retrieved from accountManager for /e/ cloud account + * @return mail's incoming & outgoing connection detail, null if failed to retrieve + */ + private static ConnectionSettings retrieveConfigFromApi(String email) { + String domain = EmailHelper.getDomainFromEmailAddress(email); + if (domain == null) { + return null; + } + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://autoconfig.e.email/") //dummy url. Retrofit required base url to configure. But we will use @Url on th api interface level + .addConverterFactory(SimpleXmlConverterFactory.create()) + .build(); + + EeloMailAutoConfigApi eeloMailAutoConfigApi = retrofit.create(EeloMailAutoConfigApi.class); + // api's basic url pattern is: https://autoconfig.${DOMAIN}/mail/config-v1.1.xml?emailaddress=${USER}@${DOMAIN} + Call eeloMailAutoConfigResponseCall = eeloMailAutoConfigApi.getConfig("https://autoconfig." + domain + "/mail/config-v1.1.xml?emailaddress=" + email); + try { + Response response = eeloMailAutoConfigResponseCall.execute(); + if (response.isSuccessful()) { + EeloMailAutoConfigResponse eeloMailAutoConfigResponse = response.body(); + return providersAutoConfigDiscoveryDiscover(eeloMailAutoConfigResponse); + } + } catch (IOException e) { + Timber.e(e); + } + return null; + } + + /** + * Parse connection settings from api response + */ + private static ConnectionSettings providersAutoConfigDiscoveryDiscover(EeloMailAutoConfigResponse response) { + if (response == null) { + return null; + } + + DiscoveredServerSettings incoming = getAutoConfigIncomingSettings(response); + DiscoveredServerSettings outgoing = getAutoConfigOutgoingSettings(response); + + if (incoming == null || outgoing == null) { + return null; + } + + return new ConnectionSettings( + new ServerSettings( + incoming.getProtocol(), + incoming.getHost(), + incoming.getPort(), + incoming.getSecurity(), + incoming.getAuthType(), + incoming.getUsername(), + null, + null + ), + new ServerSettings( + outgoing.getProtocol(), + outgoing.getHost(), + outgoing.getPort(), + outgoing.getSecurity(), + outgoing.getAuthType(), + outgoing.getUsername(), + null, + null + ) + ); + } + + /** + * parse common settings for incoming & outgoing mail server + */ + private static DiscoveredServerSettings getAutoConfigSettings(EeloMailAutoConfigBaseServer server) { + ConnectionSecurity security = null; + if (server.getSocketType().equalsIgnoreCase("SSL")) { + security = ConnectionSecurity.SSL_TLS_REQUIRED; + } else if (server.getSocketType().equalsIgnoreCase("STARTTLS")) { + security = ConnectionSecurity.STARTTLS_REQUIRED; + } else { + return null; + } + + String host = server.getHostname(); + if (host == null || host.trim().equals("")) { + return null; + } + + String username = server.getUsername(); + if (username == null || username.trim().equals("")) { + return null; + } + return new DiscoveredServerSettings(Protocols.IMAP, host, server.getPort(), security, AuthType.PLAIN, username); + + } + + private static DiscoveredServerSettings getAutoConfigIncomingSettings(EeloMailAutoConfigResponse response) { + DiscoveredServerSettings settings = getAutoConfigSettings(response.getEmailProvider().getIncomingServer()); + if (settings == null) { + return null; + } + int port = settings.getPort(); + if (port <= 0) { + port = (settings.getSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) ? 143 : 993; + } + + return new DiscoveredServerSettings(Protocols.IMAP, settings.getHost(), port, settings.getSecurity(), AuthType.PLAIN, settings.getUsername()); + } + + + private static DiscoveredServerSettings getAutoConfigOutgoingSettings(EeloMailAutoConfigResponse response) { + DiscoveredServerSettings settings = getAutoConfigSettings(response.getEmailProvider().getOutgoingServer()); + if (settings == null) { + return null; + } + int port = settings.getPort(); + if (port <= 0) { + port = (settings.getSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) ? 587 : 465; + } + + return new DiscoveredServerSettings(Protocols.SMTP, settings.getHost(), port, settings.getSecurity(), AuthType.PLAIN, settings.getUsername()); + } + private static ConnectionSettings providersXmlDiscoveryDiscover(String email) { DiscoveryResults discoveryResults = providersXmlDiscovery.discover(email, DiscoveryTarget.INCOMING_AND_OUTGOING); diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigApi.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigApi.java new file mode 100644 index 0000000000..675e6eb926 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigApi.java @@ -0,0 +1,16 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Url; + + +/** + * To retrieve /e/ cloud mail configuration settings from api + */ +public interface EeloMailAutoConfigApi { + + @GET + Call getConfig(@Url String url); +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigBaseServer.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigBaseServer.java new file mode 100644 index 0000000000..dc558cd845 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigBaseServer.java @@ -0,0 +1,75 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import org.simpleframework.xml.Attribute; +import org.simpleframework.xml.Element; + + +public abstract class EeloMailAutoConfigBaseServer { + + @Attribute + private String type; + + @Element + private String hostname; + + @Element(name = "port", type = Integer.class) + private int port; + + @Element + private String socketType; + + @Element + private String authentication; + + @Element + private String username; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getSocketType() { + return socketType; + } + + public void setSocketType(String socketType) { + this.socketType = socketType; + } + + public String getAuthentication() { + return authentication; + } + + public void setAuthentication(String authentication) { + this.authentication = authentication; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigEmailProvider.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigEmailProvider.java new file mode 100644 index 0000000000..fdd09588ce --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigEmailProvider.java @@ -0,0 +1,44 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import org.simpleframework.xml.Attribute; +import org.simpleframework.xml.Element; +import org.simpleframework.xml.Root; + + +@Root(name = "emailProvider", strict = false) +public class EeloMailAutoConfigEmailProvider { + + @Attribute + private String id; + + @Element + private EeloMailAutoConfigOutgoingServer outgoingServer; + + @Element + private EeloMailAutoConfigIncomingServer incomingServer; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public EeloMailAutoConfigOutgoingServer getOutgoingServer() { + return outgoingServer; + } + + public void setOutgoingServer(EeloMailAutoConfigOutgoingServer outgoingServer) { + this.outgoingServer = outgoingServer; + } + + public EeloMailAutoConfigIncomingServer getIncomingServer() { + return incomingServer; + } + + public void setIncomingServer(EeloMailAutoConfigIncomingServer incomingServer) { + this.incomingServer = incomingServer; + } +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigIncomingServer.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigIncomingServer.java new file mode 100644 index 0000000000..dee6f7dad3 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigIncomingServer.java @@ -0,0 +1,9 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import org.simpleframework.xml.Root; + + +@Root(name = "incomingServer", strict = false) +public class EeloMailAutoConfigIncomingServer extends EeloMailAutoConfigBaseServer { +} diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigOutgoingServer.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigOutgoingServer.java new file mode 100644 index 0000000000..7da5c2a2ea --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigOutgoingServer.java @@ -0,0 +1,22 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import org.simpleframework.xml.Element; +import org.simpleframework.xml.Root; + + +@Root(name = "outgoingServer", strict = false) +public class EeloMailAutoConfigOutgoingServer extends EeloMailAutoConfigBaseServer { + + @Element + private String useGlobalPreferredServer; + + public String getUseGlobalPreferredServer() { + return useGlobalPreferredServer; + } + + public void setUseGlobalPreferredServer(String useGlobalPreferredServer) { + this.useGlobalPreferredServer = useGlobalPreferredServer; + } +} + diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigResponse.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigResponse.java new file mode 100644 index 0000000000..eba8eabc7a --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigResponse.java @@ -0,0 +1,33 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import org.simpleframework.xml.Attribute; +import org.simpleframework.xml.Element; +import org.simpleframework.xml.Root; + + +@Root(name = "clientConfig", strict = false) +public class EeloMailAutoConfigResponse { + + @Attribute + private String version; + + @Element + private EeloMailAutoConfigEmailProvider emailProvider; + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public EeloMailAutoConfigEmailProvider getEmailProvider() { + return emailProvider; + } + + public void setEmailProvider(EeloMailAutoConfigEmailProvider emailProvider) { + this.emailProvider = emailProvider; + } +} -- GitLab From 3feb59b81477716a5856bec092192bd5793404ff Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 18 Apr 2022 12:28:22 +0600 Subject: [PATCH 3/3] move eCloud autoConfig logic to separate class --- .../main/java/com/fsck/k9/helper/Utility.java | 4 + .../accountmanager/EeloAccountCreator.java | 135 +------------ .../EeloMailAutoConfigDiscovery.java | 177 ++++++++++++++++++ 3 files changed, 182 insertions(+), 134 deletions(-) create mode 100644 app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java diff --git a/app/core/src/main/java/com/fsck/k9/helper/Utility.java b/app/core/src/main/java/com/fsck/k9/helper/Utility.java index 1788f8b3a0..8775ca0db3 100644 --- a/app/core/src/main/java/com/fsck/k9/helper/Utility.java +++ b/app/core/src/main/java/com/fsck/k9/helper/Utility.java @@ -295,4 +295,8 @@ public class Utility { return null; } + + public static boolean isStringEmpty(String st) { + return (st == null || st.trim().equals("")); + } } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java index bb9b23a8ec..b05af6f10d 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloAccountCreator.java @@ -1,8 +1,6 @@ package com.fsck.k9.activity.setup.accountmanager; -import java.io.IOException; - import android.content.Context; import com.fsck.k9.Account; @@ -15,17 +13,9 @@ import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; import com.fsck.k9.autodiscovery.api.DiscoveryResults; import com.fsck.k9.autodiscovery.api.DiscoveryTarget; import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery; -import com.fsck.k9.helper.EmailHelper; -import com.fsck.k9.mail.AuthType; -import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mailstore.SpecialLocalFoldersCreator; -import com.fsck.k9.preferences.Protocols; import com.fsck.k9.ui.ConnectionSettings; -import retrofit2.Call; -import retrofit2.Response; -import retrofit2.Retrofit; -import retrofit2.converter.simplexml.SimpleXmlConverterFactory; import timber.log.Timber; @@ -45,7 +35,7 @@ public class EeloAccountCreator { ConnectionSettings connectionSettings = providersXmlDiscoveryDiscover(emailId); if (connectionSettings == null) { // connection details not predefined in the xml. Try to load from the api - connectionSettings = retrieveConfigFromApi(emailId); + connectionSettings = EeloMailAutoConfigDiscovery.retrieveConfigFromApi(emailId); } if (connectionSettings == null) { Timber.e("Error while trying to initialise account configuration."); @@ -66,129 +56,6 @@ public class EeloAccountCreator { Core.setServicesEnabled(context); } - /** - * Try to retrieve /e/ cloud mail info from the api - * @param email retrieved from accountManager for /e/ cloud account - * @return mail's incoming & outgoing connection detail, null if failed to retrieve - */ - private static ConnectionSettings retrieveConfigFromApi(String email) { - String domain = EmailHelper.getDomainFromEmailAddress(email); - if (domain == null) { - return null; - } - - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://autoconfig.e.email/") //dummy url. Retrofit required base url to configure. But we will use @Url on th api interface level - .addConverterFactory(SimpleXmlConverterFactory.create()) - .build(); - - EeloMailAutoConfigApi eeloMailAutoConfigApi = retrofit.create(EeloMailAutoConfigApi.class); - // api's basic url pattern is: https://autoconfig.${DOMAIN}/mail/config-v1.1.xml?emailaddress=${USER}@${DOMAIN} - Call eeloMailAutoConfigResponseCall = eeloMailAutoConfigApi.getConfig("https://autoconfig." + domain + "/mail/config-v1.1.xml?emailaddress=" + email); - try { - Response response = eeloMailAutoConfigResponseCall.execute(); - if (response.isSuccessful()) { - EeloMailAutoConfigResponse eeloMailAutoConfigResponse = response.body(); - return providersAutoConfigDiscoveryDiscover(eeloMailAutoConfigResponse); - } - } catch (IOException e) { - Timber.e(e); - } - return null; - } - - /** - * Parse connection settings from api response - */ - private static ConnectionSettings providersAutoConfigDiscoveryDiscover(EeloMailAutoConfigResponse response) { - if (response == null) { - return null; - } - - DiscoveredServerSettings incoming = getAutoConfigIncomingSettings(response); - DiscoveredServerSettings outgoing = getAutoConfigOutgoingSettings(response); - - if (incoming == null || outgoing == null) { - return null; - } - - return new ConnectionSettings( - new ServerSettings( - incoming.getProtocol(), - incoming.getHost(), - incoming.getPort(), - incoming.getSecurity(), - incoming.getAuthType(), - incoming.getUsername(), - null, - null - ), - new ServerSettings( - outgoing.getProtocol(), - outgoing.getHost(), - outgoing.getPort(), - outgoing.getSecurity(), - outgoing.getAuthType(), - outgoing.getUsername(), - null, - null - ) - ); - } - - /** - * parse common settings for incoming & outgoing mail server - */ - private static DiscoveredServerSettings getAutoConfigSettings(EeloMailAutoConfigBaseServer server) { - ConnectionSecurity security = null; - if (server.getSocketType().equalsIgnoreCase("SSL")) { - security = ConnectionSecurity.SSL_TLS_REQUIRED; - } else if (server.getSocketType().equalsIgnoreCase("STARTTLS")) { - security = ConnectionSecurity.STARTTLS_REQUIRED; - } else { - return null; - } - - String host = server.getHostname(); - if (host == null || host.trim().equals("")) { - return null; - } - - String username = server.getUsername(); - if (username == null || username.trim().equals("")) { - return null; - } - return new DiscoveredServerSettings(Protocols.IMAP, host, server.getPort(), security, AuthType.PLAIN, username); - - } - - private static DiscoveredServerSettings getAutoConfigIncomingSettings(EeloMailAutoConfigResponse response) { - DiscoveredServerSettings settings = getAutoConfigSettings(response.getEmailProvider().getIncomingServer()); - if (settings == null) { - return null; - } - int port = settings.getPort(); - if (port <= 0) { - port = (settings.getSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) ? 143 : 993; - } - - return new DiscoveredServerSettings(Protocols.IMAP, settings.getHost(), port, settings.getSecurity(), AuthType.PLAIN, settings.getUsername()); - } - - - private static DiscoveredServerSettings getAutoConfigOutgoingSettings(EeloMailAutoConfigResponse response) { - DiscoveredServerSettings settings = getAutoConfigSettings(response.getEmailProvider().getOutgoingServer()); - if (settings == null) { - return null; - } - int port = settings.getPort(); - if (port <= 0) { - port = (settings.getSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) ? 587 : 465; - } - - return new DiscoveredServerSettings(Protocols.SMTP, settings.getHost(), port, settings.getSecurity(), AuthType.PLAIN, settings.getUsername()); - } - private static ConnectionSettings providersXmlDiscoveryDiscover(String email) { DiscoveryResults discoveryResults = providersXmlDiscovery.discover(email, DiscoveryTarget.INCOMING_AND_OUTGOING); diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java new file mode 100644 index 0000000000..9c914cbe99 --- /dev/null +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/accountmanager/EeloMailAutoConfigDiscovery.java @@ -0,0 +1,177 @@ +package com.fsck.k9.activity.setup.accountmanager; + + +import java.io.IOException; + +import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings; +import com.fsck.k9.helper.EmailHelper; +import com.fsck.k9.helper.Utility; +import com.fsck.k9.mail.AuthType; +import com.fsck.k9.mail.ConnectionSecurity; +import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.ui.ConnectionSettings; +import retrofit2.Call; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.simplexml.SimpleXmlConverterFactory; +import timber.log.Timber; + + +public class EeloMailAutoConfigDiscovery { + + /** + * Try to retrieve /e/ cloud mail info from the api + * + * @param email retrieved from accountManager for /e/ cloud account + * @return mail's incoming & outgoing connection detail, null if failed to retrieve + */ + public static ConnectionSettings retrieveConfigFromApi(String email) { + String domain = EmailHelper.getDomainFromEmailAddress(email); + if (domain == null) { + return null; + } + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl( + "https://autoconfig.e.email/") //dummy url. Retrofit required base url to configure. But we will use @Url on th api interface level + .addConverterFactory(SimpleXmlConverterFactory.create()) + .build(); + + EeloMailAutoConfigApi eeloMailAutoConfigApi = retrofit.create(EeloMailAutoConfigApi.class); + // api's basic url pattern is: https://autoconfig.${DOMAIN}/mail/config-v1.1.xml?emailaddress=${USER}@${DOMAIN} + Call + eeloMailAutoConfigResponseCall = eeloMailAutoConfigApi + .getConfig("https://autoconfig." + domain + "/mail/config-v1.1.xml?emailaddress=" + email); + try { + Response response = eeloMailAutoConfigResponseCall.execute(); + if (response.isSuccessful()) { + EeloMailAutoConfigResponse eeloMailAutoConfigResponse = response.body(); + return providersAutoConfigDiscoveryDiscover(eeloMailAutoConfigResponse); + } + } catch (IOException e) { + Timber.e(e); + } + return null; + } + + /** + * Parse connection settings from api response + */ + private static ConnectionSettings providersAutoConfigDiscoveryDiscover(EeloMailAutoConfigResponse response) { + if (response == null) { + return null; + } + + DiscoveredServerSettings incoming = getAutoConfigIncomingSettings(response); + DiscoveredServerSettings outgoing = getAutoConfigOutgoingSettings(response); + + if (incoming == null || outgoing == null) { + return null; + } + + return new ConnectionSettings( + new ServerSettings( + incoming.getProtocol(), + incoming.getHost(), + incoming.getPort(), + incoming.getSecurity(), + incoming.getAuthType(), + incoming.getUsername(), + null, + null + ), + new ServerSettings( + outgoing.getProtocol(), + outgoing.getHost(), + outgoing.getPort(), + outgoing.getSecurity(), + outgoing.getAuthType(), + outgoing.getUsername(), + null, + null + ) + ); + } + + /** + * parse common settings for incoming & outgoing mail server + */ + private static DiscoveredServerSettings getAutoConfigSettings(EeloMailAutoConfigBaseServer server) { + if (Utility.isStringEmpty(server.getType())) { + return null; + } + if (Utility.isStringEmpty(server.getHostname())) { + return null; + } + + if (Utility.isStringEmpty(server.getUsername())) { + return null; + } + + AuthType authType = getAuthType(server.getAuthentication()); + if (authType == null) { + return null; + } + + if (Utility.isStringEmpty(server.getSocketType())) { + return null; + } + + ConnectionSecurity security = getConnectionSecurity(server.getSocketType()); + if (security == null) { + return null; + } + + return new DiscoveredServerSettings(server.getType(), server.getHostname(), server.getPort(), security, + authType, server.getUsername()); + } + + private static AuthType getAuthType(String authString) { + if (Utility.isStringEmpty(authString)) { + return null; + } + authString = authString.toUpperCase(); + switch (authString) { + case "PASSWORD-CLEARTEXT": + return AuthType.PLAIN; + case "PASSWORD-ENCRYPTED": + return AuthType.CRAM_MD5; + case "OAUTH2": + return AuthType.XOAUTH2; + } + return null; + } + + private static ConnectionSecurity getConnectionSecurity(String securityString) { + if (Utility.isStringEmpty(securityString)) { + return null; + } + securityString = securityString.toUpperCase(); + switch (securityString) { + case "SSL": + return ConnectionSecurity.SSL_TLS_REQUIRED; + case "STARTTLS": + return ConnectionSecurity.STARTTLS_REQUIRED; + case "PLAIN": + return ConnectionSecurity.NONE; + } + return null; + } + + private static DiscoveredServerSettings getAutoConfigIncomingSettings(EeloMailAutoConfigResponse response) { + if (response.getEmailProvider() == null || response.getEmailProvider().getIncomingServer() == null) { + return null; + } + return getAutoConfigSettings(response.getEmailProvider().getIncomingServer()); + } + + private static DiscoveredServerSettings getAutoConfigOutgoingSettings(EeloMailAutoConfigResponse response) { + if (response.getEmailProvider() == null || response.getEmailProvider().getOutgoingServer() == null) { + return null; + } + return getAutoConfigSettings(response.getEmailProvider().getOutgoingServer()); + } + + private EeloMailAutoConfigDiscovery() { + } +} -- GitLab