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 #define CAMEL_FOLDER_SEARCH_GET_PRIVATE(obj) \
62 (G_TYPE_INSTANCE_GET_PRIVATE \
63 ((obj), CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate))
65 struct _CamelFolderSearchPrivate {
68 CamelFolderThread *threads;
69 GHashTable *threads_hash;
72 static ESExpResult *search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_starts_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_header_ends_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_header_exists(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
79 static ESExpResult *search_header_soundex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_header_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
81 static ESExpResult *search_header_full_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
82 static ESExpResult *search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
83 static ESExpResult *search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
84 static ESExpResult *search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
85 static ESExpResult *search_body_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
86 static ESExpResult *search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_system_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_get_size(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
93 static ESExpResult *search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
94 static ESExpResult *search_message_location(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
96 static ESExpResult *search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
98 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
100 G_DEFINE_TYPE (CamelFolderSearch, camel_folder_search, CAMEL_TYPE_OBJECT)
103 folder_search_dispose (GObject *object)
105 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
107 if (search->sexp != NULL) {
108 e_sexp_unref (search->sexp);
112 /* Chain up to parent's dispose() method. */
113 G_OBJECT_CLASS (camel_folder_search_parent_class)->dispose (object);
117 folder_search_finalize (GObject *object)
119 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
121 g_free (search->last_search);
123 /* Chain up to parent's finalize() method. */
124 G_OBJECT_CLASS (camel_folder_search_parent_class)->finalize (object);
128 camel_folder_search_class_init (CamelFolderSearchClass *class)
130 GObjectClass *object_class;
132 g_type_class_add_private (class, sizeof (CamelFolderSearchPrivate));
134 object_class = G_OBJECT_CLASS (class);
135 object_class->dispose = folder_search_dispose;
136 object_class->finalize = folder_search_finalize;
138 class->not = search_not;
139 class->match_all = search_match_all;
140 class->match_threads = search_match_threads;
141 class->body_contains = search_body_contains;
142 class->body_regex = search_body_regex;
143 class->header_contains = search_header_contains;
144 class->header_matches = search_header_matches;
145 class->header_starts_with = search_header_starts_with;
146 class->header_ends_with = search_header_ends_with;
147 class->header_exists = search_header_exists;
148 class->header_soundex = search_header_soundex;
149 class->header_regex = search_header_regex;
150 class->header_full_regex = search_header_full_regex;
151 class->user_tag = search_user_tag;
152 class->user_flag = search_user_flag;
153 class->system_flag = search_system_flag;
154 class->get_sent_date = search_get_sent_date;
155 class->get_received_date = search_get_received_date;
156 class->get_current_date = search_get_current_date;
157 class->get_size = search_get_size;
158 class->uid = search_uid;
159 class->message_location = search_message_location;
163 camel_folder_search_init (CamelFolderSearch *search)
165 search->priv = CAMEL_FOLDER_SEARCH_GET_PRIVATE (search);
166 search->sexp = e_sexp_new();
172 gint flags; /* 0x02 = immediate, 0x01 = always enter */
174 /* these have default implementations in e-sexp */
175 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
176 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
177 /* we need to override this one though to implement an 'array not' */
178 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
179 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
180 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
181 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
183 /* these we have to use our own default if there is none */
184 /* they should all be defined in the language? so it parses, or should they not?? */
185 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
186 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
187 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
188 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
189 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
190 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
191 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
192 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
193 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
194 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
195 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
196 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
197 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
198 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
199 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
200 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
201 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
202 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
203 { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
204 { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
205 { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
209 camel_folder_search_construct (CamelFolderSearch *search)
212 CamelFolderSearchClass *class;
214 class = CAMEL_FOLDER_SEARCH_GET_CLASS (search);
216 for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
218 /* c is sure messy sometimes */
219 func = *((gpointer *)(((gchar *)class)+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, G_OBJECT_TYPE_NAME (search));
222 func = (gpointer)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 * Returns: A new CamelFolderSearch widget.
247 camel_folder_search_new (void)
249 CamelFolderSearch *new;
251 new = g_object_new (CAMEL_TYPE_FOLDER_SEARCH, NULL);
252 camel_folder_search_construct(new);
258 * camel_folder_search_set_folder:
262 * Set the folder attribute of the search. This is currently unused, but
263 * could be used to perform a slow-search when indexes and so forth are not
264 * available. Or for use by subclasses.
267 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
269 search->folder = folder;
273 * camel_folder_search_set_summary:
275 * @summary: An array of CamelMessageInfo pointers.
277 * Set the array of summary objects representing the span of the search.
279 * If this is not set, then a subclass must provide the functions
280 * for searching headers and for the match-all operator.
283 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
285 search->summary = summary;
289 * camel_folder_search_set_body_index:
293 * Set the index representing the contents of all messages
294 * in this folder. If this is not set, then the folder implementation
295 * should sub-class the CamelFolderSearch and provide its own
296 * body-contains function.
299 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
301 if (search->body_index)
302 g_object_unref (search->body_index);
303 search->body_index = index;
305 g_object_ref (index);
309 * camel_folder_search_execute_expression:
314 * Execute the search expression @expr, returning an array of
315 * all matches as a GPtrArray of uid's of matching messages.
317 * Note that any settings such as set_body_index(), set_folder(),
318 * and so on are reset to #NULL once the search has completed.
320 * TODO: The interface should probably return summary items instead
321 * (since they are much more useful to any client).
323 * Returns: A GPtrArray of strings of all matching messages.
324 * This must only be freed by camel_folder_search_free_result.
327 camel_folder_search_execute_expression (CamelFolderSearch *search,
335 CamelFolderSearchPrivate *p = search->priv;
339 /* only re-parse if the search has changed */
340 if (search->last_search == NULL
341 || strcmp(search->last_search, expr)) {
342 e_sexp_input_text(search->sexp, expr, strlen(expr));
343 if (e_sexp_parse(search->sexp) == -1) {
344 camel_exception_setv (
346 _("Cannot parse search expression: %s:\n%s"),
347 e_sexp_error(search->sexp), expr);
351 g_free(search->last_search);
352 search->last_search = g_strdup(expr);
354 r = e_sexp_eval(search->sexp);
356 if (!camel_exception_is_set(ex))
357 camel_exception_setv (
359 _("Error executing search expression: %s:\n%s"),
360 e_sexp_error(search->sexp), expr);
364 matches = g_ptr_array_new();
366 /* now create a folder summary to return?? */
367 if (r->type == ESEXP_RES_ARRAY_PTR) {
368 d(printf("got result ...\n"));
369 if (search->summary) {
370 /* reorder result in summary order */
371 results = g_hash_table_new(g_str_hash, g_str_equal);
372 for (i=0;i<r->value.ptrarray->len;i++) {
373 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
374 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
376 for (i=0;i<search->summary->len;i++) {
377 gchar *uid = g_ptr_array_index(search->summary, i);
378 if (g_hash_table_lookup(results, uid)) {
379 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
382 g_hash_table_destroy(results);
384 for (i=0;i<r->value.ptrarray->len;i++) {
385 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
386 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
390 g_warning("Search returned an invalid result type");
393 e_sexp_result_free(search->sexp, r);
396 camel_folder_thread_messages_unref(p->threads);
398 g_hash_table_destroy(p->threads_hash);
401 p->threads_hash = NULL;
402 search->folder = NULL;
403 search->summary = NULL;
404 search->current = NULL;
405 search->body_index = NULL;
411 * camel_folder_search_count:
414 * @uids: to search against, NULL for all uid's.
417 * Run a search. Search must have had Folder already set on it, and
418 * it must implement summaries.
420 * Returns: Number of messages that match the query.
426 camel_folder_search_count (CamelFolderSearch *search,
431 GPtrArray *summary_set;
434 gchar *sql_query, *tmp, *tmp1;
438 CamelFolderSearchPrivate *p = search->priv;
440 g_assert(search->folder);
444 /* We route body-contains search and thread based search through memory and not via db. */
445 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
446 /* setup our search list only contains those we're interested in */
447 search->summary = camel_folder_get_summary(search->folder);
449 summary_set = search->summary;
451 /* only re-parse if the search has changed */
452 if (search->last_search == NULL
453 || strcmp(search->last_search, expr)) {
454 e_sexp_input_text(search->sexp, expr, strlen(expr));
455 if (e_sexp_parse(search->sexp) == -1) {
456 camel_exception_setv (
458 _("Cannot parse search expression: %s:\n%s"),
459 e_sexp_error(search->sexp), expr);
463 g_free(search->last_search);
464 search->last_search = g_strdup(expr);
466 r = e_sexp_eval(search->sexp);
468 if (!camel_exception_is_set(ex))
469 camel_exception_setv (
471 _("Error executing search expression: %s:\n%s"),
472 e_sexp_error(search->sexp), expr);
476 /* now create a folder summary to return?? */
477 if (r->type == ESEXP_RES_ARRAY_PTR) {
478 d(printf("got result\n"));
480 /* reorder result in summary order */
481 results = g_hash_table_new(g_str_hash, g_str_equal);
482 for (i=0;i<r->value.ptrarray->len;i++) {
483 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
484 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
487 for (i=0;i<summary_set->len;i++) {
488 gchar *uid = g_ptr_array_index(summary_set, i);
489 if (g_hash_table_lookup(results, uid))
492 g_hash_table_destroy(results);
495 e_sexp_result_free(search->sexp, r);
498 /* Sync the db, so that we search the db for changes */
499 camel_folder_summary_save_to_db (search->folder->summary, ex);
501 dd(printf ("sexp is : [%s]\n", expr));
502 if (g_getenv("SQL_SEARCH_OLD"))
503 sql_query = camel_sexp_to_sql (expr);
505 sql_query = camel_sexp_to_sql_sexp (expr);
506 tmp1 = camel_db_sqlize_string(search->folder->full_name);
507 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
508 camel_db_free_sqlized_string (tmp1);
510 dd(printf("Equivalent sql %s\n", tmp));
512 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
513 camel_db_count_message_info (cdb, tmp, &count, ex);
514 if (ex && camel_exception_is_set(ex)) {
515 const gchar *exception = camel_exception_get_description (ex);
516 if (strncmp(exception, "no such table", 13) == 0) {
517 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
518 /* Suppress no such table */
519 camel_exception_clear (ex);
526 /* these might be allocated by match-threads */
528 camel_folder_thread_messages_unref(p->threads);
530 g_hash_table_destroy(p->threads_hash);
531 if (search->summary_set)
532 g_ptr_array_free(search->summary_set, TRUE);
534 camel_folder_free_summary(search->folder, search->summary);
537 p->threads_hash = NULL;
538 search->folder = NULL;
539 search->summary = NULL;
540 search->summary_set = NULL;
541 search->current = NULL;
542 search->body_index = NULL;
548 do_search_in_memory (const gchar *expr)
550 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
551 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
557 for (i = 0; in_memory_tokens [i]; i++) {
558 if (strstr (expr, in_memory_tokens [i]))
566 * camel_folder_search_search:
569 * @uids: to search against, NULL for all uid's.
572 * Run a search. Search must have had Folder already set on it, and
573 * it must implement summaries.
578 camel_folder_search_search (CamelFolderSearch *search,
584 GPtrArray *matches = NULL, *summary_set;
587 gchar *sql_query, *tmp, *tmp1;
590 CamelFolderSearchPrivate *p = search->priv;
592 g_assert(search->folder);
596 /* We route body-contains / thread based search and uid search through memory and not via db. */
597 if (uids || do_search_in_memory (expr)) {
598 /* setup our search list only contains those we're interested in */
599 search->summary = camel_folder_get_summary(search->folder);
602 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
604 summary_set = search->summary_set = g_ptr_array_new();
605 for (i=0;i<uids->len;i++)
606 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
607 for (i=0;i<search->summary->len;i++)
608 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
609 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
610 g_hash_table_destroy(uids_hash);
612 summary_set = search->summary;
615 /* only re-parse if the search has changed */
616 if (search->last_search == NULL
617 || strcmp(search->last_search, expr)) {
618 e_sexp_input_text(search->sexp, expr, strlen(expr));
619 if (e_sexp_parse(search->sexp) == -1) {
620 camel_exception_setv (
622 _("Cannot parse search expression: %s:\n%s"),
623 e_sexp_error(search->sexp), expr);
627 g_free(search->last_search);
628 search->last_search = g_strdup(expr);
630 r = e_sexp_eval(search->sexp);
632 if (!camel_exception_is_set(ex))
633 camel_exception_setv (
635 _("Error executing search expression: %s:\n%s"),
636 e_sexp_error(search->sexp), expr);
640 matches = g_ptr_array_new();
642 /* now create a folder summary to return?? */
643 if (r->type == ESEXP_RES_ARRAY_PTR) {
644 d(printf("got result\n"));
646 /* reorder result in summary order */
647 results = g_hash_table_new(g_str_hash, g_str_equal);
648 for (i=0;i<r->value.ptrarray->len;i++) {
649 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
650 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
653 for (i=0;i<summary_set->len;i++) {
654 gchar *uid = g_ptr_array_index(summary_set, i);
655 if (g_hash_table_lookup(results, uid))
656 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
658 g_hash_table_destroy(results);
661 e_sexp_result_free(search->sexp, r);
664 /* Sync the db, so that we search the db for changes */
665 camel_folder_summary_save_to_db (search->folder->summary, ex);
667 dd(printf ("sexp is : [%s]\n", expr));
668 if (g_getenv("SQL_SEARCH_OLD"))
669 sql_query = camel_sexp_to_sql (expr);
671 sql_query = camel_sexp_to_sql_sexp (expr);
672 tmp1 = camel_db_sqlize_string(search->folder->full_name);
673 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
674 camel_db_free_sqlized_string (tmp1);
676 dd(printf("Equivalent sql %s\n", tmp));
678 matches = g_ptr_array_new();
679 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
680 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
681 if (ex && camel_exception_is_set(ex)) {
682 const gchar *exception = camel_exception_get_description (ex);
683 if (strncmp(exception, "no such table", 13) == 0) {
684 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
685 /* Suppress no such table */
686 camel_exception_clear (ex);
694 /* these might be allocated by match-threads */
696 camel_folder_thread_messages_unref(p->threads);
698 g_hash_table_destroy(p->threads_hash);
699 if (search->summary_set)
700 g_ptr_array_free(search->summary_set, TRUE);
702 camel_folder_free_summary(search->folder, search->summary);
705 p->threads_hash = NULL;
706 search->folder = NULL;
707 search->summary = NULL;
708 search->summary_set = NULL;
709 search->current = NULL;
710 search->body_index = NULL;
715 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
717 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
718 g_ptr_array_free(result, TRUE);
721 /* dummy function, returns false always, or an empty match array */
723 search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
727 if (search->current == NULL) {
728 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
729 r->value.boolean = FALSE;
731 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
732 r->value.ptrarray = g_ptr_array_new();
738 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
740 search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
746 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
747 GPtrArray *v = argv[0]->value.ptrarray;
750 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
751 r->value.ptrarray = g_ptr_array_new();
753 /* not against a single message?*/
754 if (search->current) {
757 uid = camel_message_info_uid(search->current);
758 for (i=0;!found && i<v->len;i++) {
759 if (strcmp(uid, v->pdata[i]) == 0)
764 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
765 } else if (search->summary == NULL) {
766 g_warning("No summary set, 'not' against an array requires a summary");
768 /* 'not' against the whole summary */
769 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
773 s = (gchar **)v->pdata;
774 for (i=0;i<v->len;i++)
775 g_hash_table_insert(have, s[i], s[i]);
777 v = search->summary_set?search->summary_set:search->summary;
778 m = (gchar **)v->pdata;
779 for (i=0;i<v->len;i++) {
782 if (g_hash_table_lookup(have, uid) == NULL)
783 g_ptr_array_add(r->value.ptrarray, uid);
785 g_hash_table_destroy(have);
790 if (argv[0]->type == ESEXP_RES_BOOL)
791 res = !argv[0]->value.boolean;
793 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
794 r->value.boolean = res;
797 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
798 r->value.boolean = TRUE;
805 search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
813 g_warning("match-all only takes a single argument, other arguments ignored");
816 /* we are only matching a single message? or already inside a match-all? */
817 if (search->current) {
818 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
820 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
821 r->value.boolean = FALSE;
824 r1 = e_sexp_term_eval(f, argv[0]);
825 if (r1->type == ESEXP_RES_BOOL) {
826 r->value.boolean = r1->value.boolean;
828 g_warning("invalid syntax, matches require a single bool result");
829 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
830 e_sexp_fatal_error(f, error_msg);
833 e_sexp_result_free(f, r1);
835 r->value.boolean = TRUE;
840 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
841 r->value.ptrarray = g_ptr_array_new();
843 if (search->summary == NULL) {
844 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
845 g_warning("No summary supplied, match-all doesn't work with no summary");
850 v = search->summary_set?search->summary_set:search->summary;
852 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
853 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->ex);
856 for (i=0;i<v->len;i++) {
859 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
860 if (!search->current)
862 uid = camel_message_info_uid(search->current);
865 r1 = e_sexp_term_eval(f, argv[0]);
866 if (r1->type == ESEXP_RES_BOOL) {
867 if (r1->value.boolean)
868 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
870 g_warning("invalid syntax, matches require a single bool result");
871 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
872 e_sexp_fatal_error(f, error_msg);
875 e_sexp_result_free(f, r1);
877 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
879 camel_message_info_free (search->current);
881 search->current = NULL;
886 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
889 g_hash_table_insert(id_hash, (gchar *)camel_message_info_uid(root->message), root);
891 fill_thread_table(root->child, id_hash);
897 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
900 g_hash_table_insert(result_hash, (gchar *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
902 add_thread_results(root->child, result_hash);
908 add_results(gchar *uid, gpointer dummy, GPtrArray *result)
910 g_ptr_array_add(result, uid);
914 search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
917 CamelFolderSearchPrivate *p = search->priv;
922 /* not supported in match-all */
923 if (search->current) {
924 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
925 e_sexp_fatal_error(f, error_msg);
930 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
931 e_sexp_fatal_error(f, error_msg);
935 r = e_sexp_term_eval(f, argv[0]);
936 if (r->type != ESEXP_RES_STRING) {
937 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
938 e_sexp_fatal_error(f, error_msg);
943 if (!strcmp(r->value.string, "none"))
945 else if (!strcmp(r->value.string, "all"))
947 else if (!strcmp(r->value.string, "replies"))
949 else if (!strcmp(r->value.string, "replies_parents"))
951 else if (!strcmp(r->value.string, "single"))
953 e_sexp_result_free(f, r);
955 /* behave as (begin does */
957 for (i=1;i<argc;i++) {
959 e_sexp_result_free(f, r);
960 r = e_sexp_term_eval(f, argv[i]);
963 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
964 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
965 e_sexp_fatal_error(f, error_msg);
972 if (search->folder == NULL) {
973 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
974 e_sexp_fatal_error(f, error_msg);
978 /* cache this, so we only have to re-calculate once per search at most */
979 if (p->threads == NULL) {
980 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
981 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
983 fill_thread_table(p->threads->tree, p->threads_hash);
986 results = g_hash_table_new(g_str_hash, g_str_equal);
987 for (i=0;i<r->value.ptrarray->len;i++) {
988 struct _CamelFolderThreadNode *node, *scan;
991 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
993 node = g_hash_table_lookup(p->threads_hash, (gchar *)g_ptr_array_index(r->value.ptrarray, i));
994 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
997 /* select messages in thread according to search criteria */
999 if (node->child == NULL && node->parent == NULL)
1000 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
1004 while (scan && scan->parent) {
1005 scan = scan->parent;
1006 g_hash_table_insert(results, (gchar *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
1008 } else if (type == 1) {
1009 while (node && node->parent)
1010 node = node->parent;
1012 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
1014 add_thread_results(node->child, results);
1017 e_sexp_result_free(f, r);
1019 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1020 r->value.ptrarray = g_ptr_array_new();
1022 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
1023 g_hash_table_destroy(results);
1028 static CamelMimeMessage *
1029 get_current_message (CamelFolderSearch *search)
1031 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1032 CamelMimeMessage *res;
1034 if (!search || !search->folder || !search->current)
1037 res = camel_folder_get_message (search->folder, search->current->uid, &x);
1040 camel_exception_clear (&x);
1045 static ESExpResult *
1046 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1051 r(printf("executing check-header %d\n", how));
1053 /* are we inside a match-all? */
1054 if (search->current && argc>1
1055 && argv[0]->type == ESEXP_RES_STRING) {
1057 const gchar *header = NULL, *charset = NULL;
1060 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1061 struct _camel_search_words *words;
1062 CamelMimeMessage *message = NULL;
1063 struct _camel_header_raw *raw_header;
1065 /* only a subset of headers are supported .. */
1066 headername = argv[0]->value.string;
1067 if (!g_ascii_strcasecmp(headername, "subject")) {
1068 header = camel_message_info_subject(search->current);
1069 } else if (!g_ascii_strcasecmp(headername, "date")) {
1070 /* FIXME: not a very useful form of the date */
1071 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1073 } else if (!g_ascii_strcasecmp(headername, "from")) {
1074 header = camel_message_info_from(search->current);
1075 type = CAMEL_SEARCH_TYPE_ADDRESS;
1076 } else if (!g_ascii_strcasecmp(headername, "to")) {
1077 header = camel_message_info_to(search->current);
1078 type = CAMEL_SEARCH_TYPE_ADDRESS;
1079 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1080 header = camel_message_info_cc(search->current);
1081 type = CAMEL_SEARCH_TYPE_ADDRESS;
1082 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1083 header = camel_message_info_mlist(search->current);
1084 type = CAMEL_SEARCH_TYPE_MLIST;
1086 message = get_current_message (search);
1088 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1091 charset = camel_content_type_param (ct, "charset");
1092 charset = camel_iconv_charset_name (charset);
1100 /* performs an OR of all words */
1101 for (i=1;i<argc && !truth;i++) {
1102 if (argv[i]->type == ESEXP_RES_STRING) {
1103 if (argv[i]->value.string[0] == 0) {
1105 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1106 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1107 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1109 for (j=0;j<words->len && truth;j++) {
1111 for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1112 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1113 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1118 truth = raw_header != NULL;
1120 truth = camel_search_header_match(header, words->words[j]->word, how, type, charset);
1122 camel_search_words_free(words);
1125 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1126 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1127 truth = camel_search_header_match(raw_header->value, argv[i]->value.string, how, type, charset);
1131 truth = camel_search_header_match(header, argv[i]->value.string, how, type, charset);
1137 g_object_unref (message);
1139 /* TODO: else, find all matches */
1141 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1142 r->value.boolean = truth;
1149 l_printf(gchar *node)
1151 printf("%s\t", node);
1155 static ESExpResult *
1156 search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1158 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1161 static ESExpResult *
1162 search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1164 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1167 static ESExpResult *
1168 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1170 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1173 static ESExpResult *
1174 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1176 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1179 static ESExpResult *
1180 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1184 r(printf ("executing header-exists\n"));
1186 if (search->current) {
1187 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1188 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1189 r->value.boolean = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
1192 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1193 r->value.ptrarray = g_ptr_array_new();
1199 static ESExpResult *
1200 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1202 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1205 static ESExpResult *
1206 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1209 CamelMimeMessage *msg;
1211 msg = get_current_message (search);
1215 const gchar *contents;
1217 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1219 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1220 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1221 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->ex) == 0) {
1222 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1225 r->value.boolean = FALSE;
1227 g_object_unref (msg);
1229 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1230 r->value.ptrarray = g_ptr_array_new();
1237 get_full_header (CamelMimeMessage *message)
1239 CamelMimePart *mp = CAMEL_MIME_PART (message);
1240 GString *str = g_string_new ("");
1241 struct _camel_header_raw *h;
1243 for (h = mp->headers; h; h = h->next) {
1244 if (h->value != NULL) {
1245 g_string_append (str, h->name);
1246 if (isspace (h->value[0]))
1247 g_string_append (str, ":");
1249 g_string_append (str, ": ");
1250 g_string_append (str, h->value);
1251 g_string_append_c (str, '\n');
1255 return g_string_free (str, FALSE);
1258 static ESExpResult *
1259 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1262 CamelMimeMessage *msg;
1264 msg = get_current_message (search);
1269 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1271 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) {
1274 contents = get_full_header (msg);
1275 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1280 r->value.boolean = FALSE;
1282 g_object_unref (msg);
1284 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1285 r->value.ptrarray = g_ptr_array_new();
1291 /* this is just to OR results together */
1297 /* or, store all unique values */
1299 htor(gchar *key, gint value, struct IterData *iter_data)
1301 g_ptr_array_add(iter_data->uids, key);
1304 /* and, only store duplicates */
1306 htand(gchar *key, gint value, struct IterData *iter_data)
1308 if (value == iter_data->count)
1309 g_ptr_array_add(iter_data->uids, key);
1313 match_message_index (CamelIndex *idx,
1318 CamelIndexCursor *wc, *nc;
1319 const gchar *word, *name;
1322 wc = camel_index_words(idx);
1324 while (!truth && (word = camel_index_cursor_next(wc))) {
1325 if (camel_ustrstrcase(word,match) != NULL) {
1326 /* perf: could have the wc cursor return the name cursor */
1327 nc = camel_index_find(idx, word);
1329 while (!truth && (name = camel_index_cursor_next(nc)))
1330 truth = strcmp(name, uid) == 0;
1331 g_object_unref (nc);
1335 g_object_unref (wc);
1342 "one two" "three" "four five"
1351 /* returns messages which contain all words listed in words */
1353 match_words_index (CamelFolderSearch *search,
1354 struct _camel_search_words *words,
1357 GPtrArray *result = g_ptr_array_new();
1358 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1359 struct IterData lambdafoo;
1360 CamelIndexCursor *wc, *nc;
1361 const gchar *word, *name;
1364 /* we can have a maximum of 32 words, as we use it as the AND mask */
1366 wc = camel_index_words(search->body_index);
1368 while ((word = camel_index_cursor_next(wc))) {
1369 for (i=0;i<words->len;i++) {
1370 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1371 /* perf: could have the wc cursor return the name cursor */
1372 nc = camel_index_find(search->body_index, word);
1374 while ((name = camel_index_cursor_next(nc))) {
1377 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1378 g_hash_table_insert(ht, (gchar *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1380 g_object_unref (nc);
1385 g_object_unref (wc);
1387 lambdafoo.uids = result;
1388 lambdafoo.count = (1<<words->len) - 1;
1389 g_hash_table_foreach(ht, (GHFunc)htand, &lambdafoo);
1390 g_hash_table_destroy(ht);
1397 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1399 CamelDataWrapper *containee;
1403 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1405 if (containee == NULL)
1408 /* using the object types is more accurate than using the mime/types */
1409 if (CAMEL_IS_MULTIPART (containee)) {
1410 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1411 for (i = 0; i < parts && truth == FALSE; i++) {
1412 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1414 truth = match_words_1message(part, words, mask);
1416 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1417 /* for messages we only look at its contents */
1418 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1419 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1420 /* for all other text parts, we look inside, otherwise we dont care */
1421 CamelStream *stream;
1422 GByteArray *byte_array;
1424 byte_array = g_byte_array_new ();
1425 stream = camel_stream_mem_new_with_byte_array (byte_array);
1427 /* FIXME: The match should be part of a stream op */
1428 camel_data_wrapper_decode_to_stream (containee, stream);
1429 camel_stream_write (stream, "", 1);
1430 for (i=0;i<words->len;i++) {
1431 /* FIXME: This is horridly slow, and should use a real search algorithm */
1432 if (camel_ustrstrcase((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1434 /* shortcut a match */
1435 if (*mask == (1<<(words->len))-1)
1440 g_object_unref (stream);
1447 match_words_message (CamelFolder *folder,
1449 struct _camel_search_words *words,
1453 CamelMimeMessage *msg;
1454 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1457 msg = camel_folder_get_message(folder, uid, &x);
1460 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1461 g_object_unref (msg);
1463 camel_exception_clear (&x);
1469 match_words_messages (CamelFolderSearch *search,
1470 struct _camel_search_words *words,
1474 GPtrArray *matches = g_ptr_array_new();
1476 if (search->body_index) {
1478 struct _camel_search_words *simple;
1480 simple = camel_search_words_simple(words);
1481 indexed = match_words_index(search, simple, ex);
1482 camel_search_words_free(simple);
1484 for (i=0;i<indexed->len;i++) {
1485 const gchar *uid = g_ptr_array_index(indexed, i);
1487 if (match_words_message(search->folder, uid, words, ex))
1488 g_ptr_array_add(matches, (gchar *)uid);
1491 g_ptr_array_free(indexed, TRUE);
1493 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1495 for (i=0;i<v->len;i++) {
1496 gchar *uid = g_ptr_array_index(v, i);
1498 if (match_words_message(search->folder, uid, words, ex))
1499 g_ptr_array_add(matches, (gchar *)uid);
1506 static ESExpResult *
1507 search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1510 CamelException *ex = search->priv->ex;
1511 struct _camel_search_words *words;
1513 struct IterData lambdafoo;
1515 if (search->current) {
1518 if (argc == 1 && argv[0]->value.string[0] == 0) {
1521 for (i=0;i<argc && !truth;i++) {
1522 if (argv[i]->type == ESEXP_RES_STRING) {
1523 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1525 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1526 for (j=0;j<words->len && truth;j++)
1527 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1529 /* TODO: cache current message incase of multiple body search terms */
1530 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1532 camel_search_words_free(words);
1536 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1537 r->value.boolean = truth;
1539 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1540 r->value.ptrarray = g_ptr_array_new();
1542 if (argc == 1 && argv[0]->value.string[0] == 0) {
1543 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1545 for (i=0;i<v->len;i++) {
1546 gchar *uid = g_ptr_array_index(v, i);
1548 g_ptr_array_add(r->value.ptrarray, uid);
1551 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1554 for (i=0;i<argc;i++) {
1555 if (argv[i]->type == ESEXP_RES_STRING) {
1556 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1557 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1558 matches = match_words_index(search, words, ex);
1560 matches = match_words_messages(search, words, ex);
1562 for (j=0;j<matches->len;j++) {
1563 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1565 g_ptr_array_free(matches, TRUE);
1566 camel_search_words_free(words);
1569 lambdafoo.uids = r->value.ptrarray;
1570 g_hash_table_foreach(ht, (GHFunc)htor, &lambdafoo);
1571 g_hash_table_destroy(ht);
1578 static ESExpResult *
1579 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1582 CamelMimeMessage *msg = get_current_message (search);
1587 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1589 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) {
1590 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1593 r->value.boolean = FALSE;
1595 g_object_unref (msg);
1599 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1600 r->value.ptrarray = g_ptr_array_new ();
1602 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) {
1604 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1605 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1606 CamelMimeMessage *message;
1608 for (i = 0; i < v->len; i++) {
1609 gchar *uid = g_ptr_array_index(v, i);
1611 message = camel_folder_get_message (search->folder, uid, &x);
1613 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1614 g_ptr_array_add (r->value.ptrarray, uid);
1617 g_object_unref (message);
1619 camel_exception_clear (&x);
1630 static ESExpResult *
1631 search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1636 r(printf("executing user-flag\n"));
1638 /* are we inside a match-all? */
1639 if (search->current) {
1641 /* performs an OR of all words */
1642 for (i=0;i<argc && !truth;i++) {
1643 if (argv[i]->type == ESEXP_RES_STRING
1644 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1649 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1650 r->value.boolean = truth;
1652 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1653 r->value.ptrarray = g_ptr_array_new();
1659 static ESExpResult *
1660 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1664 r(printf ("executing system-flag\n"));
1666 if (search->current) {
1667 gboolean truth = FALSE;
1670 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1672 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1673 r->value.boolean = truth;
1675 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1676 r->value.ptrarray = g_ptr_array_new ();
1682 static ESExpResult *
1683 search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1685 const gchar *value = NULL;
1688 r(printf("executing user-tag\n"));
1690 if (search->current && argc == 1)
1691 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1693 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1694 r->value.string = g_strdup (value ? value : "");
1699 static ESExpResult *
1700 search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1704 r(printf("executing get-sent-date\n"));
1706 /* are we inside a match-all? */
1708 r = e_sexp_result_new(f, ESEXP_RES_INT);
1710 r->value.number = camel_message_info_date_sent(s->current);
1712 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1713 r->value.ptrarray = g_ptr_array_new ();
1719 static ESExpResult *
1720 search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1724 r(printf("executing get-received-date\n"));
1726 /* are we inside a match-all? */
1728 r = e_sexp_result_new(f, ESEXP_RES_INT);
1730 r->value.number = camel_message_info_date_received(s->current);
1732 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1733 r->value.ptrarray = g_ptr_array_new ();
1739 static ESExpResult *
1740 search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1744 r(printf("executing get-current-date\n"));
1746 r = e_sexp_result_new(f, ESEXP_RES_INT);
1747 r->value.number = time (NULL);
1751 static ESExpResult *
1752 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1756 r(printf("executing get-size\n"));
1758 /* are we inside a match-all? */
1760 r = e_sexp_result_new (f, ESEXP_RES_INT);
1761 r->value.number = camel_message_info_size(s->current) / 1024;
1763 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1764 r->value.ptrarray = g_ptr_array_new ();
1770 static ESExpResult *
1771 search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1776 r(printf("executing uid\n"));
1778 /* are we inside a match-all? */
1779 if (search->current) {
1781 const gchar *uid = camel_message_info_uid(search->current);
1783 /* performs an OR of all words */
1784 for (i=0;i<argc && !truth;i++) {
1785 if (argv[i]->type == ESEXP_RES_STRING
1786 && !strcmp(uid, argv[i]->value.string)) {
1791 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1792 r->value.boolean = truth;
1794 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1795 r->value.ptrarray = g_ptr_array_new();
1796 for (i=0;i<argc;i++) {
1797 if (argv[i]->type == ESEXP_RES_STRING)
1798 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1806 read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name)
1810 matches = (GPtrArray *) ref;
1812 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols [0]));
1816 static ESExpResult *
1817 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1820 gboolean same = FALSE;
1822 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1823 if (argv[0]->value.string && search->folder && search->folder->parent_store && camel_folder_get_full_name (search->folder)) {
1824 CamelFolderInfo *fi = camel_store_get_folder_info (search->folder->parent_store, camel_folder_get_full_name (search->folder), 0, NULL);
1826 same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
1828 camel_store_free_folder_info (search->folder->parent_store, fi);
1833 if (search->current) {
1834 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1835 r->value.boolean = same ? TRUE : FALSE;
1837 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1838 r->value.ptrarray = g_ptr_array_new ();
1843 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1845 for (i = 0; i < v->len; i++) {
1846 gchar *uid = g_ptr_array_index (v, i);
1848 g_ptr_array_add (r->value.ptrarray, uid);