public boolean wasByeResponseReceived() { for (ImapResponse response : responses) { if (response.getTag() == null && response.size() >= 1 && equalsIgnoreCase(response.get(0), Responses.BYE)) { return true; } } return false; }
private boolean isListResponse(ImapResponse response) { boolean responseTooShort = response.size() < 4; if (responseTooShort) { return false; } boolean isListResponse = equalsIgnoreCase(response.get(0), Responses.LIST); boolean hierarchyDelimiterValid = response.get(2) instanceof String; return isListResponse && hierarchyDelimiterValid; }
@Override public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws MessagingException, IOException { if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) { ImapList fetchList = (ImapList)response.getKeyedValue("FETCH"); String uid = fetchList.getKeyedString("UID"); ImapMessage message = (ImapMessage) mMessageMap.get(uid); message.parse(literal); // Return placeholder object return 1; } return null; } }
private static void parseUidCopyResponse(ImapResponse response, Map<String, String> uidMappingOutput) { if (!response.isTagged() || response.size() < 2 || !equalsIgnoreCase(response.get(0), Responses.OK) || !response.isList(1)) { return; } ImapList responseTextList = response.getList(1); if (responseTextList.size() < 4 || !equalsIgnoreCase(responseTextList.get(0), Responses.COPYUID) || !responseTextList.isString(1) || !responseTextList.isString(2) || !responseTextList.isString(3)) { return; } List<String> sourceUids = getImapSequenceValues(responseTextList.getString(2)); List<String> destinationUids = getImapSequenceValues(responseTextList.getString(3)); int size = sourceUids.size(); if (size == 0 || size != destinationUids.size()) { return; } for (int i = 0; i < size; i++) { String sourceUid = sourceUids.get(i); String destinationUid = destinationUids.get(i); uidMappingOutput.put(sourceUid, destinationUid); } }
public static CapabilityResponse parse(List<ImapResponse> responses) { for (ImapResponse response : responses) { CapabilityResponse result; if (!response.isEmpty() && equalsIgnoreCase(response.get(0), Responses.OK) && response.isList(1)) { ImapList capabilityList = response.getList(1); result = parse(capabilityList); } else if (response.getTag() == null) { result = parse(response); } else { result = null; } if (result != null) { return result; } } return null; }
private static ListResponse parseSingleLine(ImapResponse response, String commandResponse) { if (response.size() < 4 || !equalsIgnoreCase(response.get(0), commandResponse)) { return null; } // We have special support for LIST responses in ImapResponseParser so we can relax the length/type checks here List<String> attributes = extractAttributes(response); if (attributes == null) { return null; } String hierarchyDelimiter = response.getString(2); if (hierarchyDelimiter != null && hierarchyDelimiter.length() != 1) { return null; } String name = response.getString(3); return new ListResponse(attributes, hierarchyDelimiter, name); }
private static void parseSingleLine(ImapResponse response, List<Long> numbers) { if (response.isTagged() || response.size() < 2 || !equalsIgnoreCase(response.get(0), Responses.SEARCH)) { return; } int end = response.size(); for (int i = 1; i < end; i++) { try { long number = response.getLong(i); numbers.add(number); } catch (NumberFormatException e) { return; } } }
public static SelectOrExamineResponse parse(ImapResponse response) { if (!response.isTagged() || !equalsIgnoreCase(response.get(0), Responses.OK)) { return null; } if (!response.isList(1)) { return noOpenModeInResponse(); } ImapList responseTextList = response.getList(1); if (!responseTextList.isString(0)) { return noOpenModeInResponse(); } String responseCode = responseTextList.getString(0); if ("READ-ONLY".equalsIgnoreCase(responseCode)) { return new SelectOrExamineResponse(false); } else if ("READ-WRITE".equalsIgnoreCase(responseCode)) { return new SelectOrExamineResponse(true); } return noOpenModeInResponse(); }
@Override public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws IOException { if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) { //TODO: check for correct UID String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0]; String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; return bodyFactory.createBody(contentTransferEncoding, contentType, literal); } return null; } }
private List<ImapResponse> authCramMD5() throws MessagingException, IOException { String command = Commands.AUTHENTICATE_CRAM_MD5; String tag = sendCommand(command, false); ImapResponse response = readContinuationResponse(tag); if (response.size() != 1 || !(response.get(0) instanceof String)) { throw new MessagingException("Invalid Cram-MD5 nonce received"); } byte[] b64Nonce = response.getString(0).getBytes(); byte[] b64CRAM = Authentication.computeCramMd5Bytes(settings.getUsername(), settings.getPassword(), b64Nonce); outputStream.write(b64CRAM); outputStream.write('\r'); outputStream.write('\n'); outputStream.flush(); try { return responseParser.readStatusResponse(tag, command, getLogId(), null); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }
private int getRemoteMessageCount(String criteria) throws MessagingException { checkOpen(); try { int count = 0; int start = 1; String command = String.format(Locale.US, "SEARCH %d:* %s", start, criteria); List<ImapResponse> responses = executeSimpleCommand(command); for (ImapResponse response : responses) { if (ImapResponseParser.equalsIgnoreCase(response.get(0), "SEARCH")) { count += response.size() - 1; } } return count; } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } }
@Test public void readResponse_withListResponseContainingNil() throws Exception { ImapResponseParser parser = createParser("* LIST (\\NoInferiors) NIL INBOX\r\n"); ImapResponse response = parser.readResponse(); assertEquals(4, response.size()); assertEquals("LIST", response.get(0)); assertEquals(1, response.getList(1).size()); assertEquals("\\NoInferiors", response.getList(1).getString(0)); assertEquals(null, response.get(2)); assertEquals("INBOX", response.get(3)); }
@Override protected void handleUntaggedResponse(ImapResponse response) { if (response.getTag() == null && response.size() > 1) { Object responseType = response.get(1); if (equalsIgnoreCase(responseType, "FETCH") || equalsIgnoreCase(responseType, "EXPUNGE") || equalsIgnoreCase(responseType, "EXISTS")) { if (K9MailLib.isDebug()) { Timber.d("Storing response %s for later processing", response); } synchronized (storedUntaggedResponses) { storedUntaggedResponses.add(response); } } handlePossibleUidNext(response); } }
@Test public void testOkResponseWithText() throws IOException { ImapResponseParser parser = createParser("* OK Some text here\r\n"); ImapResponse response = parser.readResponse(); assertNotNull(response); assertEquals(2, response.size()); assertEquals("OK", response.get(0)); assertEquals("Some text here", response.get(1)); }
@Test public void testListResponseContainingFolderNameWithBrackets() throws Exception { ImapResponseParser parser = createParser("* LIST (\\HasNoChildren) \".\" [FolderName]\r\n"); ImapResponse response = parser.readResponse(); assertEquals(4, response.size()); assertEquals("LIST", response.get(0)); assertEquals(1, response.getList(1).size()); assertEquals("\\HasNoChildren", response.getList(1).getString(0)); assertEquals(".", response.get(2)); assertEquals("[FolderName]", response.get(3)); }
@Test public void testOkResponseWithRespTextCodeAndText() throws IOException { ImapResponseParser parser = createParser("* OK [token1 token2] {x} test [...]\r\n"); ImapResponse response = parser.readResponse(); assertNotNull(response); assertEquals(3, response.size()); assertEquals("OK", response.get(0)); assertTrue(response.get(1) instanceof ImapList); assertEquals("{x} test [...]", response.get(2)); ImapList respTextCode = (ImapList) response.get(1); assertEquals(2, respTextCode.size()); assertEquals("token1", respTextCode.get(0)); assertEquals("token2", respTextCode.get(1)); }
static NamespaceResponse parse(ImapResponse response) { if (response.size() < 4 || !equalsIgnoreCase(response.get(0), Responses.NAMESPACE)) { return null; } if (!response.isList(1)) { return null; } ImapList personalNamespaces = response.getList(1); if (!personalNamespaces.isList(0)) { return null; } ImapList firstPersonalNamespace = personalNamespaces.getList(0); if (!firstPersonalNamespace.isString(0) || !firstPersonalNamespace.isString(1)) { return null; } String prefix = firstPersonalNamespace.getString(0); String hierarchyDelimiter = firstPersonalNamespace.getString(1); return new NamespaceResponse(prefix, hierarchyDelimiter); }
@Test public void testOkResponseWithRespTextCode() throws IOException { ImapResponseParser parser = createParser("* OK [UIDVALIDITY 3857529045]\r\n"); ImapResponse response = parser.readResponse(); assertNotNull(response); assertEquals(2, response.size()); assertEquals("OK", response.get(0)); assertTrue(response.get(1) instanceof ImapList); ImapList respTextCode = (ImapList) response.get(1); assertEquals(2, respTextCode.size()); assertEquals("UIDVALIDITY", respTextCode.get(0)); assertEquals("3857529045", respTextCode.get(1)); }
@Test public void testRespTextCodeWithList() throws Exception { ImapResponseParser parser = createParser("* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Deleted \\Seen " + "\\Draft NonJunk $MDNSent \\*)] Flags permitted.\r\n"); ImapResponse response = parser.readResponse(); assertEquals(3, response.size()); assertTrue(response.get(1) instanceof ImapList); assertEquals(2, response.getList(1).size()); assertEquals("PERMANENTFLAGS", response.getList(1).getString(0)); assertTrue(response.getList(1).get(1) instanceof ImapList); assertEquals("\\Answered", response.getList(1).getList(1).getString(0)); assertEquals("\\Flagged", response.getList(1).getList(1).getString(1)); assertEquals("\\Deleted", response.getList(1).getList(1).getString(2)); assertEquals("\\Seen", response.getList(1).getList(1).getString(3)); assertEquals("\\Draft", response.getList(1).getList(1).getString(4)); assertEquals("NonJunk", response.getList(1).getList(1).getString(5)); assertEquals("$MDNSent", response.getList(1).getList(1).getString(6)); assertEquals("\\*", response.getList(1).getList(1).getString(7)); }
@Test public void testSimpleOkResponse() throws IOException { ImapResponseParser parser = createParser("* OK\r\n"); ImapResponse response = parser.readResponse(); assertNotNull(response); assertEquals(1, response.size()); assertEquals("OK", response.get(0)); }