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