diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/DecryptStreamParser.java b/k9mail/src/main/java/com/fsck/k9/mailstore/DecryptStreamParser.java index e38da1ce9..13cd6c87d 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/DecryptStreamParser.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/DecryptStreamParser.java @@ -24,6 +24,8 @@ import org.apache.james.mime4j.parser.MimeStreamParser; import org.apache.james.mime4j.stream.BodyDescriptor; import org.apache.james.mime4j.stream.Field; import org.apache.james.mime4j.stream.MimeConfig; +import org.openintents.openpgp.OpenPgpError; +import org.openintents.openpgp.OpenPgpSignatureResult; public class DecryptStreamParser { @@ -181,8 +183,27 @@ public class DecryptStreamParser { } public static class DecryptedBodyPart extends MimeBodyPart { + private OpenPgpSignatureResult signatureResult; + private OpenPgpError error; + public DecryptedBodyPart() throws MessagingException { // Do nothing } + + public OpenPgpSignatureResult getSignatureResult() { + return signatureResult; + } + + public void setSignatureResult(OpenPgpSignatureResult signatureResult) { + this.signatureResult = signatureResult; + } + + public OpenPgpError getError() { + return error; + } + + public void setError(OpenPgpError error) { + this.error = error; + } } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java index a021bcede..95aaf3794 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java @@ -4,15 +4,20 @@ import android.content.Context; import android.net.Uri; import com.fsck.k9.R; +import com.fsck.k9.crypto.MessageDecryptor; import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; import com.fsck.k9.helper.HtmlConverter; import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.internet.Viewable; +import com.fsck.k9.mailstore.DecryptStreamParser.DecryptedBodyPart; import com.fsck.k9.mailstore.MessageViewInfo.MessageViewContainer; import com.fsck.k9.provider.AttachmentProvider; import org.openintents.openpgp.OpenPgpSignatureResult; @@ -36,6 +41,7 @@ public class LocalMessageExtractor { private static final int FILENAME_PREFIX_LENGTH = FILENAME_PREFIX.length(); private static final String FILENAME_SUFFIX = " "; private static final int FILENAME_SUFFIX_LENGTH = FILENAME_SUFFIX.length(); + private static final OpenPgpSignatureResult NO_SIGNATURE_RESULT = null; private LocalMessageExtractor() {} /** @@ -433,8 +439,7 @@ public class LocalMessageExtractor { attachments); List attachmentInfos = extractAttachmentInfos(attachments); - // TODO correctly extract OpenPgpSignatureResult and add to MessageViewContainer - OpenPgpSignatureResult result = null; + OpenPgpSignatureResult result = getSignatureResultForPart(part); containers.add(new MessageViewContainer(viewable.html, attachmentInfos, result, false, null)); } @@ -442,6 +447,30 @@ public class LocalMessageExtractor { return new MessageViewInfo(containers, message); } + private static OpenPgpSignatureResult getSignatureResultForPart(Part part) { + if (!MessageDecryptor.isPgpMimeEncryptedPart(part)) { + return NO_SIGNATURE_RESULT; + } + + Body body = part.getBody(); + if (!(body instanceof Multipart)) { + return NO_SIGNATURE_RESULT; + } + + Multipart multipart = (Multipart) body; + if (multipart.getCount() < 3) { + return NO_SIGNATURE_RESULT; + } + + BodyPart bodyPart = multipart.getBodyPart(2); + if (!(bodyPart instanceof DecryptedBodyPart)) { + return NO_SIGNATURE_RESULT; + } + + DecryptedBodyPart decryptedBodyPart = (DecryptedBodyPart) bodyPart; + return decryptedBodyPart.getSignatureResult(); + } + private static List extractAttachmentInfos(List attachmentParts) throws MessagingException { diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 9228e5845..3deb3ca4a 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -71,6 +71,8 @@ import com.fsck.k9.ui.message.DecodeMessageLoader; import com.fsck.k9.ui.message.LocalMessageLoader; import com.fsck.k9.view.MessageHeader; import org.openintents.openpgp.IOpenPgpService; +import org.openintents.openpgp.OpenPgpError; +import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback; import org.openintents.openpgp.util.OpenPgpServiceConnection; @@ -433,14 +435,20 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF break; } case OpenPgpApi.RESULT_CODE_ERROR: { + OpenPgpError error = currentDecryptingResult.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + if (K9.DEBUG) { - String errorMessage = currentDecryptingResult.getStringExtra(OpenPgpApi.RESULT_ERROR); - Log.w(K9.LOG_TAG, "OpenPGP API error: " + errorMessage); + Log.w(K9.LOG_TAG, "OpenPGP API error: " + error.getMessage()); } - onDecryptionFailed(); + + onDecryptionFailed(error); break; } case OpenPgpApi.RESULT_CODE_SUCCESS: { + OpenPgpSignatureResult signatureResult = + currentDecryptingResult.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); + decryptedPart.setSignatureResult(signatureResult); + onDecryptionSuccess(decryptedPart); break; } @@ -454,7 +462,8 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF if (resultCode == Activity.RESULT_OK) { decryptNextPartOrStartExtractingTextAndAttachments(); } else { - onDecryptionFailed(); + //FIXME: don't pass null + onDecryptionFailed(null); } } @@ -468,8 +477,14 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF multipart.addBodyPart(decryptedPart); } - private void onDecryptionFailed() { - // TODO: display error to user? + private void onDecryptionFailed(OpenPgpError error) { + try { + DecryptedBodyPart errorPart = new DecryptedBodyPart(); + errorPart.setError(error); + addDecryptedPartToMessage(errorPart); + } catch (MessagingException e) { + Log.e(K9.LOG_TAG, "This shouldn't happen", e); + } onDecryptionFinished(); }