Search in sources :

Example 91 with Message

use of com.fsck.k9.mail.Message in project k-9 by k9mail.

the class EncryptionDetectorTest method isEncrypted_withSMimePart_shouldReturnTrue.

@Test
public void isEncrypted_withSMimePart_shouldReturnTrue() throws Exception {
    Message message = createMessage("application/pkcs7-mime");
    boolean encrypted = encryptionDetector.isEncrypted(message);
    assertTrue(encrypted);
}
Also used : MessageCreationHelper.createMultipartMessage(com.fsck.k9.message.MessageCreationHelper.createMultipartMessage) MessageCreationHelper.createMessage(com.fsck.k9.message.MessageCreationHelper.createMessage) MessageCreationHelper.createTextMessage(com.fsck.k9.message.MessageCreationHelper.createTextMessage) Message(com.fsck.k9.mail.Message) Test(org.junit.Test)

Example 92 with Message

use of com.fsck.k9.mail.Message in project k-9 by k9mail.

the class EncryptionDetectorTest method isEncrypted_withTextPlain_shouldReturnFalse.

@Test
public void isEncrypted_withTextPlain_shouldReturnFalse() throws Exception {
    Message message = createTextMessage("text/plain", "plain text");
    boolean encrypted = encryptionDetector.isEncrypted(message);
    assertFalse(encrypted);
}
Also used : MessageCreationHelper.createMultipartMessage(com.fsck.k9.message.MessageCreationHelper.createMultipartMessage) MessageCreationHelper.createMessage(com.fsck.k9.message.MessageCreationHelper.createMessage) MessageCreationHelper.createTextMessage(com.fsck.k9.message.MessageCreationHelper.createTextMessage) Message(com.fsck.k9.mail.Message) Test(org.junit.Test)

Example 93 with Message

use of com.fsck.k9.mail.Message in project k-9 by k9mail.

the class EncryptionDetectorTest method isEncrypted_withMultipartEncrypted_shouldReturnTrue.

@Test
public void isEncrypted_withMultipartEncrypted_shouldReturnTrue() throws Exception {
    Message message = createMultipartMessage("multipart/encrypted", createPart("application/octet-stream"), createPart("application/octet-stream"));
    boolean encrypted = encryptionDetector.isEncrypted(message);
    assertTrue(encrypted);
}
Also used : MessageCreationHelper.createMultipartMessage(com.fsck.k9.message.MessageCreationHelper.createMultipartMessage) MessageCreationHelper.createMessage(com.fsck.k9.message.MessageCreationHelper.createMessage) MessageCreationHelper.createTextMessage(com.fsck.k9.message.MessageCreationHelper.createTextMessage) Message(com.fsck.k9.mail.Message) Test(org.junit.Test)

Example 94 with Message

use of com.fsck.k9.mail.Message in project k-9 by k9mail.

the class WebDavFolder method fetchMessages.

/**
     * Fetches the full messages or up to {@param lines} lines and passes them to the message parser.
     */
private void fetchMessages(List<WebDavMessage> messages, MessageRetrievalListener<WebDavMessage> listener, int lines) throws MessagingException {
    WebDavHttpClient httpclient;
    httpclient = store.getHttpClient();
    /**
         * We can't hand off to processRequest() since we need the stream to parse.
         */
    for (int i = 0, count = messages.size(); i < count; i++) {
        WebDavMessage wdMessage = messages.get(i);
        int statusCode = 0;
        if (listener != null) {
            listener.messageStarted(wdMessage.getUid(), i, count);
        }
        /**
             * If fetch is called outside of the initial list (ie, a locally stored message), it may not have a URL
             * associated. Verify and fix that
             */
        if (wdMessage.getUrl().equals("")) {
            wdMessage.setUrl(getMessageUrls(new String[] { wdMessage.getUid() }).get(wdMessage.getUid()));
            Log.i(LOG_TAG, "Fetching messages with UID = '" + wdMessage.getUid() + "', URL = '" + wdMessage.getUrl() + "'");
            if (wdMessage.getUrl().equals("")) {
                throw new MessagingException("Unable to get URL for message");
            }
        }
        try {
            Log.i(LOG_TAG, "Fetching message with UID = '" + wdMessage.getUid() + "', URL = '" + wdMessage.getUrl() + "'");
            HttpGet httpget = new HttpGet(new URI(wdMessage.getUrl()));
            HttpResponse response;
            HttpEntity entity;
            httpget.setHeader("translate", "f");
            if (store.getAuthentication() == WebDavConstants.AUTH_TYPE_BASIC) {
                httpget.setHeader("Authorization", store.getAuthString());
            }
            response = httpclient.executeOverride(httpget, store.getHttpContext());
            statusCode = response.getStatusLine().getStatusCode();
            entity = response.getEntity();
            if (statusCode < 200 || statusCode > 300) {
                throw new IOException("Error during with code " + statusCode + " during fetch: " + response.getStatusLine().toString());
            }
            if (entity != null) {
                InputStream istream = null;
                StringBuilder buffer = new StringBuilder();
                String tempText;
                String resultText;
                BufferedReader reader = null;
                int currentLines = 0;
                try {
                    istream = WebDavHttpClient.getUngzippedContent(entity);
                    if (lines != -1) {
                        //Convert the ungzipped input stream into a StringBuilder
                        //containing the given line count
                        reader = new BufferedReader(new InputStreamReader(istream), 8192);
                        while ((tempText = reader.readLine()) != null && (currentLines < lines)) {
                            buffer.append(tempText).append("\r\n");
                            currentLines++;
                        }
                        IOUtils.closeQuietly(istream);
                        resultText = buffer.toString();
                        istream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
                    }
                    //Parse either the entire message stream, or a stream of the given lines
                    wdMessage.parse(istream);
                } catch (IOException ioe) {
                    Log.e(LOG_TAG, "IOException: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe));
                    throw new MessagingException("I/O Error", ioe);
                } finally {
                    IOUtils.closeQuietly(reader);
                    IOUtils.closeQuietly(istream);
                }
            } else {
                Log.v(LOG_TAG, "Empty response");
            }
        } catch (IllegalArgumentException iae) {
            Log.e(LOG_TAG, "IllegalArgumentException caught " + iae + "\nTrace: " + WebDavUtils.processException(iae));
            throw new MessagingException("IllegalArgumentException caught", iae);
        } catch (URISyntaxException use) {
            Log.e(LOG_TAG, "URISyntaxException caught " + use + "\nTrace: " + WebDavUtils.processException(use));
            throw new MessagingException("URISyntaxException caught", use);
        } catch (IOException ioe) {
            Log.e(LOG_TAG, "Non-success response code loading message, response code was " + statusCode + "\nURL: " + wdMessage.getUrl() + "\nError: " + ioe.getMessage() + "\nTrace: " + WebDavUtils.processException(ioe));
            throw new MessagingException("Failure code " + statusCode, ioe);
        }
        if (listener != null) {
            listener.messageFinished(wdMessage, i, count);
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader)

Example 95 with Message

use of com.fsck.k9.mail.Message in project k-9 by k9mail.

the class ImapFolder method fetchPart.

@Override
public void fetchPart(Message message, Part part, MessageRetrievalListener<Message> listener) throws MessagingException {
    checkOpen();
    String partId = part.getServerExtra();
    String fetch;
    if ("TEXT".equalsIgnoreCase(partId)) {
        int maximumAutoDownloadMessageSize = store.getStoreConfig().getMaximumAutoDownloadMessageSize();
        fetch = String.format(Locale.US, "BODY.PEEK[TEXT]<0.%d>", maximumAutoDownloadMessageSize);
    } else {
        fetch = String.format("BODY.PEEK[%s]", partId);
    }
    try {
        String command = String.format("UID FETCH %s (UID %s)", message.getUid(), fetch);
        connection.sendCommand(command, false);
        ImapResponse response;
        int messageNumber = 0;
        ImapResponseCallback callback = new FetchPartCallback(part);
        do {
            response = connection.readResponse(callback);
            if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) {
                ImapList fetchList = (ImapList) response.getKeyedValue("FETCH");
                String uid = fetchList.getKeyedString("UID");
                if (!message.getUid().equals(uid)) {
                    if (K9MailLib.isDebug()) {
                        Log.d(LOG_TAG, "Did not ask for UID " + uid + " for " + getLogId());
                    }
                    handleUntaggedResponse(response);
                    continue;
                }
                if (listener != null) {
                    listener.messageStarted(uid, messageNumber++, 1);
                }
                ImapMessage imapMessage = (ImapMessage) message;
                Object literal = handleFetchResponse(imapMessage, fetchList);
                if (literal != null) {
                    if (literal instanceof Body) {
                        // Most of the work was done in FetchAttchmentCallback.foundLiteral()
                        MimeMessageHelper.setBody(part, (Body) literal);
                    } else if (literal instanceof String) {
                        String bodyString = (String) literal;
                        InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
                        String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
                        String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
                        MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream, contentTransferEncoding, contentType));
                    } else {
                        // This shouldn't happen
                        throw new MessagingException("Got FETCH response with bogus parameters");
                    }
                }
                if (listener != null) {
                    listener.messageFinished(message, messageNumber, 1);
                }
            } else {
                handleUntaggedResponse(response);
            }
        } while (response.getTag() == null);
    } catch (IOException ioe) {
        throw ioExceptionHandler(connection, ioe);
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Body(com.fsck.k9.mail.Body)

Aggregations

Test (org.junit.Test)127 Message (com.fsck.k9.mail.Message)111 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)102 Part (com.fsck.k9.mail.Part)47 LocalMessage (com.fsck.k9.mailstore.LocalMessage)46 MessagingException (com.fsck.k9.mail.MessagingException)41 ArrayList (java.util.ArrayList)41 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)33 BodyPart (com.fsck.k9.mail.BodyPart)32 Account (com.fsck.k9.Account)27 LocalFolder (com.fsck.k9.mailstore.LocalFolder)24 TextBody (com.fsck.k9.mail.internet.TextBody)23 IOException (java.io.IOException)22 Address (com.fsck.k9.mail.Address)21 LocalStore (com.fsck.k9.mailstore.LocalStore)21 Date (java.util.Date)20 MessageReference (com.fsck.k9.activity.MessageReference)16 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)16 Folder (com.fsck.k9.mail.Folder)14 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)13