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>
33 #include <libcitadel.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
45 #include "internet_addressing.h"
46 #include "serv_imap.h"
47 #include "imap_tools.h"
48 #include "imap_fetch.h"
49 #include "imap_search.h"
54 * imap_do_search() calls imap_do_search_msg() to search an individual
55 * message after it has been fetched from the disk. This function returns
56 * nonzero if there is a match.
58 * supplied_msg MAY be used to pass a pointer to the message in memory,
59 * if for some reason it's already been loaded. If not, the message will
60 * be loaded only if one or more search criteria require it.
62 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
63 int num_items, char **itemlist, int is_uid) {
71 struct CtdlMessage *msg = NULL;
72 int need_to_free_msg = 0;
79 /* Initially we start at the beginning. */
82 /* Check for the dreaded NOT criterion. */
83 if (!strcasecmp(itemlist[0], "NOT")) {
88 /* Check for the dreaded OR criterion. */
89 if (!strcasecmp(itemlist[0], "OR")) {
94 /* Now look for criteria. */
95 if (!strcasecmp(itemlist[pos], "ALL")) {
100 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
101 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
107 else if (!strcasecmp(itemlist[pos], "BCC")) {
109 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
110 need_to_free_msg = 1;
113 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
114 if (fieldptr != NULL) {
115 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;
130 if (msg->cm_fields['T'] != NULL) {
131 if (imap_datecmp(itemlist[pos+1],
132 atol(msg->cm_fields['T'])) < 0) {
140 else if (!strcasecmp(itemlist[pos], "BODY")) {
142 /* If fulltext indexing is active, on this server,
143 * all messages have already been qualified.
145 if (config.c_enable_fulltext) {
149 /* Otherwise, we have to do a slow search. */
152 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
153 need_to_free_msg = 1;
156 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
165 else if (!strcasecmp(itemlist[pos], "CC")) {
167 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
168 need_to_free_msg = 1;
171 fieldptr = msg->cm_fields['Y'];
172 if (fieldptr != NULL) {
173 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
178 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
179 if (fieldptr != NULL) {
180 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
190 else if (!strcasecmp(itemlist[pos], "DELETED")) {
191 if (IMAP->flags[seq-1] & IMAP_DELETED) {
197 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
198 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
204 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
205 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
211 else if (!strcasecmp(itemlist[pos], "FROM")) {
213 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
214 need_to_free_msg = 1;
217 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
220 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
227 else if (!strcasecmp(itemlist[pos], "HEADER")) {
229 /* We've got to do a slow search for this because the client
230 * might be asking for an RFC822 header field that has not been
231 * converted into a Citadel header field. That requires
232 * examining the message body.
235 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
236 need_to_free_msg = 1;
241 CC->redirect_buffer = malloc(SIZ);
242 CC->redirect_len = 0;
243 CC->redirect_alloc = SIZ;
244 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
246 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
247 if (fieldptr != NULL) {
248 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
254 free(CC->redirect_buffer);
255 CC->redirect_buffer = NULL;
256 CC->redirect_len = 0;
257 CC->redirect_alloc = 0;
260 pos += 3; /* Yes, three */
263 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
264 /* not implemented */
268 else if (!strcasecmp(itemlist[pos], "LARGER")) {
270 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
271 need_to_free_msg = 1;
274 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
281 else if (!strcasecmp(itemlist[pos], "NEW")) {
282 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
288 else if (!strcasecmp(itemlist[pos], "OLD")) {
289 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
295 else if (!strcasecmp(itemlist[pos], "ON")) {
297 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
298 need_to_free_msg = 1;
301 if (msg->cm_fields['T'] != NULL) {
302 if (imap_datecmp(itemlist[pos+1],
303 atol(msg->cm_fields['T'])) == 0) {
311 else if (!strcasecmp(itemlist[pos], "RECENT")) {
312 if (IMAP->flags[seq-1] & IMAP_RECENT) {
318 else if (!strcasecmp(itemlist[pos], "SEEN")) {
319 if (IMAP->flags[seq-1] & IMAP_SEEN) {
325 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
327 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
328 need_to_free_msg = 1;
331 if (msg->cm_fields['T'] != NULL) {
332 if (imap_datecmp(itemlist[pos+1],
333 atol(msg->cm_fields['T'])) < 0) {
341 else if (!strcasecmp(itemlist[pos], "SENTON")) {
343 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
344 need_to_free_msg = 1;
347 if (msg->cm_fields['T'] != NULL) {
348 if (imap_datecmp(itemlist[pos+1],
349 atol(msg->cm_fields['T'])) == 0) {
357 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
359 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
360 need_to_free_msg = 1;
363 if (msg->cm_fields['T'] != NULL) {
364 if (imap_datecmp(itemlist[pos+1],
365 atol(msg->cm_fields['T'])) >= 0) {
373 else if (!strcasecmp(itemlist[pos], "SINCE")) {
375 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
376 need_to_free_msg = 1;
379 if (msg->cm_fields['T'] != NULL) {
380 if (imap_datecmp(itemlist[pos+1],
381 atol(msg->cm_fields['T'])) >= 0) {
389 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
391 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
392 need_to_free_msg = 1;
395 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
402 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
404 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
405 need_to_free_msg = 1;
408 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
415 else if (!strcasecmp(itemlist[pos], "TEXT")) {
417 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
418 need_to_free_msg = 1;
421 for (i='A'; i<='Z'; ++i) {
422 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
430 else if (!strcasecmp(itemlist[pos], "TO")) {
432 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
433 need_to_free_msg = 1;
436 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
443 /* FIXME this is b0rken. fix it. */
444 else if (imap_is_message_set(itemlist[pos])) {
445 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
451 /* FIXME this is b0rken. fix it. */
452 else if (!strcasecmp(itemlist[pos], "UID")) {
453 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
459 /* Now here come the 'UN' criteria. Why oh why do we have to
460 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
461 * can't there be *one* way to do things? More gratuitous complexity.
464 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
465 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
471 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
472 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
478 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
479 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
485 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
486 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
492 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
497 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
498 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
504 /* Remember to negate if we were told to */
509 /* Keep going if there are more criteria! */
510 if (pos < num_items) {
513 match = (match || imap_do_search_msg(seq, msg,
514 num_items - pos, &itemlist[pos], is_uid));
517 match = (match && imap_do_search_msg(seq, msg,
518 num_items - pos, &itemlist[pos], is_uid));
523 if (need_to_free_msg) {
524 CtdlFreeMessage(msg);
531 * imap_search() calls imap_do_search() to do its actual work, once it's
532 * validated and boiled down the request a bit.
534 void imap_do_search(int num_items, char **itemlist, int is_uid) {
536 int fts_num_msgs = 0;
537 long *fts_msgs = NULL;
541 /* Strip parentheses. We realize that this method will not work
542 * in all cases, but it seems to work with all currently available
543 * client software. Revisit later...
545 for (i=0; i<num_items; ++i) {
546 if (itemlist[i][0] == '(') {
547 strcpy(&itemlist[i][0], &itemlist[i][1]);
549 if (itemlist[i][strlen(itemlist[i])-1] == ')') {
550 itemlist[i][strlen(itemlist[i])-1] = 0;
554 /* If there is a BODY search criterion in the query, use our full
555 * text index to disqualify messages that don't have any chance of
556 * matching. (Only do this if the index is enabled!!)
558 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
559 if (!strcasecmp(itemlist[i], "BODY")) {
560 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1], "fulltext");
561 if (fts_num_msgs > 0) {
562 for (j=0; j < IMAP->num_msgs; ++j) {
563 if (IMAP->flags[j] & IMAP_SELECTED) {
565 for (k=0; k<fts_num_msgs; ++k) {
566 if (IMAP->msgids[j] == fts_msgs[k]) {
572 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
576 else { /* no hits on the index; disqualify every message */
577 for (j=0; j < IMAP->num_msgs; ++j) {
578 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
587 /* Now go through the messages and apply all search criteria. */
589 cprintf("* SEARCH ");
590 if (IMAP->num_msgs > 0)
591 for (i = 0; i < IMAP->num_msgs; ++i)
592 if (IMAP->flags[i] & IMAP_SELECTED) {
593 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
594 if (num_results != 0) {
598 cprintf("%ld", IMAP->msgids[i]);
612 * This function is called by the main command loop.
614 void imap_search(int num_parms, char *parms[]) {
618 cprintf("%s BAD invalid parameters\r\n", parms[0]);
622 for (i = 0; i < IMAP->num_msgs; ++i) {
623 IMAP->flags[i] |= IMAP_SELECTED;
626 imap_do_search(num_parms-2, &parms[2], 0);
627 cprintf("%s OK SEARCH completed\r\n", parms[0]);
631 * This function is called by the main command loop.
633 void imap_uidsearch(int num_parms, char *parms[]) {
637 cprintf("%s BAD invalid parameters\r\n", parms[0]);
641 for (i = 0; i < IMAP->num_msgs; ++i) {
642 IMAP->flags[i] |= IMAP_SELECTED;
645 imap_do_search(num_parms-3, &parms[3], 1);
646 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);