Search in sources :

Example 11 with ConnectionSecurity

use of com.fsck.k9.mail.ConnectionSecurity 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)

Aggregations

ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)9 AuthType (com.fsck.k9.mail.AuthType)6 ServerSettings (com.fsck.k9.mail.ServerSettings)5 URISyntaxException (java.net.URISyntaxException)5 URI (java.net.URI)3 OnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)2 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)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 HashMap (java.util.HashMap)1 Matchers.anyString (org.mockito.Matchers.anyString)1