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;
115 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
116 if (fieldptr != NULL) {
117 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
126 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
128 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
129 need_to_free_msg = 1;
132 if (msg->cm_fields['T'] != NULL) {
133 if (imap_datecmp(itemlist[pos+1],
134 atol(msg->cm_fields['T'])) < 0) {
142 else if (!strcasecmp(itemlist[pos], "BODY")) {
144 /* If fulltext indexing is active, on this server,
145 * all messages have already been qualified.
147 if (config.c_enable_fulltext) {
151 /* Otherwise, we have to do a slow search. */
154 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
155 need_to_free_msg = 1;
158 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
167 else if (!strcasecmp(itemlist[pos], "CC")) {
169 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
170 need_to_free_msg = 1;
173 fieldptr = msg->cm_fields['Y'];
174 if (fieldptr != NULL) {
175 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
180 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
181 if (fieldptr != NULL) {
182 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
192 else if (!strcasecmp(itemlist[pos], "DELETED")) {
193 if (IMAP->flags[seq-1] & IMAP_DELETED) {
199 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
200 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
206 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
207 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
213 else if (!strcasecmp(itemlist[pos], "FROM")) {
215 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
216 need_to_free_msg = 1;
219 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
222 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
229 else if (!strcasecmp(itemlist[pos], "HEADER")) {
231 /* We've got to do a slow search for this because the client
232 * might be asking for an RFC822 header field that has not been
233 * converted into a Citadel header field. That requires
234 * examining the message body.
237 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
238 need_to_free_msg = 1;
243 CC->redirect_buffer = malloc(SIZ);
244 CC->redirect_len = 0;
245 CC->redirect_alloc = SIZ;
246 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
248 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
249 if (fieldptr != NULL) {
250 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
256 free(CC->redirect_buffer);
257 CC->redirect_buffer = NULL;
258 CC->redirect_len = 0;
259 CC->redirect_alloc = 0;
262 pos += 3; /* Yes, three */
265 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
266 /* not implemented */
270 else if (!strcasecmp(itemlist[pos], "LARGER")) {
272 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
273 need_to_free_msg = 1;
276 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
283 else if (!strcasecmp(itemlist[pos], "NEW")) {
284 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
290 else if (!strcasecmp(itemlist[pos], "OLD")) {
291 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
297 else if (!strcasecmp(itemlist[pos], "ON")) {
299 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
300 need_to_free_msg = 1;
303 if (msg->cm_fields['T'] != NULL) {
304 if (imap_datecmp(itemlist[pos+1],
305 atol(msg->cm_fields['T'])) == 0) {
313 else if (!strcasecmp(itemlist[pos], "RECENT")) {
314 if (IMAP->flags[seq-1] & IMAP_RECENT) {
320 else if (!strcasecmp(itemlist[pos], "SEEN")) {
321 if (IMAP->flags[seq-1] & IMAP_SEEN) {
327 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
329 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
330 need_to_free_msg = 1;
333 if (msg->cm_fields['T'] != NULL) {
334 if (imap_datecmp(itemlist[pos+1],
335 atol(msg->cm_fields['T'])) < 0) {
343 else if (!strcasecmp(itemlist[pos], "SENTON")) {
345 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
346 need_to_free_msg = 1;
349 if (msg->cm_fields['T'] != NULL) {
350 if (imap_datecmp(itemlist[pos+1],
351 atol(msg->cm_fields['T'])) == 0) {
359 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
361 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
362 need_to_free_msg = 1;
365 if (msg->cm_fields['T'] != NULL) {
366 if (imap_datecmp(itemlist[pos+1],
367 atol(msg->cm_fields['T'])) >= 0) {
375 else if (!strcasecmp(itemlist[pos], "SINCE")) {
377 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
378 need_to_free_msg = 1;
381 if (msg->cm_fields['T'] != NULL) {
382 if (imap_datecmp(itemlist[pos+1],
383 atol(msg->cm_fields['T'])) >= 0) {
391 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
393 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
394 need_to_free_msg = 1;
397 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
404 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
406 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
407 need_to_free_msg = 1;
410 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
417 else if (!strcasecmp(itemlist[pos], "TEXT")) {
419 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
420 need_to_free_msg = 1;
423 for (i='A'; i<='Z'; ++i) {
424 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
432 else if (!strcasecmp(itemlist[pos], "TO")) {
434 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
435 need_to_free_msg = 1;
438 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
445 /* FIXME this is b0rken. fix it. */
446 else if (imap_is_message_set(itemlist[pos])) {
447 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
453 /* FIXME this is b0rken. fix it. */
454 else if (!strcasecmp(itemlist[pos], "UID")) {
455 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
461 /* Now here come the 'UN' criteria. Why oh why do we have to
462 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
463 * can't there be *one* way to do things? More gratuitous complexity.
466 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
467 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
473 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
474 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
480 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
481 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
487 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
488 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
494 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
499 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
500 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
506 /* Remember to negate if we were told to */
511 /* Keep going if there are more criteria! */
512 if (pos < num_items) {
515 match = (match || imap_do_search_msg(seq, msg,
516 num_items - pos, &itemlist[pos], is_uid));
519 match = (match && imap_do_search_msg(seq, msg,
520 num_items - pos, &itemlist[pos], is_uid));
525 if (need_to_free_msg) {
526 CtdlFreeMessage(msg);
533 * imap_search() calls imap_do_search() to do its actual work, once it's
534 * validated and boiled down the request a bit.
536 void imap_do_search(int num_items, char **itemlist, int is_uid) {
538 int fts_num_msgs = 0;
539 long *fts_msgs = NULL;
543 /* If there is a BODY search criterion in the query, use our full
544 * text index to disqualify messages that don't have any chance of
545 * matching. (Only do this if the index is enabled!!)
547 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
548 if (!strcasecmp(itemlist[i], "BODY")) {
549 ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
550 if (fts_num_msgs > 0) {
551 for (j=0; j < IMAP->num_msgs; ++j) {
552 if (IMAP->flags[j] & IMAP_SELECTED) {
554 for (k=0; k<fts_num_msgs; ++k) {
555 if (IMAP->msgids[j] == fts_msgs[k]) {
561 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
565 else { /* no hits on the index; disqualify every message */
566 for (j=0; j < IMAP->num_msgs; ++j) {
567 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
576 /* Now go through the messages and apply all search criteria. */
578 cprintf("* SEARCH ");
579 if (IMAP->num_msgs > 0)
580 for (i = 0; i < IMAP->num_msgs; ++i)
581 if (IMAP->flags[i] & IMAP_SELECTED) {
582 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
583 if (num_results != 0) {
587 cprintf("%ld", IMAP->msgids[i]);
601 * This function is called by the main command loop.
603 void imap_search(int num_parms, char *parms[]) {
607 cprintf("%s BAD invalid parameters\r\n", parms[0]);
611 for (i = 0; i < IMAP->num_msgs; ++i) {
612 IMAP->flags[i] |= IMAP_SELECTED;
615 imap_do_search(num_parms-2, &parms[2], 0);
616 cprintf("%s OK SEARCH completed\r\n", parms[0]);
620 * This function is called by the main command loop.
622 void imap_uidsearch(int num_parms, char *parms[]) {
626 cprintf("%s BAD invalid parameters\r\n", parms[0]);
630 for (i = 0; i < IMAP->num_msgs; ++i) {
631 IMAP->flags[i] |= IMAP_SELECTED;
634 imap_do_search(num_parms-3, &parms[3], 1);
635 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);