private ImapConnection createImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, ConnectivityManager connectivityManager, OAuth2TokenProvider oAuth2TokenProvider) { return new ImapConnection(settings, socketFactory, connectivityManager, oAuth2TokenProvider, SOCKET_CONNECT_TIMEOUT, SOCKET_READ_TIMEOUT); }
private void sendDone() { try { imapConnection.setReadTimeout(RemoteStore.SOCKET_READ_TIMEOUT); imapConnection.sendContinuation("DONE"); } catch (IOException e) { imapConnection.close(); } } }
protected List<ImapMessage> getMessagesFromUids(final List<String> mesgUids) throws MessagingException { checkOpen(); Set<Long> uidSet = new HashSet<>(); for (String uid : mesgUids) { uidSet.add(Long.parseLong(uid)); } try { List<ImapResponse> imapResponses = connection.executeCommandWithIdSet("UID SEARCH UID", "", uidSet); SearchResponse searchResponse = SearchResponse.parse(imapResponses); return getMessages(searchResponse, null); } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }
private List<ImapResponse> saslAuthPlainWithLoginFallback() throws IOException, MessagingException { try { return saslAuthPlain(); } catch (AuthenticationFailedException e) { if (!isConnected()) { throw e; } return login(); } }
private void enableCompressionIfRequested() throws IOException, MessagingException { if (hasCapability(Capabilities.COMPRESS_DEFLATE) && shouldEnableCompression()) { enableCompression(); } }
@Test public void appendMessages_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); folder.open(OPEN_MODE_RW); List<ImapMessage> messages = createImapMessages("1"); when(imapConnection.readResponse()).thenReturn(createImapResponse("x OK [APPENDUID 1 23]")); folder.appendMessages(messages); verify(imapConnection).sendCommand("APPEND \"Folder\" () {0}", false); }
@Test public void sendContinuation() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); simpleOpenDialog(server, "IDLE"); server.expect("4 IDLE"); server.output("+ idling"); server.expect("DONE"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); imapConnection.sendCommand("IDLE", false); imapConnection.readResponse(); imapConnection.sendContinuation("DONE"); server.waitForInteractionToComplete(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
if (oauthTokenProvider == null) { throw new MessagingException("No OAuthToken Provider available."); } else if (hasCapability(Capabilities.AUTH_XOAUTH2) && hasCapability(Capabilities.SASL_IR)) { return authXoauth2withSASLIR(); } else { throw new MessagingException("Server doesn't support SASL XOAUTH2."); if (hasCapability(Capabilities.AUTH_CRAM_MD5)) { return authCramMD5(); } else { throw new MessagingException("Server doesn't support encrypted passwords using CRAM-MD5."); if (hasCapability(Capabilities.AUTH_PLAIN)) { return saslAuthPlainWithLoginFallback(); } else if (!hasCapability(Capabilities.LOGINDISABLED)) { return login(); } else { throw new MessagingException("Server doesn't support unencrypted passwords using AUTH=PLAIN " + if (hasCapability(Capabilities.AUTH_EXTERNAL)) { return saslAuthExternal(); } else {
@Test public void getPersonalNamespaces_withoutSpecialUseCapability_shouldUseSimpleListCommand() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); when(imapConnection.hasCapability(Capabilities.LIST_EXTENDED)).thenReturn(true); when(imapConnection.hasCapability(Capabilities.SPECIAL_USE)).thenReturn(false); imapStore.enqueueImapConnection(imapConnection); imapStore.getPersonalNamespaces(); verify(imapConnection, never()).executeSimpleCommand("LIST \"\" \"*\" RETURN (SPECIAL-USE)"); verify(imapConnection).executeSimpleCommand("LIST \"\" \"*\""); }
protected boolean hasCapability(String capability) throws IOException, MessagingException { if (!open) { open(); } return capabilities.contains(capability.toUpperCase(Locale.US)); }
ImapConnection getConnection() throws MessagingException { ImapConnection connection; while ((connection = pollConnection()) != null) { try { connection.executeSimpleCommand(Commands.NOOP); break; } catch (IOException ioe) { connection.close(); } } if (connection == null) { connection = createImapConnection(); } return connection; }
@Test public void open_withPostAuthCapabilityResponse_shouldNotIssueCapabilityCommand() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 OK [CAPABILITY IDLE]"); simplePostAuthenticationDialog(server, 3); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); assertTrue(imapConnection.isIdleCapable()); }
private void setupSearchResponses(String... responses) throws MessagingException, IOException { List<ImapResponse> imapResponses = new ArrayList<>(responses.length); for (String response : responses) { imapResponses.add(createImapResponse(response)); } when(imapConnection.executeSimpleCommand(startsWith("SEARCH"))).thenReturn(imapResponses); }
public List<ImapResponse> executeSimpleCommand(String command, boolean sensitive) throws IOException, MessagingException { String commandToLog = command; if (sensitive && !K9MailLib.isDebugSensitive()) { commandToLog = "*sensitive*"; } String tag = sendCommand(command, sensitive); try { return responseParser.readStatusResponse(tag, commandToLog, getLogId(), null); } catch (IOException e) { close(); throw e; } }
private void sendIdle(ImapConnection conn) throws MessagingException, IOException { String tag = conn.sendCommand(Commands.IDLE, false); List<ImapResponse> responses; try { try { responses = conn.readStatusResponse(tag, Commands.IDLE, this); } finally { idleStopper.stopAcceptingDoneContinuation(); } } catch (IOException e) { conn.close(); throw e; } handleUntaggedResponses(responses); }
private MessagingException handleAuthenticationFailure(NegativeImapResponseException e) { ImapResponse lastResponse = e.getLastResponse(); String responseCode = ResponseCodeExtractor.getResponseCode(lastResponse); // If there's no response code we simply assume it was an authentication failure. if (responseCode == null || responseCode.equals(ResponseCodeExtractor.AUTHENTICATION_FAILED)) { if (e.wasByeResponseReceived()) { close(); } return new AuthenticationFailedException(e.getMessage()); } else { close(); return e; } }
@Test public void open_withInvalidHostname_shouldThrow() throws Exception { settings.setHost("host name"); settings.setPort(143); ImapConnection imapConnection = createImapConnection( settings, socketFactory, connectivityManager, oAuth2TokenProvider); try { imapConnection.open(); fail("Expected exception"); } catch (UnknownHostException ignored) { } assertFalse(imapConnection.isConnected()); }
String command = String.format(Locale.US, "APPEND %s (%s) {%d}", escapedFolderName, combinedFlags, messageSize); connection.sendCommand(command, false); response = connection.readResponse(); EOLConvertingOutputStream eolOut = new EOLConvertingOutputStream(connection.getOutputStream()); message.writeTo(eolOut); eolOut.write('\r');
@Override public void expungeUids(List<String> uids) throws MessagingException { if (uids == null || uids.isEmpty()) { throw new IllegalArgumentException("expungeUids() must be called with a non-empty set of UIDs"); } open(OPEN_MODE_RW); checkOpen(); try { if (connection.isUidPlusCapable()) { Set<Long> longUids = new HashSet<>(uids.size()); for (String uid : uids) { longUids.add(Long.parseLong(uid)); } connection.executeCommandWithIdSet(Commands.UID_EXPUNGE, "", longUids); } else { executeSimpleCommand("EXPUNGE"); } } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }