]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/imap/imap_search.c
Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[citadel.git] / citadel / modules / imap / imap_search.c
index 8a8673e6afdd44c704bd07fff484a211a454d82f..f1b7f73e993ce26cc246a3f3effcd7c8070c48aa 100644 (file)
@@ -1,22 +1,15 @@
 /*
  * Implements IMAP's gratuitously complex SEARCH command.
  *
+ * Copyright (c) 2001-2015 by the citadel.org team
  *
- * Copyright (c) 2001-2009 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 free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  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.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * 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"
@@ -122,11 +115,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
@@ -139,13 +132,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -158,18 +151,18 @@ 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;
                }
 
                /* Otherwise, we have to do a slow search. */
                else {
                        if (msg == NULL) {
-                               msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                               msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                                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;
                                }
                        }
@@ -180,18 +173,18 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "CC")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
@@ -226,14 +219,14 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                        }
                }
@@ -248,7 +241,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
                 * examining the message body.
                 */
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        need_to_free_msg = 1;
                }
 
@@ -278,11 +271,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                        }
                }
@@ -305,13 +298,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "ON")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -335,13 +328,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -351,13 +344,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -367,13 +360,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -383,13 +376,13 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                                }
                        }
@@ -399,11 +392,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                        }
                }
@@ -412,11 +405,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                        }
                }
@@ -425,7 +418,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        need_to_free_msg = 1;
                }
                if (msg != NULL) {
@@ -440,11 +433,11 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
 
        else if (!strcasecmp(itemlist[pos].Key, "TO")) {
                if (msg == NULL) {
-                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
+                       msg = CtdlFetchMessage(Imap->msgids[seq-1], 1, 1);
                        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;
                        }
                }
@@ -532,7 +525,7 @@ int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
        }
 
        if (need_to_free_msg) {
-               CtdlFreeMessage(msg);
+               CM_Free(msg);
        }
        return(match);
 }
@@ -572,7 +565,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) {