]> code.citadel.org Git - citadel.git/blobdiff - citadel/imap_search.c
* Full text indexer is now switchable on/off
[citadel.git] / citadel / imap_search.c
index dce7e605dad2a1395615f2bdd3c6e5a351a13be9..a179a759e283e4e92fbed70b4446f95502224734 100644 (file)
@@ -1,14 +1,10 @@
 /*
  * $Id$
  *
- * Implements the SEARCH command in IMAP.
- * This command is way too convoluted.  Marc Crispin is a fscking idiot.
- *
- * NOTE: this is a partial implementation.  It is NOT FINISHED.
+ * Implements IMAP's gratuitously complex SEARCH command.
  *
  */
 
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include "imap_fetch.h"
 #include "imap_search.h"
 #include "genstamp.h"
-
+#include "serv_fulltext.h"
 
 
 /*
  * imap_do_search() calls imap_do_search_msg() to search an individual
  * message after it has been fetched from the disk.  This function returns
  * nonzero if there is a match.
+ *
+ * supplied_msg MAY be used to pass a pointer to the message in memory,
+ * if for some reason it's already been loaded.  If not, the message will
+ * be loaded only if one or more search criteria require it.
  */
-int imap_do_search_msg(int seq, struct CtdlMessage *msg,
+int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        int num_items, char **itemlist, int is_uid) {
 
        int match = 0;
@@ -70,8 +70,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        int pos = 0;
        int i;
        char *fieldptr;
+       struct CtdlMessage *msg = NULL;
+       int need_to_free_msg = 0;
 
-       if (num_items == 0) return(0);
+       if (num_items == 0) {
+               return(0);
+       }
+       msg = supplied_msg;
 
        /* Initially we start at the beginning. */
        pos = 0;
@@ -102,6 +107,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "BCC")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
                if (fieldptr != NULL) {
                        if (bmstrstr(fieldptr, itemlist[pos+1], strncasecmp)) {
@@ -113,6 +122,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "BEFORE")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) < 0) {
@@ -123,6 +136,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "BODY")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (bmstrstr(msg->cm_fields['M'], itemlist[pos+1], strncasecmp)) {
                        match = 1;
                }
@@ -130,6 +147,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "CC")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
                if (fieldptr != NULL) {
                        if (bmstrstr(fieldptr, itemlist[pos+1], strncasecmp)) {
@@ -162,6 +183,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "FROM")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (bmstrstr(msg->cm_fields['A'], itemlist[pos+1], strncasecmp)) {
                        match = 1;
                }
@@ -179,6 +204,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "LARGER")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
                        match = 1;
                }
@@ -186,16 +215,24 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "NEW")) {
-               /* FIXME */
+               if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
+                       match = 1;
+               }
                ++pos;
        }
 
        else if (!strcasecmp(itemlist[pos], "OLD")) {
-               /* FIXME */
+               if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
+                       match = 1;
+               }
                ++pos;
        }
 
        else if (!strcasecmp(itemlist[pos], "ON")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) == 0) {
@@ -206,7 +243,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "RECENT")) {
-               /* FIXME */
+               if (IMAP->flags[seq-1] & IMAP_RECENT) {
+                       match = 1;
+               }
                ++pos;
        }
 
@@ -218,6 +257,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) < 0) {
@@ -228,6 +271,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SENTON")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) == 0) {
@@ -238,6 +285,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) >= 0) {
@@ -248,6 +299,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SINCE")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (msg->cm_fields['T'] != NULL) {
                        if (imap_datecmp(itemlist[pos+1],
                                        atol(msg->cm_fields['T'])) >= 0) {
@@ -258,6 +313,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SMALLER")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
                        match = 1;
                }
@@ -265,6 +324,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (bmstrstr(msg->cm_fields['U'], itemlist[pos+1], strncasecmp)) {
                        match = 1;
                }
@@ -272,6 +335,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "TEXT")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                for (i='A'; i<='Z'; ++i) {
                        if (bmstrstr(msg->cm_fields[i], itemlist[pos+1], strncasecmp)) {
                                match = 1;
@@ -281,6 +348,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "TO")) {
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
                if (bmstrstr(msg->cm_fields['R'], itemlist[pos+1], strncasecmp)) {
                        match = 1;
                }
@@ -288,7 +359,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
        }
 
        else if (!strcasecmp(itemlist[pos], "UID")) {
-               if (is_msg_in_mset(itemlist[pos+1], IMAP->msgids[seq-1])) {
+               if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
                        match = 1;
                }
                pos += 2;
@@ -296,8 +367,8 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
 
        /* Now here come the 'UN' criteria.  Why oh why do we have to
         * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
-        * can't there be *one* way to do things?  Answer: because Mark
-        * Crispin is an idiot.
+        * can't there be *one* way to do things?  Answer: the design of
+        * IMAP suffers from gratuitous complexity.
         */
 
        else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
@@ -359,6 +430,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
 
        }
 
+       if (need_to_free_msg) {
+               CtdlFreeMessage(msg);
+       }
        return(match);
 }
 
@@ -368,31 +442,61 @@ int imap_do_search_msg(int seq, struct CtdlMessage *msg,
  * validated and boiled down the request a bit.
  */
 void imap_do_search(int num_items, char **itemlist, int is_uid) {
-       int i;
-       struct CtdlMessage *msg;
+       int i, j, k;
+       int fts_num_msgs = 0;
+       long *fts_msgs = NULL;
+       int is_in_list = 0;
+
+       /* If there is a BODY search criterion in the query, use our full
+        * text index to disqualify messages that don't have any chance of
+        * matching.  (Only do this if the index is enabled!!)
+        */
+        if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
+               if (!strcasecmp(itemlist[i], "BODY")) {
+                       ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
+                       if (fts_num_msgs > 0) {
+                               for (j=0; j < IMAP->num_msgs; ++j) {
+                                       if (IMAP->flags[j] & IMAP_SELECTED) {
+                                               is_in_list = 0;
+                                               for (k=0; k<fts_num_msgs; ++k) {
+                                                       if (IMAP->msgids[j] == fts_msgs[k]) {
+                                                               ++is_in_list;
+                                                       }
+                                               }
+                                       }
+                                       if (!is_in_list) {
+                                               IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
+                                       }
+                               }
+                       }
+                       else {          /* no hits on the index; disqualify every message */
+                               for (j=0; j < IMAP->num_msgs; ++j) {
+                                       IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
+                               }
+                       }
+                       if (fts_msgs) {
+                               free(fts_msgs);
+                       }
+               }
+       }
 
+       /* Now go through the messages and apply all search criteria. */
+       buffer_output();
        cprintf("* SEARCH ");
        if (IMAP->num_msgs > 0)
         for (i = 0; i < IMAP->num_msgs; ++i)
-         if (IMAP->flags[i] && IMAP_SELECTED) {
-               msg = CtdlFetchMessage(IMAP->msgids[i]);
-               if (msg != NULL) {
-                       if (imap_do_search_msg(i+1, msg, num_items,
-                          itemlist, is_uid)) {
-                               if (is_uid) {
-                                       cprintf("%ld ", IMAP->msgids[i]);
-                               }
-                               else {
-                                       cprintf("%d ", i+1);
-                               }
+         if (IMAP->flags[i] & IMAP_SELECTED) {
+               if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
+                       if (is_uid) {
+                               cprintf("%ld ", IMAP->msgids[i]);
+                       }
+                       else {
+                               cprintf("%d ", i+1);
                        }
-                       CtdlFreeMessage(msg);
-               }
-               else {
-                       lprintf(CTDL_ERR, "SEARCH internal error\n");
                }
        }
        cprintf("\r\n");
+       unbuffer_output();
 }
 
 
@@ -407,6 +511,10 @@ void imap_search(int num_parms, char *parms[]) {
                return;
        }
 
+       for (i = 0; i < IMAP->num_msgs; ++i) {
+               IMAP->flags[i] |= IMAP_SELECTED;
+       }
+
        for (i=1; i<num_parms; ++i) {
                if (imap_is_message_set(parms[i])) {
                        imap_pick_range(parms[i], 0);
@@ -428,6 +536,10 @@ void imap_uidsearch(int num_parms, char *parms[]) {
                return;
        }
 
+       for (i = 0; i < IMAP->num_msgs; ++i) {
+               IMAP->flags[i] |= IMAP_SELECTED;
+       }
+
        for (i=1; i<num_parms; ++i) {
                if (imap_is_message_set(parms[i])) {
                        imap_pick_range(parms[i], 1);