From 70fc412250637845781d91f5430d0815c61af53c Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 3 Feb 2009 14:38:05 +0000 Subject: [PATCH] IMAP: flagged, junk flags, draft replace (append + delete) git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@314 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 26 ++++++++++-- src/java/davmail/imap/ImapConnection.java | 41 +++++++++++-------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 996c67d1..cdeea1c2 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -479,6 +479,10 @@ public class ExchangeSession { PutMethod putmethod = new PutMethod(messageUrl); putmethod.setRequestHeader("Translate", "f"); putmethod.setRequestHeader("Content-Type", "message/rfc822"); + if (!allowOverwrite) { + putmethod.setRequestHeader("Allow-Rename", "t"); + putmethod.setRequestHeader("If-None-Match", "*"); + } InputStream bodyStream = null; try { // use same encoding as client socket reader @@ -543,6 +547,10 @@ public class ExchangeSession { message.uid = prop.getPropertyAsString(); } else if ("read".equals(localName)) { message.read = "1".equals(prop.getPropertyAsString()); + } else if ("x10830003".equals(localName)) { + message.junk = "1".equals(prop.getPropertyAsString()); + } else if ("x10900003".equals(localName)) { + message.flagged = "2".equals(prop.getPropertyAsString()); } else if ("message-id".equals(prop.getLocalName())) { message.messageId = prop.getPropertyAsString(); if (message.messageId.startsWith("<") && message.messageId.endsWith(">")) { @@ -575,6 +583,10 @@ public class ExchangeSession { for (Map.Entry entry : properties.entrySet()) { if ("read".equals(entry.getKey())) { patchMethod.addPropertyToSet("read", entry.getValue(), "e", "urn:schemas:httpmail:"); + } else if ("junk".equals(entry.getKey())) { + patchMethod.addPropertyToSet("x10830003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/"); + } else if ("flagged".equals(entry.getKey())) { + patchMethod.addPropertyToSet("x10900003", entry.getValue(), "f", "http://schemas.microsoft.com/mapi/proptag/"); } } int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod); @@ -591,6 +603,7 @@ public class ExchangeSession { String folderUrl = getFolderPath(folderName); MessageList messages = new MessageList(); String searchRequest = "Select \"DAV:uid\", \"http://schemas.microsoft.com/mapi/proptag/x0e080003\"" + + " ,\"http://schemas.microsoft.com/mapi/proptag/x10830003\", \"http://schemas.microsoft.com/mapi/proptag/x10900003\"" + " ,\"urn:schemas:mailheader:message-id\", \"urn:schemas:httpmail:read\"" + " FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" + " WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n" + @@ -877,7 +890,6 @@ public class ExchangeSession { MoveMethod method = new MoveMethod(URIUtil.encodePath(folderPath), URIUtil.encodePath(targetPath)); method.setOverwrite(false); - //method.addRequestHeader("Allow-Rename", "t"); try { int statusCode = wdr.retrieveSessionInstance().executeMethod(method); if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) { @@ -920,6 +932,8 @@ public class ExchangeSession { public String messageId; public boolean read; public boolean deleted; + public boolean junk; + public boolean flagged; public long getUidAsLong() { byte[] decodedValue = Base64.decode(uid.getBytes()); @@ -939,9 +953,15 @@ public class ExchangeSession { buffer.append("\\Seen "); } if (deleted) { - buffer.append("\\Deleted"); + buffer.append("\\Deleted "); } - return buffer.toString(); + if (flagged) { + buffer.append("\\Flagged "); + } + if (junk) { + buffer.append("Junk "); + } + return buffer.toString().trim(); } public void write(OutputStream os) throws IOException { diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 1ed1c499..097646dd 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -17,6 +17,7 @@ import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.List; import java.util.StringTokenizer; +import java.util.Random; /** * Dav Gateway smtp connection implementation. @@ -26,6 +27,8 @@ public class ImapConnection extends AbstractConnection { protected static final int INITIAL = 0; protected static final int AUTHENTICATED = 2; + protected Random random = new Random(); + ExchangeSession.Folder currentFolder; ExchangeSession.MessageList messages; @@ -156,8 +159,8 @@ public class ImapConnection extends AbstractConnection { } else { sendClient("* OK [UIDNEXT " + (messages.get(messages.size() - 1).getUidAsLong() + 1) + "]"); } - sendClient("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded $MDNSent Forwarded $Junk $NotJunk Junk JunkRecorded NonJunk NotJunk)"); - sendClient("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded $MDNSent Forwarded \\*)] junk-related flags are not permanent"); + sendClient("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded Forwarded Junk)"); + sendClient("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded Forwarded Junk \\*)]"); //sendClient("* [UNSEEN 1] first unseen message in inbox"); sendClient(commandId + " OK [READ-WRITE] " + command + " completed"); } else { @@ -308,6 +311,12 @@ public class ImapConnection extends AbstractConnection { if ("\\Seen".equals(flag)) { properties.put("read", "0"); message.read = false; + } else if ("\\Flagged".equals(flag)) { + properties.put("flagged", "0"); + message.flagged = false; + } else if ("Junk".equals(flag)) { + properties.put("junk", "0"); + message.junk = false; } } } else if ("+Flags".equalsIgnoreCase(action)) { @@ -319,10 +328,18 @@ public class ImapConnection extends AbstractConnection { message.read = true; } else if ("\\Deleted".equals(flag)) { message.deleted = true; + } else if ("\\Flagged".equals(flag)) { + properties.put("flagged", "2"); + message.flagged = true; + } else if ("Junk".equals(flag)) { + properties.put("junk", "1"); + message.junk = true; } } } - session.updateMessage(messages.getByUid(uid), properties); + if (properties.size() > 0) { + session.updateMessage(messages.getByUid(uid), properties); + } int index = 0; for (ExchangeSession.Message currentMessage : messages) { index++; @@ -378,19 +395,9 @@ public class ImapConnection extends AbstractConnection { } // empty line readClient(); - String messageBody = new String(buffer); - String subject = null; - int subjectStartIndex = messageBody.indexOf("Subject: "); - if (subjectStartIndex >= 0) { - int subjectEndIndex = messageBody.indexOf("\r", subjectStartIndex); - if (subjectEndIndex >= 0) { - subject = messageBody.substring(subjectStartIndex + "Subject: ".length(), subjectEndIndex); - } - } - if (subject == null) { - subject = "mail" + System.currentTimeMillis(); - } - session.createMessage(session.getFolderPath(folderName), subject, null, new String(buffer), true); + + String mailName = Long.toHexString(System.currentTimeMillis()) + Long.toHexString(random.nextLong()); + session.createMessage(session.getFolderPath(folderName), mailName, null, new String(buffer), false); sendClient(commandId + " OK APPEND completed"); } else if ("noop".equalsIgnoreCase(command) || "check".equalsIgnoreCase(command)) { if (currentFolder != null) { @@ -439,7 +446,7 @@ public class ImapConnection extends AbstractConnection { index++; if (message.deleted) { session.deleteMessage(message.messageUrl); - sendClient("* "+index+" EXPUNGE"); + sendClient("* " + index + " EXPUNGE"); } } }