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