private boolean existsNonDeletedMessageInRange(int startIndex, int endIndex, String dateSearchString) throws MessagingException, IOException { String command = String.format(Locale.US, "SEARCH %d:%d%s NOT DELETED", startIndex, endIndex, dateSearchString); List<ImapResponse> imapResponses = executeSimpleCommand(command); SearchResponse response = SearchResponse.parse(imapResponses); return response.getNumbers().size() > 0; }
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); } }
protected long getHighestUid() throws MessagingException { try { List<ImapResponse> responses = executeSimpleCommand("UID SEARCH *:*"); SearchResponse searchResponse = SearchResponse.parse(responses); return extractHighestUid(searchResponse); } catch (NegativeImapResponseException e) { return -1L; } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }
@Override public String getUidFromMessageId(String messageId) throws MessagingException { if (K9MailLib.isDebug()) { Timber.d("Looking for UID for message with message-id %s for %s", messageId, getLogId()); } String command = String.format("UID SEARCH HEADER MESSAGE-ID %s", ImapUtility.encodeString(messageId)); List<ImapResponse> imapResponses; try { imapResponses = executeSimpleCommand(command); } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } SearchResponse searchResponse = SearchResponse.parse(imapResponses); List<Long> uids = searchResponse.getNumbers(); if (uids.size() > 0) { return Long.toString(uids.get(0)); } return null; }
protected List<ImapMessage> getMessages(final Set<Long> mesgSeqs, final boolean includeDeleted, final MessageRetrievalListener<ImapMessage> listener) throws MessagingException { checkOpen(); try { String commandSuffix = includeDeleted ? "" : " NOT DELETED"; List<ImapResponse> imapResponses = connection.executeCommandWithIdSet(Commands.UID_SEARCH, commandSuffix, mesgSeqs); SearchResponse searchResponse = SearchResponse.parse(imapResponses); return getMessages(searchResponse, listener); } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }
protected List<ImapMessage> getMessages(final int start, final int end, Date earliestDate, final boolean includeDeleted, final MessageRetrievalListener<ImapMessage> listener) throws MessagingException { if (start < 1 || end < 1 || end < start) { throw new MessagingException(String.format(Locale.US, "Invalid message set %d %d", start, end)); } checkOpen(); String dateSearchString = getDateSearchString(earliestDate); String command = String.format(Locale.US, "UID SEARCH %d:%d%s%s", start, end, dateSearchString, includeDeleted ? "" : " NOT DELETED"); try { List<ImapResponse> imapResponses = connection.executeSimpleCommand(command); SearchResponse searchResponse = SearchResponse.parse(imapResponses); return getMessages(searchResponse, listener); } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }
@Test public void parse_withSingleTooShortResponse_shouldReturnEmptyList() throws Exception { List<ImapResponse> imapResponses = createImapResponseList("* SEARCH"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(Collections.emptyList(), result.getNumbers()); }
@Test public void parse_withSingleNoSearchResponse_shouldReturnEmptyList() throws Exception { List<ImapResponse> imapResponses = createImapResponseList("* 23 EXPUNGE"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(Collections.emptyList(), result.getNumbers()); }
@Test public void parse_withSingleSearchResponse_shouldExtractNumbers() throws Exception { List<ImapResponse> imapResponses = createImapResponseList( "* SEARCH 1 2 3", "* 23 EXISTS", "* SEARCH 4", "1 OK SEARCH completed"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(asList(1L, 2L, 3L, 4L), result.getNumbers()); }
@Test public void parse_withSingleTaggedSearchResponse_shouldReturnEmptyList() throws Exception { List<ImapResponse> imapResponses = createImapResponseList("x SEARCH 7 8 9"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(Collections.emptyList(), result.getNumbers()); }
@Test public void parse_withSingleSearchResponseContainingInvalidNumber_shouldReturnEmptyList() throws Exception { List<ImapResponse> imapResponses = createImapResponseList("* SEARCH A"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(Collections.emptyList(), result.getNumbers()); } }
@Test public void parse_withMultipleSearchResponses_shouldExtractNumbers() throws Exception { List<ImapResponse> imapResponses = createImapResponseList( "* SEARCH 1 2 3", "* 23 EXISTS", "* SEARCH 4", "1 OK SEARCH completed", "* SEARCH 5 6", "* 19 EXPUNGED", "* SEARCH 7", "2 OK SEARCH completed", "* SEARCH 8", "3 OK SEARCH completed"); SearchResponse result = SearchResponse.parse(imapResponses); assertNotNull(result); assertEquals(asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), result.getNumbers()); }
SearchResponse searchResponse = SearchResponse.parse(imapResponses); return getMessages(searchResponse, null); } catch (IOException ioe) {