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])) {
216 else if (!strcasecmp(itemlist[pos], "HEADER")) {
218 pos += 3; /* Yes, three */
221 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
226 else if (!strcasecmp(itemlist[pos], "LARGER")) {
228 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
229 need_to_free_msg = 1;
231 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
237 else if (!strcasecmp(itemlist[pos], "NEW")) {
238 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
244 else if (!strcasecmp(itemlist[pos], "OLD")) {
245 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
251 else if (!strcasecmp(itemlist[pos], "ON")) {
253 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
254 need_to_free_msg = 1;
256 if (msg->cm_fields['T'] != NULL) {
257 if (imap_datecmp(itemlist[pos+1],
258 atol(msg->cm_fields['T'])) == 0) {
265 else if (!strcasecmp(itemlist[pos], "RECENT")) {
266 if (IMAP->flags[seq-1] & IMAP_RECENT) {
272 else if (!strcasecmp(itemlist[pos], "SEEN")) {
273 if (IMAP->flags[seq-1] & IMAP_SEEN) {
279 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
281 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
282 need_to_free_msg = 1;
284 if (msg->cm_fields['T'] != NULL) {
285 if (imap_datecmp(itemlist[pos+1],
286 atol(msg->cm_fields['T'])) < 0) {
293 else if (!strcasecmp(itemlist[pos], "SENTON")) {
295 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
296 need_to_free_msg = 1;
298 if (msg->cm_fields['T'] != NULL) {
299 if (imap_datecmp(itemlist[pos+1],
300 atol(msg->cm_fields['T'])) == 0) {
307 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
309 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
310 need_to_free_msg = 1;
312 if (msg->cm_fields['T'] != NULL) {
313 if (imap_datecmp(itemlist[pos+1],
314 atol(msg->cm_fields['T'])) >= 0) {
321 else if (!strcasecmp(itemlist[pos], "SINCE")) {
323 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
324 need_to_free_msg = 1;
326 if (msg->cm_fields['T'] != NULL) {
327 if (imap_datecmp(itemlist[pos+1],
328 atol(msg->cm_fields['T'])) >= 0) {
335 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
337 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
338 need_to_free_msg = 1;
340 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
346 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
348 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
349 need_to_free_msg = 1;
351 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
357 else if (!strcasecmp(itemlist[pos], "TEXT")) {
359 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
360 need_to_free_msg = 1;
362 for (i='A'; i<='Z'; ++i) {
363 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
370 else if (!strcasecmp(itemlist[pos], "TO")) {
372 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
373 need_to_free_msg = 1;
375 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
381 else if (!strcasecmp(itemlist[pos], "UID")) {
382 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
388 /* Now here come the 'UN' criteria. Why oh why do we have to
389 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
390 * can't there be *one* way to do things? Answer: the design of
391 * IMAP suffers from gratuitous complexity.
394 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
395 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
401 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
402 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
408 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
409 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
415 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
416 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
422 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
427 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
428 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
434 /* Remember to negate if we were told to */
439 /* Keep going if there are more criteria! */
440 if (pos < num_items) {
443 match = (match || imap_do_search_msg(seq, msg,
444 num_items - pos, &itemlist[pos], is_uid));
447 match = (match && imap_do_search_msg(seq, msg,
448 num_items - pos, &itemlist[pos], is_uid));
453 if (need_to_free_msg) {
454 CtdlFreeMessage(msg);
461 * imap_search() calls imap_do_search() to do its actual work, once it's
462 * validated and boiled down the request a bit.
464 void imap_do_search(int num_items, char **itemlist, int is_uid) {
466 int fts_num_msgs = 0;
467 long *fts_msgs = NULL;
471 /* If there is a BODY search criterion in the query, use our full
472 * text index to disqualify messages that don't have any chance of
473 * matching. (Only do this if the index is enabled!!)
475 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
476 if (!strcasecmp(itemlist[i], "BODY")) {
477 ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
478 if (fts_num_msgs > 0) {
479 for (j=0; j < IMAP->num_msgs; ++j) {
480 if (IMAP->flags[j] & IMAP_SELECTED) {
482 for (k=0; k<fts_num_msgs; ++k) {
483 if (IMAP->msgids[j] == fts_msgs[k]) {
489 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
493 else { /* no hits on the index; disqualify every message */
494 for (j=0; j < IMAP->num_msgs; ++j) {
495 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
504 /* Now go through the messages and apply all search criteria. */
506 cprintf("* SEARCH ");
507 if (IMAP->num_msgs > 0)
508 for (i = 0; i < IMAP->num_msgs; ++i)
509 if (IMAP->flags[i] & IMAP_SELECTED) {
510 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
511 if (num_results != 0) {
515 cprintf("%ld", IMAP->msgids[i]);
529 * This function is called by the main command loop.
531 void imap_search(int num_parms, char *parms[]) {
535 cprintf("%s BAD invalid parameters\r\n", parms[0]);
539 for (i = 0; i < IMAP->num_msgs; ++i) {
540 IMAP->flags[i] |= IMAP_SELECTED;
543 for (i=1; i<num_parms; ++i) {
544 if (imap_is_message_set(parms[i])) {
545 imap_pick_range(parms[i], 0);
549 imap_do_search(num_parms-2, &parms[2], 0);
550 cprintf("%s OK SEARCH completed\r\n", parms[0]);
554 * This function is called by the main command loop.
556 void imap_uidsearch(int num_parms, char *parms[]) {
560 cprintf("%s BAD invalid parameters\r\n", parms[0]);
564 for (i = 0; i < IMAP->num_msgs; ++i) {
565 IMAP->flags[i] |= IMAP_SELECTED;
568 for (i=1; i<num_parms; ++i) {
569 if (imap_is_message_set(parms[i])) {
570 imap_pick_range(parms[i], 1);
574 imap_do_search(num_parms-3, &parms[3], 1);
575 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);