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"
38 #include "string-utils.h"
40 #include "camel-exception.h"
41 #include "camel-medium.h"
42 #include "camel-multipart.h"
43 #include "camel-mime-message.h"
44 #include "camel-stream-mem.h"
45 #include "e-util/e-memory.h"
46 #include "camel-search-private.h"
51 struct _CamelFolderSearchPrivate {
52 GHashTable *mempool_hash;
56 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
58 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
60 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
61 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
62 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
63 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
64 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
65 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
66 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
67 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
68 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
69 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
70 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
71 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
72 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
73 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
74 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
76 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
79 static void camel_folder_search_init (CamelFolderSearch *obj);
80 static void camel_folder_search_finalize (CamelObject *obj);
82 static CamelObjectClass *camel_folder_search_parent;
85 camel_folder_search_class_init (CamelFolderSearchClass *klass)
87 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
89 klass->not = search_not;
91 klass->match_all = search_match_all;
92 klass->body_contains = search_body_contains;
93 klass->header_contains = search_header_contains;
94 klass->header_matches = search_header_matches;
95 klass->header_starts_with = search_header_starts_with;
96 klass->header_ends_with = search_header_ends_with;
97 klass->header_exists = search_header_exists;
98 klass->user_tag = search_user_tag;
99 klass->user_flag = search_user_flag;
100 klass->system_flag = search_system_flag;
101 klass->get_sent_date = search_get_sent_date;
102 klass->get_received_date = search_get_received_date;
103 klass->get_current_date = search_get_current_date;
104 klass->get_size = search_get_size;
105 klass->uid = search_uid;
109 camel_folder_search_init (CamelFolderSearch *obj)
111 struct _CamelFolderSearchPrivate *p;
113 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
115 obj->sexp = e_sexp_new();
117 /* use a hash of mempools to associate the returned uid lists with
118 the backing mempool. yes pretty weird, but i didn't want to change
121 p->mempool_hash = g_hash_table_new(0, 0);
125 free_mempool(void *key, void *value, void *data)
127 GPtrArray *uids = key;
128 EMemPool *pool = value;
130 g_warning("Search closed with outstanding result unfreed: %p", uids);
132 g_ptr_array_free(uids, TRUE);
133 e_mempool_destroy(pool);
137 camel_folder_search_finalize (CamelObject *obj)
139 CamelFolderSearch *search = (CamelFolderSearch *)obj;
140 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
143 e_sexp_unref(search->sexp);
144 if (search->summary_hash)
145 g_hash_table_destroy(search->summary_hash);
147 g_free(search->last_search);
148 g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
149 g_hash_table_destroy(p->mempool_hash);
154 camel_folder_search_get_type (void)
156 static CamelType type = CAMEL_INVALID_TYPE;
158 if (type == CAMEL_INVALID_TYPE) {
159 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
160 sizeof (CamelFolderSearch),
161 sizeof (CamelFolderSearchClass),
162 (CamelObjectClassInitFunc) camel_folder_search_class_init,
164 (CamelObjectInitFunc) camel_folder_search_init,
165 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
172 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
174 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
180 int flags; /* 0x02 = immediate, 0x01 = always enter */
182 /* these have default implementations in e-sexp */
183 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
184 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
185 /* we need to override this one though to implement an 'array not' */
186 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
187 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
188 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
189 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
191 /* these we have to use our own default if there is none */
192 /* they should all be defined in the language? so it parses, or should they not?? */
193 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
194 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
195 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
196 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
197 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
198 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
199 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
200 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
201 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
202 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
203 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
204 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
205 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
206 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
207 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
211 camel_folder_search_construct (CamelFolderSearch *search)
214 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
216 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
218 /* c is sure messy sometimes */
219 func = *((void **)(((char *)klass)+builtins[i].offset));
220 if (func == NULL && builtins[i].flags&1) {
221 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
222 func = (void *)search_dummy;
225 if (builtins[i].flags&2) {
226 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
228 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
235 * camel_folder_search_new:
237 * Create a new CamelFolderSearch object.
239 * A CamelFolderSearch is a subclassable, extensible s-exp
240 * evaluator which enforces a particular set of s-expressions.
241 * Particular methods may be overriden by an implementation to
242 * implement a search for any sort of backend.
244 * Return value: A new CamelFolderSearch widget.
247 camel_folder_search_new (void)
249 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
251 camel_folder_search_construct(new);
256 * camel_folder_search_set_folder:
260 * Set the folder attribute of the search. This is currently unused, but
261 * could be used to perform a slow-search when indexes and so forth are not
262 * available. Or for use by subclasses.
265 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
267 search->folder = folder;
271 * camel_folder_search_set_summary:
273 * @summary: An array of CamelMessageInfo pointers.
275 * Set the array of summary objects representing the span of the search.
277 * If this is not set, then a subclass must provide the functions
278 * for searching headers and for the match-all operator.
281 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
285 search->summary = summary;
286 if (search->summary_hash)
287 g_hash_table_destroy(search->summary_hash);
288 search->summary_hash = g_hash_table_new(g_str_hash, g_str_equal);
289 for (i=0;i<summary->len;i++)
290 g_hash_table_insert(search->summary_hash, (char *)camel_message_info_uid(summary->pdata[i]), summary->pdata[i]);
294 * camel_folder_search_set_body_index:
298 * Set the index representing the contents of all messages
299 * in this folder. If this is not set, then the folder implementation
300 * should sub-class the CamelFolderSearch and provide its own
301 * body-contains function.
304 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
306 if (search->body_index)
307 camel_object_unref((CamelObject *)search->body_index);
308 search->body_index = index;
310 camel_object_ref((CamelObject *)index);
314 * camel_folder_search_execute_expression:
319 * Execute the search expression @expr, returning an array of
320 * all matches as a GPtrArray of uid's of matching messages.
322 * Note that any settings such as set_body_index(), set_folder(),
323 * and so on are reset to #NULL once the search has completed.
325 * TODO: The interface should probably return summary items instead
326 * (since they are much more useful to any client).
328 * Return value: A GPtrArray of strings of all matching messages.
329 * This must only be freed by camel_folder_search_free_result.
332 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
339 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
343 /* only re-parse if the search has changed */
344 if (search->last_search == NULL
345 || strcmp(search->last_search, expr)) {
346 e_sexp_input_text(search->sexp, expr, strlen(expr));
347 if (e_sexp_parse(search->sexp) == -1) {
348 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
352 g_free(search->last_search);
353 search->last_search = g_strdup(expr);
355 r = e_sexp_eval(search->sexp);
357 if (!camel_exception_is_set(ex))
358 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
362 matches = g_ptr_array_new();
364 /* now create a folder summary to return?? */
366 && r->type == ESEXP_RES_ARRAY_PTR) {
367 d(printf("got result ...\n"));
368 /* we use a mempool to store the strings, packed in tight as possible, and freed together */
369 /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
370 of memory just storing the size tag that malloc assigns us and alignment padding, so this
371 gets around that (and is faster to allocate and free as a bonus) */
372 pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
373 if (search->summary) {
374 /* reorder result in summary order */
375 results = g_hash_table_new(g_str_hash, g_str_equal);
376 for (i=0;i<r->value.ptrarray->len;i++) {
377 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
378 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), (void *)1);
380 for (i=0;i<search->summary->len;i++) {
381 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
382 char *uid = (char *)camel_message_info_uid(info);
383 if (g_hash_table_lookup(results, uid)) {
384 g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
387 g_hash_table_destroy(results);
389 for (i=0;i<r->value.ptrarray->len;i++) {
390 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
391 g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
394 e_sexp_result_free(search->sexp, r);
395 /* instead of putting the mempool_hash in the structure, we keep the api clean by
396 putting a reference to it in a hashtable. Lets us do some debugging and catch
397 unfree'd results as well. */
398 g_hash_table_insert(p->mempool_hash, matches, pool);
400 d(printf("no result!\n"));
403 search->folder = NULL;
404 search->summary = NULL;
405 search->current = NULL;
406 search->body_index = NULL;
412 * camel_folder_search_match_expression:
418 * Returns #TRUE if the expression matches the specific message info @info.
419 * Note that the folder and index may need to be set for body searches to
425 camel_folder_search_match_expression(CamelFolderSearch *search, const char *expr, const CamelMessageInfo *info, CamelException *ex)
430 search->match1 = (CamelMessageInfo *)info;
432 uids = camel_folder_search_execute_expression(search, expr, ex);
436 camel_folder_search_free_result(search, uids);
438 search->match1 = NULL;
443 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
446 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
449 pool = g_hash_table_lookup(p->mempool_hash, result);
451 e_mempool_destroy(pool);
452 g_hash_table_remove(p->mempool_hash, result);
454 for (i=0;i<result->len;i++)
455 g_free(g_ptr_array_index(result, i));
457 g_ptr_array_free(result, TRUE);
463 /* dummy function, returns false always, or an empty match array */
465 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
469 if (search->current == NULL) {
470 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
471 r->value.bool = FALSE;
473 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
474 r->value.ptrarray = g_ptr_array_new();
480 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
482 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
488 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
489 GPtrArray *v = argv[0]->value.ptrarray;
492 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
493 r->value.ptrarray = g_ptr_array_new();
495 /* not against a single message?*/
496 if (search->match1 || search->current) {
500 uid = camel_message_info_uid(search->match1);
502 uid = camel_message_info_uid(search->current);
504 for (i=0;!found && i<v->len;i++) {
505 if (strcmp(uid, v->pdata[i]) == 0)
510 g_ptr_array_add(r->value.ptrarray, (char *)uid);
511 } else if (search->summary == NULL) {
512 g_warning("No summary set, 'not' against an array requires a summary");
514 /* 'not' against the whole summary */
515 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
517 CamelMessageInfo **m;
519 s = (char **)v->pdata;
520 for (i=0;i<v->len;i++)
521 g_hash_table_insert(have, s[i], s[i]);
524 m = (CamelMessageInfo **)v->pdata;
525 for (i=0;i<v->len;i++) {
526 char *uid = (char *)camel_message_info_uid(m[i]);
528 if (g_hash_table_lookup(have, uid) == NULL)
529 g_ptr_array_add(r->value.ptrarray, uid);
531 g_hash_table_destroy(have);
536 if (argv[0]->type == ESEXP_RES_BOOL)
537 res = ! argv[0]->value.bool;
539 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
543 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
544 r->value.bool = TRUE;
551 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
557 g_warning("match-all only takes a single argument, other arguments ignored");
559 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
560 r->value.ptrarray = g_ptr_array_new();
562 /* we are only matching a single message? */
563 if (search->match1) {
564 search->current = search->match1;
566 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
569 r1 = e_sexp_term_eval(f, argv[0]);
570 if (r1->type == ESEXP_RES_BOOL) {
572 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
574 g_warning("invalid syntax, matches require a single bool result");
575 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
577 e_sexp_result_free(f, r1);
579 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
581 search->current = NULL;
586 if (search->summary == NULL) {
587 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
588 g_warning("No summary supplied, match-all doesn't work with no summary");
593 /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
594 for (i=0;i<search->summary->len;i++) {
595 search->current = g_ptr_array_index(search->summary, i);
597 r1 = e_sexp_term_eval(f, argv[0]);
598 if (r1->type == ESEXP_RES_BOOL) {
600 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
602 g_warning("invalid syntax, matches require a single bool result");
603 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
605 e_sexp_result_free(f, r1);
607 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
610 search->current = NULL;
616 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
621 r(printf("executing check-header %d\n", how));
623 /* are we inside a match-all? */
624 if (search->current && argc>1
625 && argv[0]->type == ESEXP_RES_STRING) {
627 const char *header = NULL;
630 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
631 struct _camel_search_words *words;
633 /* only a subset of headers are supported .. */
634 headername = argv[0]->value.string;
635 if (!strcasecmp(headername, "subject")) {
636 header = camel_message_info_subject(search->current);
637 } else if (!strcasecmp(headername, "date")) {
638 /* FIXME: not a very useful form of the date */
639 sprintf(strbuf, "%d", (int)search->current->date_sent);
641 } else if (!strcasecmp(headername, "from")) {
642 header = camel_message_info_from(search->current);
643 type = CAMEL_SEARCH_TYPE_ADDRESS;
644 } else if (!strcasecmp(headername, "to")) {
645 header = camel_message_info_to(search->current);
646 type = CAMEL_SEARCH_TYPE_ADDRESS;
647 } else if (!strcasecmp(headername, "cc")) {
648 header = camel_message_info_cc(search->current);
649 type = CAMEL_SEARCH_TYPE_ADDRESS;
650 } else if (!strcasecmp(headername, "x-camel-mlist")) {
651 header = camel_message_info_mlist(search->current);
652 type = CAMEL_SEARCH_TYPE_MLIST;
654 e_sexp_resultv_free(f, argc, argv);
655 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
659 /* performs an OR of all words */
660 for (i=1;i<argc && !truth;i++) {
661 if (argv[i]->type == ESEXP_RES_STRING) {
662 if (argv[i]->value.string[0] == 0) {
664 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
665 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
666 words = camel_search_words_split(argv[i]->value.string);
668 for (j=0;j<words->len && truth;j++) {
669 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
671 camel_search_words_free(words);
673 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
679 /* TODO: else, find all matches */
681 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
682 r->value.bool = truth;
688 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
690 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
694 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
696 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
700 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
702 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
706 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
708 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
712 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
716 r(printf ("executing header-exists\n"));
718 if (search->current) {
719 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
720 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
721 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
724 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
725 r->value.ptrarray = g_ptr_array_new();
731 /* this is just to OR results together */
732 struct _glib_sux_donkeys {
737 /* or, store all unique values */
739 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
741 g_ptr_array_add(fuckup->uids, key);
744 /* and, only store duplicates */
746 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
748 if (value == fuckup->count)
749 g_ptr_array_add(fuckup->uids, key);
753 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
755 CamelIndexCursor *wc, *nc;
756 const char *word, *name;
759 wc = camel_index_words(idx);
761 while (!truth && (word = camel_index_cursor_next(wc))) {
762 if (camel_ustrstrcase(word,match) != NULL) {
763 /* perf: could have the wc cursor return the name cursor */
764 nc = camel_index_find(idx, word);
766 while (!truth && (name = camel_index_cursor_next(nc)))
767 truth = strcmp(name, uid) == 0;
768 camel_object_unref((CamelObject *)nc);
772 camel_object_unref((CamelObject *)wc);
779 "one two" "three" "four five"
788 /* returns messages which contain all words listed in words */
790 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
792 GPtrArray *result = g_ptr_array_new();
793 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
794 struct _glib_sux_donkeys lambdafoo;
795 CamelIndexCursor *wc, *nc;
796 const char *word, *name;
797 CamelMessageInfo *mi;
800 /* we can have a maximum of 32 words, as we use it as the AND mask */
802 wc = camel_index_words(search->body_index);
804 while ((word = camel_index_cursor_next(wc))) {
805 for (i=0;i<words->len;i++) {
806 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
807 /* perf: could have the wc cursor return the name cursor */
808 nc = camel_index_find(search->body_index, word);
810 while ((name = camel_index_cursor_next(nc))) {
811 mi = g_hash_table_lookup(search->summary_hash, name);
814 const char *uid = camel_message_info_uid(mi);
816 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
817 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
820 camel_object_unref((CamelObject *)nc);
825 camel_object_unref((CamelObject *)wc);
827 lambdafoo.uids = result;
828 lambdafoo.count = (1<<words->len) - 1;
829 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
830 g_hash_table_destroy(ht);
837 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
839 CamelDataWrapper *containee;
843 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
845 if (containee == NULL)
848 /* using the object types is more accurate than using the mime/types */
849 if (CAMEL_IS_MULTIPART (containee)) {
850 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
851 for (i = 0; i < parts && truth == FALSE; i++) {
852 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
854 truth = match_words_1message(part, words, mask);
856 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
857 /* for messages we only look at its contents */
858 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
859 } else if (header_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
860 /* for all other text parts, we look inside, otherwise we dont care */
861 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
863 /* FIXME: The match should be part of a stream op */
864 camel_data_wrapper_write_to_stream (containee, CAMEL_STREAM (mem));
865 camel_stream_write (CAMEL_STREAM (mem), "", 1);
866 for (i=0;i<words->len;i++) {
867 /* FIXME: This is horridly slow, and should use a real search algorithm */
868 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
870 /* shortcut a match */
871 if (*mask == (1<<(words->len))-1)
875 camel_object_unref (CAMEL_OBJECT (mem));
882 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
885 CamelMimeMessage *msg;
888 msg = camel_folder_get_message(folder, uid, ex);
891 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
892 camel_object_unref((CamelObject *)msg);
894 camel_exception_clear(ex);
902 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
905 GPtrArray *matches = g_ptr_array_new();
907 if (search->body_index) {
909 struct _camel_search_words *simple;
911 simple = camel_search_words_simple(words);
912 indexed = match_words_index(search, simple, ex);
913 camel_search_words_free(simple);
915 for (i=0;i<indexed->len;i++) {
916 const char *uid = g_ptr_array_index(indexed, i);
918 if (match_words_message(search->folder, uid, words, ex))
919 g_ptr_array_add(matches, (char *)uid);
922 g_ptr_array_free(indexed, TRUE);
924 for (i=0;i<search->summary->len;i++) {
925 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
926 const char *uid = camel_message_info_uid(info);
928 if (match_words_message(search->folder, uid, words, ex))
929 g_ptr_array_add(matches, (char *)uid);
937 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
940 CamelException *ex = search->priv->ex;
941 struct _camel_search_words *words;
943 struct _glib_sux_donkeys lambdafoo;
945 if (search->current) {
948 if (argc == 1 && argv[0]->value.string[0] == 0) {
951 for (i=0;i<argc && !truth;i++) {
952 if (argv[i]->type == ESEXP_RES_STRING) {
953 words = camel_search_words_split(argv[i]->value.string);
955 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
956 for (j=0;j<words->len && truth;j++)
957 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
959 /* TODO: cache current message incase of multiple body search terms */
960 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
962 camel_search_words_free(words);
966 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
967 r->value.bool = truth;
969 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
970 r->value.ptrarray = g_ptr_array_new();
972 if (argc == 1 && argv[0]->value.string[0] == 0) {
973 for (i=0;i<search->summary->len;i++) {
974 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
976 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
979 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
982 for (i=0;i<argc;i++) {
983 if (argv[i]->type == ESEXP_RES_STRING) {
984 words = camel_search_words_split(argv[i]->value.string);
985 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
986 matches = match_words_index(search, words, ex);
988 matches = match_words_messages(search, words, ex);
990 for (j=0;j<matches->len;j++)
991 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
992 g_ptr_array_free(matches, TRUE);
993 camel_search_words_free(words);
996 lambdafoo.uids = r->value.ptrarray;
997 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
998 g_hash_table_destroy(ht);
1005 static ESExpResult *
1006 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1011 r(printf("executing user-flag\n"));
1013 /* are we inside a match-all? */
1014 if (search->current) {
1016 /* performs an OR of all words */
1017 for (i=0;i<argc && !truth;i++) {
1018 if (argv[i]->type == ESEXP_RES_STRING
1019 && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
1024 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1025 r->value.bool = truth;
1027 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1028 r->value.ptrarray = g_ptr_array_new();
1034 static ESExpResult *
1035 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1039 r(printf ("executing system-flag\n"));
1041 if (search->current) {
1042 gboolean truth = FALSE;
1045 truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
1047 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1048 r->value.bool = truth;
1050 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1051 r->value.ptrarray = g_ptr_array_new ();
1057 static ESExpResult *
1058 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1060 const char *value = NULL;
1063 r(printf("executing user-tag\n"));
1066 value = camel_tag_get (&search->current->user_tags, argv[0]->value.string);
1068 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1069 r->value.string = g_strdup (value ? value : "");
1074 static ESExpResult *
1075 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1079 r(printf("executing get-sent-date\n"));
1081 /* are we inside a match-all? */
1083 r = e_sexp_result_new(f, ESEXP_RES_INT);
1085 r->value.number = s->current->date_sent;
1087 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1088 r->value.ptrarray = g_ptr_array_new ();
1094 static ESExpResult *
1095 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1099 r(printf("executing get-received-date\n"));
1101 /* are we inside a match-all? */
1103 r = e_sexp_result_new(f, ESEXP_RES_INT);
1105 r->value.number = s->current->date_received;
1107 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1108 r->value.ptrarray = g_ptr_array_new ();
1114 static ESExpResult *
1115 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1119 r(printf("executing get-current-date\n"));
1121 r = e_sexp_result_new(f, ESEXP_RES_INT);
1122 r->value.number = time (NULL);
1126 static ESExpResult *
1127 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1131 r(printf("executing get-size\n"));
1133 /* are we inside a match-all? */
1135 r = e_sexp_result_new (f, ESEXP_RES_INT);
1136 r->value.number = s->current->size / 1024;
1138 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1139 r->value.ptrarray = g_ptr_array_new ();
1145 static ESExpResult *
1146 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1151 r(printf("executing uid\n"));
1153 /* are we inside a match-all? */
1154 if (search->current) {
1156 const char *uid = camel_message_info_uid(search->current);
1158 /* performs an OR of all words */
1159 for (i=0;i<argc && !truth;i++) {
1160 if (argv[i]->type == ESEXP_RES_STRING
1161 && !strcmp(uid, argv[i]->value.string)) {
1166 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1167 r->value.bool = truth;
1169 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1170 r->value.ptrarray = g_ptr_array_new();
1171 for (i=0;i<argc;i++) {
1172 if (argv[i]->type == ESEXP_RES_STRING)
1173 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);