From f30c26757a2019d29e3023013f8af043c94722af Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 21 Feb 2021 08:56:21 +0100 Subject: [PATCH] Improved body structure workaround --- .../java/com/sun/mail/imap/IMAPMessage.java | 20 +++++++++---- .../java/eu/faircode/email/MessageHelper.java | 29 ++----------------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/sun/mail/imap/IMAPMessage.java b/app/src/main/java/com/sun/mail/imap/IMAPMessage.java index 4f92211b4d..a7b6170f90 100644 --- a/app/src/main/java/com/sun/mail/imap/IMAPMessage.java +++ b/app/src/main/java/com/sun/mail/imap/IMAPMessage.java @@ -574,15 +574,23 @@ public class IMAPMessage extends MimeMessage implements ReadableMime { if (type == null) { loadBODYSTRUCTURE(); - // Some servers report incorrectly text/plain in some situations - if ("text".equalsIgnoreCase(bs.type) && - "plain".equalsIgnoreCase(bs.subtype)) + // Some servers, like Yandex, report an incorrect/incomplete BODYSTRUCTURE + if (("text".equalsIgnoreCase(bs.type) && + "plain".equalsIgnoreCase(bs.subtype)) || + ("multipart".equalsIgnoreCase(bs.type) && + ("signed".equalsIgnoreCase(bs.subtype) || "encrypted".equalsIgnoreCase(bs.subtype)) && + (bs.cParams == null || bs.cParams.get("protocol") == null)) || + ("application".equalsIgnoreCase(bs.type) && + ("pkcs7-mime".equalsIgnoreCase(bs.subtype) || "x-pkcs7-mime".equalsIgnoreCase(bs.subtype)) && + (bs.cParams == null || bs.cParams.get("smime-type") == null))) try { - String[] c = getHeader("Content-type"); + String[] c = getHeader("Content-type"); // Fetches header if (c != null && c.length == 1) { ContentType ct = new ContentType(c[0]); - if (!bs.type.equalsIgnoreCase(ct.getPrimaryType()) || - !bs.subtype.equalsIgnoreCase(ct.getSubType())) { + if (!("text".equalsIgnoreCase(bs.type) && + "plain".equalsIgnoreCase(bs.subtype)) || + !(bs.type.equalsIgnoreCase(ct.getPrimaryType()) && + bs.subtype.equalsIgnoreCase(ct.getSubType()))) { eu.faircode.email.Log.e("Inconsistent" + " bs=" + bs.type + "/" + bs.subtype + "/" + bs.cParams + " header=" + ct); type = ct.toString(); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 57f9684bb8..1da5cd03de 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -2645,32 +2645,9 @@ public class MessageHelper { try { if (imessage instanceof IMAPMessage) { - if (structure) { - String contentType = imessage.getContentType(); // force loadBODYSTRUCTURE - - // Workaround protocol parameter missing - // Happens with Yandex and possibly other providers - boolean load = false; - try { - ContentType ct = new ContentType(contentType); - if (ct.match("multipart/signed") || ct.match("multipart/encrypted")) { - String protocol = ct.getParameter("protocol"); - if (protocol == null) - load = true; - } else if (ct.match("application/pkcs7-mime") || ct.match("application/x-pkcs7-mime")) { - String smimeType = ct.getParameter("smime-type"); - if (smimeType == null) - load = true; - } - } catch (Throwable ex) { - Log.w(ex); - } - - if (load) { - Log.w("Protocol missing content-type=" + contentType); - throw new MessagingException("Failed to load IMAP envelope"); - } - } else { + if (structure) + imessage.getContentType(); // force loadBODYSTRUCTURE + else { if (headers) imessage.getAllHeaders(); // force loadHeaders else