Search in sources :

Example 1 with Pop3Store

use of com.fsck.k9.mail.store.pop3.Pop3Store in project k-9 by k9mail.

the class RemoteStore method getInstance.

/**
     * Get an instance of a remote mail store.
     */
public static synchronized Store getInstance(Context context, StoreConfig storeConfig) throws MessagingException {
    String uri = storeConfig.getStoreUri();
    if (uri.startsWith("local")) {
        throw new RuntimeException("Asked to get non-local Store object but given LocalStore URI");
    }
    Store store = sStores.get(uri);
    if (store == null) {
        if (uri.startsWith("imap")) {
            OAuth2TokenProvider oAuth2TokenProvider = null;
            store = new ImapStore(storeConfig, new DefaultTrustedSocketFactory(context), (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE), oAuth2TokenProvider);
        } else if (uri.startsWith("pop3")) {
            store = new Pop3Store(storeConfig, new DefaultTrustedSocketFactory(context));
        } else if (uri.startsWith("webdav")) {
            store = new WebDavStore(storeConfig, new WebDavHttpClient.WebDavHttpClientFactory());
        }
        if (store != null) {
            sStores.put(uri, store);
        }
    }
    if (store == null) {
        throw new MessagingException("Unable to locate an applicable Store for " + uri);
    }
    return store;
}
Also used : Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) DefaultTrustedSocketFactory(com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory) MessagingException(com.fsck.k9.mail.MessagingException) OAuth2TokenProvider(com.fsck.k9.mail.oauth.OAuth2TokenProvider) ConnectivityManager(android.net.ConnectivityManager) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Store(com.fsck.k9.mail.Store) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore)

Example 2 with Pop3Store

use of com.fsck.k9.mail.store.pop3.Pop3Store 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 3 with Pop3Store

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

Aggregations

AuthType (com.fsck.k9.mail.AuthType)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ConnectivityManager (android.net.ConnectivityManager)1 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)1 MessagingException (com.fsck.k9.mail.MessagingException)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1 Store (com.fsck.k9.mail.Store)1 OAuth2TokenProvider (com.fsck.k9.mail.oauth.OAuth2TokenProvider)1 DefaultTrustedSocketFactory (com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory)1 ImapStore (com.fsck.k9.mail.store.imap.ImapStore)1 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)1 WebDavStore (com.fsck.k9.mail.store.webdav.WebDavStore)1