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 (bmstrstr(fieldptr, itemlist[pos+1], strncasecmp)) {
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 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
141 need_to_free_msg = 1;
143 if (bmstrstr(msg->cm_fields['M'], itemlist[pos+1], strncasecmp)) {
149 else if (!strcasecmp(itemlist[pos], "CC")) {
151 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
152 need_to_free_msg = 1;
154 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
155 if (fieldptr != NULL) {
156 if (bmstrstr(fieldptr, itemlist[pos+1], strncasecmp)) {
164 else if (!strcasecmp(itemlist[pos], "DELETED")) {
165 if (IMAP->flags[seq-1] & IMAP_DELETED) {
171 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
172 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
178 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
179 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
185 else if (!strcasecmp(itemlist[pos], "FROM")) {
187 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
188 need_to_free_msg = 1;
190 if (bmstrstr(msg->cm_fields['A'], itemlist[pos+1], strncasecmp)) {
196 else if (!strcasecmp(itemlist[pos], "HEADER")) {
198 pos += 3; /* Yes, three */
201 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
206 else if (!strcasecmp(itemlist[pos], "LARGER")) {
208 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
209 need_to_free_msg = 1;
211 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
217 else if (!strcasecmp(itemlist[pos], "NEW")) {
218 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
224 else if (!strcasecmp(itemlist[pos], "OLD")) {
225 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
231 else if (!strcasecmp(itemlist[pos], "ON")) {
233 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
234 need_to_free_msg = 1;
236 if (msg->cm_fields['T'] != NULL) {
237 if (imap_datecmp(itemlist[pos+1],
238 atol(msg->cm_fields['T'])) == 0) {
245 else if (!strcasecmp(itemlist[pos], "RECENT")) {
246 if (IMAP->flags[seq-1] & IMAP_RECENT) {
252 else if (!strcasecmp(itemlist[pos], "SEEN")) {
253 if (IMAP->flags[seq-1] & IMAP_SEEN) {
259 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
261 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
262 need_to_free_msg = 1;
264 if (msg->cm_fields['T'] != NULL) {
265 if (imap_datecmp(itemlist[pos+1],
266 atol(msg->cm_fields['T'])) < 0) {
273 else if (!strcasecmp(itemlist[pos], "SENTON")) {
275 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
276 need_to_free_msg = 1;
278 if (msg->cm_fields['T'] != NULL) {
279 if (imap_datecmp(itemlist[pos+1],
280 atol(msg->cm_fields['T'])) == 0) {
287 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
289 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
290 need_to_free_msg = 1;
292 if (msg->cm_fields['T'] != NULL) {
293 if (imap_datecmp(itemlist[pos+1],
294 atol(msg->cm_fields['T'])) >= 0) {
301 else if (!strcasecmp(itemlist[pos], "SINCE")) {
303 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
304 need_to_free_msg = 1;
306 if (msg->cm_fields['T'] != NULL) {
307 if (imap_datecmp(itemlist[pos+1],
308 atol(msg->cm_fields['T'])) >= 0) {
315 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
317 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
318 need_to_free_msg = 1;
320 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
326 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
328 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
329 need_to_free_msg = 1;
331 if (bmstrstr(msg->cm_fields['U'], itemlist[pos+1], strncasecmp)) {
337 else if (!strcasecmp(itemlist[pos], "TEXT")) {
339 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
340 need_to_free_msg = 1;
342 for (i='A'; i<='Z'; ++i) {
343 if (bmstrstr(msg->cm_fields[i], itemlist[pos+1], strncasecmp)) {
350 else if (!strcasecmp(itemlist[pos], "TO")) {
352 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
353 need_to_free_msg = 1;
355 if (bmstrstr(msg->cm_fields['R'], itemlist[pos+1], strncasecmp)) {
361 else if (!strcasecmp(itemlist[pos], "UID")) {
362 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
368 /* Now here come the 'UN' criteria. Why oh why do we have to
369 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
370 * can't there be *one* way to do things? Answer: the design of
371 * IMAP suffers from gratuitous complexity.
374 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
375 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
381 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
382 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
388 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
389 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
395 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
396 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
402 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
407 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
408 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
414 /* Remember to negate if we were told to */
419 /* Keep going if there are more criteria! */
420 if (pos < num_items) {
423 match = (match || imap_do_search_msg(seq, msg,
424 num_items - pos, &itemlist[pos], is_uid));
427 match = (match && imap_do_search_msg(seq, msg,
428 num_items - pos, &itemlist[pos], is_uid));
433 if (need_to_free_msg) {
434 CtdlFreeMessage(msg);
441 * imap_search() calls imap_do_search() to do its actual work, once it's
442 * validated and boiled down the request a bit.
444 void imap_do_search(int num_items, char **itemlist, int is_uid) {
446 int fts_num_msgs = 0;
447 long *fts_msgs = NULL;
450 /* If there is a BODY search criterion in the query, use our full
451 * text index to disqualify messages that don't have any chance of
452 * matching. (Only do this if the index is enabled!!)
454 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
455 if (!strcasecmp(itemlist[i], "BODY")) {
456 ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
457 if (fts_num_msgs > 0) {
458 for (j=0; j < IMAP->num_msgs; ++j) {
459 if (IMAP->flags[j] & IMAP_SELECTED) {
461 for (k=0; k<fts_num_msgs; ++k) {
462 if (IMAP->msgids[j] == fts_msgs[k]) {
468 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
472 else { /* no hits on the index; disqualify every message */
473 for (j=0; j < IMAP->num_msgs; ++j) {
474 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
483 /* Now go through the messages and apply all search criteria. */
485 cprintf("* SEARCH ");
486 if (IMAP->num_msgs > 0)
487 for (i = 0; i < IMAP->num_msgs; ++i)
488 if (IMAP->flags[i] & IMAP_SELECTED) {
489 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
491 cprintf("%ld ", IMAP->msgids[i]);
504 * This function is called by the main command loop.
506 void imap_search(int num_parms, char *parms[]) {
510 cprintf("%s BAD invalid parameters\r\n", parms[0]);
514 for (i = 0; i < IMAP->num_msgs; ++i) {
515 IMAP->flags[i] |= IMAP_SELECTED;
518 for (i=1; i<num_parms; ++i) {
519 if (imap_is_message_set(parms[i])) {
520 imap_pick_range(parms[i], 0);
524 imap_do_search(num_parms-2, &parms[2], 0);
525 cprintf("%s OK SEARCH completed\r\n", parms[0]);
529 * This function is called by the main command loop.
531 void imap_uidsearch(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], 1);
549 imap_do_search(num_parms-3, &parms[3], 1);
550 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);