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"
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"
51 #include "serv_fulltext.h"
55 * imap_do_search() calls imap_do_search_msg() to search an individual
56 * message after it has been fetched from the disk. This function returns
57 * nonzero if there is a match.
59 * supplied_msg MAY be used to pass a pointer to the message in memory,
60 * if for some reason it's already been loaded. If not, the message will
61 * be loaded only if one or more search criteria require it.
63 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
64 int num_items, char **itemlist, int is_uid) {
72 struct CtdlMessage *msg = NULL;
73 int need_to_free_msg = 0;
80 /* Initially we start at the beginning. */
83 /* Check for the dreaded NOT criterion. */
84 if (!strcasecmp(itemlist[0], "NOT")) {
89 /* Check for the dreaded OR criterion. */
90 if (!strcasecmp(itemlist[0], "OR")) {
95 /* Now look for criteria. */
96 if (!strcasecmp(itemlist[pos], "ALL")) {
101 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
102 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
108 else if (!strcasecmp(itemlist[pos], "BCC")) {
110 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
111 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])) {
125 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
127 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
128 need_to_free_msg = 1;
131 if (msg->cm_fields['T'] != NULL) {
132 if (imap_datecmp(itemlist[pos+1],
133 atol(msg->cm_fields['T'])) < 0) {
141 else if (!strcasecmp(itemlist[pos], "BODY")) {
143 /* If fulltext indexing is active, on this server,
144 * all messages have already been qualified.
146 if (config.c_enable_fulltext) {
150 /* Otherwise, we have to do a slow search. */
153 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
154 need_to_free_msg = 1;
157 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
166 else if (!strcasecmp(itemlist[pos], "CC")) {
168 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
169 need_to_free_msg = 1;
172 fieldptr = msg->cm_fields['Y'];
173 if (fieldptr != NULL) {
174 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
179 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
180 if (fieldptr != NULL) {
181 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
191 else if (!strcasecmp(itemlist[pos], "DELETED")) {
192 if (IMAP->flags[seq-1] & IMAP_DELETED) {
198 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
199 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
205 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
206 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
212 else if (!strcasecmp(itemlist[pos], "FROM")) {
214 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
215 need_to_free_msg = 1;
218 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
221 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
228 else if (!strcasecmp(itemlist[pos], "HEADER")) {
230 /* We've got to do a slow search for this because the client
231 * might be asking for an RFC822 header field that has not been
232 * converted into a Citadel header field. That requires
233 * examining the message body.
236 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
237 need_to_free_msg = 1;
242 CC->redirect_buffer = malloc(SIZ);
243 CC->redirect_len = 0;
244 CC->redirect_alloc = SIZ;
245 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
247 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
248 if (fieldptr != NULL) {
249 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
255 free(CC->redirect_buffer);
256 CC->redirect_buffer = NULL;
257 CC->redirect_len = 0;
258 CC->redirect_alloc = 0;
261 pos += 3; /* Yes, three */
264 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
265 /* not implemented */
269 else if (!strcasecmp(itemlist[pos], "LARGER")) {
271 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
272 need_to_free_msg = 1;
275 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
282 else if (!strcasecmp(itemlist[pos], "NEW")) {
283 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
289 else if (!strcasecmp(itemlist[pos], "OLD")) {
290 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
296 else if (!strcasecmp(itemlist[pos], "ON")) {
298 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
299 need_to_free_msg = 1;
302 if (msg->cm_fields['T'] != NULL) {
303 if (imap_datecmp(itemlist[pos+1],
304 atol(msg->cm_fields['T'])) == 0) {
312 else if (!strcasecmp(itemlist[pos], "RECENT")) {
313 if (IMAP->flags[seq-1] & IMAP_RECENT) {
319 else if (!strcasecmp(itemlist[pos], "SEEN")) {
320 if (IMAP->flags[seq-1] & IMAP_SEEN) {
326 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
328 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
329 need_to_free_msg = 1;
332 if (msg->cm_fields['T'] != NULL) {
333 if (imap_datecmp(itemlist[pos+1],
334 atol(msg->cm_fields['T'])) < 0) {
342 else if (!strcasecmp(itemlist[pos], "SENTON")) {
344 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
345 need_to_free_msg = 1;
348 if (msg->cm_fields['T'] != NULL) {
349 if (imap_datecmp(itemlist[pos+1],
350 atol(msg->cm_fields['T'])) == 0) {
358 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
360 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
361 need_to_free_msg = 1;
364 if (msg->cm_fields['T'] != NULL) {
365 if (imap_datecmp(itemlist[pos+1],
366 atol(msg->cm_fields['T'])) >= 0) {
374 else if (!strcasecmp(itemlist[pos], "SINCE")) {
376 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
377 need_to_free_msg = 1;
380 if (msg->cm_fields['T'] != NULL) {
381 if (imap_datecmp(itemlist[pos+1],
382 atol(msg->cm_fields['T'])) >= 0) {
390 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
392 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
393 need_to_free_msg = 1;
396 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
403 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
405 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
406 need_to_free_msg = 1;
409 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
416 else if (!strcasecmp(itemlist[pos], "TEXT")) {
418 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
419 need_to_free_msg = 1;
422 for (i='A'; i<='Z'; ++i) {
423 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
431 else if (!strcasecmp(itemlist[pos], "TO")) {
433 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
434 need_to_free_msg = 1;
437 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
444 /* FIXME this is b0rken. fix it. */
445 else if (imap_is_message_set(itemlist[pos])) {
446 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
452 /* FIXME this is b0rken. fix it. */
453 else if (!strcasecmp(itemlist[pos], "UID")) {
454 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
460 /* Now here come the 'UN' criteria. Why oh why do we have to
461 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
462 * can't there be *one* way to do things? More gratuitous complexity.
465 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
466 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
472 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
473 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
479 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
480 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
486 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
487 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
493 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
498 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
499 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
505 /* Remember to negate if we were told to */
510 /* Keep going if there are more criteria! */
511 if (pos < num_items) {
514 match = (match || imap_do_search_msg(seq, msg,
515 num_items - pos, &itemlist[pos], is_uid));
518 match = (match && imap_do_search_msg(seq, msg,
519 num_items - pos, &itemlist[pos], is_uid));
524 if (need_to_free_msg) {
525 CtdlFreeMessage(msg);
532 * imap_search() calls imap_do_search() to do its actual work, once it's
533 * validated and boiled down the request a bit.
535 void imap_do_search(int num_items, char **itemlist, int is_uid) {
537 int fts_num_msgs = 0;
538 long *fts_msgs = NULL;
542 /* If there is a BODY search criterion in the query, use our full
543 * text index to disqualify messages that don't have any chance of
544 * matching. (Only do this if the index is enabled!!)
546 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
547 if (!strcasecmp(itemlist[i], "BODY")) {
548 ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
549 if (fts_num_msgs > 0) {
550 for (j=0; j < IMAP->num_msgs; ++j) {
551 if (IMAP->flags[j] & IMAP_SELECTED) {
553 for (k=0; k<fts_num_msgs; ++k) {
554 if (IMAP->msgids[j] == fts_msgs[k]) {
560 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
564 else { /* no hits on the index; disqualify every message */
565 for (j=0; j < IMAP->num_msgs; ++j) {
566 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
575 /* Now go through the messages and apply all search criteria. */
577 cprintf("* SEARCH ");
578 if (IMAP->num_msgs > 0)
579 for (i = 0; i < IMAP->num_msgs; ++i)
580 if (IMAP->flags[i] & IMAP_SELECTED) {
581 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
582 if (num_results != 0) {
586 cprintf("%ld", IMAP->msgids[i]);
600 * This function is called by the main command loop.
602 void imap_search(int num_parms, char *parms[]) {
606 cprintf("%s BAD invalid parameters\r\n", parms[0]);
610 for (i = 0; i < IMAP->num_msgs; ++i) {
611 IMAP->flags[i] |= IMAP_SELECTED;
614 imap_do_search(num_parms-2, &parms[2], 0);
615 cprintf("%s OK SEARCH completed\r\n", parms[0]);
619 * This function is called by the main command loop.
621 void imap_uidsearch(int num_parms, char *parms[]) {
625 cprintf("%s BAD invalid parameters\r\n", parms[0]);
629 for (i = 0; i < IMAP->num_msgs; ++i) {
630 IMAP->flags[i] |= IMAP_SELECTED;
633 imap_do_search(num_parms-3, &parms[3], 1);
634 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);