4 * Implements IMAP's gratuitously complex SEARCH command.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
39 #include "serv_extensions.h"
46 #include "internet_addressing.h"
47 #include "serv_imap.h"
48 #include "imap_tools.h"
49 #include "imap_fetch.h"
50 #include "imap_search.h"
52 #include "serv_fulltext.h"
56 * imap_do_search() calls imap_do_search_msg() to search an individual
57 * message after it has been fetched from the disk. This function returns
58 * nonzero if there is a match.
60 * supplied_msg MAY be used to pass a pointer to the message in memory,
61 * if for some reason it's already been loaded. If not, the message will
62 * be loaded only if one or more search criteria require it.
64 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
65 int num_items, char **itemlist, int is_uid) {
73 struct CtdlMessage *msg = NULL;
74 int need_to_free_msg = 0;
81 /* Initially we start at the beginning. */
84 /* Check for the dreaded NOT criterion. */
85 if (!strcasecmp(itemlist[0], "NOT")) {
90 /* Check for the dreaded OR criterion. */
91 if (!strcasecmp(itemlist[0], "OR")) {
96 /* Now look for criteria. */
97 if (!strcasecmp(itemlist[pos], "ALL")) {
102 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
103 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
109 else if (!strcasecmp(itemlist[pos], "BCC")) {
111 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
112 need_to_free_msg = 1;
114 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
115 if (fieldptr != NULL) {
116 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
124 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
126 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
127 need_to_free_msg = 1;
129 if (msg->cm_fields['T'] != NULL) {
130 if (imap_datecmp(itemlist[pos+1],
131 atol(msg->cm_fields['T'])) < 0) {
138 else if (!strcasecmp(itemlist[pos], "BODY")) {
140 /* If fulltext indexing is active, on this server,
141 * all messages have already been qualified.
143 if (config.c_enable_fulltext) {
147 /* Otherwise, we have to do a slow search. */
150 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
151 need_to_free_msg = 1;
153 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
161 else if (!strcasecmp(itemlist[pos], "CC")) {
163 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
164 need_to_free_msg = 1;
166 fieldptr = msg->cm_fields['Y'];
167 if (fieldptr != NULL) {
168 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
173 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
174 if (fieldptr != NULL) {
175 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
184 else if (!strcasecmp(itemlist[pos], "DELETED")) {
185 if (IMAP->flags[seq-1] & IMAP_DELETED) {
191 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
192 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
198 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
199 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
205 else if (!strcasecmp(itemlist[pos], "FROM")) {
207 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
208 need_to_free_msg = 1;
210 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
213 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
219 else if (!strcasecmp(itemlist[pos], "HEADER")) {
221 /* We've got to do a slow search for this because the client
222 * might be asking for an RFC822 header field that has not been
223 * converted into a Citadel header field. That requires
224 * examining the message body.
227 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
228 need_to_free_msg = 1;
231 CC->redirect_buffer = malloc(SIZ);
232 CC->redirect_len = 0;
233 CC->redirect_alloc = SIZ;
234 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
236 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
237 if (fieldptr != NULL) {
238 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
244 free(CC->redirect_buffer);
245 CC->redirect_buffer = NULL;
246 CC->redirect_len = 0;
247 CC->redirect_alloc = 0;
249 pos += 3; /* Yes, three */
252 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
257 else if (!strcasecmp(itemlist[pos], "LARGER")) {
259 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
260 need_to_free_msg = 1;
262 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
268 else if (!strcasecmp(itemlist[pos], "NEW")) {
269 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
275 else if (!strcasecmp(itemlist[pos], "OLD")) {
276 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
282 else if (!strcasecmp(itemlist[pos], "ON")) {
284 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
285 need_to_free_msg = 1;
287 if (msg->cm_fields['T'] != NULL) {
288 if (imap_datecmp(itemlist[pos+1],
289 atol(msg->cm_fields['T'])) == 0) {
296 else if (!strcasecmp(itemlist[pos], "RECENT")) {
297 if (IMAP->flags[seq-1] & IMAP_RECENT) {
303 else if (!strcasecmp(itemlist[pos], "SEEN")) {
304 if (IMAP->flags[seq-1] & IMAP_SEEN) {
310 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
312 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
313 need_to_free_msg = 1;
315 if (msg->cm_fields['T'] != NULL) {
316 if (imap_datecmp(itemlist[pos+1],
317 atol(msg->cm_fields['T'])) < 0) {
324 else if (!strcasecmp(itemlist[pos], "SENTON")) {
326 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
327 need_to_free_msg = 1;
329 if (msg->cm_fields['T'] != NULL) {
330 if (imap_datecmp(itemlist[pos+1],
331 atol(msg->cm_fields['T'])) == 0) {
338 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
340 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
341 need_to_free_msg = 1;
343 if (msg->cm_fields['T'] != NULL) {
344 if (imap_datecmp(itemlist[pos+1],
345 atol(msg->cm_fields['T'])) >= 0) {
352 else if (!strcasecmp(itemlist[pos], "SINCE")) {
354 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
355 need_to_free_msg = 1;
357 if (msg->cm_fields['T'] != NULL) {
358 if (imap_datecmp(itemlist[pos+1],
359 atol(msg->cm_fields['T'])) >= 0) {
366 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
368 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
369 need_to_free_msg = 1;
371 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
377 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
379 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
380 need_to_free_msg = 1;
382 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
388 else if (!strcasecmp(itemlist[pos], "TEXT")) {
390 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
391 need_to_free_msg = 1;
393 for (i='A'; i<='Z'; ++i) {
394 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
401 else if (!strcasecmp(itemlist[pos], "TO")) {
403 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
404 need_to_free_msg = 1;
406 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
412 else if (!strcasecmp(itemlist[pos], "UID")) {
413 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
419 /* Now here come the 'UN' criteria. Why oh why do we have to
420 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
421 * can't there be *one* way to do things? Answer: the design of
422 * IMAP suffers from gratuitous complexity.
425 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
426 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
432 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
433 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
439 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
440 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
446 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
447 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
453 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
458 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
459 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
465 /* Remember to negate if we were told to */
470 /* Keep going if there are more criteria! */
471 if (pos < num_items) {
474 match = (match || imap_do_search_msg(seq, msg,
475 num_items - pos, &itemlist[pos], is_uid));
478 match = (match && imap_do_search_msg(seq, msg,
479 num_items - pos, &itemlist[pos], is_uid));
484 if (need_to_free_msg) {
485 CtdlFreeMessage(msg);
492 * imap_search() calls imap_do_search() to do its actual work, once it's
493 * validated and boiled down the request a bit.
495 void imap_do_search(int num_items, char **itemlist, int is_uid) {
497 int fts_num_msgs = 0;
498 long *fts_msgs = NULL;
502 /* If there is a BODY search criterion in the query, use our full
503 * text index to disqualify messages that don't have any chance of
504 * matching. (Only do this if the index is enabled!!)
506 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
507 if (!strcasecmp(itemlist[i], "BODY")) {
508 ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
509 if (fts_num_msgs > 0) {
510 for (j=0; j < IMAP->num_msgs; ++j) {
511 if (IMAP->flags[j] & IMAP_SELECTED) {
513 for (k=0; k<fts_num_msgs; ++k) {
514 if (IMAP->msgids[j] == fts_msgs[k]) {
520 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
524 else { /* no hits on the index; disqualify every message */
525 for (j=0; j < IMAP->num_msgs; ++j) {
526 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
535 /* Now go through the messages and apply all search criteria. */
537 cprintf("* SEARCH ");
538 if (IMAP->num_msgs > 0)
539 for (i = 0; i < IMAP->num_msgs; ++i)
540 if (IMAP->flags[i] & IMAP_SELECTED) {
541 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
542 if (num_results != 0) {
546 cprintf("%ld", IMAP->msgids[i]);
560 * This function is called by the main command loop.
562 void imap_search(int num_parms, char *parms[]) {
566 cprintf("%s BAD invalid parameters\r\n", parms[0]);
570 for (i = 0; i < IMAP->num_msgs; ++i) {
571 IMAP->flags[i] |= IMAP_SELECTED;
574 for (i=1; i<num_parms; ++i) {
575 if (imap_is_message_set(parms[i])) {
576 imap_pick_range(parms[i], 0);
580 imap_do_search(num_parms-2, &parms[2], 0);
581 cprintf("%s OK SEARCH completed\r\n", parms[0]);
585 * This function is called by the main command loop.
587 void imap_uidsearch(int num_parms, char *parms[]) {
591 cprintf("%s BAD invalid parameters\r\n", parms[0]);
595 for (i = 0; i < IMAP->num_msgs; ++i) {
596 IMAP->flags[i] |= IMAP_SELECTED;
599 for (i=1; i<num_parms; ++i) {
600 if (imap_is_message_set(parms[i])) {
601 imap_pick_range(parms[i], 1);
605 imap_do_search(num_parms-3, &parms[3], 1);
606 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);