1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

IMAP: fix flag handling in createMessage

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1243 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-21 10:18:46 +00:00
parent 84a05c6bdd
commit 34b1491ec9
4 changed files with 43 additions and 48 deletions

View File

@ -1077,7 +1077,7 @@ public class DavExchangeSession extends ExchangeSession {
} }
protected Message buildMessage(MultiStatusResponse responseEntity) throws URIException { protected Message buildMessage(MultiStatusResponse responseEntity) throws URIException, DavMailException {
Message message = new Message(); Message message = new Message();
message.messageUrl = URIUtil.decode(responseEntity.getHref()); message.messageUrl = URIUtil.decode(responseEntity.getHref());
DavPropertySet properties = responseEntity.getProperties(HttpStatus.SC_OK); DavPropertySet properties = responseEntity.getProperties(HttpStatus.SC_OK);
@ -1089,11 +1089,11 @@ public class DavExchangeSession extends ExchangeSession {
message.read = "1".equals(getPropertyIfExists(properties, "read")); message.read = "1".equals(getPropertyIfExists(properties, "read"));
message.junk = "1".equals(getPropertyIfExists(properties, "junk")); message.junk = "1".equals(getPropertyIfExists(properties, "junk"));
message.flagged = "2".equals(getPropertyIfExists(properties, "flagStatus")); message.flagged = "2".equals(getPropertyIfExists(properties, "flagStatus"));
message.draft = "9".equals(getPropertyIfExists(properties, "messageFlags")) || "8".equals(getPropertyIfExists(properties, "messageFlags")); message.draft = (getIntPropertyIfExists(properties, "messageFlags") & 8) != 0;
String lastVerbExecuted = getPropertyIfExists(properties, "lastVerbExecuted"); String lastVerbExecuted = getPropertyIfExists(properties, "lastVerbExecuted");
message.answered = "102".equals(lastVerbExecuted) || "103".equals(lastVerbExecuted); message.answered = "102".equals(lastVerbExecuted) || "103".equals(lastVerbExecuted);
message.forwarded = "104".equals(lastVerbExecuted); message.forwarded = "104".equals(lastVerbExecuted);
message.date = getPropertyIfExists(properties, "date"); message.date = convertDateFromExchange(getPropertyIfExists(properties, "date"));
message.deleted = "1".equals(getPropertyIfExists(properties, "deleted")); message.deleted = "1".equals(getPropertyIfExists(properties, "deleted"));
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -1462,9 +1462,6 @@ public class DavExchangeSession extends ExchangeSession {
} }
} else if ("bcc".equals(entry.getKey())) { } else if ("bcc".equals(entry.getKey())) {
list.add(Field.createDavProperty("bcc", entry.getValue())); list.add(Field.createDavProperty("bcc", entry.getValue()));
} else if ("draft".equals(entry.getKey())) {
// note: draft is readonly after create
list.add(Field.createDavProperty("messageFlags", entry.getValue()));
} else if ("deleted".equals(entry.getKey())) { } else if ("deleted".equals(entry.getKey())) {
list.add(Field.createDavProperty("writedeleted", entry.getValue())); list.add(Field.createDavProperty("writedeleted", entry.getValue()));
} else if ("datereceived".equals(entry.getKey())) { } else if ("datereceived".equals(entry.getKey())) {
@ -1477,7 +1474,7 @@ public class DavExchangeSession extends ExchangeSession {
/** /**
* Create message in specified folder. * Create message in specified folder.
* Will overwrite an existing message with same subject in the same folder * Will overwrite an existing message with same messageName in the same folder
* *
* @param folderPath Exchange folder path * @param folderPath Exchange folder path
* @param messageName message name * @param messageName message name
@ -1490,13 +1487,12 @@ public class DavExchangeSession extends ExchangeSession {
String messageUrl = URIUtil.encodePathQuery(getFolderPath(folderPath) + '/' + messageName); String messageUrl = URIUtil.encodePathQuery(getFolderPath(folderPath) + '/' + messageName);
PropPatchMethod patchMethod; PropPatchMethod patchMethod;
List<DavConstants> davProperties = buildProperties(properties); List<DavConstants> davProperties = buildProperties(properties);
if (properties != null) {
if (!properties.containsKey("read")) {
// force unread
davProperties.add(Field.createDavProperty("read", "0"));
}
// create the message first as draft
if (properties.containsKey("draft")) { if (properties.containsKey("draft")) {
// note: draft is readonly after create, create the message first with requested messageFlags
davProperties.add(Field.createDavProperty("messageFlags", properties.get("draft")));
}
if (!davProperties.isEmpty()) {
patchMethod = new PropPatchMethod(messageUrl, davProperties); patchMethod = new PropPatchMethod(messageUrl, davProperties);
try { try {
// update message with blind carbon copy and other flags // update message with blind carbon copy and other flags
@ -1509,7 +1505,8 @@ public class DavExchangeSession extends ExchangeSession {
patchMethod.releaseConnection(); patchMethod.releaseConnection();
} }
} }
}
// update message body
PutMethod putmethod = new PutMethod(messageUrl); PutMethod putmethod = new PutMethod(messageUrl);
putmethod.setRequestHeader("Translate", "f"); putmethod.setRequestHeader("Translate", "f");
try { try {
@ -1523,21 +1520,6 @@ public class DavExchangeSession extends ExchangeSession {
} finally { } finally {
putmethod.releaseConnection(); putmethod.releaseConnection();
} }
// add bcc and other properties
if (!davProperties.isEmpty()) {
patchMethod = new PropPatchMethod(messageUrl, davProperties);
try {
// update message with blind carbon copy and other flags
int statusCode = httpClient.executeMethod(patchMethod);
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
throw new DavMailException("EXCEPTION_UNABLE_TO_PATCH_MESSAGE", messageUrl, statusCode, ' ', patchMethod.getStatusLine());
}
} finally {
patchMethod.releaseConnection();
}
}
} }
/** /**
@ -1578,7 +1560,7 @@ public class DavExchangeSession extends ExchangeSession {
public void sendMessage(byte[] messageBody) throws IOException { public void sendMessage(byte[] messageBody) throws IOException {
String messageName = UUID.randomUUID().toString() + ".EML"; String messageName = UUID.randomUUID().toString() + ".EML";
createMessage("Drafts", messageName, null, messageBody); createMessage(DRAFTS, messageName, null, messageBody);
String tempUrl = draftsUrl + '/' + messageName + ".EML"; String tempUrl = draftsUrl + '/' + messageName + ".EML";
MoveMethod method = new MoveMethod(URIUtil.encodePath(tempUrl), URIUtil.encodePath(sendmsgUrl), true); MoveMethod method = new MoveMethod(URIUtil.encodePath(tempUrl), URIUtil.encodePath(sendmsgUrl), true);

View File

@ -134,8 +134,8 @@ public class Field {
//createField("read", 0x0e69, PropertyType.Boolean);//PR_READ //createField("read", 0x0e69, PropertyType.Boolean);//PR_READ
createField("deleted", DistinguishedPropertySetType.Common, 0x8570, "deleted", PropertyType.Boolean); createField("deleted", DistinguishedPropertySetType.Common, 0x8570, "deleted", PropertyType.Boolean);
createField(URN_SCHEMAS_HTTPMAIL, "date");//PR_CLIENT_SUBMIT_TIME, 0x0039 //createField(URN_SCHEMAS_HTTPMAIL, "date");//PR_CLIENT_SUBMIT_TIME, 0x0039
//createField("date", 0x0e06, PropertyType.SystemTime);//PR_MESSAGE_DELIVERY_TIME createField("date", 0x0e06, PropertyType.SystemTime);//PR_MESSAGE_DELIVERY_TIME
createField(URN_SCHEMAS_MAILHEADER, "bcc");//PS_INTERNET_HEADERS/bcc createField(URN_SCHEMAS_MAILHEADER, "bcc");//PS_INTERNET_HEADERS/bcc
createField(URN_SCHEMAS_HTTPMAIL, "datereceived");//PR_MESSAGE_DELIVERY_TIME, 0x0E06 createField(URN_SCHEMAS_HTTPMAIL, "datereceived");//PR_MESSAGE_DELIVERY_TIME, 0x0E06

View File

@ -390,11 +390,19 @@ public class ImapConnection extends AbstractConnection {
} }
if (flags != null) { if (flags != null) {
// parse flags, on create read and draft flags are on the
// same messageFlags property, 8 means draft and 1 means read
StringTokenizer flagtokenizer = new StringTokenizer(flags); StringTokenizer flagtokenizer = new StringTokenizer(flags);
while (flagtokenizer.hasMoreTokens()) { while (flagtokenizer.hasMoreTokens()) {
String flag = flagtokenizer.nextToken(); String flag = flagtokenizer.nextToken();
if ("\\Seen".equals(flag)) { if ("\\Seen".equals(flag)) {
properties.put("read", "1"); if (properties.containsKey("draft")) {
// draft message, add read flag
properties.put("draft", "9");
} else {
// not (yet) draft, set read flag
properties.put("draft", "1");
}
} else if ("\\Flagged".equals(flag)) { } else if ("\\Flagged".equals(flag)) {
properties.put("flagged", "2"); properties.put("flagged", "2");
} else if ("\\Answered".equals(flag)) { } else if ("\\Answered".equals(flag)) {
@ -402,7 +410,13 @@ public class ImapConnection extends AbstractConnection {
} else if ("$Forwarded".equals(flag)) { } else if ("$Forwarded".equals(flag)) {
properties.put("forwarded", "104"); properties.put("forwarded", "104");
} else if ("\\Draft".equals(flag)) { } else if ("\\Draft".equals(flag)) {
if (properties.containsKey("draft")) {
// read message, add draft flag
properties.put("draft", "9"); properties.put("draft", "9");
} else {
// not (yet) read, set draft flag
properties.put("draft", "8");
}
} else if ("Junk".equals(flag)) { } else if ("Junk".equals(flag)) {
properties.put("junk", "1"); properties.put("junk", "1");
} }

View File

@ -59,7 +59,6 @@ public class TestExchangeSessionMessageFlags extends AbstractExchangeSessionTest
String messageName = UUID.randomUUID().toString(); String messageName = UUID.randomUUID().toString();
HashMap<String, String> properties = new HashMap<String, String>(); HashMap<String, String> properties = new HashMap<String, String>();
properties.put("draft", "9"); properties.put("draft", "9");
properties.put("read", "1");
session.createMessage("testfolder", messageName, properties, getMimeBody(mimeMessage)); session.createMessage("testfolder", messageName, properties, getMimeBody(mimeMessage));
ExchangeSession.MessageList messageList = session.searchMessages("testfolder"); ExchangeSession.MessageList messageList = session.searchMessages("testfolder");
assertNotNull(messageList); assertNotNull(messageList);
@ -72,7 +71,7 @@ public class TestExchangeSessionMessageFlags extends AbstractExchangeSessionTest
MimeMessage mimeMessage = createMimeMessage(); MimeMessage mimeMessage = createMimeMessage();
String messageName = UUID.randomUUID().toString(); String messageName = UUID.randomUUID().toString();
HashMap<String, String> properties = new HashMap<String, String>(); HashMap<String, String> properties = new HashMap<String, String>();
properties.put("read", "1"); properties.put("draft", "1");
session.createMessage("testfolder", messageName, properties, getMimeBody(mimeMessage)); session.createMessage("testfolder", messageName, properties, getMimeBody(mimeMessage));
ExchangeSession.MessageList messageList = session.searchMessages("testfolder"); ExchangeSession.MessageList messageList = session.searchMessages("testfolder");
assertNotNull(messageList); assertNotNull(messageList);