Search in sources :

Example 31 with ServerSettings

use of com.fsck.k9.mail.ServerSettings in project k-9 by k9mail.

the class AccountSetupOutgoing method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_setup_outgoing);
    String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    try {
        if (new URI(mAccount.getStoreUri()).getScheme().startsWith("webdav")) {
            mAccount.setTransportUri(mAccount.getStoreUri());
            AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING);
        }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mUsernameView = (EditText) findViewById(R.id.account_username);
    mPasswordView = (EditText) findViewById(R.id.account_password);
    mClientCertificateSpinner = (ClientCertificateSpinner) findViewById(R.id.account_client_certificate_spinner);
    mClientCertificateLabelView = (TextView) findViewById(R.id.account_client_certificate_label);
    mPasswordLabelView = (TextView) findViewById(R.id.account_password_label);
    mServerView = (EditText) findViewById(R.id.account_server);
    mPortView = (EditText) findViewById(R.id.account_port);
    mRequireLoginView = (CheckBox) findViewById(R.id.account_require_login);
    mRequireLoginSettingsView = (ViewGroup) findViewById(R.id.account_require_login_settings);
    mSecurityTypeView = (Spinner) findViewById(R.id.account_security_type);
    mAuthTypeView = (Spinner) findViewById(R.id.account_auth_type);
    mNextButton = (Button) findViewById(R.id.next);
    mNextButton.setOnClickListener(this);
    mSecurityTypeView.setAdapter(ConnectionSecurityAdapter.get(this));
    mAuthTypeAdapter = AuthTypeAdapter.get(this);
    mAuthTypeView.setAdapter(mAuthTypeAdapter);
    /*
         * Only allow digits in the port field.
         */
    mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    //FIXME: get Account object again?
    accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    mMakeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
    /*
         * If we're being reloaded we override the original account with the one
         * we saved
         */
    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
        accountUuid = savedInstanceState.getString(EXTRA_ACCOUNT);
        mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    }
    try {
        ServerSettings settings = Transport.decodeTransportUri(mAccount.getTransportUri());
        updateAuthPlainTextFromSecurityType(settings.connectionSecurity);
        if (savedInstanceState == null) {
            // The first item is selected if settings.authenticationType is null or is not in mAuthTypeAdapter
            mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(settings.authenticationType);
        } else {
            mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION);
        }
        mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
        updateViewFromAuthType();
        // Select currently configured security type
        if (savedInstanceState == null) {
            mCurrentSecurityTypeViewPosition = settings.connectionSecurity.ordinal();
        } else {
            /*
                 * Restore the spinner state now, before calling
                 * setOnItemSelectedListener(), thus avoiding a call to
                 * onItemSelected(). Then, when the system restores the state
                 * (again) in onRestoreInstanceState(), The system will see that
                 * the new state is the same as the current state (set here), so
                 * once again onItemSelected() will not be called.
                 */
            mCurrentSecurityTypeViewPosition = savedInstanceState.getInt(STATE_SECURITY_TYPE_POSITION);
        }
        mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
        if (settings.username != null && !settings.username.isEmpty()) {
            mUsernameView.setText(settings.username);
            mRequireLoginView.setChecked(true);
            mRequireLoginSettingsView.setVisibility(View.VISIBLE);
        }
        if (settings.password != null) {
            mPasswordView.setText(settings.password);
        }
        if (settings.clientCertificateAlias != null) {
            mClientCertificateSpinner.setAlias(settings.clientCertificateAlias);
        }
        if (settings.host != null) {
            mServerView.setText(settings.host);
        }
        if (settings.port != -1) {
            mPortView.setText(String.format("%d", settings.port));
        } else {
            updatePortFromSecurityType();
        }
        mCurrentPortViewSetting = mPortView.getText().toString();
    } catch (Exception e) {
        /*
             * We should always be able to parse our own settings.
             */
        failure(e);
    }
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException)

Example 32 with ServerSettings

use of com.fsck.k9.mail.ServerSettings in project k-9 by k9mail.

the class SmtpTransport method decodeUri.

/**
     * Decodes a SmtpTransport URI.
     *
     * NOTE: In contrast to ImapStore and Pop3Store, the authType is appended at the end!
     *
     * <p>Possible forms:</p>
     * <pre>
     * smtp://user:password:auth@server:port ConnectionSecurity.NONE
     * smtp+tls+://user:password:auth@server:port ConnectionSecurity.STARTTLS_REQUIRED
     * smtp+ssl+://user:password:auth@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     */
public static ServerSettings decodeUri(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    AuthType authType = null;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    URI smtpUri;
    try {
        smtpUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid SmtpTransport URI", use);
    }
    String scheme = smtpUri.getScheme();
    /*
         * Currently available schemes are:
         * smtp
         * smtp+tls+
         * smtp+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * smtp+tls
         * smtp+ssl
         */
    if (scheme.equals("smtp")) {
        connectionSecurity = ConnectionSecurity.NONE;
        port = ServerSettings.Type.SMTP.defaultPort;
    } else if (scheme.startsWith("smtp+tls")) {
        connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
        port = ServerSettings.Type.SMTP.defaultPort;
    } else if (scheme.startsWith("smtp+ssl")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
        port = ServerSettings.Type.SMTP.defaultTlsPort;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = smtpUri.getHost();
    if (smtpUri.getPort() != -1) {
        port = smtpUri.getPort();
    }
    if (smtpUri.getUserInfo() != null) {
        String[] userInfoParts = smtpUri.getUserInfo().split(":");
        if (userInfoParts.length == 1) {
            authType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
        } else if (userInfoParts.length == 2) {
            authType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
            password = decodeUtf8(userInfoParts[1]);
        } else if (userInfoParts.length == 3) {
            // NOTE: In SmtpTransport URIs, the authType comes last!
            authType = AuthType.valueOf(userInfoParts[2]);
            username = decodeUtf8(userInfoParts[0]);
            if (authType == AuthType.EXTERNAL) {
                clientCertificateAlias = decodeUtf8(userInfoParts[1]);
            } else {
                password = decodeUtf8(userInfoParts[1]);
            }
        }
    }
    return new ServerSettings(ServerSettings.Type.SMTP, host, port, connectionSecurity, authType, username, password, clientCertificateAlias);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) ServerSettings(com.fsck.k9.mail.ServerSettings) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 33 with ServerSettings

use of com.fsck.k9.mail.ServerSettings in project k-9 by k9mail.

the class SmtpTransport method createUri.

/**
     * Creates a SmtpTransport URI with the supplied settings.
     *
     * @param server
     *         The {@link ServerSettings} object that holds the server settings.
     *
     * @return A SmtpTransport URI that holds the same information as the {@code server} parameter.
     *
     * @see com.fsck.k9.mail.store.StoreConfig#getTransportUri()
     * @see SmtpTransport#decodeUri(String)
     */
public static String createUri(ServerSettings server) {
    String userEnc = (server.username != null) ? encodeUtf8(server.username) : "";
    String passwordEnc = (server.password != null) ? encodeUtf8(server.password) : "";
    String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ? encodeUtf8(server.clientCertificateAlias) : "";
    String scheme;
    switch(server.connectionSecurity) {
        case SSL_TLS_REQUIRED:
            scheme = "smtp+ssl+";
            break;
        case STARTTLS_REQUIRED:
            scheme = "smtp+tls+";
            break;
        default:
        case NONE:
            scheme = "smtp";
            break;
    }
    String userInfo;
    AuthType authType = server.authenticationType;
    // NOTE: authType is append at last item, in contrast to ImapStore and Pop3Store!
    if (authType != null) {
        if (AuthType.EXTERNAL == authType) {
            userInfo = userEnc + ":" + clientCertificateAliasEnc + ":" + authType.name();
        } else {
            userInfo = userEnc + ":" + passwordEnc + ":" + authType.name();
        }
    } else {
        userInfo = userEnc + ":" + passwordEnc;
    }
    try {
        return new URI(scheme, userInfo, server.host, server.port, null, null, null).toString();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Can't create SmtpTransport URI", e);
    }
}
Also used : AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 34 with ServerSettings

use of com.fsck.k9.mail.ServerSettings in project k-9 by k9mail.

the class SmtpTransportUriTest method decodeUri_canDecodePort.

@Test
public void decodeUri_canDecodePort() {
    String storeUri = "smtp://user:password:PLAIN@server:123456";
    ServerSettings result = SmtpTransport.decodeUri(storeUri);
    assertEquals(123456, result.port);
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) Test(org.junit.Test)

Example 35 with ServerSettings

use of com.fsck.k9.mail.ServerSettings in project k-9 by k9mail.

the class SmtpTransportUriTest method decodeUri_canDecodeUsername.

@Test
public void decodeUri_canDecodeUsername() {
    String storeUri = "smtp://user:password:PLAIN@server:123456";
    ServerSettings result = SmtpTransport.decodeUri(storeUri);
    assertEquals("user", result.username);
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) Test(org.junit.Test)

Aggregations

ServerSettings (com.fsck.k9.mail.ServerSettings)45 Test (org.junit.Test)34 URISyntaxException (java.net.URISyntaxException)9 AuthType (com.fsck.k9.mail.AuthType)8 URI (java.net.URI)7 HashMap (java.util.HashMap)5 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)4 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)2 Map (java.util.Map)2 SharedPreferences (android.content.SharedPreferences)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 Account (com.fsck.k9.Account)1 Preferences (com.fsck.k9.Preferences)1 Store (com.fsck.k9.mail.Store)1 RemoteStore (com.fsck.k9.mail.store.RemoteStore)1 StoreConfig (com.fsck.k9.mail.store.StoreConfig)1 ImapStoreSettings (com.fsck.k9.mail.store.imap.ImapStoreSettings)1 WebDavStoreSettings (com.fsck.k9.mail.store.webdav.WebDavStoreSettings)1 SettingsDescription (com.fsck.k9.preferences.Settings.SettingsDescription)1 ArrayList (java.util.ArrayList)1