From 85da3d8667488d19ebfa17a1bc7690fbfd8b49ee Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Sun, 8 Apr 2012 12:38:45 -0400 Subject: [PATCH] workaround to detect the charset from HTML mail without charset parameter on the header. Conflicts: src/com/fsck/k9/mail/internet/MimeUtility.java --- .../fsck/k9/mail/internet/MimeUtility.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index e1908670d..38658adf7 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -15,6 +15,7 @@ import org.apache.james.mime4j.codec.QuotedPrintableInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; @@ -22,6 +23,7 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; @@ -1033,6 +1035,25 @@ public class MimeUtility { * We've got a text part, so let's see if it needs to be processed further. */ String charset = getHeaderParameter(part.getContentType(), "charset"); + /* + * determine the charset from HTML message. + */ + if (mimeType.equalsIgnoreCase("text/html") && charset == null) { + InputStreamReader in = new InputStreamReader(part.getBody().getInputStream(), + "US-ASCII"); + char[] buf = new char[256]; + in.read(buf, 0, buf.length); + String str = new String(buf); + + if (str.length() == 0) { + return ""; + } + Pattern p = Pattern.compile("", Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(str); + if (m.find()) { + charset = m.group(1); + } + } charset = fixupCharset(charset, getMessageFromPart(part)); /*