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"
62 #include "internet_addressing.h"
63 #include "imap_tools.h"
64 #include "serv_imap.h"
65 #include "imap_fetch.h"
66 #include "imap_search.h"
71 * imap_do_search() calls imap_do_search_msg() to search an individual
72 * message after it has been fetched from the disk. This function returns
73 * nonzero if there is a match.
75 * supplied_msg MAY be used to pass a pointer to the message in memory,
76 * if for some reason it's already been loaded. If not, the message will
77 * be loaded only if one or more search criteria require it.
79 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
80 int num_items, ConstStr *itemlist, int is_uid) {
88 struct CtdlMessage *msg = NULL;
89 int need_to_free_msg = 0;
96 /* Initially we start at the beginning. */
99 /* Check for the dreaded NOT criterion. */
100 if (!strcasecmp(itemlist[0].Key, "NOT")) {
105 /* Check for the dreaded OR criterion. */
106 if (!strcasecmp(itemlist[0].Key, "OR")) {
111 /* Now look for criteria. */
112 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
117 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
118 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
124 else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
126 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
127 need_to_free_msg = 1;
130 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
131 if (fieldptr != NULL) {
132 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
141 else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
143 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
144 need_to_free_msg = 1;
147 if (msg->cm_fields['T'] != NULL) {
148 if (imap_datecmp(itemlist[pos+1].Key,
149 atol(msg->cm_fields['T'])) < 0) {
157 else if (!strcasecmp(itemlist[pos].Key, "BODY")) {
159 /* If fulltext indexing is active, on this server,
160 * all messages have already been qualified.
162 if (config.c_enable_fulltext) {
166 /* Otherwise, we have to do a slow search. */
169 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
170 need_to_free_msg = 1;
173 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1].Key)) {
182 else if (!strcasecmp(itemlist[pos].Key, "CC")) {
184 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
185 need_to_free_msg = 1;
188 fieldptr = msg->cm_fields['Y'];
189 if (fieldptr != NULL) {
190 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
195 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
196 if (fieldptr != NULL) {
197 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
207 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
208 if (IMAP->flags[seq-1] & IMAP_DELETED) {
214 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
215 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
221 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
222 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
228 else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
230 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
231 need_to_free_msg = 1;
234 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1].Key)) {
237 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1].Key)) {
244 else if (!strcasecmp(itemlist[pos].Key, "HEADER")) {
246 /* We've got to do a slow search for this because the client
247 * might be asking for an RFC822 header field that has not been
248 * converted into a Citadel header field. That requires
249 * examining the message body.
252 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
253 need_to_free_msg = 1;
258 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
259 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
261 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
262 if (fieldptr != NULL) {
263 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
269 FreeStrBuf(&CC->redirect_buffer);
272 pos += 3; /* Yes, three */
275 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
276 /* not implemented */
280 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
282 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
283 need_to_free_msg = 1;
286 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1].Key)) {
293 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
294 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
300 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
301 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
307 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
309 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
310 need_to_free_msg = 1;
313 if (msg->cm_fields['T'] != NULL) {
314 if (imap_datecmp(itemlist[pos+1].Key,
315 atol(msg->cm_fields['T'])) == 0) {
323 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
324 if (IMAP->flags[seq-1] & IMAP_RECENT) {
330 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
331 if (IMAP->flags[seq-1] & IMAP_SEEN) {
337 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
339 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
340 need_to_free_msg = 1;
343 if (msg->cm_fields['T'] != NULL) {
344 if (imap_datecmp(itemlist[pos+1].Key,
345 atol(msg->cm_fields['T'])) < 0) {
353 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
355 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
356 need_to_free_msg = 1;
359 if (msg->cm_fields['T'] != NULL) {
360 if (imap_datecmp(itemlist[pos+1].Key,
361 atol(msg->cm_fields['T'])) == 0) {
369 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
371 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
372 need_to_free_msg = 1;
375 if (msg->cm_fields['T'] != NULL) {
376 if (imap_datecmp(itemlist[pos+1].Key,
377 atol(msg->cm_fields['T'])) >= 0) {
385 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
387 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
388 need_to_free_msg = 1;
391 if (msg->cm_fields['T'] != NULL) {
392 if (imap_datecmp(itemlist[pos+1].Key,
393 atol(msg->cm_fields['T'])) >= 0) {
401 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
403 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
404 need_to_free_msg = 1;
407 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1].Key)) {
414 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
416 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
417 need_to_free_msg = 1;
420 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1].Key)) {
427 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
429 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
430 need_to_free_msg = 1;
433 for (i='A'; i<='Z'; ++i) {
434 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
442 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
444 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
445 need_to_free_msg = 1;
448 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1].Key)) {
455 /* FIXME this is b0rken. fix it. */
456 else if (imap_is_message_set(itemlist[pos].Key)) {
457 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
463 /* FIXME this is b0rken. fix it. */
464 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
465 if (is_msg_in_sequence_set(itemlist[pos+1].Key, IMAP->msgids[seq-1])) {
471 /* Now here come the 'UN' criteria. Why oh why do we have to
472 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
473 * can't there be *one* way to do things? More gratuitous complexity.
476 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
477 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
483 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
484 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
490 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
491 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
497 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
498 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
504 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
509 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
510 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
516 /* Remember to negate if we were told to */
521 /* Keep going if there are more criteria! */
522 if (pos < num_items) {
525 match = (match || imap_do_search_msg(seq, msg,
526 num_items - pos, &itemlist[pos], is_uid));
529 match = (match && imap_do_search_msg(seq, msg,
530 num_items - pos, &itemlist[pos], is_uid));
535 if (need_to_free_msg) {
536 CtdlFreeMessage(msg);
543 * imap_search() calls imap_do_search() to do its actual work, once it's
544 * validated and boiled down the request a bit.
546 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
548 int fts_num_msgs = 0;
549 long *fts_msgs = NULL;
553 /* Strip parentheses. We realize that this method will not work
554 * in all cases, but it seems to work with all currently available
555 * client software. Revisit later...
557 for (i=0; i<num_items; ++i) {
558 if (itemlist[i].Key[0] == '(') {
560 TokenCutLeft(&IMAP->Cmd,
564 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
565 TokenCutRight(&IMAP->Cmd,
571 /* If there is a BODY search criterion in the query, use our full
572 * text index to disqualify messages that don't have any chance of
573 * matching. (Only do this if the index is enabled!!)
575 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
576 if (!strcasecmp(itemlist[i].Key, "BODY")) {
577 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
578 if (fts_num_msgs > 0) {
579 for (j=0; j < IMAP->num_msgs; ++j) {
580 if (IMAP->flags[j] & IMAP_SELECTED) {
582 for (k=0; k<fts_num_msgs; ++k) {
583 if (IMAP->msgids[j] == fts_msgs[k]) {
589 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
593 else { /* no hits on the index; disqualify every message */
594 for (j=0; j < IMAP->num_msgs; ++j) {
595 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
604 /* Now go through the messages and apply all search criteria. */
606 cprintf("* SEARCH ");
607 if (IMAP->num_msgs > 0)
608 for (i = 0; i < IMAP->num_msgs; ++i)
609 if (IMAP->flags[i] & IMAP_SELECTED) {
610 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
611 if (num_results != 0) {
615 cprintf("%ld", IMAP->msgids[i]);
629 * This function is called by the main command loop.
631 void imap_search(int num_parms, ConstStr *Params) {
635 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
639 for (i = 0; i < IMAP->num_msgs; ++i) {
640 IMAP->flags[i] |= IMAP_SELECTED;
643 imap_do_search(num_parms-2, &Params[2], 0);
644 cprintf("%s OK SEARCH completed\r\n", Params[0].Key);
648 * This function is called by the main command loop.
650 void imap_uidsearch(int num_parms, ConstStr *Params) {
654 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
658 for (i = 0; i < IMAP->num_msgs; ++i) {
659 IMAP->flags[i] |= IMAP_SELECTED;
662 imap_do_search(num_parms-3, &Params[3], 1);
663 cprintf("%s OK UID SEARCH completed\r\n", Params[0].Key);