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

IMAP: additional complex search filters

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@370 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-02-18 00:58:20 +00:00
parent 0bc057a5a2
commit 6c067d47c7

View File

@ -228,31 +228,32 @@ public class ImapConnection extends AbstractConnection {
} else if ("search".equalsIgnoreCase(subcommand)) { } else if ("search".equalsIgnoreCase(subcommand)) {
SearchConditions conditions = new SearchConditions(); SearchConditions conditions = new SearchConditions();
conditions.append("AND (");
boolean undeleted = true; boolean undeleted = true;
boolean or = false;
while (tokens.hasMoreTokens()) { while (tokens.hasMoreTokens()) {
String token = tokens.nextToken(); String token = tokens.nextToken();
if ("UNDELETED".equals(token)) { if ("UNDELETED".equals(token)) {
undeleted = true; undeleted = true;
} else if ("OR".equals(token)) {
or = true;
} else if (token.startsWith("OR ")) { } else if (token.startsWith("OR ")) {
conditions.append(" AND ("); or = true;
IMAPTokenizer innerTokens = new IMAPTokenizer(token); appendOrSearchParams(tokens, token, conditions);
innerTokens.nextToken();
boolean first = true;
while (innerTokens.hasMoreTokens()) {
String innerToken = innerTokens.nextToken();
String operator = "";
if (!first) {
operator = " OR ";
}
first = false;
appendSearchParam(operator, innerTokens, innerToken, conditions);
}
conditions.append(")");
} else { } else {
appendSearchParam(" AND ",tokens, token, conditions); String operator;
if (conditions.query.length() == 5) {
operator = "";
} else if (or) {
operator = " OR ";
} else {
operator = " AND ";
}
appendSearchParam(operator, tokens, token, conditions);
} }
} }
conditions.append(")");
DavGatewayTray.debug("Search: " + conditions.query); DavGatewayTray.debug("Search: " + conditions.query);
messages = session.searchMessages(currentFolder.folderName, conditions.query.toString()); messages = session.searchMessages(currentFolder.folderName, conditions.query.toString());
for (ExchangeSession.Message message : messages) { for (ExchangeSession.Message message : messages) {
@ -444,16 +445,42 @@ public class ImapConnection extends AbstractConnection {
} }
} }
protected void appendOrSearchParams(StringTokenizer tokens, String token, SearchConditions conditions) throws IOException {
IMAPTokenizer innerTokens = new IMAPTokenizer(token);
innerTokens.nextToken();
boolean first = true;
while (innerTokens.hasMoreTokens()) {
String innerToken = innerTokens.nextToken();
String operator = "";
if (!first) {
operator = " OR ";
}
first = false;
appendSearchParam(operator, innerTokens, innerToken, conditions);
}
}
protected void appendSearchParam(String operator, StringTokenizer tokens, String token, SearchConditions conditions) throws IOException { protected void appendSearchParam(String operator, StringTokenizer tokens, String token, SearchConditions conditions) throws IOException {
if ("NOT".equals(token)) { if ("NOT".equals(token)) {
conditions.append(operator).append(" NOT "); conditions.append(operator).append(" NOT ");
appendSearchParam("", tokens, tokens.nextToken(), conditions); appendSearchParam("", tokens, tokens.nextToken(), conditions);
} else if (token.startsWith("OR ")) {
appendOrSearchParams(tokens, token, conditions);
} else if ("SUBJECT".equals(token)) { } else if ("SUBJECT".equals(token)) {
conditions.append(operator).append("\"urn:schemas:httpmail:subject\" LIKE '%").append(tokens.nextToken()).append("%'"); conditions.append(operator).append("\"urn:schemas:httpmail:subject\" LIKE '%").append(tokens.nextToken()).append("%'");
} else if ("BODY".equals(token)) { } else if ("BODY".equals(token)) {
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x01000001E\" LIKE '%").append(tokens.nextToken()).append("%'"); conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x01000001E\" LIKE '%").append(tokens.nextToken()).append("%'");
} else if ("FROM".equals(token)) { } else if ("FROM".equals(token)) {
conditions.append(operator).append("\"urn:schemas:mailheader:from\" LIKE '%").append(tokens.nextToken()).append("%'"); conditions.append(operator).append("\"urn:schemas:mailheader:from\" LIKE '%").append(tokens.nextToken()).append("%'");
} else if ("TO".equals(token)) {
conditions.append(operator).append("\"urn:schemas:mailheader:to\" LIKE '%").append(tokens.nextToken()).append("%'");
} else if ("CC".equals(token)) {
conditions.append(operator).append("\"urn:schemas:mailheader:cc\" LIKE '%").append(tokens.nextToken()).append("%'");
} else if ("LARGER".equals(token)) {
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x0e080003\" >= ").append(Long.parseLong(tokens.nextToken())).append("");
} else if ("SMALLER".equals(token)) {
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x0e080003\" < ").append(Long.parseLong(tokens.nextToken())).append("");
} else if (token.startsWith("SENT")) { } else if (token.startsWith("SENT")) {
conditions.append(operator); conditions.append(operator);
appendDateSearchParam(tokens, token, conditions); appendDateSearchParam(tokens, token, conditions);
@ -470,10 +497,8 @@ public class ImapConnection extends AbstractConnection {
} else if ("UNANSWERED".equals(token)) { } else if ("UNANSWERED".equals(token)) {
conditions.answered = Boolean.FALSE; conditions.answered = Boolean.FALSE;
} else if ("HEADER".equals(token)) { } else if ("HEADER".equals(token)) {
String headerName = tokens.nextToken(); String headerName = tokens.nextToken().toLowerCase();
if ("Message-ID".equalsIgnoreCase(headerName)) { conditions.append(operator).append("\"urn:schemas:mailheader:" + headerName + "\"='").append(tokens.nextToken()).append("'");
conditions.append(operator).append("\"urn:schemas:mailheader:message-id\"='").append(tokens.nextToken()).append("'");
}
} else if ("OLD".equals(token)) { } else if ("OLD".equals(token)) {
// ignore // ignore
} else { } else {