From 0bee705e4d758465481de6f61e08edaf2dc39dc8 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 8 Jul 2019 11:01:40 +0200 Subject: [PATCH] Improved message decoding --- .../eu/faircode/email/FragmentMessages.java | 2 +- .../java/eu/faircode/email/MessageHelper.java | 32 ++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index a82ff2f215..d10ef55490 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -3714,7 +3714,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. // Remove previously decrypted attachments for (EntityAttachment local : attachments) - if (local.encryption == null && !"application/pgp-encrypted".equals(local.type)) + if (local.encryption == null) db.attachment().deleteAttachment(local.id); int sequence = db.attachment().getAttachmentSequence(id); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index ada72edd49..82e0c1887c 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -780,7 +780,6 @@ public class MessageHelper { String getHtml(Context context) throws MessagingException, IOException { if (plain == null && html == null) { Log.i("No body part"); - warnings.add(context.getString(R.string.title_no_body)); return null; } @@ -814,6 +813,7 @@ public class MessageHelper { try { ContentType ct = new ContentType(part.getContentType()); String charset = ct.getParameter("charset"); + String encoding = null; try { String[] enc = part.getHeader("Content-Transfer-Encoding"); @@ -822,26 +822,24 @@ public class MessageHelper { } catch (MessagingException ex) { Log.w(ex); } - if (TextUtils.isEmpty(charset)) { - if (BuildConfig.DEBUG) - warnings.add(context.getString(R.string.title_no_charset, ct)); + + if (TextUtils.isEmpty(charset) || "US-ASCII".equals(charset.toUpperCase())) { // The first 127 characters are the same as in US-ASCII result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); + } else if (encoding != null && "8bit".equals(encoding.toLowerCase()) && + "ISO-8859-1".equals(charset.toUpperCase())) { + // Workaround JavaMail bug + result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); } else { - if ("US-ASCII".equals(charset.toUpperCase()) || - (encoding != null && "8bit".equals(encoding.toLowerCase()) && - "ISO-8859-1".equals(charset.toUpperCase()))) - result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); - else { - if ("US-ASCII".equals(Charset.forName(charset).name())) - warnings.add(context.getString(R.string.title_no_charset, charset)); - } + if ("US-ASCII".equals(Charset.forName(charset).name())) + warnings.add(context.getString(R.string.title_no_charset, charset)); } } catch (ParseException ex) { Log.w(ex); warnings.add(Helper.formatThrowable(ex, false)); } + // Prevent Jsoup throwing an exception result = result.replace("\0", ""); if (part.isMimeType("text/plain")) { @@ -997,10 +995,11 @@ public class MessageHelper { for (int i = 0; i < multipart.getCount(); i++) try { Part cpart = multipart.getBodyPart(i); - getMessageParts(cpart, parts, pgp); ContentType ct = new ContentType(cpart.getContentType()); if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase())) pgp = true; + else + getMessageParts(cpart, parts, pgp); } catch (ParseException ex) { // Nested body: try to continue // ParseException: In parameter list boundary="...">, expected parameter name, got ";" @@ -1029,11 +1028,6 @@ public class MessageHelper { filename = null; } - //Log.i("Part" + - // " disposition=" + disposition + - // " filename=" + filename + - // " content type=" + part.getContentType()); - if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && TextUtils.isEmpty(filename) && ((parts.plain == null && part.isMimeType("text/plain")) || @@ -1081,7 +1075,7 @@ public class MessageHelper { apart.attachment.name = "invite.ics"; // Try to guess a better content type - // Sometimes PDF files are sent using the wrong type + // For example, sometimes PDF files are sent as application/octet-stream if ("application/octet-stream".equalsIgnoreCase(apart.attachment.type)) { String extension = Helper.getExtension(apart.attachment.name); if (extension != null) {