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 "imap_tools.h"
65 #include "serv_imap.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, ConstStr *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].Key, "NOT")) {
106 /* Check for the dreaded OR criterion. */
107 if (!strcasecmp(itemlist[0].Key, "OR")) {
112 /* Now look for criteria. */
113 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
118 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
119 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
125 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
142 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
150 atol(msg->cm_fields['T'])) < 0) {
158 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
183 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
196 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
197 if (fieldptr != NULL) {
198 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
208 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
209 if (IMAP->flags[seq-1] & IMAP_DELETED) {
215 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
216 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
222 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
223 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
229 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
238 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1].Key)) {
245 else if (!strcasecmp(itemlist[pos].Key, "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].Key);
265 if (fieldptr != NULL) {
266 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
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].Key, "KEYWORD")) {
282 /* not implemented */
286 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
299 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
300 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
306 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
307 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
313 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
321 atol(msg->cm_fields['T'])) == 0) {
329 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
330 if (IMAP->flags[seq-1] & IMAP_RECENT) {
336 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
337 if (IMAP->flags[seq-1] & IMAP_SEEN) {
343 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
351 atol(msg->cm_fields['T'])) < 0) {
359 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
367 atol(msg->cm_fields['T'])) == 0) {
375 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
383 atol(msg->cm_fields['T'])) >= 0) {
391 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
399 atol(msg->cm_fields['T'])) >= 0) {
407 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
420 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
433 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
448 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
461 /* FIXME this is b0rken. fix it. */
462 else if (imap_is_message_set(itemlist[pos].Key)) {
463 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
469 /* FIXME this is b0rken. fix it. */
470 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
471 if (is_msg_in_sequence_set(itemlist[pos+1].Key, 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].Key, "UNANSWERED")) {
483 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
489 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
490 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
496 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
497 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
503 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
504 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
510 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
515 else if (!strcasecmp(itemlist[pos].Key, "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, ConstStr *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].Key[0] == '(') {
566 TokenCutLeft(&IMAP->Cmd,
570 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
571 TokenCutRight(&IMAP->Cmd,
577 /* If there is a BODY search criterion in the query, use our full
578 * text index to disqualify messages that don't have any chance of
579 * matching. (Only do this if the index is enabled!!)
581 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
582 if (!strcasecmp(itemlist[i].Key, "BODY")) {
583 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
584 if (fts_num_msgs > 0) {
585 for (j=0; j < IMAP->num_msgs; ++j) {
586 if (IMAP->flags[j] & IMAP_SELECTED) {
588 for (k=0; k<fts_num_msgs; ++k) {
589 if (IMAP->msgids[j] == fts_msgs[k]) {
595 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
599 else { /* no hits on the index; disqualify every message */
600 for (j=0; j < IMAP->num_msgs; ++j) {
601 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
610 /* Now go through the messages and apply all search criteria. */
612 cprintf("* SEARCH ");
613 if (IMAP->num_msgs > 0)
614 for (i = 0; i < IMAP->num_msgs; ++i)
615 if (IMAP->flags[i] & IMAP_SELECTED) {
616 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
617 if (num_results != 0) {
621 cprintf("%ld", IMAP->msgids[i]);
635 * This function is called by the main command loop.
637 void imap_search(int num_parms, ConstStr *Params) {
641 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
645 for (i = 0; i < IMAP->num_msgs; ++i) {
646 IMAP->flags[i] |= IMAP_SELECTED;
649 imap_do_search(num_parms-2, &Params[2], 0);
650 cprintf("%s OK SEARCH completed\r\n", Params[0].Key);
654 * This function is called by the main command loop.
656 void imap_uidsearch(int num_parms, ConstStr *Params) {
660 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
664 for (i = 0; i < IMAP->num_msgs; ++i) {
665 IMAP->flags[i] |= IMAP_SELECTED;
668 imap_do_search(num_parms-3, &Params[3], 1);
669 cprintf("%s OK UID SEARCH completed\r\n", Params[0].Key);