Merge pull request #5325 from k9mail/GH-5324_strip_signature_crash
This commit is contained in:
commit
a72dac6e9d
2 changed files with 38 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
|||
package com.fsck.k9.message.extractors;
|
||||
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import timber.log.Timber;
|
||||
|
||||
import com.fsck.k9.mail.Part;
|
||||
|
@ -15,6 +16,7 @@ public class BodyTextExtractor {
|
|||
/** Fetch the body text from a messagePart in the desired messagePart format. This method handles
|
||||
* conversions between formats (html to text and vice versa) if necessary.
|
||||
*/
|
||||
@NonNull
|
||||
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
|
||||
Part part;
|
||||
if (format == SimpleMessageFormat.HTML) {
|
||||
|
@ -22,13 +24,13 @@ public class BodyTextExtractor {
|
|||
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
|
||||
if (part != null) {
|
||||
Timber.d("getBodyTextFromMessage: HTML requested, HTML found.");
|
||||
return MessageExtractor.getTextFromPart(part);
|
||||
return getTextFromPartOrEmpty(part);
|
||||
}
|
||||
|
||||
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
|
||||
if (part != null) {
|
||||
Timber.d("getBodyTextFromMessage: HTML requested, text found.");
|
||||
String text = MessageExtractor.getTextFromPart(part);
|
||||
String text = getTextFromPartOrEmpty(part);
|
||||
return HtmlConverter.textToHtml(text);
|
||||
}
|
||||
} else if (format == SimpleMessageFormat.TEXT) {
|
||||
|
@ -36,13 +38,13 @@ public class BodyTextExtractor {
|
|||
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
|
||||
if (part != null) {
|
||||
Timber.d("getBodyTextFromMessage: Text requested, text found.");
|
||||
return MessageExtractor.getTextFromPart(part);
|
||||
return getTextFromPartOrEmpty(part);
|
||||
}
|
||||
|
||||
part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
|
||||
if (part != null) {
|
||||
Timber.d("getBodyTextFromMessage: Text requested, HTML found.");
|
||||
String text = MessageExtractor.getTextFromPart(part);
|
||||
String text = getTextFromPartOrEmpty(part);
|
||||
return HtmlConverter.htmlToText(text);
|
||||
}
|
||||
}
|
||||
|
@ -50,4 +52,10 @@ public class BodyTextExtractor {
|
|||
// If we had nothing interesting, return an empty string.
|
||||
return "";
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static String getTextFromPartOrEmpty(Part part) {
|
||||
String text = MessageExtractor.getTextFromPart(part);
|
||||
return text != null ? text : "";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,27 +44,33 @@ public class MessageExtractor {
|
|||
}
|
||||
|
||||
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");
|
||||
if (part == null) {
|
||||
throw new IllegalArgumentException("Argument 'part' must not be null");
|
||||
}
|
||||
|
||||
try {
|
||||
Body body = part.getBody();
|
||||
if (body == null) {
|
||||
Timber.v("No body present for this message part");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (body instanceof TextBody) {
|
||||
TextBody textBody = (TextBody) body;
|
||||
return textBody.getRawText();
|
||||
}
|
||||
|
||||
String mimeType = part.getMimeType();
|
||||
if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") ||
|
||||
part.isMimeType("application/pgp")) {
|
||||
return getTextFromTextPart(part, body, mimeType, textSizeLimit);
|
||||
}
|
||||
|
||||
Timber.w("Provided non-text part: %s", mimeType);
|
||||
} catch (IOException | MessagingException e) {
|
||||
Timber.e(e, "Unable to getTextFromPart");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue