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; }
@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); } }
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 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; }
public boolean wasByeResponseReceived() { for (ImapResponse response : responses) { if (response.getTag() == null && response.size() >= 1 && equalsIgnoreCase(response.get(0), Responses.BYE)) { return true; } } return false; }
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); }
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; } } }
@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 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); } }
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); } }
@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 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 void readTokens(ImapResponse response) throws IOException { response.clear(); Object firstToken = readToken(response); checkTokenIsString(firstToken); String symbol = (String) firstToken; response.add(symbol); if (isStatusResponse(symbol)) { parseResponseText(response); } else if (equalsIgnoreCase(symbol, Responses.LIST) || equalsIgnoreCase(symbol, Responses.LSUB)) { parseListResponse(response); } else { Object token; while ((token = readToken(response)) != null) { if (!(token instanceof ImapList)) { response.add(token); } } } }
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); } }
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(); }
/** * Handle an untagged response that the caller doesn't care to handle themselves. */ protected void handleUntaggedResponse(ImapResponse response) { if (response.getTag() == null && response.size() > 1) { if (ImapResponseParser.equalsIgnoreCase(response.get(1), "EXISTS")) { messageCount = response.getNumber(0); if (K9MailLib.isDebug()) { Timber.d("Got untagged EXISTS with value %d for %s", messageCount, getLogId()); } } handlePossibleUidNext(response); if (ImapResponseParser.equalsIgnoreCase(response.get(1), "EXPUNGE") && messageCount > 0) { messageCount--; if (K9MailLib.isDebug()) { Timber.d("Got untagged EXPUNGE with messageCount %d for %s", messageCount, getLogId()); } } } }
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()); } } } } } } }
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; }
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); }