1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

IMAP: implement BODYSTRUCTURE, needed for Apple Mail

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@406 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-03-02 15:38:02 +00:00
parent 87fdfb4936
commit 2691f87144

View File

@ -8,10 +8,12 @@ import davmail.exchange.ExchangeSessionFactory;
import davmail.tray.DavGatewayTray; import davmail.tray.DavGatewayTray;
import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpException;
import java.io.ByteArrayOutputStream; import javax.mail.internet.MimeMessage;
import java.io.FilterOutputStream; import javax.mail.internet.MimeMultipart;
import java.io.IOException; import javax.mail.internet.MimeBodyPart;
import java.io.OutputStream; import javax.mail.internet.MimePart;
import javax.mail.MessagingException;
import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.SocketException; import java.net.SocketException;
@ -437,8 +439,10 @@ public class ImapConnection extends AbstractConnection {
// Apple Mail: send structure with body, need exact RFC822.SIZE // Apple Mail: send structure with body, need exact RFC822.SIZE
bodystructure = true; bodystructure = true;
} else { } else {
// thunderbird : send fake BODYSTRUCTURE // thunderbird : send BODYSTRUCTURE
buffer.append(" BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"windows-1252\") NIL NIL \"8BIT\" ").append(message.size).append(" NIL))"); ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.write(baos);
appendBodyStructure(buffer, baos);
} }
} else if ("INTERNALDATE".equals(param) && message.date != null && message.date.length() > 0) { } else if ("INTERNALDATE".equals(param) && message.date != null && message.date.length() > 0) {
try { try {
@ -480,7 +484,8 @@ public class ImapConnection extends AbstractConnection {
baos.close(); baos.close();
DavGatewayTray.debug("Message size: " + message.size + " actual size:" + baos.size() + " message+headers: " + (message.size + baos.size())); DavGatewayTray.debug("Message size: " + message.size + " actual size:" + baos.size() + " message+headers: " + (message.size + baos.size()));
if (bodystructure) { if (bodystructure) {
buffer.append(" BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"windows-1252\") NIL NIL \"8BIT\" ").append(rfc822size).append(" NIL))"); // Apple Mail: need to build full bodystructure
appendBodyStructure(buffer, baos);
} }
buffer.append(" RFC822.SIZE ").append(rfc822size).append(" ").append("BODY[]").append(" {").append(baos.size()).append("}"); buffer.append(" RFC822.SIZE ").append(rfc822size).append(" ").append("BODY[]").append(" {").append(baos.size()).append("}");
sendClient(buffer.toString()); sendClient(buffer.toString());
@ -493,6 +498,93 @@ public class ImapConnection extends AbstractConnection {
sendClient(buffer.toString()); sendClient(buffer.toString());
} }
protected void appendBodyStructure(StringBuilder buffer, ByteArrayOutputStream baos) throws IOException {
buffer.append(" BODYSTRUCTURE ");
try {
MimeMessage mimeMessage = new MimeMessage(null, new ByteArrayInputStream(baos.toByteArray()));
Object mimeBody = mimeMessage.getContent();
if (mimeBody instanceof MimeMultipart) {
buffer.append("(");
MimeMultipart multiPart = (MimeMultipart) mimeBody;
for (int i = 0; i < multiPart.getCount(); i++) {
MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i);
appendBodyStructure(buffer, bodyPart);
}
int slashIndex = multiPart.getContentType().indexOf('/');
if (slashIndex < 0) {
throw new IOException("Invalid content type: " + multiPart.getContentType());
}
int semiColonIndex = multiPart.getContentType().indexOf(';');
if (semiColonIndex < 0) {
buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1).toUpperCase()).append("\")");
} else {
buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append("\")");
}
} else {
// no multipart, single body
appendBodyStructure(buffer, mimeMessage);
}
} catch (MessagingException me) {
throw new IOException(me);
}
}
protected void appendBodyStructure(StringBuilder buffer, MimePart bodyPart) throws IOException, MessagingException {
String contentType = bodyPart.getContentType();
int slashIndex = contentType.indexOf('/');
if (slashIndex < 0) {
throw new IOException("Invalid content type: " + contentType);
}
buffer.append("(\"").append(contentType.substring(0, slashIndex).toUpperCase()).append("\" \"");
int semiColonIndex = contentType.indexOf(';');
if (semiColonIndex < 0) {
buffer.append(contentType.substring(slashIndex + 1).toUpperCase()).append("\" ()");
} else {
// extended content type
buffer.append(contentType.substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append("\"");
int charsetindex = contentType.indexOf("charset=");
if (charsetindex >= 0) {
buffer.append(" (\"CHARSET\" ");
int charsetEndIndex = Math.max(contentType.indexOf(' '), contentType.length());
String charSet = contentType.substring(charsetindex + "charset=".length(), charsetEndIndex);
if (!charSet.startsWith("\"")) {
buffer.append('"');
}
buffer.append(charSet.toUpperCase());
if (!charSet.endsWith("\"")) {
buffer.append('"');
}
buffer.append(")");
} else {
buffer.append(" ()");
}
}
// body id
if (bodyPart.getContentID() == null) {
buffer.append(" NIL");
} else {
buffer.append(" \"").append(bodyPart.getContentID()).append("\"");
}
if (bodyPart.getDescription() == null) {
buffer.append(" NIL");
} else {
buffer.append(" \"").append(bodyPart.getDescription()).append("\"");
}
if (bodyPart.getHeader("Content-Transfer-Encoding") == null) {
buffer.append(" NIL");
} else {
buffer.append(" \"").append(bodyPart.getEncoding().toUpperCase()).append("\"");
}
buffer.append(' ').append(bodyPart.getSize());
if (bodyPart.getLineCount() < 0) {
buffer.append(" NIL");
} else {
buffer.append(' ').append(bodyPart.getLineCount()).append('"');
}
buffer.append(')');
}
static final class SearchConditions { static final class SearchConditions {
Boolean flagged = null; Boolean flagged = null;
Boolean answered = null; Boolean answered = null;