From 810d0cf6b43f5679f6519dd489810cc3a9c13f43 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 8 Mar 2015 11:05:53 +0000 Subject: [PATCH 1/5] Move default port and default TLS port to a single location --- .../java/com/fsck/k9/mail/ServerSettings.java | 25 +++++++++++++++- .../fsck/k9/mail/store/imap/ImapStore.java | 7 +++-- .../fsck/k9/mail/store/pop3/Pop3Store.java | 7 +++-- .../fsck/k9/mail/transport/SmtpTransport.java | 6 ++-- .../activity/setup/AccountSetupIncoming.java | 30 ++++--------------- .../activity/setup/AccountSetupOutgoing.java | 18 ++++------- 6 files changed, 45 insertions(+), 48 deletions(-) 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..37bef8fc7 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/activity/setup/AccountSetupIncoming.java b/k9mail/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java index ec84f663d..23992d521 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,25 +473,20 @@ 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(getDefaultPort(securityType))); mPortView.addTextChangedListener(validationTextWatcher); } - private String getDefaultPort(ConnectionSecurity securityType) { - String port; + private int getDefaultPort(ConnectionSecurity securityType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: - port = mDefaultPort; - break; + return mStoreType.defaultPort; case SSL_TLS_REQUIRED: - port = mDefaultSslPort; - break; + return mStoreType.defaultTlsPort; default: - Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered"); - port = ""; + throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } - return port; } private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { 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..20a948ac0 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 @@ -38,9 +38,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,25 +424,20 @@ 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(getDefaultSmtpPort(securityType))); mPortView.addTextChangedListener(validationTextWatcher); } - private String getDefaultSmtpPort(ConnectionSecurity securityType) { - String port; + private int getDefaultSmtpPort(ConnectionSecurity securityType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: - port = SMTP_PORT; - break; + return Type.SMTP.defaultPort; case SSL_TLS_REQUIRED: - port = SMTP_SSL_PORT; - break; + return Type.SMTP.defaultTlsPort; default: - port = ""; - Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered"); + throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } - return port; } private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { From d0cd7c368d3d5c7a4f6b40e8257dec2986306f2e Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 8 Mar 2015 11:07:33 +0000 Subject: [PATCH 2/5] formatting --- .../src/main/java/com/fsck/k9/mail/ServerSettings.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 37bef8fc7..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 @@ -20,9 +20,9 @@ public class ServerSettings { public enum Type { IMAP(143, 993), - SMTP(587,465), - WebDAV(80,443), - POP3(110,995); + SMTP(587, 465), + WebDAV(80, 443), + POP3(110, 995); public final int defaultPort; From 520e327775478ef282c5716de7b07b11f806a085 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 8 Mar 2015 11:26:54 +0000 Subject: [PATCH 3/5] remove duplication --- .../java/com/fsck/k9/account/AccountCreator.java | 13 +++++++++++++ .../k9/activity/setup/AccountSetupIncoming.java | 14 +------------- .../k9/activity/setup/AccountSetupOutgoing.java | 15 ++------------- 3 files changed, 16 insertions(+), 26 deletions(-) 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..54056c71b 100644 --- a/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java +++ b/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java @@ -1,6 +1,7 @@ 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; @@ -25,4 +26,16 @@ 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; + default: + 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 23992d521..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 @@ -473,22 +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(String.valueOf(getDefaultPort(securityType))); + mPortView.setText(String.valueOf(AccountCreator.getDefaultPort(securityType, mStoreType))); mPortView.addTextChangedListener(validationTextWatcher); } - private int getDefaultPort(ConnectionSecurity securityType) { - switch (securityType) { - case NONE: - case STARTTLS_REQUIRED: - return mStoreType.defaultPort; - case SSL_TLS_REQUIRED: - return mStoreType.defaultTlsPort; - default: - throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); - } - } - 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 20a948ac0..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; @@ -424,22 +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(String.valueOf(getDefaultSmtpPort(securityType))); + mPortView.setText(String.valueOf(AccountCreator.getDefaultPort(securityType, Type.SMTP))); mPortView.addTextChangedListener(validationTextWatcher); } - private int getDefaultSmtpPort(ConnectionSecurity securityType) { - switch (securityType) { - case NONE: - case STARTTLS_REQUIRED: - return Type.SMTP.defaultPort; - case SSL_TLS_REQUIRED: - return Type.SMTP.defaultTlsPort; - default: - throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); - } - } - private void updateAuthPlainTextFromSecurityType(ConnectionSecurity securityType) { mAuthTypeAdapter.useInsecureText(securityType == ConnectionSecurity.NONE); } From 855da35f3adf106acc97a18a47036bd210d03310 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 16 Mar 2015 13:06:40 +0100 Subject: [PATCH 4/5] Code style fixes --- .../com/fsck/k9/account/AccountCreator.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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 54056c71b..61db90546 100644 --- a/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java +++ b/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java @@ -1,5 +1,6 @@ package com.fsck.k9.account; + import com.fsck.k9.Account.DeletePolicy; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.ServerSettings.Type; @@ -7,6 +8,7 @@ import com.fsck.k9.mail.ServerSettings.Type; import java.util.HashMap; import java.util.Map; + /** * Deals with logic surrounding account creation. *

@@ -28,14 +30,16 @@ public class AccountCreator { 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; - default: - throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); + case NONE: + case STARTTLS_REQUIRED: { + return storeType.defaultPort; + } + case SSL_TLS_REQUIRED: { + return storeType.defaultTlsPort; + } + default: { + throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); + } } } - } From 00528f5d24d176c980771330fe95a1034461c903 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 16 Mar 2015 13:11:01 +0100 Subject: [PATCH 5/5] Move throw statement outside of switch body This way static analysis can detect when we're missing a switch case. --- k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 61db90546..fddb456a8 100644 --- a/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java +++ b/k9mail/src/main/java/com/fsck/k9/account/AccountCreator.java @@ -37,9 +37,8 @@ public class AccountCreator { case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } - default: { - throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); - } } + + throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } }