Moved to new module init structure.
[citadel.git] / citadel / imap_search.c
index 46963990b841908ab78cdb69afac14040bb050bb..1c647774bbe8deee7d87a50890c7c2dd7e5d6c3f 100644 (file)
@@ -36,7 +36,6 @@
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
@@ -111,12 +110,14 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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 (bmstrcasestr(fieldptr, itemlist[pos+1])) {
-                               match = 1;
+               if (msg != NULL) {
+                       fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
+                       if (fieldptr != NULL) {
+                               if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
+                                       match = 1;
+                               }
+                               free(fieldptr);
                        }
-                       free(fieldptr);
                }
                pos += 2;
        }
@@ -126,10 +127,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) < 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -150,8 +153,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                                msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                                need_to_free_msg = 1;
                        }
-                       if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
-                               match = 1;
+                       if (msg != NULL) {
+                               if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
+                                       match = 1;
+                               }
                        }
                }
 
@@ -163,19 +168,21 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               fieldptr = msg->cm_fields['Y'];
-               if (fieldptr != NULL) {
-                       if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
-                               match = 1;
-                       }
-               }
-               else {
-                       fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
+               if (msg != NULL) {
+                       fieldptr = msg->cm_fields['Y'];
                        if (fieldptr != NULL) {
                                if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
                                        match = 1;
                                }
-                               free(fieldptr);
+                       }
+                       else {
+                               fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
+                               if (fieldptr != NULL) {
+                                       if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
+                                               match = 1;
+                                       }
+                                       free(fieldptr);
+                               }
                        }
                }
                pos += 2;
@@ -207,22 +214,55 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
-                       match = 1;
-               }
-               if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
-                       match = 1;
+               if (msg != NULL) {
+                       if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
+                               match = 1;
+                       }
+                       if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
+                               match = 1;
+                       }
                }
                pos += 2;
        }
 
        else if (!strcasecmp(itemlist[pos], "HEADER")) {
-               /* FIXME */
+
+               /* We've got to do a slow search for this because the client
+                * might be asking for an RFC822 header field that has not been
+                * converted into a Citadel header field.  That requires
+                * examining the message body.
+                */
+               if (msg == NULL) {
+                       msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
+                       need_to_free_msg = 1;
+               }
+
+               if (msg != NULL) {
+       
+                       CC->redirect_buffer = malloc(SIZ);
+                       CC->redirect_len = 0;
+                       CC->redirect_alloc = SIZ;
+                       CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
+       
+                       fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
+                       if (fieldptr != NULL) {
+                               if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
+                                       match = 1;
+                               }
+                               free(fieldptr);
+                       }
+       
+                       free(CC->redirect_buffer);
+                       CC->redirect_buffer = NULL;
+                       CC->redirect_len = 0;
+                       CC->redirect_alloc = 0;
+               }
+
                pos += 3;       /* Yes, three */
        }
 
        else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
-               /* FIXME */
+               /* not implemented */
                pos += 2;
        }
 
@@ -231,8 +271,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
-                       match = 1;
+               if (msg != NULL) {
+                       if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
+                               match = 1;
+                       }
                }
                pos += 2;
        }
@@ -256,10 +298,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) == 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -284,10 +328,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) < 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -298,10 +344,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) == 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -312,10 +360,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) >= 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -326,10 +376,12 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        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) {
-                               match = 1;
+               if (msg != NULL) {
+                       if (msg->cm_fields['T'] != NULL) {
+                               if (imap_datecmp(itemlist[pos+1],
+                                               atol(msg->cm_fields['T'])) >= 0) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -340,8 +392,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
-                       match = 1;
+               if (msg != NULL) {
+                       if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
+                               match = 1;
+                       }
                }
                pos += 2;
        }
@@ -351,8 +405,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
-                       match = 1;
+               if (msg != NULL) {
+                       if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
+                               match = 1;
+                       }
                }
                pos += 2;
        }
@@ -362,9 +418,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               for (i='A'; i<='Z'; ++i) {
-                       if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
-                               match = 1;
+               if (msg != NULL) {
+                       for (i='A'; i<='Z'; ++i) {
+                               if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
+                                       match = 1;
+                               }
                        }
                }
                pos += 2;
@@ -375,12 +433,23 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
                        need_to_free_msg = 1;
                }
-               if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
-                       match = 1;
+               if (msg != NULL) {
+                       if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
+                               match = 1;
+                       }
                }
                pos += 2;
        }
 
+       /* FIXME this is b0rken.  fix it. */
+       else if (imap_is_message_set(itemlist[pos])) {
+               if (is_msg_in_sequence_set(itemlist[pos], seq)) {
+                       match = 1;
+               }
+               pos += 1;
+       }
+
+       /* FIXME this is b0rken.  fix it. */
        else if (!strcasecmp(itemlist[pos], "UID")) {
                if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
                        match = 1;
@@ -390,8 +459,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_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: the design of
-        * IMAP suffers from gratuitous complexity.
+        * can't there be *one* way to do things?  More gratuitous complexity.
         */
 
        else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
@@ -475,7 +543,7 @@ void imap_do_search(int num_items, char **itemlist, int is_uid) {
         * 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 (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) {
@@ -543,12 +611,6 @@ void imap_search(int num_parms, char *parms[]) {
                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);
-               }
-       }
-
        imap_do_search(num_parms-2, &parms[2], 0);
        cprintf("%s OK SEARCH completed\r\n", parms[0]);
 }
@@ -568,12 +630,6 @@ void imap_uidsearch(int num_parms, char *parms[]) {
                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);
-               }
-       }
-
        imap_do_search(num_parms-3, &parms[3], 1);
        cprintf("%s OK UID SEARCH completed\r\n", parms[0]);
 }