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 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; }
public static String getResponseCode(ImapResponse response) { if (response.size() < 2 || !response.isList(1)) { return null; } ImapList responseTextCode = response.getList(1); return responseTextCode.size() != 1 ? null : responseTextCode.getString(0); } }
public static String getAlertText(ImapResponse response) { if (response.size() < 3 || !response.isList(1)) { return null; } ImapList responseTextCode = response.getList(1); if (responseTextCode.size() != 1 || !equalsIgnoreCase(responseTextCode.get(0), ALERT_RESPONSE_CODE)) { return null; } return response.getString(2); } }
@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 testCommandContinuation() throws Exception { ImapResponseParser parser = createParser("+ Ready for additional command text\r\n"); ImapResponse response = parser.readResponse(); assertEquals(1, response.size()); assertEquals("Ready for additional command text", response.getString(0)); }
@Test public void testParseLiteral() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); ImapResponse response = parser.readResponse(); assertEquals(1, response.size()); assertEquals("test", response.getString(0)); }
@Test public void testParseQuoted() throws Exception { ImapResponseParser parser = createParser("* \"qu\\\"oted\"\r\n"); ImapResponse response = parser.readResponse(); assertEquals(1, response.size()); assertEquals("qu\"oted", response.getString(0)); }
@Test public void testParseLiteralWithEmptyString() throws Exception { ImapResponseParser parser = createParser("* {0}\r\n\r\n"); ImapResponse response = parser.readResponse(); assertEquals(1, response.size()); assertEquals("", response.getString(0)); }
@Test public void testParseLiteralWithConsumingCallbackReturningNull() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(4, "cheeseburger"); ImapResponse response = parser.readResponse(callback); assertEquals(1, response.size()); assertEquals("cheeseburger", response.getString(0)); }
@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)); }
@Test public void testExistsResponse() throws Exception { ImapResponseParser parser = createParser("* 23 EXISTS\r\n"); ImapResponse response = parser.readResponse(); assertEquals(2, response.size()); assertEquals(23, response.getNumber(0)); assertEquals("EXISTS", response.getString(1)); }
@Test public void testParseLiteralWithIncompleteConsumingCallbackReturningString() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(2, "ninja"); ImapResponse response = parser.readResponse(callback); assertEquals(1, response.size()); assertEquals("ninja", response.getString(0)); assertAllInputConsumed(); }
@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 testParseLiteralWithNonConsumingCallbackReturningNull() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndReturn(0, null); ImapResponse response = parser.readResponse(callback); assertEquals(1, response.size()); assertEquals("test", response.getString(0)); assertTrue(callback.foundLiteralCalled); assertAllInputConsumed(); }
@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 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)); }
@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 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)); }