Improved message decoding

This commit is contained in:
M66B 2019-07-08 11:01:40 +02:00
parent b31df5d1a4
commit 0bee705e4d
2 changed files with 14 additions and 20 deletions

View File

@ -3714,7 +3714,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
// Remove previously decrypted attachments // Remove previously decrypted attachments
for (EntityAttachment local : attachments) for (EntityAttachment local : attachments)
if (local.encryption == null && !"application/pgp-encrypted".equals(local.type)) if (local.encryption == null)
db.attachment().deleteAttachment(local.id); db.attachment().deleteAttachment(local.id);
int sequence = db.attachment().getAttachmentSequence(id); int sequence = db.attachment().getAttachmentSequence(id);

View File

@ -780,7 +780,6 @@ public class MessageHelper {
String getHtml(Context context) throws MessagingException, IOException { String getHtml(Context context) throws MessagingException, IOException {
if (plain == null && html == null) { if (plain == null && html == null) {
Log.i("No body part"); Log.i("No body part");
warnings.add(context.getString(R.string.title_no_body));
return null; return null;
} }
@ -814,6 +813,7 @@ public class MessageHelper {
try { try {
ContentType ct = new ContentType(part.getContentType()); ContentType ct = new ContentType(part.getContentType());
String charset = ct.getParameter("charset"); String charset = ct.getParameter("charset");
String encoding = null; String encoding = null;
try { try {
String[] enc = part.getHeader("Content-Transfer-Encoding"); String[] enc = part.getHeader("Content-Transfer-Encoding");
@ -822,26 +822,24 @@ public class MessageHelper {
} catch (MessagingException ex) { } catch (MessagingException ex) {
Log.w(ex); Log.w(ex);
} }
if (TextUtils.isEmpty(charset)) {
if (BuildConfig.DEBUG) if (TextUtils.isEmpty(charset) || "US-ASCII".equals(charset.toUpperCase())) {
warnings.add(context.getString(R.string.title_no_charset, ct));
// The first 127 characters are the same as in US-ASCII // The first 127 characters are the same as in US-ASCII
result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); result = new String(result.getBytes(StandardCharsets.ISO_8859_1));
} else { } else if (encoding != null && "8bit".equals(encoding.toLowerCase()) &&
if ("US-ASCII".equals(charset.toUpperCase()) || "ISO-8859-1".equals(charset.toUpperCase())) {
(encoding != null && "8bit".equals(encoding.toLowerCase()) && // Workaround JavaMail bug
"ISO-8859-1".equals(charset.toUpperCase())))
result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); result = new String(result.getBytes(StandardCharsets.ISO_8859_1));
else { } else {
if ("US-ASCII".equals(Charset.forName(charset).name())) if ("US-ASCII".equals(Charset.forName(charset).name()))
warnings.add(context.getString(R.string.title_no_charset, charset)); warnings.add(context.getString(R.string.title_no_charset, charset));
} }
}
} catch (ParseException ex) { } catch (ParseException ex) {
Log.w(ex); Log.w(ex);
warnings.add(Helper.formatThrowable(ex, false)); warnings.add(Helper.formatThrowable(ex, false));
} }
// Prevent Jsoup throwing an exception
result = result.replace("\0", ""); result = result.replace("\0", "");
if (part.isMimeType("text/plain")) { if (part.isMimeType("text/plain")) {
@ -997,10 +995,11 @@ public class MessageHelper {
for (int i = 0; i < multipart.getCount(); i++) for (int i = 0; i < multipart.getCount(); i++)
try { try {
Part cpart = multipart.getBodyPart(i); Part cpart = multipart.getBodyPart(i);
getMessageParts(cpart, parts, pgp);
ContentType ct = new ContentType(cpart.getContentType()); ContentType ct = new ContentType(cpart.getContentType());
if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase())) if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase()))
pgp = true; pgp = true;
else
getMessageParts(cpart, parts, pgp);
} catch (ParseException ex) { } catch (ParseException ex) {
// Nested body: try to continue // Nested body: try to continue
// ParseException: In parameter list boundary="...">, expected parameter name, got ";" // ParseException: In parameter list boundary="...">, expected parameter name, got ";"
@ -1029,11 +1028,6 @@ public class MessageHelper {
filename = null; filename = null;
} }
//Log.i("Part" +
// " disposition=" + disposition +
// " filename=" + filename +
// " content type=" + part.getContentType());
if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) &&
TextUtils.isEmpty(filename) && TextUtils.isEmpty(filename) &&
((parts.plain == null && part.isMimeType("text/plain")) || ((parts.plain == null && part.isMimeType("text/plain")) ||
@ -1081,7 +1075,7 @@ public class MessageHelper {
apart.attachment.name = "invite.ics"; apart.attachment.name = "invite.ics";
// Try to guess a better content type // 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)) { if ("application/octet-stream".equalsIgnoreCase(apart.attachment.type)) {
String extension = Helper.getExtension(apart.attachment.name); String extension = Helper.getExtension(apart.attachment.name);
if (extension != null) { if (extension != null) {