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
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
61 #include "internet_addressing.h"
62 #include "serv_imap.h"
63 #include "imap_tools.h"
64 #include "imap_fetch.h"
65 #include "imap_search.h"
70 * imap_do_search() calls imap_do_search_msg() to search an individual
71 * message after it has been fetched from the disk. This function returns
72 * nonzero if there is a match.
74 * supplied_msg MAY be used to pass a pointer to the message in memory,
75 * if for some reason it's already been loaded. If not, the message will
76 * be loaded only if one or more search criteria require it.
78 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
79 int num_items, char **itemlist, int is_uid) {
87 struct CtdlMessage *msg = NULL;
88 int need_to_free_msg = 0;
95 /* Initially we start at the beginning. */
98 /* Check for the dreaded NOT criterion. */
99 if (!strcasecmp(itemlist[0], "NOT")) {
104 /* Check for the dreaded OR criterion. */
105 if (!strcasecmp(itemlist[0], "OR")) {
110 /* Now look for criteria. */
111 if (!strcasecmp(itemlist[pos], "ALL")) {
116 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
117 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
123 else if (!strcasecmp(itemlist[pos], "BCC")) {
125 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
126 need_to_free_msg = 1;
129 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
130 if (fieldptr != NULL) {
131 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
140 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
142 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
143 need_to_free_msg = 1;
146 if (msg->cm_fields['T'] != NULL) {
147 if (imap_datecmp(itemlist[pos+1],
148 atol(msg->cm_fields['T'])) < 0) {
156 else if (!strcasecmp(itemlist[pos], "BODY")) {
158 /* If fulltext indexing is active, on this server,
159 * all messages have already been qualified.
161 if (config.c_enable_fulltext) {
165 /* Otherwise, we have to do a slow search. */
168 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
169 need_to_free_msg = 1;
172 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
181 else if (!strcasecmp(itemlist[pos], "CC")) {
183 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
184 need_to_free_msg = 1;
187 fieldptr = msg->cm_fields['Y'];
188 if (fieldptr != NULL) {
189 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
194 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
195 if (fieldptr != NULL) {
196 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
206 else if (!strcasecmp(itemlist[pos], "DELETED")) {
207 if (IMAP->flags[seq-1] & IMAP_DELETED) {
213 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
214 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
220 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
221 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
227 else if (!strcasecmp(itemlist[pos], "FROM")) {
229 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
230 need_to_free_msg = 1;
233 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
236 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
243 else if (!strcasecmp(itemlist[pos], "HEADER")) {
245 /* We've got to do a slow search for this because the client
246 * might be asking for an RFC822 header field that has not been
247 * converted into a Citadel header field. That requires
248 * examining the message body.
251 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
252 need_to_free_msg = 1;
257 CC->redirect_buffer = malloc(SIZ);
258 CC->redirect_len = 0;
259 CC->redirect_alloc = SIZ;
260 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
262 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
263 if (fieldptr != NULL) {
264 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
270 free(CC->redirect_buffer);
271 CC->redirect_buffer = NULL;
272 CC->redirect_len = 0;
273 CC->redirect_alloc = 0;
276 pos += 3; /* Yes, three */
279 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
280 /* not implemented */
284 else if (!strcasecmp(itemlist[pos], "LARGER")) {
286 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
287 need_to_free_msg = 1;
290 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
297 else if (!strcasecmp(itemlist[pos], "NEW")) {
298 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
304 else if (!strcasecmp(itemlist[pos], "OLD")) {
305 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
311 else if (!strcasecmp(itemlist[pos], "ON")) {
313 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
314 need_to_free_msg = 1;
317 if (msg->cm_fields['T'] != NULL) {
318 if (imap_datecmp(itemlist[pos+1],
319 atol(msg->cm_fields['T'])) == 0) {
327 else if (!strcasecmp(itemlist[pos], "RECENT")) {
328 if (IMAP->flags[seq-1] & IMAP_RECENT) {
334 else if (!strcasecmp(itemlist[pos], "SEEN")) {
335 if (IMAP->flags[seq-1] & IMAP_SEEN) {
341 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
343 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
344 need_to_free_msg = 1;
347 if (msg->cm_fields['T'] != NULL) {
348 if (imap_datecmp(itemlist[pos+1],
349 atol(msg->cm_fields['T'])) < 0) {
357 else if (!strcasecmp(itemlist[pos], "SENTON")) {
359 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
360 need_to_free_msg = 1;
363 if (msg->cm_fields['T'] != NULL) {
364 if (imap_datecmp(itemlist[pos+1],
365 atol(msg->cm_fields['T'])) == 0) {
373 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
375 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
376 need_to_free_msg = 1;
379 if (msg->cm_fields['T'] != NULL) {
380 if (imap_datecmp(itemlist[pos+1],
381 atol(msg->cm_fields['T'])) >= 0) {
389 else if (!strcasecmp(itemlist[pos], "SINCE")) {
391 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
392 need_to_free_msg = 1;
395 if (msg->cm_fields['T'] != NULL) {
396 if (imap_datecmp(itemlist[pos+1],
397 atol(msg->cm_fields['T'])) >= 0) {
405 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
407 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
408 need_to_free_msg = 1;
411 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
418 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
420 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
421 need_to_free_msg = 1;
424 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
431 else if (!strcasecmp(itemlist[pos], "TEXT")) {
433 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
434 need_to_free_msg = 1;
437 for (i='A'; i<='Z'; ++i) {
438 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
446 else if (!strcasecmp(itemlist[pos], "TO")) {
448 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
449 need_to_free_msg = 1;
452 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
459 /* FIXME this is b0rken. fix it. */
460 else if (imap_is_message_set(itemlist[pos])) {
461 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
467 /* FIXME this is b0rken. fix it. */
468 else if (!strcasecmp(itemlist[pos], "UID")) {
469 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
475 /* Now here come the 'UN' criteria. Why oh why do we have to
476 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
477 * can't there be *one* way to do things? More gratuitous complexity.
480 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
481 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
487 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
488 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
494 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
495 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
501 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
502 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
508 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
513 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
514 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
520 /* Remember to negate if we were told to */
525 /* Keep going if there are more criteria! */
526 if (pos < num_items) {
529 match = (match || imap_do_search_msg(seq, msg,
530 num_items - pos, &itemlist[pos], is_uid));
533 match = (match && imap_do_search_msg(seq, msg,
534 num_items - pos, &itemlist[pos], is_uid));
539 if (need_to_free_msg) {
540 CtdlFreeMessage(msg);
547 * imap_search() calls imap_do_search() to do its actual work, once it's
548 * validated and boiled down the request a bit.
550 void imap_do_search(int num_items, char **itemlist, int is_uid) {
552 int fts_num_msgs = 0;
553 long *fts_msgs = NULL;
557 /* Strip parentheses. We realize that this method will not work
558 * in all cases, but it seems to work with all currently available
559 * client software. Revisit later...
561 for (i=0; i<num_items; ++i) {
562 if (itemlist[i][0] == '(') {
563 strcpy(&itemlist[i][0], &itemlist[i][1]);
565 if (itemlist[i][strlen(itemlist[i])-1] == ')') {
566 itemlist[i][strlen(itemlist[i])-1] = 0;
570 /* If there is a BODY search criterion in the query, use our full
571 * text index to disqualify messages that don't have any chance of
572 * matching. (Only do this if the index is enabled!!)
574 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
575 if (!strcasecmp(itemlist[i], "BODY")) {
576 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1], "fulltext");
577 if (fts_num_msgs > 0) {
578 for (j=0; j < IMAP->num_msgs; ++j) {
579 if (IMAP->flags[j] & IMAP_SELECTED) {
581 for (k=0; k<fts_num_msgs; ++k) {
582 if (IMAP->msgids[j] == fts_msgs[k]) {
588 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
592 else { /* no hits on the index; disqualify every message */
593 for (j=0; j < IMAP->num_msgs; ++j) {
594 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
603 /* Now go through the messages and apply all search criteria. */
605 cprintf("* SEARCH ");
606 if (IMAP->num_msgs > 0)
607 for (i = 0; i < IMAP->num_msgs; ++i)
608 if (IMAP->flags[i] & IMAP_SELECTED) {
609 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
610 if (num_results != 0) {
614 cprintf("%ld", IMAP->msgids[i]);
628 * This function is called by the main command loop.
630 void imap_search(int num_parms, char *parms[]) {
634 cprintf("%s BAD invalid parameters\r\n", parms[0]);
638 for (i = 0; i < IMAP->num_msgs; ++i) {
639 IMAP->flags[i] |= IMAP_SELECTED;
642 imap_do_search(num_parms-2, &parms[2], 0);
643 cprintf("%s OK SEARCH completed\r\n", parms[0]);
647 * This function is called by the main command loop.
649 void imap_uidsearch(int num_parms, char *parms[]) {
653 cprintf("%s BAD invalid parameters\r\n", parms[0]);
657 for (i = 0; i < IMAP->num_msgs; ++i) {
658 IMAP->flags[i] |= IMAP_SELECTED;
661 imap_do_search(num_parms-3, &parms[3], 1);
662 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);