1 // Implements IMAP's gratuitously complex SEARCH command.
3 // Copyright (c) 2001-2024 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License v3.
7 #include "../../ctdl_module.h"
8 #include "../../sysdep.h"
16 #include <sys/types.h>
22 #include <libcitadel.h>
23 #include "../../citadel_defs.h"
24 #include "../../server.h"
25 #include "../../sysdep_decls.h"
26 #include "../../citserver.h"
27 #include "../../support.h"
28 #include "../../config.h"
29 #include "../../user_ops.h"
30 #include "../../database.h"
31 #include "../../msgbase.h"
32 #include "../../internet_addressing.h"
33 #include "serv_imap.h"
34 #include "imap_tools.h"
35 #include "imap_fetch.h"
36 #include "imap_search.h"
37 #include "../../genstamp.h"
38 #include "../fulltext/serv_fulltext.h"
42 * imap_do_search() calls imap_do_search_msg() to search an individual
43 * message after it has been fetched from the disk. This function returns
44 * nonzero if there is a match.
46 * msg_in MAY be used to pass a pointer to the message in memory,
47 * if for some reason it's already been loaded. If not, the message will
48 * be loaded only if one or more search criteria require it.
50 int imap_do_search_msg(int seq, struct CtdlMessage *msg_in,
51 int num_items, ConstStr *itemlist, int is_uid) {
60 struct CtdlMessage *msg = NULL;
61 int need_to_free_msg = 0;
68 /* Initially we start at the beginning. */
71 /* Check for the dreaded NOT criterion. */
72 if (!strcasecmp(itemlist[0].Key, "NOT")) {
77 /* Check for the dreaded OR criterion. */
78 if (!strcasecmp(itemlist[0].Key, "OR")) {
83 /* Now look for criteria. */
84 if (!strcasecmp(itemlist[pos].Key, "ALL")) {
89 else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
90 if (Imap->flags[seq-1] & IMAP_ANSWERED) {
96 else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
98 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
102 fieldptr = rfc822_fetch_field(msg->cm_fields[eMessageText], "Bcc");
103 if (fieldptr != NULL) {
104 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
113 else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
115 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
116 need_to_free_msg = 1;
119 if (!CM_IsEmpty(msg, eTimestamp)) {
120 if (imap_datecmp(itemlist[pos+1].Key,
121 atol(msg->cm_fields[eTimestamp])) < 0) {
129 else if (!strcasecmp(itemlist[pos].Key, "BODY")) {
131 /* If fulltext indexing is active, on this server,
132 * all messages have already been qualified.
134 if (CtdlGetConfigInt("c_enable_fulltext")) {
138 /* Otherwise, we have to do a slow search. */
141 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
142 need_to_free_msg = 1;
145 if (bmstrcasestr(msg->cm_fields[eMessageText], itemlist[pos+1].Key)) {
154 else if (!strcasecmp(itemlist[pos].Key, "CC")) {
156 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
157 need_to_free_msg = 1;
160 fieldptr = msg->cm_fields[eCarbonCopY];
161 if (fieldptr != NULL) {
162 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
167 fieldptr = rfc822_fetch_field(msg->cm_fields[eMessageText], "Cc");
168 if (fieldptr != NULL) {
169 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
179 else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
180 if (Imap->flags[seq-1] & IMAP_DELETED) {
186 else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
187 if (Imap->flags[seq-1] & IMAP_DRAFT) {
193 else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
194 if (Imap->flags[seq-1] & IMAP_FLAGGED) {
200 else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
202 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
203 need_to_free_msg = 1;
206 if (bmstrcasestr(msg->cm_fields[eAuthor], itemlist[pos+1].Key)) {
209 if (bmstrcasestr(msg->cm_fields[erFc822Addr], itemlist[pos+1].Key)) {
216 else if (!strcasecmp(itemlist[pos].Key, "HEADER")) {
218 /* We've got to do a slow search for this because the client
219 * might be asking for an RFC822 header field that has not been
220 * converted into a Citadel header field. That requires
221 * examining the message body.
224 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
225 need_to_free_msg = 1;
230 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
231 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
233 fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
234 if (fieldptr != NULL) {
235 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
241 FreeStrBuf(&CC->redirect_buffer);
244 pos += 3; /* Yes, three */
247 else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
248 /* not implemented */
252 else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
254 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
255 need_to_free_msg = 1;
258 if (msg->cm_lengths[eMessageText] > atoi(itemlist[pos+1].Key)) {
265 else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
266 if ( (Imap->flags[seq-1] & IMAP_RECENT) && (!(Imap->flags[seq-1] & IMAP_SEEN))) {
272 else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
273 if (!(Imap->flags[seq-1] & IMAP_RECENT)) {
279 else if (!strcasecmp(itemlist[pos].Key, "ON")) {
281 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
282 need_to_free_msg = 1;
285 if (!CM_IsEmpty(msg, eTimestamp)) {
286 if (imap_datecmp(itemlist[pos+1].Key,
287 atol(msg->cm_fields[eTimestamp])) == 0) {
295 else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
296 if (Imap->flags[seq-1] & IMAP_RECENT) {
302 else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
303 if (Imap->flags[seq-1] & IMAP_SEEN) {
309 else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
311 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
312 need_to_free_msg = 1;
315 if (!CM_IsEmpty(msg, eTimestamp)) {
316 if (imap_datecmp(itemlist[pos+1].Key,
317 atol(msg->cm_fields[eTimestamp])) < 0) {
325 else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
327 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
328 need_to_free_msg = 1;
331 if (!CM_IsEmpty(msg, eTimestamp)) {
332 if (imap_datecmp(itemlist[pos+1].Key,
333 atol(msg->cm_fields[eTimestamp])) == 0) {
341 else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
343 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
344 need_to_free_msg = 1;
347 if (!CM_IsEmpty(msg, eTimestamp)) {
348 if (imap_datecmp(itemlist[pos+1].Key,
349 atol(msg->cm_fields[eTimestamp])) >= 0) {
357 else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
359 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
360 need_to_free_msg = 1;
363 if (!CM_IsEmpty(msg, eTimestamp)) {
364 if (imap_datecmp(itemlist[pos+1].Key,
365 atol(msg->cm_fields[eTimestamp])) >= 0) {
373 else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
375 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
376 need_to_free_msg = 1;
379 if (msg->cm_lengths[eMessageText] < atoi(itemlist[pos+1].Key)) {
386 else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
388 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
389 need_to_free_msg = 1;
392 if (bmstrcasestr(msg->cm_fields[eMsgSubject], itemlist[pos+1].Key)) {
399 else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
401 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
402 need_to_free_msg = 1;
405 for (i='A'; i<='Z'; ++i) {
406 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
414 else if (!strcasecmp(itemlist[pos].Key, "TO")) {
416 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
417 need_to_free_msg = 1;
420 if (bmstrcasestr(msg->cm_fields[eRecipient], itemlist[pos+1].Key)) {
427 /* FIXME this is b0rken. fix it. */
428 else if (imap_is_message_set(itemlist[pos].Key)) {
429 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
435 /* FIXME this is b0rken. fix it. */
436 else if (!strcasecmp(itemlist[pos].Key, "UID")) {
437 if (is_msg_in_sequence_set(itemlist[pos+1].Key, Imap->msgids[seq-1])) {
443 /* Now here come the 'UN' criteria. Why oh why do we have to
444 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
445 * can't there be *one* way to do things? More gratuitous complexity.
448 else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
449 if ((Imap->flags[seq-1] & IMAP_ANSWERED) == 0) {
455 else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
456 if ((Imap->flags[seq-1] & IMAP_DELETED) == 0) {
462 else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
463 if ((Imap->flags[seq-1] & IMAP_DRAFT) == 0) {
469 else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
470 if ((Imap->flags[seq-1] & IMAP_FLAGGED) == 0) {
476 else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
481 else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
482 if ((Imap->flags[seq-1] & IMAP_SEEN) == 0) {
488 /* Remember to negate if we were told to */
493 /* Keep going if there are more criteria! */
494 if (pos < num_items) {
497 match = (match || imap_do_search_msg(seq, msg, num_items - pos, &itemlist[pos], is_uid));
500 match = (match && imap_do_search_msg(seq, msg, num_items - pos, &itemlist[pos], is_uid));
505 if (need_to_free_msg) {
513 * imap_search() calls imap_do_search() to do its actual work, once it's
514 * validated and boiled down the request a bit.
516 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
517 citimap *Imap = IMAP;
523 /* Strip parentheses. We realize that this method will not work
524 * in all cases, but it seems to work with all currently available
525 * client software. Revisit later...
527 for (i=0; i<num_items; ++i) {
528 if (itemlist[i].len && (itemlist[i].Key[0] == '(')) {
529 TokenCutLeft(&Imap->Cmd, &itemlist[i], 1);
531 if (itemlist[i].len && (itemlist[i].Key[itemlist[i].len-1] == ')')) {
532 TokenCutRight(&Imap->Cmd, &itemlist[i], 1);
536 /* If there is a BODY search criterion in the query, use our full
537 * text index to disqualify messages that don't have any chance of
538 * matching. (Only do this if the index is enabled!!)
540 if (CtdlGetConfigInt("c_enable_fulltext")) for (i=0; i<(num_items-1); ++i) {
541 if (!strcasecmp(itemlist[i].Key, "BODY")) {
542 fts = CtdlFullTextSearch(itemlist[i+1].Key);
543 if ((fts) && (array_len(fts) > 0)) {
544 for (j=0; j < Imap->num_msgs; ++j) {
545 if (Imap->flags[j] & IMAP_SELECTED) {
547 for (k=0; k<array_len(fts); ++k) {
549 memcpy(&smsgnum, array_get_element_at(fts, k), sizeof(long));
550 if (Imap->msgids[j] == smsgnum) {
556 Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
560 else { /* no hits on the index; disqualify every message */
561 for (j=0; j < Imap->num_msgs; ++j) {
562 Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
571 /* Now go through the messages and apply all search criteria. */
574 if (Imap->num_msgs > 0)
575 for (i = 0; i < Imap->num_msgs; ++i)
576 if (Imap->flags[i] & IMAP_SELECTED) {
577 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
578 if (num_results != 0) {
582 IAPrintf("%ld", Imap->msgids[i]);
596 * This function is called by the main command loop.
598 void imap_search(int num_parms, ConstStr *Params) {
602 IReply("BAD invalid parameters");
606 for (i = 0; i < IMAP->num_msgs; ++i) {
607 IMAP->flags[i] |= IMAP_SELECTED;
610 imap_do_search(num_parms-2, &Params[2], 0);
611 IReply("OK SEARCH completed");
615 * This function is called by the main command loop.
617 void imap_uidsearch(int num_parms, ConstStr *Params) {
621 IReply("BAD invalid parameters");
625 for (i = 0; i < IMAP->num_msgs; ++i) {
626 IMAP->flags[i] |= IMAP_SELECTED;
629 imap_do_search(num_parms-3, &Params[3], 1);
630 IReply("OK UID SEARCH completed");