Search in sources :

Example 86 with MessagingException

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

the class AttachmentProvider method query.

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    String[] columnNames = (projection == null) ? DEFAULT_PROJECTION : projection;
    List<String> segments = uri.getPathSegments();
    String accountUuid = segments.get(0);
    String id = segments.get(1);
    final AttachmentInfo attachmentInfo;
    try {
        final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
        attachmentInfo = DI.get(LocalStoreProvider.class).getInstance(account).getAttachmentInfo(id);
    } catch (MessagingException e) {
        Timber.e(e, "Unable to retrieve attachment info from local store for ID: %s", id);
        return null;
    }
    if (attachmentInfo == null) {
        Timber.d("No attachment info for ID: %s", id);
        return null;
    }
    MatrixCursor ret = new MatrixCursor(columnNames);
    Object[] values = new Object[columnNames.length];
    for (int i = 0, count = columnNames.length; i < count; i++) {
        String column = columnNames[i];
        if (AttachmentProviderColumns._ID.equals(column)) {
            values[i] = id;
        } else if (AttachmentProviderColumns.DATA.equals(column)) {
            values[i] = uri.toString();
        } else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
            values[i] = attachmentInfo.name;
        } else if (AttachmentProviderColumns.SIZE.equals(column)) {
            values[i] = attachmentInfo.size;
        }
    }
    ret.addRow(values);
    return ret;
}
Also used : Account(com.fsck.k9.Account) MessagingException(com.fsck.k9.mail.MessagingException) AttachmentInfo(com.fsck.k9.mailstore.LocalStore.AttachmentInfo) LocalStoreProvider(com.fsck.k9.mailstore.LocalStoreProvider) MatrixCursor(android.database.MatrixCursor)

Example 87 with MessagingException

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

the class RawMessageProvider method loadMessage.

private LocalMessage loadMessage(MessageReference messageReference) {
    String accountUuid = messageReference.getAccountUuid();
    long folderId = messageReference.getFolderId();
    String uid = messageReference.getUid();
    Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
    if (account == null) {
        Timber.w("Account not found: %s", accountUuid);
        return null;
    }
    try {
        LocalStore localStore = DI.get(LocalStoreProvider.class).getInstance(account);
        LocalFolder localFolder = localStore.getFolder(folderId);
        localFolder.open();
        LocalMessage message = localFolder.getMessage(uid);
        if (message == null || message.getDatabaseId() == 0) {
            Timber.w("Message not found: folder=%s, uid=%s", folderId, uid);
            return null;
        }
        FetchProfile fetchProfile = new FetchProfile();
        fetchProfile.add(FetchProfile.Item.BODY);
        localFolder.fetch(Collections.singletonList(message), fetchProfile, null);
        return message;
    } catch (MessagingException e) {
        Timber.e(e, "Error loading message: folder=%d, uid=%s", folderId, uid);
        return null;
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) LocalStoreProvider(com.fsck.k9.mailstore.LocalStoreProvider)

Example 88 with MessagingException

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

the class PgpMessageBuilder method startOrContinueBuildMessage.

private void startOrContinueBuildMessage(@Nullable Intent pgpApiIntent) {
    try {
        boolean shouldSign = cryptoStatus.isSigningEnabled() && !isDraft();
        boolean shouldEncrypt = cryptoStatus.isEncryptionEnabled() || (isDraft() && cryptoStatus.isEncryptAllDrafts());
        boolean isPgpInlineMode = cryptoStatus.isPgpInlineModeEnabled() && !isDraft();
        if (!shouldSign && !shouldEncrypt) {
            queueMessageBuildSuccess(currentProcessedMimeMessage);
            return;
        }
        boolean isSimpleTextMessage = MimeUtility.isSameMimeType("text/plain", currentProcessedMimeMessage.getMimeType());
        if (isPgpInlineMode && !isSimpleTextMessage) {
            throw new MessagingException("Attachments are not supported in PGP/INLINE format!");
        }
        if (shouldEncrypt && !isDraft() && !cryptoStatus.hasRecipients()) {
            throw new MessagingException("Must have recipients to build message!");
        }
        if (messageContentBodyPart == null) {
            messageContentBodyPart = createBodyPartFromMessageContent();
            boolean payloadSupportsMimeHeaders = !isPgpInlineMode;
            if (payloadSupportsMimeHeaders) {
                if (cryptoStatus.isEncryptSubject() && shouldEncrypt) {
                    moveSubjectIntoEncryptedPayload();
                }
                maybeAddGossipHeadersToBodyPart();
            // unfortuntately, we can't store the Autocrypt-Draft-State header in the payload
            // see https://github.com/autocrypt/autocrypt/pull/376#issuecomment-384293480
            }
        }
        if (pgpApiIntent == null) {
            boolean encryptToSelfOnly = isDraft();
            pgpApiIntent = buildOpenPgpApiIntent(shouldSign, shouldEncrypt, encryptToSelfOnly, isPgpInlineMode);
        }
        PendingIntent returnedPendingIntent = launchOpenPgpApiIntent(pgpApiIntent, messageContentBodyPart, shouldEncrypt || isPgpInlineMode, shouldEncrypt || !isPgpInlineMode, isPgpInlineMode);
        if (returnedPendingIntent != null) {
            queueMessageBuildPendingIntent(returnedPendingIntent, REQUEST_USER_INTERACTION);
            return;
        }
        queueMessageBuildSuccess(currentProcessedMimeMessage);
    } catch (MessagingException me) {
        queueMessageBuildException(me);
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) PendingIntent(android.app.PendingIntent)

Example 89 with MessagingException

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

the class PgpMessageBuilder method createOpenPgpDataSourceFromBodyPart.

@NonNull
private OpenPgpDataSource createOpenPgpDataSourceFromBodyPart(final MimeBodyPart bodyPart, final boolean writeBodyContentOnly) throws MessagingException {
    return new OpenPgpDataSource() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try {
                if (writeBodyContentOnly) {
                    Body body = bodyPart.getBody();
                    InputStream inputStream = body.getInputStream();
                    IOUtils.copy(inputStream, os);
                } else {
                    bodyPart.writeTo(os);
                }
            } catch (MessagingException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) InputStream(java.io.InputStream) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) IOException(java.io.IOException) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) NonNull(androidx.annotation.NonNull)

Example 90 with MessagingException

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

the class PgpMessageBuilder method launchOpenPgpApiIntent.

private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, MimeBodyPart bodyPart, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
    BinaryTempFileBody pgpResultTempBody = null;
    OutputStream outputStream = null;
    if (captureOutputPart) {
        try {
            pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
            outputStream = pgpResultTempBody.getOutputStream();
            // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
            // we need this to be CRLF, so we convert the data after receiving.
            outputStream = new EOLConvertingOutputStream(outputStream);
        } catch (IOException e) {
            throw new MessagingException("could not allocate temp file for storage!", e);
        }
    }
    Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
        case OpenPgpApi.RESULT_CODE_SUCCESS:
            mimeBuildMessage(result, bodyPart, pgpResultTempBody);
            return null;
        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
            PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
            if (returnedPendingIntent == null) {
                throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
            }
            return returnedPendingIntent;
        case OpenPgpApi.RESULT_CODE_ERROR:
            OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
            if (error == null) {
                throw new MessagingException("internal openpgp api error");
            }
            /*
                boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
                if (isOpportunisticError) {
                    if (!cryptoStatus.isEncryptionOpportunistic()) {
                        throw new IllegalStateException(
                                "Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                    }
                    Timber.d("Skipping encryption due to opportunistic mode");
                    return null;
                }
                */
            throw new MessagingException(error.getMessage());
    }
    throw new IllegalStateException("unreachable code segment reached");
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) MessagingException(com.fsck.k9.mail.MessagingException) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)159 Test (org.junit.Test)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)52 LocalFolder (com.fsck.k9.mailstore.LocalFolder)49 LocalStore (com.fsck.k9.mailstore.LocalStore)49 ArrayList (java.util.ArrayList)49 Message (com.fsck.k9.mail.Message)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)42 IOException (java.io.IOException)42 FetchProfile (com.fsck.k9.mail.FetchProfile)30 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)26 BodyPart (com.fsck.k9.mail.BodyPart)23 Part (com.fsck.k9.mail.Part)22 Account (com.fsck.k9.Account)21 Body (com.fsck.k9.mail.Body)21 TextBody (com.fsck.k9.mail.internet.TextBody)21 Date (java.util.Date)20 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)18