1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
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.
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.
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.
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. */
30 /* POSIX requires <sys/types.h> be included before <regex.h> */
31 #include <sys/types.h>
39 #include <glib/gi18n-lib.h>
41 #include <libedataserver/e-memory.h>
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"
52 #include "camel-store.h"
53 #include "camel-vee-folder.h"
54 #include "camel-string-utils.h"
55 #include "camel-search-sql.h"
60 struct _CamelFolderSearchPrivate {
61 GHashTable *mempool_hash;
64 CamelFolderThread *threads;
65 GHashTable *threads_hash;
68 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
70 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
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);
90 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
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);
96 static int read_uid_callback (void * ref, int ncol, char ** cols, char **name);
98 static CamelObjectClass *camel_folder_search_parent;
101 camel_folder_search_class_init (CamelFolderSearchClass *klass)
103 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
105 klass->not = search_not;
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;
126 camel_folder_search_init (CamelFolderSearch *obj)
128 struct _CamelFolderSearchPrivate *p;
130 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
132 obj->sexp = e_sexp_new();
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
138 p->mempool_hash = g_hash_table_new(0, 0);
142 free_mempool(void *key, void *value, void *data)
144 GPtrArray *uids = key;
145 EMemPool *pool = value;
147 g_warning("Search closed with outstanding result unfreed: %p", (void *) uids);
149 g_ptr_array_free(uids, TRUE);
150 e_mempool_destroy(pool);
154 camel_folder_search_finalize (CamelObject *obj)
156 CamelFolderSearch *search = (CamelFolderSearch *)obj;
157 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
160 e_sexp_unref(search->sexp);
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);
168 g_print ("\nFinalizing search query and the query is : \n%s\n", search->query->str);
169 g_string_free (search->query, TRUE);
174 camel_folder_search_get_type (void)
176 static CamelType type = CAMEL_INVALID_TYPE;
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,
184 (CamelObjectInitFunc) camel_folder_search_init,
185 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
192 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
194 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
200 int flags; /* 0x02 = immediate, 0x01 = always enter */
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 },
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 },
232 camel_folder_search_construct (CamelFolderSearch *search)
235 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
237 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
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;
246 if (builtins[i].flags&2) {
247 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
249 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
254 search->query = NULL;
258 * camel_folder_search_new:
260 * Create a new CamelFolderSearch object.
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.
267 * Return value: A new CamelFolderSearch widget.
270 camel_folder_search_new (void)
272 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
274 camel_folder_search_construct(new);
279 * camel_folder_search_set_folder:
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.
288 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
290 char *tmp = camel_db_sqlize_string(folder->full_name);
291 search->folder = folder;
294 g_string_free (search->query, TRUE);
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);
303 * camel_folder_search_set_summary:
305 * @summary: An array of CamelMessageInfo pointers.
307 * Set the array of summary objects representing the span of the search.
309 * If this is not set, then a subclass must provide the functions
310 * for searching headers and for the match-all operator.
313 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
315 search->summary = summary;
319 * camel_folder_search_set_body_index:
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.
329 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
331 if (search->body_index)
332 camel_object_unref((CamelObject *)search->body_index);
333 search->body_index = index;
335 camel_object_ref((CamelObject *)index);
339 * camel_folder_search_execute_expression:
344 * Execute the search expression @expr, returning an array of
345 * all matches as a GPtrArray of uid's of matching messages.
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.
350 * TODO: The interface should probably return summary items instead
351 * (since they are much more useful to any client).
353 * Return value: A GPtrArray of strings of all matching messages.
354 * This must only be freed by camel_folder_search_free_result.
357 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
364 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
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);
377 g_free(search->last_search);
378 search->last_search = g_strdup(expr);
380 r = e_sexp_eval(search->sexp);
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);
387 matches = g_ptr_array_new();
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));
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));
411 g_hash_table_destroy(results);
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)));
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);
424 g_warning("Search returned an invalid result type");
427 e_sexp_result_free(search->sexp, r);
430 camel_folder_thread_messages_unref(p->threads);
432 g_hash_table_destroy(p->threads_hash);
435 p->threads_hash = NULL;
436 search->folder = NULL;
437 search->summary = NULL;
438 search->current = NULL;
439 search->body_index = NULL;
445 * camel_folder_search_search:
448 * @uids: to search against, NULL for all uid's.
451 * Run a search. Search must have had Folder already set on it, and
452 * it must implement summaries.
457 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
460 GPtrArray *matches = NULL, *summary_set;
463 char *sql_query, *tmp, *tmp1;
464 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
466 g_assert(search->folder);
470 /* setup our search list, summary_hash only contains those we're interested in */
471 search->summary = camel_folder_get_summary(search->folder);
474 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
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);
484 summary_set = search->summary;
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);
496 g_free(search->last_search);
497 search->last_search = g_strdup(expr);
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);
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);
513 printf("tmp %s\n", tmp);
515 matches = g_ptr_array_new();
516 cdb = (CamelDB *) (search->folder->cdb);
517 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
519 //e_sexp_result_free(search->sexp, r);
522 /* these might be allocated by match-threads */
524 camel_folder_thread_messages_unref(p->threads);
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);
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;
542 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
546 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
548 pool = g_hash_table_lookup(p->mempool_hash, result);
550 e_mempool_destroy(pool);
551 g_hash_table_remove(p->mempool_hash, result);
553 for (i=0;i<result->len;i++)
554 g_free(g_ptr_array_index(result, i));
557 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
558 g_ptr_array_free(result, TRUE);
561 /* dummy function, returns false always, or an empty match array */
563 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
567 if (search->current == NULL) {
568 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
569 r->value.bool = FALSE;
571 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
572 r->value.ptrarray = g_ptr_array_new();
578 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
580 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
586 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
587 GPtrArray *v = argv[0]->value.ptrarray;
590 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
591 r->value.ptrarray = g_ptr_array_new();
593 /* not against a single message?*/
594 if (search->current) {
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)
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");
608 /* 'not' against the whole summary */
609 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
613 s = (char **)v->pdata;
614 for (i=0;i<v->len;i++)
615 g_hash_table_insert(have, s[i], s[i]);
617 v = search->summary_set?search->summary_set:search->summary;
618 m = (char **)v->pdata;
619 for (i=0;i<v->len;i++) {
622 if (g_hash_table_lookup(have, uid) == NULL)
623 g_ptr_array_add(r->value.ptrarray, uid);
625 g_hash_table_destroy(have);
630 if (argv[0]->type == ESEXP_RES_BOOL)
631 res = ! argv[0]->value.bool;
633 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
637 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
638 r->value.bool = TRUE;
645 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
651 g_warning("match-all only takes a single argument, other arguments ignored");
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)));
658 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
659 r->value.bool = FALSE;
662 r1 = e_sexp_term_eval(f, argv[0]);
663 if (r1->type == ESEXP_RES_BOOL) {
664 r->value.bool = r1->value.bool;
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);
671 e_sexp_result_free(f, r1);
673 r->value.bool = TRUE;
678 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
679 r->value.ptrarray = g_ptr_array_new();
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");
688 v = search->summary_set?search->summary_set:search->summary;
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);
697 e_sexp_term_eval (f, argv [0]);
699 for (i=0;i<v->len;i++) {
702 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
703 uid = camel_message_info_uid(search->current);
706 r1 = e_sexp_term_eval(f, argv[0]);
707 if (r1->type == ESEXP_RES_BOOL) {
709 g_ptr_array_add(r->value.ptrarray, (char *)uid);
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);
716 e_sexp_result_free(f, r1);
718 g_ptr_array_add(r->value.ptrarray, (char *)uid);
720 camel_message_info_free (search->current);
723 search->current = NULL;
727 //FIXME Check threads mis
729 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
732 g_hash_table_insert(id_hash, (char *)camel_message_info_uid(root->message), root);
734 fill_thread_table(root->child, id_hash);
740 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
743 g_hash_table_insert(result_hash, (char *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
745 add_thread_results(root->child, result_hash);
751 add_results(char *uid, void *dummy, GPtrArray *result)
753 g_ptr_array_add(result, uid);
757 search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
760 struct _CamelFolderSearchPrivate *p = search->priv;
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);
773 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
774 e_sexp_fatal_error(f, error_msg);
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);
786 if (!strcmp(r->value.string, "none"))
788 else if (!strcmp(r->value.string, "all"))
790 else if (!strcmp(r->value.string, "replies"))
792 else if (!strcmp(r->value.string, "replies_parents"))
794 else if (!strcmp(r->value.string, "single"))
796 e_sexp_result_free(f, r);
798 /* behave as (begin does */
800 for (i=1;i<argc;i++) {
802 e_sexp_result_free(f, r);
803 r = e_sexp_term_eval(f, argv[i]);
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);
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);
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);
827 fill_thread_table(p->threads->tree, p->threads_hash);
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;
835 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
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 */
841 /* select messages in thread according to search criteria */
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));
848 while (scan && scan->parent) {
850 g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
852 } else if (type == 1) {
853 while (node && node->parent)
856 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
858 add_thread_results(node->child, results);
861 e_sexp_result_free(f, r);
863 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
864 r->value.ptrarray = g_ptr_array_new();
866 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
867 g_hash_table_destroy(results);
873 check_header_deprecated (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
878 r(printf("executing check-header %d\n", how));
880 /* are we inside a match-all? */
881 if (search->current && argc>1
882 && argv[0]->type == ESEXP_RES_STRING) {
884 const char *header = NULL;
887 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
888 struct _camel_search_words *words;
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));
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;
911 e_sexp_resultv_free(f, argc, argv);
912 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
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) {
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);
927 for (j=0;j<words->len && truth;j++) {
928 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
930 camel_search_words_free(words);
932 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
937 /* TODO: else, find all matches */
939 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
940 r->value.bool = truth;
949 printf("%s\t", node);
954 check_header (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
956 /* FIXME: What to do for headers that are not stored in db */
960 if (strlen (argv [1]->value.string) > 1) {
966 column = camel_db_get_column_name (argv [0]->value.string);
969 case CAMEL_SEARCH_MATCH_EXACT:
970 temp = g_strdup_printf ("%s", argv [1]->value.string);
972 case CAMEL_SEARCH_MATCH_CONTAINS:
973 case CAMEL_SEARCH_MATCH_SOUNDEX:
974 temp = g_strdup_printf ("%%%s%%", argv [1]->value.string);
976 case CAMEL_SEARCH_MATCH_STARTS:
977 temp = g_strdup_printf ("%s%%", argv [1]->value.string);
979 case CAMEL_SEARCH_MATCH_ENDS:
980 temp = g_strdup_printf ("%%%s", argv [1]->value.string);
984 value = camel_db_sqlize_string (temp);
987 if (g_str_has_suffix (search->query->str, " "))
988 g_string_append_printf (search->query, "WHERE %s LIKE %s", column, value);
991 /*g_slist_foreach (f->operators, l_printf, NULL);
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))));
998 g_string_append_printf (search->query, " OR %s LIKE %s", column, value);
1002 camel_db_free_sqlized_string (value);
1005 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1006 r->value.bool = FALSE;
1011 static ESExpResult *
1012 search_header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1014 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1017 static ESExpResult *
1018 db_search_header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1020 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1023 static ESExpResult *
1024 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1026 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1029 static ESExpResult *
1030 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1032 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1035 static ESExpResult *
1036 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1038 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1041 static ESExpResult *
1042 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1046 r(printf ("executing header-exists\n"));
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;
1054 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1055 r->value.ptrarray = g_ptr_array_new();
1061 /* this is just to OR results together */
1062 struct _glib_sux_donkeys {
1067 /* or, store all unique values */
1069 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
1071 g_ptr_array_add(fuckup->uids, key);
1074 /* and, only store duplicates */
1076 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
1078 if (value == fuckup->count)
1079 g_ptr_array_add(fuckup->uids, key);
1083 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
1085 CamelIndexCursor *wc, *nc;
1086 const char *word, *name;
1089 wc = camel_index_words(idx);
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);
1096 while (!truth && (name = camel_index_cursor_next(nc)))
1097 truth = strcmp(name, uid) == 0;
1098 camel_object_unref((CamelObject *)nc);
1102 camel_object_unref((CamelObject *)wc);
1109 "one two" "three" "four five"
1118 /* returns messages which contain all words listed in words */
1120 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
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;
1129 /* we can have a maximum of 32 words, as we use it as the AND mask */
1131 wc = camel_index_words(search->body_index);
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);
1139 while ((name = camel_index_cursor_next(nc))) {
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));
1146 camel_object_unref((CamelObject *)nc);
1151 camel_object_unref((CamelObject *)wc);
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);
1163 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1165 CamelDataWrapper *containee;
1169 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1171 if (containee == NULL)
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);
1180 truth = match_words_1message(part, words, mask);
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 ();
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) {
1196 /* shortcut a match */
1197 if (*mask == (1<<(words->len))-1)
1202 camel_object_unref (mem);
1209 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1212 CamelMimeMessage *msg;
1213 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1216 msg = camel_folder_get_message(folder, uid, &x);
1219 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1220 camel_object_unref((CamelObject *)msg);
1222 camel_exception_clear(&x);
1230 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1233 GPtrArray *matches = g_ptr_array_new();
1235 if (search->body_index) {
1237 struct _camel_search_words *simple;
1239 simple = camel_search_words_simple(words);
1240 indexed = match_words_index(search, simple, ex);
1241 camel_search_words_free(simple);
1243 for (i=0;i<indexed->len;i++) {
1244 const char *uid = g_ptr_array_index(indexed, i);
1246 if (match_words_message(search->folder, uid, words, ex))
1247 g_ptr_array_add(matches, (char *)uid);
1250 g_ptr_array_free(indexed, TRUE);
1252 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1254 for (i=0;i<v->len;i++) {
1255 char *uid = g_ptr_array_index(v, i);
1257 if (match_words_message(search->folder, uid, words, ex))
1258 g_ptr_array_add(matches, (char *)uid);
1265 static ESExpResult *
1266 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1269 CamelException *ex = search->priv->ex;
1270 struct _camel_search_words *words;
1272 struct _glib_sux_donkeys lambdafoo;
1274 if (search->current) {
1277 if (argc == 1 && argv[0]->value.string[0] == 0) {
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);
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);
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);
1291 camel_search_words_free(words);
1295 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1296 r->value.bool = truth;
1298 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1299 r->value.ptrarray = g_ptr_array_new();
1301 if (argc == 1 && argv[0]->value.string[0] == 0) {
1302 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1304 for (i=0;i<v->len;i++) {
1305 char *uid = g_ptr_array_index(v, i);
1307 g_ptr_array_add(r->value.ptrarray, uid);
1310 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
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);
1319 matches = match_words_messages(search, words, ex);
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);
1327 lambdafoo.uids = r->value.ptrarray;
1328 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1329 g_hash_table_destroy(ht);
1336 static ESExpResult *
1337 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1343 r(printf("\nexecuting user-flag with init str: [%s]\n", search->query->str));
1347 if (search->current) {
1348 /* FIXME: I am not sure if this will ever be executed */
1351 tmp = g_strdup_printf ("%%%s%%", argv[0]->value.string);
1352 value = camel_db_sqlize_string (tmp);
1355 if (g_str_has_suffix (search->query->str, " ")) {
1356 g_string_append_printf (search->query, "WHERE labels LIKE %s", value);
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));
1362 g_string_append_printf (search->query, " OR labels LIKE %s", value);
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);
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);
1372 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1373 r->value.bool = FALSE;
1378 static ESExpResult *
1379 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1383 r(printf ("executing system-flag\n"));
1385 if (search->current) {
1386 gboolean truth = FALSE;
1389 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1391 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1392 r->value.bool = truth;
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 = "=";
1399 if (! strcmp(argv[1]->value.string, "set") )
1403 if (g_str_has_suffix (search->query->str, " "))
1404 g_string_append_printf (search->query, "WHERE (%s %s 0)", value, connector);
1406 gboolean flag = FALSE;
1407 if (search->query->str [search->query->len - 1] == ')') {
1408 search->query->len -= 1;
1413 g_string_append_printf (search->query, " %s %s %s 0", (char *) (g_slist_nth_data (f->operators, 0)), value, connector);
1416 g_string_append_printf (search->query, " OR %s %s 0", value, connector);
1420 g_string_append (search->query,")");
1425 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1426 r->value.bool = FALSE;
1432 static ESExpResult *
1433 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1439 r(printf("executing user-tag\n"));
1443 if (search->current) {
1444 /* FIXME: I am not sure if this will ever be executed */
1448 tmp = g_strdup_printf ("%s%s", argv[0]->value.string, argv[1]->value.string);
1449 value = camel_db_sqlize_string (tmp);
1452 if (g_str_has_suffix (search->query->str, " "))
1453 g_string_append_printf (search->query, "WHERE usertags = %s", value);
1456 g_string_append_printf (search->query, " %s usertags = %s", (char *) (g_slist_nth_data (f->operators, 0)), value);
1458 g_string_append_printf (search->query, " OR usertags = %s", value);
1461 camel_db_free_sqlized_string (value);
1465 g_warning ("Makes no sense to search for multiple things in user tag as it can hold only one string data : [%d] ", argc);
1467 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1468 r->value.bool = TRUE;
1473 static ESExpResult *
1474 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1478 r(printf("executing get-sent-date\n"));
1480 /* are we inside a match-all? */
1482 r = e_sexp_result_new(f, ESEXP_RES_INT);
1484 r->value.number = camel_message_info_date_sent(s->current);
1486 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1487 r->value.ptrarray = g_ptr_array_new ();
1493 static ESExpResult *
1494 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1498 r(printf("executing get-received-date\n"));
1500 /* are we inside a match-all? */
1502 r = e_sexp_result_new(f, ESEXP_RES_INT);
1504 r->value.number = camel_message_info_date_received(s->current);
1506 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1507 r->value.ptrarray = g_ptr_array_new ();
1513 static ESExpResult *
1514 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1518 r(printf("executing get-current-date\n"));
1520 r = e_sexp_result_new(f, ESEXP_RES_INT);
1521 r->value.number = time (NULL);
1525 static ESExpResult *
1526 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1530 r(printf("executing get-size\n"));
1532 /* are we inside a match-all? */
1534 r = e_sexp_result_new (f, ESEXP_RES_INT);
1535 r->value.number = camel_message_info_size(s->current) / 1024;
1537 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1538 r->value.ptrarray = g_ptr_array_new ();
1544 static ESExpResult *
1545 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1550 r(printf("executing uid\n"));
1552 /* are we inside a match-all? */
1553 if (search->current) {
1555 const char *uid = camel_message_info_uid(search->current);
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)) {
1565 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1566 r->value.bool = truth;
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);
1580 read_uid_callback (void * ref, int ncol, char ** cols, char **name)
1584 matches = (GPtrArray *) ref;
1589 for (i = 0; i < ncol; ++i) {
1590 if ( !strcmp (name [i], "uid") )
1591 g_ptr_array_add (matches, g_strdup (cols [i]));
1594 g_ptr_array_add (matches, (GFunc) camel_pstring_strdup (cols [0]));