private boolean inRange(int index) { return index >= 0 && index < size(); }
private static List<String> extractAttributes(ImapResponse response) { ImapList nameAttributes = response.getList(1); List<String> attributes = new ArrayList<>(nameAttributes.size()); for (Object nameAttribute : nameAttributes) { if (!(nameAttribute instanceof String)) { return null; } String attribute = (String) nameAttribute; attributes.add(attribute); } return attributes; }
public int getKeyIndex(String key) { for (int i = 0, count = size() - 1; i < count; i++) { if (ImapResponseParser.equalsIgnoreCase(get(i), key)) { return i; } } throw new IllegalArgumentException("getKeyIndex() only works for keys that are in the collection."); }
public boolean containsKey(String key) { if (key == null) { return false; } for (int i = 0, count = size() - 1; i < count; i++) { if (ImapResponseParser.equalsIgnoreCase(get(i), key)) { return true; } } return false; }
public Object getKeyedValue(String key) { for (int i = 0, count = size() - 1; i < count; i++) { if (ImapResponseParser.equalsIgnoreCase(get(i), key)) { return get(i + 1); } } return null; }
static CapabilityResponse parse(ImapList capabilityList) { if (capabilityList.isEmpty() || !equalsIgnoreCase(capabilityList.get(0), Responses.CAPABILITY)) { return null; } int size = capabilityList.size(); HashSet<String> capabilities = new HashSet<>(size - 1); for (int i = 1; i < size; i++) { if (!capabilityList.isString(i)) { return null; } String uppercaseCapability = capabilityList.getString(i).toUpperCase(Locale.US); capabilities.add(uppercaseCapability); } return new CapabilityResponse(capabilities); }
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); } }
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); } }
for (int i = 0, count = bs.size(); i < count; i++) { if (bs.get(i) instanceof ImapList) { for (int i = 0, count = bodyParams.size(); i < count; i += 2) { String paramName = bodyParams.getString(i); String paramValue = bodyParams.getString(i + 1); if ("text".equalsIgnoreCase(type) && bs.size() > 9 && bs.get(9) instanceof ImapList) { bodyDisposition = bs.getList(9); } else if (!("text".equalsIgnoreCase(type)) && bs.size() > 8 && bs.get(8) instanceof ImapList) { bodyDisposition = bs.getList(8); if (bodyDisposition.size() > 1 && bodyDisposition.get(1) instanceof ImapList) { ImapList bodyDispositionParams = bodyDisposition.getList(1); for (int i = 0, count = bodyDispositionParams.size(); i < count; i += 2) { String paramName = bodyDispositionParams.getString(i).toLowerCase(Locale.US); String paramValue = bodyDispositionParams.getString(i + 1);
protected void handlePossibleUidNext(ImapResponse response) { if (ImapResponseParser.equalsIgnoreCase(response.get(0), "OK") && response.size() > 1) { Object bracketedObj = response.get(1); if (bracketedObj instanceof ImapList) { ImapList bracketed = (ImapList) bracketedObj; if (bracketed.size() > 1) { Object keyObj = bracketed.get(0); if (keyObj instanceof String) { String key = (String) keyObj; if ("UIDNEXT".equalsIgnoreCase(key)) { uidNext = bracketed.getLong(1); if (K9MailLib.isDebug()) { Timber.d("Got UidNext = %s for %s", uidNext, getLogId()); } } } } } } }
if (appendList.size() >= 3 && appendList.getString(0).equals("APPENDUID")) { String newUid = appendList.getString(2);
@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 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 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)); }
@Test public void testFetchResponse() throws Exception { ImapResponseParser parser = createParser("* 1 FETCH (" + "UID 23 " + "INTERNALDATE \"01-Jul-2015 12:34:56 +0200\" " + "RFC822.SIZE 3456 " + "BODY[HEADER.FIELDS (date subject from)] \"<headers>\" " + "FLAGS (\\Seen))\r\n"); ImapResponse response = parser.readResponse(); assertEquals(3, response.size()); assertEquals("1", response.getString(0)); assertEquals("FETCH", response.getString(1)); assertEquals("UID", response.getList(2).getString(0)); assertEquals(23, response.getList(2).getNumber(1)); assertEquals("INTERNALDATE", response.getList(2).getString(2)); assertEquals("01-Jul-2015 12:34:56 +0200", response.getList(2).getString(3)); assertEquals("RFC822.SIZE", response.getList(2).getString(4)); assertEquals(3456, response.getList(2).getNumber(5)); assertEquals("BODY", response.getList(2).getString(6)); assertEquals(2, response.getList(2).getList(7).size()); assertEquals("HEADER.FIELDS", response.getList(2).getList(7).getString(0)); assertEquals(3, response.getList(2).getList(7).getList(1).size()); assertEquals("date", response.getList(2).getList(7).getList(1).getString(0)); assertEquals("subject", response.getList(2).getList(7).getList(1).getString(1)); assertEquals("from", response.getList(2).getList(7).getList(1).getString(2)); assertEquals("<headers>", response.getList(2).getString(8)); assertEquals("FLAGS", response.getList(2).getString(9)); assertEquals(1, response.getList(2).getList(10).size()); assertEquals("\\Seen", response.getList(2).getList(10).getString(0)); }
@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)); }