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?? */
365 && r->type == ESEXP_RES_ARRAY_PTR) {
366 d(printf("got result ...\n"));
367 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
368 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
369 of memory just storing the size tag that malloc assigns us and alignment padding, so this
370 gets around that (and is faster to allocate and free as a bonus) */
371 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
372 if (search->summary) {
373 /* reorder result in summary order */
374 results = g_hash_table_new(g_str_hash, g_str_equal);
375 for (i=0;i<r->value.ptrarray->len;i++) {
376 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
377 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), (void *)1);
379 for (i=0;i<search->summary->len;i++) {
380 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
381 char *uid = (char *)camel_message_info_uid(info);
382 if (g_hash_table_lookup(results, uid)) {
383 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
386 g_hash_table_destroy(results);
388 for (i=0;i<r->value.ptrarray->len;i++) {
389 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
390 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
393 e_sexp_result_free(search->sexp, r);
394 /* instead of putting the mempool_hash in the structure, we keep the api clean by
395 putting a reference to it in a hashtable. Lets us do some debugging and catch
396 unfree'd results as well. */
397 g_hash_table_insert(p->mempool_hash, matches, pool);
399 d(printf("no result!\n"));
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->match1 = (CamelMessageInfo *)info;
431 uids = camel_folder_search_execute_expression(search, expr, ex);
435 camel_folder_search_free_result(search, uids);
437 search->match1 = 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->match1 || search->current) {
499 uid = camel_message_info_uid(search->match1);
501 uid = camel_message_info_uid(search->current);
503 for (i=0;!found && i<v->len;i++) {
504 if (strcmp(uid, v->pdata[i]) == 0)
509 g_ptr_array_add(r->value.ptrarray, (char *)uid);
510 } else if (search->summary == NULL) {
511 g_warning("No summary set, 'not' against an array requires a summary");
513 /* 'not' against the whole summary */
514 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
516 CamelMessageInfo **m;
518 s = (char **)v->pdata;
519 for (i=0;i<v->len;i++)
520 g_hash_table_insert(have, s[i], s[i]);
523 m = (CamelMessageInfo **)v->pdata;
524 for (i=0;i<v->len;i++) {
525 char *uid = (char *)camel_message_info_uid(m[i]);
527 if (g_hash_table_lookup(have, uid) == NULL)
528 g_ptr_array_add(r->value.ptrarray, uid);
530 g_hash_table_destroy(have);
535 if (argv[0]->type == ESEXP_RES_BOOL)
536 res = ! argv[0]->value.bool;
538 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
542 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
543 r->value.bool = TRUE;
550 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
556 g_warning("match-all only takes a single argument, other arguments ignored");
558 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
559 r->value.ptrarray = g_ptr_array_new();
561 /* we are only matching a single message? */
562 if (search->match1) {
563 search->current = search->match1;
565 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
568 r1 = e_sexp_term_eval(f, argv[0]);
569 if (r1->type == ESEXP_RES_BOOL) {
571 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
573 g_warning("invalid syntax, matches require a single bool result");
574 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
576 e_sexp_result_free(f, r1);
578 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
580 search->current = NULL;
585 if (search->summary == NULL) {
586 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
587 g_warning("No summary supplied, match-all doesn't work with no summary");
592 /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
593 for (i=0;i<search->summary->len;i++) {
594 search->current = g_ptr_array_index(search->summary, i);
596 r1 = e_sexp_term_eval(f, argv[0]);
597 if (r1->type == ESEXP_RES_BOOL) {
599 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
601 g_warning("invalid syntax, matches require a single bool result");
602 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
604 e_sexp_result_free(f, r1);
606 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
609 search->current = NULL;
615 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
620 r(printf("executing check-header %d\n", how));
622 /* are we inside a match-all? */
623 if (search->current && argc>1
624 && argv[0]->type == ESEXP_RES_STRING) {
626 const char *header = NULL;
629 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
630 struct _camel_search_words *words;
632 /* only a subset of headers are supported .. */
633 headername = argv[0]->value.string;
634 if (!strcasecmp(headername, "subject")) {
635 header = camel_message_info_subject(search->current);
636 } else if (!strcasecmp(headername, "date")) {
637 /* FIXME: not a very useful form of the date */
638 sprintf(strbuf, "%d", (int)search->current->date_sent);
640 } else if (!strcasecmp(headername, "from")) {
641 header = camel_message_info_from(search->current);
642 type = CAMEL_SEARCH_TYPE_ADDRESS;
643 } else if (!strcasecmp(headername, "to")) {
644 header = camel_message_info_to(search->current);
645 type = CAMEL_SEARCH_TYPE_ADDRESS;
646 } else if (!strcasecmp(headername, "cc")) {
647 header = camel_message_info_cc(search->current);
648 type = CAMEL_SEARCH_TYPE_ADDRESS;
649 } else if (!strcasecmp(headername, "x-camel-mlist")) {
650 header = camel_message_info_mlist(search->current);
651 type = CAMEL_SEARCH_TYPE_MLIST;
653 e_sexp_resultv_free(f, argc, argv);
654 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
658 /* performs an OR of all words */
659 for (i=1;i<argc && !truth;i++) {
660 if (argv[i]->type == ESEXP_RES_STRING) {
661 if (argv[i]->value.string[0] == 0) {
663 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
664 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
665 words = camel_search_words_split(argv[i]->value.string);
667 for (j=0;j<words->len && truth;j++) {
668 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
670 camel_search_words_free(words);
672 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
678 /* TODO: else, find all matches */
680 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
681 r->value.bool = truth;
687 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
689 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
693 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
695 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
699 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
701 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
705 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
707 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
711 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
715 r(printf ("executing header-exists\n"));
717 if (search->current) {
718 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
719 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
720 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
723 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
724 r->value.ptrarray = g_ptr_array_new();
730 /* this is just to OR results together */
731 struct _glib_sux_donkeys {
736 /* or, store all unique values */
738 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
740 g_ptr_array_add(fuckup->uids, key);
743 /* and, only store duplicates */
745 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
747 if (value == fuckup->count)
748 g_ptr_array_add(fuckup->uids, key);
752 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
754 CamelIndexCursor *wc, *nc;
755 const char *word, *name;
758 wc = camel_index_words(idx);
760 while (!truth && (word = camel_index_cursor_next(wc))) {
761 if (camel_ustrstrcase(word,match) != NULL) {
762 /* perf: could have the wc cursor return the name cursor */
763 nc = camel_index_find(idx, word);
765 while (!truth && (name = camel_index_cursor_next(nc)))
766 truth = strcmp(name, uid) == 0;
767 camel_object_unref((CamelObject *)nc);
771 camel_object_unref((CamelObject *)wc);
778 "one two" "three" "four five"
787 /* returns messages which contain all words listed in words */
789 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
791 GPtrArray *result = g_ptr_array_new();
792 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
793 struct _glib_sux_donkeys lambdafoo;
794 CamelIndexCursor *wc, *nc;
795 const char *word, *name;
796 CamelMessageInfo *mi;
799 /* we can have a maximum of 32 words, as we use it as the AND mask */
801 wc = camel_index_words(search->body_index);
803 while ((word = camel_index_cursor_next(wc))) {
804 for (i=0;i<words->len;i++) {
805 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
806 /* perf: could have the wc cursor return the name cursor */
807 nc = camel_index_find(search->body_index, word);
809 while ((name = camel_index_cursor_next(nc))) {
810 mi = g_hash_table_lookup(search->summary_hash, name);
813 const char *uid = camel_message_info_uid(mi);
815 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
816 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
819 camel_object_unref((CamelObject *)nc);
824 camel_object_unref((CamelObject *)wc);
826 lambdafoo.uids = result;
827 lambdafoo.count = (1<<words->len) - 1;
828 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
829 g_hash_table_destroy(ht);
836 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
838 CamelDataWrapper *containee;
842 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
844 if (containee == NULL)
847 /* using the object types is more accurate than using the mime/types */
848 if (CAMEL_IS_MULTIPART (containee)) {
849 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
850 for (i = 0; i < parts && truth == FALSE; i++) {
851 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
853 truth = match_words_1message(part, words, mask);
855 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
856 /* for messages we only look at its contents */
857 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
858 } else if (header_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
859 /* for all other text parts, we look inside, otherwise we dont care */
860 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
862 /* FIXME: The match should be part of a stream op */
863 camel_data_wrapper_write_to_stream (containee, CAMEL_STREAM (mem));
864 camel_stream_write (CAMEL_STREAM (mem), "", 1);
865 for (i=0;i<words->len;i++) {
866 /* FIXME: This is horridly slow, and should use a real search algorithm */
867 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
869 /* shortcut a match */
870 if (*mask == (1<<(words->len))-1)
874 camel_object_unref (CAMEL_OBJECT (mem));
881 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
884 CamelMimeMessage *msg;
887 msg = camel_folder_get_message(folder, uid, ex);
890 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
891 camel_object_unref((CamelObject *)msg);
893 camel_exception_clear(ex);
901 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
904 GPtrArray *matches = g_ptr_array_new();
906 if (search->body_index) {
908 struct _camel_search_words *simple;
910 simple = camel_search_words_simple(words);
911 indexed = match_words_index(search, simple, ex);
912 camel_search_words_free(simple);
914 for (i=0;i<indexed->len;i++) {
915 const char *uid = g_ptr_array_index(indexed, i);
917 if (match_words_message(search->folder, uid, words, ex))
918 g_ptr_array_add(matches, (char *)uid);
921 g_ptr_array_free(indexed, TRUE);
923 for (i=0;i<search->summary->len;i++) {
924 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
925 const char *uid = camel_message_info_uid(info);
927 if (match_words_message(search->folder, uid, words, ex))
928 g_ptr_array_add(matches, (char *)uid);
936 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
939 CamelException *ex = search->priv->ex;
940 struct _camel_search_words *words;
942 struct _glib_sux_donkeys lambdafoo;
944 if (search->current) {
947 if (argc == 1 && argv[0]->value.string[0] == 0) {
950 for (i=0;i<argc && !truth;i++) {
951 if (argv[i]->type == ESEXP_RES_STRING) {
952 words = camel_search_words_split(argv[i]->value.string);
954 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
955 for (j=0;j<words->len && truth;j++)
956 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
958 /* TODO: cache current message incase of multiple body search terms */
959 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
961 camel_search_words_free(words);
965 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
966 r->value.bool = truth;
968 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
969 r->value.ptrarray = g_ptr_array_new();
971 if (argc == 1 && argv[0]->value.string[0] == 0) {
972 for (i=0;i<search->summary->len;i++) {
973 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
975 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
978 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
981 for (i=0;i<argc;i++) {
982 if (argv[i]->type == ESEXP_RES_STRING) {
983 words = camel_search_words_split(argv[i]->value.string);
984 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
985 matches = match_words_index(search, words, ex);
987 matches = match_words_messages(search, words, ex);
989 for (j=0;j<matches->len;j++)
990 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
991 g_ptr_array_free(matches, TRUE);
992 camel_search_words_free(words);
995 lambdafoo.uids = r->value.ptrarray;
996 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
997 g_hash_table_destroy(ht);
1004 static ESExpResult *
1005 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1010 r(printf("executing user-flag\n"));
1012 /* are we inside a match-all? */
1013 if (search->current) {
1015 /* performs an OR of all words */
1016 for (i=0;i<argc && !truth;i++) {
1017 if (argv[i]->type == ESEXP_RES_STRING
1018 && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
1023 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1024 r->value.bool = truth;
1026 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1027 r->value.ptrarray = g_ptr_array_new();
1033 static ESExpResult *
1034 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1038 r(printf ("executing system-flag\n"));
1040 if (search->current) {
1041 gboolean truth = FALSE;
1044 truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
1046 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1047 r->value.bool = truth;
1049 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1050 r->value.ptrarray = g_ptr_array_new ();
1056 static ESExpResult *
1057 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1059 const char *value = NULL;
1062 r(printf("executing user-tag\n"));
1065 value = camel_tag_get (&search->current->user_tags, argv[0]->value.string);
1067 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1068 r->value.string = g_strdup (value ? value : "");
1073 static ESExpResult *
1074 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1078 r(printf("executing get-sent-date\n"));
1080 /* are we inside a match-all? */
1082 r = e_sexp_result_new(f, ESEXP_RES_INT);
1084 r->value.number = s->current->date_sent;
1086 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1087 r->value.ptrarray = g_ptr_array_new ();
1093 static ESExpResult *
1094 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1098 r(printf("executing get-received-date\n"));
1100 /* are we inside a match-all? */
1102 r = e_sexp_result_new(f, ESEXP_RES_INT);
1104 r->value.number = s->current->date_received;
1106 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1107 r->value.ptrarray = g_ptr_array_new ();
1113 static ESExpResult *
1114 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1118 r(printf("executing get-current-date\n"));
1120 r = e_sexp_result_new(f, ESEXP_RES_INT);
1121 r->value.number = time (NULL);
1125 static ESExpResult *
1126 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1130 r(printf("executing get-size\n"));
1132 /* are we inside a match-all? */
1134 r = e_sexp_result_new (f, ESEXP_RES_INT);
1135 r->value.number = s->current->size / 1024;
1137 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1138 r->value.ptrarray = g_ptr_array_new ();
1144 static ESExpResult *
1145 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1150 r(printf("executing uid\n"));
1152 /* are we inside a match-all? */
1153 if (search->current) {
1155 const char *uid = camel_message_info_uid(search->current);
1157 /* performs an OR of all words */
1158 for (i=0;i<argc && !truth;i++) {
1159 if (argv[i]->type == ESEXP_RES_STRING
1160 && !strcmp(uid, argv[i]->value.string)) {
1165 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1166 r->value.bool = truth;
1168 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1169 r->value.ptrarray = g_ptr_array_new();
1170 for (i=0;i<argc;i++) {
1171 if (argv[i]->type == ESEXP_RES_STRING)
1172 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);