Search in sources :

Example 1 with ImapStore

use of com.fsck.k9.mail.store.imap.ImapStore 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 ImapStore

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

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

the class ImapFolderTest method fetch_withEmptyMessageListArgument_shouldDoNothing.

@Test
public void fetch_withEmptyMessageListArgument_shouldDoNothing() throws Exception {
    ImapFolder folder = createFolder("Folder");
    FetchProfile fetchProfile = createFetchProfile();
    folder.fetch(Collections.<ImapMessage>emptyList(), fetchProfile, null);
    verifyNoMoreInteractions(imapStore);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Test(org.junit.Test)

Example 4 with ImapStore

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

the class ImapPusherTest method setUp.

@Before
public void setUp() throws Exception {
    imapStore = mock(ImapStore.class);
    PushReceiver pushReceiver = mock(PushReceiver.class);
    imapPusher = new TestImapPusher(imapStore, pushReceiver);
}
Also used : PushReceiver(com.fsck.k9.mail.PushReceiver) Before(org.junit.Before)

Example 5 with ImapStore

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

the class ImapStoreUriCreator method create.

/**
     * Creates an ImapStore URI with the supplied settings.
     *
     * @param server
     *         The {@link ServerSettings} object that holds the server settings.
     *
     * @return An ImapStore URI that holds the same information as the {@code server} parameter.
     *
     * @see com.fsck.k9.mail.store.StoreConfig#getStoreUri()
     * @see ImapStore#decodeUri(String)
     */
public static String create(ServerSettings server) {
    String userEnc = 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 = "imap+ssl+";
            break;
        case STARTTLS_REQUIRED:
            scheme = "imap+tls+";
            break;
        default:
        case NONE:
            scheme = "imap";
            break;
    }
    AuthType authType = server.authenticationType;
    String userInfo;
    if (authType == AuthType.EXTERNAL) {
        userInfo = authType.name() + ":" + userEnc + ":" + clientCertificateAliasEnc;
    } else {
        userInfo = authType.name() + ":" + userEnc + ":" + passwordEnc;
    }
    try {
        Map<String, String> extra = server.getExtra();
        String path;
        if (extra != null) {
            boolean autoDetectNamespace = Boolean.TRUE.toString().equals(extra.get(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY));
            String pathPrefix = (autoDetectNamespace) ? null : extra.get(ImapStoreSettings.PATH_PREFIX_KEY);
            path = "/" + (autoDetectNamespace ? "1" : "0") + "|" + ((pathPrefix == null) ? "" : pathPrefix);
        } else {
            path = "/1|";
        }
        return new URI(scheme, userInfo, server.host, server.port, path, null, null).toString();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Can't create ImapStore 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)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 ConnectivityManager (android.net.ConnectivityManager)2 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)2 FetchProfile (com.fsck.k9.mail.FetchProfile)2 OAuth2TokenProvider (com.fsck.k9.mail.oauth.OAuth2TokenProvider)2 Before (org.junit.Before)2 Test (org.junit.Test)2 MessagingException (com.fsck.k9.mail.MessagingException)1 PushReceiver (com.fsck.k9.mail.PushReceiver)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1 Store (com.fsck.k9.mail.Store)1 DefaultTrustedSocketFactory (com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory)1 TrustedSocketFactory (com.fsck.k9.mail.ssl.TrustedSocketFactory)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