Prefer G_N_ELEMENTS over sizeof calculations.
[platform/upstream/evolution-data-server.git] / camel / camel-folder-search.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  *  Authors: Michael Zucchi <notzed@ximian.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /* This is a helper class for folders to implement the search function.
23    It implements enough to do basic searches on folders that can provide
24    an in-memory summary and a body index. */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 /* POSIX requires <sys/types.h> be included before <regex.h> */
31 #include <sys/types.h>
32
33 #include <ctype.h>
34 #include <regex.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <glib.h>
39 #include <glib/gi18n-lib.h>
40
41 #include "camel-exception.h"
42 #include "camel-folder-search.h"
43 #include "camel-folder-thread.h"
44 #include "camel-iconv.h"
45 #include "camel-medium.h"
46 #include "camel-mime-message.h"
47 #include "camel-multipart.h"
48 #include "camel-search-private.h"
49 #include "camel-stream-mem.h"
50 #include "camel-db.h"
51 #include "camel-debug.h"
52 #include "camel-store.h"
53 #include "camel-vee-folder.h"
54 #include "camel-string-utils.h"
55 #include "camel-search-sql.h"
56 #include "camel-search-sql-sexp.h"
57
58 #define d(x)
59 #define r(x)
60 #define dd(x) if (camel_debug("search")) x
61
62 struct _CamelFolderSearchPrivate {
63         CamelException *ex;
64
65         CamelFolderThread *threads;
66         GHashTable *threads_hash;
67 };
68
69 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
70
71 static ESExpResult *search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72
73 static ESExpResult *search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_starts_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_ends_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_header_exists(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_header_soundex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
79 static ESExpResult *search_header_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_header_full_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
81 static ESExpResult *search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
82 static ESExpResult *search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
83 static ESExpResult *search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
84 static ESExpResult *search_body_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
85 static ESExpResult *search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_system_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_get_size(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
93 static ESExpResult *search_message_location(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
94
95 static ESExpResult *search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
96
97 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
98 static void camel_folder_search_init       (CamelFolderSearch *obj);
99 static void camel_folder_search_finalize   (CamelObject *obj);
100
101 static gint read_uid_callback (gpointer  ref, gint ncol, gchar ** cols, gchar **name);
102
103 static CamelObjectClass *camel_folder_search_parent;
104
105 static void
106 camel_folder_search_class_init (CamelFolderSearchClass *klass)
107 {
108         camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
109
110         klass->not = search_not;
111
112         klass->match_all = search_match_all;
113         klass->match_threads = search_match_threads;
114         klass->body_contains = search_body_contains;
115         klass->body_regex = search_body_regex;
116         klass->header_contains = search_header_contains;
117         klass->header_matches = search_header_matches;
118         klass->header_starts_with = search_header_starts_with;
119         klass->header_ends_with = search_header_ends_with;
120         klass->header_exists = search_header_exists;
121         klass->header_soundex = search_header_soundex;
122         klass->header_regex = search_header_regex;
123         klass->header_full_regex = search_header_full_regex;
124         klass->user_tag = search_user_tag;
125         klass->user_flag = search_user_flag;
126         klass->system_flag = search_system_flag;
127         klass->get_sent_date = search_get_sent_date;
128         klass->get_received_date = search_get_received_date;
129         klass->get_current_date = search_get_current_date;
130         klass->get_size = search_get_size;
131         klass->uid = search_uid;
132         klass->message_location = search_message_location;
133 }
134
135 static void
136 camel_folder_search_init (CamelFolderSearch *obj)
137 {
138         struct _CamelFolderSearchPrivate *p;
139
140         p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
141
142         obj->sexp = e_sexp_new();
143 }
144
145 static void
146 camel_folder_search_finalize (CamelObject *obj)
147 {
148         CamelFolderSearch *search = (CamelFolderSearch *)obj;
149         struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
150
151         if (search->sexp)
152                 e_sexp_unref(search->sexp);
153
154         g_free(search->last_search);
155         g_free(p);
156 }
157
158 CamelType
159 camel_folder_search_get_type (void)
160 {
161         static CamelType type = CAMEL_INVALID_TYPE;
162
163         if (type == CAMEL_INVALID_TYPE) {
164                 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
165                                             sizeof (CamelFolderSearch),
166                                             sizeof (CamelFolderSearchClass),
167                                             (CamelObjectClassInitFunc) camel_folder_search_class_init,
168                                             NULL,
169                                             (CamelObjectInitFunc) camel_folder_search_init,
170                                             (CamelObjectFinalizeFunc) camel_folder_search_finalize);
171         }
172
173         return type;
174 }
175
176 #ifdef offsetof
177 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) offsetof (type, field))
178 #else
179 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) ((gchar *) &((type *) 0)->field))
180 #endif
181
182 static struct {
183         const gchar *name;
184         gint offset;
185         gint flags;             /* 0x02 = immediate, 0x01 = always enter */
186 } builtins[] = {
187         /* these have default implementations in e-sexp */
188         { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
189         { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
190         /* we need to override this one though to implement an 'array not' */
191         { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
192         { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
193         { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
194         { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
195
196         /* these we have to use our own default if there is none */
197         /* they should all be defined in the language? so it parses, or should they not?? */
198         { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
199         { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
200         { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
201         { "body-regex",  CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1  },
202         { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
203         { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
204         { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
205         { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
206         { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
207         { "header-soundex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
208         { "header-regex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
209         { "header-full-regex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
210         { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
211         { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
212         { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
213         { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
214         { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
215         { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
216         { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
217         { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
218         { "message-location", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
219 };
220
221 void
222 camel_folder_search_construct (CamelFolderSearch *search)
223 {
224         gint i;
225         CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
226
227         for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
228                 gpointer func;
229                 /* c is sure messy sometimes */
230                 func = *((gpointer *)(((gchar *)klass)+builtins[i].offset));
231                 if (func == NULL && builtins[i].flags&1) {
232                         g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
233                         func = (gpointer)search_dummy;
234                 }
235                 if (func != NULL) {
236                         if (builtins[i].flags&2) {
237                                 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
238                         } else {
239                                 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
240                         }
241                 }
242         }
243 }
244
245 /**
246  * camel_folder_search_new:
247  *
248  * Create a new CamelFolderSearch object.
249  *
250  * A CamelFolderSearch is a subclassable, extensible s-exp
251  * evaluator which enforces a particular set of s-expressions.
252  * Particular methods may be overriden by an implementation to
253  * implement a search for any sort of backend.
254  *
255  * Return value: A new CamelFolderSearch widget.
256  **/
257 CamelFolderSearch *
258 camel_folder_search_new (void)
259 {
260         CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
261
262         camel_folder_search_construct(new);
263         return new;
264 }
265
266 /**
267  * camel_folder_search_set_folder:
268  * @search:
269  * @folder: A folder.
270  *
271  * Set the folder attribute of the search.  This is currently unused, but
272  * could be used to perform a slow-search when indexes and so forth are not
273  * available.  Or for use by subclasses.
274  **/
275 void
276 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
277 {
278         search->folder = folder;
279 }
280
281 /**
282  * camel_folder_search_set_summary:
283  * @search:
284  * @summary: An array of CamelMessageInfo pointers.
285  *
286  * Set the array of summary objects representing the span of the search.
287  *
288  * If this is not set, then a subclass must provide the functions
289  * for searching headers and for the match-all operator.
290  **/
291 void
292 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
293 {
294         search->summary = summary;
295 }
296
297 /**
298  * camel_folder_search_set_body_index:
299  * @search:
300  * @index:
301  *
302  * Set the index representing the contents of all messages
303  * in this folder.  If this is not set, then the folder implementation
304  * should sub-class the CamelFolderSearch and provide its own
305  * body-contains function.
306  **/
307 void
308 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
309 {
310         if (search->body_index)
311                 camel_object_unref((CamelObject *)search->body_index);
312         search->body_index = index;
313         if (index)
314                 camel_object_ref((CamelObject *)index);
315 }
316
317 /**
318  * camel_folder_search_execute_expression:
319  * @search:
320  * @expr:
321  * @ex:
322  *
323  * Execute the search expression @expr, returning an array of
324  * all matches as a GPtrArray of uid's of matching messages.
325  *
326  * Note that any settings such as set_body_index(), set_folder(),
327  * and so on are reset to #NULL once the search has completed.
328  *
329  * TODO: The interface should probably return summary items instead
330  * (since they are much more useful to any client).
331  *
332  * Return value: A GPtrArray of strings of all matching messages.
333  * This must only be freed by camel_folder_search_free_result.
334  **/
335 GPtrArray *
336 camel_folder_search_execute_expression(CamelFolderSearch *search, const gchar *expr, CamelException *ex)
337 {
338         ESExpResult *r;
339         GPtrArray *matches;
340         gint i;
341         GHashTable *results;
342         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
343
344         p->ex = ex;
345
346         /* only re-parse if the search has changed */
347         if (search->last_search == NULL
348             || strcmp(search->last_search, expr)) {
349                 e_sexp_input_text(search->sexp, expr, strlen(expr));
350                 if (e_sexp_parse(search->sexp) == -1) {
351                         camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
352                         return NULL;
353                 }
354
355                 g_free(search->last_search);
356                 search->last_search = g_strdup(expr);
357         }
358         r = e_sexp_eval(search->sexp);
359         if (r == NULL) {
360                 if (!camel_exception_is_set(ex))
361                         camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
362                 return NULL;
363         }
364
365         matches = g_ptr_array_new();
366
367         /* now create a folder summary to return?? */
368         if (r->type == ESEXP_RES_ARRAY_PTR) {
369                 d(printf("got result ...\n"));
370                 if (search->summary) {
371                         /* reorder result in summary order */
372                         results = g_hash_table_new(g_str_hash, g_str_equal);
373                         for (i=0;i<r->value.ptrarray->len;i++) {
374                                 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
375                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
376                         }
377                         for (i=0;i<search->summary->len;i++) {
378                                 gchar *uid = g_ptr_array_index(search->summary, i);
379                                 if (g_hash_table_lookup(results, uid)) {
380                                         g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
381                                 }
382                         }
383                         g_hash_table_destroy(results);
384                 } else {
385                         for (i=0;i<r->value.ptrarray->len;i++) {
386                                 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
387                                 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
388                         }
389                 }
390         } else {
391                 g_warning("Search returned an invalid result type");
392         }
393
394         e_sexp_result_free(search->sexp, r);
395
396         if (p->threads)
397                 camel_folder_thread_messages_unref(p->threads);
398         if (p->threads_hash)
399                 g_hash_table_destroy(p->threads_hash);
400
401         p->threads = NULL;
402         p->threads_hash = NULL;
403         search->folder = NULL;
404         search->summary = NULL;
405         search->current = NULL;
406         search->body_index = NULL;
407
408         return matches;
409 }
410
411 /**
412  * camel_folder_search_count:
413  * @search:
414  * @expr:
415  * @uids: to search against, NULL for all uid's.
416  * @ex:
417  *
418  * Run a search.  Search must have had Folder already set on it, and
419  * it must implement summaries.
420  *
421  * Return value: Number of messages that match the query.
422  **/
423
424 guint32
425 camel_folder_search_count(CamelFolderSearch *search, const gchar *expr, CamelException *ex)
426 {
427         ESExpResult *r;
428         GPtrArray *summary_set;
429         gint i;
430         CamelDB *cdb;
431         gchar *sql_query, *tmp, *tmp1;
432         GHashTable *results;
433         guint32 count = 0;
434
435         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
436
437         g_assert(search->folder);
438
439         p->ex = ex;
440
441         /* We route body-contains search and thread based search through memory and not via db. */
442         if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
443                 /* setup our search list only contains those we're interested in */
444                 search->summary = camel_folder_get_summary(search->folder);
445
446                 summary_set = search->summary;
447
448                 /* only re-parse if the search has changed */
449                 if (search->last_search == NULL
450                     || strcmp(search->last_search, expr)) {
451                         e_sexp_input_text(search->sexp, expr, strlen(expr));
452                         if (e_sexp_parse(search->sexp) == -1) {
453                                 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
454                                 goto fail;
455                         }
456
457                         g_free(search->last_search);
458                         search->last_search = g_strdup(expr);
459                 }
460                 r = e_sexp_eval(search->sexp);
461                 if (r == NULL) {
462                         if (!camel_exception_is_set(ex))
463                                 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
464                         goto fail;
465                 }
466
467                 /* now create a folder summary to return?? */
468                 if (r->type == ESEXP_RES_ARRAY_PTR) {
469                         d(printf("got result\n"));
470
471                         /* reorder result in summary order */
472                         results = g_hash_table_new(g_str_hash, g_str_equal);
473                         for (i=0;i<r->value.ptrarray->len;i++) {
474                                 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
475                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
476                         }
477
478                         for (i=0;i<summary_set->len;i++) {
479                                 gchar *uid = g_ptr_array_index(summary_set, i);
480                                 if (g_hash_table_lookup(results, uid))
481                                         count++;
482                         }
483                         g_hash_table_destroy(results);
484                 }
485
486                 e_sexp_result_free(search->sexp, r);
487
488         } else {
489                 /* Sync the db, so that we search the db for changes */
490                 camel_folder_summary_save_to_db (search->folder->summary, ex);
491
492                 dd(printf ("sexp is : [%s]\n", expr));
493                 if (g_getenv("SQL_SEARCH_OLD"))
494                         sql_query = camel_sexp_to_sql (expr);
495                 else
496                         sql_query = camel_sexp_to_sql_sexp (expr);
497                 tmp1 = camel_db_sqlize_string(search->folder->full_name);
498                 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
499                 camel_db_free_sqlized_string (tmp1);
500                 g_free (sql_query);
501                 dd(printf("Equivalent sql %s\n", tmp));
502
503                 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
504                 camel_db_count_message_info  (cdb, tmp, &count, ex);
505                 if (ex && camel_exception_is_set(ex)) {
506                         const gchar *exception = camel_exception_get_description (ex);
507                         if (strncmp(exception, "no such table", 13) == 0) {
508                                 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
509                                 camel_exception_clear (ex); /* Suppress no such table */
510                         }
511                 }
512                 g_free (tmp);
513
514         }
515
516 fail:
517         /* these might be allocated by match-threads */
518         if (p->threads)
519                 camel_folder_thread_messages_unref(p->threads);
520         if (p->threads_hash)
521                 g_hash_table_destroy(p->threads_hash);
522         if (search->summary_set)
523                 g_ptr_array_free(search->summary_set, TRUE);
524         if (search->summary)
525                 camel_folder_free_summary(search->folder, search->summary);
526
527         p->threads = NULL;
528         p->threads_hash = NULL;
529         search->folder = NULL;
530         search->summary = NULL;
531         search->summary_set = NULL;
532         search->current = NULL;
533         search->body_index = NULL;
534
535         return count;
536 }
537
538 static gboolean
539 do_search_in_memory (const gchar *expr)
540 {
541         /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
542         const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
543         gint i;
544
545         if (!expr)
546                 return FALSE;
547
548         for (i = 0; in_memory_tokens [i]; i++) {
549                 if (strstr (expr, in_memory_tokens [i]))
550                         return TRUE;
551         }
552
553         return FALSE;
554 }
555
556 /**
557  * camel_folder_search_search:
558  * @search:
559  * @expr:
560  * @uids: to search against, NULL for all uid's.
561  * @ex:
562  *
563  * Run a search.  Search must have had Folder already set on it, and
564  * it must implement summaries.
565  *
566  * Return value:
567  **/
568 GPtrArray *
569 camel_folder_search_search(CamelFolderSearch *search, const gchar *expr, GPtrArray *uids, CamelException *ex)
570 {
571         ESExpResult *r;
572         GPtrArray *matches = NULL, *summary_set;
573         gint i;
574         CamelDB *cdb;
575         gchar *sql_query, *tmp, *tmp1;
576         GHashTable *results;
577
578         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
579
580         g_assert(search->folder);
581
582         p->ex = ex;
583
584         /* We route body-contains / thread based search and uid search through memory and not via db. */
585         if (uids || do_search_in_memory (expr)) {
586                 /* setup our search list only contains those we're interested in */
587                 search->summary = camel_folder_get_summary(search->folder);
588
589                 if (uids) {
590                         GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
591
592                         summary_set = search->summary_set = g_ptr_array_new();
593                         for (i=0;i<uids->len;i++)
594                                 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
595                         for (i=0;i<search->summary->len;i++)
596                                 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
597                                         g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
598                         g_hash_table_destroy(uids_hash);
599                 } else {
600                         summary_set = search->summary;
601                 }
602
603                 /* only re-parse if the search has changed */
604                 if (search->last_search == NULL
605                     || strcmp(search->last_search, expr)) {
606                         e_sexp_input_text(search->sexp, expr, strlen(expr));
607                         if (e_sexp_parse(search->sexp) == -1) {
608                                 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
609                                 goto fail;
610                         }
611
612                         g_free(search->last_search);
613                         search->last_search = g_strdup(expr);
614                 }
615                 r = e_sexp_eval(search->sexp);
616                 if (r == NULL) {
617                         if (!camel_exception_is_set(ex))
618                                 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
619                         goto fail;
620                 }
621
622                 matches = g_ptr_array_new();
623
624                 /* now create a folder summary to return?? */
625                 if (r->type == ESEXP_RES_ARRAY_PTR) {
626                         d(printf("got result\n"));
627
628                         /* reorder result in summary order */
629                         results = g_hash_table_new(g_str_hash, g_str_equal);
630                         for (i=0;i<r->value.ptrarray->len;i++) {
631                                 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
632                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
633                         }
634
635                         for (i=0;i<summary_set->len;i++) {
636                                 gchar *uid = g_ptr_array_index(summary_set, i);
637                                 if (g_hash_table_lookup(results, uid))
638                                         g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
639                         }
640                         g_hash_table_destroy(results);
641                 }
642
643                 e_sexp_result_free(search->sexp, r);
644
645         } else {
646                 /* Sync the db, so that we search the db for changes */
647                 camel_folder_summary_save_to_db (search->folder->summary, ex);
648
649                 dd(printf ("sexp is : [%s]\n", expr));
650                 if (g_getenv("SQL_SEARCH_OLD"))
651                         sql_query = camel_sexp_to_sql (expr);
652                 else
653                         sql_query = camel_sexp_to_sql_sexp (expr);
654                 tmp1 = camel_db_sqlize_string(search->folder->full_name);
655                 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
656                 camel_db_free_sqlized_string (tmp1);
657                 g_free (sql_query);
658                 dd(printf("Equivalent sql %s\n", tmp));
659
660                 matches = g_ptr_array_new();
661                 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
662                 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
663                 if (ex && camel_exception_is_set(ex)) {
664                         const gchar *exception = camel_exception_get_description (ex);
665                         if (strncmp(exception, "no such table", 13) == 0) {
666                                 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
667                                 camel_exception_clear (ex); /* Suppress no such table */
668                         }
669                 }
670                 g_free (tmp);
671
672         }
673
674 fail:
675         /* these might be allocated by match-threads */
676         if (p->threads)
677                 camel_folder_thread_messages_unref(p->threads);
678         if (p->threads_hash)
679                 g_hash_table_destroy(p->threads_hash);
680         if (search->summary_set)
681                 g_ptr_array_free(search->summary_set, TRUE);
682         if (search->summary)
683                 camel_folder_free_summary(search->folder, search->summary);
684
685         p->threads = NULL;
686         p->threads_hash = NULL;
687         search->folder = NULL;
688         search->summary = NULL;
689         search->summary_set = NULL;
690         search->current = NULL;
691         search->body_index = NULL;
692
693         return matches;
694 }
695
696 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
697 {
698         g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
699         g_ptr_array_free(result, TRUE);
700 }
701
702 /* dummy function, returns false always, or an empty match array */
703 static ESExpResult *
704 search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
705 {
706         ESExpResult *r;
707
708         if (search->current == NULL) {
709                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
710                 r->value.bool = FALSE;
711         } else {
712                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
713                 r->value.ptrarray = g_ptr_array_new();
714         }
715
716         return r;
717 }
718
719 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
720 static ESExpResult *
721 search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
722 {
723         ESExpResult *r;
724         gint i;
725
726         if (argc>0) {
727                 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
728                         GPtrArray *v = argv[0]->value.ptrarray;
729                         const gchar *uid;
730
731                         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
732                         r->value.ptrarray = g_ptr_array_new();
733
734                         /* not against a single message?*/
735                         if (search->current) {
736                                 gint found = FALSE;
737
738                                 uid = camel_message_info_uid(search->current);
739                                 for (i=0;!found && i<v->len;i++) {
740                                         if (strcmp(uid, v->pdata[i]) == 0)
741                                                 found = TRUE;
742                                 }
743
744                                 if (!found)
745                                         g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
746                         } else if (search->summary == NULL) {
747                                 g_warning("No summary set, 'not' against an array requires a summary");
748                         } else {
749                                 /* 'not' against the whole summary */
750                                 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
751                                 gchar **s;
752                                 gchar **m;
753
754                                 s = (gchar **)v->pdata;
755                                 for (i=0;i<v->len;i++)
756                                         g_hash_table_insert(have, s[i], s[i]);
757
758                                 v = search->summary_set?search->summary_set:search->summary;
759                                 m = (gchar **)v->pdata;
760                                 for (i=0;i<v->len;i++) {
761                                         gchar *uid = m[i];
762
763                                         if (g_hash_table_lookup(have, uid) == NULL)
764                                                 g_ptr_array_add(r->value.ptrarray, uid);
765                                 }
766                                 g_hash_table_destroy(have);
767                         }
768                 } else {
769                         gint res = TRUE;
770
771                         if (argv[0]->type == ESEXP_RES_BOOL)
772                                 res = ! argv[0]->value.bool;
773
774                         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
775                         r->value.bool = res;
776                 }
777         } else {
778                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
779                 r->value.bool = TRUE;
780         }
781
782         return r;
783 }
784
785 static ESExpResult *
786 search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
787 {
788         gint i;
789         ESExpResult *r, *r1;
790         gchar *error_msg;
791         GPtrArray *v;
792
793         if (argc>1) {
794                 g_warning("match-all only takes a single argument, other arguments ignored");
795         }
796
797         /* we are only matching a single message?  or already inside a match-all? */
798         if (search->current) {
799                 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
800
801                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
802                 r->value.bool = FALSE;
803
804                 if (argc>0) {
805                         r1 = e_sexp_term_eval(f, argv[0]);
806                         if (r1->type == ESEXP_RES_BOOL) {
807                                 r->value.bool = r1->value.bool;
808                         } else {
809                                 g_warning("invalid syntax, matches require a single bool result");
810                                 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
811                                 e_sexp_fatal_error(f, error_msg);
812                                 g_free(error_msg);
813                         }
814                         e_sexp_result_free(f, r1);
815                 } else {
816                         r->value.bool = TRUE;
817                 }
818                 return r;
819         }
820
821         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
822         r->value.ptrarray = g_ptr_array_new();
823
824         if (search->summary == NULL) {
825                 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
826                 g_warning("No summary supplied, match-all doesn't work with no summary");
827                 g_assert(0);
828                 return r;
829         }
830
831         v = search->summary_set?search->summary_set:search->summary;
832
833         if (v->len > g_hash_table_size (search->folder->summary->loaded_infos) && !CAMEL_IS_VEE_FOLDER (search->folder)) {
834                 camel_folder_summary_reload_from_db (search->folder->summary, search->priv->ex);
835         }
836
837         for (i=0;i<v->len;i++) {
838                 const gchar *uid;
839
840                 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
841                 if (!search->current)
842                         continue;
843                 uid = camel_message_info_uid(search->current);
844
845                 if (argc>0) {
846                         r1 = e_sexp_term_eval(f, argv[0]);
847                         if (r1->type == ESEXP_RES_BOOL) {
848                                 if (r1->value.bool)
849                                         g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
850                         } else {
851                                 g_warning("invalid syntax, matches require a single bool result");
852                                 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
853                                 e_sexp_fatal_error(f, error_msg);
854                                 g_free(error_msg);
855                         }
856                         e_sexp_result_free(f, r1);
857                 } else {
858                         g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
859                 }
860                 camel_message_info_free (search->current);
861         }
862         search->current = NULL;
863         return r;
864 }
865
866 static void
867 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
868 {
869         while (root) {
870                 g_hash_table_insert(id_hash, (gchar *)camel_message_info_uid(root->message), root);
871                 if (root->child)
872                         fill_thread_table(root->child, id_hash);
873                 root = root->next;
874         }
875 }
876
877 static void
878 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
879 {
880         while (root) {
881                 g_hash_table_insert(result_hash, (gchar *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
882                 if (root->child)
883                         add_thread_results(root->child, result_hash);
884                 root = root->next;
885         }
886 }
887
888 static void
889 add_results(gchar *uid, gpointer dummy, GPtrArray *result)
890 {
891         g_ptr_array_add(result, uid);
892 }
893
894 static ESExpResult *
895 search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
896 {
897         ESExpResult *r;
898         struct _CamelFolderSearchPrivate *p = search->priv;
899         gint i, type;
900         GHashTable *results;
901         gchar *error_msg;
902
903         /* not supported in match-all */
904         if (search->current) {
905                 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
906                 e_sexp_fatal_error(f, error_msg);
907                 g_free(error_msg);
908         }
909
910         if (argc == 0) {
911                 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
912                 e_sexp_fatal_error(f, error_msg);
913                 g_free(error_msg);
914         }
915
916         r = e_sexp_term_eval(f, argv[0]);
917         if (r->type != ESEXP_RES_STRING) {
918                 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
919                 e_sexp_fatal_error(f, error_msg);
920                 g_free(error_msg);
921         }
922
923         type = 0;
924         if (!strcmp(r->value.string, "none"))
925                 type = 0;
926         else if (!strcmp(r->value.string, "all"))
927                 type = 1;
928         else if (!strcmp(r->value.string, "replies"))
929                 type = 2;
930         else if (!strcmp(r->value.string, "replies_parents"))
931                 type = 3;
932         else if (!strcmp(r->value.string, "single"))
933                 type = 4;
934         e_sexp_result_free(f, r);
935
936         /* behave as (begin does */
937         r = NULL;
938         for (i=1;i<argc;i++) {
939                 if (r)
940                         e_sexp_result_free(f, r);
941                 r = e_sexp_term_eval(f, argv[i]);
942         }
943
944         if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
945                 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
946                 e_sexp_fatal_error(f, error_msg);
947                 g_free(error_msg);
948         }
949
950         if (type == 0)
951                 return r;
952
953         if (search->folder == NULL) {
954                 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
955                 e_sexp_fatal_error(f, error_msg);
956                 g_free(error_msg);
957         }
958
959         /* cache this, so we only have to re-calculate once per search at most */
960         if (p->threads == NULL) {
961                 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
962                 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
963
964                 fill_thread_table(p->threads->tree, p->threads_hash);
965         }
966
967         results = g_hash_table_new(g_str_hash, g_str_equal);
968         for (i=0;i<r->value.ptrarray->len;i++) {
969                 struct _CamelFolderThreadNode *node, *scan;
970
971                 if (type != 4)
972                         g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
973
974                 node = g_hash_table_lookup(p->threads_hash, (gchar *)g_ptr_array_index(r->value.ptrarray, i));
975                 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
976                         continue;
977
978                 /* select messages in thread according to search criteria */
979                 if (type == 4) {
980                         if (node->child == NULL && node->parent == NULL)
981                                 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
982                 } else {
983                         if (type == 3) {
984                                 scan = node;
985                                 while (scan && scan->parent) {
986                                         scan = scan->parent;
987                                         g_hash_table_insert(results, (gchar *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
988                                 }
989                         } else if (type == 1) {
990                                 while (node && node->parent)
991                                         node = node->parent;
992                         }
993                         g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
994                         if (node->child)
995                                 add_thread_results(node->child, results);
996                 }
997         }
998         e_sexp_result_free(f, r);
999
1000         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1001         r->value.ptrarray = g_ptr_array_new();
1002
1003         g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
1004         g_hash_table_destroy(results);
1005
1006         return r;
1007 }
1008
1009 static CamelMimeMessage *
1010 get_current_message (CamelFolderSearch *search)
1011 {
1012         CamelException x = CAMEL_EXCEPTION_INITIALISER;
1013         CamelMimeMessage *res;
1014
1015         if (!search || !search->folder || !search->current)
1016                 return NULL;
1017
1018         res = camel_folder_get_message (search->folder, search->current->uid, &x);
1019
1020         if (!res)
1021                 camel_exception_clear (&x);
1022
1023         return res;
1024 }
1025
1026 static ESExpResult *
1027 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1028 {
1029         ESExpResult *r;
1030         gint truth = FALSE;
1031
1032         r(printf("executing check-header %d\n", how));
1033
1034         /* are we inside a match-all? */
1035         if (search->current && argc>1
1036             && argv[0]->type == ESEXP_RES_STRING) {
1037                 gchar *headername;
1038                 const gchar *header = NULL, *charset = NULL;
1039                 gchar strbuf[32];
1040                 gint i, j;
1041                 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1042                 struct _camel_search_words *words;
1043                 CamelMimeMessage *message = NULL;
1044                 struct _camel_header_raw *raw_header;
1045
1046                 /* only a subset of headers are supported .. */
1047                 headername = argv[0]->value.string;
1048                 if (!g_ascii_strcasecmp(headername, "subject")) {
1049                         header = camel_message_info_subject(search->current);
1050                 } else if (!g_ascii_strcasecmp(headername, "date")) {
1051                         /* FIXME: not a very useful form of the date */
1052                         sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1053                         header = strbuf;
1054                 } else if (!g_ascii_strcasecmp(headername, "from")) {
1055                         header = camel_message_info_from(search->current);
1056                         type = CAMEL_SEARCH_TYPE_ADDRESS;
1057                 } else if (!g_ascii_strcasecmp(headername, "to")) {
1058                         header = camel_message_info_to(search->current);
1059                         type = CAMEL_SEARCH_TYPE_ADDRESS;
1060                 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1061                         header = camel_message_info_cc(search->current);
1062                         type = CAMEL_SEARCH_TYPE_ADDRESS;
1063                 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1064                         header = camel_message_info_mlist(search->current);
1065                         type = CAMEL_SEARCH_TYPE_MLIST;
1066                 } else {
1067                         message = get_current_message (search);
1068                         if (message) {
1069                                 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1070
1071                                 if (ct) {
1072                                         charset = camel_content_type_param (ct, "charset");
1073                                         charset = camel_iconv_charset_name (charset);
1074                                 }
1075                         }
1076                 }
1077
1078                 if (header == NULL)
1079                         header = "";
1080
1081                 /* performs an OR of all words */
1082                 for (i=1;i<argc && !truth;i++) {
1083                         if (argv[i]->type == ESEXP_RES_STRING) {
1084                                 if (argv[i]->value.string[0] == 0) {
1085                                         truth = TRUE;
1086                                 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1087                                         /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1088                                         words = camel_search_words_split((const guchar *) argv[i]->value.string);
1089                                         truth = TRUE;
1090                                         for (j=0;j<words->len && truth;j++) {
1091                                                 if (message) {
1092                                                         for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1093                                                                 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1094                                                                         if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1095                                                                                 break;;
1096                                                                 }
1097                                                         }
1098
1099                                                         truth = raw_header != NULL;
1100                                                 } else
1101                                                         truth = camel_search_header_match(header, words->words[j]->word, how, type, charset);
1102                                         }
1103                                         camel_search_words_free(words);
1104                                 } else {
1105                                         if (message) {
1106                                                 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1107                                                         if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1108                                                                 truth = camel_search_header_match(raw_header->value, argv[i]->value.string, how, type, charset);
1109                                                         }
1110                                                 }
1111                                         } else
1112                                                 truth = camel_search_header_match(header, argv[i]->value.string, how, type, charset);
1113                                 }
1114                         }
1115                 }
1116
1117                 if (message)
1118                         camel_object_unref (message);
1119         }
1120         /* TODO: else, find all matches */
1121
1122         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1123         r->value.bool = truth;
1124
1125         return r;
1126 }
1127
1128 /*
1129 static void
1130 l_printf(gchar *node)
1131 {
1132 printf("%s\t", node);
1133 }
1134 */
1135
1136 static ESExpResult *
1137 search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1138 {
1139         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1140 }
1141
1142 static ESExpResult *
1143 search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1144 {
1145         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1146 }
1147
1148 static ESExpResult *
1149 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1150 {
1151         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1152 }
1153
1154 static ESExpResult *
1155 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1156 {
1157         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1158 }
1159
1160 static ESExpResult *
1161 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1162 {
1163         ESExpResult *r;
1164
1165         r(printf ("executing header-exists\n"));
1166
1167         if (search->current) {
1168                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1169                 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1170                         r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
1171
1172         } else {
1173                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1174                 r->value.ptrarray = g_ptr_array_new();
1175         }
1176
1177         return r;
1178 }
1179
1180 static ESExpResult *
1181 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1182 {
1183         return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1184 }
1185
1186 static ESExpResult *
1187 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1188 {
1189         ESExpResult *r;
1190         CamelMimeMessage *msg;
1191
1192         msg = get_current_message (search);
1193
1194         if (msg) {
1195                 regex_t pattern;
1196                 const gchar *contents;
1197
1198                 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1199
1200                 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1201                     && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1202                     && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->ex) == 0) {
1203                         r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
1204                         regfree (&pattern);
1205                 } else
1206                         r->value.bool = FALSE;
1207
1208                 camel_object_unref (msg);
1209         } else {
1210                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1211                 r->value.ptrarray = g_ptr_array_new();
1212         }
1213
1214         return r;
1215 }
1216
1217 static gchar *
1218 get_full_header (CamelMimeMessage *message)
1219 {
1220         CamelMimePart *mp = CAMEL_MIME_PART (message);
1221         GString *str = g_string_new ("");
1222         struct _camel_header_raw *h;
1223
1224         for (h = mp->headers; h; h = h->next) {
1225                 if (h->value != NULL) {
1226                         g_string_append (str, h->name);
1227                         if (isspace (h->value[0]))
1228                                 g_string_append (str, ":");
1229                         else
1230                                 g_string_append (str, ": ");
1231                         g_string_append (str, h->value);
1232                         g_string_append_c (str, '\n');
1233                 }
1234         }
1235
1236         return g_string_free (str, FALSE);
1237 }
1238
1239 static ESExpResult *
1240 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1241 {
1242         ESExpResult *r;
1243         CamelMimeMessage *msg;
1244
1245         msg = get_current_message (search);
1246
1247         if (msg) {
1248                 regex_t pattern;
1249
1250                 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1251
1252                 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1253                         gchar *contents;
1254
1255                         contents = get_full_header (msg);
1256                         r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
1257
1258                         g_free (contents);
1259                         regfree (&pattern);
1260                 } else
1261                         r->value.bool = FALSE;
1262
1263                 camel_object_unref (msg);
1264         } else {
1265                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1266                 r->value.ptrarray = g_ptr_array_new();
1267         }
1268
1269         return r;
1270 }
1271
1272 /* this is just to OR results together */
1273 struct IterData {
1274         gint count;
1275         GPtrArray *uids;
1276 };
1277
1278 /* or, store all unique values */
1279 static void
1280 htor(gchar *key, gint value, struct IterData *iter_data)
1281 {
1282         g_ptr_array_add(iter_data->uids, key);
1283 }
1284
1285 /* and, only store duplicates */
1286 static void
1287 htand(gchar *key, gint value, struct IterData *iter_data)
1288 {
1289         if (value == iter_data->count)
1290                 g_ptr_array_add(iter_data->uids, key);
1291 }
1292
1293 static gint
1294 match_message_index(CamelIndex *idx, const gchar *uid, const gchar *match, CamelException *ex)
1295 {
1296         CamelIndexCursor *wc, *nc;
1297         const gchar *word, *name;
1298         gint truth = FALSE;
1299
1300         wc = camel_index_words(idx);
1301         if (wc) {
1302                 while (!truth && (word = camel_index_cursor_next(wc))) {
1303                         if (camel_ustrstrcase(word,match) != NULL) {
1304                                 /* perf: could have the wc cursor return the name cursor */
1305                                 nc = camel_index_find(idx, word);
1306                                 if (nc) {
1307                                         while (!truth && (name = camel_index_cursor_next(nc)))
1308                                                 truth = strcmp(name, uid) == 0;
1309                                         camel_object_unref((CamelObject *)nc);
1310                                 }
1311                         }
1312                 }
1313                 camel_object_unref((CamelObject *)wc);
1314         }
1315
1316         return truth;
1317 }
1318
1319 /*
1320  "one two" "three" "four five"
1321
1322   one and two
1323 or
1324   three
1325 or
1326   four and five
1327 */
1328
1329 /* returns messages which contain all words listed in words */
1330 static GPtrArray *
1331 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1332 {
1333         GPtrArray *result = g_ptr_array_new();
1334         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1335         struct IterData lambdafoo;
1336         CamelIndexCursor *wc, *nc;
1337         const gchar *word, *name;
1338         gint i;
1339
1340         /* we can have a maximum of 32 words, as we use it as the AND mask */
1341
1342         wc = camel_index_words(search->body_index);
1343         if (wc) {
1344                 while ((word = camel_index_cursor_next(wc))) {
1345                         for (i=0;i<words->len;i++) {
1346                                 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1347                                         /* perf: could have the wc cursor return the name cursor */
1348                                         nc = camel_index_find(search->body_index, word);
1349                                         if (nc) {
1350                                                 while ((name = camel_index_cursor_next(nc))) {
1351                                                                 gint mask;
1352
1353                                                                 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1354                                                                 g_hash_table_insert(ht, (gchar *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1355                                                 }
1356                                                 camel_object_unref((CamelObject *)nc);
1357                                         }
1358                                 }
1359                         }
1360                 }
1361                 camel_object_unref((CamelObject *)wc);
1362
1363                 lambdafoo.uids = result;
1364                 lambdafoo.count = (1<<words->len) - 1;
1365                 g_hash_table_foreach(ht, (GHFunc)htand, &lambdafoo);
1366                 g_hash_table_destroy(ht);
1367         }
1368
1369         return result;
1370 }
1371
1372 static gboolean
1373 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1374 {
1375         CamelDataWrapper *containee;
1376         gint truth = FALSE;
1377         gint parts, i;
1378
1379         containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1380
1381         if (containee == NULL)
1382                 return FALSE;
1383
1384         /* using the object types is more accurate than using the mime/types */
1385         if (CAMEL_IS_MULTIPART (containee)) {
1386                 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1387                 for (i = 0; i < parts && truth == FALSE; i++) {
1388                         CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1389                         if (part)
1390                                 truth = match_words_1message(part, words, mask);
1391                 }
1392         } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1393                 /* for messages we only look at its contents */
1394                 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1395         } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1396                 /* for all other text parts, we look inside, otherwise we dont care */
1397                 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1398
1399                 /* FIXME: The match should be part of a stream op */
1400                 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1401                 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1402                 for (i=0;i<words->len;i++) {
1403                         /* FIXME: This is horridly slow, and should use a real search algorithm */
1404                         if (camel_ustrstrcase((const gchar *) mem->buffer->data, words->words[i]->word) != NULL) {
1405                                 *mask |= (1<<i);
1406                                 /* shortcut a match */
1407                                 if (*mask == (1<<(words->len))-1)
1408                                         return TRUE;
1409                         }
1410                 }
1411
1412                 camel_object_unref (mem);
1413         }
1414
1415         return truth;
1416 }
1417
1418 static gboolean
1419 match_words_message(CamelFolder *folder, const gchar *uid, struct _camel_search_words *words, CamelException *ex)
1420 {
1421         guint32 mask;
1422         CamelMimeMessage *msg;
1423         CamelException x = CAMEL_EXCEPTION_INITIALISER;
1424         gint truth;
1425
1426         msg = camel_folder_get_message(folder, uid, &x);
1427         if (msg) {
1428                 mask = 0;
1429                 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1430                 camel_object_unref((CamelObject *)msg);
1431         } else {
1432                 camel_exception_clear(&x);
1433                 truth = FALSE;
1434         }
1435
1436         return truth;
1437 }
1438
1439 static GPtrArray *
1440 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1441 {
1442         gint i;
1443         GPtrArray *matches = g_ptr_array_new();
1444
1445         if (search->body_index) {
1446                 GPtrArray *indexed;
1447                 struct _camel_search_words *simple;
1448
1449                 simple = camel_search_words_simple(words);
1450                 indexed = match_words_index(search, simple, ex);
1451                 camel_search_words_free(simple);
1452
1453                 for (i=0;i<indexed->len;i++) {
1454                         const gchar *uid = g_ptr_array_index(indexed, i);
1455
1456                         if (match_words_message(search->folder, uid, words, ex))
1457                                 g_ptr_array_add(matches, (gchar *)uid);
1458                 }
1459
1460                 g_ptr_array_free(indexed, TRUE);
1461         } else {
1462                 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1463
1464                 for (i=0;i<v->len;i++) {
1465                         gchar *uid  = g_ptr_array_index(v, i);
1466
1467                         if (match_words_message(search->folder, uid, words, ex))
1468                                 g_ptr_array_add(matches, (gchar *)uid);
1469                 }
1470         }
1471
1472         return matches;
1473 }
1474
1475 static ESExpResult *
1476 search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1477 {
1478         gint i, j;
1479         CamelException *ex = search->priv->ex;
1480         struct _camel_search_words *words;
1481         ESExpResult *r;
1482         struct IterData lambdafoo;
1483
1484         if (search->current) {
1485                 gint truth = FALSE;
1486
1487                 if (argc == 1 && argv[0]->value.string[0] == 0) {
1488                         truth = TRUE;
1489                 } else {
1490                         for (i=0;i<argc && !truth;i++) {
1491                                 if (argv[i]->type == ESEXP_RES_STRING) {
1492                                         words = camel_search_words_split((const guchar *) argv[i]->value.string);
1493                                         truth = TRUE;
1494                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1495                                                 for (j=0;j<words->len && truth;j++)
1496                                                         truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1497                                         } else {
1498                                                 /* TODO: cache current message incase of multiple body search terms */
1499                                                 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1500                                         }
1501                                         camel_search_words_free(words);
1502                                 }
1503                         }
1504                 }
1505                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1506                 r->value.bool = truth;
1507         } else {
1508                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1509                 r->value.ptrarray = g_ptr_array_new();
1510
1511                 if (argc == 1 && argv[0]->value.string[0] == 0) {
1512                         GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1513
1514                         for (i=0;i<v->len;i++) {
1515                                 gchar *uid = g_ptr_array_index(v, i);
1516
1517                                 g_ptr_array_add(r->value.ptrarray, uid);
1518                         }
1519                 } else {
1520                         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1521                         GPtrArray *matches;
1522
1523                         for (i=0;i<argc;i++) {
1524                                 if (argv[i]->type == ESEXP_RES_STRING) {
1525                                         words = camel_search_words_split((const guchar *) argv[i]->value.string);
1526                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1527                                                 matches = match_words_index(search, words, ex);
1528                                         } else {
1529                                                 matches = match_words_messages(search, words, ex);
1530                                         }
1531                                         for (j=0;j<matches->len;j++) {
1532                                                 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1533                                         }
1534                                         g_ptr_array_free(matches, TRUE);
1535                                         camel_search_words_free(words);
1536                                 }
1537                         }
1538                         lambdafoo.uids = r->value.ptrarray;
1539                         g_hash_table_foreach(ht, (GHFunc)htor, &lambdafoo);
1540                         g_hash_table_destroy(ht);
1541                 }
1542         }
1543
1544         return r;
1545 }
1546
1547 static ESExpResult *
1548 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1549 {
1550         ESExpResult *r;
1551         CamelMimeMessage *msg = get_current_message (search);
1552
1553         if (msg) {
1554                 regex_t pattern;
1555
1556                 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1557
1558                 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1559                         r->value.bool = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1560                         regfree (&pattern);
1561                 } else
1562                         r->value.bool = FALSE;
1563
1564                 camel_object_unref (msg);
1565         } else {
1566                 regex_t pattern;
1567
1568                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1569                 r->value.ptrarray = g_ptr_array_new ();
1570
1571                 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1572                         gint i;
1573                         GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1574                         CamelException x = CAMEL_EXCEPTION_INITIALISER;
1575                         CamelMimeMessage *message;
1576
1577                         for (i = 0; i < v->len; i++) {
1578                                 gchar *uid = g_ptr_array_index(v, i);
1579
1580                                 message = camel_folder_get_message (search->folder, uid, &x);
1581                                 if (message) {
1582                                         if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1583                                                 g_ptr_array_add (r->value.ptrarray, uid);
1584                                         }
1585
1586                                         camel_object_unref ((CamelObject *)message);
1587                                 } else {
1588                                         camel_exception_clear (&x);
1589                                 }
1590                         }
1591
1592                         regfree (&pattern);
1593                 }
1594         }
1595
1596         return r;
1597 }
1598
1599 static ESExpResult *
1600 search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1601 {
1602         ESExpResult *r;
1603         gint i;
1604
1605         r(printf("executing user-flag\n"));
1606
1607         /* are we inside a match-all? */
1608         if (search->current) {
1609                 gint truth = FALSE;
1610                 /* performs an OR of all words */
1611                 for (i=0;i<argc && !truth;i++) {
1612                         if (argv[i]->type == ESEXP_RES_STRING
1613                             && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1614                                 truth = TRUE;
1615                                 break;
1616                         }
1617                 }
1618                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1619                 r->value.bool = truth;
1620         } else {
1621                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1622                 r->value.ptrarray = g_ptr_array_new();
1623         }
1624
1625         return r;
1626 }
1627
1628 static ESExpResult *
1629 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1630 {
1631         ESExpResult *r;
1632
1633         r(printf ("executing system-flag\n"));
1634
1635         if (search->current) {
1636                 gboolean truth = FALSE;
1637
1638                 if (argc == 1)
1639                         truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1640
1641                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1642                 r->value.bool = truth;
1643         } else {
1644                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1645                 r->value.ptrarray = g_ptr_array_new ();
1646         }
1647
1648         return r;
1649 }
1650
1651 static ESExpResult *
1652 search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1653 {
1654         const gchar *value = NULL;
1655         ESExpResult *r;
1656
1657         r(printf("executing user-tag\n"));
1658
1659         if (search->current && argc == 1)
1660                 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1661
1662         r = e_sexp_result_new(f, ESEXP_RES_STRING);
1663         r->value.string = g_strdup (value ? value : "");
1664
1665         return r;
1666 }
1667
1668 static ESExpResult *
1669 search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1670 {
1671         ESExpResult *r;
1672
1673         r(printf("executing get-sent-date\n"));
1674
1675         /* are we inside a match-all? */
1676         if (s->current) {
1677                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1678
1679                 r->value.number = camel_message_info_date_sent(s->current);
1680         } else {
1681                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1682                 r->value.ptrarray = g_ptr_array_new ();
1683         }
1684
1685         return r;
1686 }
1687
1688 static ESExpResult *
1689 search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1690 {
1691         ESExpResult *r;
1692
1693         r(printf("executing get-received-date\n"));
1694
1695         /* are we inside a match-all? */
1696         if (s->current) {
1697                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1698
1699                 r->value.number = camel_message_info_date_received(s->current);
1700         } else {
1701                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1702                 r->value.ptrarray = g_ptr_array_new ();
1703         }
1704
1705         return r;
1706 }
1707
1708 static ESExpResult *
1709 search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1710 {
1711         ESExpResult *r;
1712
1713         r(printf("executing get-current-date\n"));
1714
1715         r = e_sexp_result_new(f, ESEXP_RES_INT);
1716         r->value.number = time (NULL);
1717         return r;
1718 }
1719
1720 static ESExpResult *
1721 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1722 {
1723         ESExpResult *r;
1724
1725         r(printf("executing get-size\n"));
1726
1727         /* are we inside a match-all? */
1728         if (s->current) {
1729                 r = e_sexp_result_new (f, ESEXP_RES_INT);
1730                 r->value.number = camel_message_info_size(s->current) / 1024;
1731         } else {
1732                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1733                 r->value.ptrarray = g_ptr_array_new ();
1734         }
1735
1736         return r;
1737 }
1738
1739 static ESExpResult *
1740 search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1741 {
1742         ESExpResult *r;
1743         gint i;
1744
1745         r(printf("executing uid\n"));
1746
1747         /* are we inside a match-all? */
1748         if (search->current) {
1749                 gint truth = FALSE;
1750                 const gchar *uid = camel_message_info_uid(search->current);
1751
1752                 /* performs an OR of all words */
1753                 for (i=0;i<argc && !truth;i++) {
1754                         if (argv[i]->type == ESEXP_RES_STRING
1755                             && !strcmp(uid, argv[i]->value.string)) {
1756                                 truth = TRUE;
1757                                 break;
1758                         }
1759                 }
1760                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1761                 r->value.bool = truth;
1762         } else {
1763                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1764                 r->value.ptrarray = g_ptr_array_new();
1765                 for (i=0;i<argc;i++) {
1766                         if (argv[i]->type == ESEXP_RES_STRING)
1767                                 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1768                 }
1769         }
1770
1771         return r;
1772 }
1773
1774 static gint
1775 read_uid_callback (gpointer  ref, gint ncol, gchar ** cols, gchar **name)
1776 {
1777         GPtrArray *matches;
1778
1779         matches = (GPtrArray *) ref;
1780
1781         g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols [0]));
1782         return 0;
1783 }
1784
1785 static ESExpResult *
1786 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1787 {
1788         ESExpResult *r;
1789         gboolean same = FALSE;
1790
1791         if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1792                 if (argv[0]->value.string && search->folder && search->folder->parent_store && camel_folder_get_full_name (search->folder)) {
1793                         CamelFolderInfo *fi = camel_store_get_folder_info (search->folder->parent_store, camel_folder_get_full_name (search->folder), 0, NULL);
1794                         if (fi) {
1795                                 same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
1796
1797                                 camel_store_free_folder_info (search->folder->parent_store, fi);
1798                         }
1799                 }
1800         }
1801
1802         if (search->current) {
1803                 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1804                 r->value.bool = same ? TRUE : FALSE;
1805         } else {
1806                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1807                 r->value.ptrarray = g_ptr_array_new ();
1808
1809                 if (same) {
1810                         /* all matches */
1811                         gint i;
1812                         GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1813
1814                         for (i = 0; i < v->len; i++) {
1815                                 gchar *uid = g_ptr_array_index (v, i);
1816
1817                                 g_ptr_array_add (r->value.ptrarray, uid);
1818                         }
1819                 }
1820         }
1821
1822         return r;
1823 }