1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Simplify code with better use of enum ConnectionSecurity

This commit is contained in:
Joe Steele 2014-02-13 19:43:24 -05:00
parent 90fedf7125
commit c7e46faf0a
8 changed files with 134 additions and 215 deletions

View File

@ -39,27 +39,13 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
private static final String EXTRA_ACCOUNT = "account"; private static final String EXTRA_ACCOUNT = "account";
private static final String EXTRA_MAKE_DEFAULT = "makeDefault"; private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
private static final int[] POP3_PORTS = { private static final String POP3_PORT = "110";
110, 995, 995, 110, 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 static final int[] IMAP_PORTS = {
143, 993, 993, 143, 143
};
private static final int[] WEBDAV_PORTS = {
80, 443, 443, 443, 443
};
private static final ConnectionSecurity[] CONNECTION_SECURITY_TYPES = {
ConnectionSecurity.NONE,
ConnectionSecurity.SSL_TLS_OPTIONAL,
ConnectionSecurity.SSL_TLS_REQUIRED,
ConnectionSecurity.STARTTLS_OPTIONAL,
ConnectionSecurity.STARTTLS_REQUIRED
};
private int[] mAccountPorts;
private String mStoreType; private String mStoreType;
private EditText mUsernameView; private EditText mUsernameView;
private EditText mPasswordView; private EditText mPasswordView;
@ -80,6 +66,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
private CheckBox mCompressionOther; private CheckBox mCompressionOther;
private CheckBox mSubscribedFoldersOnly; private CheckBox mSubscribedFoldersOnly;
private ArrayAdapter<AuthType> mAuthTypeAdapter; private ArrayAdapter<AuthType> mAuthTypeAdapter;
private String mDefaultPort = "";
private String mDefaultSslPort = "";
public static void actionIncomingSettings(Activity context, Account account, boolean makeDefault) { public static void actionIncomingSettings(Activity context, Account account, boolean makeDefault) {
Intent i = new Intent(context, AccountSetupIncoming.class); Intent i = new Intent(context, AccountSetupIncoming.class);
@ -136,18 +124,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
} }
}); });
SpinnerOption securityTypes[] = { ArrayAdapter<ConnectionSecurity> securityTypesAdapter = new ArrayAdapter<ConnectionSecurity>(this,
new SpinnerOption(0, getString(R.string.account_setup_incoming_security_none_label)), android.R.layout.simple_spinner_item, ConnectionSecurity.values());
new SpinnerOption(1,
getString(R.string.account_setup_incoming_security_ssl_optional_label)),
new SpinnerOption(2, getString(R.string.account_setup_incoming_security_ssl_label)),
new SpinnerOption(3,
getString(R.string.account_setup_incoming_security_tls_optional_label)),
new SpinnerOption(4, getString(R.string.account_setup_incoming_security_tls_label)),
};
ArrayAdapter<SpinnerOption> securityTypesAdapter = new ArrayAdapter<SpinnerOption>(this,
android.R.layout.simple_spinner_item, securityTypes);
securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSecurityTypeView.setAdapter(securityTypesAdapter); mSecurityTypeView.setAdapter(securityTypesAdapter);
@ -212,10 +190,14 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
int position = mAuthTypeAdapter.getPosition(settings.authenticationType); int position = mAuthTypeAdapter.getPosition(settings.authenticationType);
mAuthTypeView.setSelection(position, false); mAuthTypeView.setSelection(position, false);
// Select currently configured security type
mSecurityTypeView.setSelection(settings.connectionSecurity.ordinal(), false);
mStoreType = settings.type; mStoreType = settings.type;
if (Pop3Store.STORE_TYPE.equals(settings.type)) { if (Pop3Store.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_pop_server_label); serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
mAccountPorts = POP3_PORTS; mDefaultPort = POP3_PORT;
mDefaultSslPort = POP3_SSL_PORT;
findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE); findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE); findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE); findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
@ -227,7 +209,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
mAccount.setDeletePolicy(Account.DELETE_POLICY_NEVER); mAccount.setDeletePolicy(Account.DELETE_POLICY_NEVER);
} else if (ImapStore.STORE_TYPE.equals(settings.type)) { } else if (ImapStore.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_imap_server_label); serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
mAccountPorts = IMAP_PORTS; mDefaultPort = IMAP_PORT;
mDefaultSslPort = IMAP_SSL_PORT;
ImapStoreSettings imapSettings = (ImapStoreSettings) settings; ImapStoreSettings imapSettings = (ImapStoreSettings) settings;
@ -247,7 +230,8 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
} }
} else if (WebDavStore.STORE_TYPE.equals(settings.type)) { } else if (WebDavStore.STORE_TYPE.equals(settings.type)) {
serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label); serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
mAccountPorts = WEBDAV_PORTS; mDefaultPort = WEBDAV_PORT;
mDefaultSslPort = WEBDAV_SSL_PORT;
// Hide the unnecessary fields // Hide the unnecessary fields
findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE); findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
@ -275,13 +259,6 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
throw new Exception("Unknown account type: " + mAccount.getStoreUri()); throw new Exception("Unknown account type: " + mAccount.getStoreUri());
} }
// Select currently configured security type
for (int i = 0; i < CONNECTION_SECURITY_TYPES.length; i++) {
if (CONNECTION_SECURITY_TYPES[i] == settings.connectionSecurity) {
SpinnerOption.setSpinnerOptionValue(mSecurityTypeView, i);
}
}
/* /*
* Updates the port when the user changes the security type. This allows * Updates the port when the user changes the security type. This allows
* us to show a reasonable default which the user can change. * us to show a reasonable default which the user can change.
@ -340,10 +317,38 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
} }
private void updatePortFromSecurityType() { private void updatePortFromSecurityType() {
if (mAccountPorts != null) { ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();
int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value; mPortView.setText(getDefaultPort(securityType));
mPortView.setText(Integer.toString(mAccountPorts[securityType]));
} }
private String getDefaultPort(ConnectionSecurity securityType) {
String port;
switch (securityType) {
case NONE:
port = mDefaultPort;
break;
case STARTTLS_OPTIONAL:
case STARTTLS_REQUIRED:
if (WebDavStore.STORE_TYPE.equals(mStoreType)) {
/*
* The concept of STARTTLS is not really applicable for WebDav and should never
* have been made a user-selectable option. But now we must support the setting
* if it exists.
*/
port = mDefaultSslPort;
} else {
port = mDefaultPort;
}
break;
case SSL_TLS_OPTIONAL:
case SSL_TLS_REQUIRED:
port = mDefaultSslPort;
break;
default:
Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered");
port = "";
}
return port;
} }
@Override @Override
@ -389,8 +394,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
protected void onNext() { protected void onNext() {
try { try {
ConnectionSecurity connectionSecurity = CONNECTION_SECURITY_TYPES[ ConnectionSecurity connectionSecurity = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();
(Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value];
String username = mUsernameView.getText().toString(); String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString(); String password = mPasswordView.getText().toString();

View File

@ -18,6 +18,7 @@ import com.fsck.k9.activity.K9Activity;
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
@ -31,23 +32,12 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
private static final String EXTRA_MAKE_DEFAULT = "makeDefault"; private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
private static final int smtpPorts[] = { private static final String SMTP_PORT = "587";
587, 465, 465, 587, 587 private static final String SMTP_SSL_PORT = "465";
};
private static final String smtpSchemes[] = { private static final String smtpSchemes[] = {
"smtp", "smtp+ssl", "smtp+ssl+", "smtp+tls", "smtp+tls+" "smtp", "smtp+ssl", "smtp+ssl+", "smtp+tls", "smtp+tls+"
}; };
/*
private static final int webdavPorts[] =
{
80, 443, 443, 443, 443
};
private static final String webdavSchemes[] =
{
"webdav", "webdav+ssl", "webdav+ssl+", "webdav+tls", "webdav+tls+"
};
*/
private EditText mUsernameView; private EditText mUsernameView;
private EditText mPasswordView; private EditText mPasswordView;
private EditText mServerView; private EditText mServerView;
@ -111,18 +101,8 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
mNextButton.setOnClickListener(this); mNextButton.setOnClickListener(this);
mRequireLoginView.setOnCheckedChangeListener(this); mRequireLoginView.setOnCheckedChangeListener(this);
SpinnerOption securityTypes[] = { ArrayAdapter<ConnectionSecurity> securityTypesAdapter = new ArrayAdapter<ConnectionSecurity>(this,
new SpinnerOption(0, getString(R.string.account_setup_incoming_security_none_label)), android.R.layout.simple_spinner_item, ConnectionSecurity.values());
new SpinnerOption(1,
getString(R.string.account_setup_incoming_security_ssl_optional_label)),
new SpinnerOption(2, getString(R.string.account_setup_incoming_security_ssl_label)),
new SpinnerOption(3,
getString(R.string.account_setup_incoming_security_tls_optional_label)),
new SpinnerOption(4, getString(R.string.account_setup_incoming_security_tls_label)),
};
ArrayAdapter<SpinnerOption> securityTypesAdapter = new ArrayAdapter<SpinnerOption>(this,
android.R.layout.simple_spinner_item, securityTypes);
securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); securityTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSecurityTypeView.setAdapter(securityTypesAdapter); mSecurityTypeView.setAdapter(securityTypesAdapter);
@ -267,8 +247,27 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
} }
private void updatePortFromSecurityType() { private void updatePortFromSecurityType() {
int securityType = (Integer)((SpinnerOption)mSecurityTypeView.getSelectedItem()).value; ConnectionSecurity securityType = (ConnectionSecurity) mSecurityTypeView.getSelectedItem();
mPortView.setText(Integer.toString(smtpPorts[securityType])); mPortView.setText(getDefaultSmtpPort(securityType));
}
private String getDefaultSmtpPort(ConnectionSecurity securityType) {
String port;
switch (securityType) {
case NONE:
case STARTTLS_OPTIONAL:
case STARTTLS_REQUIRED:
port = SMTP_PORT;
break;
case SSL_TLS_OPTIONAL:
case SSL_TLS_REQUIRED:
port = SMTP_SSL_PORT;
break;
default:
port = "";
Log.e(K9.LOG_TAG, "Unhandled ConnectionSecurity type encountered");
}
return port;
} }
@Override @Override

View File

@ -1,19 +1,23 @@
package com.fsck.k9.mail; package com.fsck.k9.mail;
/** import com.fsck.k9.K9;
* The currently available connection security types. import com.fsck.k9.R;
*
* <p>
* Right now this enum is only used by {@link ServerSettings} and converted to store- or
* transport-specific constants in the different {@link Store} and {@link Transport}
* implementations. In the future we probably want to change this and use
* {@code ConnectionSecurity} exclusively.
* </p>
*/
public enum ConnectionSecurity { public enum ConnectionSecurity {
NONE, NONE(R.string.account_setup_incoming_security_none_label),
STARTTLS_OPTIONAL, STARTTLS_OPTIONAL(R.string.account_setup_incoming_security_tls_optional_label),
STARTTLS_REQUIRED, STARTTLS_REQUIRED(R.string.account_setup_incoming_security_tls_label),
SSL_TLS_OPTIONAL, SSL_TLS_OPTIONAL(R.string.account_setup_incoming_security_ssl_optional_label),
SSL_TLS_REQUIRED SSL_TLS_REQUIRED(R.string.account_setup_incoming_security_ssl_label);
private final int mResourceId;
private ConnectionSecurity(int id) {
mResourceId = id;
}
@Override
public String toString() {
return K9.app.getString(mResourceId);
}
} }

View File

@ -112,12 +112,6 @@ import com.jcraft.jzlib.ZOutputStream;
public class ImapStore extends Store { public class ImapStore extends Store {
public static final String STORE_TYPE = "IMAP"; public static final String STORE_TYPE = "IMAP";
public static final int CONNECTION_SECURITY_NONE = 0;
public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
private static final int IDLE_READ_TIMEOUT_INCREMENT = 5 * 60 * 1000; private static final int IDLE_READ_TIMEOUT_INCREMENT = 5 * 60 * 1000;
private static final int IDLE_FAILURE_COUNT_LIMIT = 10; private static final int IDLE_FAILURE_COUNT_LIMIT = 10;
private static int MAX_DELAY_TIME = 5 * 60 * 1000; // 5 minutes private static int MAX_DELAY_TIME = 5 * 60 * 1000; // 5 minutes
@ -355,7 +349,7 @@ public class ImapStore extends Store {
private int mPort; private int mPort;
private String mUsername; private String mUsername;
private String mPassword; private String mPassword;
private int mConnectionSecurity; private ConnectionSecurity mConnectionSecurity;
private AuthType mAuthType; private AuthType mAuthType;
private volatile String mPathPrefix; private volatile String mPathPrefix;
private volatile String mCombinedPrefix = null; private volatile String mCombinedPrefix = null;
@ -374,7 +368,7 @@ public class ImapStore extends Store {
} }
@Override @Override
public int getConnectionSecurity() { public ConnectionSecurity getConnectionSecurity() {
return mConnectionSecurity; return mConnectionSecurity;
} }
@ -460,23 +454,7 @@ public class ImapStore extends Store {
mHost = settings.host; mHost = settings.host;
mPort = settings.port; mPort = settings.port;
switch (settings.connectionSecurity) { mConnectionSecurity = settings.connectionSecurity;
case NONE:
mConnectionSecurity = CONNECTION_SECURITY_NONE;
break;
case STARTTLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
break;
case STARTTLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
break;
case SSL_TLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
break;
case SSL_TLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
break;
}
mAuthType = settings.authenticationType; mAuthType = settings.authenticationType;
mUsername = settings.username; mUsername = settings.username;
@ -2427,7 +2405,7 @@ public class ImapStore extends Store {
} }
try { try {
int connectionSecurity = mSettings.getConnectionSecurity(); ConnectionSecurity connectionSecurity = mSettings.getConnectionSecurity();
// Try all IPv4 and IPv6 addresses of the host // Try all IPv4 and IPv6 addresses of the host
InetAddress[] addresses = InetAddress.getAllByName(mSettings.getHost()); InetAddress[] addresses = InetAddress.getAllByName(mSettings.getHost());
@ -2441,10 +2419,10 @@ public class ImapStore extends Store {
SocketAddress socketAddress = new InetSocketAddress(addresses[i], SocketAddress socketAddress = new InetSocketAddress(addresses[i],
mSettings.getPort()); mSettings.getPort());
if (connectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || if (connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
connectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) { connectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = connectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; boolean secure = connectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
sslContext sslContext
.init(null, .init(null,
new TrustManager[] { TrustManagerFactory.get( new TrustManager[] { TrustManagerFactory.get(
@ -2494,15 +2472,15 @@ public class ImapStore extends Store {
} }
} }
if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_OPTIONAL if (mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_OPTIONAL
|| mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED) { || mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) {
if (hasCapability("STARTTLS")) { if (hasCapability("STARTTLS")) {
// STARTTLS // STARTTLS
executeSimpleCommand("STARTTLS"); executeSimpleCommand("STARTTLS");
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED; boolean secure = mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED;
sslContext.init(null, sslContext.init(null,
new TrustManager[] { TrustManagerFactory.get( new TrustManager[] { TrustManagerFactory.get(
mSettings.getHost(), mSettings.getHost(),
@ -2523,7 +2501,7 @@ public class ImapStore extends Store {
if (responses.size() != 2) { if (responses.size() != 2) {
throw new MessagingException("Invalid CAPABILITY response received"); throw new MessagingException("Invalid CAPABILITY response received");
} }
} else if (mSettings.getConnectionSecurity() == CONNECTION_SECURITY_TLS_REQUIRED) { } else if (mSettings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) {
throw new MessagingException("TLS not supported but required"); throw new MessagingException("TLS not supported but required");
} }
} }

View File

@ -37,12 +37,6 @@ import java.util.Set;
public class Pop3Store extends Store { public class Pop3Store extends Store {
public static final String STORE_TYPE = "POP3"; public static final String STORE_TYPE = "POP3";
public static final int CONNECTION_SECURITY_NONE = 0;
public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
private static final String STLS_COMMAND = "STLS"; private static final String STLS_COMMAND = "STLS";
private static final String USER_COMMAND = "USER"; private static final String USER_COMMAND = "USER";
private static final String PASS_COMMAND = "PASS"; private static final String PASS_COMMAND = "PASS";
@ -200,7 +194,7 @@ public class Pop3Store extends Store {
private String mUsername; private String mUsername;
private String mPassword; private String mPassword;
private AuthType mAuthType; private AuthType mAuthType;
private int mConnectionSecurity; private ConnectionSecurity mConnectionSecurity;
private HashMap<String, Folder> mFolders = new HashMap<String, Folder>(); private HashMap<String, Folder> mFolders = new HashMap<String, Folder>();
private Pop3Capabilities mCapabilities; private Pop3Capabilities mCapabilities;
@ -225,23 +219,7 @@ public class Pop3Store extends Store {
mHost = settings.host; mHost = settings.host;
mPort = settings.port; mPort = settings.port;
switch (settings.connectionSecurity) { mConnectionSecurity = settings.connectionSecurity;
case NONE:
mConnectionSecurity = CONNECTION_SECURITY_NONE;
break;
case STARTTLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
break;
case STARTTLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
break;
case SSL_TLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
break;
case SSL_TLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
break;
}
mUsername = settings.username; mUsername = settings.username;
mPassword = settings.password; mPassword = settings.password;
@ -321,10 +299,10 @@ public class Pop3Store extends Store {
try { try {
SocketAddress socketAddress = new InetSocketAddress(mHost, mPort); SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || if (mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) { mConnectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
final boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; final boolean secure = mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
sslContext.init(null, sslContext.init(null,
new TrustManager[] { TrustManagerFactory.get(mHost, new TrustManager[] { TrustManagerFactory.get(mHost,
mPort, secure) }, new SecureRandom()); mPort, secure) }, new SecureRandom());
@ -345,14 +323,14 @@ public class Pop3Store extends Store {
String serverGreeting = executeSimpleCommand(null); String serverGreeting = executeSimpleCommand(null);
mCapabilities = getCapabilities(); mCapabilities = getCapabilities();
if (mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL if (mConnectionSecurity == ConnectionSecurity.STARTTLS_OPTIONAL
|| mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) { || mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
if (mCapabilities.stls) { if (mCapabilities.stls) {
executeSimpleCommand(STLS_COMMAND); executeSimpleCommand(STLS_COMMAND);
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED; boolean secure = mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED;
sslContext.init(null, sslContext.init(null,
new TrustManager[] { TrustManagerFactory.get( new TrustManager[] { TrustManagerFactory.get(
mHost, mPort, secure) }, mHost, mPort, secure) },
@ -366,7 +344,7 @@ public class Pop3Store extends Store {
throw new MessagingException("Unable to connect socket"); throw new MessagingException("Unable to connect socket");
} }
mCapabilities = getCapabilities(); mCapabilities = getCapabilities();
} else if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) { } else if (mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
throw new MessagingException("TLS not supported but required"); throw new MessagingException("TLS not supported but required");
} }
} }

View File

@ -57,13 +57,6 @@ import java.util.zip.GZIPInputStream;
public class WebDavStore extends Store { public class WebDavStore extends Store {
public static final String STORE_TYPE = "WebDAV"; public static final String STORE_TYPE = "WebDAV";
// Security options
private static final short CONNECTION_SECURITY_NONE = 0;
private static final short CONNECTION_SECURITY_TLS_OPTIONAL = 1;
private static final short CONNECTION_SECURITY_TLS_REQUIRED = 2;
private static final short CONNECTION_SECURITY_SSL_OPTIONAL = 3;
private static final short CONNECTION_SECURITY_SSL_REQUIRED = 4;
// Authentication types // Authentication types
private static final short AUTH_TYPE_NONE = 0; private static final short AUTH_TYPE_NONE = 0;
private static final short AUTH_TYPE_BASIC = 1; private static final short AUTH_TYPE_BASIC = 1;
@ -298,7 +291,7 @@ public class WebDavStore extends Store {
} }
private short mConnectionSecurity; private ConnectionSecurity mConnectionSecurity;
private String mUsername; /* Stores the username for authentications */ private String mUsername; /* Stores the username for authentications */
private String mAlias; /* Stores the alias for the user's mailbox */ private String mAlias; /* Stores the alias for the user's mailbox */
private String mPassword; /* Stores the password for authentications */ private String mPassword; /* Stores the password for authentications */
@ -334,23 +327,7 @@ public class WebDavStore extends Store {
mHost = settings.host; mHost = settings.host;
mPort = settings.port; mPort = settings.port;
switch (settings.connectionSecurity) { mConnectionSecurity = settings.connectionSecurity;
case NONE:
mConnectionSecurity = CONNECTION_SECURITY_NONE;
break;
case STARTTLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
break;
case STARTTLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
break;
case SSL_TLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
break;
case SSL_TLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
break;
}
mUsername = settings.username; mUsername = settings.username;
mPassword = settings.password; mPassword = settings.password;
@ -383,16 +360,16 @@ public class WebDavStore extends Store {
// The inbox path would look like: "https://mail.domain.com/Exchange/alias/Inbox". // The inbox path would look like: "https://mail.domain.com/Exchange/alias/Inbox".
mUrl = getRoot() + mPath + mMailboxPath; mUrl = getRoot() + mPath + mMailboxPath;
mSecure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; mSecure = mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
mAuthString = "Basic " + Utility.base64Encode(mUsername + ":" + mPassword); mAuthString = "Basic " + Utility.base64Encode(mUsername + ":" + mPassword);
} }
private String getRoot() { private String getRoot() {
String root; String root;
if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED || if (mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED ||
mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL || mConnectionSecurity == ConnectionSecurity.STARTTLS_OPTIONAL ||
mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) { mConnectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
root = "https"; root = "https";
} else { } else {
root = "http"; root = "http";

View File

@ -33,12 +33,6 @@ import java.util.*;
public class SmtpTransport extends Transport { public class SmtpTransport extends Transport {
public static final String TRANSPORT_TYPE = "SMTP"; public static final String TRANSPORT_TYPE = "SMTP";
public static final int CONNECTION_SECURITY_NONE = 0;
public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
/** /**
* Decodes a SmtpTransport URI. * Decodes a SmtpTransport URI.
* *
@ -175,7 +169,7 @@ public class SmtpTransport extends Transport {
String mUsername; String mUsername;
String mPassword; String mPassword;
AuthType mAuthType; AuthType mAuthType;
int mConnectionSecurity; ConnectionSecurity mConnectionSecurity;
Socket mSocket; Socket mSocket;
PeekableInputStream mIn; PeekableInputStream mIn;
OutputStream mOut; OutputStream mOut;
@ -193,23 +187,7 @@ public class SmtpTransport extends Transport {
mHost = settings.host; mHost = settings.host;
mPort = settings.port; mPort = settings.port;
switch (settings.connectionSecurity) { mConnectionSecurity = settings.connectionSecurity;
case NONE:
mConnectionSecurity = CONNECTION_SECURITY_NONE;
break;
case STARTTLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
break;
case STARTTLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
break;
case SSL_TLS_OPTIONAL:
mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
break;
case SSL_TLS_REQUIRED:
mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
break;
}
mAuthType = settings.authenticationType; mAuthType = settings.authenticationType;
mUsername = settings.username; mUsername = settings.username;
@ -223,10 +201,10 @@ public class SmtpTransport extends Transport {
for (int i = 0; i < addresses.length; i++) { for (int i = 0; i < addresses.length; i++) {
try { try {
SocketAddress socketAddress = new InetSocketAddress(addresses[i], mPort); SocketAddress socketAddress = new InetSocketAddress(addresses[i], mPort);
if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || if (mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED ||
mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) { mConnectionSecurity == ConnectionSecurity.SSL_TLS_OPTIONAL) {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; boolean secure = mConnectionSecurity == ConnectionSecurity.SSL_TLS_REQUIRED;
sslContext.init(null, sslContext.init(null,
new TrustManager[] { TrustManagerFactory.get( new TrustManager[] { TrustManagerFactory.get(
mHost, mPort, secure) }, mHost, mPort, secure) },
@ -280,13 +258,13 @@ public class SmtpTransport extends Transport {
m8bitEncodingAllowed = extensions.containsKey("8BITMIME"); m8bitEncodingAllowed = extensions.containsKey("8BITMIME");
if (mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL if (mConnectionSecurity == ConnectionSecurity.STARTTLS_OPTIONAL
|| mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) { || mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
if (extensions.containsKey("STARTTLS")) { if (extensions.containsKey("STARTTLS")) {
executeSimpleCommand("STARTTLS"); executeSimpleCommand("STARTTLS");
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED; boolean secure = mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED;
sslContext.init(null, sslContext.init(null,
new TrustManager[] { TrustManagerFactory.get(mHost, new TrustManager[] { TrustManagerFactory.get(mHost,
mPort, secure) }, new SecureRandom()); mPort, secure) }, new SecureRandom());
@ -300,7 +278,7 @@ public class SmtpTransport extends Transport {
* Exim. * Exim.
*/ */
extensions = sendHello(localHost); extensions = sendHello(localHost);
} else if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) { } else if (mConnectionSecurity == ConnectionSecurity.STARTTLS_REQUIRED) {
throw new MessagingException("TLS not supported but required"); throw new MessagingException("TLS not supported but required");
} }
} }

View File

@ -1,6 +1,7 @@
package com.fsck.k9.mail.transport.imap; package com.fsck.k9.mail.transport.imap;
import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.store.ImapStore; import com.fsck.k9.mail.store.ImapStore;
import com.fsck.k9.mail.store.ImapStore.ImapConnection; import com.fsck.k9.mail.store.ImapStore.ImapConnection;
@ -12,7 +13,7 @@ public interface ImapSettings {
int getPort(); int getPort();
int getConnectionSecurity(); ConnectionSecurity getConnectionSecurity();
AuthType getAuthType(); AuthType getAuthType();