Search in sources :

Example 1 with ImapStoreSettings

use of com.fsck.k9.mail.store.imap.ImapStoreSettings in project k-9 by k9mail.

the class ImapStoreUriDecoder method decode.

/**
     * Decodes an ImapStore URI.
     *
     * <p>Possible forms:</p>
     * <pre>
     * imap://auth:user:password@server:port ConnectionSecurity.NONE
     * imap+tls+://auth:user:password@server:port ConnectionSecurity.STARTTLS_REQUIRED
     * imap+ssl+://auth:user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     *
     * NOTE: this method expects the userinfo part of the uri to be encoded twice, due to a bug in
     * {@link ImapStoreUriCreator#create(ServerSettings)}.
     *
     * @param uri the store uri.
     */
public static ImapStoreSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    AuthType authenticationType = null;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    String pathPrefix = null;
    boolean autoDetectNamespace = true;
    URI imapUri;
    try {
        imapUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid ImapStore URI", use);
    }
    String scheme = imapUri.getScheme();
    /*
         * Currently available schemes are:
         * imap
         * imap+tls+
         * imap+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:
         * imap+tls
         * imap+ssl
         */
    if (scheme.equals("imap")) {
        connectionSecurity = ConnectionSecurity.NONE;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+tls")) {
        connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+ssl")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
        port = Type.IMAP.defaultTlsPort;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = imapUri.getHost();
    if (imapUri.getPort() != -1) {
        port = imapUri.getPort();
    }
    if (imapUri.getUserInfo() != null) {
        String userinfo = imapUri.getUserInfo();
        String[] userInfoParts = userinfo.split(":");
        if (userinfo.endsWith(":")) {
            // Or XOAUTH2 where it's a valid config - XOAUTH:username:
            if (userInfoParts.length > 1) {
                authenticationType = AuthType.valueOf(userInfoParts[0]);
                username = decodeUtf8(userInfoParts[1]);
            } else {
                authenticationType = AuthType.PLAIN;
                username = decodeUtf8(userInfoParts[0]);
            }
        } else if (userInfoParts.length == 2) {
            // Old/standard style of encoding - PLAIN auth only:
            // username:password
            authenticationType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
            password = decodeUtf8(userInfoParts[1]);
        } else if (userInfoParts.length == 3) {
            // Standard encoding
            // PLAIN:username:password
            // EXTERNAL:username:certAlias
            authenticationType = AuthType.valueOf(userInfoParts[0]);
            username = decodeUtf8(userInfoParts[1]);
            if (AuthType.EXTERNAL == authenticationType) {
                clientCertificateAlias = decodeUtf8(userInfoParts[2]);
            } else {
                password = decodeUtf8(userInfoParts[2]);
            }
        }
    }
    String path = imapUri.getPath();
    if (path != null && path.length() > 1) {
        // Strip off the leading "/"
        String cleanPath = path.substring(1);
        if (cleanPath.length() >= 2 && cleanPath.charAt(1) == '|') {
            autoDetectNamespace = cleanPath.charAt(0) == '1';
            if (!autoDetectNamespace) {
                pathPrefix = cleanPath.substring(2);
            }
        } else {
            if (cleanPath.length() > 0) {
                pathPrefix = cleanPath;
                autoDetectNamespace = false;
            }
        }
    }
    return new ImapStoreSettings(host, port, connectionSecurity, authenticationType, username, password, clientCertificateAlias, autoDetectNamespace, pathPrefix);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with ImapStoreSettings

use of com.fsck.k9.mail.store.imap.ImapStoreSettings in project k-9 by k9mail.

the class AccountSetupIncoming method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_setup_incoming);
    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);
    TextView serverLabelView = (TextView) findViewById(R.id.account_server_label);
    mServerView = (EditText) findViewById(R.id.account_server);
    mPortView = (EditText) findViewById(R.id.account_port);
    mSecurityTypeView = (Spinner) findViewById(R.id.account_security_type);
    mAuthTypeView = (Spinner) findViewById(R.id.account_auth_type);
    mImapAutoDetectNamespaceView = (CheckBox) findViewById(R.id.imap_autodetect_namespace);
    mImapPathPrefixView = (EditText) findViewById(R.id.imap_path_prefix);
    mWebdavPathPrefixView = (EditText) findViewById(R.id.webdav_path_prefix);
    mWebdavAuthPathView = (EditText) findViewById(R.id.webdav_auth_path);
    mWebdavMailboxPathView = (EditText) findViewById(R.id.webdav_mailbox_path);
    mNextButton = (Button) findViewById(R.id.next);
    mCompressionMobile = (CheckBox) findViewById(R.id.compression_mobile);
    mCompressionWifi = (CheckBox) findViewById(R.id.compression_wifi);
    mCompressionOther = (CheckBox) findViewById(R.id.compression_other);
    mSubscribedFoldersOnly = (CheckBox) findViewById(R.id.subscribed_folders_only);
    mNextButton.setOnClickListener(this);
    mImapAutoDetectNamespaceView.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mImapPathPrefixView.setEnabled(!isChecked);
            if (isChecked && mImapPathPrefixView.hasFocus()) {
                mImapPathPrefixView.focusSearch(View.FOCUS_UP).requestFocus();
            } else if (!isChecked) {
                mImapPathPrefixView.requestFocus();
            }
        }
    });
    mAuthTypeAdapter = AuthTypeAdapter.get(this);
    mAuthTypeView.setAdapter(mAuthTypeAdapter);
    /*
         * Only allow digits in the port field.
         */
    mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    String 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);
    }
    boolean editSettings = Intent.ACTION_EDIT.equals(getIntent().getAction());
    try {
        ServerSettings settings = RemoteStore.decodeStoreUri(mAccount.getStoreUri());
        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();
        if (settings.username != null) {
            mUsernameView.setText(settings.username);
        }
        if (settings.password != null) {
            mPasswordView.setText(settings.password);
        }
        if (settings.clientCertificateAlias != null) {
            mClientCertificateSpinner.setAlias(settings.clientCertificateAlias);
        }
        mStoreType = settings.type;
        if (Type.POP3 == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
            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);
            findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
            findViewById(R.id.compression_section).setVisibility(View.GONE);
            findViewById(R.id.compression_label).setVisibility(View.GONE);
            mSubscribedFoldersOnly.setVisibility(View.GONE);
        } else if (Type.IMAP == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
            ImapStoreSettings imapSettings = (ImapStoreSettings) settings;
            mImapAutoDetectNamespaceView.setChecked(imapSettings.autoDetectNamespace);
            if (imapSettings.pathPrefix != null) {
                mImapPathPrefixView.setText(imapSettings.pathPrefix);
            }
            findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
            findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
            if (!editSettings) {
                findViewById(R.id.imap_folder_setup_section).setVisibility(View.GONE);
            }
        } else if (Type.WebDAV == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
            mConnectionSecurityChoices = new ConnectionSecurity[] { ConnectionSecurity.NONE, ConnectionSecurity.SSL_TLS_REQUIRED };
            // Hide the unnecessary fields
            findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
            findViewById(R.id.account_auth_type_label).setVisibility(View.GONE);
            findViewById(R.id.account_auth_type).setVisibility(View.GONE);
            findViewById(R.id.compression_section).setVisibility(View.GONE);
            findViewById(R.id.compression_label).setVisibility(View.GONE);
            mSubscribedFoldersOnly.setVisibility(View.GONE);
            WebDavStoreSettings webDavSettings = (WebDavStoreSettings) settings;
            if (webDavSettings.path != null) {
                mWebdavPathPrefixView.setText(webDavSettings.path);
            }
            if (webDavSettings.authPath != null) {
                mWebdavAuthPathView.setText(webDavSettings.authPath);
            }
            if (webDavSettings.mailboxPath != null) {
                mWebdavMailboxPathView.setText(webDavSettings.mailboxPath);
            }
        } else {
            throw new Exception("Unknown account type: " + mAccount.getStoreUri());
        }
        if (!editSettings) {
            mAccount.setDeletePolicy(AccountCreator.getDefaultDeletePolicy(settings.type));
        }
        // Note that mConnectionSecurityChoices is configured above based on server type
        ConnectionSecurityAdapter securityTypesAdapter = ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices);
        mSecurityTypeView.setAdapter(securityTypesAdapter);
        // Select currently configured security type
        if (savedInstanceState == null) {
            mCurrentSecurityTypeViewPosition = securityTypesAdapter.getConnectionSecurityPosition(settings.connectionSecurity);
        } 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);
        updateAuthPlainTextFromSecurityType(settings.connectionSecurity);
        mCompressionMobile.setChecked(mAccount.useCompression(NetworkType.MOBILE));
        mCompressionWifi.setChecked(mAccount.useCompression(NetworkType.WIFI));
        mCompressionOther.setChecked(mAccount.useCompression(NetworkType.OTHER));
        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();
        mSubscribedFoldersOnly.setChecked(mAccount.subscribedFoldersOnly());
    } catch (Exception e) {
        failure(e);
    }
}
Also used : WebDavStoreSettings(com.fsck.k9.mail.store.webdav.WebDavStoreSettings) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ServerSettings(com.fsck.k9.mail.ServerSettings) ImapStoreSettings(com.fsck.k9.mail.store.imap.ImapStoreSettings) URISyntaxException(java.net.URISyntaxException)

Aggregations

URISyntaxException (java.net.URISyntaxException)2 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 AuthType (com.fsck.k9.mail.AuthType)1 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1 ImapStoreSettings (com.fsck.k9.mail.store.imap.ImapStoreSettings)1 WebDavStoreSettings (com.fsck.k9.mail.store.webdav.WebDavStoreSettings)1 URI (java.net.URI)1