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. */
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"
55 struct _CamelFolderSearchPrivate {
56 GHashTable *mempool_hash;
59 CamelFolderThread *threads;
60 GHashTable *threads_hash;
63 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
65 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
67 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
68 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
69 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
70 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
73 static ESExpResult *search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
74 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
76 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
77 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
78 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
79 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
80 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
81 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
82 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
84 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
86 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
87 static void camel_folder_search_init (CamelFolderSearch *obj);
88 static void camel_folder_search_finalize (CamelObject *obj);
90 static CamelObjectClass *camel_folder_search_parent;
93 camel_folder_search_class_init (CamelFolderSearchClass *klass)
95 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
97 klass->not = search_not;
99 klass->match_all = search_match_all;
100 klass->match_threads = search_match_threads;
101 klass->body_contains = search_body_contains;
102 klass->header_contains = search_header_contains;
103 klass->header_matches = search_header_matches;
104 klass->header_starts_with = search_header_starts_with;
105 klass->header_ends_with = search_header_ends_with;
106 klass->header_exists = search_header_exists;
107 klass->user_tag = search_user_tag;
108 klass->user_flag = search_user_flag;
109 klass->system_flag = search_system_flag;
110 klass->get_sent_date = search_get_sent_date;
111 klass->get_received_date = search_get_received_date;
112 klass->get_current_date = search_get_current_date;
113 klass->get_size = search_get_size;
114 klass->uid = search_uid;
118 camel_folder_search_init (CamelFolderSearch *obj)
120 struct _CamelFolderSearchPrivate *p;
122 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
124 obj->sexp = e_sexp_new();
126 /* use a hash of mempools to associate the returned uid lists with
127 the backing mempool. yes pretty weird, but i didn't want to change
130 p->mempool_hash = g_hash_table_new(0, 0);
134 free_mempool(void *key, void *value, void *data)
136 GPtrArray *uids = key;
137 EMemPool *pool = value;
139 g_warning("Search closed with outstanding result unfreed: %p", uids);
141 g_ptr_array_free(uids, TRUE);
142 e_mempool_destroy(pool);
146 camel_folder_search_finalize (CamelObject *obj)
148 CamelFolderSearch *search = (CamelFolderSearch *)obj;
149 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
152 e_sexp_unref(search->sexp);
153 if (search->summary_hash)
154 g_hash_table_destroy(search->summary_hash);
156 g_free(search->last_search);
157 g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
158 g_hash_table_destroy(p->mempool_hash);
163 camel_folder_search_get_type (void)
165 static CamelType type = CAMEL_INVALID_TYPE;
167 if (type == CAMEL_INVALID_TYPE) {
168 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
169 sizeof (CamelFolderSearch),
170 sizeof (CamelFolderSearchClass),
171 (CamelObjectClassInitFunc) camel_folder_search_class_init,
173 (CamelObjectInitFunc) camel_folder_search_init,
174 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
181 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
183 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
189 int flags; /* 0x02 = immediate, 0x01 = always enter */
191 /* these have default implementations in e-sexp */
192 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
193 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
194 /* we need to override this one though to implement an 'array not' */
195 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
196 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
197 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
198 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
200 /* these we have to use our own default if there is none */
201 /* they should all be defined in the language? so it parses, or should they not?? */
202 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
203 { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
204 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
205 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
206 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
207 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
208 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
209 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
210 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
211 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
212 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
213 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
214 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
215 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
216 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
217 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
221 camel_folder_search_construct (CamelFolderSearch *search)
224 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
226 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
228 /* c is sure messy sometimes */
229 func = *((void **)(((char *)klass)+builtins[i].offset));
230 if (func == NULL && builtins[i].flags&1) {
231 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
232 func = (void *)search_dummy;
235 if (builtins[i].flags&2) {
236 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
238 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
245 * camel_folder_search_new:
247 * Create a new CamelFolderSearch object.
249 * A CamelFolderSearch is a subclassable, extensible s-exp
250 * evaluator which enforces a particular set of s-expressions.
251 * Particular methods may be overriden by an implementation to
252 * implement a search for any sort of backend.
254 * Return value: A new CamelFolderSearch widget.
257 camel_folder_search_new (void)
259 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
261 camel_folder_search_construct(new);
266 * camel_folder_search_set_folder:
270 * Set the folder attribute of the search. This is currently unused, but
271 * could be used to perform a slow-search when indexes and so forth are not
272 * available. Or for use by subclasses.
275 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
277 search->folder = folder;
281 * camel_folder_search_set_summary:
283 * @summary: An array of CamelMessageInfo pointers.
285 * Set the array of summary objects representing the span of the search.
287 * If this is not set, then a subclass must provide the functions
288 * for searching headers and for the match-all operator.
291 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
295 search->summary = summary;
296 if (search->summary_hash)
297 g_hash_table_destroy(search->summary_hash);
298 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
299 for (i=0;i<summary->len;i++)
300 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary->pdata[i]), summary->pdata[i]);
304 * camel_folder_search_set_body_index:
308 * Set the index representing the contents of all messages
309 * in this folder. If this is not set, then the folder implementation
310 * should sub-class the CamelFolderSearch and provide its own
311 * body-contains function.
314 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
316 if (search->body_index)
317 camel_object_unref((CamelObject *)search->body_index);
318 search->body_index = index;
320 camel_object_ref((CamelObject *)index);
324 * camel_folder_search_execute_expression:
329 * Execute the search expression @expr, returning an array of
330 * all matches as a GPtrArray of uid's of matching messages.
332 * Note that any settings such as set_body_index(), set_folder(),
333 * and so on are reset to #NULL once the search has completed.
335 * TODO: The interface should probably return summary items instead
336 * (since they are much more useful to any client).
338 * Return value: A GPtrArray of strings of all matching messages.
339 * This must only be freed by camel_folder_search_free_result.
342 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
349 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
353 /* only re-parse if the search has changed */
354 if (search->last_search == NULL
355 || strcmp(search->last_search, expr)) {
356 e_sexp_input_text(search->sexp, expr, strlen(expr));
357 if (e_sexp_parse(search->sexp) == -1) {
358 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
362 g_free(search->last_search);
363 search->last_search = g_strdup(expr);
365 r = e_sexp_eval(search->sexp);
367 if (!camel_exception_is_set(ex))
368 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
372 matches = g_ptr_array_new();
374 /* now create a folder summary to return?? */
375 if (r->type == ESEXP_RES_ARRAY_PTR) {
376 d(printf("got result ...\n"));
377 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
378 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
379 of memory just storing the size tag that malloc assigns us and alignment padding, so this
380 gets around that (and is faster to allocate and free as a bonus) */
381 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
382 if (search->summary) {
383 /* reorder result in summary order */
384 results = g_hash_table_new(g_str_hash, g_str_equal);
385 for (i=0;i<r->value.ptrarray->len;i++) {
386 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
387 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
389 for (i=0;i<search->summary->len;i++) {
390 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
391 char *uid = (char *)camel_message_info_uid(info);
392 if (g_hash_table_lookup(results, uid)) {
393 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
396 g_hash_table_destroy(results);
398 for (i=0;i<r->value.ptrarray->len;i++) {
399 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
400 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
403 /* instead of putting the mempool_hash in the structure, we keep the api clean by
404 putting a reference to it in a hashtable. Lets us do some debugging and catch
405 unfree'd results as well. */
406 g_hash_table_insert(p->mempool_hash, matches, pool);
408 g_warning("Search returned an invalid result type");
411 e_sexp_result_free(search->sexp, r);
414 camel_folder_thread_messages_unref(p->threads);
416 g_hash_table_destroy(p->threads_hash);
419 p->threads_hash = NULL;
420 search->folder = NULL;
421 search->summary = NULL;
422 search->current = NULL;
423 search->body_index = NULL;
429 * camel_folder_search_search:
432 * @uids: to search against, NULL for all uid's.
435 * Run a search. Search must have had Folder already set on it, and
436 * it must implement summaries.
441 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
444 GPtrArray *matches = NULL, *summary_set;
448 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
450 g_assert(search->folder);
454 /* setup our search list, summary_hash only contains those we're interested in */
455 search->summary = camel_folder_get_summary(search->folder);
456 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
459 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
461 summary_set = search->summary_set = g_ptr_array_new();
462 for (i=0;i<uids->len;i++)
463 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
464 for (i=0;i<search->summary->len;i++)
465 if (g_hash_table_lookup(uids_hash, camel_message_info_uid(search->summary->pdata[i])))
466 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
467 g_hash_table_destroy(uids_hash);
469 summary_set = search->summary;
472 for (i=0;i<summary_set->len;i++)
473 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary_set->pdata[i]), summary_set->pdata[i]);
475 /* only re-parse if the search has changed */
476 if (search->last_search == NULL
477 || strcmp(search->last_search, expr)) {
478 e_sexp_input_text(search->sexp, expr, strlen(expr));
479 if (e_sexp_parse(search->sexp) == -1) {
480 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
484 g_free(search->last_search);
485 search->last_search = g_strdup(expr);
487 r = e_sexp_eval(search->sexp);
489 if (!camel_exception_is_set(ex))
490 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
494 matches = g_ptr_array_new();
496 /* now create a folder summary to return?? */
497 if (r->type == ESEXP_RES_ARRAY_PTR) {
498 d(printf("got result ...\n"));
500 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
501 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
502 of memory just storing the size tag that malloc assigns us and alignment padding, so this
503 gets around that (and is faster to allocate and free as a bonus) */
504 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
505 /* reorder result in summary order */
506 results = g_hash_table_new(g_str_hash, g_str_equal);
507 for (i=0;i<r->value.ptrarray->len;i++) {
508 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
509 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
512 for (i=0;i<summary_set->len;i++) {
513 CamelMessageInfo *info = g_ptr_array_index(summary_set, i);
514 char *uid = (char *)camel_message_info_uid(info);
515 if (g_hash_table_lookup(results, uid))
516 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
518 g_hash_table_destroy(results);
520 /* instead of putting the mempool_hash in the structure, we keep the api clean by
521 putting a reference to it in a hashtable. Lets us do some debugging and catch
522 unfree'd results as well. */
523 g_hash_table_insert(p->mempool_hash, matches, pool);
525 g_warning("Search returned an invalid result type");
528 e_sexp_result_free(search->sexp, r);
530 /* these might be allocated by match-threads */
532 camel_folder_thread_messages_unref(p->threads);
534 g_hash_table_destroy(p->threads_hash);
535 if (search->summary_set)
536 g_ptr_array_free(search->summary_set, TRUE);
537 g_hash_table_destroy(search->summary_hash);
538 camel_folder_free_summary(search->folder, search->summary);
541 p->threads_hash = NULL;
542 search->folder = NULL;
543 search->summary = NULL;
544 search->summary_hash = NULL;
545 search->summary_set = NULL;
546 search->current = NULL;
547 search->body_index = NULL;
552 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
555 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
558 pool = g_hash_table_lookup(p->mempool_hash, result);
560 e_mempool_destroy(pool);
561 g_hash_table_remove(p->mempool_hash, result);
563 for (i=0;i<result->len;i++)
564 g_free(g_ptr_array_index(result, i));
566 g_ptr_array_free(result, TRUE);
569 /* dummy function, returns false always, or an empty match array */
571 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
575 if (search->current == NULL) {
576 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
577 r->value.bool = FALSE;
579 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
580 r->value.ptrarray = g_ptr_array_new();
586 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
588 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
594 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
595 GPtrArray *v = argv[0]->value.ptrarray;
598 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
599 r->value.ptrarray = g_ptr_array_new();
601 /* not against a single message?*/
602 if (search->current) {
605 uid = camel_message_info_uid(search->current);
606 for (i=0;!found && i<v->len;i++) {
607 if (strcmp(uid, v->pdata[i]) == 0)
612 g_ptr_array_add(r->value.ptrarray, (char *)uid);
613 } else if (search->summary == NULL) {
614 g_warning("No summary set, 'not' against an array requires a summary");
616 /* 'not' against the whole summary */
617 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
619 CamelMessageInfo **m;
621 s = (char **)v->pdata;
622 for (i=0;i<v->len;i++)
623 g_hash_table_insert(have, s[i], s[i]);
625 v = search->summary_set?search->summary_set:search->summary;
626 m = (CamelMessageInfo **)v->pdata;
627 for (i=0;i<v->len;i++) {
628 char *uid = (char *)camel_message_info_uid(m[i]);
630 if (g_hash_table_lookup(have, uid) == NULL)
631 g_ptr_array_add(r->value.ptrarray, uid);
633 g_hash_table_destroy(have);
638 if (argv[0]->type == ESEXP_RES_BOOL)
639 res = ! argv[0]->value.bool;
641 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
645 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
646 r->value.bool = TRUE;
653 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
660 g_warning("match-all only takes a single argument, other arguments ignored");
663 /* we are only matching a single message? or already inside a match-all? */
664 if (search->current) {
665 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
667 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
668 r->value.bool = FALSE;
671 r1 = e_sexp_term_eval(f, argv[0]);
672 if (r1->type == ESEXP_RES_BOOL) {
673 r->value.bool = r1->value.bool;
675 g_warning("invalid syntax, matches require a single bool result");
676 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
678 e_sexp_result_free(f, r1);
680 r->value.bool = TRUE;
685 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
686 r->value.ptrarray = g_ptr_array_new();
688 if (search->summary == NULL) {
689 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
690 g_warning("No summary supplied, match-all doesn't work with no summary");
695 v = search->summary_set?search->summary_set:search->summary;
696 for (i=0;i<v->len;i++) {
699 search->current = g_ptr_array_index(v, i);
700 uid = camel_message_info_uid(search->current);
703 r1 = e_sexp_term_eval(f, argv[0]);
704 if (r1->type == ESEXP_RES_BOOL) {
706 g_ptr_array_add(r->value.ptrarray, (char *)uid);
708 g_warning("invalid syntax, matches require a single bool result");
709 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
711 e_sexp_result_free(f, r1);
713 g_ptr_array_add(r->value.ptrarray, (char *)uid);
716 search->current = NULL;
722 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
725 g_hash_table_insert(id_hash, (char *)camel_message_info_uid(root->message), root);
727 fill_thread_table(root->child, id_hash);
733 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
736 g_hash_table_insert(result_hash, (char *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
738 add_thread_results(root->child, result_hash);
744 add_results(char *uid, void *dummy, GPtrArray *result)
746 g_ptr_array_add(result, uid);
750 search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
753 struct _CamelFolderSearchPrivate *p = search->priv;
757 /* not supported in match-all */
759 e_sexp_fatal_error(f, _("(match-threads) not allowed inside match-all"));
762 e_sexp_fatal_error(f, _("(match-threads) requires a match type string"));
764 r = e_sexp_term_eval(f, argv[0]);
765 if (r->type != ESEXP_RES_STRING)
766 e_sexp_fatal_error(f, _("(match-threads) requires a match type string"));
769 if (!strcmp(r->value.string, "none"))
771 else if (!strcmp(r->value.string, "all"))
773 else if (!strcmp(r->value.string, "replies"))
775 else if (!strcmp(r->value.string, "replies_parents"))
777 else if (!strcmp(r->value.string, "single"))
779 e_sexp_result_free(f, r);
781 /* behave as (begin does */
783 for (i=1;i<argc;i++) {
785 e_sexp_result_free(f, r);
786 r = e_sexp_term_eval(f, argv[i]);
789 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR)
790 e_sexp_fatal_error(f, _("(match-threads) expects an array result"));
795 if (search->folder == NULL)
796 e_sexp_fatal_error(f, _("(match-threads) requires the folder set"));
798 /* cache this, so we only have to re-calculate once per search at most */
799 if (p->threads == NULL) {
800 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
801 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
803 fill_thread_table(p->threads->tree, p->threads_hash);
806 results = g_hash_table_new(g_str_hash, g_str_equal);
807 for (i=0;i<r->value.ptrarray->len;i++) {
808 struct _CamelFolderThreadNode *node, *scan;
811 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
813 node = g_hash_table_lookup(p->threads_hash, (char *)g_ptr_array_index(r->value.ptrarray, i));
814 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
817 /* select messages in thread according to search criteria */
819 if (node->child == NULL && node->parent == NULL)
820 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
824 while (scan && scan->parent) {
826 g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
828 } else if (type == 1) {
829 while (node && node->parent)
832 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
834 add_thread_results(node->child, results);
837 e_sexp_result_free(f, r);
839 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
840 r->value.ptrarray = g_ptr_array_new();
842 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
843 g_hash_table_destroy(results);
849 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
854 r(printf("executing check-header %d\n", how));
856 /* are we inside a match-all? */
857 if (search->current && argc>1
858 && argv[0]->type == ESEXP_RES_STRING) {
860 const char *header = NULL;
863 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
864 struct _camel_search_words *words;
866 /* only a subset of headers are supported .. */
867 headername = argv[0]->value.string;
868 if (!g_ascii_strcasecmp(headername, "subject")) {
869 header = camel_message_info_subject(search->current);
870 } else if (!g_ascii_strcasecmp(headername, "date")) {
871 /* FIXME: not a very useful form of the date */
872 sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
874 } else if (!g_ascii_strcasecmp(headername, "from")) {
875 header = camel_message_info_from(search->current);
876 type = CAMEL_SEARCH_TYPE_ADDRESS;
877 } else if (!g_ascii_strcasecmp(headername, "to")) {
878 header = camel_message_info_to(search->current);
879 type = CAMEL_SEARCH_TYPE_ADDRESS;
880 } else if (!g_ascii_strcasecmp(headername, "cc")) {
881 header = camel_message_info_cc(search->current);
882 type = CAMEL_SEARCH_TYPE_ADDRESS;
883 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
884 header = camel_message_info_mlist(search->current);
885 type = CAMEL_SEARCH_TYPE_MLIST;
887 e_sexp_resultv_free(f, argc, argv);
888 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
894 /* performs an OR of all words */
895 for (i=1;i<argc && !truth;i++) {
896 if (argv[i]->type == ESEXP_RES_STRING) {
897 if (argv[i]->value.string[0] == 0) {
899 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
900 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
901 words = camel_search_words_split(argv[i]->value.string);
903 for (j=0;j<words->len && truth;j++) {
904 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
906 camel_search_words_free(words);
908 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
913 /* TODO: else, find all matches */
915 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
916 r->value.bool = truth;
922 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
924 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
928 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
930 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
934 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
936 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
940 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
942 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
946 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
950 r(printf ("executing header-exists\n"));
952 if (search->current) {
953 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
954 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
955 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
958 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
959 r->value.ptrarray = g_ptr_array_new();
965 /* this is just to OR results together */
966 struct _glib_sux_donkeys {
971 /* or, store all unique values */
973 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
975 g_ptr_array_add(fuckup->uids, key);
978 /* and, only store duplicates */
980 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
982 if (value == fuckup->count)
983 g_ptr_array_add(fuckup->uids, key);
987 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
989 CamelIndexCursor *wc, *nc;
990 const char *word, *name;
993 wc = camel_index_words(idx);
995 while (!truth && (word = camel_index_cursor_next(wc))) {
996 if (camel_ustrstrcase(word,match) != NULL) {
997 /* perf: could have the wc cursor return the name cursor */
998 nc = camel_index_find(idx, word);
1000 while (!truth && (name = camel_index_cursor_next(nc)))
1001 truth = strcmp(name, uid) == 0;
1002 camel_object_unref((CamelObject *)nc);
1006 camel_object_unref((CamelObject *)wc);
1013 "one two" "three" "four five"
1022 /* returns messages which contain all words listed in words */
1024 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1026 GPtrArray *result = g_ptr_array_new();
1027 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1028 struct _glib_sux_donkeys lambdafoo;
1029 CamelIndexCursor *wc, *nc;
1030 const char *word, *name;
1031 CamelMessageInfo *mi;
1034 /* we can have a maximum of 32 words, as we use it as the AND mask */
1036 wc = camel_index_words(search->body_index);
1038 while ((word = camel_index_cursor_next(wc))) {
1039 for (i=0;i<words->len;i++) {
1040 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1041 /* perf: could have the wc cursor return the name cursor */
1042 nc = camel_index_find(search->body_index, word);
1044 while ((name = camel_index_cursor_next(nc))) {
1045 mi = g_hash_table_lookup(search->summary_hash, name);
1048 const char *uid = camel_message_info_uid(mi);
1050 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
1051 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
1054 camel_object_unref((CamelObject *)nc);
1059 camel_object_unref((CamelObject *)wc);
1061 lambdafoo.uids = result;
1062 lambdafoo.count = (1<<words->len) - 1;
1063 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
1064 g_hash_table_destroy(ht);
1071 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1073 CamelDataWrapper *containee;
1077 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1079 if (containee == NULL)
1082 /* using the object types is more accurate than using the mime/types */
1083 if (CAMEL_IS_MULTIPART (containee)) {
1084 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1085 for (i = 0; i < parts && truth == FALSE; i++) {
1086 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1088 truth = match_words_1message(part, words, mask);
1090 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1091 /* for messages we only look at its contents */
1092 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1093 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1094 /* for all other text parts, we look inside, otherwise we dont care */
1095 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1097 /* FIXME: The match should be part of a stream op */
1098 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1099 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1100 for (i=0;i<words->len;i++) {
1101 /* FIXME: This is horridly slow, and should use a real search algorithm */
1102 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
1104 /* shortcut a match */
1105 if (*mask == (1<<(words->len))-1)
1110 camel_object_unref (mem);
1117 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1120 CamelMimeMessage *msg;
1121 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1124 msg = camel_folder_get_message(folder, uid, &x);
1127 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1128 camel_object_unref((CamelObject *)msg);
1130 camel_exception_clear(&x);
1138 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1141 GPtrArray *matches = g_ptr_array_new();
1143 if (search->body_index) {
1145 struct _camel_search_words *simple;
1147 simple = camel_search_words_simple(words);
1148 indexed = match_words_index(search, simple, ex);
1149 camel_search_words_free(simple);
1151 for (i=0;i<indexed->len;i++) {
1152 const char *uid = g_ptr_array_index(indexed, i);
1154 if (match_words_message(search->folder, uid, words, ex))
1155 g_ptr_array_add(matches, (char *)uid);
1158 g_ptr_array_free(indexed, TRUE);
1160 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1162 for (i=0;i<v->len;i++) {
1163 CamelMessageInfo *info = g_ptr_array_index(v, i);
1164 const char *uid = camel_message_info_uid(info);
1166 if (match_words_message(search->folder, uid, words, ex))
1167 g_ptr_array_add(matches, (char *)uid);
1174 static ESExpResult *
1175 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1178 CamelException *ex = search->priv->ex;
1179 struct _camel_search_words *words;
1181 struct _glib_sux_donkeys lambdafoo;
1183 if (search->current) {
1186 if (argc == 1 && argv[0]->value.string[0] == 0) {
1189 for (i=0;i<argc && !truth;i++) {
1190 if (argv[i]->type == ESEXP_RES_STRING) {
1191 words = camel_search_words_split(argv[i]->value.string);
1193 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1194 for (j=0;j<words->len && truth;j++)
1195 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1197 /* TODO: cache current message incase of multiple body search terms */
1198 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1200 camel_search_words_free(words);
1204 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1205 r->value.bool = truth;
1207 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1208 r->value.ptrarray = g_ptr_array_new();
1210 if (argc == 1 && argv[0]->value.string[0] == 0) {
1211 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1213 for (i=0;i<v->len;i++) {
1214 CamelMessageInfo *info = g_ptr_array_index(v, i);
1216 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
1219 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1222 for (i=0;i<argc;i++) {
1223 if (argv[i]->type == ESEXP_RES_STRING) {
1224 words = camel_search_words_split(argv[i]->value.string);
1225 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1226 matches = match_words_index(search, words, ex);
1228 matches = match_words_messages(search, words, ex);
1230 for (j=0;j<matches->len;j++)
1231 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1232 g_ptr_array_free(matches, TRUE);
1233 camel_search_words_free(words);
1236 lambdafoo.uids = r->value.ptrarray;
1237 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1238 g_hash_table_destroy(ht);
1245 static ESExpResult *
1246 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1251 r(printf("executing user-flag\n"));
1253 /* are we inside a match-all? */
1254 if (search->current) {
1256 /* performs an OR of all words */
1257 for (i=0;i<argc && !truth;i++) {
1258 if (argv[i]->type == ESEXP_RES_STRING
1259 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1264 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1265 r->value.bool = truth;
1267 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1268 r->value.ptrarray = g_ptr_array_new();
1274 static ESExpResult *
1275 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1279 r(printf ("executing system-flag\n"));
1281 if (search->current) {
1282 gboolean truth = FALSE;
1285 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1287 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1288 r->value.bool = truth;
1290 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1291 r->value.ptrarray = g_ptr_array_new ();
1297 static ESExpResult *
1298 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1300 const char *value = NULL;
1303 r(printf("executing user-tag\n"));
1306 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1308 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1309 r->value.string = g_strdup (value ? value : "");
1314 static ESExpResult *
1315 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1319 r(printf("executing get-sent-date\n"));
1321 /* are we inside a match-all? */
1323 r = e_sexp_result_new(f, ESEXP_RES_INT);
1325 r->value.number = camel_message_info_date_sent(s->current);
1327 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1328 r->value.ptrarray = g_ptr_array_new ();
1334 static ESExpResult *
1335 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1339 r(printf("executing get-received-date\n"));
1341 /* are we inside a match-all? */
1343 r = e_sexp_result_new(f, ESEXP_RES_INT);
1345 r->value.number = camel_message_info_date_received(s->current);
1347 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1348 r->value.ptrarray = g_ptr_array_new ();
1354 static ESExpResult *
1355 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1359 r(printf("executing get-current-date\n"));
1361 r = e_sexp_result_new(f, ESEXP_RES_INT);
1362 r->value.number = time (NULL);
1366 static ESExpResult *
1367 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1371 r(printf("executing get-size\n"));
1373 /* are we inside a match-all? */
1375 r = e_sexp_result_new (f, ESEXP_RES_INT);
1376 r->value.number = camel_message_info_size(s->current) / 1024;
1378 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1379 r->value.ptrarray = g_ptr_array_new ();
1385 static ESExpResult *
1386 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1391 r(printf("executing uid\n"));
1393 /* are we inside a match-all? */
1394 if (search->current) {
1396 const char *uid = camel_message_info_uid(search->current);
1398 /* performs an OR of all words */
1399 for (i=0;i<argc && !truth;i++) {
1400 if (argv[i]->type == ESEXP_RES_STRING
1401 && !strcmp(uid, argv[i]->value.string)) {
1406 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1407 r->value.bool = truth;
1409 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1410 r->value.ptrarray = g_ptr_array_new();
1411 for (i=0;i<argc;i++) {
1412 if (argv[i]->type == ESEXP_RES_STRING)
1413 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);