4 * Implements IMAP's gratuitously complex SEARCH command.
7 * Copyright (c) 2001-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
69 * imap_do_search() calls imap_do_search_msg() to search an individual
70 * message after it has been fetched from the disk. This function returns
71 * nonzero if there is a match.
73 * supplied_msg MAY be used to pass a pointer to the message in memory,
74 * if for some reason it's already been loaded. If not, the message will
75 * be loaded only if one or more search criteria require it.
77 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
78 int num_items, char **itemlist, int is_uid) {
86 struct CtdlMessage *msg = NULL;
87 int need_to_free_msg = 0;
94 /* Initially we start at the beginning. */
97 /* Check for the dreaded NOT criterion. */
98 if (!strcasecmp(itemlist[0], "NOT")) {
103 /* Check for the dreaded OR criterion. */
104 if (!strcasecmp(itemlist[0], "OR")) {
109 /* Now look for criteria. */
110 if (!strcasecmp(itemlist[pos], "ALL")) {
115 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
116 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
122 else if (!strcasecmp(itemlist[pos], "BCC")) {
124 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
125 need_to_free_msg = 1;
128 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
129 if (fieldptr != NULL) {
130 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
139 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
141 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
142 need_to_free_msg = 1;
145 if (msg->cm_fields['T'] != NULL) {
146 if (imap_datecmp(itemlist[pos+1],
147 atol(msg->cm_fields['T'])) < 0) {
155 else if (!strcasecmp(itemlist[pos], "BODY")) {
157 /* If fulltext indexing is active, on this server,
158 * all messages have already been qualified.
160 if (config.c_enable_fulltext) {
164 /* Otherwise, we have to do a slow search. */
167 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
168 need_to_free_msg = 1;
171 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
180 else if (!strcasecmp(itemlist[pos], "CC")) {
182 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
183 need_to_free_msg = 1;
186 fieldptr = msg->cm_fields['Y'];
187 if (fieldptr != NULL) {
188 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
193 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
194 if (fieldptr != NULL) {
195 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
205 else if (!strcasecmp(itemlist[pos], "DELETED")) {
206 if (IMAP->flags[seq-1] & IMAP_DELETED) {
212 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
213 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
219 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
220 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
226 else if (!strcasecmp(itemlist[pos], "FROM")) {
228 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
229 need_to_free_msg = 1;
232 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
235 if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
242 else if (!strcasecmp(itemlist[pos], "HEADER")) {
244 /* We've got to do a slow search for this because the client
245 * might be asking for an RFC822 header field that has not been
246 * converted into a Citadel header field. That requires
247 * examining the message body.
250 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
251 need_to_free_msg = 1;
256 CC->redirect_buffer = malloc(SIZ);
257 CC->redirect_len = 0;
258 CC->redirect_alloc = SIZ;
259 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
261 fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
262 if (fieldptr != NULL) {
263 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
269 free(CC->redirect_buffer);
270 CC->redirect_buffer = NULL;
271 CC->redirect_len = 0;
272 CC->redirect_alloc = 0;
275 pos += 3; /* Yes, three */
278 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
279 /* not implemented */
283 else if (!strcasecmp(itemlist[pos], "LARGER")) {
285 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
286 need_to_free_msg = 1;
289 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
296 else if (!strcasecmp(itemlist[pos], "NEW")) {
297 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
303 else if (!strcasecmp(itemlist[pos], "OLD")) {
304 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
310 else if (!strcasecmp(itemlist[pos], "ON")) {
312 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
313 need_to_free_msg = 1;
316 if (msg->cm_fields['T'] != NULL) {
317 if (imap_datecmp(itemlist[pos+1],
318 atol(msg->cm_fields['T'])) == 0) {
326 else if (!strcasecmp(itemlist[pos], "RECENT")) {
327 if (IMAP->flags[seq-1] & IMAP_RECENT) {
333 else if (!strcasecmp(itemlist[pos], "SEEN")) {
334 if (IMAP->flags[seq-1] & IMAP_SEEN) {
340 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
342 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
343 need_to_free_msg = 1;
346 if (msg->cm_fields['T'] != NULL) {
347 if (imap_datecmp(itemlist[pos+1],
348 atol(msg->cm_fields['T'])) < 0) {
356 else if (!strcasecmp(itemlist[pos], "SENTON")) {
358 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
359 need_to_free_msg = 1;
362 if (msg->cm_fields['T'] != NULL) {
363 if (imap_datecmp(itemlist[pos+1],
364 atol(msg->cm_fields['T'])) == 0) {
372 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
374 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
375 need_to_free_msg = 1;
378 if (msg->cm_fields['T'] != NULL) {
379 if (imap_datecmp(itemlist[pos+1],
380 atol(msg->cm_fields['T'])) >= 0) {
388 else if (!strcasecmp(itemlist[pos], "SINCE")) {
390 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
391 need_to_free_msg = 1;
394 if (msg->cm_fields['T'] != NULL) {
395 if (imap_datecmp(itemlist[pos+1],
396 atol(msg->cm_fields['T'])) >= 0) {
404 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
406 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
407 need_to_free_msg = 1;
410 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
417 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
419 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
420 need_to_free_msg = 1;
423 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
430 else if (!strcasecmp(itemlist[pos], "TEXT")) {
432 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
433 need_to_free_msg = 1;
436 for (i='A'; i<='Z'; ++i) {
437 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
445 else if (!strcasecmp(itemlist[pos], "TO")) {
447 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
448 need_to_free_msg = 1;
451 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
458 /* FIXME this is b0rken. fix it. */
459 else if (imap_is_message_set(itemlist[pos])) {
460 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
466 /* FIXME this is b0rken. fix it. */
467 else if (!strcasecmp(itemlist[pos], "UID")) {
468 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
474 /* Now here come the 'UN' criteria. Why oh why do we have to
475 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
476 * can't there be *one* way to do things? More gratuitous complexity.
479 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
480 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
486 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
487 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
493 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
494 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
500 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
501 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
507 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
512 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
513 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
519 /* Remember to negate if we were told to */
524 /* Keep going if there are more criteria! */
525 if (pos < num_items) {
528 match = (match || imap_do_search_msg(seq, msg,
529 num_items - pos, &itemlist[pos], is_uid));
532 match = (match && imap_do_search_msg(seq, msg,
533 num_items - pos, &itemlist[pos], is_uid));
538 if (need_to_free_msg) {
539 CtdlFreeMessage(msg);
546 * imap_search() calls imap_do_search() to do its actual work, once it's
547 * validated and boiled down the request a bit.
549 void imap_do_search(int num_items, char **itemlist, int is_uid) {
551 int fts_num_msgs = 0;
552 long *fts_msgs = NULL;
556 /* Strip parentheses. We realize that this method will not work
557 * in all cases, but it seems to work with all currently available
558 * client software. Revisit later...
560 for (i=0; i<num_items; ++i) {
561 if (itemlist[i][0] == '(') {
562 strcpy(&itemlist[i][0], &itemlist[i][1]);
564 if (itemlist[i][strlen(itemlist[i])-1] == ')') {
565 itemlist[i][strlen(itemlist[i])-1] = 0;
569 /* If there is a BODY search criterion in the query, use our full
570 * text index to disqualify messages that don't have any chance of
571 * matching. (Only do this if the index is enabled!!)
573 if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
574 if (!strcasecmp(itemlist[i], "BODY")) {
575 CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1], "fulltext");
576 if (fts_num_msgs > 0) {
577 for (j=0; j < IMAP->num_msgs; ++j) {
578 if (IMAP->flags[j] & IMAP_SELECTED) {
580 for (k=0; k<fts_num_msgs; ++k) {
581 if (IMAP->msgids[j] == fts_msgs[k]) {
587 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
591 else { /* no hits on the index; disqualify every message */
592 for (j=0; j < IMAP->num_msgs; ++j) {
593 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
602 /* Now go through the messages and apply all search criteria. */
604 cprintf("* SEARCH ");
605 if (IMAP->num_msgs > 0)
606 for (i = 0; i < IMAP->num_msgs; ++i)
607 if (IMAP->flags[i] & IMAP_SELECTED) {
608 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
609 if (num_results != 0) {
613 cprintf("%ld", IMAP->msgids[i]);
627 * This function is called by the main command loop.
629 void imap_search(int num_parms, char *parms[]) {
633 cprintf("%s BAD invalid parameters\r\n", parms[0]);
637 for (i = 0; i < IMAP->num_msgs; ++i) {
638 IMAP->flags[i] |= IMAP_SELECTED;
641 imap_do_search(num_parms-2, &parms[2], 0);
642 cprintf("%s OK SEARCH completed\r\n", parms[0]);
646 * This function is called by the main command loop.
648 void imap_uidsearch(int num_parms, char *parms[]) {
652 cprintf("%s BAD invalid parameters\r\n", parms[0]);
656 for (i = 0; i < IMAP->num_msgs; ++i) {
657 IMAP->flags[i] |= IMAP_SELECTED;
660 imap_do_search(num_parms-3, &parms[3], 1);
661 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);