2 * Copyright (C) 2000,2001 Ximian Inc.
4 * Authors: Michael Zucchi <notzed@ximian.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 /* This is a helper class for folders to implement the search function.
22 It implements enough to do basic searches on folders that can provide
23 an in-memory summary and a body index. */
32 #include <sys/types.h>
37 #include "camel-folder-search.h"
39 #include "camel-exception.h"
40 #include "camel-medium.h"
41 #include "camel-multipart.h"
42 #include "camel-mime-message.h"
43 #include "camel-stream-mem.h"
44 #include "e-util/e-memory.h"
45 #include "camel-search-private.h"
50 struct _CamelFolderSearchPrivate {
51 GHashTable *mempool_hash;
55 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
57 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
59 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
60 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
61 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
62 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
63 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
64 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
65 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
66 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
67 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
68 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
69 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
70 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
71 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
72 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
73 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
75 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
78 static void camel_folder_search_init (CamelFolderSearch *obj);
79 static void camel_folder_search_finalize (CamelObject *obj);
81 static CamelObjectClass *camel_folder_search_parent;
84 camel_folder_search_class_init (CamelFolderSearchClass *klass)
86 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
88 klass->not = search_not;
90 klass->match_all = search_match_all;
91 klass->body_contains = search_body_contains;
92 klass->header_contains = search_header_contains;
93 klass->header_matches = search_header_matches;
94 klass->header_starts_with = search_header_starts_with;
95 klass->header_ends_with = search_header_ends_with;
96 klass->header_exists = search_header_exists;
97 klass->user_tag = search_user_tag;
98 klass->user_flag = search_user_flag;
99 klass->system_flag = search_system_flag;
100 klass->get_sent_date = search_get_sent_date;
101 klass->get_received_date = search_get_received_date;
102 klass->get_current_date = search_get_current_date;
103 klass->get_size = search_get_size;
104 klass->uid = search_uid;
108 camel_folder_search_init (CamelFolderSearch *obj)
110 struct _CamelFolderSearchPrivate *p;
112 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
114 obj->sexp = e_sexp_new();
116 /* use a hash of mempools to associate the returned uid lists with
117 the backing mempool. yes pretty weird, but i didn't want to change
120 p->mempool_hash = g_hash_table_new(0, 0);
124 free_mempool(void *key, void *value, void *data)
126 GPtrArray *uids = key;
127 EMemPool *pool = value;
129 g_warning("Search closed with outstanding result unfreed: %p", uids);
131 g_ptr_array_free(uids, TRUE);
132 e_mempool_destroy(pool);
136 camel_folder_search_finalize (CamelObject *obj)
138 CamelFolderSearch *search = (CamelFolderSearch *)obj;
139 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
142 e_sexp_unref(search->sexp);
143 if (search->summary_hash)
144 g_hash_table_destroy(search->summary_hash);
146 g_free(search->last_search);
147 g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
148 g_hash_table_destroy(p->mempool_hash);
153 camel_folder_search_get_type (void)
155 static CamelType type = CAMEL_INVALID_TYPE;
157 if (type == CAMEL_INVALID_TYPE) {
158 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
159 sizeof (CamelFolderSearch),
160 sizeof (CamelFolderSearchClass),
161 (CamelObjectClassInitFunc) camel_folder_search_class_init,
163 (CamelObjectInitFunc) camel_folder_search_init,
164 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
171 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
173 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
179 int flags; /* 0x02 = immediate, 0x01 = always enter */
181 /* these have default implementations in e-sexp */
182 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
183 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
184 /* we need to override this one though to implement an 'array not' */
185 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
186 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
187 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
188 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
190 /* these we have to use our own default if there is none */
191 /* they should all be defined in the language? so it parses, or should they not?? */
192 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
193 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
194 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
195 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
196 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
197 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
198 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
199 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
200 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
201 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
202 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
203 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
204 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
205 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
206 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
210 camel_folder_search_construct (CamelFolderSearch *search)
213 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
215 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
217 /* c is sure messy sometimes */
218 func = *((void **)(((char *)klass)+builtins[i].offset));
219 if (func == NULL && builtins[i].flags&1) {
220 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
221 func = (void *)search_dummy;
224 if (builtins[i].flags&2) {
225 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
227 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
234 * camel_folder_search_new:
236 * Create a new CamelFolderSearch object.
238 * A CamelFolderSearch is a subclassable, extensible s-exp
239 * evaluator which enforces a particular set of s-expressions.
240 * Particular methods may be overriden by an implementation to
241 * implement a search for any sort of backend.
243 * Return value: A new CamelFolderSearch widget.
246 camel_folder_search_new (void)
248 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
250 camel_folder_search_construct(new);
255 * camel_folder_search_set_folder:
259 * Set the folder attribute of the search. This is currently unused, but
260 * could be used to perform a slow-search when indexes and so forth are not
261 * available. Or for use by subclasses.
264 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
266 search->folder = folder;
270 * camel_folder_search_set_summary:
272 * @summary: An array of CamelMessageInfo pointers.
274 * Set the array of summary objects representing the span of the search.
276 * If this is not set, then a subclass must provide the functions
277 * for searching headers and for the match-all operator.
280 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
284 search->summary = summary;
285 if (search->summary_hash)
286 g_hash_table_destroy(search->summary_hash);
287 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
288 for (i=0;i<summary->len;i++)
289 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary->pdata[i]), summary->pdata[i]);
293 * camel_folder_search_set_body_index:
297 * Set the index representing the contents of all messages
298 * in this folder. If this is not set, then the folder implementation
299 * should sub-class the CamelFolderSearch and provide its own
300 * body-contains function.
303 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
305 if (search->body_index)
306 camel_object_unref((CamelObject *)search->body_index);
307 search->body_index = index;
309 camel_object_ref((CamelObject *)index);
313 * camel_folder_search_execute_expression:
318 * Execute the search expression @expr, returning an array of
319 * all matches as a GPtrArray of uid's of matching messages.
321 * Note that any settings such as set_body_index(), set_folder(),
322 * and so on are reset to #NULL once the search has completed.
324 * TODO: The interface should probably return summary items instead
325 * (since they are much more useful to any client).
327 * Return value: A GPtrArray of strings of all matching messages.
328 * This must only be freed by camel_folder_search_free_result.
331 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
338 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
342 /* only re-parse if the search has changed */
343 if (search->last_search == NULL
344 || strcmp(search->last_search, expr)) {
345 e_sexp_input_text(search->sexp, expr, strlen(expr));
346 if (e_sexp_parse(search->sexp) == -1) {
347 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
351 g_free(search->last_search);
352 search->last_search = g_strdup(expr);
354 r = e_sexp_eval(search->sexp);
356 if (!camel_exception_is_set(ex))
357 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
361 matches = g_ptr_array_new();
363 /* now create a folder summary to return?? */
364 if (r->type == ESEXP_RES_ARRAY_PTR) {
365 d(printf("got result ...\n"));
366 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
367 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
368 of memory just storing the size tag that malloc assigns us and alignment padding, so this
369 gets around that (and is faster to allocate and free as a bonus) */
370 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
371 if (search->summary) {
372 /* reorder result in summary order */
373 results = g_hash_table_new(g_str_hash, g_str_equal);
374 for (i=0;i<r->value.ptrarray->len;i++) {
375 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
376 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), (void *)1);
378 for (i=0;i<search->summary->len;i++) {
379 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
380 char *uid = (char *)camel_message_info_uid(info);
381 if (g_hash_table_lookup(results, uid)) {
382 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
385 g_hash_table_destroy(results);
387 for (i=0;i<r->value.ptrarray->len;i++) {
388 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
389 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
392 /* instead of putting the mempool_hash in the structure, we keep the api clean by
393 putting a reference to it in a hashtable. Lets us do some debugging and catch
394 unfree'd results as well. */
395 g_hash_table_insert(p->mempool_hash, matches, pool);
397 g_warning("Search returned an invalid result type");
400 e_sexp_result_free(search->sexp, r);
402 search->folder = NULL;
403 search->summary = NULL;
404 search->current = NULL;
405 search->body_index = NULL;
411 * camel_folder_search_match_expression:
417 * Returns #TRUE if the expression matches the specific message info @info.
418 * Note that the folder and index may need to be set for body searches to
424 camel_folder_search_match_expression(CamelFolderSearch *search, const char *expr, const CamelMessageInfo *info, CamelException *ex)
429 search->current = (CamelMessageInfo *)info;
431 uids = camel_folder_search_execute_expression(search, expr, ex);
435 camel_folder_search_free_result(search, uids);
437 search->current = NULL;
442 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
445 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
448 pool = g_hash_table_lookup(p->mempool_hash, result);
450 e_mempool_destroy(pool);
451 g_hash_table_remove(p->mempool_hash, result);
453 for (i=0;i<result->len;i++)
454 g_free(g_ptr_array_index(result, i));
456 g_ptr_array_free(result, TRUE);
462 /* dummy function, returns false always, or an empty match array */
464 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
468 if (search->current == NULL) {
469 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
470 r->value.bool = FALSE;
472 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
473 r->value.ptrarray = g_ptr_array_new();
479 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
481 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
487 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
488 GPtrArray *v = argv[0]->value.ptrarray;
491 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
492 r->value.ptrarray = g_ptr_array_new();
494 /* not against a single message?*/
495 if (search->current) {
498 uid = camel_message_info_uid(search->current);
499 for (i=0;!found && i<v->len;i++) {
500 if (strcmp(uid, v->pdata[i]) == 0)
505 g_ptr_array_add(r->value.ptrarray, (char *)uid);
506 } else if (search->summary == NULL) {
507 g_warning("No summary set, 'not' against an array requires a summary");
509 /* 'not' against the whole summary */
510 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
512 CamelMessageInfo **m;
514 s = (char **)v->pdata;
515 for (i=0;i<v->len;i++)
516 g_hash_table_insert(have, s[i], s[i]);
519 m = (CamelMessageInfo **)v->pdata;
520 for (i=0;i<v->len;i++) {
521 char *uid = (char *)camel_message_info_uid(m[i]);
523 if (g_hash_table_lookup(have, uid) == NULL)
524 g_ptr_array_add(r->value.ptrarray, uid);
526 g_hash_table_destroy(have);
531 if (argv[0]->type == ESEXP_RES_BOOL)
532 res = ! argv[0]->value.bool;
534 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
538 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
539 r->value.bool = TRUE;
546 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
552 g_warning("match-all only takes a single argument, other arguments ignored");
555 /* we are only matching a single message? or already inside a match-all? */
556 if (search->current) {
557 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
559 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
560 r->value.bool = FALSE;
563 r1 = e_sexp_term_eval(f, argv[0]);
564 if (r1->type == ESEXP_RES_BOOL) {
565 r->value.bool = r1->value.bool;
567 g_warning("invalid syntax, matches require a single bool result");
568 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
570 e_sexp_result_free(f, r1);
572 r->value.bool = TRUE;
577 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
578 r->value.ptrarray = g_ptr_array_new();
580 if (search->summary == NULL) {
581 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
582 g_warning("No summary supplied, match-all doesn't work with no summary");
587 /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
588 for (i=0;i<search->summary->len;i++) {
589 search->current = g_ptr_array_index(search->summary, i);
591 r1 = e_sexp_term_eval(f, argv[0]);
592 if (r1->type == ESEXP_RES_BOOL) {
594 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
596 g_warning("invalid syntax, matches require a single bool result");
597 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
599 e_sexp_result_free(f, r1);
601 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
604 search->current = NULL;
610 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
615 r(printf("executing check-header %d\n", how));
617 /* are we inside a match-all? */
618 if (search->current && argc>1
619 && argv[0]->type == ESEXP_RES_STRING) {
621 const char *header = NULL;
624 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
625 struct _camel_search_words *words;
627 /* only a subset of headers are supported .. */
628 headername = argv[0]->value.string;
629 if (!strcasecmp(headername, "subject")) {
630 header = camel_message_info_subject(search->current);
631 } else if (!strcasecmp(headername, "date")) {
632 /* FIXME: not a very useful form of the date */
633 sprintf(strbuf, "%d", (int)search->current->date_sent);
635 } else if (!strcasecmp(headername, "from")) {
636 header = camel_message_info_from(search->current);
637 type = CAMEL_SEARCH_TYPE_ADDRESS;
638 } else if (!strcasecmp(headername, "to")) {
639 header = camel_message_info_to(search->current);
640 type = CAMEL_SEARCH_TYPE_ADDRESS;
641 } else if (!strcasecmp(headername, "cc")) {
642 header = camel_message_info_cc(search->current);
643 type = CAMEL_SEARCH_TYPE_ADDRESS;
644 } else if (!strcasecmp(headername, "x-camel-mlist")) {
645 header = camel_message_info_mlist(search->current);
646 type = CAMEL_SEARCH_TYPE_MLIST;
648 e_sexp_resultv_free(f, argc, argv);
649 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
653 /* performs an OR of all words */
654 for (i=1;i<argc && !truth;i++) {
655 if (argv[i]->type == ESEXP_RES_STRING) {
656 if (argv[i]->value.string[0] == 0) {
658 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
659 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
660 words = camel_search_words_split(argv[i]->value.string);
662 for (j=0;j<words->len && truth;j++) {
663 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
665 camel_search_words_free(words);
667 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
673 /* TODO: else, find all matches */
675 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
676 r->value.bool = truth;
682 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
684 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
688 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
690 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
694 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
696 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
700 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
702 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
706 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
710 r(printf ("executing header-exists\n"));
712 if (search->current) {
713 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
714 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
715 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
718 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
719 r->value.ptrarray = g_ptr_array_new();
725 /* this is just to OR results together */
726 struct _glib_sux_donkeys {
731 /* or, store all unique values */
733 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
735 g_ptr_array_add(fuckup->uids, key);
738 /* and, only store duplicates */
740 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
742 if (value == fuckup->count)
743 g_ptr_array_add(fuckup->uids, key);
747 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
749 CamelIndexCursor *wc, *nc;
750 const char *word, *name;
753 wc = camel_index_words(idx);
755 while (!truth && (word = camel_index_cursor_next(wc))) {
756 if (camel_ustrstrcase(word,match) != NULL) {
757 /* perf: could have the wc cursor return the name cursor */
758 nc = camel_index_find(idx, word);
760 while (!truth && (name = camel_index_cursor_next(nc)))
761 truth = strcmp(name, uid) == 0;
762 camel_object_unref((CamelObject *)nc);
766 camel_object_unref((CamelObject *)wc);
773 "one two" "three" "four five"
782 /* returns messages which contain all words listed in words */
784 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
786 GPtrArray *result = g_ptr_array_new();
787 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
788 struct _glib_sux_donkeys lambdafoo;
789 CamelIndexCursor *wc, *nc;
790 const char *word, *name;
791 CamelMessageInfo *mi;
794 /* we can have a maximum of 32 words, as we use it as the AND mask */
796 wc = camel_index_words(search->body_index);
798 while ((word = camel_index_cursor_next(wc))) {
799 for (i=0;i<words->len;i++) {
800 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
801 /* perf: could have the wc cursor return the name cursor */
802 nc = camel_index_find(search->body_index, word);
804 while ((name = camel_index_cursor_next(nc))) {
805 mi = g_hash_table_lookup(search->summary_hash, name);
808 const char *uid = camel_message_info_uid(mi);
810 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
811 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
814 camel_object_unref((CamelObject *)nc);
819 camel_object_unref((CamelObject *)wc);
821 lambdafoo.uids = result;
822 lambdafoo.count = (1<<words->len) - 1;
823 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
824 g_hash_table_destroy(ht);
831 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
833 CamelDataWrapper *containee;
837 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
839 if (containee == NULL)
842 /* using the object types is more accurate than using the mime/types */
843 if (CAMEL_IS_MULTIPART (containee)) {
844 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
845 for (i = 0; i < parts && truth == FALSE; i++) {
846 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
848 truth = match_words_1message(part, words, mask);
850 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
851 /* for messages we only look at its contents */
852 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
853 } else if (header_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
854 /* for all other text parts, we look inside, otherwise we dont care */
855 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
857 /* FIXME: The match should be part of a stream op */
858 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
859 camel_stream_write (CAMEL_STREAM (mem), "", 1);
860 for (i=0;i<words->len;i++) {
861 /* FIXME: This is horridly slow, and should use a real search algorithm */
862 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
864 /* shortcut a match */
865 if (*mask == (1<<(words->len))-1)
869 camel_object_unref (CAMEL_OBJECT (mem));
876 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
879 CamelMimeMessage *msg;
882 msg = camel_folder_get_message(folder, uid, ex);
885 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
886 camel_object_unref((CamelObject *)msg);
888 camel_exception_clear(ex);
896 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
899 GPtrArray *matches = g_ptr_array_new();
901 if (search->body_index) {
903 struct _camel_search_words *simple;
905 simple = camel_search_words_simple(words);
906 indexed = match_words_index(search, simple, ex);
907 camel_search_words_free(simple);
909 for (i=0;i<indexed->len;i++) {
910 const char *uid = g_ptr_array_index(indexed, i);
912 if (match_words_message(search->folder, uid, words, ex))
913 g_ptr_array_add(matches, (char *)uid);
916 g_ptr_array_free(indexed, TRUE);
918 for (i=0;i<search->summary->len;i++) {
919 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
920 const char *uid = camel_message_info_uid(info);
922 if (match_words_message(search->folder, uid, words, ex))
923 g_ptr_array_add(matches, (char *)uid);
931 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
934 CamelException *ex = search->priv->ex;
935 struct _camel_search_words *words;
937 struct _glib_sux_donkeys lambdafoo;
939 if (search->current) {
942 if (argc == 1 && argv[0]->value.string[0] == 0) {
945 for (i=0;i<argc && !truth;i++) {
946 if (argv[i]->type == ESEXP_RES_STRING) {
947 words = camel_search_words_split(argv[i]->value.string);
949 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
950 for (j=0;j<words->len && truth;j++)
951 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
953 /* TODO: cache current message incase of multiple body search terms */
954 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
956 camel_search_words_free(words);
960 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
961 r->value.bool = truth;
963 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
964 r->value.ptrarray = g_ptr_array_new();
966 if (argc == 1 && argv[0]->value.string[0] == 0) {
967 for (i=0;i<search->summary->len;i++) {
968 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
970 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
973 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
976 for (i=0;i<argc;i++) {
977 if (argv[i]->type == ESEXP_RES_STRING) {
978 words = camel_search_words_split(argv[i]->value.string);
979 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
980 matches = match_words_index(search, words, ex);
982 matches = match_words_messages(search, words, ex);
984 for (j=0;j<matches->len;j++)
985 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
986 g_ptr_array_free(matches, TRUE);
987 camel_search_words_free(words);
990 lambdafoo.uids = r->value.ptrarray;
991 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
992 g_hash_table_destroy(ht);
1000 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1005 r(printf("executing user-flag\n"));
1007 /* are we inside a match-all? */
1008 if (search->current) {
1010 /* performs an OR of all words */
1011 for (i=0;i<argc && !truth;i++) {
1012 if (argv[i]->type == ESEXP_RES_STRING
1013 && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
1018 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1019 r->value.bool = truth;
1021 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1022 r->value.ptrarray = g_ptr_array_new();
1028 static ESExpResult *
1029 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1033 r(printf ("executing system-flag\n"));
1035 if (search->current) {
1036 gboolean truth = FALSE;
1039 truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
1041 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1042 r->value.bool = truth;
1044 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1045 r->value.ptrarray = g_ptr_array_new ();
1051 static ESExpResult *
1052 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1054 const char *value = NULL;
1057 r(printf("executing user-tag\n"));
1060 value = camel_tag_get (&search->current->user_tags, argv[0]->value.string);
1062 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1063 r->value.string = g_strdup (value ? value : "");
1068 static ESExpResult *
1069 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1073 r(printf("executing get-sent-date\n"));
1075 /* are we inside a match-all? */
1077 r = e_sexp_result_new(f, ESEXP_RES_INT);
1079 r->value.number = s->current->date_sent;
1081 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1082 r->value.ptrarray = g_ptr_array_new ();
1088 static ESExpResult *
1089 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1093 r(printf("executing get-received-date\n"));
1095 /* are we inside a match-all? */
1097 r = e_sexp_result_new(f, ESEXP_RES_INT);
1099 r->value.number = s->current->date_received;
1101 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1102 r->value.ptrarray = g_ptr_array_new ();
1108 static ESExpResult *
1109 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1113 r(printf("executing get-current-date\n"));
1115 r = e_sexp_result_new(f, ESEXP_RES_INT);
1116 r->value.number = time (NULL);
1120 static ESExpResult *
1121 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1125 r(printf("executing get-size\n"));
1127 /* are we inside a match-all? */
1129 r = e_sexp_result_new (f, ESEXP_RES_INT);
1130 r->value.number = s->current->size / 1024;
1132 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1133 r->value.ptrarray = g_ptr_array_new ();
1139 static ESExpResult *
1140 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1145 r(printf("executing uid\n"));
1147 /* are we inside a match-all? */
1148 if (search->current) {
1150 const char *uid = camel_message_info_uid(search->current);
1152 /* performs an OR of all words */
1153 for (i=0;i<argc && !truth;i++) {
1154 if (argv[i]->type == ESEXP_RES_STRING
1155 && !strcmp(uid, argv[i]->value.string)) {
1160 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1161 r->value.bool = truth;
1163 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1164 r->value.ptrarray = g_ptr_array_new();
1165 for (i=0;i<argc;i++) {
1166 if (argv[i]->type == ESEXP_RES_STRING)
1167 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);