** Fix for BNC bug #418080
[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-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-store.h"
51 #include "camel-vee-folder.h"
52 #include "camel-string-utils.h"
53 #include "camel-search-sql.h"
54 #include "camel-search-sql-sexp.h"
55
56 #define d(x) 
57 #define r(x) 
58
59 struct _CamelFolderSearchPrivate {
60         CamelException *ex;
61
62         CamelFolderThread *threads;
63         GHashTable *threads_hash;
64 };
65
66 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
67
68 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
69
70 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
76 static ESExpResult *search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
77 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
79 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
80 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
81 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
82 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
83 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
84 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
85 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86
87 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
88
89 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
90 static void camel_folder_search_init       (CamelFolderSearch *obj);
91 static void camel_folder_search_finalize   (CamelObject *obj);
92
93 static int read_uid_callback (void * ref, int ncol, char ** cols, char **name);
94
95 static CamelObjectClass *camel_folder_search_parent;
96
97 static void
98 camel_folder_search_class_init (CamelFolderSearchClass *klass)
99 {
100         camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
101
102         klass->not = search_not;
103
104         klass->match_all = search_match_all;
105         klass->match_threads = search_match_threads;
106         klass->body_contains = search_body_contains;
107         klass->header_contains = search_header_contains;
108         klass->header_matches = search_header_matches;
109         klass->header_starts_with = search_header_starts_with;
110         klass->header_ends_with = search_header_ends_with;
111         klass->header_exists = search_header_exists;
112         klass->user_tag = search_user_tag;
113         klass->user_flag = search_user_flag;
114         klass->system_flag = search_system_flag;
115         klass->get_sent_date = search_get_sent_date;
116         klass->get_received_date = search_get_received_date;
117         klass->get_current_date = search_get_current_date;
118         klass->get_size = search_get_size;
119         klass->uid = search_uid;
120 }
121
122 static void
123 camel_folder_search_init (CamelFolderSearch *obj)
124 {
125         struct _CamelFolderSearchPrivate *p;
126
127         p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
128
129         obj->sexp = e_sexp_new();
130 }
131
132 static void
133 camel_folder_search_finalize (CamelObject *obj)
134 {
135         CamelFolderSearch *search = (CamelFolderSearch *)obj;
136         struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
137
138         if (search->sexp)
139                 e_sexp_unref(search->sexp);
140
141         g_free(search->last_search);
142         g_free(p);
143 }
144
145 CamelType
146 camel_folder_search_get_type (void)
147 {
148         static CamelType type = CAMEL_INVALID_TYPE;
149         
150         if (type == CAMEL_INVALID_TYPE) {
151                 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
152                                             sizeof (CamelFolderSearch),
153                                             sizeof (CamelFolderSearchClass),
154                                             (CamelObjectClassInitFunc) camel_folder_search_class_init,
155                                             NULL,
156                                             (CamelObjectInitFunc) camel_folder_search_init,
157                                             (CamelObjectFinalizeFunc) camel_folder_search_finalize);
158         }
159         
160         return type;
161 }
162
163 #ifdef offsetof
164 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) offsetof (type, field))
165 #else
166 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) ((gchar*) &((type *) 0)->field))
167 #endif
168
169 static struct {
170         char *name;
171         int offset;
172         int flags;              /* 0x02 = immediate, 0x01 = always enter */
173 } builtins[] = {
174         /* these have default implementations in e-sexp */
175         { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
176         { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
177         /* we need to override this one though to implement an 'array not' */
178         { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
179         { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
180         { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
181         { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
182
183         /* these we have to use our own default if there is none */
184         /* they should all be defined in the language? so it parses, or should they not?? */
185         { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
186         { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
187         { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
188         { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
189         { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
190         { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
191         { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
192         { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
193         { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
194         { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
195         { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
196         { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
197         { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
198         { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
199         { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
200         { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
201 };
202
203 void
204 camel_folder_search_construct (CamelFolderSearch *search)
205 {
206         int i;
207         CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
208
209         for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
210                 void *func;
211                 /* c is sure messy sometimes */
212                 func = *((void **)(((char *)klass)+builtins[i].offset));
213                 if (func == NULL && builtins[i].flags&1) {
214                         g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
215                         func = (void *)search_dummy;
216                 }
217                 if (func != NULL) {
218                         if (builtins[i].flags&2) {
219                                 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
220                         } else {
221                                 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
222                         }
223                 }
224         }
225 }
226
227 /**
228  * camel_folder_search_new:
229  *
230  * Create a new CamelFolderSearch object.
231  * 
232  * A CamelFolderSearch is a subclassable, extensible s-exp
233  * evaluator which enforces a particular set of s-expressions.
234  * Particular methods may be overriden by an implementation to
235  * implement a search for any sort of backend.
236  *
237  * Return value: A new CamelFolderSearch widget.
238  **/
239 CamelFolderSearch *
240 camel_folder_search_new (void)
241 {
242         CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
243
244         camel_folder_search_construct(new);
245         return new;
246 }
247
248 /**
249  * camel_folder_search_set_folder:
250  * @search:
251  * @folder: A folder.
252  * 
253  * Set the folder attribute of the search.  This is currently unused, but
254  * could be used to perform a slow-search when indexes and so forth are not
255  * available.  Or for use by subclasses.
256  **/
257 void
258 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
259 {
260         search->folder = folder;
261 }
262
263 /**
264  * camel_folder_search_set_summary:
265  * @search: 
266  * @summary: An array of CamelMessageInfo pointers.
267  * 
268  * Set the array of summary objects representing the span of the search.
269  *
270  * If this is not set, then a subclass must provide the functions
271  * for searching headers and for the match-all operator.
272  **/
273 void
274 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
275 {
276         search->summary = summary;
277 }
278
279 /**
280  * camel_folder_search_set_body_index:
281  * @search: 
282  * @index: 
283  * 
284  * Set the index representing the contents of all messages
285  * in this folder.  If this is not set, then the folder implementation
286  * should sub-class the CamelFolderSearch and provide its own
287  * body-contains function.
288  **/
289 void
290 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
291 {
292         if (search->body_index)
293                 camel_object_unref((CamelObject *)search->body_index);
294         search->body_index = index;
295         if (index)
296                 camel_object_ref((CamelObject *)index);
297 }
298
299 /**
300  * camel_folder_search_execute_expression:
301  * @search: 
302  * @expr: 
303  * @ex: 
304  * 
305  * Execute the search expression @expr, returning an array of
306  * all matches as a GPtrArray of uid's of matching messages.
307  *
308  * Note that any settings such as set_body_index(), set_folder(),
309  * and so on are reset to #NULL once the search has completed.
310  *
311  * TODO: The interface should probably return summary items instead
312  * (since they are much more useful to any client).
313  * 
314  * Return value: A GPtrArray of strings of all matching messages.
315  * This must only be freed by camel_folder_search_free_result.
316  **/
317 GPtrArray *
318 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
319 {
320         ESExpResult *r;
321         GPtrArray *matches;
322         int i;
323         GHashTable *results;
324         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
325
326         p->ex = ex;
327
328         /* only re-parse if the search has changed */
329         if (search->last_search == NULL
330             || strcmp(search->last_search, expr)) {
331                 e_sexp_input_text(search->sexp, expr, strlen(expr));
332                 if (e_sexp_parse(search->sexp) == -1) {
333                         camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
334                         return NULL;
335                 }
336
337                 g_free(search->last_search);
338                 search->last_search = g_strdup(expr);
339         }
340         r = e_sexp_eval(search->sexp);
341         if (r == NULL) {
342                 if (!camel_exception_is_set(ex))
343                         camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
344                 return NULL;
345         }
346
347         matches = g_ptr_array_new();
348
349         /* now create a folder summary to return?? */
350         if (r->type == ESEXP_RES_ARRAY_PTR) {
351                 d(printf("got result ...\n"));
352                 if (search->summary) {
353                         /* reorder result in summary order */
354                         results = g_hash_table_new(g_str_hash, g_str_equal);
355                         for (i=0;i<r->value.ptrarray->len;i++) {
356                                 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
357                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
358                         }
359                         for (i=0;i<search->summary->len;i++) {
360                                 char *uid = g_ptr_array_index(search->summary, i);
361                                 if (g_hash_table_lookup(results, uid)) {
362                                         g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
363                                 }
364                         }
365                         g_hash_table_destroy(results);
366                 } else {
367                         for (i=0;i<r->value.ptrarray->len;i++) {
368                                 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
369                                 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
370                         }
371                 }
372         } else {
373                 g_warning("Search returned an invalid result type");
374         }
375
376         e_sexp_result_free(search->sexp, r);
377
378         if (p->threads)
379                 camel_folder_thread_messages_unref(p->threads);
380         if (p->threads_hash)
381                 g_hash_table_destroy(p->threads_hash);
382
383         p->threads = NULL;
384         p->threads_hash = NULL;
385         search->folder = NULL;
386         search->summary = NULL;
387         search->current = NULL;
388         search->body_index = NULL;
389
390         return matches;
391 }
392
393 /**
394  * camel_folder_search_search:
395  * @search: 
396  * @expr: 
397  * @uids: to search against, NULL for all uid's.
398  * @ex: 
399  * 
400  * Run a search.  Search must have had Folder already set on it, and
401  * it must implement summaries.
402  * 
403  * Return value: 
404  **/
405 GPtrArray *
406 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
407 {
408         ESExpResult *r;
409         GPtrArray *matches = NULL, *summary_set;
410         int i;
411         CamelDB *cdb;
412         char *sql_query, *tmp, *tmp1;
413         GHashTable *results;
414
415         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
416
417         g_assert(search->folder);
418         
419         p->ex = ex;
420
421         /* We route body-contains search and uid search through memory and not via db. */
422         if (uids || strstr((const char *) expr, "body-contains")) {
423                 /* setup our search list only contains those we're interested in */
424                 search->summary = camel_folder_get_summary(search->folder);
425
426                 if (uids) {
427                         GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
428
429                         summary_set = search->summary_set = g_ptr_array_new();
430                         for (i=0;i<uids->len;i++)
431                                 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
432                         for (i=0;i<search->summary->len;i++)
433                                 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
434                                         g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
435                         g_hash_table_destroy(uids_hash);
436                 } else {
437                         summary_set = search->summary;
438                 }
439
440                 /* only re-parse if the search has changed */
441                 if (search->last_search == NULL
442                     || strcmp(search->last_search, expr)) {
443                         e_sexp_input_text(search->sexp, expr, strlen(expr));
444                         if (e_sexp_parse(search->sexp) == -1) {
445                                 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
446                                 goto fail;
447                         }
448
449                         g_free(search->last_search);
450                         search->last_search = g_strdup(expr);
451                 }
452                 r = e_sexp_eval(search->sexp);
453                 if (r == NULL) {
454                         if (!camel_exception_is_set(ex))
455                                 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
456                         goto fail;
457                 }
458
459                 matches = g_ptr_array_new();
460
461                 /* now create a folder summary to return?? */
462                 if (r->type == ESEXP_RES_ARRAY_PTR) {
463                         d(printf("got result\n"));
464         
465                         /* reorder result in summary order */
466                         results = g_hash_table_new(g_str_hash, g_str_equal);
467                         for (i=0;i<r->value.ptrarray->len;i++) {
468                                 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
469                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
470                         }
471         
472                         for (i=0;i<summary_set->len;i++) {
473                                 char *uid = g_ptr_array_index(summary_set, i);
474                                 if (g_hash_table_lookup(results, uid))
475                                         g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
476                         }
477                         g_hash_table_destroy(results);
478                 }
479
480                 e_sexp_result_free(search->sexp, r);
481
482         } else {
483                 /* Sync the db, so that we search the db for changes */
484                 camel_folder_summary_save_to_db (search->folder->summary, ex);
485         
486                 d(printf ("sexp is : [%s]\n", expr));
487                 if (g_getenv("SQL_SEARCH_OLD"))
488                         sql_query = camel_sexp_to_sql (expr);
489                 else
490                         sql_query = camel_sexp_to_sql_sexp (expr);
491                 tmp1 = camel_db_sqlize_string(search->folder->full_name);
492                 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
493                 camel_db_free_sqlized_string (tmp1);
494                 g_free (sql_query);
495                 d(printf("Equivalent sql %s\n", tmp));
496         
497                 matches = g_ptr_array_new();
498                 cdb = (CamelDB *) (search->folder->cdb);
499                 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
500                 if (ex && camel_exception_is_set(ex)) {
501                         const char *exception = camel_exception_get_description (ex);
502                         if (strncmp(exception, "no such table", 13) == 0) {
503                                 g_warning ("Error during searching %s: %s\n", tmp, exception);
504                                 camel_exception_clear (ex); /* Suppress no such table */
505                         }
506                 }
507                 g_free (tmp);
508
509         }
510
511 fail:
512         /* these might be allocated by match-threads */
513         if (p->threads)
514                 camel_folder_thread_messages_unref(p->threads);
515         if (p->threads_hash)
516                 g_hash_table_destroy(p->threads_hash);
517         if (search->summary_set)
518                 g_ptr_array_free(search->summary_set, TRUE);
519         if (search->summary)
520                 camel_folder_free_summary(search->folder, search->summary);
521
522         p->threads = NULL;
523         p->threads_hash = NULL;
524         search->folder = NULL;
525         search->summary = NULL;
526         search->summary_set = NULL;
527         search->current = NULL;
528         search->body_index = NULL;
529
530         return matches;
531 }
532
533 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
534 {
535         g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
536         g_ptr_array_free(result, TRUE);
537 }
538
539 /* dummy function, returns false always, or an empty match array */
540 static ESExpResult *
541 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
542 {
543         ESExpResult *r;
544
545         if (search->current == NULL) {
546                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
547                 r->value.bool = FALSE;
548         } else {
549                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
550                 r->value.ptrarray = g_ptr_array_new();
551         }
552
553         return r;
554 }
555
556 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
557 static ESExpResult *
558 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
559 {
560         ESExpResult *r;
561         int i;
562
563         if (argc>0) {
564                 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
565                         GPtrArray *v = argv[0]->value.ptrarray;
566                         const char *uid;
567
568                         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
569                         r->value.ptrarray = g_ptr_array_new();
570
571                         /* not against a single message?*/
572                         if (search->current) {
573                                 int found = FALSE;
574
575                                 uid = camel_message_info_uid(search->current);
576                                 for (i=0;!found && i<v->len;i++) {
577                                         if (strcmp(uid, v->pdata[i]) == 0)
578                                                 found = TRUE;
579                                 }
580
581                                 if (!found)
582                                         g_ptr_array_add(r->value.ptrarray, (char *)uid);
583                         } else if (search->summary == NULL) {
584                                 g_warning("No summary set, 'not' against an array requires a summary");
585                         } else {
586                                 /* 'not' against the whole summary */
587                                 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
588                                 char **s;
589                                 char **m;
590
591                                 s = (char **)v->pdata;
592                                 for (i=0;i<v->len;i++)
593                                         g_hash_table_insert(have, s[i], s[i]);
594
595                                 v = search->summary_set?search->summary_set:search->summary;
596                                 m = (char **)v->pdata;
597                                 for (i=0;i<v->len;i++) {
598                                         char *uid = m[i];
599
600                                         if (g_hash_table_lookup(have, uid) == NULL)
601                                                 g_ptr_array_add(r->value.ptrarray, uid);
602                                 }
603                                 g_hash_table_destroy(have);
604                         }
605                 } else {
606                         int res = TRUE;
607
608                         if (argv[0]->type == ESEXP_RES_BOOL)
609                                 res = ! argv[0]->value.bool;
610
611                         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
612                         r->value.bool = res;
613                 }
614         } else {
615                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
616                 r->value.bool = TRUE;
617         }
618
619         return r;
620 }
621
622 static ESExpResult *
623 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
624 {
625         int i;
626         ESExpResult *r, *r1;
627         gchar *error_msg;
628         GPtrArray *v;
629
630         if (argc>1) {
631                 g_warning("match-all only takes a single argument, other arguments ignored");
632         }
633
634         /* we are only matching a single message?  or already inside a match-all? */
635         if (search->current) {
636                 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
637
638                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
639                 r->value.bool = FALSE;
640
641                 if (argc>0) {
642                         r1 = e_sexp_term_eval(f, argv[0]);
643                         if (r1->type == ESEXP_RES_BOOL) {
644                                 r->value.bool = r1->value.bool;
645                         } else {
646                                 g_warning("invalid syntax, matches require a single bool result");
647                                 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
648                                 e_sexp_fatal_error(f, error_msg);
649                                 g_free(error_msg);
650                         }
651                         e_sexp_result_free(f, r1);
652                 } else {
653                         r->value.bool = TRUE;
654                 }
655                 return r;
656         }
657
658         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
659         r->value.ptrarray = g_ptr_array_new();
660
661         if (search->summary == NULL) {
662                 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
663                 g_warning("No summary supplied, match-all doesn't work with no summary");
664                 g_assert(0);
665                 return r;
666         }
667
668         v = search->summary_set?search->summary_set:search->summary;
669         
670         if (v->len > g_hash_table_size (search->folder->summary->loaded_infos) && !CAMEL_IS_VEE_FOLDER (search->folder)) {
671                 camel_folder_summary_reload_from_db (search->folder->summary, search->priv->ex);
672         } 
673
674         e_sexp_term_eval (f, argv [0]);
675
676         for (i=0;i<v->len;i++) {
677                 const char *uid;
678
679                 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
680                 if (!search->current)
681                         continue;
682                 uid = camel_message_info_uid(search->current);
683
684                 if (argc>0) {
685                         r1 = e_sexp_term_eval(f, argv[0]);
686                         if (r1->type == ESEXP_RES_BOOL) {
687                                 if (r1->value.bool)
688                                         g_ptr_array_add(r->value.ptrarray, (char *)uid);
689                         } else {
690                                 g_warning("invalid syntax, matches require a single bool result");
691                                 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
692                                 e_sexp_fatal_error(f, error_msg);
693                                 g_free(error_msg);
694                         }
695                         e_sexp_result_free(f, r1);
696                 } else {
697                         g_ptr_array_add(r->value.ptrarray, (char *)uid);
698                 }
699                 camel_message_info_free (search->current);
700         }
701         search->current = NULL;
702         return r;
703 }
704
705 static void
706 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
707 {
708         while (root) {
709                 g_hash_table_insert(id_hash, (char *)camel_message_info_uid(root->message), root);
710                 if (root->child)
711                         fill_thread_table(root->child, id_hash);
712                 root = root->next;
713         }
714 }
715
716 static void
717 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
718 {
719         while (root) {
720                 g_hash_table_insert(result_hash, (char *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
721                 if (root->child)
722                         add_thread_results(root->child, result_hash);
723                 root = root->next;
724         }
725 }
726
727 static void
728 add_results(char *uid, void *dummy, GPtrArray *result)
729 {
730         g_ptr_array_add(result, uid);
731 }
732
733 static ESExpResult *
734 search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
735 {
736         ESExpResult *r;
737         struct _CamelFolderSearchPrivate *p = search->priv;
738         int i, type;
739         GHashTable *results;
740         gchar *error_msg;
741
742         /* not supported in match-all */
743         if (search->current) {
744                 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
745                 e_sexp_fatal_error(f, error_msg);
746                 g_free(error_msg);
747         }
748
749         if (argc == 0) {
750                 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
751                 e_sexp_fatal_error(f, error_msg);
752                 g_free(error_msg);
753         }
754
755         r = e_sexp_term_eval(f, argv[0]);
756         if (r->type != ESEXP_RES_STRING) {
757                 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
758                 e_sexp_fatal_error(f, error_msg);
759                 g_free(error_msg);
760         }
761
762         type = 0;
763         if (!strcmp(r->value.string, "none"))
764                 type = 0;
765         else if (!strcmp(r->value.string, "all"))
766                 type = 1;
767         else if (!strcmp(r->value.string, "replies"))
768                 type = 2;
769         else if (!strcmp(r->value.string, "replies_parents"))
770                 type = 3;
771         else if (!strcmp(r->value.string, "single"))
772                 type = 4;
773         e_sexp_result_free(f, r);
774
775         /* behave as (begin does */
776         r = NULL;
777         for (i=1;i<argc;i++) {
778                 if (r)
779                         e_sexp_result_free(f, r);
780                 r = e_sexp_term_eval(f, argv[i]);
781         }
782
783         if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
784                 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
785                 e_sexp_fatal_error(f, error_msg);
786                 g_free(error_msg);
787         }
788
789         if (type == 0)
790                 return r;
791
792         if (search->folder == NULL) {
793                 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
794                 e_sexp_fatal_error(f, error_msg);
795                 g_free(error_msg);
796         }
797
798         /* cache this, so we only have to re-calculate once per search at most */
799         if (p->threads == NULL) {
800                 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
801                 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
802
803                 fill_thread_table(p->threads->tree, p->threads_hash);
804         }
805
806         results = g_hash_table_new(g_str_hash, g_str_equal);
807         for (i=0;i<r->value.ptrarray->len;i++) {
808                 struct _CamelFolderThreadNode *node, *scan;
809
810                 if (type != 4)
811                         g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
812
813                 node = g_hash_table_lookup(p->threads_hash, (char *)g_ptr_array_index(r->value.ptrarray, i));
814                 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
815                         continue;
816
817                 /* select messages in thread according to search criteria */
818                 if (type == 4) {
819                         if (node->child == NULL && node->parent == NULL)
820                                 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
821                 } else {
822                         if (type == 3) {
823                                 scan = node;
824                                 while (scan && scan->parent) {
825                                         scan = scan->parent;
826                                         g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
827                                 }
828                         } else if (type == 1) {
829                                 while (node && node->parent)
830                                         node = node->parent;
831                         }
832                         g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
833                         if (node->child)
834                                 add_thread_results(node->child, results);
835                 }
836         }
837         e_sexp_result_free(f, r);
838
839         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
840         r->value.ptrarray = g_ptr_array_new();
841
842         g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
843         g_hash_table_destroy(results);
844
845         return r;
846 }
847
848 static ESExpResult *
849 check_header (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
850 {
851         ESExpResult *r;
852         int truth = FALSE;
853
854         r(printf("executing check-header %d\n", how));
855
856         /* are we inside a match-all? */
857         if (search->current && argc>1
858             && argv[0]->type == ESEXP_RES_STRING) {
859                 char *headername;
860                 const char *header = NULL;
861                 char strbuf[32];
862                 int i, j;
863                 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
864                 struct _camel_search_words *words;
865
866                 /* only a subset of headers are supported .. */
867                 headername = argv[0]->value.string;
868                 if (!g_ascii_strcasecmp(headername, "subject")) {
869                         header = camel_message_info_subject(search->current);
870                 } else if (!g_ascii_strcasecmp(headername, "date")) {
871                         /* FIXME: not a very useful form of the date */
872                         sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
873                         header = strbuf;
874                 } else if (!g_ascii_strcasecmp(headername, "from")) {
875                         header = camel_message_info_from(search->current);
876                         type = CAMEL_SEARCH_TYPE_ADDRESS;
877                 } else if (!g_ascii_strcasecmp(headername, "to")) {
878                         header = camel_message_info_to(search->current);
879                         type = CAMEL_SEARCH_TYPE_ADDRESS;
880                 } else if (!g_ascii_strcasecmp(headername, "cc")) {
881                         header = camel_message_info_cc(search->current);
882                         type = CAMEL_SEARCH_TYPE_ADDRESS;
883                 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
884                         header = camel_message_info_mlist(search->current);
885                         type = CAMEL_SEARCH_TYPE_MLIST;
886                 } else {
887                         e_sexp_resultv_free(f, argc, argv);
888                         e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
889                 }
890
891                 if (header == NULL)
892                         header = "";
893
894                 /* performs an OR of all words */
895                 for (i=1;i<argc && !truth;i++) {
896                         if (argv[i]->type == ESEXP_RES_STRING) {
897                                 if (argv[i]->value.string[0] == 0) {
898                                         truth = TRUE;
899                                 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
900                                         /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
901                                         words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
902                                         truth = TRUE;
903                                         for (j=0;j<words->len && truth;j++) {
904                                                 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
905                                         }
906                                         camel_search_words_free(words);
907                                 } else {
908                                         truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
909                                 }
910                         }
911                 }
912         }
913         /* TODO: else, find all matches */
914
915         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
916         r->value.bool = truth;
917
918         return r;
919 }
920
921 /*
922 static void 
923 l_printf(char *node)
924 {
925 printf("%s\t", node);
926 }
927 */
928
929 static ESExpResult *
930 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
931 {
932         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
933 }
934
935 static ESExpResult *
936 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
937 {
938         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
939 }
940
941 static ESExpResult *
942 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
943 {
944         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
945 }
946
947 static ESExpResult *
948 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
949 {
950         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
951 }
952
953 static ESExpResult *
954 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
955 {
956         ESExpResult *r;
957         
958         r(printf ("executing header-exists\n"));
959         
960         if (search->current) {
961                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
962                 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
963                         r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
964                 
965         } else {
966                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
967                 r->value.ptrarray = g_ptr_array_new();
968         }
969         
970         return r;
971 }
972
973 /* this is just to OR results together */
974 struct _glib_sux_donkeys {
975         int count;
976         GPtrArray *uids;
977 };
978
979 /* or, store all unique values */
980 static void
981 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
982 {
983         g_ptr_array_add(fuckup->uids, key);
984 }
985
986 /* and, only store duplicates */
987 static void
988 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
989 {
990         if (value == fuckup->count)
991                 g_ptr_array_add(fuckup->uids, key);
992 }
993
994 static int
995 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
996 {
997         CamelIndexCursor *wc, *nc;
998         const char *word, *name;
999         int truth = FALSE;
1000
1001         wc = camel_index_words(idx);
1002         if (wc) {
1003                 while (!truth && (word = camel_index_cursor_next(wc))) {
1004                         if (camel_ustrstrcase(word,match) != NULL) {
1005                                 /* perf: could have the wc cursor return the name cursor */
1006                                 nc = camel_index_find(idx, word);
1007                                 if (nc) {
1008                                         while (!truth && (name = camel_index_cursor_next(nc)))
1009                                                 truth = strcmp(name, uid) == 0;
1010                                         camel_object_unref((CamelObject *)nc);
1011                                 }
1012                         }
1013                 }
1014                 camel_object_unref((CamelObject *)wc);
1015         }
1016
1017         return truth;
1018 }
1019
1020 /*
1021  "one two" "three" "four five"
1022
1023   one and two
1024 or
1025   three
1026 or
1027   four and five
1028 */
1029
1030 /* returns messages which contain all words listed in words */
1031 static GPtrArray *
1032 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1033 {
1034         GPtrArray *result = g_ptr_array_new();
1035         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1036         struct _glib_sux_donkeys lambdafoo;
1037         CamelIndexCursor *wc, *nc;
1038         const char *word, *name;
1039         int i;
1040
1041         /* we can have a maximum of 32 words, as we use it as the AND mask */
1042                         
1043         wc = camel_index_words(search->body_index);
1044         if (wc) {
1045                 while ((word = camel_index_cursor_next(wc))) {
1046                         for (i=0;i<words->len;i++) {
1047                                 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1048                                         /* perf: could have the wc cursor return the name cursor */
1049                                         nc = camel_index_find(search->body_index, word);
1050                                         if (nc) {
1051                                                 while ((name = camel_index_cursor_next(nc))) {
1052                                                                 int mask;
1053
1054                                                                 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1055                                                                 g_hash_table_insert(ht, (char *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1056                                                 }
1057                                                 camel_object_unref((CamelObject *)nc);
1058                                         }
1059                                 }
1060                         }
1061                 }
1062                 camel_object_unref((CamelObject *)wc);
1063
1064                 lambdafoo.uids = result;
1065                 lambdafoo.count = (1<<words->len) - 1;
1066                 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
1067                 g_hash_table_destroy(ht);
1068         }
1069
1070         return result;
1071 }
1072
1073 static gboolean
1074 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1075 {
1076         CamelDataWrapper *containee;
1077         int truth = FALSE;
1078         int parts, i;
1079         
1080         containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1081         
1082         if (containee == NULL)
1083                 return FALSE;
1084         
1085         /* using the object types is more accurate than using the mime/types */
1086         if (CAMEL_IS_MULTIPART (containee)) {
1087                 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1088                 for (i = 0; i < parts && truth == FALSE; i++) {
1089                         CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1090                         if (part)
1091                                 truth = match_words_1message(part, words, mask);
1092                 }
1093         } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1094                 /* for messages we only look at its contents */
1095                 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1096         } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1097                 /* for all other text parts, we look inside, otherwise we dont care */
1098                 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1099
1100                 /* FIXME: The match should be part of a stream op */
1101                 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1102                 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1103                 for (i=0;i<words->len;i++) {
1104                         /* FIXME: This is horridly slow, and should use a real search algorithm */
1105                         if (camel_ustrstrcase((const char *) mem->buffer->data, words->words[i]->word) != NULL) {
1106                                 *mask |= (1<<i);
1107                                 /* shortcut a match */
1108                                 if (*mask == (1<<(words->len))-1)
1109                                         return TRUE;
1110                         }
1111                 }
1112                 
1113                 camel_object_unref (mem);
1114         }
1115         
1116         return truth;
1117 }
1118
1119 static gboolean
1120 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1121 {
1122         guint32 mask;
1123         CamelMimeMessage *msg;
1124         CamelException x = CAMEL_EXCEPTION_INITIALISER;
1125         int truth;
1126
1127         msg = camel_folder_get_message(folder, uid, &x);
1128         if (msg) {
1129                 mask = 0;
1130                 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1131                 camel_object_unref((CamelObject *)msg);
1132         } else {
1133                 camel_exception_clear(&x);
1134                 truth = FALSE;
1135         }
1136
1137         return truth;
1138 }
1139
1140 static GPtrArray *
1141 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1142 {
1143         int i;
1144         GPtrArray *matches = g_ptr_array_new();
1145
1146         if (search->body_index) {
1147                 GPtrArray *indexed;
1148                 struct _camel_search_words *simple;
1149
1150                 simple = camel_search_words_simple(words);
1151                 indexed = match_words_index(search, simple, ex);
1152                 camel_search_words_free(simple);
1153
1154                 for (i=0;i<indexed->len;i++) {
1155                         const char *uid = g_ptr_array_index(indexed, i);
1156                         
1157                         if (match_words_message(search->folder, uid, words, ex))
1158                                 g_ptr_array_add(matches, (char *)uid);
1159                 }
1160                 
1161                 g_ptr_array_free(indexed, TRUE);
1162         } else {
1163                 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1164
1165                 for (i=0;i<v->len;i++) {
1166                         char *uid  = g_ptr_array_index(v, i);
1167                         
1168                         if (match_words_message(search->folder, uid, words, ex))
1169                                 g_ptr_array_add(matches, (char *)uid);
1170                 }
1171         }
1172
1173         return matches;
1174 }
1175
1176 static ESExpResult *
1177 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1178 {
1179         int i, j;
1180         CamelException *ex = search->priv->ex;
1181         struct _camel_search_words *words;
1182         ESExpResult *r;
1183         struct _glib_sux_donkeys lambdafoo;
1184
1185         if (search->current) {  
1186                 int truth = FALSE;
1187
1188                 if (argc == 1 && argv[0]->value.string[0] == 0) {
1189                         truth = TRUE;
1190                 } else {
1191                         for (i=0;i<argc && !truth;i++) {
1192                                 if (argv[i]->type == ESEXP_RES_STRING) {
1193                                         words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1194                                         truth = TRUE;
1195                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1196                                                 for (j=0;j<words->len && truth;j++)
1197                                                         truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1198                                         } else {
1199                                                 /* TODO: cache current message incase of multiple body search terms */
1200                                                 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1201                                         }
1202                                         camel_search_words_free(words);
1203                                 }
1204                         }
1205                 }
1206                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1207                 r->value.bool = truth;
1208         } else {
1209                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1210                 r->value.ptrarray = g_ptr_array_new();
1211
1212                 if (argc == 1 && argv[0]->value.string[0] == 0) {
1213                         GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1214
1215                         for (i=0;i<v->len;i++) {
1216                                 char *uid = g_ptr_array_index(v, i);
1217
1218                                 g_ptr_array_add(r->value.ptrarray, uid);
1219                         }
1220                 } else {
1221                         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1222                         GPtrArray *matches;
1223
1224                         for (i=0;i<argc;i++) {
1225                                 if (argv[i]->type == ESEXP_RES_STRING) {
1226                                         words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1227                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1228                                                 matches = match_words_index(search, words, ex);
1229                                         } else {
1230                                                 matches = match_words_messages(search, words, ex);
1231                                         }
1232                                         for (j=0;j<matches->len;j++) {
1233                                                 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1234                                         }
1235                                         g_ptr_array_free(matches, TRUE);
1236                                         camel_search_words_free(words);
1237                                 }
1238                         }
1239                         lambdafoo.uids = r->value.ptrarray;
1240                         g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1241                         g_hash_table_destroy(ht);
1242                 }
1243         }
1244         
1245         return r;
1246 }
1247
1248 static ESExpResult *
1249 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1250 {
1251         ESExpResult *r;
1252         int i;
1253
1254         r(printf("executing user-flag\n"));
1255
1256         /* are we inside a match-all? */
1257         if (search->current) {
1258                 int truth = FALSE;
1259                 /* performs an OR of all words */
1260                 for (i=0;i<argc && !truth;i++) {
1261                         if (argv[i]->type == ESEXP_RES_STRING
1262                             && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1263                                 truth = TRUE;
1264                                 break;
1265                         }
1266                 }
1267                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1268                 r->value.bool = truth;
1269         } else {
1270                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1271                 r->value.ptrarray = g_ptr_array_new();
1272         }
1273
1274         return r;
1275 }
1276
1277 static ESExpResult *
1278 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1279 {
1280         ESExpResult *r;
1281         
1282         r(printf ("executing system-flag\n"));
1283         
1284         if (search->current) {
1285                 gboolean truth = FALSE;
1286                 
1287                 if (argc == 1)
1288                         truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1289                 
1290                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1291                 r->value.bool = truth;
1292         } else {
1293                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1294                 r->value.ptrarray = g_ptr_array_new ();
1295         }
1296         
1297         return r;
1298 }
1299
1300 static ESExpResult *
1301 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1302 {
1303         const char *value = NULL;
1304         ESExpResult *r;
1305         
1306         r(printf("executing user-tag\n"));
1307         
1308         if (search->current && argc == 1)
1309                 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1310         
1311         r = e_sexp_result_new(f, ESEXP_RES_STRING);
1312         r->value.string = g_strdup (value ? value : "");
1313         
1314         return r;
1315 }
1316
1317 static ESExpResult *
1318 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1319 {
1320         ESExpResult *r;
1321
1322         r(printf("executing get-sent-date\n"));
1323
1324         /* are we inside a match-all? */
1325         if (s->current) {
1326                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1327
1328                 r->value.number = camel_message_info_date_sent(s->current);
1329         } else {
1330                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1331                 r->value.ptrarray = g_ptr_array_new ();
1332         }
1333
1334         return r;
1335 }
1336
1337 static ESExpResult *
1338 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1339 {
1340         ESExpResult *r;
1341
1342         r(printf("executing get-received-date\n"));
1343
1344         /* are we inside a match-all? */
1345         if (s->current) {
1346                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1347
1348                 r->value.number = camel_message_info_date_received(s->current);
1349         } else {
1350                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1351                 r->value.ptrarray = g_ptr_array_new ();
1352         }
1353
1354         return r;
1355 }
1356
1357 static ESExpResult *
1358 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1359 {
1360         ESExpResult *r;
1361
1362         r(printf("executing get-current-date\n"));
1363
1364         r = e_sexp_result_new(f, ESEXP_RES_INT);
1365         r->value.number = time (NULL);
1366         return r;
1367 }
1368
1369 static ESExpResult *
1370 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1371 {
1372         ESExpResult *r;
1373         
1374         r(printf("executing get-size\n"));
1375         
1376         /* are we inside a match-all? */
1377         if (s->current) {
1378                 r = e_sexp_result_new (f, ESEXP_RES_INT);
1379                 r->value.number = camel_message_info_size(s->current) / 1024;
1380         } else {
1381                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1382                 r->value.ptrarray = g_ptr_array_new ();
1383         }
1384         
1385         return r;
1386 }
1387
1388 static ESExpResult *
1389 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1390 {
1391         ESExpResult *r;
1392         int i;
1393
1394         r(printf("executing uid\n"));
1395
1396         /* are we inside a match-all? */
1397         if (search->current) {
1398                 int truth = FALSE;
1399                 const char *uid = camel_message_info_uid(search->current);
1400
1401                 /* performs an OR of all words */
1402                 for (i=0;i<argc && !truth;i++) {
1403                         if (argv[i]->type == ESEXP_RES_STRING
1404                             && !strcmp(uid, argv[i]->value.string)) {
1405                                 truth = TRUE;
1406                                 break;
1407                         }
1408                 }
1409                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1410                 r->value.bool = truth;
1411         } else {
1412                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1413                 r->value.ptrarray = g_ptr_array_new();
1414                 for (i=0;i<argc;i++) {
1415                         if (argv[i]->type == ESEXP_RES_STRING)
1416                                 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1417                 }
1418         }
1419
1420         return r;
1421 }
1422
1423 static int 
1424 read_uid_callback (void * ref, int ncol, char ** cols, char **name)
1425 {
1426         GPtrArray *matches;
1427
1428         matches = (GPtrArray *) ref;
1429
1430         g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols [0]));
1431         return 0;
1432 }