stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_search.c
index 8cc3fd3d663f00311780d4c4377d297dc552e6ef..efc19920274401a412b275376c29a705af801c6f 100644 (file)
@@ -1,21 +1,15 @@
 /*
  * Implements IMAP's gratuitously complex SEARCH command.
  *
- * Copyright (c) 2001-2012 by the citadel.org team
+ * Copyright (c) 2001-2020 by the citadel.org team
  *
- *  This program is open source software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 3.
- *  
- *  
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  
- *  
- *  
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  */
 
 #include "ctdl_module.h"
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
@@ -125,7 +108,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
+                       fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
                        if (fieldptr != NULL) {
                                if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
                                        match = 1;
@@ -142,9 +125,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) < 0) {
+                                               atol(msg->cm_fields[eTimestamp])) < 0) {
                                        match = 1;
                                }
                        }
@@ -157,7 +140,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                /* If fulltext indexing is active, on this server,
                 *  all messages have already been qualified.
                 */
-               if (config.c_enable_fulltext) {
+               if (CtdlGetConfigInt("c_enable_fulltext")) {
                        match = 1;
                }
 
@@ -168,7 +151,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                                need_to_free_msg = 1;
                        }
                        if (msg != NULL) {
-                               if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1].Key)) {
+                               if (bmstrcasestr(msg->cm_fields[eMesageText], itemlist[pos+1].Key)) {
                                        match = 1;
                                }
                        }
@@ -183,14 +166,14 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       fieldptr = msg->cm_fields['Y'];
+                       fieldptr = msg->cm_fields[eCarbonCopY];
                        if (fieldptr != NULL) {
                                if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
                                        match = 1;
                                }
                        }
                        else {
-                               fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
+                               fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
                                if (fieldptr != NULL) {
                                        if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
                                                match = 1;
@@ -229,10 +212,10 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1].Key)) {
+                       if (bmstrcasestr(msg->cm_fields[eAuthor], itemlist[pos+1].Key)) {
                                match = 1;
                        }
-                       if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1].Key)) {
+                       if (bmstrcasestr(msg->cm_fields[erFc822Addr], itemlist[pos+1].Key)) {
                                match = 1;
                        }
                }
@@ -281,7 +264,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1].Key)) {
+                       if (msg->cm_lengths[eMesageText] > atoi(itemlist[pos+1].Key)) {
                                match = 1;
                        }
                }
@@ -308,9 +291,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) == 0) {
+                                               atol(msg->cm_fields[eTimestamp])) == 0) {
                                        match = 1;
                                }
                        }
@@ -338,9 +321,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) < 0) {
+                                               atol(msg->cm_fields[eTimestamp])) < 0) {
                                        match = 1;
                                }
                        }
@@ -354,9 +337,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) == 0) {
+                                               atol(msg->cm_fields[eTimestamp])) == 0) {
                                        match = 1;
                                }
                        }
@@ -370,9 +353,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) >= 0) {
+                                               atol(msg->cm_fields[eTimestamp])) >= 0) {
                                        match = 1;
                                }
                        }
@@ -386,9 +369,9 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (msg->cm_fields['T'] != NULL) {
+                       if (!CM_IsEmpty(msg, eTimestamp)) {
                                if (imap_datecmp(itemlist[pos+1].Key,
-                                               atol(msg->cm_fields['T'])) >= 0) {
+                                               atol(msg->cm_fields[eTimestamp])) >= 0) {
                                        match = 1;
                                }
                        }
@@ -402,7 +385,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1].Key)) {
+                       if (msg->cm_lengths[eMesageText] < atoi(itemlist[pos+1].Key)) {
                                match = 1;
                        }
                }
@@ -415,7 +398,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1].Key)) {
+                       if (bmstrcasestr(msg->cm_fields[eMsgSubject], itemlist[pos+1].Key)) {
                                match = 1;
                        }
                }
@@ -443,7 +426,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
-                       if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1].Key)) {
+                       if (bmstrcasestr(msg->cm_fields[eRecipient], itemlist[pos+1].Key)) {
                                match = 1;
                        }
                }
@@ -531,7 +514,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
        }
 
        if (need_to_free_msg) {
-               CtdlFreeMessage(msg);
+               CM_Free(msg);
        }
        return(match);
 }
@@ -571,7 +554,7 @@ void imap_do_search(int num_items, ConstStr *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 (CtdlGetConfigInt("c_enable_fulltext")) for (i=0; i<(num_items-1); ++i) {
                if (!strcasecmp(itemlist[i].Key, "BODY")) {
                        CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
                        if (fts_num_msgs > 0) {