2011-01-13 19:53:19 -05:00
|
|
|
|
|
|
|
package com.fsck.k9.mail.internet;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.mail.Message;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
2011-01-19 16:32:09 -05:00
|
|
|
import org.apache.james.mime4j.codec.Base64InputStream;
|
|
|
|
import org.apache.james.mime4j.codec.QuotedPrintableInputStream;
|
2011-01-13 19:53:19 -05:00
|
|
|
import org.apache.james.mime4j.util.CharsetUtil;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods for decoding strings, byte arrays and encoded words.
|
|
|
|
*
|
|
|
|
* This class is copied from the org.apache.james.mime4j.decoder.DecoderUtil class. It's modified here in order to
|
|
|
|
* decode emoji characters in the Subject headers. The method to decode emoji depends on the MimeMessage class because
|
|
|
|
* it has to be determined with the sender address, the mailer and so on.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public class DecoderUtil {
|
2011-01-13 19:53:19 -05:00
|
|
|
/**
|
|
|
|
* Decodes an encoded word encoded with the 'B' encoding (described in
|
|
|
|
* RFC 2047) found in a header field body.
|
|
|
|
*
|
|
|
|
* @param encodedWord the encoded word to decode.
|
|
|
|
* @param charset the Java charset to use.
|
|
|
|
* @return the decoded string.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private static String decodeB(String encodedWord, String charset) {
|
2011-01-13 19:53:19 -05:00
|
|
|
byte[] bytes;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-13 19:53:19 -05:00
|
|
|
bytes = encodedWord.getBytes("US-ASCII");
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Base64InputStream is = new Base64InputStream(new ByteArrayInputStream(bytes));
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-13 19:53:19 -05:00
|
|
|
return MimeUtility.readToString(is, charset);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (IOException e) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes an encoded word encoded with the 'Q' encoding (described in
|
|
|
|
* RFC 2047) found in a header field body.
|
|
|
|
*
|
|
|
|
* @param encodedWord the encoded word to decode.
|
|
|
|
* @param charset the Java charset to use.
|
|
|
|
* @return the decoded string.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private static String decodeQ(String encodedWord, String charset) {
|
2011-01-13 19:53:19 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace _ with =20
|
|
|
|
*/
|
2011-09-30 02:18:00 -04:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int i = 0; i < encodedWord.length(); i++) {
|
2011-01-13 19:53:19 -05:00
|
|
|
char c = encodedWord.charAt(i);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c == '_') {
|
2011-01-13 19:53:19 -05:00
|
|
|
sb.append("=20");
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-13 19:53:19 -05:00
|
|
|
sb.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] bytes;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-29 22:00:46 -05:00
|
|
|
bytes = sb.toString().getBytes("US-ASCII");
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuotedPrintableInputStream is = new QuotedPrintableInputStream(new ByteArrayInputStream(bytes));
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-13 19:53:19 -05:00
|
|
|
return MimeUtility.readToString(is, charset);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (IOException e) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a string containing encoded words as defined by RFC 2047.
|
|
|
|
* Encoded words in have the form
|
|
|
|
* =?charset?enc?Encoded word?= where enc is either 'Q' or 'q' for
|
|
|
|
* quoted-printable and 'B' or 'b' for Base64.
|
|
|
|
*
|
|
|
|
* ANDROID: COPIED FROM A NEWER VERSION OF MIME4J
|
|
|
|
*
|
|
|
|
* @param body the string to decode.
|
|
|
|
* @param message the message which has the string.
|
|
|
|
* @return the decoded string.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static String decodeEncodedWords(String body, Message message) {
|
2011-01-13 19:53:19 -05:00
|
|
|
|
|
|
|
// ANDROID: Most strings will not include "=?" so a quick test can prevent unneeded
|
|
|
|
// object creation. This could also be handled via lazy creation of the StringBuilder.
|
2011-02-06 17:09:48 -05:00
|
|
|
if (body.indexOf("=?") == -1) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
int previousEnd = 0;
|
|
|
|
boolean previousWasEncoded = false;
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
while (true) {
|
2011-01-13 19:53:19 -05:00
|
|
|
int begin = body.indexOf("=?", previousEnd);
|
|
|
|
|
|
|
|
// ANDROID: The mime4j original version has an error here. It gets confused if
|
|
|
|
// the encoded string begins with an '=' (just after "?Q?"). This patch seeks forward
|
|
|
|
// to find the two '?' in the "header", before looking for the final "?=".
|
|
|
|
int endScan = begin + 2;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (begin != -1) {
|
2011-01-13 19:53:19 -05:00
|
|
|
int qm1 = body.indexOf('?', endScan + 2);
|
|
|
|
int qm2 = body.indexOf('?', qm1 + 1);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (qm2 != -1) {
|
2011-01-13 19:53:19 -05:00
|
|
|
endScan = qm2 + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int end = begin == -1 ? -1 : body.indexOf("?=", endScan);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (end == -1) {
|
2011-01-13 19:53:19 -05:00
|
|
|
if (previousEnd == 0)
|
|
|
|
return body;
|
|
|
|
|
|
|
|
sb.append(body.substring(previousEnd));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
end += 2;
|
|
|
|
|
|
|
|
String sep = body.substring(previousEnd, begin);
|
|
|
|
|
|
|
|
String decoded = decodeEncodedWord(body, begin, end, message);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (decoded == null) {
|
2011-01-13 19:53:19 -05:00
|
|
|
sb.append(sep);
|
|
|
|
sb.append(body.substring(begin, end));
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
|
|
|
if (!previousWasEncoded || !CharsetUtil.isWhitespace(sep)) {
|
2011-01-13 19:53:19 -05:00
|
|
|
sb.append(sep);
|
|
|
|
}
|
|
|
|
sb.append(decoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
previousEnd = end;
|
|
|
|
previousWasEncoded = decoded != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return null on error
|
2011-02-06 17:09:48 -05:00
|
|
|
private static String decodeEncodedWord(String body, int begin, int end, Message message) {
|
2011-01-13 19:53:19 -05:00
|
|
|
int qm1 = body.indexOf('?', begin + 2);
|
|
|
|
if (qm1 == end - 2)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int qm2 = body.indexOf('?', qm1 + 1);
|
|
|
|
if (qm2 == end - 2)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
String mimeCharset = body.substring(begin + 2, qm1);
|
|
|
|
String encoding = body.substring(qm1 + 1, qm2);
|
|
|
|
String encodedText = body.substring(qm2 + 1, end - 2);
|
|
|
|
|
|
|
|
String charset;
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-01-13 19:53:19 -05:00
|
|
|
charset = MimeUtility.fixupCharset(mimeCharset, message);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (MessagingException e) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (encodedText.length() == 0) {
|
2011-01-13 19:53:19 -05:00
|
|
|
Log.w(K9.LOG_TAG, "Missing encoded text in encoded word: '" + body.substring(begin, end) + "'");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (encoding.equalsIgnoreCase("Q")) {
|
2011-01-29 22:00:46 -05:00
|
|
|
return decodeQ(encodedText, charset);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (encoding.equalsIgnoreCase("B")) {
|
2011-01-13 19:53:19 -05:00
|
|
|
return DecoderUtil.decodeB(encodedText, charset);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-01-13 19:53:19 -05:00
|
|
|
Log.w(K9.LOG_TAG, "Warning: Unknown encoding in encoded word '" + body.substring(begin, end) + "'");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|