use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.
the class MessageExtractor method findViewablesAndAttachments.
/** Traverse the MIME tree of a message and extract viewable parts. */
public static void findViewablesAndAttachments(Part part, @Nullable List<Viewable> outputViewableParts, @Nullable List<Part> outputNonViewableParts) throws MessagingException {
boolean skipSavingNonViewableParts = outputNonViewableParts == null;
boolean skipSavingViewableParts = outputViewableParts == null;
if (skipSavingNonViewableParts && skipSavingViewableParts) {
throw new IllegalArgumentException("method was called but no output is to be collected - this a bug!");
}
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart multipart = (Multipart) body;
if (isSameMimeType(part.getMimeType(), "multipart/alternative")) {
/*
* For multipart/alternative parts we try to find a text/plain and a text/html
* child. Everything else we find is put into 'attachments'.
*/
List<Viewable> text = findTextPart(multipart, true);
Set<Part> knownTextParts = getParts(text);
List<Viewable> html = findHtmlPart(multipart, knownTextParts, outputNonViewableParts, true);
if (skipSavingViewableParts) {
return;
}
if (!text.isEmpty() || !html.isEmpty()) {
Alternative alternative = new Alternative(text, html);
outputViewableParts.add(alternative);
}
} else {
// For all other multipart parts we recurse to grab all viewable children.
for (Part bodyPart : multipart.getBodyParts()) {
findViewablesAndAttachments(bodyPart, outputViewableParts, outputNonViewableParts);
}
}
} else if (body instanceof Message && !("attachment".equalsIgnoreCase(getContentDisposition(part)))) {
if (skipSavingViewableParts) {
return;
}
/*
* We only care about message/rfc822 parts whose Content-Disposition header has a value
* other than "attachment".
*/
Message message = (Message) body;
// We add the Message object so we can extract the filename later.
outputViewableParts.add(new MessageHeader(part, message));
// Recurse to grab all viewable parts and attachments from that message.
findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
} else if (isPartTextualBody(part)) {
if (skipSavingViewableParts) {
return;
}
String mimeType = part.getMimeType();
Viewable viewable;
if (isSameMimeType(mimeType, "text/plain")) {
if (isFormatFlowed(part.getContentType())) {
viewable = new Flowed(part);
} else {
viewable = new Text(part);
}
} else {
viewable = new Html(part);
}
outputViewableParts.add(viewable);
} else if (isSameMimeType(part.getMimeType(), "application/pgp-signature")) {
// ignore this type explicitly
} else {
if (skipSavingNonViewableParts) {
return;
}
// Everything else is treated as attachment.
outputNonViewableParts.add(part);
}
}
use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.
the class MessageExtractor method getMessageFromPart.
private static Message getMessageFromPart(Part part) {
while (part != null) {
if (part instanceof Message)
return (Message) part;
if (!(part instanceof BodyPart))
return null;
Multipart multipart = ((BodyPart) part).getParent();
if (multipart == null)
return null;
part = multipart.getParent();
}
return null;
}
use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.
the class Multipart method setCharset.
public void setCharset(String charset) throws MessagingException {
if (mParts.isEmpty())
return;
BodyPart part = mParts.get(0);
Body body = part.getBody();
if (body instanceof TextBody) {
CharsetSupport.setCharset(charset, part);
((TextBody) body).setCharset(charset);
}
}
use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.
the class PgpMessageBuilder method launchOpenPgpApiIntent.
private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
final MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart();
String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE);
if (contentType.length > 0) {
bodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType[0]);
}
OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
BinaryTempFileBody pgpResultTempBody = null;
OutputStream outputStream = null;
if (captureOutputPart) {
try {
pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
outputStream = pgpResultTempBody.getOutputStream();
// OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
// we need this to be CRLF, so we convert the data after receiving.
outputStream = new EOLConvertingOutputStream(outputStream);
} catch (IOException e) {
throw new MessagingException("could not allocate temp file for storage!", e);
}
}
Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
mimeBuildMessage(result, bodyPart, pgpResultTempBody);
return null;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
if (returnedPendingIntent == null) {
throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
}
return returnedPendingIntent;
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
if (error == null) {
throw new MessagingException("internal openpgp api error");
}
boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
if (isOpportunisticError) {
if (!cryptoStatus.isEncryptionOpportunistic()) {
throw new IllegalStateException("Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
}
Timber.d("Skipping encryption due to opportunistic mode");
return null;
}
throw new MessagingException(error.getMessage());
}
throw new IllegalStateException("unreachable code segment reached");
}
use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.
the class EncryptionDetector method containsPartWithMimeType.
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;
}
Aggregations