1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2003 Ximian Inc.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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. */
33 #include <sys/types.h>
38 #include "libedataserver/e-memory.h"
40 #include "camel-folder-search.h"
41 #include "camel-folder-thread.h"
43 #include "camel-exception.h"
44 #include "camel-medium.h"
45 #include "camel-multipart.h"
46 #include "camel-mime-message.h"
47 #include "camel-stream-mem.h"
48 #include "camel-search-private.h"
49 #include "camel-i18n.h"
54 struct _CamelFolderSearchPrivate {
55 GHashTable *mempool_hash;
58 CamelFolderThread *threads;
59 GHashTable *threads_hash;
62 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
64 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
66 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
67 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
68 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
69 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
70 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
72 static ESExpResult *search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
73 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
75 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
76 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
77 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
78 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
79 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
80 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
81 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
83 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
85 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
86 static void camel_folder_search_init (CamelFolderSearch *obj);
87 static void camel_folder_search_finalize (CamelObject *obj);
89 static CamelObjectClass *camel_folder_search_parent;
92 camel_folder_search_class_init (CamelFolderSearchClass *klass)
94 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
96 klass->not = search_not;
98 klass->match_all = search_match_all;
99 klass->match_threads = search_match_threads;
100 klass->body_contains = search_body_contains;
101 klass->header_contains = search_header_contains;
102 klass->header_matches = search_header_matches;
103 klass->header_starts_with = search_header_starts_with;
104 klass->header_ends_with = search_header_ends_with;
105 klass->header_exists = search_header_exists;
106 klass->user_tag = search_user_tag;
107 klass->user_flag = search_user_flag;
108 klass->system_flag = search_system_flag;
109 klass->get_sent_date = search_get_sent_date;
110 klass->get_received_date = search_get_received_date;
111 klass->get_current_date = search_get_current_date;
112 klass->get_size = search_get_size;
113 klass->uid = search_uid;
117 camel_folder_search_init (CamelFolderSearch *obj)
119 struct _CamelFolderSearchPrivate *p;
121 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
123 obj->sexp = e_sexp_new();
125 /* use a hash of mempools to associate the returned uid lists with
126 the backing mempool. yes pretty weird, but i didn't want to change
129 p->mempool_hash = g_hash_table_new(0, 0);
133 free_mempool(void *key, void *value, void *data)
135 GPtrArray *uids = key;
136 EMemPool *pool = value;
138 g_warning("Search closed with outstanding result unfreed: %p", uids);
140 g_ptr_array_free(uids, TRUE);
141 e_mempool_destroy(pool);
145 camel_folder_search_finalize (CamelObject *obj)
147 CamelFolderSearch *search = (CamelFolderSearch *)obj;
148 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
151 e_sexp_unref(search->sexp);
152 if (search->summary_hash)
153 g_hash_table_destroy(search->summary_hash);
155 g_free(search->last_search);
156 g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
157 g_hash_table_destroy(p->mempool_hash);
162 camel_folder_search_get_type (void)
164 static CamelType type = CAMEL_INVALID_TYPE;
166 if (type == CAMEL_INVALID_TYPE) {
167 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
168 sizeof (CamelFolderSearch),
169 sizeof (CamelFolderSearchClass),
170 (CamelObjectClassInitFunc) camel_folder_search_class_init,
172 (CamelObjectInitFunc) camel_folder_search_init,
173 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
180 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
182 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
188 int flags; /* 0x02 = immediate, 0x01 = always enter */
190 /* these have default implementations in e-sexp */
191 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
192 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
193 /* we need to override this one though to implement an 'array not' */
194 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
195 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
196 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
197 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
199 /* these we have to use our own default if there is none */
200 /* they should all be defined in the language? so it parses, or should they not?? */
201 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
202 { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
203 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
204 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
205 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
206 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
207 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
208 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
209 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
210 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
211 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
212 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
213 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
214 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
215 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
216 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
220 camel_folder_search_construct (CamelFolderSearch *search)
223 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
225 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
227 /* c is sure messy sometimes */
228 func = *((void **)(((char *)klass)+builtins[i].offset));
229 if (func == NULL && builtins[i].flags&1) {
230 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
231 func = (void *)search_dummy;
234 if (builtins[i].flags&2) {
235 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
237 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
244 * camel_folder_search_new:
246 * Create a new CamelFolderSearch object.
248 * A CamelFolderSearch is a subclassable, extensible s-exp
249 * evaluator which enforces a particular set of s-expressions.
250 * Particular methods may be overriden by an implementation to
251 * implement a search for any sort of backend.
253 * Return value: A new CamelFolderSearch widget.
256 camel_folder_search_new (void)
258 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
260 camel_folder_search_construct(new);
265 * camel_folder_search_set_folder:
269 * Set the folder attribute of the search. This is currently unused, but
270 * could be used to perform a slow-search when indexes and so forth are not
271 * available. Or for use by subclasses.
274 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
276 search->folder = folder;
280 * camel_folder_search_set_summary:
282 * @summary: An array of CamelMessageInfo pointers.
284 * Set the array of summary objects representing the span of the search.
286 * If this is not set, then a subclass must provide the functions
287 * for searching headers and for the match-all operator.
290 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
294 search->summary = summary;
295 if (search->summary_hash)
296 g_hash_table_destroy(search->summary_hash);
297 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
298 for (i=0;i<summary->len;i++)
299 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary->pdata[i]), summary->pdata[i]);
303 * camel_folder_search_set_body_index:
307 * Set the index representing the contents of all messages
308 * in this folder. If this is not set, then the folder implementation
309 * should sub-class the CamelFolderSearch and provide its own
310 * body-contains function.
313 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
315 if (search->body_index)
316 camel_object_unref((CamelObject *)search->body_index);
317 search->body_index = index;
319 camel_object_ref((CamelObject *)index);
323 * camel_folder_search_execute_expression:
328 * Execute the search expression @expr, returning an array of
329 * all matches as a GPtrArray of uid's of matching messages.
331 * Note that any settings such as set_body_index(), set_folder(),
332 * and so on are reset to #NULL once the search has completed.
334 * TODO: The interface should probably return summary items instead
335 * (since they are much more useful to any client).
337 * Return value: A GPtrArray of strings of all matching messages.
338 * This must only be freed by camel_folder_search_free_result.
341 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
348 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
352 /* only re-parse if the search has changed */
353 if (search->last_search == NULL
354 || strcmp(search->last_search, expr)) {
355 e_sexp_input_text(search->sexp, expr, strlen(expr));
356 if (e_sexp_parse(search->sexp) == -1) {
357 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
361 g_free(search->last_search);
362 search->last_search = g_strdup(expr);
364 r = e_sexp_eval(search->sexp);
366 if (!camel_exception_is_set(ex))
367 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
371 matches = g_ptr_array_new();
373 /* now create a folder summary to return?? */
374 if (r->type == ESEXP_RES_ARRAY_PTR) {
375 d(printf("got result ...\n"));
376 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
377 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
378 of memory just storing the size tag that malloc assigns us and alignment padding, so this
379 gets around that (and is faster to allocate and free as a bonus) */
380 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
381 if (search->summary) {
382 /* reorder result in summary order */
383 results = g_hash_table_new(g_str_hash, g_str_equal);
384 for (i=0;i<r->value.ptrarray->len;i++) {
385 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
386 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
388 for (i=0;i<search->summary->len;i++) {
389 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
390 char *uid = (char *)camel_message_info_uid(info);
391 if (g_hash_table_lookup(results, uid)) {
392 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
395 g_hash_table_destroy(results);
397 for (i=0;i<r->value.ptrarray->len;i++) {
398 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
399 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
402 /* instead of putting the mempool_hash in the structure, we keep the api clean by
403 putting a reference to it in a hashtable. Lets us do some debugging and catch
404 unfree'd results as well. */
405 g_hash_table_insert(p->mempool_hash, matches, pool);
407 g_warning("Search returned an invalid result type");
410 e_sexp_result_free(search->sexp, r);
413 camel_folder_thread_messages_unref(p->threads);
415 g_hash_table_destroy(p->threads_hash);
418 p->threads_hash = NULL;
419 search->folder = NULL;
420 search->summary = NULL;
421 search->current = NULL;
422 search->body_index = NULL;
428 * camel_folder_search_search:
431 * @uids: to search against, NULL for all uid's.
434 * Run a search. Search must have had Folder already set on it, and
435 * it must implement summaries.
440 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
443 GPtrArray *matches = NULL, *summary_set;
447 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
449 g_assert(search->folder);
453 /* setup our search list, summary_hash only contains those we're interested in */
454 search->summary = camel_folder_get_summary(search->folder);
455 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
458 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
460 summary_set = search->summary_set = g_ptr_array_new();
461 for (i=0;i<uids->len;i++)
462 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
463 for (i=0;i<search->summary->len;i++)
464 if (g_hash_table_lookup(uids_hash, camel_message_info_uid(search->summary->pdata[i])))
465 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
467 summary_set = search->summary;
470 for (i=0;i<summary_set->len;i++)
471 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary_set->pdata[i]), summary_set->pdata[i]);
473 /* only re-parse if the search has changed */
474 if (search->last_search == NULL
475 || strcmp(search->last_search, expr)) {
476 e_sexp_input_text(search->sexp, expr, strlen(expr));
477 if (e_sexp_parse(search->sexp) == -1) {
478 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
482 g_free(search->last_search);
483 search->last_search = g_strdup(expr);
485 r = e_sexp_eval(search->sexp);
487 if (!camel_exception_is_set(ex))
488 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
492 matches = g_ptr_array_new();
494 /* now create a folder summary to return?? */
495 if (r->type == ESEXP_RES_ARRAY_PTR) {
496 d(printf("got result ...\n"));
498 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
499 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
500 of memory just storing the size tag that malloc assigns us and alignment padding, so this
501 gets around that (and is faster to allocate and free as a bonus) */
502 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
503 /* reorder result in summary order */
504 results = g_hash_table_new(g_str_hash, g_str_equal);
505 for (i=0;i<r->value.ptrarray->len;i++) {
506 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
507 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
510 for (i=0;i<summary_set->len;i++) {
511 CamelMessageInfo *info = g_ptr_array_index(summary_set, i);
512 char *uid = (char *)camel_message_info_uid(info);
513 if (g_hash_table_lookup(results, uid))
514 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
516 g_hash_table_destroy(results);
518 /* instead of putting the mempool_hash in the structure, we keep the api clean by
519 putting a reference to it in a hashtable. Lets us do some debugging and catch
520 unfree'd results as well. */
521 g_hash_table_insert(p->mempool_hash, matches, pool);
523 g_warning("Search returned an invalid result type");
526 e_sexp_result_free(search->sexp, r);
528 /* these might be allocated by match-threads */
530 camel_folder_thread_messages_unref(p->threads);
532 g_hash_table_destroy(p->threads_hash);
533 if (search->summary_set)
534 g_ptr_array_free(search->summary_set, TRUE);
535 g_hash_table_destroy(search->summary_hash);
536 camel_folder_free_summary(search->folder, search->summary);
539 p->threads_hash = NULL;
540 search->folder = NULL;
541 search->summary = NULL;
542 search->summary_hash = NULL;
543 search->summary_set = NULL;
544 search->current = NULL;
545 search->body_index = NULL;
550 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
553 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
556 pool = g_hash_table_lookup(p->mempool_hash, result);
558 e_mempool_destroy(pool);
559 g_hash_table_remove(p->mempool_hash, result);
561 for (i=0;i<result->len;i++)
562 g_free(g_ptr_array_index(result, i));
564 g_ptr_array_free(result, TRUE);
567 /* dummy function, returns false always, or an empty match array */
569 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
573 if (search->current == NULL) {
574 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
575 r->value.bool = FALSE;
577 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
578 r->value.ptrarray = g_ptr_array_new();
584 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
586 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
592 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
593 GPtrArray *v = argv[0]->value.ptrarray;
596 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
597 r->value.ptrarray = g_ptr_array_new();
599 /* not against a single message?*/
600 if (search->current) {
603 uid = camel_message_info_uid(search->current);
604 for (i=0;!found && i<v->len;i++) {
605 if (strcmp(uid, v->pdata[i]) == 0)
610 g_ptr_array_add(r->value.ptrarray, (char *)uid);
611 } else if (search->summary == NULL) {
612 g_warning("No summary set, 'not' against an array requires a summary");
614 /* 'not' against the whole summary */
615 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
617 CamelMessageInfo **m;
619 s = (char **)v->pdata;
620 for (i=0;i<v->len;i++)
621 g_hash_table_insert(have, s[i], s[i]);
623 v = search->summary_set?search->summary_set:search->summary;
624 m = (CamelMessageInfo **)v->pdata;
625 for (i=0;i<v->len;i++) {
626 char *uid = (char *)camel_message_info_uid(m[i]);
628 if (g_hash_table_lookup(have, uid) == NULL)
629 g_ptr_array_add(r->value.ptrarray, uid);
631 g_hash_table_destroy(have);
636 if (argv[0]->type == ESEXP_RES_BOOL)
637 res = ! argv[0]->value.bool;
639 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
643 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
644 r->value.bool = TRUE;
651 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
658 g_warning("match-all only takes a single argument, other arguments ignored");
661 /* we are only matching a single message? or already inside a match-all? */
662 if (search->current) {
663 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
665 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
666 r->value.bool = FALSE;
669 r1 = e_sexp_term_eval(f, argv[0]);
670 if (r1->type == ESEXP_RES_BOOL) {
671 r->value.bool = r1->value.bool;
673 g_warning("invalid syntax, matches require a single bool result");
674 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
676 e_sexp_result_free(f, r1);
678 r->value.bool = TRUE;
683 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
684 r->value.ptrarray = g_ptr_array_new();
686 if (search->summary == NULL) {
687 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
688 g_warning("No summary supplied, match-all doesn't work with no summary");
693 v = search->summary_set?search->summary_set:search->summary;
694 for (i=0;i<v->len;i++) {
697 search->current = g_ptr_array_index(v, i);
698 uid = camel_message_info_uid(search->current);
701 r1 = e_sexp_term_eval(f, argv[0]);
702 if (r1->type == ESEXP_RES_BOOL) {
704 g_ptr_array_add(r->value.ptrarray, (char *)uid);
706 g_warning("invalid syntax, matches require a single bool result");
707 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
709 e_sexp_result_free(f, r1);
711 g_ptr_array_add(r->value.ptrarray, (char *)uid);
714 search->current = NULL;
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;
755 /* not supported in match-all */
757 e_sexp_fatal_error(f, _("(match-threads) not allowed inside match-all"));
760 e_sexp_fatal_error(f, _("(match-threads) requires a match type string"));
762 r = e_sexp_term_eval(f, argv[0]);
763 if (r->type != ESEXP_RES_STRING)
764 e_sexp_fatal_error(f, _("(match-threads) requires a match type string"));
767 if (!strcmp(r->value.string, "none"))
769 else if (!strcmp(r->value.string, "all"))
771 else if (!strcmp(r->value.string, "replies"))
773 else if (!strcmp(r->value.string, "replies_parents"))
775 else if (!strcmp(r->value.string, "single"))
777 e_sexp_result_free(f, r);
779 /* behave as (begin does */
781 for (i=1;i<argc;i++) {
783 e_sexp_result_free(f, r);
784 r = e_sexp_term_eval(f, argv[i]);
787 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR)
788 e_sexp_fatal_error(f, _("(match-threads) expects an array result"));
793 if (search->folder == NULL)
794 e_sexp_fatal_error(f, _("(match-threads) requires the folder set"));
796 /* cache this, so we only have to re-calculate once per search at most */
797 if (p->threads == NULL) {
798 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
799 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
801 fill_thread_table(p->threads->tree, p->threads_hash);
804 results = g_hash_table_new(g_str_hash, g_str_equal);
805 for (i=0;i<r->value.ptrarray->len;i++) {
806 struct _CamelFolderThreadNode *node, *scan;
809 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
811 node = g_hash_table_lookup(p->threads_hash, (char *)g_ptr_array_index(r->value.ptrarray, i));
812 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
815 /* select messages in thread according to search criteria */
817 if (node->child == NULL && node->parent == NULL)
818 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
822 while (scan && scan->parent) {
824 g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
826 } else if (type == 1) {
827 while (node && node->parent)
830 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
832 add_thread_results(node->child, results);
835 e_sexp_result_free(f, r);
837 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
838 r->value.ptrarray = g_ptr_array_new();
840 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
841 g_hash_table_destroy(results);
847 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
852 r(printf("executing check-header %d\n", how));
854 /* are we inside a match-all? */
855 if (search->current && argc>1
856 && argv[0]->type == ESEXP_RES_STRING) {
858 const char *header = NULL;
861 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
862 struct _camel_search_words *words;
864 /* only a subset of headers are supported .. */
865 headername = argv[0]->value.string;
866 if (!g_ascii_strcasecmp(headername, "subject")) {
867 header = camel_message_info_subject(search->current);
868 } else if (!g_ascii_strcasecmp(headername, "date")) {
869 /* FIXME: not a very useful form of the date */
870 sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
872 } else if (!g_ascii_strcasecmp(headername, "from")) {
873 header = camel_message_info_from(search->current);
874 type = CAMEL_SEARCH_TYPE_ADDRESS;
875 } else if (!g_ascii_strcasecmp(headername, "to")) {
876 header = camel_message_info_to(search->current);
877 type = CAMEL_SEARCH_TYPE_ADDRESS;
878 } else if (!g_ascii_strcasecmp(headername, "cc")) {
879 header = camel_message_info_cc(search->current);
880 type = CAMEL_SEARCH_TYPE_ADDRESS;
881 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
882 header = camel_message_info_mlist(search->current);
883 type = CAMEL_SEARCH_TYPE_MLIST;
885 e_sexp_resultv_free(f, argc, argv);
886 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
892 /* performs an OR of all words */
893 for (i=1;i<argc && !truth;i++) {
894 if (argv[i]->type == ESEXP_RES_STRING) {
895 if (argv[i]->value.string[0] == 0) {
897 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
898 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
899 words = camel_search_words_split(argv[i]->value.string);
901 for (j=0;j<words->len && truth;j++) {
902 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
904 camel_search_words_free(words);
906 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
911 /* TODO: else, find all matches */
913 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
914 r->value.bool = truth;
920 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
922 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
926 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
928 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
932 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
934 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
938 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
940 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
944 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
948 r(printf ("executing header-exists\n"));
950 if (search->current) {
951 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
952 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
953 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
956 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
957 r->value.ptrarray = g_ptr_array_new();
963 /* this is just to OR results together */
964 struct _glib_sux_donkeys {
969 /* or, store all unique values */
971 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
973 g_ptr_array_add(fuckup->uids, key);
976 /* and, only store duplicates */
978 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
980 if (value == fuckup->count)
981 g_ptr_array_add(fuckup->uids, key);
985 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
987 CamelIndexCursor *wc, *nc;
988 const char *word, *name;
991 wc = camel_index_words(idx);
993 while (!truth && (word = camel_index_cursor_next(wc))) {
994 if (camel_ustrstrcase(word,match) != NULL) {
995 /* perf: could have the wc cursor return the name cursor */
996 nc = camel_index_find(idx, word);
998 while (!truth && (name = camel_index_cursor_next(nc)))
999 truth = strcmp(name, uid) == 0;
1000 camel_object_unref((CamelObject *)nc);
1004 camel_object_unref((CamelObject *)wc);
1011 "one two" "three" "four five"
1020 /* returns messages which contain all words listed in words */
1022 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1024 GPtrArray *result = g_ptr_array_new();
1025 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1026 struct _glib_sux_donkeys lambdafoo;
1027 CamelIndexCursor *wc, *nc;
1028 const char *word, *name;
1029 CamelMessageInfo *mi;
1032 /* we can have a maximum of 32 words, as we use it as the AND mask */
1034 wc = camel_index_words(search->body_index);
1036 while ((word = camel_index_cursor_next(wc))) {
1037 for (i=0;i<words->len;i++) {
1038 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1039 /* perf: could have the wc cursor return the name cursor */
1040 nc = camel_index_find(search->body_index, word);
1042 while ((name = camel_index_cursor_next(nc))) {
1043 mi = g_hash_table_lookup(search->summary_hash, name);
1046 const char *uid = camel_message_info_uid(mi);
1048 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
1049 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
1052 camel_object_unref((CamelObject *)nc);
1057 camel_object_unref((CamelObject *)wc);
1059 lambdafoo.uids = result;
1060 lambdafoo.count = (1<<words->len) - 1;
1061 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
1062 g_hash_table_destroy(ht);
1069 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1071 CamelDataWrapper *containee;
1075 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1077 if (containee == NULL)
1080 /* using the object types is more accurate than using the mime/types */
1081 if (CAMEL_IS_MULTIPART (containee)) {
1082 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1083 for (i = 0; i < parts && truth == FALSE; i++) {
1084 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1086 truth = match_words_1message(part, words, mask);
1088 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1089 /* for messages we only look at its contents */
1090 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1091 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1092 /* for all other text parts, we look inside, otherwise we dont care */
1093 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1095 /* FIXME: The match should be part of a stream op */
1096 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1097 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1098 for (i=0;i<words->len;i++) {
1099 /* FIXME: This is horridly slow, and should use a real search algorithm */
1100 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
1102 /* shortcut a match */
1103 if (*mask == (1<<(words->len))-1)
1108 camel_object_unref (mem);
1115 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1118 CamelMimeMessage *msg;
1119 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1122 msg = camel_folder_get_message(folder, uid, &x);
1125 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1126 camel_object_unref((CamelObject *)msg);
1128 camel_exception_clear(&x);
1136 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1139 GPtrArray *matches = g_ptr_array_new();
1141 if (search->body_index) {
1143 struct _camel_search_words *simple;
1145 simple = camel_search_words_simple(words);
1146 indexed = match_words_index(search, simple, ex);
1147 camel_search_words_free(simple);
1149 for (i=0;i<indexed->len;i++) {
1150 const char *uid = g_ptr_array_index(indexed, i);
1152 if (match_words_message(search->folder, uid, words, ex))
1153 g_ptr_array_add(matches, (char *)uid);
1156 g_ptr_array_free(indexed, TRUE);
1158 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1160 for (i=0;i<v->len;i++) {
1161 CamelMessageInfo *info = g_ptr_array_index(v, i);
1162 const char *uid = camel_message_info_uid(info);
1164 if (match_words_message(search->folder, uid, words, ex))
1165 g_ptr_array_add(matches, (char *)uid);
1172 static ESExpResult *
1173 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1176 CamelException *ex = search->priv->ex;
1177 struct _camel_search_words *words;
1179 struct _glib_sux_donkeys lambdafoo;
1181 if (search->current) {
1184 if (argc == 1 && argv[0]->value.string[0] == 0) {
1187 for (i=0;i<argc && !truth;i++) {
1188 if (argv[i]->type == ESEXP_RES_STRING) {
1189 words = camel_search_words_split(argv[i]->value.string);
1191 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1192 for (j=0;j<words->len && truth;j++)
1193 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1195 /* TODO: cache current message incase of multiple body search terms */
1196 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1198 camel_search_words_free(words);
1202 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1203 r->value.bool = truth;
1205 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1206 r->value.ptrarray = g_ptr_array_new();
1208 if (argc == 1 && argv[0]->value.string[0] == 0) {
1209 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1211 for (i=0;i<v->len;i++) {
1212 CamelMessageInfo *info = g_ptr_array_index(v, i);
1214 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
1217 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1220 for (i=0;i<argc;i++) {
1221 if (argv[i]->type == ESEXP_RES_STRING) {
1222 words = camel_search_words_split(argv[i]->value.string);
1223 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1224 matches = match_words_index(search, words, ex);
1226 matches = match_words_messages(search, words, ex);
1228 for (j=0;j<matches->len;j++)
1229 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1230 g_ptr_array_free(matches, TRUE);
1231 camel_search_words_free(words);
1234 lambdafoo.uids = r->value.ptrarray;
1235 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1236 g_hash_table_destroy(ht);
1243 static ESExpResult *
1244 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1249 r(printf("executing user-flag\n"));
1251 /* are we inside a match-all? */
1252 if (search->current) {
1254 /* performs an OR of all words */
1255 for (i=0;i<argc && !truth;i++) {
1256 if (argv[i]->type == ESEXP_RES_STRING
1257 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1262 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1263 r->value.bool = truth;
1265 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1266 r->value.ptrarray = g_ptr_array_new();
1272 static ESExpResult *
1273 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1277 r(printf ("executing system-flag\n"));
1279 if (search->current) {
1280 gboolean truth = FALSE;
1283 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1285 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1286 r->value.bool = truth;
1288 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1289 r->value.ptrarray = g_ptr_array_new ();
1295 static ESExpResult *
1296 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1298 const char *value = NULL;
1301 r(printf("executing user-tag\n"));
1304 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1306 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1307 r->value.string = g_strdup (value ? value : "");
1312 static ESExpResult *
1313 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1317 r(printf("executing get-sent-date\n"));
1319 /* are we inside a match-all? */
1321 r = e_sexp_result_new(f, ESEXP_RES_INT);
1323 r->value.number = camel_message_info_date_sent(s->current);
1325 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1326 r->value.ptrarray = g_ptr_array_new ();
1332 static ESExpResult *
1333 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1337 r(printf("executing get-received-date\n"));
1339 /* are we inside a match-all? */
1341 r = e_sexp_result_new(f, ESEXP_RES_INT);
1343 r->value.number = camel_message_info_date_received(s->current);
1345 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1346 r->value.ptrarray = g_ptr_array_new ();
1352 static ESExpResult *
1353 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1357 r(printf("executing get-current-date\n"));
1359 r = e_sexp_result_new(f, ESEXP_RES_INT);
1360 r->value.number = time (NULL);
1364 static ESExpResult *
1365 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1369 r(printf("executing get-size\n"));
1371 /* are we inside a match-all? */
1373 r = e_sexp_result_new (f, ESEXP_RES_INT);
1374 r->value.number = camel_message_info_size(s->current) / 1024;
1376 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1377 r->value.ptrarray = g_ptr_array_new ();
1383 static ESExpResult *
1384 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1389 r(printf("executing uid\n"));
1391 /* are we inside a match-all? */
1392 if (search->current) {
1394 const char *uid = camel_message_info_uid(search->current);
1396 /* performs an OR of all words */
1397 for (i=0;i<argc && !truth;i++) {
1398 if (argv[i]->type == ESEXP_RES_STRING
1399 && !strcmp(uid, argv[i]->value.string)) {
1404 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1405 r->value.bool = truth;
1407 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1408 r->value.ptrarray = g_ptr_array_new();
1409 for (i=0;i<argc;i++) {
1410 if (argv[i]->type == ESEXP_RES_STRING)
1411 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);