Attempt to fix broken encodings

This commit is contained in:
M66B 2019-02-06 09:29:08 +00:00
parent ce3e38c9c6
commit 0b08d00d61
1 changed files with 38 additions and 0 deletions

View File

@ -35,6 +35,7 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
@ -58,6 +59,7 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;
@ -70,6 +72,13 @@ public class MessageHelper {
static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
private static final List<String> ENCODINGS = Arrays.asList(
"base64",
"quoted-printable",
"uuencode", "x-uuencode", "x-uue",
"binary", "7bit", "8bit"
);
static Properties getSessionProperties(int auth_type, String realm, boolean insecure) {
Properties props = new Properties();
@ -595,6 +604,8 @@ public class MessageHelper {
Part part = (html == null ? plain : html);
try {
fixEncoding((MimePart) part);
Object content = part.getContent();
if (content instanceof String)
result = (String) content;
@ -633,6 +644,33 @@ public class MessageHelper {
return result;
}
private void fixEncoding(MimePart part) throws MessagingException {
String mencoding = part.getEncoding();
if (mencoding == null || ENCODINGS.contains(mencoding))
return;
String cte = part.getHeader("Content-Transfer-Encoding")[0];
Log.i("Attempting to fix encoding=" + cte);
for (String encoding : ENCODINGS) {
boolean contains = true;
for (String pencoding : encoding.split("-"))
contains = (contains && mencoding.toLowerCase().contains(pencoding));
if (contains) {
String warning = "Changed invalid encoding " + cte + " to " + encoding;
Log.w(warning);
warnings.add(warning);
part.setHeader("Content-Transfer-Encoding", encoding);
return;
}
}
String warning = "Unfixable encoding " + cte;
Log.w(warning);
warnings.add(warning);
}
List<EntityAttachment> getAttachments() throws MessagingException {
List<EntityAttachment> result = new ArrayList<>();