2 * Implements IMAP's gratuitously complex SEARCH command.
4 * Copyright (c) 2001-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 #include "ctdl_module.h"
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"
59 #include "internet_addressing.h"
60 #include "serv_imap.h"
61 #include "imap_tools.h"
62 #include "imap_fetch.h"
63 #include "imap_search.h"
68 * imap_do_search() calls imap_do_search_msg() to search an individual
69 * message after it has been fetched from the disk. This function returns
70 * nonzero if there is a match.
72 * supplied_msg MAY be used to pass a pointer to the message in memory,
73 * if for some reason it's already been loaded. If not, the message will
74 * be loaded only if one or more search criteria require it.
76 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
77 int num_items, ConstStr *itemlist, int is_uid) {
86 struct CtdlMessage *msg = NULL;
87 int need_to_free_msg = 0;
94 /* Initially we start at the beginning. */
97 /* Check for the dreaded NOT criterion. */
98 if (!strcasecmp(itemlist[0].Key, "NOT")) {
103 /* Check for the dreaded OR criterion. */
104 if (!strcasecmp(itemlist[0].Key, "OR")) {
109 /* Now look for criteria. */
110 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
115 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
116 if (Imap->flags[seq-1] & IMAP_ANSWERED) {
122 else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
124 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
125 need_to_free_msg = 1;
128 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
129 if (fieldptr != NULL) {
130 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
139 else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
141 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
142 need_to_free_msg = 1;
145 if (msg->cm_fields['T'] != NULL) {
146 if (imap_datecmp(itemlist[pos+1].Key,
147 atol(msg->cm_fields['T'])) < 0) {
155 else if (!strcasecmp(itemlist[pos].Key, "BODY")) {
157 /* If fulltext indexing is active, on this server,
158 * all messages have already been qualified.
160 if (config.c_enable_fulltext) {
164 /* Otherwise, we have to do a slow search. */
167 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
168 need_to_free_msg = 1;
171 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1].Key)) {
180 else if (!strcasecmp(itemlist[pos].Key, "CC")) {
182 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
183 need_to_free_msg = 1;
186 fieldptr = msg->cm_fields['Y'];
187 if (fieldptr != NULL) {
188 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
193 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
194 if (fieldptr != NULL) {
195 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
205 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
206 if (Imap->flags[seq-1] & IMAP_DELETED) {
212 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
213 if (Imap->flags[seq-1] & IMAP_DRAFT) {
219 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
220 if (Imap->flags[seq-1] & IMAP_FLAGGED) {
226 else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
228 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
229 need_to_free_msg = 1;
232 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1].Key)) {
235 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1].Key)) {
242 else if (!strcasecmp(itemlist[pos].Key, "HEADER")) {
244 /* We've got to do a slow search for this because the client
245 * might be asking for an RFC822 header field that has not been
246 * converted into a Citadel header field. That requires
247 * examining the message body.
250 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
251 need_to_free_msg = 1;
256 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
257 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
259 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
260 if (fieldptr != NULL) {
261 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
267 FreeStrBuf(&CC->redirect_buffer);
270 pos += 3; /* Yes, three */
273 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
274 /* not implemented */
278 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
280 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
281 need_to_free_msg = 1;
284 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1].Key)) {
291 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
292 if ( (Imap->flags[seq-1] & IMAP_RECENT) && (!(Imap->flags[seq-1] & IMAP_SEEN))) {
298 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
299 if (!(Imap->flags[seq-1] & IMAP_RECENT)) {
305 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
307 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
308 need_to_free_msg = 1;
311 if (msg->cm_fields['T'] != NULL) {
312 if (imap_datecmp(itemlist[pos+1].Key,
313 atol(msg->cm_fields['T'])) == 0) {
321 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
322 if (Imap->flags[seq-1] & IMAP_RECENT) {
328 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
329 if (Imap->flags[seq-1] & IMAP_SEEN) {
335 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
337 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
338 need_to_free_msg = 1;
341 if (msg->cm_fields['T'] != NULL) {
342 if (imap_datecmp(itemlist[pos+1].Key,
343 atol(msg->cm_fields['T'])) < 0) {
351 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
353 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
354 need_to_free_msg = 1;
357 if (msg->cm_fields['T'] != NULL) {
358 if (imap_datecmp(itemlist[pos+1].Key,
359 atol(msg->cm_fields['T'])) == 0) {
367 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
369 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
370 need_to_free_msg = 1;
373 if (msg->cm_fields['T'] != NULL) {
374 if (imap_datecmp(itemlist[pos+1].Key,
375 atol(msg->cm_fields['T'])) >= 0) {
383 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
385 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
386 need_to_free_msg = 1;
389 if (msg->cm_fields['T'] != NULL) {
390 if (imap_datecmp(itemlist[pos+1].Key,
391 atol(msg->cm_fields['T'])) >= 0) {
399 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
401 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
402 need_to_free_msg = 1;
405 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1].Key)) {
412 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
414 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
415 need_to_free_msg = 1;
418 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1].Key)) {
425 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
427 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
428 need_to_free_msg = 1;
431 for (i='A'; i<='Z'; ++i) {
432 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
440 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
442 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
443 need_to_free_msg = 1;
446 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1].Key)) {
453 /* FIXME this is b0rken. fix it. */
454 else if (imap_is_message_set(itemlist[pos].Key)) {
455 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
461 /* FIXME this is b0rken. fix it. */
462 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
463 if (is_msg_in_sequence_set(itemlist[pos+1].Key, Imap->msgids[seq-1])) {
469 /* Now here come the 'UN' criteria. Why oh why do we have to
470 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
471 * can't there be *one* way to do things? More gratuitous complexity.
474 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
475 if ((Imap->flags[seq-1] & IMAP_ANSWERED) == 0) {
481 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
482 if ((Imap->flags[seq-1] & IMAP_DELETED) == 0) {
488 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
489 if ((Imap->flags[seq-1] & IMAP_DRAFT) == 0) {
495 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
496 if ((Imap->flags[seq-1] & IMAP_FLAGGED) == 0) {
502 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
507 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
508 if ((Imap->flags[seq-1] & IMAP_SEEN) == 0) {
514 /* Remember to negate if we were told to */
519 /* Keep going if there are more criteria! */
520 if (pos < num_items) {
523 match = (match || imap_do_search_msg(seq, msg,
524 num_items - pos, &itemlist[pos], is_uid));
527 match = (match && imap_do_search_msg(seq, msg,
528 num_items - pos, &itemlist[pos], is_uid));
533 if (need_to_free_msg) {
534 CtdlFreeMessage(msg);
541 * imap_search() calls imap_do_search() to do its actual work, once it's
542 * validated and boiled down the request a bit.
544 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
545 citimap *Imap = IMAP;
547 int fts_num_msgs = 0;
548 long *fts_msgs = NULL;
552 /* Strip parentheses. We realize that this method will not work
553 * in all cases, but it seems to work with all currently available
554 * client software. Revisit later...
556 for (i=0; i<num_items; ++i) {
557 if (itemlist[i].Key[0] == '(') {
559 TokenCutLeft(&Imap->Cmd,
563 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
564 TokenCutRight(&Imap->Cmd,
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].Key, "BODY")) {
576 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "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. */
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 IAPrintf("%ld", Imap->msgids[i]);
628 * This function is called by the main command loop.
630 void imap_search(int num_parms, ConstStr *Params) {
634 IReply("BAD invalid parameters");
638 for (i = 0; i < IMAP->num_msgs; ++i) {
639 IMAP->flags[i] |= IMAP_SELECTED;
642 imap_do_search(num_parms-2, &Params[2], 0);
643 IReply("OK SEARCH completed");
647 * This function is called by the main command loop.
649 void imap_uidsearch(int num_parms, ConstStr *Params) {
653 IReply("BAD invalid parameters");
657 for (i = 0; i < IMAP->num_msgs; ++i) {
658 IMAP->flags[i] |= IMAP_SELECTED;
661 imap_do_search(num_parms-3, &Params[3], 1);
662 IReply("OK UID SEARCH completed");