s/HSCAN_/CAMEL_MIME_PARSER_STATE_/g and
[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) 2000-2003 Ximian Inc.
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 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 General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <regex.h>
35
36 #include <glib.h>
37
38 #include "camel-folder-search.h"
39
40 #include "camel-exception.h"
41 #include "camel-medium.h"
42 #include "camel-multipart.h"
43 #include "camel-mime-message.h"
44 #include "camel-stream-mem.h"
45 #include "e-util/e-memory.h"
46 #include "camel-search-private.h"
47
48 #define d(x) 
49 #define r(x) 
50
51 struct _CamelFolderSearchPrivate {
52         GHashTable *mempool_hash;
53         CamelException *ex;
54 };
55
56 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
57
58 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
59
60 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
61 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
62 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
63 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
64 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
65 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
66 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
67 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
68 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
69 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
70 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
71 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
72 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
73 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
74 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
75
76 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77
78 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
79 static void camel_folder_search_init       (CamelFolderSearch *obj);
80 static void camel_folder_search_finalize   (CamelObject *obj);
81
82 static CamelObjectClass *camel_folder_search_parent;
83
84 static void
85 camel_folder_search_class_init (CamelFolderSearchClass *klass)
86 {
87         camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
88
89         klass->not = search_not;
90
91         klass->match_all = search_match_all;
92         klass->body_contains = search_body_contains;
93         klass->header_contains = search_header_contains;
94         klass->header_matches = search_header_matches;
95         klass->header_starts_with = search_header_starts_with;
96         klass->header_ends_with = search_header_ends_with;
97         klass->header_exists = search_header_exists;
98         klass->user_tag = search_user_tag;
99         klass->user_flag = search_user_flag;
100         klass->system_flag = search_system_flag;
101         klass->get_sent_date = search_get_sent_date;
102         klass->get_received_date = search_get_received_date;
103         klass->get_current_date = search_get_current_date;
104         klass->get_size = search_get_size;
105         klass->uid = search_uid;
106 }
107
108 static void
109 camel_folder_search_init (CamelFolderSearch *obj)
110 {
111         struct _CamelFolderSearchPrivate *p;
112
113         p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
114
115         obj->sexp = e_sexp_new();
116
117         /* use a hash of mempools to associate the returned uid lists with
118            the backing mempool.  yes pretty weird, but i didn't want to change
119            the api just yet */
120
121         p->mempool_hash = g_hash_table_new(0, 0);
122 }
123
124 static void
125 free_mempool(void *key, void *value, void *data)
126 {
127         GPtrArray *uids = key;
128         EMemPool *pool = value;
129
130         g_warning("Search closed with outstanding result unfreed: %p", uids);
131
132         g_ptr_array_free(uids, TRUE);
133         e_mempool_destroy(pool);
134 }
135
136 static void
137 camel_folder_search_finalize (CamelObject *obj)
138 {
139         CamelFolderSearch *search = (CamelFolderSearch *)obj;
140         struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
141
142         if (search->sexp)
143                 e_sexp_unref(search->sexp);
144         if (search->summary_hash)
145                 g_hash_table_destroy(search->summary_hash);
146
147         g_free(search->last_search);
148         g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
149         g_hash_table_destroy(p->mempool_hash);
150         g_free(p);
151 }
152
153 CamelType
154 camel_folder_search_get_type (void)
155 {
156         static CamelType type = CAMEL_INVALID_TYPE;
157         
158         if (type == CAMEL_INVALID_TYPE) {
159                 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
160                                             sizeof (CamelFolderSearch),
161                                             sizeof (CamelFolderSearchClass),
162                                             (CamelObjectClassInitFunc) camel_folder_search_class_init,
163                                             NULL,
164                                             (CamelObjectInitFunc) camel_folder_search_init,
165                                             (CamelObjectFinalizeFunc) camel_folder_search_finalize);
166         }
167         
168         return type;
169 }
170
171 #ifdef offsetof
172 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) offsetof (type, field))
173 #else
174 #define CAMEL_STRUCT_OFFSET(type, field)        ((gint) ((gchar*) &((type *) 0)->field))
175 #endif
176
177 struct {
178         char *name;
179         int offset;
180         int flags;              /* 0x02 = immediate, 0x01 = always enter */
181 } builtins[] = {
182         /* these have default implementations in e-sexp */
183         { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
184         { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
185         /* we need to override this one though to implement an 'array not' */
186         { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
187         { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
188         { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
189         { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
190
191         /* these we have to use our own default if there is none */
192         /* they should all be defined in the language? so it parses, or should they not?? */
193         { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
194         { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
195         { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
196         { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
197         { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
198         { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
199         { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
200         { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
201         { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
202         { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
203         { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
204         { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
205         { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
206         { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
207         { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
208 };
209
210 void
211 camel_folder_search_construct (CamelFolderSearch *search)
212 {
213         int i;
214         CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
215
216         for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
217                 void *func;
218                 /* c is sure messy sometimes */
219                 func = *((void **)(((char *)klass)+builtins[i].offset));
220                 if (func == NULL && builtins[i].flags&1) {
221                         g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
222                         func = (void *)search_dummy;
223                 }
224                 if (func != NULL) {
225                         if (builtins[i].flags&2) {
226                                 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
227                         } else {
228                                 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
229                         }
230                 }
231         }
232 }
233
234 /**
235  * camel_folder_search_new:
236  *
237  * Create a new CamelFolderSearch object.
238  * 
239  * A CamelFolderSearch is a subclassable, extensible s-exp
240  * evaluator which enforces a particular set of s-expressions.
241  * Particular methods may be overriden by an implementation to
242  * implement a search for any sort of backend.
243  *
244  * Return value: A new CamelFolderSearch widget.
245  **/
246 CamelFolderSearch *
247 camel_folder_search_new (void)
248 {
249         CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
250
251         camel_folder_search_construct(new);
252         return new;
253 }
254
255 /**
256  * camel_folder_search_set_folder:
257  * @search:
258  * @folder: A folder.
259  * 
260  * Set the folder attribute of the search.  This is currently unused, but
261  * could be used to perform a slow-search when indexes and so forth are not
262  * available.  Or for use by subclasses.
263  **/
264 void
265 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
266 {
267         search->folder = folder;
268 }
269
270 /**
271  * camel_folder_search_set_summary:
272  * @search: 
273  * @summary: An array of CamelMessageInfo pointers.
274  * 
275  * Set the array of summary objects representing the span of the search.
276  *
277  * If this is not set, then a subclass must provide the functions
278  * for searching headers and for the match-all operator.
279  **/
280 void
281 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
282 {
283         int i;
284
285         search->summary = summary;
286         if (search->summary_hash)
287                 g_hash_table_destroy(search->summary_hash);
288         search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
289         for (i=0;i<summary->len;i++)
290                 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary->pdata[i]), summary->pdata[i]);
291 }
292
293 /**
294  * camel_folder_search_set_body_index:
295  * @search: 
296  * @index: 
297  * 
298  * Set the index representing the contents of all messages
299  * in this folder.  If this is not set, then the folder implementation
300  * should sub-class the CamelFolderSearch and provide its own
301  * body-contains function.
302  **/
303 void
304 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
305 {
306         if (search->body_index)
307                 camel_object_unref((CamelObject *)search->body_index);
308         search->body_index = index;
309         if (index)
310                 camel_object_ref((CamelObject *)index);
311 }
312
313 /**
314  * camel_folder_search_execute_expression:
315  * @search: 
316  * @expr: 
317  * @ex: 
318  * 
319  * Execute the search expression @expr, returning an array of
320  * all matches as a GPtrArray of uid's of matching messages.
321  *
322  * Note that any settings such as set_body_index(), set_folder(),
323  * and so on are reset to #NULL once the search has completed.
324  *
325  * TODO: The interface should probably return summary items instead
326  * (since they are much more useful to any client).
327  * 
328  * Return value: A GPtrArray of strings of all matching messages.
329  * This must only be freed by camel_folder_search_free_result.
330  **/
331 GPtrArray *
332 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
333 {
334         ESExpResult *r;
335         GPtrArray *matches;
336         int i;
337         GHashTable *results;
338         EMemPool *pool;
339         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
340
341         p->ex = ex;
342
343         /* only re-parse if the search has changed */
344         if (search->last_search == NULL
345             || strcmp(search->last_search, expr)) {
346                 e_sexp_input_text(search->sexp, expr, strlen(expr));
347                 if (e_sexp_parse(search->sexp) == -1) {
348                         camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
349                         return NULL;
350                 }
351
352                 g_free(search->last_search);
353                 search->last_search = g_strdup(expr);
354         }
355         r = e_sexp_eval(search->sexp);
356         if (r == NULL) {
357                 if (!camel_exception_is_set(ex))
358                         camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
359                 return NULL;
360         }
361
362         matches = g_ptr_array_new();
363
364         /* now create a folder summary to return?? */
365         if (r->type == ESEXP_RES_ARRAY_PTR) {
366                 d(printf("got result ...\n"));
367                 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
368                 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
369                    of memory just storing the size tag that malloc assigns us and alignment padding, so this
370                    gets around that (and is faster to allocate and free as a bonus) */
371                 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
372                 if (search->summary) {
373                         /* reorder result in summary order */
374                         results = g_hash_table_new(g_str_hash, g_str_equal);
375                         for (i=0;i<r->value.ptrarray->len;i++) {
376                                 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
377                                 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), (void *)1);
378                         }
379                         for (i=0;i<search->summary->len;i++) {
380                                 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
381                                 char *uid = (char *)camel_message_info_uid(info);
382                                 if (g_hash_table_lookup(results, uid)) {
383                                         g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
384                                 }
385                         }
386                         g_hash_table_destroy(results);
387                 } else {
388                         for (i=0;i<r->value.ptrarray->len;i++) {
389                                 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
390                                 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
391                         }
392                 }
393                 /* instead of putting the mempool_hash in the structure, we keep the api clean by
394                    putting a reference to it in a hashtable.  Lets us do some debugging and catch
395                    unfree'd results as well. */
396                 g_hash_table_insert(p->mempool_hash, matches, pool);
397         } else {
398                 g_warning("Search returned an invalid result type");
399         }
400
401         e_sexp_result_free(search->sexp, r);
402
403         search->folder = NULL;
404         search->summary = NULL;
405         search->current = NULL;
406         search->body_index = NULL;
407
408         return matches;
409 }
410
411 /**
412  * camel_folder_search_match_expression:
413  * @search: 
414  * @expr: 
415  * @info: 
416  * @ex: 
417  * 
418  * Returns #TRUE if the expression matches the specific message info @info.
419  * Note that the folder and index may need to be set for body searches to
420  * operate as well.
421  * 
422  * Return value: 
423  **/
424 gboolean
425 camel_folder_search_match_expression(CamelFolderSearch *search, const char *expr, const CamelMessageInfo *info, CamelException *ex)
426 {
427         GPtrArray *uids;
428         int ret = FALSE;
429
430         search->current = (CamelMessageInfo *)info;
431
432         uids = camel_folder_search_execute_expression(search, expr, ex);
433         if (uids) {
434                 if (uids->len == 1)
435                         ret = TRUE;
436                 camel_folder_search_free_result(search, uids);
437         }
438         search->current = NULL;
439
440         return ret;
441 }
442
443 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
444 {
445         int i;
446         struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
447         EMemPool *pool;
448
449         pool = g_hash_table_lookup(p->mempool_hash, result);
450         if (pool) {
451                 e_mempool_destroy(pool);
452                 g_hash_table_remove(p->mempool_hash, result);
453         } else {
454                 for (i=0;i<result->len;i++)
455                         g_free(g_ptr_array_index(result, i));
456         }
457         g_ptr_array_free(result, TRUE);
458 }
459
460
461
462
463 /* dummy function, returns false always, or an empty match array */
464 static ESExpResult *
465 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
466 {
467         ESExpResult *r;
468
469         if (search->current == NULL) {
470                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
471                 r->value.bool = FALSE;
472         } else {
473                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
474                 r->value.ptrarray = g_ptr_array_new();
475         }
476
477         return r;
478 }
479
480 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
481 static ESExpResult *
482 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
483 {
484         ESExpResult *r;
485         int i;
486
487         if (argc>0) {
488                 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
489                         GPtrArray *v = argv[0]->value.ptrarray;
490                         const char *uid;
491
492                         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
493                         r->value.ptrarray = g_ptr_array_new();
494
495                         /* not against a single message?*/
496                         if (search->current) {
497                                 int found = FALSE;
498
499                                 uid = camel_message_info_uid(search->current);
500                                 for (i=0;!found && i<v->len;i++) {
501                                         if (strcmp(uid, v->pdata[i]) == 0)
502                                                 found = TRUE;
503                                 }
504
505                                 if (!found)
506                                         g_ptr_array_add(r->value.ptrarray, (char *)uid);
507                         } else if (search->summary == NULL) {
508                                 g_warning("No summary set, 'not' against an array requires a summary");
509                         } else {
510                                 /* 'not' against the whole summary */
511                                 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
512                                 char **s;
513                                 CamelMessageInfo **m;
514
515                                 s = (char **)v->pdata;
516                                 for (i=0;i<v->len;i++)
517                                         g_hash_table_insert(have, s[i], s[i]);
518
519                                 v = search->summary;
520                                 m = (CamelMessageInfo **)v->pdata;
521                                 for (i=0;i<v->len;i++) {
522                                         char *uid = (char *)camel_message_info_uid(m[i]);
523
524                                         if (g_hash_table_lookup(have, uid) == NULL)
525                                                 g_ptr_array_add(r->value.ptrarray, uid);
526                                 }
527                                 g_hash_table_destroy(have);
528                         }
529                 } else {
530                         int res = TRUE;
531
532                         if (argv[0]->type == ESEXP_RES_BOOL)
533                                 res = ! argv[0]->value.bool;
534
535                         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
536                         r->value.bool = res;
537                 }
538         } else {
539                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
540                 r->value.bool = TRUE;
541         }
542
543         return r;
544 }
545
546 static ESExpResult *
547 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
548 {
549         int i;
550         ESExpResult *r, *r1;
551
552         if (argc>1) {
553                 g_warning("match-all only takes a single argument, other arguments ignored");
554         }
555
556         /* we are only matching a single message?  or already inside a match-all? */
557         if (search->current) {
558                 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
559
560                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
561                 r->value.bool = FALSE;
562
563                 if (argc>0) {
564                         r1 = e_sexp_term_eval(f, argv[0]);
565                         if (r1->type == ESEXP_RES_BOOL) {
566                                 r->value.bool = r1->value.bool;
567                         } else {
568                                 g_warning("invalid syntax, matches require a single bool result");
569                                 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
570                         }
571                         e_sexp_result_free(f, r1);
572                 } else {
573                         r->value.bool = TRUE;
574                 }
575                 return r;
576         }
577
578         r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
579         r->value.ptrarray = g_ptr_array_new();
580
581         if (search->summary == NULL) {
582                 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
583                 g_warning("No summary supplied, match-all doesn't work with no summary");
584                 g_assert(0);
585                 return r;
586         }
587
588         /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
589         for (i=0;i<search->summary->len;i++) {
590                 search->current = g_ptr_array_index(search->summary, i);
591                 if (argc>0) {
592                         r1 = e_sexp_term_eval(f, argv[0]);
593                         if (r1->type == ESEXP_RES_BOOL) {
594                                 if (r1->value.bool)
595                                         g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
596                         } else {
597                                 g_warning("invalid syntax, matches require a single bool result");
598                                 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
599                         }
600                         e_sexp_result_free(f, r1);
601                 } else {
602                         g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
603                 }
604         }
605         search->current = NULL;
606
607         return r;
608 }
609
610 static ESExpResult *
611 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
612 {
613         ESExpResult *r;
614         int truth = FALSE;
615
616         r(printf("executing check-header %d\n", how));
617
618         /* are we inside a match-all? */
619         if (search->current && argc>1
620             && argv[0]->type == ESEXP_RES_STRING) {
621                 char *headername;
622                 const char *header = NULL;
623                 char strbuf[32];
624                 int i, j;
625                 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
626                 struct _camel_search_words *words;
627
628                 /* only a subset of headers are supported .. */
629                 headername = argv[0]->value.string;
630                 if (!strcasecmp(headername, "subject")) {
631                         header = camel_message_info_subject(search->current);
632                 } else if (!strcasecmp(headername, "date")) {
633                         /* FIXME: not a very useful form of the date */
634                         sprintf(strbuf, "%d", (int)search->current->date_sent);
635                         header = strbuf;
636                 } else if (!strcasecmp(headername, "from")) {
637                         header = camel_message_info_from(search->current);
638                         type = CAMEL_SEARCH_TYPE_ADDRESS;
639                 } else if (!strcasecmp(headername, "to")) {
640                         header = camel_message_info_to(search->current);
641                         type = CAMEL_SEARCH_TYPE_ADDRESS;
642                 } else if (!strcasecmp(headername, "cc")) {
643                         header = camel_message_info_cc(search->current);
644                         type = CAMEL_SEARCH_TYPE_ADDRESS;
645                 } else if (!strcasecmp(headername, "x-camel-mlist")) {
646                         header = camel_message_info_mlist(search->current);
647                         type = CAMEL_SEARCH_TYPE_MLIST;
648                 } else {
649                         e_sexp_resultv_free(f, argc, argv);
650                         e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
651                 }
652
653                 if (header) {
654                         /* performs an OR of all words */
655                         for (i=1;i<argc && !truth;i++) {
656                                 if (argv[i]->type == ESEXP_RES_STRING) {
657                                         if (argv[i]->value.string[0] == 0) {
658                                                 truth = TRUE;
659                                         } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
660                                                 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
661                                                 words = camel_search_words_split(argv[i]->value.string);
662                                                 truth = TRUE;
663                                                 for (j=0;j<words->len && truth;j++) {
664                                                         truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
665                                                 }
666                                                 camel_search_words_free(words);
667                                         } else {
668                                                 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
669                                         }
670                                 }
671                         }
672                 }
673         }
674         /* TODO: else, find all matches */
675
676         r = e_sexp_result_new(f, ESEXP_RES_BOOL);
677         r->value.bool = truth;
678
679         return r;
680 }
681
682 static ESExpResult *
683 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
684 {
685         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
686 }
687
688 static ESExpResult *
689 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
690 {
691         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
692 }
693
694 static ESExpResult *
695 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
696 {
697         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
698 }
699
700 static ESExpResult *
701 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
702 {
703         return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
704 }
705
706 static ESExpResult *
707 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
708 {
709         ESExpResult *r;
710         
711         r(printf ("executing header-exists\n"));
712         
713         if (search->current) {
714                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
715                 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
716                         r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
717                 
718         } else {
719                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
720                 r->value.ptrarray = g_ptr_array_new();
721         }
722         
723         return r;
724 }
725
726 /* this is just to OR results together */
727 struct _glib_sux_donkeys {
728         int count;
729         GPtrArray *uids;
730 };
731
732 /* or, store all unique values */
733 static void
734 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
735 {
736         g_ptr_array_add(fuckup->uids, key);
737 }
738
739 /* and, only store duplicates */
740 static void
741 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
742 {
743         if (value == fuckup->count)
744                 g_ptr_array_add(fuckup->uids, key);
745 }
746
747 static int
748 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
749 {
750         CamelIndexCursor *wc, *nc;
751         const char *word, *name;
752         int truth = FALSE;
753
754         wc = camel_index_words(idx);
755         if (wc) {
756                 while (!truth && (word = camel_index_cursor_next(wc))) {
757                         if (camel_ustrstrcase(word,match) != NULL) {
758                                 /* perf: could have the wc cursor return the name cursor */
759                                 nc = camel_index_find(idx, word);
760                                 if (nc) {
761                                         while (!truth && (name = camel_index_cursor_next(nc)))
762                                                 truth = strcmp(name, uid) == 0;
763                                         camel_object_unref((CamelObject *)nc);
764                                 }
765                         }
766                 }
767                 camel_object_unref((CamelObject *)wc);
768         }
769
770         return truth;
771 }
772
773 /*
774  "one two" "three" "four five"
775
776   one and two
777 or
778   three
779 or
780   four and five
781 */
782
783 /* returns messages which contain all words listed in words */
784 static GPtrArray *
785 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
786 {
787         GPtrArray *result = g_ptr_array_new();
788         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
789         struct _glib_sux_donkeys lambdafoo;
790         CamelIndexCursor *wc, *nc;
791         const char *word, *name;
792         CamelMessageInfo *mi;
793         int i;
794
795         /* we can have a maximum of 32 words, as we use it as the AND mask */
796                         
797         wc = camel_index_words(search->body_index);
798         if (wc) {
799                 while ((word = camel_index_cursor_next(wc))) {
800                         for (i=0;i<words->len;i++) {
801                                 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
802                                         /* perf: could have the wc cursor return the name cursor */
803                                         nc = camel_index_find(search->body_index, word);
804                                         if (nc) {
805                                                 while ((name = camel_index_cursor_next(nc))) {
806                                                         mi = g_hash_table_lookup(search->summary_hash, name);
807                                                         if (mi) {
808                                                                 int mask;
809                                                                 const char *uid = camel_message_info_uid(mi);
810
811                                                                 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
812                                                                 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
813                                                         }
814                                                 }
815                                                 camel_object_unref((CamelObject *)nc);
816                                         }
817                                 }
818                         }
819                 }
820                 camel_object_unref((CamelObject *)wc);
821
822                 lambdafoo.uids = result;
823                 lambdafoo.count = (1<<words->len) - 1;
824                 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
825                 g_hash_table_destroy(ht);
826         }
827
828         return result;
829 }
830
831 static gboolean
832 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
833 {
834         CamelDataWrapper *containee;
835         int truth = FALSE;
836         int parts, i;
837         
838         containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
839         
840         if (containee == NULL)
841                 return FALSE;
842         
843         /* using the object types is more accurate than using the mime/types */
844         if (CAMEL_IS_MULTIPART (containee)) {
845                 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
846                 for (i = 0; i < parts && truth == FALSE; i++) {
847                         CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
848                         if (part)
849                                 truth = match_words_1message(part, words, mask);
850                 }
851         } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
852                 /* for messages we only look at its contents */
853                 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
854         } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
855                 /* for all other text parts, we look inside, otherwise we dont care */
856                 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
857
858                 /* FIXME: The match should be part of a stream op */
859                 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
860                 camel_stream_write (CAMEL_STREAM (mem), "", 1);
861                 for (i=0;i<words->len;i++) {
862                         /* FIXME: This is horridly slow, and should use a real search algorithm */
863                         if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
864                                 *mask |= (1<<i);
865                                 /* shortcut a match */
866                                 if (*mask == (1<<(words->len))-1)
867                                         return TRUE;
868                         }
869                 }
870                 camel_object_unref (CAMEL_OBJECT (mem));
871         }
872         
873         return truth;
874 }
875
876 static gboolean
877 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
878 {
879         guint32 mask;
880         CamelMimeMessage *msg;
881         int truth;
882
883         msg = camel_folder_get_message(folder, uid, ex);
884         if (msg) {
885                 mask = 0;
886                 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
887                 camel_object_unref((CamelObject *)msg);
888         } else {
889                 camel_exception_clear(ex);
890                 truth = FALSE;
891         }
892
893         return truth;
894 }
895
896 static GPtrArray *
897 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
898 {
899         int i;
900         GPtrArray *matches = g_ptr_array_new();
901
902         if (search->body_index) {
903                 GPtrArray *indexed;
904                 struct _camel_search_words *simple;
905
906                 simple = camel_search_words_simple(words);
907                 indexed = match_words_index(search, simple, ex);
908                 camel_search_words_free(simple);
909
910                 for (i=0;i<indexed->len;i++) {
911                         const char *uid = g_ptr_array_index(indexed, i);
912                         
913                         if (match_words_message(search->folder, uid, words, ex))
914                                 g_ptr_array_add(matches, (char *)uid);
915                 }
916                 
917                 g_ptr_array_free(indexed, TRUE);
918         } else {
919                 for (i=0;i<search->summary->len;i++) {
920                         CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
921                         const char *uid = camel_message_info_uid(info);
922                         
923                         if (match_words_message(search->folder, uid, words, ex))
924                                 g_ptr_array_add(matches, (char *)uid);
925                 }
926         }
927
928         return matches;
929 }
930
931 static ESExpResult *
932 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
933 {
934         int i, j;
935         CamelException *ex = search->priv->ex;
936         struct _camel_search_words *words;
937         ESExpResult *r;
938         struct _glib_sux_donkeys lambdafoo;
939
940         if (search->current) {  
941                 int truth = FALSE;
942
943                 if (argc == 1 && argv[0]->value.string[0] == 0) {
944                         truth = TRUE;
945                 } else {
946                         for (i=0;i<argc && !truth;i++) {
947                                 if (argv[i]->type == ESEXP_RES_STRING) {
948                                         words = camel_search_words_split(argv[i]->value.string);
949                                         truth = TRUE;
950                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
951                                                 for (j=0;j<words->len && truth;j++)
952                                                         truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
953                                         } else {
954                                                 /* TODO: cache current message incase of multiple body search terms */
955                                                 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
956                                         }
957                                         camel_search_words_free(words);
958                                 }
959                         }
960                 }
961                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
962                 r->value.bool = truth;
963         } else {
964                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
965                 r->value.ptrarray = g_ptr_array_new();
966
967                 if (argc == 1 && argv[0]->value.string[0] == 0) {
968                         for (i=0;i<search->summary->len;i++) {
969                                 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
970
971                                 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
972                         }
973                 } else {
974                         GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
975                         GPtrArray *matches;
976
977                         for (i=0;i<argc;i++) {
978                                 if (argv[i]->type == ESEXP_RES_STRING) {
979                                         words = camel_search_words_split(argv[i]->value.string);
980                                         if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
981                                                 matches = match_words_index(search, words, ex);
982                                         } else {
983                                                 matches = match_words_messages(search, words, ex);
984                                         }
985                                         for (j=0;j<matches->len;j++)
986                                                 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
987                                         g_ptr_array_free(matches, TRUE);
988                                         camel_search_words_free(words);
989                                 }
990                         }
991                         lambdafoo.uids = r->value.ptrarray;
992                         g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
993                         g_hash_table_destroy(ht);
994                 }
995         }
996
997         return r;
998 }
999
1000 static ESExpResult *
1001 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1002 {
1003         ESExpResult *r;
1004         int i;
1005
1006         r(printf("executing user-flag\n"));
1007
1008         /* are we inside a match-all? */
1009         if (search->current) {
1010                 int truth = FALSE;
1011                 /* performs an OR of all words */
1012                 for (i=0;i<argc && !truth;i++) {
1013                         if (argv[i]->type == ESEXP_RES_STRING
1014                             && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
1015                                 truth = TRUE;
1016                                 break;
1017                         }
1018                 }
1019                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1020                 r->value.bool = truth;
1021         } else {
1022                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1023                 r->value.ptrarray = g_ptr_array_new();
1024         }
1025
1026         return r;
1027 }
1028
1029 static ESExpResult *
1030 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1031 {
1032         ESExpResult *r;
1033         
1034         r(printf ("executing system-flag\n"));
1035         
1036         if (search->current) {
1037                 gboolean truth = FALSE;
1038                 
1039                 if (argc == 1)
1040                         truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
1041                 
1042                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1043                 r->value.bool = truth;
1044         } else {
1045                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1046                 r->value.ptrarray = g_ptr_array_new ();
1047         }
1048         
1049         return r;
1050 }
1051
1052 static ESExpResult *
1053 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1054 {
1055         const char *value = NULL;
1056         ESExpResult *r;
1057         
1058         r(printf("executing user-tag\n"));
1059         
1060         if (argc == 1)
1061                 value = camel_tag_get (&search->current->user_tags, argv[0]->value.string);
1062         
1063         r = e_sexp_result_new(f, ESEXP_RES_STRING);
1064         r->value.string = g_strdup (value ? value : "");
1065         
1066         return r;
1067 }
1068
1069 static ESExpResult *
1070 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1071 {
1072         ESExpResult *r;
1073
1074         r(printf("executing get-sent-date\n"));
1075
1076         /* are we inside a match-all? */
1077         if (s->current) {
1078                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1079
1080                 r->value.number = s->current->date_sent;
1081         } else {
1082                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1083                 r->value.ptrarray = g_ptr_array_new ();
1084         }
1085
1086         return r;
1087 }
1088
1089 static ESExpResult *
1090 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1091 {
1092         ESExpResult *r;
1093
1094         r(printf("executing get-received-date\n"));
1095
1096         /* are we inside a match-all? */
1097         if (s->current) {
1098                 r = e_sexp_result_new(f, ESEXP_RES_INT);
1099
1100                 r->value.number = s->current->date_received;
1101         } else {
1102                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1103                 r->value.ptrarray = g_ptr_array_new ();
1104         }
1105
1106         return r;
1107 }
1108
1109 static ESExpResult *
1110 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1111 {
1112         ESExpResult *r;
1113
1114         r(printf("executing get-current-date\n"));
1115
1116         r = e_sexp_result_new(f, ESEXP_RES_INT);
1117         r->value.number = time (NULL);
1118         return r;
1119 }
1120
1121 static ESExpResult *
1122 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1123 {
1124         ESExpResult *r;
1125         
1126         r(printf("executing get-size\n"));
1127         
1128         /* are we inside a match-all? */
1129         if (s->current) {
1130                 r = e_sexp_result_new (f, ESEXP_RES_INT);
1131                 r->value.number = s->current->size / 1024;
1132         } else {
1133                 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1134                 r->value.ptrarray = g_ptr_array_new ();
1135         }
1136         
1137         return r;
1138 }
1139
1140 static ESExpResult *
1141 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1142 {
1143         ESExpResult *r;
1144         int i;
1145
1146         r(printf("executing uid\n"));
1147
1148         /* are we inside a match-all? */
1149         if (search->current) {
1150                 int truth = FALSE;
1151                 const char *uid = camel_message_info_uid(search->current);
1152
1153                 /* performs an OR of all words */
1154                 for (i=0;i<argc && !truth;i++) {
1155                         if (argv[i]->type == ESEXP_RES_STRING
1156                             && !strcmp(uid, argv[i]->value.string)) {
1157                                 truth = TRUE;
1158                                 break;
1159                         }
1160                 }
1161                 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1162                 r->value.bool = truth;
1163         } else {
1164                 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1165                 r->value.ptrarray = g_ptr_array_new();
1166                 for (i=0;i<argc;i++) {
1167                         if (argv[i]->type == ESEXP_RES_STRING)
1168                                 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1169                 }
1170         }
1171
1172         return r;
1173 }