2 * Implements IMAP's gratuitously complex SEARCH command.
4 * Copyright (c) 2001-2015 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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include "ctdl_module.h"
26 #include <sys/types.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
43 #include <libcitadel.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
53 #include "internet_addressing.h"
54 #include "serv_imap.h"
55 #include "imap_tools.h"
56 #include "imap_fetch.h"
57 #include "imap_search.h"
62 * imap_do_search() calls imap_do_search_msg() to search an individual
63 * message after it has been fetched from the disk. This function returns
64 * nonzero if there is a match.
66 * supplied_msg MAY be used to pass a pointer to the message in memory,
67 * if for some reason it's already been loaded. If not, the message will
68 * be loaded only if one or more search criteria require it.
70 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
71 int num_items, ConstStr *itemlist, int is_uid) {
80 struct CtdlMessage *msg = NULL;
81 int need_to_free_msg = 0;
88 /* Initially we start at the beginning. */
91 /* Check for the dreaded NOT criterion. */
92 if (!strcasecmp(itemlist[0].Key, "NOT")) {
97 /* Check for the dreaded OR criterion. */
98 if (!strcasecmp(itemlist[0].Key, "OR")) {
103 /* Now look for criteria. */
104 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
109 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
110 if (Imap->flags[seq-1] & IMAP_ANSWERED) {
116 else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
118 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
119 need_to_free_msg = 1;
122 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
123 if (fieldptr != NULL) {
124 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
133 else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
135 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
136 need_to_free_msg = 1;
139 if (!CM_IsEmpty(msg, eTimestamp)) {
140 if (imap_datecmp(itemlist[pos+1].Key,
141 atol(msg->cm_fields[eTimestamp])) < 0) {
149 else if (!strcasecmp(itemlist[pos].Key, "BODY")) {
151 /* If fulltext indexing is active, on this server,
152 * all messages have already been qualified.
154 if (CtdlGetConfigInt("c_enable_fulltext")) {
158 /* Otherwise, we have to do a slow search. */
161 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
162 need_to_free_msg = 1;
165 if (bmstrcasestr(msg->cm_fields[eMesageText], itemlist[pos+1].Key)) {
174 else if (!strcasecmp(itemlist[pos].Key, "CC")) {
176 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
177 need_to_free_msg = 1;
180 fieldptr = msg->cm_fields[eCarbonCopY];
181 if (fieldptr != NULL) {
182 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
187 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
188 if (fieldptr != NULL) {
189 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
199 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
200 if (Imap->flags[seq-1] & IMAP_DELETED) {
206 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
207 if (Imap->flags[seq-1] & IMAP_DRAFT) {
213 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
214 if (Imap->flags[seq-1] & IMAP_FLAGGED) {
220 else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
222 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
223 need_to_free_msg = 1;
226 if (bmstrcasestr(msg->cm_fields[eAuthor], itemlist[pos+1].Key)) {
229 if (bmstrcasestr(msg->cm_fields[erFc822Addr], itemlist[pos+1].Key)) {
236 else if (!strcasecmp(itemlist[pos].Key, "HEADER")) {
238 /* We've got to do a slow search for this because the client
239 * might be asking for an RFC822 header field that has not been
240 * converted into a Citadel header field. That requires
241 * examining the message body.
244 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
245 need_to_free_msg = 1;
250 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
251 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
253 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
254 if (fieldptr != NULL) {
255 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
261 FreeStrBuf(&CC->redirect_buffer);
264 pos += 3; /* Yes, three */
267 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
268 /* not implemented */
272 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
274 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
275 need_to_free_msg = 1;
278 if (msg->cm_lengths[eMesageText] > atoi(itemlist[pos+1].Key)) {
285 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
286 if ( (Imap->flags[seq-1] & IMAP_RECENT) && (!(Imap->flags[seq-1] & IMAP_SEEN))) {
292 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
293 if (!(Imap->flags[seq-1] & IMAP_RECENT)) {
299 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
301 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
302 need_to_free_msg = 1;
305 if (!CM_IsEmpty(msg, eTimestamp)) {
306 if (imap_datecmp(itemlist[pos+1].Key,
307 atol(msg->cm_fields[eTimestamp])) == 0) {
315 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
316 if (Imap->flags[seq-1] & IMAP_RECENT) {
322 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
323 if (Imap->flags[seq-1] & IMAP_SEEN) {
329 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
331 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
332 need_to_free_msg = 1;
335 if (!CM_IsEmpty(msg, eTimestamp)) {
336 if (imap_datecmp(itemlist[pos+1].Key,
337 atol(msg->cm_fields[eTimestamp])) < 0) {
345 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
347 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
348 need_to_free_msg = 1;
351 if (!CM_IsEmpty(msg, eTimestamp)) {
352 if (imap_datecmp(itemlist[pos+1].Key,
353 atol(msg->cm_fields[eTimestamp])) == 0) {
361 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
363 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
364 need_to_free_msg = 1;
367 if (!CM_IsEmpty(msg, eTimestamp)) {
368 if (imap_datecmp(itemlist[pos+1].Key,
369 atol(msg->cm_fields[eTimestamp])) >= 0) {
377 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
379 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
380 need_to_free_msg = 1;
383 if (!CM_IsEmpty(msg, eTimestamp)) {
384 if (imap_datecmp(itemlist[pos+1].Key,
385 atol(msg->cm_fields[eTimestamp])) >= 0) {
393 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
395 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
396 need_to_free_msg = 1;
399 if (msg->cm_lengths[eMesageText] < atoi(itemlist[pos+1].Key)) {
406 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
408 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
409 need_to_free_msg = 1;
412 if (bmstrcasestr(msg->cm_fields[eMsgSubject], itemlist[pos+1].Key)) {
419 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
421 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
422 need_to_free_msg = 1;
425 for (i='A'; i<='Z'; ++i) {
426 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
434 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
436 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
437 need_to_free_msg = 1;
440 if (bmstrcasestr(msg->cm_fields[eRecipient], itemlist[pos+1].Key)) {
447 /* FIXME this is b0rken. fix it. */
448 else if (imap_is_message_set(itemlist[pos].Key)) {
449 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
455 /* FIXME this is b0rken. fix it. */
456 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
457 if (is_msg_in_sequence_set(itemlist[pos+1].Key, Imap->msgids[seq-1])) {
463 /* Now here come the 'UN' criteria. Why oh why do we have to
464 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
465 * can't there be *one* way to do things? More gratuitous complexity.
468 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
469 if ((Imap->flags[seq-1] & IMAP_ANSWERED) == 0) {
475 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
476 if ((Imap->flags[seq-1] & IMAP_DELETED) == 0) {
482 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
483 if ((Imap->flags[seq-1] & IMAP_DRAFT) == 0) {
489 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
490 if ((Imap->flags[seq-1] & IMAP_FLAGGED) == 0) {
496 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
501 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
502 if ((Imap->flags[seq-1] & IMAP_SEEN) == 0) {
508 /* Remember to negate if we were told to */
513 /* Keep going if there are more criteria! */
514 if (pos < num_items) {
517 match = (match || imap_do_search_msg(seq, msg,
518 num_items - pos, &itemlist[pos], is_uid));
521 match = (match && imap_do_search_msg(seq, msg,
522 num_items - pos, &itemlist[pos], is_uid));
527 if (need_to_free_msg) {
535 * imap_search() calls imap_do_search() to do its actual work, once it's
536 * validated and boiled down the request a bit.
538 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
539 citimap *Imap = IMAP;
541 int fts_num_msgs = 0;
542 long *fts_msgs = NULL;
546 /* Strip parentheses. We realize that this method will not work
547 * in all cases, but it seems to work with all currently available
548 * client software. Revisit later...
550 for (i=0; i<num_items; ++i) {
551 if (itemlist[i].Key[0] == '(') {
553 TokenCutLeft(&Imap->Cmd,
557 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
558 TokenCutRight(&Imap->Cmd,
564 /* If there is a BODY search criterion in the query, use our full
565 * text index to disqualify messages that don't have any chance of
566 * matching. (Only do this if the index is enabled!!)
568 if (CtdlGetConfigInt("c_enable_fulltext")) for (i=0; i<(num_items-1); ++i) {
569 if (!strcasecmp(itemlist[i].Key, "BODY")) {
570 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
571 if (fts_num_msgs > 0) {
572 for (j=0; j < Imap->num_msgs; ++j) {
573 if (Imap->flags[j] & IMAP_SELECTED) {
575 for (k=0; k<fts_num_msgs; ++k) {
576 if (Imap->msgids[j] == fts_msgs[k]) {
582 Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
586 else { /* no hits on the index; disqualify every message */
587 for (j=0; j < Imap->num_msgs; ++j) {
588 Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
597 /* Now go through the messages and apply all search criteria. */
600 if (Imap->num_msgs > 0)
601 for (i = 0; i < Imap->num_msgs; ++i)
602 if (Imap->flags[i] & IMAP_SELECTED) {
603 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
604 if (num_results != 0) {
608 IAPrintf("%ld", Imap->msgids[i]);
622 * This function is called by the main command loop.
624 void imap_search(int num_parms, ConstStr *Params) {
628 IReply("BAD invalid parameters");
632 for (i = 0; i < IMAP->num_msgs; ++i) {
633 IMAP->flags[i] |= IMAP_SELECTED;
636 imap_do_search(num_parms-2, &Params[2], 0);
637 IReply("OK SEARCH completed");
641 * This function is called by the main command loop.
643 void imap_uidsearch(int num_parms, ConstStr *Params) {
647 IReply("BAD invalid parameters");
651 for (i = 0; i < IMAP->num_msgs; ++i) {
652 IMAP->flags[i] |= IMAP_SELECTED;
655 imap_do_search(num_parms-3, &Params[3], 1);
656 IReply("OK UID SEARCH completed");