From 2fa0b0564bdaf056f4d8eb73de521023b6e326c1 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 21 Jan 2019 17:19:02 +0000 Subject: [PATCH] Handle message part errors --- .../java/eu/faircode/email/MessageHelper.java | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 26989d74e1..b787181ba3 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -740,65 +740,70 @@ public class MessageHelper { Part part; } - MessageParts getMessageParts() throws IOException, MessagingException { + MessageParts getMessageParts() throws IOException { MessageParts parts = new MessageParts(); getMessageParts(imessage, parts, false); // Can throw ParseException return parts; } - private void getMessageParts(Part part, MessageParts parts, boolean pgp) throws MessagingException, IOException { - if (part.isMimeType("multipart/*")) { - Multipart multipart = (Multipart) part.getContent(); - for (int i = 0; i < multipart.getCount(); i++) + private void getMessageParts(Part part, MessageParts parts, boolean pgp) throws IOException { + try { + if (part.isMimeType("multipart/*")) { + Multipart multipart = (Multipart) part.getContent(); + 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; + } catch (ParseException ex) { + // Nested body: try to continue + // ParseException: In parameter list boundary="...">, expected parameter name, got ";" + Log.w(ex); + } + } else { + // https://www.iana.org/assignments/cont-disp/cont-disp.xhtml + String disposition; 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; - } catch (ParseException ex) { - // Nested body: try to continue - // ParseException: In parameter list boundary="...">, expected parameter name, got ";" + disposition = part.getDisposition(); + } catch (MessagingException ex) { Log.w(ex); + disposition = null; } - } else { - // https://www.iana.org/assignments/cont-disp/cont-disp.xhtml - String disposition; - try { - disposition = part.getDisposition(); - } catch (MessagingException ex) { - Log.w(ex); - disposition = null; - } - String filename; - try { - filename = part.getFileName(); - } catch (MessagingException ex) { - Log.w(ex); - filename = null; - } + String filename; + try { + filename = part.getFileName(); + } catch (MessagingException ex) { + Log.w(ex); + filename = null; + } - //Log.i("Part" + - // " disposition=" + disposition + - // " filename=" + filename + - // " content type=" + part.getContentType()); + //Log.i("Part" + + // " disposition=" + disposition + + // " filename=" + filename + + // " content type=" + part.getContentType()); - if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && - ((parts.plain == null && part.isMimeType("text/plain")) || - (parts.html == null && part.isMimeType("text/html")))) { - if (part.isMimeType("text/plain")) - parts.plain = part; - else - parts.html = part; - } else { - AttachmentPart apart = new AttachmentPart(); - apart.disposition = disposition; - apart.filename = filename; - apart.pgp = pgp; - apart.part = part; - parts.attachments.add(apart); + if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && + ((parts.plain == null && part.isMimeType("text/plain")) || + (parts.html == null && part.isMimeType("text/html")))) { + if (part.isMimeType("text/plain")) + parts.plain = part; + else + parts.html = part; + } else { + AttachmentPart apart = new AttachmentPart(); + apart.disposition = disposition; + apart.filename = filename; + apart.pgp = pgp; + apart.part = part; + parts.attachments.add(apart); + } } + } catch (MessagingException ex) { + Log.w(ex); + parts.warnings.add(Helper.formatThrowable(ex)); } }