4 * Implements IMAP's gratuitously complex SEARCH command.
7 * Copyright (c) 2001-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "ctdl_module.h"
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
52 #include <libcitadel.h>
55 #include "sysdep_decls.h"
56 #include "citserver.h"
63 #include "internet_addressing.h"
64 #include "serv_imap.h"
65 #include "imap_tools.h"
66 #include "imap_fetch.h"
67 #include "imap_search.h"
72 * imap_do_search() calls imap_do_search_msg() to search an individual
73 * message after it has been fetched from the disk. This function returns
74 * nonzero if there is a match.
76 * supplied_msg MAY be used to pass a pointer to the message in memory,
77 * if for some reason it's already been loaded. If not, the message will
78 * be loaded only if one or more search criteria require it.
80 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
81 int num_items, char **itemlist, int is_uid) {
89 struct CtdlMessage *msg = NULL;
90 int need_to_free_msg = 0;
97 /* Initially we start at the beginning. */
100 /* Check for the dreaded NOT criterion. */
101 if (!strcasecmp(itemlist[0], "NOT")) {
106 /* Check for the dreaded OR criterion. */
107 if (!strcasecmp(itemlist[0], "OR")) {
112 /* Now look for criteria. */
113 if (!strcasecmp(itemlist[pos], "ALL")) {
118 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
119 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
125 else if (!strcasecmp(itemlist[pos], "BCC")) {
127 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
128 need_to_free_msg = 1;
131 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
132 if (fieldptr != NULL) {
133 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
142 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
144 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
145 need_to_free_msg = 1;
148 if (msg->cm_fields['T'] != NULL) {
149 if (imap_datecmp(itemlist[pos+1],
150 atol(msg->cm_fields['T'])) < 0) {
158 else if (!strcasecmp(itemlist[pos], "BODY")) {
160 /* If fulltext indexing is active, on this server,
161 * all messages have already been qualified.
163 if (config.c_enable_fulltext) {
167 /* Otherwise, we have to do a slow search. */
170 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
171 need_to_free_msg = 1;
174 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
183 else if (!strcasecmp(itemlist[pos], "CC")) {
185 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
186 need_to_free_msg = 1;
189 fieldptr = msg->cm_fields['Y'];
190 if (fieldptr != NULL) {
191 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
196 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
197 if (fieldptr != NULL) {
198 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
208 else if (!strcasecmp(itemlist[pos], "DELETED")) {
209 if (IMAP->flags[seq-1] & IMAP_DELETED) {
215 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
216 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
222 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
223 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
229 else if (!strcasecmp(itemlist[pos], "FROM")) {
231 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
232 need_to_free_msg = 1;
235 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
238 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
245 else if (!strcasecmp(itemlist[pos], "HEADER")) {
247 /* We've got to do a slow search for this because the client
248 * might be asking for an RFC822 header field that has not been
249 * converted into a Citadel header field. That requires
250 * examining the message body.
253 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
254 need_to_free_msg = 1;
259 CC->redirect_buffer = malloc(SIZ);
260 CC->redirect_len = 0;
261 CC->redirect_alloc = SIZ;
262 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
264 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
265 if (fieldptr != NULL) {
266 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
272 free(CC->redirect_buffer);
273 CC->redirect_buffer = NULL;
274 CC->redirect_len = 0;
275 CC->redirect_alloc = 0;
278 pos += 3; /* Yes, three */
281 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
282 /* not implemented */
286 else if (!strcasecmp(itemlist[pos], "LARGER")) {
288 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
289 need_to_free_msg = 1;
292 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
299 else if (!strcasecmp(itemlist[pos], "NEW")) {
300 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
306 else if (!strcasecmp(itemlist[pos], "OLD")) {
307 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
313 else if (!strcasecmp(itemlist[pos], "ON")) {
315 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
316 need_to_free_msg = 1;
319 if (msg->cm_fields['T'] != NULL) {
320 if (imap_datecmp(itemlist[pos+1],
321 atol(msg->cm_fields['T'])) == 0) {
329 else if (!strcasecmp(itemlist[pos], "RECENT")) {
330 if (IMAP->flags[seq-1] & IMAP_RECENT) {
336 else if (!strcasecmp(itemlist[pos], "SEEN")) {
337 if (IMAP->flags[seq-1] & IMAP_SEEN) {
343 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
345 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
346 need_to_free_msg = 1;
349 if (msg->cm_fields['T'] != NULL) {
350 if (imap_datecmp(itemlist[pos+1],
351 atol(msg->cm_fields['T'])) < 0) {
359 else if (!strcasecmp(itemlist[pos], "SENTON")) {
361 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
362 need_to_free_msg = 1;
365 if (msg->cm_fields['T'] != NULL) {
366 if (imap_datecmp(itemlist[pos+1],
367 atol(msg->cm_fields['T'])) == 0) {
375 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
377 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
378 need_to_free_msg = 1;
381 if (msg->cm_fields['T'] != NULL) {
382 if (imap_datecmp(itemlist[pos+1],
383 atol(msg->cm_fields['T'])) >= 0) {
391 else if (!strcasecmp(itemlist[pos], "SINCE")) {
393 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
394 need_to_free_msg = 1;
397 if (msg->cm_fields['T'] != NULL) {
398 if (imap_datecmp(itemlist[pos+1],
399 atol(msg->cm_fields['T'])) >= 0) {
407 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
409 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
410 need_to_free_msg = 1;
413 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
420 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
422 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
423 need_to_free_msg = 1;
426 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
433 else if (!strcasecmp(itemlist[pos], "TEXT")) {
435 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
436 need_to_free_msg = 1;
439 for (i='A'; i<='Z'; ++i) {
440 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
448 else if (!strcasecmp(itemlist[pos], "TO")) {
450 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
451 need_to_free_msg = 1;
454 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
461 /* FIXME this is b0rken. fix it. */
462 else if (imap_is_message_set(itemlist[pos])) {
463 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
469 /* FIXME this is b0rken. fix it. */
470 else if (!strcasecmp(itemlist[pos], "UID")) {
471 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
477 /* Now here come the 'UN' criteria. Why oh why do we have to
478 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
479 * can't there be *one* way to do things? More gratuitous complexity.
482 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
483 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
489 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
490 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
496 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
497 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
503 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
504 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
510 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
515 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
516 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
522 /* Remember to negate if we were told to */
527 /* Keep going if there are more criteria! */
528 if (pos < num_items) {
531 match = (match || imap_do_search_msg(seq, msg,
532 num_items - pos, &itemlist[pos], is_uid));
535 match = (match && imap_do_search_msg(seq, msg,
536 num_items - pos, &itemlist[pos], is_uid));
541 if (need_to_free_msg) {
542 CtdlFreeMessage(msg);
549 * imap_search() calls imap_do_search() to do its actual work, once it's
550 * validated and boiled down the request a bit.
552 void imap_do_search(int num_items, char **itemlist, int is_uid) {
554 int fts_num_msgs = 0;
555 long *fts_msgs = NULL;
559 /* Strip parentheses. We realize that this method will not work
560 * in all cases, but it seems to work with all currently available
561 * client software. Revisit later...
563 for (i=0; i<num_items; ++i) {
564 if (itemlist[i][0] == '(') {
565 strcpy(&itemlist[i][0], &itemlist[i][1]);
567 if (itemlist[i][strlen(itemlist[i])-1] == ')') {
568 itemlist[i][strlen(itemlist[i])-1] = 0;
572 /* If there is a BODY search criterion in the query, use our full
573 * text index to disqualify messages that don't have any chance of
574 * matching. (Only do this if the index is enabled!!)
576 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
577 if (!strcasecmp(itemlist[i], "BODY")) {
578 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1], "fulltext");
579 if (fts_num_msgs > 0) {
580 for (j=0; j < IMAP->num_msgs; ++j) {
581 if (IMAP->flags[j] & IMAP_SELECTED) {
583 for (k=0; k<fts_num_msgs; ++k) {
584 if (IMAP->msgids[j] == fts_msgs[k]) {
590 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
594 else { /* no hits on the index; disqualify every message */
595 for (j=0; j < IMAP->num_msgs; ++j) {
596 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
605 /* Now go through the messages and apply all search criteria. */
607 cprintf("* SEARCH ");
608 if (IMAP->num_msgs > 0)
609 for (i = 0; i < IMAP->num_msgs; ++i)
610 if (IMAP->flags[i] & IMAP_SELECTED) {
611 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
612 if (num_results != 0) {
616 cprintf("%ld", IMAP->msgids[i]);
630 * This function is called by the main command loop.
632 void imap_search(int num_parms, char *parms[]) {
636 cprintf("%s BAD invalid parameters\r\n", parms[0]);
640 for (i = 0; i < IMAP->num_msgs; ++i) {
641 IMAP->flags[i] |= IMAP_SELECTED;
644 imap_do_search(num_parms-2, &parms[2], 0);
645 cprintf("%s OK SEARCH completed\r\n", parms[0]);
649 * This function is called by the main command loop.
651 void imap_uidsearch(int num_parms, char *parms[]) {
655 cprintf("%s BAD invalid parameters\r\n", parms[0]);
659 for (i = 0; i < IMAP->num_msgs; ++i) {
660 IMAP->flags[i] |= IMAP_SELECTED;
663 imap_do_search(num_parms-3, &parms[3], 1);
664 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);