public static boolean isMultipartEncryptedOpenPgpProtocol(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED)) { throw new IllegalArgumentException("Part is not multipart/encrypted!"); } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); return APPLICATION_PGP_ENCRYPTED.equalsIgnoreCase(protocolParameter); }
private void missingPartToContentValues(ContentValues cv, Part part) throws MessagingException { AttachmentViewInfo attachment = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); cv.put("display_name", attachment.displayName); cv.put("data_location", DataLocation.MISSING); cv.put("decoded_body_size", attachment.size); if (MimeUtility.isMultipart(part.getMimeType())) { cv.put("boundary", BoundaryGenerator.getInstance().generateBoundary()); } }
private String convertFromHtmlIfNecessary(Part textPart, String text) { String mimeType = textPart.getMimeType(); if (!isSameMimeType(mimeType, "text/html")) { return text; } return HtmlConverter.htmlToText(text); }
public static boolean isMultipartSignedOpenPgpProtocol(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) { throw new IllegalArgumentException("Part is not multipart/signed!"); } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); return APPLICATION_PGP_SIGNATURE.equalsIgnoreCase(protocolParameter); }
public static void setCharset(String charset, Part part) { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, part.getMimeType() + ";\r\n charset=" + getExternalCharset(charset)); }
public String createFulltext(@NonNull Message message) { Part textPart = textPartFinder.findFirstTextPart(message); if (textPart == null || hasEmptyBody(textPart)) { return null; } String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_FTS); String mimeType = textPart.getMimeType(); if (!MimeUtility.isSameMimeType(mimeType, "text/html")) { return text; } return HtmlConverter.htmlToText(text); }
@Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; }
private long updateOrInsertMessagePart(SQLiteDatabase db, ContentValues cv, Part part, long existingMessagePartId) throws IOException, MessagingException { byte[] headerBytes = getHeaderBytes(part); cv.put("mime_type", part.getMimeType()); cv.put("header", headerBytes); cv.put("type", MessagePartType.UNKNOWN); File file = null; Body body = part.getBody(); if (body instanceof Multipart) { multipartToContentValues(cv, (Multipart) body); } else if (body == null) { missingPartToContentValues(cv, part); } else if (body instanceof Message) { messageMarkerToContentValues(cv); } else { file = leafPartToContentValues(cv, part, body); } long messagePartId; if (existingMessagePartId != INVALID_MESSAGE_PART_ID) { messagePartId = existingMessagePartId; db.update("message_parts", cv, "id = ?", new String[] { Long.toString(messagePartId) }); } else { messagePartId = db.insertOrThrow("message_parts", null, cv); } if (file != null) { moveTemporaryFile(file, Long.toString(messagePartId)); } return messagePartId; }
private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) { String mimeType = part.getMimeType(); if (isMimeTypeAnyOf(mimeType, wantedMimeTypes)) { return true; } Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (BodyPart bodyPart : multipart.getBodyParts()) { if (containsPartWithMimeType(bodyPart, wantedMimeTypes)) { return true; } } } return false; }
isSameMimeType(part.getMimeType(), "text/html")) { Html html = new Html(part); viewables.add(html);
} else if (isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/plain")) { Text text = new Text(part); viewables.add(text);
public static Part findFirstPartByMimeType(Part part, String mimeType) { if (part.getBody() instanceof Multipart) { Multipart multipart = (Multipart)part.getBody(); for (BodyPart bodyPart : multipart.getBodyParts()) { Part ret = MimeUtility.findFirstPartByMimeType(bodyPart, mimeType); if (ret != null) { return ret; } } } else if (isSameMimeType(part.getMimeType(), mimeType)) { return part; } return null; }
if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(part.getMimeType(), "multipart/alternative")) { outputViewableParts.add(alternative); } else if (isSameMimeType(part.getMimeType(), "multipart/signed")) { if (multipart.getCount() > 0) { BodyPart bodyPart = multipart.getBodyPart(0); return; String mimeType = part.getMimeType(); Viewable viewable; if (isSameMimeType(mimeType, "text/plain")) { } else if (isSameMimeType(part.getMimeType(), "application/pgp-signature")) { } else if (isSameMimeType(part.getMimeType(), "text/rfc822-headers")) {
@WorkerThread private AttachmentViewInfo extractAttachmentInfo(Part part, Uri uri, long size, boolean isContentAvailable) { boolean inlineAttachment = false; String mimeType = part.getMimeType(); String contentTypeHeader = part.getContentType(); String contentDisposition = part.getDisposition(); String name = MimeUtility.getHeaderParameter(contentDisposition, "filename"); if (name == null) { name = MimeUtility.getHeaderParameter(contentTypeHeader, "name"); } if (name == null) { String extension = null; if (mimeType != null) { extension = MimeUtility.getExtensionByMimeType(mimeType); } name = "noname" + ((extension != null) ? "." + extension : ""); } // Inline parts with a Content-Id header and a MIME type of image/* are probably components of an HTML message, // not attachments. if (contentDisposition != null && MimeUtility.getHeaderParameter(contentDisposition, null).matches("^(?i:inline)") && part.getHeader(MimeHeader.HEADER_CONTENT_ID).length > 0 && mimeType != null && mimeType.toLowerCase(Locale.ROOT).startsWith("image/")) { inlineAttachment = true; } long attachmentSize = extractAttachmentSize(contentDisposition, size); return new AttachmentViewInfo(mimeType, name, attachmentSize, uri, inlineAttachment, part, isContentAvailable); }
String parentMimeType = parentPart.getMimeType(); if (MimeUtility.isMultipart(parentMimeType)) { BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), message, id, size);
private Part findTextPartInMultipartAlternative(Multipart multipart) { Part htmlPart = null; for (BodyPart bodyPart : multipart.getBodyParts()) { String mimeType = bodyPart.getMimeType(); Body body = bodyPart.getBody(); if (body instanceof Multipart) { Part candidatePart = findFirstTextPart(bodyPart); if (candidatePart != null) { if (isSameMimeType(candidatePart.getMimeType(), "text/html")) { htmlPart = candidatePart; } else { return candidatePart; } } } else if (isSameMimeType(mimeType, "text/plain")) { return bodyPart; } else if (isSameMimeType(mimeType, "text/html") && htmlPart == null) { htmlPart = bodyPart; } } if (htmlPart != null) { return htmlPart; } return null; }
public static boolean isPartMultipartEncrypted(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED)) { return false; } if (! (part.getBody() instanceof MimeMultipart)) { return false; } MimeMultipart mimeMultipart = (MimeMultipart) part.getBody(); if (mimeMultipart.getCount() != 2) { return false; } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); // for partially downloaded messages the protocol parameter isn't yet available, so we'll just assume it's ok boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(1).getBody() == null; boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(0).getMimeType()); return dataUnavailable || protocolMatches; }
private static boolean isPartMultipartSigned(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) { return false; } if (! (part.getBody() instanceof MimeMultipart)) { return false; } MimeMultipart mimeMultipart = (MimeMultipart) part.getBody(); if (mimeMultipart.getCount() != 2) { return false; } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); // for partially downloaded messages the protocol parameter isn't yet available, so we'll just assume it's ok boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(0).getBody() == null; boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(1).getMimeType()); return dataUnavailable || protocolMatches; }
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; }
DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else {