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