1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2003 Ximian Inc.
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 /* This is a helper class for folders to implement the search function.
23 It implements enough to do basic searches on folders that can provide
24 an in-memory summary and a body index. */
33 #include <sys/types.h>
38 #include "camel-folder-search.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?? */
365 if (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 /* instead of putting the mempool_hash in the structure, we keep the api clean by
394 putting a reference to it in a hashtable. Lets us do some debugging and catch
395 unfree'd results as well. */
396 g_hash_table_insert(p->mempool_hash, matches, pool);
398 g_warning("Search returned an invalid result type");
401 e_sexp_result_free(search->sexp, r);
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->current = (CamelMessageInfo *)info;
432 uids = camel_folder_search_execute_expression(search, expr, ex);
436 camel_folder_search_free_result(search, uids);
438 search->current = 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->current) {
499 uid = camel_message_info_uid(search->current);
500 for (i=0;!found && i<v->len;i++) {
501 if (strcmp(uid, v->pdata[i]) == 0)
506 g_ptr_array_add(r->value.ptrarray, (char *)uid);
507 } else if (search->summary == NULL) {
508 g_warning("No summary set, 'not' against an array requires a summary");
510 /* 'not' against the whole summary */
511 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
513 CamelMessageInfo **m;
515 s = (char **)v->pdata;
516 for (i=0;i<v->len;i++)
517 g_hash_table_insert(have, s[i], s[i]);
520 m = (CamelMessageInfo **)v->pdata;
521 for (i=0;i<v->len;i++) {
522 char *uid = (char *)camel_message_info_uid(m[i]);
524 if (g_hash_table_lookup(have, uid) == NULL)
525 g_ptr_array_add(r->value.ptrarray, uid);
527 g_hash_table_destroy(have);
532 if (argv[0]->type == ESEXP_RES_BOOL)
533 res = ! argv[0]->value.bool;
535 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
539 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
540 r->value.bool = TRUE;
547 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
553 g_warning("match-all only takes a single argument, other arguments ignored");
556 /* we are only matching a single message? or already inside a match-all? */
557 if (search->current) {
558 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
560 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
561 r->value.bool = FALSE;
564 r1 = e_sexp_term_eval(f, argv[0]);
565 if (r1->type == ESEXP_RES_BOOL) {
566 r->value.bool = r1->value.bool;
568 g_warning("invalid syntax, matches require a single bool result");
569 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
571 e_sexp_result_free(f, r1);
573 r->value.bool = TRUE;
578 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
579 r->value.ptrarray = g_ptr_array_new();
581 if (search->summary == NULL) {
582 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
583 g_warning("No summary supplied, match-all doesn't work with no summary");
588 /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
589 for (i=0;i<search->summary->len;i++) {
590 search->current = g_ptr_array_index(search->summary, i);
592 r1 = e_sexp_term_eval(f, argv[0]);
593 if (r1->type == ESEXP_RES_BOOL) {
595 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
597 g_warning("invalid syntax, matches require a single bool result");
598 e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
600 e_sexp_result_free(f, r1);
602 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
605 search->current = NULL;
611 check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
616 r(printf("executing check-header %d\n", how));
618 /* are we inside a match-all? */
619 if (search->current && argc>1
620 && argv[0]->type == ESEXP_RES_STRING) {
622 const char *header = NULL;
625 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
626 struct _camel_search_words *words;
628 /* only a subset of headers are supported .. */
629 headername = argv[0]->value.string;
630 if (!strcasecmp(headername, "subject")) {
631 header = camel_message_info_subject(search->current);
632 } else if (!strcasecmp(headername, "date")) {
633 /* FIXME: not a very useful form of the date */
634 sprintf(strbuf, "%d", (int)search->current->date_sent);
636 } else if (!strcasecmp(headername, "from")) {
637 header = camel_message_info_from(search->current);
638 type = CAMEL_SEARCH_TYPE_ADDRESS;
639 } else if (!strcasecmp(headername, "to")) {
640 header = camel_message_info_to(search->current);
641 type = CAMEL_SEARCH_TYPE_ADDRESS;
642 } else if (!strcasecmp(headername, "cc")) {
643 header = camel_message_info_cc(search->current);
644 type = CAMEL_SEARCH_TYPE_ADDRESS;
645 } else if (!strcasecmp(headername, "x-camel-mlist")) {
646 header = camel_message_info_mlist(search->current);
647 type = CAMEL_SEARCH_TYPE_MLIST;
649 e_sexp_resultv_free(f, argc, argv);
650 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
654 /* performs an OR of all words */
655 for (i=1;i<argc && !truth;i++) {
656 if (argv[i]->type == ESEXP_RES_STRING) {
657 if (argv[i]->value.string[0] == 0) {
659 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
660 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
661 words = camel_search_words_split(argv[i]->value.string);
663 for (j=0;j<words->len && truth;j++) {
664 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
666 camel_search_words_free(words);
668 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
674 /* TODO: else, find all matches */
676 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
677 r->value.bool = truth;
683 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
685 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
689 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
691 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
695 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
697 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
701 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
703 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
707 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
711 r(printf ("executing header-exists\n"));
713 if (search->current) {
714 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
715 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
716 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
719 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
720 r->value.ptrarray = g_ptr_array_new();
726 /* this is just to OR results together */
727 struct _glib_sux_donkeys {
732 /* or, store all unique values */
734 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
736 g_ptr_array_add(fuckup->uids, key);
739 /* and, only store duplicates */
741 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
743 if (value == fuckup->count)
744 g_ptr_array_add(fuckup->uids, key);
748 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
750 CamelIndexCursor *wc, *nc;
751 const char *word, *name;
754 wc = camel_index_words(idx);
756 while (!truth && (word = camel_index_cursor_next(wc))) {
757 if (camel_ustrstrcase(word,match) != NULL) {
758 /* perf: could have the wc cursor return the name cursor */
759 nc = camel_index_find(idx, word);
761 while (!truth && (name = camel_index_cursor_next(nc)))
762 truth = strcmp(name, uid) == 0;
763 camel_object_unref((CamelObject *)nc);
767 camel_object_unref((CamelObject *)wc);
774 "one two" "three" "four five"
783 /* returns messages which contain all words listed in words */
785 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
787 GPtrArray *result = g_ptr_array_new();
788 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
789 struct _glib_sux_donkeys lambdafoo;
790 CamelIndexCursor *wc, *nc;
791 const char *word, *name;
792 CamelMessageInfo *mi;
795 /* we can have a maximum of 32 words, as we use it as the AND mask */
797 wc = camel_index_words(search->body_index);
799 while ((word = camel_index_cursor_next(wc))) {
800 for (i=0;i<words->len;i++) {
801 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
802 /* perf: could have the wc cursor return the name cursor */
803 nc = camel_index_find(search->body_index, word);
805 while ((name = camel_index_cursor_next(nc))) {
806 mi = g_hash_table_lookup(search->summary_hash, name);
809 const char *uid = camel_message_info_uid(mi);
811 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, uid))) | (1<<i);
812 g_hash_table_insert(ht, (char *)uid, GINT_TO_POINTER(mask));
815 camel_object_unref((CamelObject *)nc);
820 camel_object_unref((CamelObject *)wc);
822 lambdafoo.uids = result;
823 lambdafoo.count = (1<<words->len) - 1;
824 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
825 g_hash_table_destroy(ht);
832 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
834 CamelDataWrapper *containee;
838 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
840 if (containee == NULL)
843 /* using the object types is more accurate than using the mime/types */
844 if (CAMEL_IS_MULTIPART (containee)) {
845 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
846 for (i = 0; i < parts && truth == FALSE; i++) {
847 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
849 truth = match_words_1message(part, words, mask);
851 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
852 /* for messages we only look at its contents */
853 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
854 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
855 /* for all other text parts, we look inside, otherwise we dont care */
856 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
858 /* FIXME: The match should be part of a stream op */
859 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
860 camel_stream_write (CAMEL_STREAM (mem), "", 1);
861 for (i=0;i<words->len;i++) {
862 /* FIXME: This is horridly slow, and should use a real search algorithm */
863 if (camel_ustrstrcase(mem->buffer->data, words->words[i]->word) != NULL) {
865 /* shortcut a match */
866 if (*mask == (1<<(words->len))-1)
870 camel_object_unref (CAMEL_OBJECT (mem));
877 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
880 CamelMimeMessage *msg;
883 msg = camel_folder_get_message(folder, uid, ex);
886 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
887 camel_object_unref((CamelObject *)msg);
889 camel_exception_clear(ex);
897 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
900 GPtrArray *matches = g_ptr_array_new();
902 if (search->body_index) {
904 struct _camel_search_words *simple;
906 simple = camel_search_words_simple(words);
907 indexed = match_words_index(search, simple, ex);
908 camel_search_words_free(simple);
910 for (i=0;i<indexed->len;i++) {
911 const char *uid = g_ptr_array_index(indexed, i);
913 if (match_words_message(search->folder, uid, words, ex))
914 g_ptr_array_add(matches, (char *)uid);
917 g_ptr_array_free(indexed, TRUE);
919 for (i=0;i<search->summary->len;i++) {
920 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
921 const char *uid = camel_message_info_uid(info);
923 if (match_words_message(search->folder, uid, words, ex))
924 g_ptr_array_add(matches, (char *)uid);
932 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
935 CamelException *ex = search->priv->ex;
936 struct _camel_search_words *words;
938 struct _glib_sux_donkeys lambdafoo;
940 if (search->current) {
943 if (argc == 1 && argv[0]->value.string[0] == 0) {
946 for (i=0;i<argc && !truth;i++) {
947 if (argv[i]->type == ESEXP_RES_STRING) {
948 words = camel_search_words_split(argv[i]->value.string);
950 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
951 for (j=0;j<words->len && truth;j++)
952 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
954 /* TODO: cache current message incase of multiple body search terms */
955 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
957 camel_search_words_free(words);
961 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
962 r->value.bool = truth;
964 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
965 r->value.ptrarray = g_ptr_array_new();
967 if (argc == 1 && argv[0]->value.string[0] == 0) {
968 for (i=0;i<search->summary->len;i++) {
969 CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
971 g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
974 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
977 for (i=0;i<argc;i++) {
978 if (argv[i]->type == ESEXP_RES_STRING) {
979 words = camel_search_words_split(argv[i]->value.string);
980 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
981 matches = match_words_index(search, words, ex);
983 matches = match_words_messages(search, words, ex);
985 for (j=0;j<matches->len;j++)
986 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
987 g_ptr_array_free(matches, TRUE);
988 camel_search_words_free(words);
991 lambdafoo.uids = r->value.ptrarray;
992 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
993 g_hash_table_destroy(ht);
1000 static ESExpResult *
1001 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1006 r(printf("executing user-flag\n"));
1008 /* are we inside a match-all? */
1009 if (search->current) {
1011 /* performs an OR of all words */
1012 for (i=0;i<argc && !truth;i++) {
1013 if (argv[i]->type == ESEXP_RES_STRING
1014 && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
1019 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1020 r->value.bool = truth;
1022 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1023 r->value.ptrarray = g_ptr_array_new();
1029 static ESExpResult *
1030 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1034 r(printf ("executing system-flag\n"));
1036 if (search->current) {
1037 gboolean truth = FALSE;
1040 truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
1042 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1043 r->value.bool = truth;
1045 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1046 r->value.ptrarray = g_ptr_array_new ();
1052 static ESExpResult *
1053 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1055 const char *value = NULL;
1058 r(printf("executing user-tag\n"));
1061 value = camel_tag_get (&search->current->user_tags, argv[0]->value.string);
1063 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1064 r->value.string = g_strdup (value ? value : "");
1069 static ESExpResult *
1070 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1074 r(printf("executing get-sent-date\n"));
1076 /* are we inside a match-all? */
1078 r = e_sexp_result_new(f, ESEXP_RES_INT);
1080 r->value.number = s->current->date_sent;
1082 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1083 r->value.ptrarray = g_ptr_array_new ();
1089 static ESExpResult *
1090 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1094 r(printf("executing get-received-date\n"));
1096 /* are we inside a match-all? */
1098 r = e_sexp_result_new(f, ESEXP_RES_INT);
1100 r->value.number = s->current->date_received;
1102 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1103 r->value.ptrarray = g_ptr_array_new ();
1109 static ESExpResult *
1110 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1114 r(printf("executing get-current-date\n"));
1116 r = e_sexp_result_new(f, ESEXP_RES_INT);
1117 r->value.number = time (NULL);
1121 static ESExpResult *
1122 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1126 r(printf("executing get-size\n"));
1128 /* are we inside a match-all? */
1130 r = e_sexp_result_new (f, ESEXP_RES_INT);
1131 r->value.number = s->current->size / 1024;
1133 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1134 r->value.ptrarray = g_ptr_array_new ();
1140 static ESExpResult *
1141 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1146 r(printf("executing uid\n"));
1148 /* are we inside a match-all? */
1149 if (search->current) {
1151 const char *uid = camel_message_info_uid(search->current);
1153 /* performs an OR of all words */
1154 for (i=0;i<argc && !truth;i++) {
1155 if (argv[i]->type == ESEXP_RES_STRING
1156 && !strcmp(uid, argv[i]->value.string)) {
1161 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1162 r->value.bool = truth;
1164 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1165 r->value.ptrarray = g_ptr_array_new();
1166 for (i=0;i<argc;i++) {
1167 if (argv[i]->type == ESEXP_RES_STRING)
1168 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);