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 = NewStrBufPlain(NULL, SIZ);
260 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
262 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
263 if (fieldptr != NULL) {
264 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
270 FreeStrBuf(&CC->redirect_buffer);
273 pos += 3; /* Yes, three */
276 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
277 /* not implemented */
281 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
283 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
284 need_to_free_msg = 1;
287 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1].Key)) {
294 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
295 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
301 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
302 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
308 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
310 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
311 need_to_free_msg = 1;
314 if (msg->cm_fields['T'] != NULL) {
315 if (imap_datecmp(itemlist[pos+1].Key,
316 atol(msg->cm_fields['T'])) == 0) {
324 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
325 if (IMAP->flags[seq-1] & IMAP_RECENT) {
331 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
332 if (IMAP->flags[seq-1] & IMAP_SEEN) {
338 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
340 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
341 need_to_free_msg = 1;
344 if (msg->cm_fields['T'] != NULL) {
345 if (imap_datecmp(itemlist[pos+1].Key,
346 atol(msg->cm_fields['T'])) < 0) {
354 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
356 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
357 need_to_free_msg = 1;
360 if (msg->cm_fields['T'] != NULL) {
361 if (imap_datecmp(itemlist[pos+1].Key,
362 atol(msg->cm_fields['T'])) == 0) {
370 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
372 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
373 need_to_free_msg = 1;
376 if (msg->cm_fields['T'] != NULL) {
377 if (imap_datecmp(itemlist[pos+1].Key,
378 atol(msg->cm_fields['T'])) >= 0) {
386 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
388 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
389 need_to_free_msg = 1;
392 if (msg->cm_fields['T'] != NULL) {
393 if (imap_datecmp(itemlist[pos+1].Key,
394 atol(msg->cm_fields['T'])) >= 0) {
402 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
404 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
405 need_to_free_msg = 1;
408 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1].Key)) {
415 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
417 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
418 need_to_free_msg = 1;
421 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1].Key)) {
428 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
430 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
431 need_to_free_msg = 1;
434 for (i='A'; i<='Z'; ++i) {
435 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
443 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
445 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
446 need_to_free_msg = 1;
449 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1].Key)) {
456 /* FIXME this is b0rken. fix it. */
457 else if (imap_is_message_set(itemlist[pos].Key)) {
458 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
464 /* FIXME this is b0rken. fix it. */
465 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
466 if (is_msg_in_sequence_set(itemlist[pos+1].Key, IMAP->msgids[seq-1])) {
472 /* Now here come the 'UN' criteria. Why oh why do we have to
473 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
474 * can't there be *one* way to do things? More gratuitous complexity.
477 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
478 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
484 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
485 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
491 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
492 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
498 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
499 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
505 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
510 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
511 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
517 /* Remember to negate if we were told to */
522 /* Keep going if there are more criteria! */
523 if (pos < num_items) {
526 match = (match || imap_do_search_msg(seq, msg,
527 num_items - pos, &itemlist[pos], is_uid));
530 match = (match && imap_do_search_msg(seq, msg,
531 num_items - pos, &itemlist[pos], is_uid));
536 if (need_to_free_msg) {
537 CtdlFreeMessage(msg);
544 * imap_search() calls imap_do_search() to do its actual work, once it's
545 * validated and boiled down the request a bit.
547 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
549 int fts_num_msgs = 0;
550 long *fts_msgs = NULL;
554 /* Strip parentheses. We realize that this method will not work
555 * in all cases, but it seems to work with all currently available
556 * client software. Revisit later...
558 for (i=0; i<num_items; ++i) {
559 if (itemlist[i].Key[0] == '(') {
561 TokenCutLeft(&IMAP->Cmd,
565 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
566 TokenCutRight(&IMAP->Cmd,
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].Key, "BODY")) {
578 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "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, ConstStr *Params) {
636 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
640 for (i = 0; i < IMAP->num_msgs; ++i) {
641 IMAP->flags[i] |= IMAP_SELECTED;
644 imap_do_search(num_parms-2, &Params[2], 0);
645 cprintf("%s OK SEARCH completed\r\n", Params[0].Key);
649 * This function is called by the main command loop.
651 void imap_uidsearch(int num_parms, ConstStr *Params) {
655 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
659 for (i = 0; i < IMAP->num_msgs; ++i) {
660 IMAP->flags[i] |= IMAP_SELECTED;
663 imap_do_search(num_parms-3, &Params[3], 1);
664 cprintf("%s OK UID SEARCH completed\r\n", Params[0].Key);