1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-01-01 12:44:42 +00:00

Fixing header encoding in some more cases

This commit is contained in:
M66B 2020-04-06 09:46:42 +02:00
parent 1e3e37d3ce
commit f3fa8cb065
2 changed files with 32 additions and 8 deletions

View file

@ -856,6 +856,19 @@ public class Helper {
return true;
}
static boolean isISO8859(String text) {
// https://en.wikipedia.org/wiki/ISO/IEC_8859-1
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
for (byte b : octets) {
int c = b & 0xFF;
if (c < 32)
return false;
if (c >= 127 && c < 160)
return false;
}
return true;
}
// Files
static String sanitizeFilename(String name) {

View file

@ -995,15 +995,29 @@ public class MessageHelper {
return null;
}
private String fixEncoding(String name, String header) {
if (header.trim().startsWith("=?"))
return header;
if (Helper.isUTF8(header)) {
if (!Helper.isISO8859(header)) {
Log.w("Converting " + name + " to UTF-8");
return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
} else {
Log.w("Converting " + name + " to ISO8859-1");
return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
}
return header;
}
private Address[] getAddressHeader(String name) throws MessagingException {
String header = imessage.getHeader(name, ",");
if (header == null)
return null;
if (!Helper.isUTF8(header)) {
Log.w("Converting header '" + name + "' to ISO8859-1");
header = new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
}
header = fixEncoding(name, header);
Address[] addresses = InternetAddress.parseHeader(header, false);
for (Address address : addresses) {
@ -1144,10 +1158,7 @@ public class MessageHelper {
if (subject == null)
return null;
if (!Helper.isUTF8(subject)) {
Log.w("Converting subject to ISO8859-1");
subject = new String(subject.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
}
subject = fixEncoding("subject", subject);
subject = subject.replaceAll("\\?=\\r?\\n\\s+=\\?", "\\?==\\?");
subject = MimeUtility.unfold(subject);
subject = decodeMime(subject);