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;
}
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;
}
}
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);
}
}
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);
}
}
};
}
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");
}
Aggregations