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"
59 struct _CamelFolderSearchPrivate {
60 GHashTable *mempool_hash;
63 CamelFolderThread *threads;
64 GHashTable *threads_hash;
67 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
69 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *db_search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
78 static ESExpResult *search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
79 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
81 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
82 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
83 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
84 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
85 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
91 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
92 static void camel_folder_search_init (CamelFolderSearch *obj);
93 static void camel_folder_search_finalize (CamelObject *obj);
95 static int read_uid_callback (void * ref, int ncol, char ** cols, char **name);
97 static CamelObjectClass *camel_folder_search_parent;
100 camel_folder_search_class_init (CamelFolderSearchClass *klass)
102 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
104 klass->not = search_not;
106 klass->match_all = search_match_all;
107 klass->match_threads = search_match_threads;
108 klass->body_contains = search_body_contains;
109 klass->header_contains = db_search_header_contains;
110 klass->header_matches = search_header_matches;
111 klass->header_starts_with = search_header_starts_with;
112 klass->header_ends_with = search_header_ends_with;
113 klass->header_exists = search_header_exists;
114 klass->user_tag = search_user_tag;
115 klass->user_flag = search_user_flag;
116 klass->system_flag = search_system_flag;
117 klass->get_sent_date = search_get_sent_date;
118 klass->get_received_date = search_get_received_date;
119 klass->get_current_date = search_get_current_date;
120 klass->get_size = search_get_size;
121 klass->uid = search_uid;
125 camel_folder_search_init (CamelFolderSearch *obj)
127 struct _CamelFolderSearchPrivate *p;
129 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
131 obj->sexp = e_sexp_new();
133 /* use a hash of mempools to associate the returned uid lists with
134 the backing mempool. yes pretty weird, but i didn't want to change
137 p->mempool_hash = g_hash_table_new(0, 0);
141 free_mempool(void *key, void *value, void *data)
143 GPtrArray *uids = key;
144 EMemPool *pool = value;
146 g_warning("Search closed with outstanding result unfreed: %p", (void *) uids);
148 g_ptr_array_free(uids, TRUE);
149 e_mempool_destroy(pool);
153 camel_folder_search_finalize (CamelObject *obj)
155 CamelFolderSearch *search = (CamelFolderSearch *)obj;
156 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
159 e_sexp_unref(search->sexp);
161 g_free(search->last_search);
162 g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
163 g_hash_table_destroy(p->mempool_hash);
167 g_print ("\nFinalizing search query and the query is : \n%s\n", search->query->str);
168 g_string_free (search->query, TRUE);
173 camel_folder_search_get_type (void)
175 static CamelType type = CAMEL_INVALID_TYPE;
177 if (type == CAMEL_INVALID_TYPE) {
178 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
179 sizeof (CamelFolderSearch),
180 sizeof (CamelFolderSearchClass),
181 (CamelObjectClassInitFunc) camel_folder_search_class_init,
183 (CamelObjectInitFunc) camel_folder_search_init,
184 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
191 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
193 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
199 int flags; /* 0x02 = immediate, 0x01 = always enter */
201 /* these have default implementations in e-sexp */
202 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
203 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
204 /* we need to override this one though to implement an 'array not' */
205 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
206 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
207 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
208 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
210 /* these we have to use our own default if there is none */
211 /* they should all be defined in the language? so it parses, or should they not?? */
212 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
213 { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
214 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
215 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
216 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
217 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
218 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
219 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
220 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
221 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
222 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
223 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
224 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
225 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
226 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
227 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
231 camel_folder_search_construct (CamelFolderSearch *search)
234 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
236 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
238 /* c is sure messy sometimes */
239 func = *((void **)(((char *)klass)+builtins[i].offset));
240 if (func == NULL && builtins[i].flags&1) {
241 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
242 func = (void *)search_dummy;
245 if (builtins[i].flags&2) {
246 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
248 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
253 search->query = NULL;
257 * camel_folder_search_new:
259 * Create a new CamelFolderSearch object.
261 * A CamelFolderSearch is a subclassable, extensible s-exp
262 * evaluator which enforces a particular set of s-expressions.
263 * Particular methods may be overriden by an implementation to
264 * implement a search for any sort of backend.
266 * Return value: A new CamelFolderSearch widget.
269 camel_folder_search_new (void)
271 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
273 camel_folder_search_construct(new);
278 * camel_folder_search_set_folder:
282 * Set the folder attribute of the search. This is currently unused, but
283 * could be used to perform a slow-search when indexes and so forth are not
284 * available. Or for use by subclasses.
287 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
289 char *tmp = camel_db_sqlize_string(folder->full_name);
290 search->folder = folder;
293 g_string_free (search->query, TRUE);
295 /* FIXME: Get this string done from camel-db by parsing with sqlite_mprintf etc. */
296 search->query = g_string_new ("SELECT uid FROM ");
297 g_string_append_printf (search->query, "%s ", tmp);
298 camel_db_free_sqlized_string (tmp);
302 * camel_folder_search_set_summary:
304 * @summary: An array of CamelMessageInfo pointers.
306 * Set the array of summary objects representing the span of the search.
308 * If this is not set, then a subclass must provide the functions
309 * for searching headers and for the match-all operator.
312 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
314 search->summary = summary;
318 * camel_folder_search_set_body_index:
322 * Set the index representing the contents of all messages
323 * in this folder. If this is not set, then the folder implementation
324 * should sub-class the CamelFolderSearch and provide its own
325 * body-contains function.
328 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
330 if (search->body_index)
331 camel_object_unref((CamelObject *)search->body_index);
332 search->body_index = index;
334 camel_object_ref((CamelObject *)index);
338 * camel_folder_search_execute_expression:
343 * Execute the search expression @expr, returning an array of
344 * all matches as a GPtrArray of uid's of matching messages.
346 * Note that any settings such as set_body_index(), set_folder(),
347 * and so on are reset to #NULL once the search has completed.
349 * TODO: The interface should probably return summary items instead
350 * (since they are much more useful to any client).
352 * Return value: A GPtrArray of strings of all matching messages.
353 * This must only be freed by camel_folder_search_free_result.
356 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
363 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
367 /* only re-parse if the search has changed */
368 if (search->last_search == NULL
369 || strcmp(search->last_search, expr)) {
370 e_sexp_input_text(search->sexp, expr, strlen(expr));
371 if (e_sexp_parse(search->sexp) == -1) {
372 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
376 g_free(search->last_search);
377 search->last_search = g_strdup(expr);
379 r = e_sexp_eval(search->sexp);
381 if (!camel_exception_is_set(ex))
382 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
386 matches = g_ptr_array_new();
388 /* now create a folder summary to return?? */
389 if (r->type == ESEXP_RES_ARRAY_PTR) {
390 d(printf("got result ...\n"));
391 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
392 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
393 of memory just storing the size tag that malloc assigns us and alignment padding, so this
394 gets around that (and is faster to allocate and free as a bonus) */
395 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
396 if (search->summary) {
397 /* reorder result in summary order */
398 results = g_hash_table_new(g_str_hash, g_str_equal);
399 for (i=0;i<r->value.ptrarray->len;i++) {
400 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
401 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
403 for (i=0;i<search->summary->len;i++) {
404 char *uid = g_ptr_array_index(search->summary, i);
405 if (g_hash_table_lookup(results, uid)) {
406 // g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
407 g_ptr_array_add(matches, (char *) camel_pstring_strdup(uid));
410 g_hash_table_destroy(results);
412 for (i=0;i<r->value.ptrarray->len;i++) {
413 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
414 // g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
415 g_ptr_array_add(matches, (char *) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
418 /* instead of putting the mempool_hash in the structure, we keep the api clean by
419 putting a reference to it in a hashtable. Lets us do some debugging and catch
420 unfree'd results as well. */
421 g_hash_table_insert(p->mempool_hash, matches, pool);
423 g_warning("Search returned an invalid result type");
426 e_sexp_result_free(search->sexp, r);
429 camel_folder_thread_messages_unref(p->threads);
431 g_hash_table_destroy(p->threads_hash);
434 p->threads_hash = NULL;
435 search->folder = NULL;
436 search->summary = NULL;
437 search->current = NULL;
438 search->body_index = NULL;
444 * camel_folder_search_search:
447 * @uids: to search against, NULL for all uid's.
450 * Run a search. Search must have had Folder already set on it, and
451 * it must implement summaries.
456 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
459 GPtrArray *matches = NULL, *summary_set;
462 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
464 g_assert(search->folder);
468 /* setup our search list, summary_hash only contains those we're interested in */
469 search->summary = camel_folder_get_summary(search->folder);
472 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
474 summary_set = search->summary_set = g_ptr_array_new();
475 for (i=0;i<uids->len;i++)
476 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
477 for (i=0;i<search->summary->len;i++)
478 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
479 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
480 g_hash_table_destroy(uids_hash);
482 summary_set = search->summary;
485 /* only re-parse if the search has changed */
486 if (search->last_search == NULL
487 || strcmp(search->last_search, expr)) {
488 e_sexp_input_text(search->sexp, expr, strlen(expr));
489 if (e_sexp_parse(search->sexp) == -1) {
490 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
494 g_free(search->last_search);
495 search->last_search = g_strdup(expr);
497 r = e_sexp_eval(search->sexp);
499 if (!camel_exception_is_set(ex))
500 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
504 printf ("\nsexp is : [%s]\n", expr);
505 printf ("Something is returned in the top-level caller : [%s]\n", search->query->str);
507 matches = g_ptr_array_new();
508 cdb = (CamelDB *) (search->folder->cdb);
509 camel_db_select (cdb, search->query->str, (CamelDBSelectCB) read_uid_callback, matches, ex);
510 e_sexp_result_free(search->sexp, r);
513 /* these might be allocated by match-threads */
515 camel_folder_thread_messages_unref(p->threads);
517 g_hash_table_destroy(p->threads_hash);
518 if (search->summary_set)
519 g_ptr_array_free(search->summary_set, TRUE);
520 camel_folder_free_summary(search->folder, search->summary);
523 p->threads_hash = NULL;
524 search->folder = NULL;
525 search->summary = NULL;
526 search->summary_set = NULL;
527 search->current = NULL;
528 search->body_index = NULL;
533 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
537 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
539 pool = g_hash_table_lookup(p->mempool_hash, result);
541 e_mempool_destroy(pool);
542 g_hash_table_remove(p->mempool_hash, result);
544 for (i=0;i<result->len;i++)
545 g_free(g_ptr_array_index(result, i));
548 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
549 g_ptr_array_free(result, TRUE);
552 /* dummy function, returns false always, or an empty match array */
554 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
558 if (search->current == NULL) {
559 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
560 r->value.bool = FALSE;
562 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
563 r->value.ptrarray = g_ptr_array_new();
569 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
571 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
577 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
578 GPtrArray *v = argv[0]->value.ptrarray;
581 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
582 r->value.ptrarray = g_ptr_array_new();
584 /* not against a single message?*/
585 if (search->current) {
588 uid = camel_message_info_uid(search->current);
589 for (i=0;!found && i<v->len;i++) {
590 if (strcmp(uid, v->pdata[i]) == 0)
595 g_ptr_array_add(r->value.ptrarray, (char *)uid);
596 } else if (search->summary == NULL) {
597 g_warning("No summary set, 'not' against an array requires a summary");
599 /* 'not' against the whole summary */
600 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
604 s = (char **)v->pdata;
605 for (i=0;i<v->len;i++)
606 g_hash_table_insert(have, s[i], s[i]);
608 v = search->summary_set?search->summary_set:search->summary;
609 m = (char **)v->pdata;
610 for (i=0;i<v->len;i++) {
613 if (g_hash_table_lookup(have, uid) == NULL)
614 g_ptr_array_add(r->value.ptrarray, uid);
616 g_hash_table_destroy(have);
621 if (argv[0]->type == ESEXP_RES_BOOL)
622 res = ! argv[0]->value.bool;
624 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
628 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
629 r->value.bool = TRUE;
636 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
642 g_warning("match-all only takes a single argument, other arguments ignored");
645 /* we are only matching a single message? or already inside a match-all? */
646 if (search->current) {
647 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
649 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
650 r->value.bool = FALSE;
653 r1 = e_sexp_term_eval(f, argv[0]);
654 if (r1->type == ESEXP_RES_BOOL) {
655 r->value.bool = r1->value.bool;
657 g_warning("invalid syntax, matches require a single bool result");
658 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
659 e_sexp_fatal_error(f, error_msg);
662 e_sexp_result_free(f, r1);
664 r->value.bool = TRUE;
669 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
670 r->value.ptrarray = g_ptr_array_new();
672 if (search->summary == NULL) {
673 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
674 g_warning("No summary supplied, match-all doesn't work with no summary");
679 v = search->summary_set?search->summary_set:search->summary;
681 if (v->len - g_hash_table_size (search->folder->summary->loaded_infos) > 50 && !CAMEL_IS_VEE_FOLDER (search->folder)) {
682 /* Load the DB contents. FIXME this 100 needs to be a better threshold to reload from DB. */
683 #warning "handle exception"
684 camel_folder_summary_reload_from_db (search->folder->summary, NULL);
688 e_sexp_term_eval (f, argv [0]);
690 for (i=0;i<v->len;i++) {
693 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
694 uid = camel_message_info_uid(search->current);
697 r1 = e_sexp_term_eval(f, argv[0]);
698 if (r1->type == ESEXP_RES_BOOL) {
700 g_ptr_array_add(r->value.ptrarray, (char *)uid);
702 g_warning("invalid syntax, matches require a single bool result");
703 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
704 e_sexp_fatal_error(f, error_msg);
707 e_sexp_result_free(f, r1);
709 g_ptr_array_add(r->value.ptrarray, (char *)uid);
711 camel_message_info_free (search->current);
714 search->current = NULL;
718 //FIXME Check threads mis
720 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
723 g_hash_table_insert(id_hash, (char *)camel_message_info_uid(root->message), root);
725 fill_thread_table(root->child, id_hash);
731 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
734 g_hash_table_insert(result_hash, (char *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
736 add_thread_results(root->child, result_hash);
742 add_results(char *uid, void *dummy, GPtrArray *result)
744 g_ptr_array_add(result, uid);
748 search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
751 struct _CamelFolderSearchPrivate *p = search->priv;
756 /* not supported in match-all */
757 if (search->current) {
758 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
759 e_sexp_fatal_error(f, error_msg);
764 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
765 e_sexp_fatal_error(f, error_msg);
769 r = e_sexp_term_eval(f, argv[0]);
770 if (r->type != ESEXP_RES_STRING) {
771 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
772 e_sexp_fatal_error(f, error_msg);
777 if (!strcmp(r->value.string, "none"))
779 else if (!strcmp(r->value.string, "all"))
781 else if (!strcmp(r->value.string, "replies"))
783 else if (!strcmp(r->value.string, "replies_parents"))
785 else if (!strcmp(r->value.string, "single"))
787 e_sexp_result_free(f, r);
789 /* behave as (begin does */
791 for (i=1;i<argc;i++) {
793 e_sexp_result_free(f, r);
794 r = e_sexp_term_eval(f, argv[i]);
797 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
798 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
799 e_sexp_fatal_error(f, error_msg);
806 if (search->folder == NULL) {
807 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
808 e_sexp_fatal_error(f, error_msg);
812 /* cache this, so we only have to re-calculate once per search at most */
813 #warning "make search threads work well. Not sure if that works"
814 if (p->threads == NULL) {
815 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
816 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
818 fill_thread_table(p->threads->tree, p->threads_hash);
821 results = g_hash_table_new(g_str_hash, g_str_equal);
822 for (i=0;i<r->value.ptrarray->len;i++) {
823 struct _CamelFolderThreadNode *node, *scan;
826 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
828 node = g_hash_table_lookup(p->threads_hash, (char *)g_ptr_array_index(r->value.ptrarray, i));
829 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
832 /* select messages in thread according to search criteria */
834 if (node->child == NULL && node->parent == NULL)
835 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
839 while (scan && scan->parent) {
841 g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
843 } else if (type == 1) {
844 while (node && node->parent)
847 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
849 add_thread_results(node->child, results);
852 e_sexp_result_free(f, r);
854 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
855 r->value.ptrarray = g_ptr_array_new();
857 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
858 g_hash_table_destroy(results);
864 check_header_deprecated (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
869 r(printf("executing check-header %d\n", how));
871 /* are we inside a match-all? */
872 if (search->current && argc>1
873 && argv[0]->type == ESEXP_RES_STRING) {
875 const char *header = NULL;
878 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
879 struct _camel_search_words *words;
881 /* only a subset of headers are supported .. */
882 headername = argv[0]->value.string;
883 if (!g_ascii_strcasecmp(headername, "subject")) {
884 header = camel_message_info_subject(search->current);
885 } else if (!g_ascii_strcasecmp(headername, "date")) {
886 /* FIXME: not a very useful form of the date */
887 sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
889 } else if (!g_ascii_strcasecmp(headername, "from")) {
890 header = camel_message_info_from(search->current);
891 type = CAMEL_SEARCH_TYPE_ADDRESS;
892 } else if (!g_ascii_strcasecmp(headername, "to")) {
893 header = camel_message_info_to(search->current);
894 type = CAMEL_SEARCH_TYPE_ADDRESS;
895 } else if (!g_ascii_strcasecmp(headername, "cc")) {
896 header = camel_message_info_cc(search->current);
897 type = CAMEL_SEARCH_TYPE_ADDRESS;
898 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
899 header = camel_message_info_mlist(search->current);
900 type = CAMEL_SEARCH_TYPE_MLIST;
902 e_sexp_resultv_free(f, argc, argv);
903 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
909 /* performs an OR of all words */
910 for (i=1;i<argc && !truth;i++) {
911 if (argv[i]->type == ESEXP_RES_STRING) {
912 if (argv[i]->value.string[0] == 0) {
914 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
915 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
916 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
918 for (j=0;j<words->len && truth;j++) {
919 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
921 camel_search_words_free(words);
923 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
928 /* TODO: else, find all matches */
930 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
931 r->value.bool = truth;
940 printf("%s\t", node);
945 check_header (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
947 /* FIXME: What to do for headers that are not stored in db */
951 if (strlen (argv [1]->value.string) > 1) {
957 column = camel_db_get_column_name (argv [0]->value.string);
960 case CAMEL_SEARCH_MATCH_EXACT:
961 temp = g_strdup_printf ("%s", argv [1]->value.string);
963 case CAMEL_SEARCH_MATCH_CONTAINS:
964 case CAMEL_SEARCH_MATCH_SOUNDEX:
965 temp = g_strdup_printf ("%%%s%%", argv [1]->value.string);
967 case CAMEL_SEARCH_MATCH_STARTS:
968 temp = g_strdup_printf ("%s%%", argv [1]->value.string);
970 case CAMEL_SEARCH_MATCH_ENDS:
971 temp = g_strdup_printf ("%%%s", argv [1]->value.string);
975 value = camel_db_sqlize_string (temp);
978 if (g_str_has_suffix (search->query->str, " "))
979 g_string_append_printf (search->query, "WHERE %s LIKE %s", column, value);
982 /*g_slist_foreach (f->operators, l_printf, NULL);
985 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);
986 f->operators = g_slist_remove_link (f->operators, (g_slist_nth (f->operators, (g_slist_length (f->operators) - 2))));
989 g_string_append_printf (search->query, " OR %s LIKE %s", column, value);
993 camel_db_free_sqlized_string (value);
996 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
997 r->value.bool = FALSE;
1002 static ESExpResult *
1003 search_header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1005 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1008 static ESExpResult *
1009 db_search_header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1011 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1014 static ESExpResult *
1015 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1017 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1020 static ESExpResult *
1021 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1023 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1026 static ESExpResult *
1027 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1029 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1032 static ESExpResult *
1033 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1037 r(printf ("executing header-exists\n"));
1039 if (search->current) {
1040 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1041 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1042 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
1045 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1046 r->value.ptrarray = g_ptr_array_new();
1052 /* this is just to OR results together */
1053 struct _glib_sux_donkeys {
1058 /* or, store all unique values */
1060 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
1062 g_ptr_array_add(fuckup->uids, key);
1065 /* and, only store duplicates */
1067 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
1069 if (value == fuckup->count)
1070 g_ptr_array_add(fuckup->uids, key);
1074 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
1076 CamelIndexCursor *wc, *nc;
1077 const char *word, *name;
1080 wc = camel_index_words(idx);
1082 while (!truth && (word = camel_index_cursor_next(wc))) {
1083 if (camel_ustrstrcase(word,match) != NULL) {
1084 /* perf: could have the wc cursor return the name cursor */
1085 nc = camel_index_find(idx, word);
1087 while (!truth && (name = camel_index_cursor_next(nc)))
1088 truth = strcmp(name, uid) == 0;
1089 camel_object_unref((CamelObject *)nc);
1093 camel_object_unref((CamelObject *)wc);
1100 "one two" "three" "four five"
1109 /* returns messages which contain all words listed in words */
1111 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1113 GPtrArray *result = g_ptr_array_new();
1114 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1115 struct _glib_sux_donkeys lambdafoo;
1116 CamelIndexCursor *wc, *nc;
1117 const char *word, *name;
1120 /* we can have a maximum of 32 words, as we use it as the AND mask */
1122 wc = camel_index_words(search->body_index);
1124 while ((word = camel_index_cursor_next(wc))) {
1125 for (i=0;i<words->len;i++) {
1126 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1127 /* perf: could have the wc cursor return the name cursor */
1128 nc = camel_index_find(search->body_index, word);
1130 while ((name = camel_index_cursor_next(nc))) {
1133 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1134 g_hash_table_insert(ht, (char *)name, GINT_TO_POINTER(mask));
1137 camel_object_unref((CamelObject *)nc);
1142 camel_object_unref((CamelObject *)wc);
1144 lambdafoo.uids = result;
1145 lambdafoo.count = (1<<words->len) - 1;
1146 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
1147 g_hash_table_destroy(ht);
1154 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1156 CamelDataWrapper *containee;
1160 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1162 if (containee == NULL)
1165 /* using the object types is more accurate than using the mime/types */
1166 if (CAMEL_IS_MULTIPART (containee)) {
1167 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1168 for (i = 0; i < parts && truth == FALSE; i++) {
1169 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1171 truth = match_words_1message(part, words, mask);
1173 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1174 /* for messages we only look at its contents */
1175 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1176 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1177 /* for all other text parts, we look inside, otherwise we dont care */
1178 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1180 /* FIXME: The match should be part of a stream op */
1181 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1182 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1183 for (i=0;i<words->len;i++) {
1184 /* FIXME: This is horridly slow, and should use a real search algorithm */
1185 if (camel_ustrstrcase((const char *) mem->buffer->data, words->words[i]->word) != NULL) {
1187 /* shortcut a match */
1188 if (*mask == (1<<(words->len))-1)
1193 camel_object_unref (mem);
1200 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1203 CamelMimeMessage *msg;
1204 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1207 msg = camel_folder_get_message(folder, uid, &x);
1210 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1211 camel_object_unref((CamelObject *)msg);
1213 camel_exception_clear(&x);
1221 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1224 GPtrArray *matches = g_ptr_array_new();
1226 if (search->body_index) {
1228 struct _camel_search_words *simple;
1230 simple = camel_search_words_simple(words);
1231 indexed = match_words_index(search, simple, ex);
1232 camel_search_words_free(simple);
1234 for (i=0;i<indexed->len;i++) {
1235 const char *uid = g_ptr_array_index(indexed, i);
1237 if (match_words_message(search->folder, uid, words, ex))
1238 g_ptr_array_add(matches, (char *)uid);
1241 g_ptr_array_free(indexed, TRUE);
1243 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1245 for (i=0;i<v->len;i++) {
1246 char *uid = g_ptr_array_index(v, i);
1248 if (match_words_message(search->folder, uid, words, ex))
1249 g_ptr_array_add(matches, (char *)uid);
1256 static ESExpResult *
1257 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1260 CamelException *ex = search->priv->ex;
1261 struct _camel_search_words *words;
1263 struct _glib_sux_donkeys lambdafoo;
1265 if (search->current) {
1268 if (argc == 1 && argv[0]->value.string[0] == 0) {
1271 for (i=0;i<argc && !truth;i++) {
1272 if (argv[i]->type == ESEXP_RES_STRING) {
1273 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1275 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1276 for (j=0;j<words->len && truth;j++)
1277 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1279 /* TODO: cache current message incase of multiple body search terms */
1280 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1282 camel_search_words_free(words);
1286 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1287 r->value.bool = truth;
1289 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1290 r->value.ptrarray = g_ptr_array_new();
1292 if (argc == 1 && argv[0]->value.string[0] == 0) {
1293 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1295 for (i=0;i<v->len;i++) {
1296 char *uid = g_ptr_array_index(v, i);
1298 g_ptr_array_add(r->value.ptrarray, uid);
1301 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1304 for (i=0;i<argc;i++) {
1305 if (argv[i]->type == ESEXP_RES_STRING) {
1306 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1307 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1308 matches = match_words_index(search, words, ex);
1310 matches = match_words_messages(search, words, ex);
1312 for (j=0;j<matches->len;j++)
1313 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1314 g_ptr_array_free(matches, TRUE);
1315 camel_search_words_free(words);
1318 lambdafoo.uids = r->value.ptrarray;
1319 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1320 g_hash_table_destroy(ht);
1327 static ESExpResult *
1328 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1334 r(printf("\nexecuting user-flag with init str: [%s]\n", search->query->str));
1338 if (search->current) {
1339 /* FIXME: I am not sure if this will ever be executed */
1342 tmp = g_strdup_printf ("%%%s%%", argv[0]->value.string);
1343 value = camel_db_sqlize_string (tmp);
1346 if (g_str_has_suffix (search->query->str, " ")) {
1347 g_string_append_printf (search->query, "WHERE labels LIKE %s", value);
1350 g_string_append_printf (search->query, " %s labels LIKE %s", (char *) (g_slist_nth_data (f->operators, 0)), value);
1351 f->operators = g_slist_remove_link (f->operators, g_slist_nth (f->operators, 0));
1353 g_string_append_printf (search->query, " OR labels LIKE %s", value);
1357 r(printf ("user-flag search value is : [%s] Appended str is : [%s]\n\n", value, search->query->str));
1358 camel_db_free_sqlized_string (value);
1361 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);
1363 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1364 r->value.bool = FALSE;
1369 static ESExpResult *
1370 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1374 r(printf ("executing system-flag\n"));
1376 if (search->current) {
1377 gboolean truth = FALSE;
1380 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1382 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1383 r->value.bool = truth;
1385 /* FIXME: You need to complete the camel-db.c:camel_db_get_column_name function and use those return values */
1386 char * value = camel_db_get_column_name (argv[0]->value.string);
1387 char *connector = "=";
1390 if (! strcmp(argv[1]->value.string, "set") )
1394 if (g_str_has_suffix (search->query->str, " "))
1395 g_string_append_printf (search->query, "WHERE (%s %s 0)", value, connector);
1397 gboolean flag = FALSE;
1398 if (search->query->str [search->query->len - 1] == ')') {
1399 search->query->len -= 1;
1404 g_string_append_printf (search->query, " %s %s %s 0", (char *) (g_slist_nth_data (f->operators, 0)), value, connector);
1407 g_string_append_printf (search->query, " OR %s %s 0", value, connector);
1411 g_string_append (search->query,")");
1416 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1417 r->value.bool = FALSE;
1423 static ESExpResult *
1424 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1430 r(printf("executing user-tag\n"));
1434 if (search->current) {
1435 /* FIXME: I am not sure if this will ever be executed */
1439 tmp = g_strdup_printf ("%s%s", argv[0]->value.string, argv[1]->value.string);
1440 value = camel_db_sqlize_string (tmp);
1443 if (g_str_has_suffix (search->query->str, " "))
1444 g_string_append_printf (search->query, "WHERE usertags = %s", value);
1447 g_string_append_printf (search->query, " %s usertags = %s", (char *) (g_slist_nth_data (f->operators, 0)), value);
1449 g_string_append_printf (search->query, " OR usertags = %s", value);
1452 camel_db_free_sqlized_string (value);
1456 g_warning ("Makes no sense to search for multiple things in user tag as it can hold only one string data : [%d] ", argc);
1458 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1459 r->value.bool = TRUE;
1464 static ESExpResult *
1465 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1469 r(printf("executing get-sent-date\n"));
1471 /* are we inside a match-all? */
1473 r = e_sexp_result_new(f, ESEXP_RES_INT);
1475 r->value.number = camel_message_info_date_sent(s->current);
1477 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1478 r->value.ptrarray = g_ptr_array_new ();
1484 static ESExpResult *
1485 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1489 r(printf("executing get-received-date\n"));
1491 /* are we inside a match-all? */
1493 r = e_sexp_result_new(f, ESEXP_RES_INT);
1495 r->value.number = camel_message_info_date_received(s->current);
1497 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1498 r->value.ptrarray = g_ptr_array_new ();
1504 static ESExpResult *
1505 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1509 r(printf("executing get-current-date\n"));
1511 r = e_sexp_result_new(f, ESEXP_RES_INT);
1512 r->value.number = time (NULL);
1516 static ESExpResult *
1517 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1521 r(printf("executing get-size\n"));
1523 /* are we inside a match-all? */
1525 r = e_sexp_result_new (f, ESEXP_RES_INT);
1526 r->value.number = camel_message_info_size(s->current) / 1024;
1528 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1529 r->value.ptrarray = g_ptr_array_new ();
1535 static ESExpResult *
1536 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1541 r(printf("executing uid\n"));
1543 /* are we inside a match-all? */
1544 if (search->current) {
1546 const char *uid = camel_message_info_uid(search->current);
1548 /* performs an OR of all words */
1549 for (i=0;i<argc && !truth;i++) {
1550 if (argv[i]->type == ESEXP_RES_STRING
1551 && !strcmp(uid, argv[i]->value.string)) {
1556 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1557 r->value.bool = truth;
1559 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1560 r->value.ptrarray = g_ptr_array_new();
1561 for (i=0;i<argc;i++) {
1562 if (argv[i]->type == ESEXP_RES_STRING)
1563 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1571 read_uid_callback (void * ref, int ncol, char ** cols, char **name)
1575 matches = (GPtrArray *) ref;
1580 for (i = 0; i < ncol; ++i) {
1581 if ( !strcmp (name [i], "uid") )
1582 g_ptr_array_add (matches, g_strdup (cols [i]));
1585 g_ptr_array_add (matches, (GFunc) camel_pstring_strdup (cols [0]));