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; }
public static SearchResponse parse(List<ImapResponse> responses) { List<Long> numbers = new ArrayList<>(); for (ImapResponse response : responses) { parseSingleLine(response, numbers); } return new SearchResponse(numbers); }
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 long extractHighestUid(SearchResponse searchResponse) { List<Long> uids = searchResponse.getNumbers(); if (uids.isEmpty()) { return -1L; } if (uids.size() == 1) { return uids.get(0); } Collections.sort(uids, Collections.reverseOrder()); return uids.get(0); }
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); } }
private List<ImapMessage> getMessages(SearchResponse searchResponse, MessageRetrievalListener<ImapMessage> listener) throws MessagingException { List<ImapMessage> messages = new ArrayList<>(); List<Long> uids = searchResponse.getNumbers(); // Sort the uids in numerically decreasing order // By doing it in decreasing order, we ensure newest messages are dealt with first // This makes the most sense when a limit is imposed, and also prevents UI from going // crazy adding stuff at the top. Collections.sort(uids, Collections.reverseOrder()); for (int i = 0, count = uids.size(); i < count; i++) { String uid = uids.get(i).toString(); if (listener != null) { listener.messageStarted(uid, i, count); } ImapMessage message = new ImapMessage(uid, this); messages.add(message); if (listener != null) { listener.messageFinished(message, i, count); } } return messages; }
@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); } }
@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()); }
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_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()); }
SearchResponse searchResponse = SearchResponse.parse(imapResponses); return getMessages(searchResponse, null); } catch (IOException 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_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()); }