From 5f09ec9c54ce9726e9a1f75c1fd84b0a0b9c7a64 Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 9 Mar 2009 23:12:08 +0000 Subject: [PATCH] IMAP: implement empty and BEFORE search filters git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@428 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 8 +++++--- src/java/davmail/imap/ImapConnection.java | 20 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index f2cd66d1..e3a9ba90 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -614,9 +614,11 @@ public class ExchangeSession { " ,\"http://schemas.microsoft.com/mapi/proptag/x0E070003\", \"http://schemas.microsoft.com/mapi/proptag/x10810003\"" + " ,\"urn:schemas:mailheader:message-id\", \"urn:schemas:httpmail:read\", \"DAV:isdeleted\", \"urn:schemas:mailheader:date\"" + " FROM Scope('SHALLOW TRAVERSAL OF \"" + folderUrl + "\"')\n" + - " WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n" + - conditions.replaceAll("<", "<").replaceAll(">", ">") + - " ORDER BY \"urn:schemas:httpmail:date\" ASC"; + " WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False\n"; + if (conditions != null) { + searchRequest += conditions.replaceAll("<", "<").replaceAll(">", ">"); + } + searchRequest += " ORDER BY \"urn:schemas:httpmail:date\" ASC"; Enumeration folderEnum = DavGatewayHttpClientFacade.executeSearchMethod(wdr.retrieveSessionInstance(), folderUrl, searchRequest); while (folderEnum.hasMoreElements()) { diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 595000ef..a498348d 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -226,8 +226,12 @@ public class ImapConnection extends AbstractConnection { } } conditions.append(")"); + String query = conditions.query.toString(); DavGatewayTray.debug("Search: " + conditions.query); - ExchangeSession.MessageList localMessages = session.searchMessages(currentFolder.folderName, conditions.query.toString()); + if ("AND ()".equals(query)) { + query = null; + } + ExchangeSession.MessageList localMessages = session.searchMessages(currentFolder.folderName, query); for (ExchangeSession.Message message : localMessages) { if (((undeleted && !message.deleted) || !undeleted) && (conditions.flagged == null || message.flagged == conditions.flagged) @@ -659,6 +663,16 @@ public class ImapConnection extends AbstractConnection { } else { throw new IOException("Invalid search parameters"); } + } else if ("BEFORE".equals(token)) { + SimpleDateFormat parser = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + dateFormatter.setTimeZone(ExchangeSession.GMT_TIMEZONE); + try { + Date date = parser.parse(tokens.nextToken()); + conditions.append(operator).append("\"urn:schemas:httpmail:datereceived\"<'").append(dateFormatter.format(date)).append("'"); + } catch (ParseException e) { + throw new IOException("Invalid search parameters"); + } } else if ("OLD".equals(token)) { // ignore } else { @@ -925,10 +939,10 @@ public class ImapConnection extends AbstractConnection { } public ExchangeSession.Message next() { - ExchangeSession.Message message = messages.get(currentIndex++); + ExchangeSession.Message message = messages.get(currentIndex++); long uid = message.getUidAsLong(); if (uid < startUid || uid > endUid) { - throw new RuntimeException("Message uid "+uid+ " not in range "+startUid+":"+endUid); + throw new RuntimeException("Message uid " + uid + " not in range " + startUid + ":" + endUid); } return message; }