private static Boolean isPartTextualBody(Part part) throws MessagingException { String disposition = part.getDisposition(); String dispositionType = null; String dispositionFilename = null; if (disposition != null) { dispositionType = MimeUtility.getHeaderParameter(disposition, null); dispositionFilename = MimeUtility.getHeaderParameter(disposition, "filename"); } boolean isAttachmentDisposition = "attachment".equalsIgnoreCase(dispositionType) || dispositionFilename != null; return !isAttachmentDisposition && (part.isMimeType("text/html") || part.isMimeType("text/plain") || part.isMimeType("application/pgp")); }
public static boolean isPartPgpInlineEncrypted(@Nullable Part part) { if (part == null) { return false; } if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); if (StringHelper.isNullOrEmpty(text)) { return false; } text = text.trim(); return text.startsWith(PGP_INLINE_START_MARKER); }
@VisibleForTesting static boolean isPartPgpInlineEncryptedOrSigned(Part part) { if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); if (StringHelper.isNullOrEmpty(text)) { return false; } text = text.trim(); return text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER); }
private static Part findPrimaryPartInAlternative(Part part) { Body body = part.getBody(); if (part.isMimeType("multipart/alternative") && body instanceof Multipart) { Multipart multipart = (Multipart) body; if (multipart.getCount() == 0) { return null; } BodyPart firstBodyPart = multipart.getBodyPart(0); if (isPartPgpInlineEncryptedOrSigned(firstBodyPart)) { return firstBodyPart; } } return null; }
@Nullable private static Part findPrimaryPartInMixed(Part part, List<Part> outputExtraParts) { Body body = part.getBody(); boolean isMultipartMixed = part.isMimeType("multipart/mixed") && body instanceof Multipart; if (!isMultipartMixed) { return null; } Multipart multipart = (Multipart) body; if (multipart.getCount() == 0) { return null; } BodyPart firstBodyPart = multipart.getBodyPart(0); Part foundPart; if (isPartEncryptedOrSigned(firstBodyPart)) { foundPart = firstBodyPart; } else { foundPart = findPrimaryPartInAlternative(firstBodyPart); } if (foundPart != null && outputExtraParts != null) { for (int i = 1; i < multipart.getCount(); i++) { outputExtraParts.add(multipart.getBodyPart(i)); } } return foundPart; }
public static String getTextFromPart(Part part, long textSizeLimit) { try { if ((part != null) && (part.getBody() != null)) { final Body body = part.getBody(); if (body instanceof TextBody) { return ((TextBody) body).getRawText(); } final String mimeType = part.getMimeType(); if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") || part.isMimeType("application/pgp")) { return getTextFromTextPart(part, body, mimeType, textSizeLimit); } else { throw new MessagingException("Provided non-text part: " + mimeType); } } else { throw new MessagingException("Provided invalid part"); } } catch (IOException e) { Timber.e(e, "Unable to getTextFromPart"); } catch (MessagingException e) { Timber.e("Unable to getTextFromPart"); } return null; }