Search in sources :

Example 1 with ImapResponse

use of com.android.voicemail.impl.mail.store.imap.ImapResponse in project android_packages_apps_Dialer by LineageOS.

the class ImapConnection method getCommandResponses.

/**
 * Read and return all of the responses from the most recent command sent to the server
 *
 * @return a list of ImapResponses
 * @throws IOException
 * @throws MessagingException
 */
List<ImapResponse> getCommandResponses() throws IOException, MessagingException {
    final List<ImapResponse> responses = new ArrayList<ImapResponse>();
    ImapResponse response;
    do {
        response = mParser.readResponse(false);
        responses.add(response);
    } while (!(response.isTagged() || response.isContinuationRequest()));
    if (!(response.isOk() || response.isContinuationRequest())) {
        final String toString = response.toString();
        final String status = response.getStatusOrEmpty().getString();
        final String statusMessage = response.getStatusResponseTextOrEmpty().getString();
        final String alert = response.getAlertTextOrEmpty().getString();
        final String responseCode = response.getResponseCodeOrEmpty().getString();
        destroyResponses();
        throw new ImapException(toString, status, statusMessage, alert, responseCode);
    }
    return responses;
}
Also used : ImapException(com.android.voicemail.impl.mail.store.ImapStore.ImapException) ArrayList(java.util.ArrayList) ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse)

Example 2 with ImapResponse

use of com.android.voicemail.impl.mail.store.imap.ImapResponse in project android_packages_apps_Dialer by LineageOS.

the class ImapConnection method open.

public void open() throws IOException, MessagingException {
    if (mTransport != null && mTransport.isOpen()) {
        return;
    }
    try {
        // copy configuration into a clean transport, if necessary
        if (mTransport == null) {
            mTransport = mImapStore.cloneTransport();
        }
        mTransport.open();
        createParser();
        // The server should greet us with something like
        // * OK IMAP4rev1 Server
        // consume the response before doing anything else.
        ImapResponse response = mParser.readResponse(false);
        if (!response.isOk()) {
            mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_INVALID_INITIAL_SERVER_RESPONSE);
            throw new MessagingException(MessagingException.AUTHENTICATION_FAILED_OR_SERVER_ERROR, "Invalid server initial response");
        }
        queryCapability();
        maybeDoStartTls();
        // LOGIN
        doLogin();
    } catch (SSLException e) {
        LogUtils.d(TAG, "SSLException ", e);
        mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_SSL_EXCEPTION);
        throw new CertificateValidationException(e.getMessage(), e);
    } catch (IOException ioe) {
        LogUtils.d(TAG, "IOException", ioe);
        mImapStore.getImapHelper().handleEvent(OmtpEvents.DATA_IOE_ON_OPEN);
        throw ioe;
    } finally {
        destroyResponses();
    }
}
Also used : MessagingException(com.android.voicemail.impl.mail.MessagingException) ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse) CertificateValidationException(com.android.voicemail.impl.mail.CertificateValidationException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 3 with ImapResponse

use of com.android.voicemail.impl.mail.store.imap.ImapResponse in project android_packages_apps_Dialer by LineageOS.

the class ImapConnection method queryCapability.

private void queryCapability() throws IOException, MessagingException {
    List<ImapResponse> responses = executeSimpleCommand(ImapConstants.CAPABILITY);
    mCapabilities.clear();
    Set<String> disabledCapabilities = mImapStore.getImapHelper().getConfig().getDisabledCapabilities();
    for (ImapResponse response : responses) {
        if (response.isTagged()) {
            continue;
        }
        for (int i = 0; i < response.size(); i++) {
            String capability = response.getStringOrEmpty(i).getString();
            if (disabledCapabilities != null) {
                if (!disabledCapabilities.contains(capability)) {
                    mCapabilities.add(capability);
                }
            } else {
                mCapabilities.add(capability);
            }
        }
    }
    LogUtils.d(TAG, "Capabilities: " + mCapabilities.toString());
}
Also used : ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse)

Example 4 with ImapResponse

use of com.android.voicemail.impl.mail.store.imap.ImapResponse in project android_packages_apps_Dialer by LineageOS.

the class ImapFolder method fetchInternal.

public void fetchInternal(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException {
    if (messages.length == 0) {
        return;
    }
    checkOpen();
    ArrayMap<String, Message> messageMap = new ArrayMap<String, Message>();
    for (Message m : messages) {
        messageMap.put(m.getUid(), m);
    }
    /*
     * Figure out what command we are going to run:
     * FLAGS     - UID FETCH (FLAGS)
     * ENVELOPE  - UID FETCH (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[
     *                            HEADER.FIELDS (date subject from content-type to cc)])
     * STRUCTURE - UID FETCH (BODYSTRUCTURE)
     * BODY_SANE - UID FETCH (BODY.PEEK[]<0.N>) where N = max bytes returned
     * BODY      - UID FETCH (BODY.PEEK[])
     * Part      - UID FETCH (BODY.PEEK[ID]) where ID = mime part ID
     */
    final LinkedHashSet<String> fetchFields = new LinkedHashSet<String>();
    fetchFields.add(ImapConstants.UID);
    if (fp.contains(FetchProfile.Item.FLAGS)) {
        fetchFields.add(ImapConstants.FLAGS);
    }
    if (fp.contains(FetchProfile.Item.ENVELOPE)) {
        fetchFields.add(ImapConstants.INTERNALDATE);
        fetchFields.add(ImapConstants.RFC822_SIZE);
        fetchFields.add(ImapConstants.FETCH_FIELD_HEADERS);
    }
    if (fp.contains(FetchProfile.Item.STRUCTURE)) {
        fetchFields.add(ImapConstants.BODYSTRUCTURE);
    }
    if (fp.contains(FetchProfile.Item.BODY_SANE)) {
        fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_SANE);
    }
    if (fp.contains(FetchProfile.Item.BODY)) {
        fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK);
    }
    // TODO Why are we only fetching the first part given?
    final Part fetchPart = fp.getFirstPart();
    if (fetchPart != null) {
        final String[] partIds = fetchPart.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
        // the first id if there are more than one?
        if (partIds != null) {
            fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_BARE + "[" + partIds[0] + "]");
        }
    }
    try {
        mConnection.sendCommand(String.format(Locale.US, ImapConstants.UID_FETCH + " %s (%s)", ImapStore.joinMessageUids(messages), Utility.combine(fetchFields.toArray(new String[fetchFields.size()]), ' ')), false);
        ImapResponse response;
        do {
            response = null;
            try {
                response = mConnection.readResponse();
                if (!response.isDataResponse(1, ImapConstants.FETCH)) {
                    // Ignore
                    continue;
                }
                final ImapList fetchList = response.getListOrEmpty(2);
                final String uid = fetchList.getKeyedStringOrEmpty(ImapConstants.UID).getString();
                if (TextUtils.isEmpty(uid))
                    continue;
                ImapMessage message = (ImapMessage) messageMap.get(uid);
                if (message == null)
                    continue;
                if (fp.contains(FetchProfile.Item.FLAGS)) {
                    final ImapList flags = fetchList.getKeyedListOrEmpty(ImapConstants.FLAGS);
                    for (int i = 0, count = flags.size(); i < count; i++) {
                        final ImapString flag = flags.getStringOrEmpty(i);
                        if (flag.is(ImapConstants.FLAG_DELETED)) {
                            message.setFlagInternal(Flag.DELETED, true);
                        } else if (flag.is(ImapConstants.FLAG_ANSWERED)) {
                            message.setFlagInternal(Flag.ANSWERED, true);
                        } else if (flag.is(ImapConstants.FLAG_SEEN)) {
                            message.setFlagInternal(Flag.SEEN, true);
                        } else if (flag.is(ImapConstants.FLAG_FLAGGED)) {
                            message.setFlagInternal(Flag.FLAGGED, true);
                        }
                    }
                }
                if (fp.contains(FetchProfile.Item.ENVELOPE)) {
                    final Date internalDate = fetchList.getKeyedStringOrEmpty(ImapConstants.INTERNALDATE).getDateOrNull();
                    final int size = fetchList.getKeyedStringOrEmpty(ImapConstants.RFC822_SIZE).getNumberOrZero();
                    final String header = fetchList.getKeyedStringOrEmpty(ImapConstants.BODY_BRACKET_HEADER, true).getString();
                    message.setInternalDate(internalDate);
                    message.setSize(size);
                    try {
                        message.parse(Utility.streamFromAsciiString(header));
                    } catch (Exception e) {
                        VvmLog.e(TAG, "Error parsing header %s", e);
                    }
                }
                if (fp.contains(FetchProfile.Item.STRUCTURE)) {
                    ImapList bs = fetchList.getKeyedListOrEmpty(ImapConstants.BODYSTRUCTURE);
                    if (!bs.isEmpty()) {
                        try {
                            parseBodyStructure(bs, message, ImapConstants.TEXT);
                        } catch (MessagingException e) {
                            VvmLog.v(TAG, "Error handling message", e);
                            message.setBody(null);
                        }
                    }
                }
                if (fp.contains(FetchProfile.Item.BODY) || fp.contains(FetchProfile.Item.BODY_SANE)) {
                    // Body is keyed by "BODY[]...".
                    // Previously used "BODY[..." but this can be confused with "BODY[HEADER..."
                    // TODO Should we accept "RFC822" as well??
                    ImapString body = fetchList.getKeyedStringOrEmpty("BODY[]", true);
                    InputStream bodyStream = body.getAsStream();
                    try {
                        message.parse(bodyStream);
                    } catch (Exception e) {
                        VvmLog.e(TAG, "Error parsing body %s", e);
                    }
                }
                if (fetchPart != null) {
                    InputStream bodyStream = fetchList.getKeyedStringOrEmpty("BODY[", true).getAsStream();
                    String[] encodings = fetchPart.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING);
                    String contentTransferEncoding = null;
                    if (encodings != null && encodings.length > 0) {
                        contentTransferEncoding = encodings[0];
                    } else {
                        // According to http://tools.ietf.org/html/rfc2045#section-6.1
                        // "7bit" is the default.
                        contentTransferEncoding = "7bit";
                    }
                    try {
                        // TODO Don't create 2 temp files.
                        // decodeBody creates BinaryTempFileBody, but we could avoid this
                        // if we implement ImapStringBody.
                        // (We'll need to share a temp file.  Protect it with a ref-count.)
                        message.setBody(decodeBody(mStore.getContext(), bodyStream, contentTransferEncoding, fetchPart.getSize(), listener));
                    } catch (Exception e) {
                        // TODO: Figure out what kinds of exceptions might actually be thrown
                        // from here. This blanket catch-all is because we're not sure what to
                        // do if we don't have a contentTransferEncoding, and we don't have
                        // time to figure out what exceptions might be thrown.
                        VvmLog.e(TAG, "Error fetching body %s", e);
                    }
                }
                if (listener != null) {
                    listener.messageRetrieved(message);
                }
            } finally {
                destroyResponses();
            }
        } while (!response.isTagged());
    } catch (IOException ioe) {
        mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE);
        throw ioExceptionHandler(mConnection, ioe);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ImapList(com.android.voicemail.impl.mail.store.imap.ImapList) Message(com.android.voicemail.impl.mail.Message) ImapMessage(com.android.voicemail.impl.mail.store.ImapStore.ImapMessage) ImapMessage(com.android.voicemail.impl.mail.store.ImapStore.ImapMessage) MessagingException(com.android.voicemail.impl.mail.MessagingException) InputStream(java.io.InputStream) ArrayMap(android.util.ArrayMap) ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString) IOException(java.io.IOException) Date(java.util.Date) AuthenticationFailedException(com.android.voicemail.impl.mail.AuthenticationFailedException) IOException(java.io.IOException) ImapException(com.android.voicemail.impl.mail.store.ImapStore.ImapException) Base64DataException(android.util.Base64DataException) MessagingException(com.android.voicemail.impl.mail.MessagingException) Part(com.android.voicemail.impl.mail.Part) MimeBodyPart(com.android.voicemail.impl.mail.internet.MimeBodyPart) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString)

Example 5 with ImapResponse

use of com.android.voicemail.impl.mail.store.imap.ImapResponse in project android_packages_apps_Dialer by LineageOS.

the class ImapFolder method doSelect.

/**
 * Selects the folder for use. Before performing any operations on this folder, it must be
 * selected.
 */
private void doSelect() throws IOException, MessagingException {
    final List<ImapResponse> responses = mConnection.executeSimpleCommand(String.format(Locale.US, ImapConstants.SELECT + " \"%s\"", mName));
    // Assume the folder is opened read-write; unless we are notified otherwise
    mMode = MODE_READ_WRITE;
    int messageCount = -1;
    for (ImapResponse response : responses) {
        if (response.isDataResponse(1, ImapConstants.EXISTS)) {
            messageCount = response.getStringOrEmpty(0).getNumberOrZero();
        } else if (response.isOk()) {
            final ImapString responseCode = response.getResponseCodeOrEmpty();
            if (responseCode.is(ImapConstants.READ_ONLY)) {
                mMode = MODE_READ_ONLY;
            } else if (responseCode.is(ImapConstants.READ_WRITE)) {
                mMode = MODE_READ_WRITE;
            }
        } else if (response.isTagged()) {
            // Not OK
            mStore.getImapHelper().handleEvent(OmtpEvents.DATA_MAILBOX_OPEN_FAILED);
            throw new MessagingException("Can't open mailbox: " + response.getStatusResponseTextOrEmpty());
        }
    }
    if (messageCount == -1) {
        throw new MessagingException("Did not find message count during select");
    }
    mMessageCount = messageCount;
    mExists = true;
}
Also used : MessagingException(com.android.voicemail.impl.mail.MessagingException) ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString)

Aggregations

ImapResponse (com.android.voicemail.impl.mail.store.imap.ImapResponse)5 MessagingException (com.android.voicemail.impl.mail.MessagingException)3 ImapException (com.android.voicemail.impl.mail.store.ImapStore.ImapException)2 ImapString (com.android.voicemail.impl.mail.store.imap.ImapString)2 IOException (java.io.IOException)2 ArrayMap (android.util.ArrayMap)1 Base64DataException (android.util.Base64DataException)1 AuthenticationFailedException (com.android.voicemail.impl.mail.AuthenticationFailedException)1 CertificateValidationException (com.android.voicemail.impl.mail.CertificateValidationException)1 Message (com.android.voicemail.impl.mail.Message)1 Part (com.android.voicemail.impl.mail.Part)1 MimeBodyPart (com.android.voicemail.impl.mail.internet.MimeBodyPart)1 ImapMessage (com.android.voicemail.impl.mail.store.ImapStore.ImapMessage)1 ImapList (com.android.voicemail.impl.mail.store.imap.ImapList)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashSet (java.util.LinkedHashSet)1 SSLException (javax.net.ssl.SSLException)1