diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/ServerSettings.java b/k9mail-library/src/main/java/com/fsck/k9/mail/ServerSettings.java index b3d2852ea..0935b84fb 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/ServerSettings.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/ServerSettings.java @@ -17,7 +17,30 @@ import java.util.Map; */ public class ServerSettings { - public enum Type { IMAP, SMTP, WebDAV, POP3 } + public enum Type { + + IMAP(143, 993), + SMTP(587, 465), + WebDAV(80, 443), + POP3(110, 995); + + public final int defaultPort; + + /** + * Note: port for connections using TLS (=SSL) immediately + * from the initial TCP connection. + * + * STARTTLS uses the defaultPort, then upgrades. + * + * See https://www.fastmail.com/help/technical/ssltlsstarttls.html. + */ + public final int defaultTlsPort; + + private Type(int defaultPort, int defaultTlsPort) { + this.defaultPort = defaultPort; + this.defaultTlsPort = defaultTlsPort; + } + } /** * Name of the store or transport type (e.g. IMAP). diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java index 006ef7ee0..8f159f8f6 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java @@ -54,6 +54,7 @@ import com.fsck.k9.mail.Part; import com.fsck.k9.mail.PushReceiver; import com.fsck.k9.mail.Pusher; import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.mail.ServerSettings.Type; import com.fsck.k9.mail.filter.EOLConvertingOutputStream; import com.fsck.k9.mail.filter.FixedLengthInputStream; import com.fsck.k9.mail.internet.MimeBodyPart; @@ -145,13 +146,13 @@ public class ImapStore extends RemoteStore { */ if (scheme.equals("imap")) { connectionSecurity = ConnectionSecurity.NONE; - port = 143; + port = Type.IMAP.defaultPort; } else if (scheme.startsWith("imap+tls")) { connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; - port = 143; + port = Type.IMAP.defaultPort; } else if (scheme.startsWith("imap+ssl")) { connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; - port = 993; + port = Type.IMAP.defaultTlsPort; } else { throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")"); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java index 37a5718f9..8d73b635f 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Store.java @@ -10,6 +10,7 @@ import com.fsck.k9.mail.filter.Hex; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.CertificateValidationException; import com.fsck.k9.mail.MessageRetrievalListener; +import com.fsck.k9.mail.ServerSettings.Type; import com.fsck.k9.mail.ssl.TrustedSocketFactory; import com.fsck.k9.mail.store.RemoteStore; import com.fsck.k9.mail.store.StoreConfig; @@ -100,13 +101,13 @@ public class Pop3Store extends RemoteStore { */ if (scheme.equals("pop3")) { connectionSecurity = ConnectionSecurity.NONE; - port = 110; + port = Type.POP3.defaultPort; } else if (scheme.startsWith("pop3+tls")) { connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; - port = 110; + port = Type.POP3.defaultPort; } else if (scheme.startsWith("pop3+ssl")) { connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; - port = 995; + port = Type.POP3.defaultTlsPort; } else { throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")"); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/transport/SmtpTransport.java b/k9mail-library/src/main/java/com/fsck/k9/mail/transport/SmtpTransport.java index 27600198b..c6292a569 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/transport/SmtpTransport.java @@ -75,13 +75,13 @@ public class SmtpTransport extends Transport { */ if (scheme.equals("smtp")) { connectionSecurity = ConnectionSecurity.NONE; - port = 587; + port = ServerSettings.Type.SMTP.defaultPort; } else if (scheme.startsWith("smtp+tls")) { connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; - port = 587; + port = ServerSettings.Type.SMTP.defaultPort; } else if (scheme.startsWith("smtp+ssl")) { connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; - port = 465; + port = ServerSettings.Type.SMTP.defaultTlsPort; } else { throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")"); } diff --git a/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java b/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java index 4b19e8759..fddb456a8 100644 --- a/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java +++ b/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java @@ -1,11 +1,14 @@ package com.fsck.k9.account; + import com.fsck.k9.Account.DeletePolicy; +import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.ServerSettings.Type; import java.util.HashMap; import java.util.Map; + /** * Deals with logic surrounding account creation. *

@@ -25,4 +28,17 @@ public class AccountCreator { return defaults.get(type); } + public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { + switch (securityType) { + case NONE: + case STARTTLS_REQUIRED: { + return storeType.defaultPort; + } + case SSL_TLS_REQUIRED: { + return storeType.defaultTlsPort; + } + } + + throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); + } } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java index ec84f663d..903dbf302 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java @@ -46,13 +46,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener private static final String STATE_SECURITY_TYPE_POSITION = "stateSecurityTypePosition"; private static final String STATE_AUTH_TYPE_POSITION = "authTypePosition"; - private static final String POP3_PORT = "110"; - private static final String POP3_SSL_PORT = "995"; - private static final String IMAP_PORT = "143"; - private static final String IMAP_SSL_PORT = "993"; - private static final String WEBDAV_PORT = "80"; - private static final String WEBDAV_SSL_PORT = "443"; - private Type mStoreType; private EditText mUsernameView; private EditText mPasswordView; @@ -79,8 +72,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener private CheckBox mCompressionOther; private CheckBox mSubscribedFoldersOnly; private AuthTypeAdapter mAuthTypeAdapter; - private String mDefaultPort = ""; - private String mDefaultSslPort = ""; private ConnectionSecurity[] mConnectionSecurityChoices = ConnectionSecurity.values(); public static void actionIncomingSettings(Activity context, Account account, boolean makeDefault) { @@ -189,8 +180,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mStoreType = settings.type; if (Type.POP3 == settings.type) { serverLabelView.setText(R.string.account_setup_incoming_pop_server_label); - mDefaultPort = POP3_PORT; - mDefaultSslPort = POP3_SSL_PORT; findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE); findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE); findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE); @@ -201,8 +190,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener mSubscribedFoldersOnly.setVisibility(View.GONE); } else if (Type.IMAP == settings.type) { serverLabelView.setText(R.string.account_setup_incoming_imap_server_label); - mDefaultPort = IMAP_PORT; - mDefaultSslPort = IMAP_SSL_PORT; ImapStoreSettings imapSettings = (ImapStoreSettings) settings; @@ -221,8 +208,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener } } else if (Type.WebDAV == settings.type) { serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label); - mDefaultPort = WEBDAV_PORT; - mDefaultSslPort = WEBDAV_SSL_PORT; mConnectionSecurityChoices = new ConnectionSecurity[] { ConnectionSecurity.NONE, ConnectionSecurity.SSL_TLS_REQUIRED }; @@ -488,27 +473,10 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener // Remove listener so as not to trigger validateFields() which is called // elsewhere as a result of user interaction. mPortView.removeTextChangedListener(validationTextWatcher); - mPortView.setText(getDefaultPort(securityType)); + mPortView.setText(String.valueOf(AccountCreator.getDefaultPort(securityType, mStoreType))); mPortView.addTextChangedListener(validationTextWatcher); } - private String getDefaultPort(ConnectionSecurity securityType) { - String port; - switch (securityType) { - case NONE: - case STARTTLS_REQUIRED: - port = mDefaultPort; - break; - case SSL_TLS_REQUIRED: - port = mDefaultSslPort; - break; - default: - Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered"); - port = ""; - } - return port; - } - private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE); } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java index 6a511ee86..6380d3e53 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java @@ -16,6 +16,7 @@ import android.widget.AdapterView.OnItemSelectedListener; import android.widget.CompoundButton.OnCheckedChangeListener; import com.fsck.k9.*; +import com.fsck.k9.account.AccountCreator; import com.fsck.k9.activity.K9Activity; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; import com.fsck.k9.helper.Utility; @@ -38,9 +39,6 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, private static final String STATE_SECURITY_TYPE_POSITION = "stateSecurityTypePosition"; private static final String STATE_AUTH_TYPE_POSITION = "authTypePosition"; - private static final String SMTP_PORT = "587"; - private static final String SMTP_SSL_PORT = "465"; - private EditText mUsernameView; private EditText mPasswordView; private ClientCertificateSpinner mClientCertificateSpinner; @@ -427,27 +425,10 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener, // Remove listener so as not to trigger validateFields() which is called // elsewhere as a result of user interaction. mPortView.removeTextChangedListener(validationTextWatcher); - mPortView.setText(getDefaultSmtpPort(securityType)); + mPortView.setText(String.valueOf(AccountCreator.getDefaultPort(securityType, Type.SMTP))); mPortView.addTextChangedListener(validationTextWatcher); } - private String getDefaultSmtpPort(ConnectionSecurity securityType) { - String port; - switch (securityType) { - case NONE: - case STARTTLS_REQUIRED: - port = SMTP_PORT; - break; - case SSL_TLS_REQUIRED: - port = SMTP_SSL_PORT; - break; - default: - port = ""; - Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered"); - } - return port; - } - private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE); }