2 * Implements IMAP's gratuitously complex SEARCH command.
5 * Copyright (c) 2001-2009 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "ctdl_module.h"
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "sysdep_decls.h"
54 #include "citserver.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
69 * imap_do_search() calls imap_do_search_msg() to search an individual
70 * message after it has been fetched from the disk. This function returns
71 * nonzero if there is a match.
73 * supplied_msg MAY be used to pass a pointer to the message in memory,
74 * if for some reason it's already been loaded. If not, the message will
75 * be loaded only if one or more search criteria require it.
77 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
78 int num_items, ConstStr *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].Key, "NOT")) {
104 /* Check for the dreaded OR criterion. */
105 if (!strcasecmp(itemlist[0].Key, "OR")) {
110 /* Now look for criteria. */
111 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
116 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
117 if (Imap->flags[seq-1] & IMAP_ANSWERED) {
123 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
140 else if (!strcasecmp(itemlist[pos].Key, "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].Key,
148 atol(msg->cm_fields['T'])) < 0) {
156 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
181 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
194 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
195 if (fieldptr != NULL) {
196 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
206 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
207 if (Imap->flags[seq-1] & IMAP_DELETED) {
213 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
214 if (Imap->flags[seq-1] & IMAP_DRAFT) {
220 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
221 if (Imap->flags[seq-1] & IMAP_FLAGGED) {
227 else if (!strcasecmp(itemlist[pos].Key, "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].Key)) {
236 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1].Key)) {
243 else if (!strcasecmp(itemlist[pos].Key, "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 = NewStrBufPlain(NULL, SIZ);
258 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
260 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
261 if (fieldptr != NULL) {
262 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
268 FreeStrBuf(&CC->redirect_buffer);
271 pos += 3; /* Yes, three */
274 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
275 /* not implemented */
279 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
281 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
282 need_to_free_msg = 1;
285 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1].Key)) {
292 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
293 if ( (Imap->flags[seq-1] & IMAP_RECENT) && (!(Imap->flags[seq-1] & IMAP_SEEN))) {
299 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
300 if (!(Imap->flags[seq-1] & IMAP_RECENT)) {
306 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
308 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
309 need_to_free_msg = 1;
312 if (msg->cm_fields['T'] != NULL) {
313 if (imap_datecmp(itemlist[pos+1].Key,
314 atol(msg->cm_fields['T'])) == 0) {
322 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
323 if (Imap->flags[seq-1] & IMAP_RECENT) {
329 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
330 if (Imap->flags[seq-1] & IMAP_SEEN) {
336 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
338 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
339 need_to_free_msg = 1;
342 if (msg->cm_fields['T'] != NULL) {
343 if (imap_datecmp(itemlist[pos+1].Key,
344 atol(msg->cm_fields['T'])) < 0) {
352 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
354 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
355 need_to_free_msg = 1;
358 if (msg->cm_fields['T'] != NULL) {
359 if (imap_datecmp(itemlist[pos+1].Key,
360 atol(msg->cm_fields['T'])) == 0) {
368 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
370 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
371 need_to_free_msg = 1;
374 if (msg->cm_fields['T'] != NULL) {
375 if (imap_datecmp(itemlist[pos+1].Key,
376 atol(msg->cm_fields['T'])) >= 0) {
384 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
386 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
387 need_to_free_msg = 1;
390 if (msg->cm_fields['T'] != NULL) {
391 if (imap_datecmp(itemlist[pos+1].Key,
392 atol(msg->cm_fields['T'])) >= 0) {
400 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
402 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
403 need_to_free_msg = 1;
406 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1].Key)) {
413 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
415 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
416 need_to_free_msg = 1;
419 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1].Key)) {
426 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
428 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
429 need_to_free_msg = 1;
432 for (i='A'; i<='Z'; ++i) {
433 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
441 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
443 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
444 need_to_free_msg = 1;
447 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1].Key)) {
454 /* FIXME this is b0rken. fix it. */
455 else if (imap_is_message_set(itemlist[pos].Key)) {
456 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
462 /* FIXME this is b0rken. fix it. */
463 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
464 if (is_msg_in_sequence_set(itemlist[pos+1].Key, Imap->msgids[seq-1])) {
470 /* Now here come the 'UN' criteria. Why oh why do we have to
471 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
472 * can't there be *one* way to do things? More gratuitous complexity.
475 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
476 if ((Imap->flags[seq-1] & IMAP_ANSWERED) == 0) {
482 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
483 if ((Imap->flags[seq-1] & IMAP_DELETED) == 0) {
489 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
490 if ((Imap->flags[seq-1] & IMAP_DRAFT) == 0) {
496 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
497 if ((Imap->flags[seq-1] & IMAP_FLAGGED) == 0) {
503 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
508 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
509 if ((Imap->flags[seq-1] & IMAP_SEEN) == 0) {
515 /* Remember to negate if we were told to */
520 /* Keep going if there are more criteria! */
521 if (pos < num_items) {
524 match = (match || imap_do_search_msg(seq, msg,
525 num_items - pos, &itemlist[pos], is_uid));
528 match = (match && imap_do_search_msg(seq, msg,
529 num_items - pos, &itemlist[pos], is_uid));
534 if (need_to_free_msg) {
535 CtdlFreeMessage(msg);
542 * imap_search() calls imap_do_search() to do its actual work, once it's
543 * validated and boiled down the request a bit.
545 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
546 citimap *Imap = IMAP;
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. */
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 IAPrintf("%ld", Imap->msgids[i]);
629 * This function is called by the main command loop.
631 void imap_search(int num_parms, ConstStr *Params) {
635 IReply("BAD invalid parameters");
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 IReply("OK SEARCH completed");
648 * This function is called by the main command loop.
650 void imap_uidsearch(int num_parms, ConstStr *Params) {
654 IReply("BAD invalid parameters");
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 IReply("OK UID SEARCH completed");