From c29722a56cb26d0cfc82d44e82c66838d6d146f6 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 21 May 2010 12:20:32 +0000 Subject: [PATCH] Make sure the References header doesn't exceed 998 characters and the value won't get Q-encoded. Thanks to fiouzy for providing the patch. Fixes issue 1559 --- .../fsck/k9/mail/internet/MimeMessage.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/com/fsck/k9/mail/internet/MimeMessage.java b/src/com/fsck/k9/mail/internet/MimeMessage.java index 802d5801a..4b32a6b2d 100644 --- a/src/com/fsck/k9/mail/internet/MimeMessage.java +++ b/src/com/fsck/k9/mail/internet/MimeMessage.java @@ -368,6 +368,34 @@ public class MimeMessage extends Message @Override public void setReferences(String references) throws MessagingException { + /* + * Make sure the References header doesn't exceed 998 characters and + * won't get (Q-)encoded later. Otherwise some clients will break + * threads apart. + * + * For more information see issue 1559. + */ + + // Make sure separator is SPACE to prevent Q-encoding when TAB is encountered + references = references.replaceAll("\\s+", " "); + + final int limit = 986; // "References: " + final int originalLength = references.length(); + if (originalLength >= limit) + { + // Find start of first reference + final int start = references.indexOf('<'); + + // First reference + SPACE + final String firstReference = references.substring(start, + references.indexOf('<', start + 1)); + + // Find longest tail + final String tail = references.substring(references.indexOf('<', + firstReference.length() + originalLength - limit)); + + references = firstReference + tail; + } setHeader("References", references); }