1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 /* This is a helper class for folders to implement the search function.
23 It implements enough to do basic searches on folders that can provide
24 an in-memory summary and a body index. */
30 /* POSIX requires <sys/types.h> be included before <regex.h> */
31 #include <sys/types.h>
38 #include <glib/gi18n-lib.h>
40 #include "camel-exception.h"
41 #include "camel-folder-search.h"
42 #include "camel-folder-thread.h"
43 #include "camel-iconv.h"
44 #include "camel-medium.h"
45 #include "camel-mime-message.h"
46 #include "camel-multipart.h"
47 #include "camel-search-private.h"
48 #include "camel-stream-mem.h"
50 #include "camel-debug.h"
51 #include "camel-store.h"
52 #include "camel-vee-folder.h"
53 #include "camel-string-utils.h"
54 #include "camel-search-sql.h"
55 #include "camel-search-sql-sexp.h"
59 #define dd(x) if (camel_debug("search")) x
61 struct _CamelFolderSearchPrivate {
64 CamelFolderThread *threads;
65 GHashTable *threads_hash;
68 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
70 static ESExpResult *search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_starts_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_ends_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_exists(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_header_soundex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_header_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
79 static ESExpResult *search_header_full_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
81 static ESExpResult *search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
82 static ESExpResult *search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
83 static ESExpResult *search_body_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
84 static ESExpResult *search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
85 static ESExpResult *search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_system_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_get_size(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_message_location(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
94 static ESExpResult *search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
96 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
98 static CamelObjectClass *camel_folder_search_parent;
101 folder_search_finalize (CamelObject *obj)
103 CamelFolderSearch *search = (CamelFolderSearch *)obj;
104 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
107 e_sexp_unref(search->sexp);
109 g_free(search->last_search);
114 camel_folder_search_class_init (CamelFolderSearchClass *class)
116 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
118 class->not = search_not;
120 class->match_all = search_match_all;
121 class->match_threads = search_match_threads;
122 class->body_contains = search_body_contains;
123 class->body_regex = search_body_regex;
124 class->header_contains = search_header_contains;
125 class->header_matches = search_header_matches;
126 class->header_starts_with = search_header_starts_with;
127 class->header_ends_with = search_header_ends_with;
128 class->header_exists = search_header_exists;
129 class->header_soundex = search_header_soundex;
130 class->header_regex = search_header_regex;
131 class->header_full_regex = search_header_full_regex;
132 class->user_tag = search_user_tag;
133 class->user_flag = search_user_flag;
134 class->system_flag = search_system_flag;
135 class->get_sent_date = search_get_sent_date;
136 class->get_received_date = search_get_received_date;
137 class->get_current_date = search_get_current_date;
138 class->get_size = search_get_size;
139 class->uid = search_uid;
140 class->message_location = search_message_location;
144 camel_folder_search_init (CamelFolderSearch *obj)
146 struct _CamelFolderSearchPrivate *p;
148 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
150 obj->sexp = e_sexp_new();
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) folder_search_finalize);
174 gint flags; /* 0x02 = immediate, 0x01 = always enter */
176 /* these have default implementations in e-sexp */
177 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
178 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
179 /* we need to override this one though to implement an 'array not' */
180 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
181 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
182 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
183 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
185 /* these we have to use our own default if there is none */
186 /* they should all be defined in the language? so it parses, or should they not?? */
187 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
188 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
189 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
190 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
191 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
192 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
193 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
194 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
195 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
196 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
197 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
198 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
199 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
200 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
201 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
202 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
203 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
204 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
205 { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
206 { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
207 { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
211 camel_folder_search_construct (CamelFolderSearch *search)
214 CamelFolderSearchClass *class;
216 class = CAMEL_FOLDER_SEARCH_GET_CLASS (search);
218 for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
220 /* c is sure messy sometimes */
221 func = *((gpointer *)(((gchar *)class)+builtins[i].offset));
222 if (func == NULL && builtins[i].flags&1) {
223 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
224 func = (gpointer)search_dummy;
227 if (builtins[i].flags&2) {
228 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
230 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
237 * camel_folder_search_new:
239 * Create a new CamelFolderSearch object.
241 * A CamelFolderSearch is a subclassable, extensible s-exp
242 * evaluator which enforces a particular set of s-expressions.
243 * Particular methods may be overriden by an implementation to
244 * implement a search for any sort of backend.
246 * Returns: A new CamelFolderSearch widget.
249 camel_folder_search_new (void)
251 CamelFolderSearch *new;
253 new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
254 camel_folder_search_construct(new);
260 * camel_folder_search_set_folder:
264 * Set the folder attribute of the search. This is currently unused, but
265 * could be used to perform a slow-search when indexes and so forth are not
266 * available. Or for use by subclasses.
269 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
271 search->folder = folder;
275 * camel_folder_search_set_summary:
277 * @summary: An array of CamelMessageInfo pointers.
279 * Set the array of summary objects representing the span of the search.
281 * If this is not set, then a subclass must provide the functions
282 * for searching headers and for the match-all operator.
285 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
287 search->summary = summary;
291 * camel_folder_search_set_body_index:
295 * Set the index representing the contents of all messages
296 * in this folder. If this is not set, then the folder implementation
297 * should sub-class the CamelFolderSearch and provide its own
298 * body-contains function.
301 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
303 if (search->body_index)
304 camel_object_unref (search->body_index);
305 search->body_index = index;
307 camel_object_ref (index);
311 * camel_folder_search_execute_expression:
316 * Execute the search expression @expr, returning an array of
317 * all matches as a GPtrArray of uid's of matching messages.
319 * Note that any settings such as set_body_index(), set_folder(),
320 * and so on are reset to #NULL once the search has completed.
322 * TODO: The interface should probably return summary items instead
323 * (since they are much more useful to any client).
325 * Returns: A GPtrArray of strings of all matching messages.
326 * This must only be freed by camel_folder_search_free_result.
329 camel_folder_search_execute_expression (CamelFolderSearch *search,
337 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
341 /* only re-parse if the search has changed */
342 if (search->last_search == NULL
343 || strcmp(search->last_search, expr)) {
344 e_sexp_input_text(search->sexp, expr, strlen(expr));
345 if (e_sexp_parse(search->sexp) == -1) {
346 camel_exception_setv (
348 _("Cannot parse search expression: %s:\n%s"),
349 e_sexp_error(search->sexp), expr);
353 g_free(search->last_search);
354 search->last_search = g_strdup(expr);
356 r = e_sexp_eval(search->sexp);
358 if (!camel_exception_is_set(ex))
359 camel_exception_setv (
361 _("Error executing search expression: %s:\n%s"),
362 e_sexp_error(search->sexp), expr);
366 matches = g_ptr_array_new();
368 /* now create a folder summary to return?? */
369 if (r->type == ESEXP_RES_ARRAY_PTR) {
370 d(printf("got result ...\n"));
371 if (search->summary) {
372 /* reorder result in summary order */
373 results = g_hash_table_new(g_str_hash, g_str_equal);
374 for (i=0;i<r->value.ptrarray->len;i++) {
375 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
376 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
378 for (i=0;i<search->summary->len;i++) {
379 gchar *uid = g_ptr_array_index(search->summary, i);
380 if (g_hash_table_lookup(results, uid)) {
381 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
384 g_hash_table_destroy(results);
386 for (i=0;i<r->value.ptrarray->len;i++) {
387 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
388 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
392 g_warning("Search returned an invalid result type");
395 e_sexp_result_free(search->sexp, r);
398 camel_folder_thread_messages_unref(p->threads);
400 g_hash_table_destroy(p->threads_hash);
403 p->threads_hash = NULL;
404 search->folder = NULL;
405 search->summary = NULL;
406 search->current = NULL;
407 search->body_index = NULL;
413 * camel_folder_search_count:
416 * @uids: to search against, NULL for all uid's.
419 * Run a search. Search must have had Folder already set on it, and
420 * it must implement summaries.
422 * Returns: Number of messages that match the query.
428 camel_folder_search_count (CamelFolderSearch *search,
433 GPtrArray *summary_set;
436 gchar *sql_query, *tmp, *tmp1;
440 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
442 g_assert(search->folder);
446 /* We route body-contains search and thread based search through memory and not via db. */
447 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
448 /* setup our search list only contains those we're interested in */
449 search->summary = camel_folder_get_summary(search->folder);
451 summary_set = search->summary;
453 /* only re-parse if the search has changed */
454 if (search->last_search == NULL
455 || strcmp(search->last_search, expr)) {
456 e_sexp_input_text(search->sexp, expr, strlen(expr));
457 if (e_sexp_parse(search->sexp) == -1) {
458 camel_exception_setv (
460 _("Cannot parse search expression: %s:\n%s"),
461 e_sexp_error(search->sexp), expr);
465 g_free(search->last_search);
466 search->last_search = g_strdup(expr);
468 r = e_sexp_eval(search->sexp);
470 if (!camel_exception_is_set(ex))
471 camel_exception_setv (
473 _("Error executing search expression: %s:\n%s"),
474 e_sexp_error(search->sexp), expr);
478 /* now create a folder summary to return?? */
479 if (r->type == ESEXP_RES_ARRAY_PTR) {
480 d(printf("got result\n"));
482 /* reorder result in summary order */
483 results = g_hash_table_new(g_str_hash, g_str_equal);
484 for (i=0;i<r->value.ptrarray->len;i++) {
485 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
486 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
489 for (i=0;i<summary_set->len;i++) {
490 gchar *uid = g_ptr_array_index(summary_set, i);
491 if (g_hash_table_lookup(results, uid))
494 g_hash_table_destroy(results);
497 e_sexp_result_free(search->sexp, r);
500 /* Sync the db, so that we search the db for changes */
501 camel_folder_summary_save_to_db (search->folder->summary, ex);
503 dd(printf ("sexp is : [%s]\n", expr));
504 if (g_getenv("SQL_SEARCH_OLD"))
505 sql_query = camel_sexp_to_sql (expr);
507 sql_query = camel_sexp_to_sql_sexp (expr);
508 tmp1 = camel_db_sqlize_string(search->folder->full_name);
509 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
510 camel_db_free_sqlized_string (tmp1);
512 dd(printf("Equivalent sql %s\n", tmp));
514 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
515 camel_db_count_message_info (cdb, tmp, &count, ex);
516 if (ex && camel_exception_is_set(ex)) {
517 const gchar *exception = camel_exception_get_description (ex);
518 if (strncmp(exception, "no such table", 13) == 0) {
519 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
520 /* Suppress no such table */
521 camel_exception_clear (ex);
528 /* these might be allocated by match-threads */
530 camel_folder_thread_messages_unref(p->threads);
532 g_hash_table_destroy(p->threads_hash);
533 if (search->summary_set)
534 g_ptr_array_free(search->summary_set, TRUE);
536 camel_folder_free_summary(search->folder, search->summary);
539 p->threads_hash = NULL;
540 search->folder = NULL;
541 search->summary = NULL;
542 search->summary_set = NULL;
543 search->current = NULL;
544 search->body_index = NULL;
550 do_search_in_memory (const gchar *expr)
552 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
553 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
559 for (i = 0; in_memory_tokens [i]; i++) {
560 if (strstr (expr, in_memory_tokens [i]))
568 * camel_folder_search_search:
571 * @uids: to search against, NULL for all uid's.
574 * Run a search. Search must have had Folder already set on it, and
575 * it must implement summaries.
580 camel_folder_search_search (CamelFolderSearch *search,
586 GPtrArray *matches = NULL, *summary_set;
589 gchar *sql_query, *tmp, *tmp1;
592 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
594 g_assert(search->folder);
598 /* We route body-contains / thread based search and uid search through memory and not via db. */
599 if (uids || do_search_in_memory (expr)) {
600 /* setup our search list only contains those we're interested in */
601 search->summary = camel_folder_get_summary(search->folder);
604 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
606 summary_set = search->summary_set = g_ptr_array_new();
607 for (i=0;i<uids->len;i++)
608 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
609 for (i=0;i<search->summary->len;i++)
610 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
611 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
612 g_hash_table_destroy(uids_hash);
614 summary_set = search->summary;
617 /* only re-parse if the search has changed */
618 if (search->last_search == NULL
619 || strcmp(search->last_search, expr)) {
620 e_sexp_input_text(search->sexp, expr, strlen(expr));
621 if (e_sexp_parse(search->sexp) == -1) {
622 camel_exception_setv (
624 _("Cannot parse search expression: %s:\n%s"),
625 e_sexp_error(search->sexp), expr);
629 g_free(search->last_search);
630 search->last_search = g_strdup(expr);
632 r = e_sexp_eval(search->sexp);
634 if (!camel_exception_is_set(ex))
635 camel_exception_setv (
637 _("Error executing search expression: %s:\n%s"),
638 e_sexp_error(search->sexp), expr);
642 matches = g_ptr_array_new();
644 /* now create a folder summary to return?? */
645 if (r->type == ESEXP_RES_ARRAY_PTR) {
646 d(printf("got result\n"));
648 /* reorder result in summary order */
649 results = g_hash_table_new(g_str_hash, g_str_equal);
650 for (i=0;i<r->value.ptrarray->len;i++) {
651 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
652 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
655 for (i=0;i<summary_set->len;i++) {
656 gchar *uid = g_ptr_array_index(summary_set, i);
657 if (g_hash_table_lookup(results, uid))
658 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
660 g_hash_table_destroy(results);
663 e_sexp_result_free(search->sexp, r);
666 /* Sync the db, so that we search the db for changes */
667 camel_folder_summary_save_to_db (search->folder->summary, ex);
669 dd(printf ("sexp is : [%s]\n", expr));
670 if (g_getenv("SQL_SEARCH_OLD"))
671 sql_query = camel_sexp_to_sql (expr);
673 sql_query = camel_sexp_to_sql_sexp (expr);
674 tmp1 = camel_db_sqlize_string(search->folder->full_name);
675 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
676 camel_db_free_sqlized_string (tmp1);
678 dd(printf("Equivalent sql %s\n", tmp));
680 matches = g_ptr_array_new();
681 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
682 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
683 if (ex && camel_exception_is_set(ex)) {
684 const gchar *exception = camel_exception_get_description (ex);
685 if (strncmp(exception, "no such table", 13) == 0) {
686 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
687 /* Suppress no such table */
688 camel_exception_clear (ex);
696 /* these might be allocated by match-threads */
698 camel_folder_thread_messages_unref(p->threads);
700 g_hash_table_destroy(p->threads_hash);
701 if (search->summary_set)
702 g_ptr_array_free(search->summary_set, TRUE);
704 camel_folder_free_summary(search->folder, search->summary);
707 p->threads_hash = NULL;
708 search->folder = NULL;
709 search->summary = NULL;
710 search->summary_set = NULL;
711 search->current = NULL;
712 search->body_index = NULL;
717 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
719 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
720 g_ptr_array_free(result, TRUE);
723 /* dummy function, returns false always, or an empty match array */
725 search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
729 if (search->current == NULL) {
730 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
731 r->value.boolean = FALSE;
733 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
734 r->value.ptrarray = g_ptr_array_new();
740 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
742 search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
748 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
749 GPtrArray *v = argv[0]->value.ptrarray;
752 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
753 r->value.ptrarray = g_ptr_array_new();
755 /* not against a single message?*/
756 if (search->current) {
759 uid = camel_message_info_uid(search->current);
760 for (i=0;!found && i<v->len;i++) {
761 if (strcmp(uid, v->pdata[i]) == 0)
766 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
767 } else if (search->summary == NULL) {
768 g_warning("No summary set, 'not' against an array requires a summary");
770 /* 'not' against the whole summary */
771 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
775 s = (gchar **)v->pdata;
776 for (i=0;i<v->len;i++)
777 g_hash_table_insert(have, s[i], s[i]);
779 v = search->summary_set?search->summary_set:search->summary;
780 m = (gchar **)v->pdata;
781 for (i=0;i<v->len;i++) {
784 if (g_hash_table_lookup(have, uid) == NULL)
785 g_ptr_array_add(r->value.ptrarray, uid);
787 g_hash_table_destroy(have);
792 if (argv[0]->type == ESEXP_RES_BOOL)
793 res = !argv[0]->value.boolean;
795 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
796 r->value.boolean = res;
799 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
800 r->value.boolean = TRUE;
807 search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
815 g_warning("match-all only takes a single argument, other arguments ignored");
818 /* we are only matching a single message? or already inside a match-all? */
819 if (search->current) {
820 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
822 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
823 r->value.boolean = FALSE;
826 r1 = e_sexp_term_eval(f, argv[0]);
827 if (r1->type == ESEXP_RES_BOOL) {
828 r->value.boolean = r1->value.boolean;
830 g_warning("invalid syntax, matches require a single bool result");
831 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
832 e_sexp_fatal_error(f, error_msg);
835 e_sexp_result_free(f, r1);
837 r->value.boolean = TRUE;
842 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
843 r->value.ptrarray = g_ptr_array_new();
845 if (search->summary == NULL) {
846 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
847 g_warning("No summary supplied, match-all doesn't work with no summary");
852 v = search->summary_set?search->summary_set:search->summary;
854 if (v->len > g_hash_table_size (search->folder->summary->loaded_infos) && !CAMEL_IS_VEE_FOLDER (search->folder)) {
855 camel_folder_summary_reload_from_db (search->folder->summary, search->priv->ex);
858 for (i=0;i<v->len;i++) {
861 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
862 if (!search->current)
864 uid = camel_message_info_uid(search->current);
867 r1 = e_sexp_term_eval(f, argv[0]);
868 if (r1->type == ESEXP_RES_BOOL) {
869 if (r1->value.boolean)
870 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
872 g_warning("invalid syntax, matches require a single bool result");
873 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
874 e_sexp_fatal_error(f, error_msg);
877 e_sexp_result_free(f, r1);
879 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
881 camel_message_info_free (search->current);
883 search->current = NULL;
888 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
891 g_hash_table_insert(id_hash, (gchar *)camel_message_info_uid(root->message), root);
893 fill_thread_table(root->child, id_hash);
899 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
902 g_hash_table_insert(result_hash, (gchar *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
904 add_thread_results(root->child, result_hash);
910 add_results(gchar *uid, gpointer dummy, GPtrArray *result)
912 g_ptr_array_add(result, uid);
916 search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
919 CamelFolderSearchPrivate *p = search->priv;
924 /* not supported in match-all */
925 if (search->current) {
926 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
927 e_sexp_fatal_error(f, error_msg);
932 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
933 e_sexp_fatal_error(f, error_msg);
937 r = e_sexp_term_eval(f, argv[0]);
938 if (r->type != ESEXP_RES_STRING) {
939 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
940 e_sexp_fatal_error(f, error_msg);
945 if (!strcmp(r->value.string, "none"))
947 else if (!strcmp(r->value.string, "all"))
949 else if (!strcmp(r->value.string, "replies"))
951 else if (!strcmp(r->value.string, "replies_parents"))
953 else if (!strcmp(r->value.string, "single"))
955 e_sexp_result_free(f, r);
957 /* behave as (begin does */
959 for (i=1;i<argc;i++) {
961 e_sexp_result_free(f, r);
962 r = e_sexp_term_eval(f, argv[i]);
965 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
966 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
967 e_sexp_fatal_error(f, error_msg);
974 if (search->folder == NULL) {
975 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
976 e_sexp_fatal_error(f, error_msg);
980 /* cache this, so we only have to re-calculate once per search at most */
981 if (p->threads == NULL) {
982 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
983 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
985 fill_thread_table(p->threads->tree, p->threads_hash);
988 results = g_hash_table_new(g_str_hash, g_str_equal);
989 for (i=0;i<r->value.ptrarray->len;i++) {
990 struct _CamelFolderThreadNode *node, *scan;
993 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
995 node = g_hash_table_lookup(p->threads_hash, (gchar *)g_ptr_array_index(r->value.ptrarray, i));
996 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
999 /* select messages in thread according to search criteria */
1001 if (node->child == NULL && node->parent == NULL)
1002 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
1006 while (scan && scan->parent) {
1007 scan = scan->parent;
1008 g_hash_table_insert(results, (gchar *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
1010 } else if (type == 1) {
1011 while (node && node->parent)
1012 node = node->parent;
1014 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
1016 add_thread_results(node->child, results);
1019 e_sexp_result_free(f, r);
1021 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1022 r->value.ptrarray = g_ptr_array_new();
1024 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
1025 g_hash_table_destroy(results);
1030 static CamelMimeMessage *
1031 get_current_message (CamelFolderSearch *search)
1033 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1034 CamelMimeMessage *res;
1036 if (!search || !search->folder || !search->current)
1039 res = camel_folder_get_message (search->folder, search->current->uid, &x);
1042 camel_exception_clear (&x);
1047 static ESExpResult *
1048 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1053 r(printf("executing check-header %d\n", how));
1055 /* are we inside a match-all? */
1056 if (search->current && argc>1
1057 && argv[0]->type == ESEXP_RES_STRING) {
1059 const gchar *header = NULL, *charset = NULL;
1062 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1063 struct _camel_search_words *words;
1064 CamelMimeMessage *message = NULL;
1065 struct _camel_header_raw *raw_header;
1067 /* only a subset of headers are supported .. */
1068 headername = argv[0]->value.string;
1069 if (!g_ascii_strcasecmp(headername, "subject")) {
1070 header = camel_message_info_subject(search->current);
1071 } else if (!g_ascii_strcasecmp(headername, "date")) {
1072 /* FIXME: not a very useful form of the date */
1073 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1075 } else if (!g_ascii_strcasecmp(headername, "from")) {
1076 header = camel_message_info_from(search->current);
1077 type = CAMEL_SEARCH_TYPE_ADDRESS;
1078 } else if (!g_ascii_strcasecmp(headername, "to")) {
1079 header = camel_message_info_to(search->current);
1080 type = CAMEL_SEARCH_TYPE_ADDRESS;
1081 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1082 header = camel_message_info_cc(search->current);
1083 type = CAMEL_SEARCH_TYPE_ADDRESS;
1084 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1085 header = camel_message_info_mlist(search->current);
1086 type = CAMEL_SEARCH_TYPE_MLIST;
1088 message = get_current_message (search);
1090 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1093 charset = camel_content_type_param (ct, "charset");
1094 charset = camel_iconv_charset_name (charset);
1102 /* performs an OR of all words */
1103 for (i=1;i<argc && !truth;i++) {
1104 if (argv[i]->type == ESEXP_RES_STRING) {
1105 if (argv[i]->value.string[0] == 0) {
1107 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1108 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1109 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1111 for (j=0;j<words->len && truth;j++) {
1113 for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1114 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1115 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1120 truth = raw_header != NULL;
1122 truth = camel_search_header_match(header, words->words[j]->word, how, type, charset);
1124 camel_search_words_free(words);
1127 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1128 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1129 truth = camel_search_header_match(raw_header->value, argv[i]->value.string, how, type, charset);
1133 truth = camel_search_header_match(header, argv[i]->value.string, how, type, charset);
1139 camel_object_unref (message);
1141 /* TODO: else, find all matches */
1143 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1144 r->value.boolean = truth;
1151 l_printf(gchar *node)
1153 printf("%s\t", node);
1157 static ESExpResult *
1158 search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1160 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1163 static ESExpResult *
1164 search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1166 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1169 static ESExpResult *
1170 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1172 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1175 static ESExpResult *
1176 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1178 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1181 static ESExpResult *
1182 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1186 r(printf ("executing header-exists\n"));
1188 if (search->current) {
1189 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1190 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1191 r->value.boolean = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
1194 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1195 r->value.ptrarray = g_ptr_array_new();
1201 static ESExpResult *
1202 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1204 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1207 static ESExpResult *
1208 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1211 CamelMimeMessage *msg;
1213 msg = get_current_message (search);
1217 const gchar *contents;
1219 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1221 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1222 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1223 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->ex) == 0) {
1224 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1227 r->value.boolean = FALSE;
1229 camel_object_unref (msg);
1231 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1232 r->value.ptrarray = g_ptr_array_new();
1239 get_full_header (CamelMimeMessage *message)
1241 CamelMimePart *mp = CAMEL_MIME_PART (message);
1242 GString *str = g_string_new ("");
1243 struct _camel_header_raw *h;
1245 for (h = mp->headers; h; h = h->next) {
1246 if (h->value != NULL) {
1247 g_string_append (str, h->name);
1248 if (isspace (h->value[0]))
1249 g_string_append (str, ":");
1251 g_string_append (str, ": ");
1252 g_string_append (str, h->value);
1253 g_string_append_c (str, '\n');
1257 return g_string_free (str, FALSE);
1260 static ESExpResult *
1261 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1264 CamelMimeMessage *msg;
1266 msg = get_current_message (search);
1271 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1273 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1276 contents = get_full_header (msg);
1277 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1282 r->value.boolean = FALSE;
1284 camel_object_unref (msg);
1286 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1287 r->value.ptrarray = g_ptr_array_new();
1293 /* this is just to OR results together */
1299 /* or, store all unique values */
1301 htor(gchar *key, gint value, struct IterData *iter_data)
1303 g_ptr_array_add(iter_data->uids, key);
1306 /* and, only store duplicates */
1308 htand(gchar *key, gint value, struct IterData *iter_data)
1310 if (value == iter_data->count)
1311 g_ptr_array_add(iter_data->uids, key);
1315 match_message_index (CamelIndex *idx,
1320 CamelIndexCursor *wc, *nc;
1321 const gchar *word, *name;
1324 wc = camel_index_words(idx);
1326 while (!truth && (word = camel_index_cursor_next(wc))) {
1327 if (camel_ustrstrcase(word,match) != NULL) {
1328 /* perf: could have the wc cursor return the name cursor */
1329 nc = camel_index_find(idx, word);
1331 while (!truth && (name = camel_index_cursor_next(nc)))
1332 truth = strcmp(name, uid) == 0;
1333 camel_object_unref (nc);
1337 camel_object_unref (wc);
1344 "one two" "three" "four five"
1353 /* returns messages which contain all words listed in words */
1355 match_words_index (CamelFolderSearch *search,
1356 struct _camel_search_words *words,
1359 GPtrArray *result = g_ptr_array_new();
1360 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1361 struct IterData lambdafoo;
1362 CamelIndexCursor *wc, *nc;
1363 const gchar *word, *name;
1366 /* we can have a maximum of 32 words, as we use it as the AND mask */
1368 wc = camel_index_words(search->body_index);
1370 while ((word = camel_index_cursor_next(wc))) {
1371 for (i=0;i<words->len;i++) {
1372 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1373 /* perf: could have the wc cursor return the name cursor */
1374 nc = camel_index_find(search->body_index, word);
1376 while ((name = camel_index_cursor_next(nc))) {
1379 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1380 g_hash_table_insert(ht, (gchar *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1382 camel_object_unref (nc);
1387 camel_object_unref (wc);
1389 lambdafoo.uids = result;
1390 lambdafoo.count = (1<<words->len) - 1;
1391 g_hash_table_foreach(ht, (GHFunc)htand, &lambdafoo);
1392 g_hash_table_destroy(ht);
1399 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1401 CamelDataWrapper *containee;
1405 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1407 if (containee == NULL)
1410 /* using the object types is more accurate than using the mime/types */
1411 if (CAMEL_IS_MULTIPART (containee)) {
1412 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1413 for (i = 0; i < parts && truth == FALSE; i++) {
1414 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1416 truth = match_words_1message(part, words, mask);
1418 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1419 /* for messages we only look at its contents */
1420 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1421 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1422 /* for all other text parts, we look inside, otherwise we dont care */
1423 CamelStream *stream;
1424 GByteArray *byte_array;
1426 byte_array = g_byte_array_new ();
1427 stream = camel_stream_mem_new_with_byte_array (byte_array);
1429 /* FIXME: The match should be part of a stream op */
1430 camel_data_wrapper_decode_to_stream (containee, stream);
1431 camel_stream_write (stream, "", 1);
1432 for (i=0;i<words->len;i++) {
1433 /* FIXME: This is horridly slow, and should use a real search algorithm */
1434 if (camel_ustrstrcase((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1436 /* shortcut a match */
1437 if (*mask == (1<<(words->len))-1)
1442 camel_object_unref (stream);
1449 match_words_message (CamelFolder *folder,
1451 struct _camel_search_words *words,
1455 CamelMimeMessage *msg;
1456 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1459 msg = camel_folder_get_message(folder, uid, &x);
1462 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1463 camel_object_unref (msg);
1465 camel_exception_clear (&x);
1471 match_words_messages (CamelFolderSearch *search,
1472 struct _camel_search_words *words,
1476 GPtrArray *matches = g_ptr_array_new();
1478 if (search->body_index) {
1480 struct _camel_search_words *simple;
1482 simple = camel_search_words_simple(words);
1483 indexed = match_words_index(search, simple, ex);
1484 camel_search_words_free(simple);
1486 for (i=0;i<indexed->len;i++) {
1487 const gchar *uid = g_ptr_array_index(indexed, i);
1489 if (match_words_message(search->folder, uid, words, ex))
1490 g_ptr_array_add(matches, (gchar *)uid);
1493 g_ptr_array_free(indexed, TRUE);
1495 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1497 for (i=0;i<v->len;i++) {
1498 gchar *uid = g_ptr_array_index(v, i);
1500 if (match_words_message(search->folder, uid, words, ex))
1501 g_ptr_array_add(matches, (gchar *)uid);
1508 static ESExpResult *
1509 search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1512 CamelException *ex = search->priv->ex;
1513 struct _camel_search_words *words;
1515 struct IterData lambdafoo;
1517 if (search->current) {
1520 if (argc == 1 && argv[0]->value.string[0] == 0) {
1523 for (i=0;i<argc && !truth;i++) {
1524 if (argv[i]->type == ESEXP_RES_STRING) {
1525 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1527 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1528 for (j=0;j<words->len && truth;j++)
1529 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1531 /* TODO: cache current message incase of multiple body search terms */
1532 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1534 camel_search_words_free(words);
1538 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1539 r->value.boolean = truth;
1541 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1542 r->value.ptrarray = g_ptr_array_new();
1544 if (argc == 1 && argv[0]->value.string[0] == 0) {
1545 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1547 for (i=0;i<v->len;i++) {
1548 gchar *uid = g_ptr_array_index(v, i);
1550 g_ptr_array_add(r->value.ptrarray, uid);
1553 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1556 for (i=0;i<argc;i++) {
1557 if (argv[i]->type == ESEXP_RES_STRING) {
1558 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1559 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1560 matches = match_words_index(search, words, ex);
1562 matches = match_words_messages(search, words, ex);
1564 for (j=0;j<matches->len;j++) {
1565 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1567 g_ptr_array_free(matches, TRUE);
1568 camel_search_words_free(words);
1571 lambdafoo.uids = r->value.ptrarray;
1572 g_hash_table_foreach(ht, (GHFunc)htor, &lambdafoo);
1573 g_hash_table_destroy(ht);
1580 static ESExpResult *
1581 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1584 CamelMimeMessage *msg = get_current_message (search);
1589 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1591 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1592 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1595 r->value.boolean = FALSE;
1597 camel_object_unref (msg);
1601 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1602 r->value.ptrarray = g_ptr_array_new ();
1604 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1606 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1607 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1608 CamelMimeMessage *message;
1610 for (i = 0; i < v->len; i++) {
1611 gchar *uid = g_ptr_array_index(v, i);
1613 message = camel_folder_get_message (search->folder, uid, &x);
1615 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1616 g_ptr_array_add (r->value.ptrarray, uid);
1619 camel_object_unref (message);
1621 camel_exception_clear (&x);
1632 static ESExpResult *
1633 search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1638 r(printf("executing user-flag\n"));
1640 /* are we inside a match-all? */
1641 if (search->current) {
1643 /* performs an OR of all words */
1644 for (i=0;i<argc && !truth;i++) {
1645 if (argv[i]->type == ESEXP_RES_STRING
1646 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1651 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1652 r->value.boolean = truth;
1654 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1655 r->value.ptrarray = g_ptr_array_new();
1661 static ESExpResult *
1662 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1666 r(printf ("executing system-flag\n"));
1668 if (search->current) {
1669 gboolean truth = FALSE;
1672 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1674 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1675 r->value.boolean = truth;
1677 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1678 r->value.ptrarray = g_ptr_array_new ();
1684 static ESExpResult *
1685 search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1687 const gchar *value = NULL;
1690 r(printf("executing user-tag\n"));
1692 if (search->current && argc == 1)
1693 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1695 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1696 r->value.string = g_strdup (value ? value : "");
1701 static ESExpResult *
1702 search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1706 r(printf("executing get-sent-date\n"));
1708 /* are we inside a match-all? */
1710 r = e_sexp_result_new(f, ESEXP_RES_INT);
1712 r->value.number = camel_message_info_date_sent(s->current);
1714 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1715 r->value.ptrarray = g_ptr_array_new ();
1721 static ESExpResult *
1722 search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1726 r(printf("executing get-received-date\n"));
1728 /* are we inside a match-all? */
1730 r = e_sexp_result_new(f, ESEXP_RES_INT);
1732 r->value.number = camel_message_info_date_received(s->current);
1734 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1735 r->value.ptrarray = g_ptr_array_new ();
1741 static ESExpResult *
1742 search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1746 r(printf("executing get-current-date\n"));
1748 r = e_sexp_result_new(f, ESEXP_RES_INT);
1749 r->value.number = time (NULL);
1753 static ESExpResult *
1754 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1758 r(printf("executing get-size\n"));
1760 /* are we inside a match-all? */
1762 r = e_sexp_result_new (f, ESEXP_RES_INT);
1763 r->value.number = camel_message_info_size(s->current) / 1024;
1765 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1766 r->value.ptrarray = g_ptr_array_new ();
1772 static ESExpResult *
1773 search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1778 r(printf("executing uid\n"));
1780 /* are we inside a match-all? */
1781 if (search->current) {
1783 const gchar *uid = camel_message_info_uid(search->current);
1785 /* performs an OR of all words */
1786 for (i=0;i<argc && !truth;i++) {
1787 if (argv[i]->type == ESEXP_RES_STRING
1788 && !strcmp(uid, argv[i]->value.string)) {
1793 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1794 r->value.boolean = truth;
1796 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1797 r->value.ptrarray = g_ptr_array_new();
1798 for (i=0;i<argc;i++) {
1799 if (argv[i]->type == ESEXP_RES_STRING)
1800 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1808 read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name)
1812 matches = (GPtrArray *) ref;
1814 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols [0]));
1818 static ESExpResult *
1819 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1822 gboolean same = FALSE;
1824 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1825 if (argv[0]->value.string && search->folder && search->folder->parent_store && camel_folder_get_full_name (search->folder)) {
1826 CamelFolderInfo *fi = camel_store_get_folder_info (search->folder->parent_store, camel_folder_get_full_name (search->folder), 0, NULL);
1828 same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
1830 camel_store_free_folder_info (search->folder->parent_store, fi);
1835 if (search->current) {
1836 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1837 r->value.boolean = same ? TRUE : FALSE;
1839 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1840 r->value.ptrarray = g_ptr_array_new ();
1845 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1847 for (i = 0; i < v->len; i++) {
1848 gchar *uid = g_ptr_array_index (v, i);
1850 g_ptr_array_add (r->value.ptrarray, uid);