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-folder-search.h"
41 #include "camel-folder-thread.h"
42 #include "camel-iconv.h"
43 #include "camel-medium.h"
44 #include "camel-mime-message.h"
45 #include "camel-multipart.h"
46 #include "camel-search-private.h"
47 #include "camel-stream-mem.h"
49 #include "camel-debug.h"
50 #include "camel-store.h"
51 #include "camel-vee-folder.h"
52 #include "camel-string-utils.h"
53 #include "camel-search-sql.h"
54 #include "camel-search-sql-sexp.h"
58 #define dd(x) if (camel_debug("search")) x
60 #define CAMEL_FOLDER_SEARCH_GET_PRIVATE(obj) \
61 (G_TYPE_INSTANCE_GET_PRIVATE \
62 ((obj), CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate))
64 struct _CamelFolderSearchPrivate {
67 CamelFolderThread *threads;
68 GHashTable *threads_hash;
71 static ESExpResult *search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
79 static ESExpResult *search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
81 static ESExpResult *search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
82 static ESExpResult *search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
83 static ESExpResult *search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
84 static ESExpResult *search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
85 static ESExpResult *search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
93 static ESExpResult *search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
95 static ESExpResult *search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
97 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
99 G_DEFINE_TYPE (CamelFolderSearch, camel_folder_search, CAMEL_TYPE_OBJECT)
102 folder_search_dispose (GObject *object)
104 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
106 if (search->sexp != NULL) {
107 e_sexp_unref (search->sexp);
111 /* Chain up to parent's dispose() method. */
112 G_OBJECT_CLASS (camel_folder_search_parent_class)->dispose (object);
116 folder_search_finalize (GObject *object)
118 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
120 g_free (search->last_search);
122 /* Chain up to parent's finalize() method. */
123 G_OBJECT_CLASS (camel_folder_search_parent_class)->finalize (object);
127 camel_folder_search_class_init (CamelFolderSearchClass *class)
129 GObjectClass *object_class;
131 g_type_class_add_private (class, sizeof (CamelFolderSearchPrivate));
133 object_class = G_OBJECT_CLASS (class);
134 object_class->dispose = folder_search_dispose;
135 object_class->finalize = folder_search_finalize;
137 class->not = search_not;
138 class->match_all = search_match_all;
139 class->match_threads = search_match_threads;
140 class->body_contains = search_body_contains;
141 class->body_regex = search_body_regex;
142 class->header_contains = search_header_contains;
143 class->header_matches = search_header_matches;
144 class->header_starts_with = search_header_starts_with;
145 class->header_ends_with = search_header_ends_with;
146 class->header_exists = search_header_exists;
147 class->header_soundex = search_header_soundex;
148 class->header_regex = search_header_regex;
149 class->header_full_regex = search_header_full_regex;
150 class->user_tag = search_user_tag;
151 class->user_flag = search_user_flag;
152 class->system_flag = search_system_flag;
153 class->get_sent_date = search_get_sent_date;
154 class->get_received_date = search_get_received_date;
155 class->get_current_date = search_get_current_date;
156 class->get_size = search_get_size;
157 class->uid = search_uid;
158 class->message_location = search_message_location;
162 camel_folder_search_init (CamelFolderSearch *search)
164 search->priv = CAMEL_FOLDER_SEARCH_GET_PRIVATE (search);
165 search->sexp = e_sexp_new ();
171 gint flags; /* 0x02 = immediate, 0x01 = always enter */
173 /* these have default implementations in e-sexp */
174 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
175 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
176 /* we need to override this one though to implement an 'array not' */
177 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
178 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
179 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
180 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
182 /* these we have to use our own default if there is none */
183 /* they should all be defined in the language? so it parses, or should they not?? */
184 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
185 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
186 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
187 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
188 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
189 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
190 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
191 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
192 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
193 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
194 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
195 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
196 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
197 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
198 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
199 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
200 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
201 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
202 { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
203 { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
204 { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
208 camel_folder_search_construct (CamelFolderSearch *search)
211 CamelFolderSearchClass *class;
213 class = CAMEL_FOLDER_SEARCH_GET_CLASS (search);
215 for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
217 /* c is sure messy sometimes */
218 func = *((gpointer *)(((gchar *)class)+builtins[i].offset));
219 if (func == NULL && builtins[i].flags&1) {
220 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, G_OBJECT_TYPE_NAME (search));
221 func = (gpointer)search_dummy;
224 if (builtins[i].flags&2) {
225 e_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
227 e_sexp_add_function (search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
234 * camel_folder_search_new:
236 * Create a new CamelFolderSearch object.
238 * A CamelFolderSearch is a subclassable, extensible s-exp
239 * evaluator which enforces a particular set of s-expressions.
240 * Particular methods may be overriden by an implementation to
241 * implement a search for any sort of backend.
243 * Returns: A new CamelFolderSearch widget.
246 camel_folder_search_new (void)
248 CamelFolderSearch *new;
250 new = g_object_new (CAMEL_TYPE_FOLDER_SEARCH, NULL);
251 camel_folder_search_construct (new);
257 * camel_folder_search_set_folder:
261 * Set the folder attribute of the search. This is currently unused, but
262 * could be used to perform a slow-search when indexes and so forth are not
263 * available. Or for use by subclasses.
266 camel_folder_search_set_folder (CamelFolderSearch *search, CamelFolder *folder)
268 search->folder = folder;
272 * camel_folder_search_set_summary:
274 * @summary: An array of CamelMessageInfo pointers.
276 * Set the array of summary objects representing the span of the search.
278 * If this is not set, then a subclass must provide the functions
279 * for searching headers and for the match-all operator.
282 camel_folder_search_set_summary (CamelFolderSearch *search, GPtrArray *summary)
284 search->summary = summary;
288 * camel_folder_search_set_body_index:
292 * Set the index representing the contents of all messages
293 * in this folder. If this is not set, then the folder implementation
294 * should sub-class the CamelFolderSearch and provide its own
295 * body-contains function.
298 camel_folder_search_set_body_index (CamelFolderSearch *search, CamelIndex *index)
300 if (search->body_index)
301 g_object_unref (search->body_index);
302 search->body_index = index;
304 g_object_ref (index);
308 * camel_folder_search_execute_expression:
311 * @error: return location for a #GError, or %NULL
313 * Execute the search expression @expr, returning an array of
314 * all matches as a GPtrArray of uid's of matching messages.
316 * Note that any settings such as set_body_index(), set_folder(),
317 * and so on are reset to #NULL once the search has completed.
319 * TODO: The interface should probably return summary items instead
320 * (since they are much more useful to any client).
322 * Returns: A GPtrArray of strings of all matching messages.
323 * This must only be freed by camel_folder_search_free_result.
326 camel_folder_search_execute_expression (CamelFolderSearch *search,
334 CamelFolderSearchPrivate *p = search->priv;
338 /* only re-parse if the search has changed */
339 if (search->last_search == NULL
340 || strcmp (search->last_search, expr)) {
341 e_sexp_input_text (search->sexp, expr, strlen (expr));
342 if (e_sexp_parse (search->sexp) == -1) {
344 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
345 _("Cannot parse search expression: %s:\n%s"),
346 e_sexp_error (search->sexp), expr);
350 g_free (search->last_search);
351 search->last_search = g_strdup (expr);
353 r = e_sexp_eval (search->sexp);
356 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
357 _("Error executing search expression: %s:\n%s"),
358 e_sexp_error (search->sexp), expr);
362 matches = g_ptr_array_new ();
364 /* now create a folder summary to return?? */
365 if (r->type == ESEXP_RES_ARRAY_PTR) {
366 d(printf("got result ...\n"));
367 if (search->summary) {
368 /* reorder result in summary order */
369 results = g_hash_table_new (g_str_hash, g_str_equal);
370 for (i=0;i<r->value.ptrarray->len;i++) {
371 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
372 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
374 for (i=0;i<search->summary->len;i++) {
375 gchar *uid = g_ptr_array_index (search->summary, i);
376 if (g_hash_table_lookup (results, uid)) {
377 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
380 g_hash_table_destroy (results);
382 for (i=0;i<r->value.ptrarray->len;i++) {
383 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
384 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (g_ptr_array_index (r->value.ptrarray, i)));
388 g_warning("Search returned an invalid result type");
391 e_sexp_result_free (search->sexp, r);
394 camel_folder_thread_messages_unref (p->threads);
396 g_hash_table_destroy (p->threads_hash);
399 p->threads_hash = NULL;
400 search->folder = NULL;
401 search->summary = NULL;
402 search->current = NULL;
403 search->body_index = NULL;
409 * camel_folder_search_count:
412 * @uids: to search against, NULL for all uid's.
413 * @error: return location for a #GError, or %NULL
415 * Run a search. Search must have had Folder already set on it, and
416 * it must implement summaries.
418 * Returns: Number of messages that match the query.
424 camel_folder_search_count (CamelFolderSearch *search,
429 GPtrArray *summary_set;
432 gchar *sql_query, *tmp, *tmp1;
436 CamelFolderSearchPrivate *p = search->priv;
438 g_assert (search->folder);
442 /* We route body-contains search and thread based search through memory and not via db. */
443 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
444 /* setup our search list only contains those we're interested in */
445 search->summary = camel_folder_get_summary (search->folder);
447 summary_set = search->summary;
449 /* only re-parse if the search has changed */
450 if (search->last_search == NULL
451 || strcmp (search->last_search, expr)) {
452 e_sexp_input_text (search->sexp, expr, strlen (expr));
453 if (e_sexp_parse (search->sexp) == -1) {
455 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
456 _("Cannot parse search expression: %s:\n%s"),
457 e_sexp_error (search->sexp), expr);
461 g_free (search->last_search);
462 search->last_search = g_strdup (expr);
464 r = e_sexp_eval (search->sexp);
467 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
468 _("Error executing search expression: %s:\n%s"),
469 e_sexp_error (search->sexp), expr);
473 /* now create a folder summary to return?? */
474 if (r->type == ESEXP_RES_ARRAY_PTR) {
475 d(printf("got result\n"));
477 /* reorder result in summary order */
478 results = g_hash_table_new (g_str_hash, g_str_equal);
479 for (i=0;i<r->value.ptrarray->len;i++) {
480 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
481 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
484 for (i=0;i<summary_set->len;i++) {
485 gchar *uid = g_ptr_array_index (summary_set, i);
486 if (g_hash_table_lookup (results, uid))
489 g_hash_table_destroy (results);
492 e_sexp_result_free (search->sexp, r);
495 CamelStore *parent_store;
496 const gchar *full_name;
497 GError *local_error = NULL;
499 full_name = camel_folder_get_full_name (search->folder);
500 parent_store = camel_folder_get_parent_store (search->folder);
502 /* Sync the db, so that we search the db for changes */
503 camel_folder_summary_save_to_db (search->folder->summary, error);
505 dd(printf ("sexp is : [%s]\n", expr));
506 if (g_getenv("SQL_SEARCH_OLD"))
507 sql_query = camel_sexp_to_sql (expr);
509 sql_query = camel_sexp_to_sql_sexp (expr);
510 tmp1 = camel_db_sqlize_string (full_name);
511 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
512 camel_db_free_sqlized_string (tmp1);
514 dd(printf("Equivalent sql %s\n", tmp));
516 cdb = (CamelDB *) (parent_store->cdb_r);
517 camel_db_count_message_info (cdb, tmp, &count, &local_error);
518 if (local_error != NULL) {
519 const gchar *message = local_error->message;
520 if (strncmp(message, "no such table", 13) == 0) {
521 d(g_warning ("Error during searching %s: %s\n", tmp, message));
522 /* Suppress no such table */
523 g_clear_error (&local_error);
525 g_propagate_error (error, local_error);
531 /* these might be allocated by match-threads */
533 camel_folder_thread_messages_unref (p->threads);
535 g_hash_table_destroy (p->threads_hash);
536 if (search->summary_set)
537 g_ptr_array_free (search->summary_set, TRUE);
539 camel_folder_free_summary (search->folder, search->summary);
542 p->threads_hash = NULL;
543 search->folder = NULL;
544 search->summary = NULL;
545 search->summary_set = NULL;
546 search->current = NULL;
547 search->body_index = NULL;
553 do_search_in_memory (const gchar *expr)
555 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
556 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
562 for (i = 0; in_memory_tokens[i]; i++) {
563 if (strstr (expr, in_memory_tokens[i]))
571 * camel_folder_search_search:
574 * @uids: to search against, NULL for all uid's.
575 * @error: return location for a #GError, or %NULL
577 * Run a search. Search must have had Folder already set on it, and
578 * it must implement summaries.
583 camel_folder_search_search (CamelFolderSearch *search,
589 GPtrArray *matches = NULL, *summary_set;
592 gchar *sql_query, *tmp, *tmp1;
595 CamelFolderSearchPrivate *p = search->priv;
597 g_assert (search->folder);
601 /* We route body-contains / thread based search and uid search through memory and not via db. */
602 if (uids || do_search_in_memory (expr)) {
603 /* setup our search list only contains those we're interested in */
604 search->summary = camel_folder_get_summary (search->folder);
607 GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
609 summary_set = search->summary_set = g_ptr_array_new ();
610 for (i=0;i<uids->len;i++)
611 g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
612 for (i=0;i<search->summary->len;i++)
613 if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
614 g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
615 g_hash_table_destroy (uids_hash);
617 summary_set = search->summary;
620 /* only re-parse if the search has changed */
621 if (search->last_search == NULL
622 || strcmp (search->last_search, expr)) {
623 e_sexp_input_text (search->sexp, expr, strlen (expr));
624 if (e_sexp_parse (search->sexp) == -1) {
626 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
627 _("Cannot parse search expression: %s:\n%s"),
628 e_sexp_error (search->sexp), expr);
632 g_free (search->last_search);
633 search->last_search = g_strdup (expr);
635 r = e_sexp_eval (search->sexp);
638 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
639 _("Error executing search expression: %s:\n%s"),
640 e_sexp_error (search->sexp), expr);
644 matches = g_ptr_array_new ();
646 /* now create a folder summary to return?? */
647 if (r->type == ESEXP_RES_ARRAY_PTR) {
648 d(printf("got result\n"));
650 /* reorder result in summary order */
651 results = g_hash_table_new (g_str_hash, g_str_equal);
652 for (i=0;i<r->value.ptrarray->len;i++) {
653 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
654 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
657 for (i=0;i<summary_set->len;i++) {
658 gchar *uid = g_ptr_array_index (summary_set, i);
659 if (g_hash_table_lookup (results, uid))
660 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
662 g_hash_table_destroy (results);
665 e_sexp_result_free (search->sexp, r);
668 CamelStore *parent_store;
669 const gchar *full_name;
670 GError *local_error = NULL;
672 full_name = camel_folder_get_full_name (search->folder);
673 parent_store = camel_folder_get_parent_store (search->folder);
675 /* Sync the db, so that we search the db for changes */
676 camel_folder_summary_save_to_db (search->folder->summary, error);
678 dd(printf ("sexp is : [%s]\n", expr));
679 if (g_getenv("SQL_SEARCH_OLD"))
680 sql_query = camel_sexp_to_sql (expr);
682 sql_query = camel_sexp_to_sql_sexp (expr);
683 tmp1 = camel_db_sqlize_string (full_name);
684 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
685 camel_db_free_sqlized_string (tmp1);
687 dd(printf("Equivalent sql %s\n", tmp));
689 matches = g_ptr_array_new ();
690 cdb = (CamelDB *) (parent_store->cdb_r);
692 cdb, tmp, (CamelDBSelectCB)
693 read_uid_callback, matches, &local_error);
694 if (local_error != NULL) {
695 const gchar *message = local_error->message;
696 if (strncmp(message, "no such table", 13) == 0) {
697 d(g_warning ("Error during searching %s: %s\n", tmp, message));
698 /* Suppress no such table */
699 g_clear_error (&local_error);
701 g_propagate_error (error, local_error);
708 /* these might be allocated by match-threads */
710 camel_folder_thread_messages_unref (p->threads);
712 g_hash_table_destroy (p->threads_hash);
713 if (search->summary_set)
714 g_ptr_array_free (search->summary_set, TRUE);
716 camel_folder_free_summary (search->folder, search->summary);
719 p->threads_hash = NULL;
720 search->folder = NULL;
721 search->summary = NULL;
722 search->summary_set = NULL;
723 search->current = NULL;
724 search->body_index = NULL;
729 void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *result)
731 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
732 g_ptr_array_free (result, TRUE);
735 /* dummy function, returns false always, or an empty match array */
737 search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
741 if (search->current == NULL) {
742 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
743 r->value.boolean = FALSE;
745 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
746 r->value.ptrarray = g_ptr_array_new ();
752 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
754 search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
760 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
761 GPtrArray *v = argv[0]->value.ptrarray;
764 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
765 r->value.ptrarray = g_ptr_array_new ();
767 /* not against a single message?*/
768 if (search->current) {
771 uid = camel_message_info_uid (search->current);
772 for (i=0;!found && i<v->len;i++) {
773 if (strcmp (uid, v->pdata[i]) == 0)
778 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
779 } else if (search->summary == NULL) {
780 g_warning("No summary set, 'not' against an array requires a summary");
782 /* 'not' against the whole summary */
783 GHashTable *have = g_hash_table_new (g_str_hash, g_str_equal);
787 s = (gchar **)v->pdata;
788 for (i=0;i<v->len;i++)
789 g_hash_table_insert (have, s[i], s[i]);
791 v = search->summary_set?search->summary_set:search->summary;
792 m = (gchar **)v->pdata;
793 for (i=0;i<v->len;i++) {
796 if (g_hash_table_lookup (have, uid) == NULL)
797 g_ptr_array_add (r->value.ptrarray, uid);
799 g_hash_table_destroy (have);
804 if (argv[0]->type == ESEXP_RES_BOOL)
805 res = !argv[0]->value.boolean;
807 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
808 r->value.boolean = res;
811 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
812 r->value.boolean = TRUE;
819 search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
827 g_warning("match-all only takes a single argument, other arguments ignored");
830 /* we are only matching a single message? or already inside a match-all? */
831 if (search->current) {
832 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
834 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
835 r->value.boolean = FALSE;
838 r1 = e_sexp_term_eval (f, argv[0]);
839 if (r1->type == ESEXP_RES_BOOL) {
840 r->value.boolean = r1->value.boolean;
842 g_warning("invalid syntax, matches require a single bool result");
843 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
844 e_sexp_fatal_error (f, error_msg);
847 e_sexp_result_free (f, r1);
849 r->value.boolean = TRUE;
854 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
855 r->value.ptrarray = g_ptr_array_new ();
857 if (search->summary == NULL) {
858 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
859 g_warning("No summary supplied, match-all doesn't work with no summary");
864 v = search->summary_set?search->summary_set:search->summary;
866 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
867 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
870 for (i=0;i<v->len;i++) {
873 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
874 if (!search->current)
876 uid = camel_message_info_uid (search->current);
879 r1 = e_sexp_term_eval (f, argv[0]);
880 if (r1->type == ESEXP_RES_BOOL) {
881 if (r1->value.boolean)
882 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
884 g_warning("invalid syntax, matches require a single bool result");
885 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
886 e_sexp_fatal_error (f, error_msg);
889 e_sexp_result_free (f, r1);
891 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
893 camel_message_info_free (search->current);
895 search->current = NULL;
900 fill_thread_table (struct _CamelFolderThreadNode *root, GHashTable *id_hash)
903 g_hash_table_insert (id_hash, (gchar *)camel_message_info_uid (root->message), root);
905 fill_thread_table (root->child, id_hash);
911 add_thread_results (struct _CamelFolderThreadNode *root, GHashTable *result_hash)
914 g_hash_table_insert (result_hash, (gchar *)camel_message_info_uid (root->message), GINT_TO_POINTER (1));
916 add_thread_results (root->child, result_hash);
922 add_results (gchar *uid, gpointer dummy, GPtrArray *result)
924 g_ptr_array_add (result, uid);
928 search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
931 CamelFolderSearchPrivate *p = search->priv;
936 /* not supported in match-all */
937 if (search->current) {
938 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
939 e_sexp_fatal_error (f, error_msg);
944 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
945 e_sexp_fatal_error (f, error_msg);
949 r = e_sexp_term_eval (f, argv[0]);
950 if (r->type != ESEXP_RES_STRING) {
951 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
952 e_sexp_fatal_error (f, error_msg);
957 if (!strcmp(r->value.string, "none"))
959 else if (!strcmp(r->value.string, "all"))
961 else if (!strcmp(r->value.string, "replies"))
963 else if (!strcmp(r->value.string, "replies_parents"))
965 else if (!strcmp(r->value.string, "single"))
967 e_sexp_result_free (f, r);
969 /* behave as (begin does */
971 for (i=1;i<argc;i++) {
973 e_sexp_result_free (f, r);
974 r = e_sexp_term_eval (f, argv[i]);
977 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
978 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
979 e_sexp_fatal_error (f, error_msg);
986 if (search->folder == NULL) {
987 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
988 e_sexp_fatal_error (f, error_msg);
992 /* cache this, so we only have to re-calculate once per search at most */
993 if (p->threads == NULL) {
994 p->threads = camel_folder_thread_messages_new (search->folder, NULL, TRUE);
995 p->threads_hash = g_hash_table_new (g_str_hash, g_str_equal);
997 fill_thread_table (p->threads->tree, p->threads_hash);
1000 results = g_hash_table_new (g_str_hash, g_str_equal);
1001 for (i=0;i<r->value.ptrarray->len;i++) {
1002 struct _CamelFolderThreadNode *node, *scan;
1005 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
1007 node = g_hash_table_lookup (p->threads_hash, (gchar *)g_ptr_array_index (r->value.ptrarray, i));
1008 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
1011 /* select messages in thread according to search criteria */
1013 if (node->child == NULL && node->parent == NULL)
1014 g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1018 while (scan && scan->parent) {
1019 scan = scan->parent;
1020 g_hash_table_insert (results, (gchar *)camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
1022 } else if (type == 1) {
1023 while (node && node->parent)
1024 node = node->parent;
1026 g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1028 add_thread_results (node->child, results);
1031 e_sexp_result_free (f, r);
1033 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1034 r->value.ptrarray = g_ptr_array_new ();
1036 g_hash_table_foreach (results, (GHFunc)add_results, r->value.ptrarray);
1037 g_hash_table_destroy (results);
1042 static CamelMimeMessage *
1043 get_current_message (CamelFolderSearch *search)
1045 if (!search || !search->folder || !search->current)
1048 /* FIXME Pass a GCancellable */
1049 return camel_folder_get_message_sync (
1050 search->folder, search->current->uid, NULL, NULL);
1053 static ESExpResult *
1054 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1059 r(printf("executing check-header %d\n", how));
1061 /* are we inside a match-all? */
1062 if (search->current && argc>1
1063 && argv[0]->type == ESEXP_RES_STRING) {
1065 const gchar *header = NULL, *charset = NULL;
1068 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1069 struct _camel_search_words *words;
1070 CamelMimeMessage *message = NULL;
1071 struct _camel_header_raw *raw_header;
1073 /* only a subset of headers are supported .. */
1074 headername = argv[0]->value.string;
1075 if (!g_ascii_strcasecmp(headername, "subject")) {
1076 header = camel_message_info_subject (search->current);
1077 } else if (!g_ascii_strcasecmp(headername, "date")) {
1078 /* FIXME: not a very useful form of the date */
1079 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1081 } else if (!g_ascii_strcasecmp(headername, "from")) {
1082 header = camel_message_info_from (search->current);
1083 type = CAMEL_SEARCH_TYPE_ADDRESS;
1084 } else if (!g_ascii_strcasecmp(headername, "to")) {
1085 header = camel_message_info_to (search->current);
1086 type = CAMEL_SEARCH_TYPE_ADDRESS;
1087 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1088 header = camel_message_info_cc (search->current);
1089 type = CAMEL_SEARCH_TYPE_ADDRESS;
1090 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1091 header = camel_message_info_mlist (search->current);
1092 type = CAMEL_SEARCH_TYPE_MLIST;
1094 message = get_current_message (search);
1096 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1099 charset = camel_content_type_param (ct, "charset");
1100 charset = camel_iconv_charset_name (charset);
1108 /* performs an OR of all words */
1109 for (i=1;i<argc && !truth;i++) {
1110 if (argv[i]->type == ESEXP_RES_STRING) {
1111 if (argv[i]->value.string[0] == 0) {
1113 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1114 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1115 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1117 for (j=0;j<words->len && truth;j++) {
1119 for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1120 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1121 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1126 truth = raw_header != NULL;
1128 truth = camel_search_header_match (header, words->words[j]->word, how, type, charset);
1130 camel_search_words_free (words);
1133 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1134 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1135 truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
1139 truth = camel_search_header_match (header, argv[i]->value.string, how, type, charset);
1145 g_object_unref (message);
1147 /* TODO: else, find all matches */
1149 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1150 r->value.boolean = truth;
1157 l_printf (gchar *node)
1159 printf("%s\t", node);
1163 static ESExpResult *
1164 search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1166 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1169 static ESExpResult *
1170 search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1172 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1175 static ESExpResult *
1176 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1178 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1181 static ESExpResult *
1182 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1184 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1187 static ESExpResult *
1188 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1192 r(printf ("executing header-exists\n"));
1194 if (search->current) {
1195 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1196 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1197 r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
1200 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1201 r->value.ptrarray = g_ptr_array_new ();
1207 static ESExpResult *
1208 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1210 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1213 static ESExpResult *
1214 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1217 CamelMimeMessage *msg;
1219 msg = get_current_message (search);
1223 const gchar *contents;
1225 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1227 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1228 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1229 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->error) == 0) {
1230 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1233 r->value.boolean = FALSE;
1235 g_object_unref (msg);
1237 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1238 r->value.ptrarray = g_ptr_array_new ();
1245 get_full_header (CamelMimeMessage *message)
1247 CamelMimePart *mp = CAMEL_MIME_PART (message);
1248 GString *str = g_string_new ("");
1249 struct _camel_header_raw *h;
1251 for (h = mp->headers; h; h = h->next) {
1252 if (h->value != NULL) {
1253 g_string_append (str, h->name);
1254 if (isspace (h->value[0]))
1255 g_string_append (str, ":");
1257 g_string_append (str, ": ");
1258 g_string_append (str, h->value);
1259 g_string_append_c (str, '\n');
1263 return g_string_free (str, FALSE);
1266 static ESExpResult *
1267 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1270 CamelMimeMessage *msg;
1272 msg = get_current_message (search);
1277 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1279 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
1282 contents = get_full_header (msg);
1283 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1288 r->value.boolean = FALSE;
1290 g_object_unref (msg);
1292 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1293 r->value.ptrarray = g_ptr_array_new ();
1299 /* this is just to OR results together */
1305 /* or, store all unique values */
1307 htor (gchar *key, gint value, struct IterData *iter_data)
1309 g_ptr_array_add (iter_data->uids, key);
1312 /* and, only store duplicates */
1314 htand (gchar *key, gint value, struct IterData *iter_data)
1316 if (value == iter_data->count)
1317 g_ptr_array_add (iter_data->uids, key);
1321 match_message_index (CamelIndex *idx,
1326 CamelIndexCursor *wc, *nc;
1327 const gchar *word, *name;
1330 wc = camel_index_words (idx);
1332 while (!truth && (word = camel_index_cursor_next (wc))) {
1333 if (camel_ustrstrcase (word,match) != NULL) {
1334 /* perf: could have the wc cursor return the name cursor */
1335 nc = camel_index_find (idx, word);
1337 while (!truth && (name = camel_index_cursor_next (nc)))
1338 truth = strcmp (name, uid) == 0;
1339 g_object_unref (nc);
1343 g_object_unref (wc);
1350 "one two" "three" "four five"
1359 /* returns messages which contain all words listed in words */
1361 match_words_index (CamelFolderSearch *search,
1362 struct _camel_search_words *words,
1365 GPtrArray *result = g_ptr_array_new ();
1366 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1367 struct IterData lambdafoo;
1368 CamelIndexCursor *wc, *nc;
1369 const gchar *word, *name;
1372 /* we can have a maximum of 32 words, as we use it as the AND mask */
1374 wc = camel_index_words (search->body_index);
1376 while ((word = camel_index_cursor_next (wc))) {
1377 for (i=0;i<words->len;i++) {
1378 if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
1379 /* perf: could have the wc cursor return the name cursor */
1380 nc = camel_index_find (search->body_index, word);
1382 while ((name = camel_index_cursor_next (nc))) {
1385 mask = (GPOINTER_TO_INT (g_hash_table_lookup (ht, name))) | (1 << i);
1386 g_hash_table_insert (ht, (gchar *) camel_pstring_peek (name), GINT_TO_POINTER (mask));
1388 g_object_unref (nc);
1393 g_object_unref (wc);
1395 lambdafoo.uids = result;
1396 lambdafoo.count = (1 << words->len) - 1;
1397 g_hash_table_foreach (ht, (GHFunc)htand, &lambdafoo);
1398 g_hash_table_destroy (ht);
1405 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1407 CamelDataWrapper *containee;
1411 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1413 if (containee == NULL)
1416 /* using the object types is more accurate than using the mime/types */
1417 if (CAMEL_IS_MULTIPART (containee)) {
1418 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1419 for (i = 0; i < parts && truth == FALSE; i++) {
1420 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1422 truth = match_words_1message (part, words, mask);
1424 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1425 /* for messages we only look at its contents */
1426 truth = match_words_1message ((CamelDataWrapper *)containee, words, mask);
1427 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1428 /* for all other text parts, we look inside, otherwise we dont care */
1429 CamelStream *stream;
1430 GByteArray *byte_array;
1432 byte_array = g_byte_array_new ();
1433 stream = camel_stream_mem_new_with_byte_array (byte_array);
1435 /* FIXME The match should be part of a stream op */
1436 /* FIXME Pass a GCancellable and GError here. */
1437 camel_data_wrapper_decode_to_stream_sync (
1438 containee, stream, NULL, NULL);
1439 camel_stream_write (stream, "", 1, NULL, NULL);
1440 for (i=0;i<words->len;i++) {
1441 /* FIXME: This is horridly slow, and should use a real search algorithm */
1442 if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1444 /* shortcut a match */
1445 if (*mask == (1 << (words->len))-1)
1450 g_object_unref (stream);
1457 match_words_message (CamelFolder *folder,
1459 struct _camel_search_words *words,
1460 GCancellable *cancellable,
1464 CamelMimeMessage *msg;
1467 msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
1470 truth = match_words_1message ((CamelDataWrapper *)msg, words, &mask);
1471 g_object_unref (msg);
1478 match_words_messages (CamelFolderSearch *search,
1479 struct _camel_search_words *words,
1480 GCancellable *cancellable,
1484 GPtrArray *matches = g_ptr_array_new ();
1486 if (search->body_index) {
1488 struct _camel_search_words *simple;
1490 simple = camel_search_words_simple (words);
1491 indexed = match_words_index (search, simple, error);
1492 camel_search_words_free (simple);
1494 for (i=0;i<indexed->len;i++) {
1495 const gchar *uid = g_ptr_array_index (indexed, i);
1497 if (match_words_message (
1498 search->folder, uid, words,
1499 cancellable, error))
1500 g_ptr_array_add (matches, (gchar *)uid);
1503 g_ptr_array_free (indexed, TRUE);
1505 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1507 for (i=0;i<v->len;i++) {
1508 gchar *uid = g_ptr_array_index (v, i);
1510 if (match_words_message (
1511 search->folder, uid, words,
1512 cancellable, error))
1513 g_ptr_array_add (matches, (gchar *)uid);
1520 static ESExpResult *
1521 search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1524 GError **error = search->priv->error;
1525 struct _camel_search_words *words;
1527 struct IterData lambdafoo;
1529 if (search->current) {
1532 if (argc == 1 && argv[0]->value.string[0] == 0) {
1535 for (i=0;i<argc && !truth;i++) {
1536 if (argv[i]->type == ESEXP_RES_STRING) {
1537 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1539 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1540 for (j=0;j<words->len && truth;j++)
1541 truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
1543 /* TODO: cache current message incase of multiple body search terms */
1544 /* FIXME Pass a GCancellable */
1545 truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
1547 camel_search_words_free (words);
1551 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1552 r->value.boolean = truth;
1554 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1555 r->value.ptrarray = g_ptr_array_new ();
1557 if (argc == 1 && argv[0]->value.string[0] == 0) {
1558 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1560 for (i=0;i<v->len;i++) {
1561 gchar *uid = g_ptr_array_index (v, i);
1563 g_ptr_array_add (r->value.ptrarray, uid);
1566 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1569 for (i=0;i<argc;i++) {
1570 if (argv[i]->type == ESEXP_RES_STRING) {
1571 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1572 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1573 matches = match_words_index (search, words, error);
1575 /* FIXME Pass a GCancellable */
1576 matches = match_words_messages (search, words, NULL, error);
1578 for (j=0;j<matches->len;j++) {
1579 g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
1581 g_ptr_array_free (matches, TRUE);
1582 camel_search_words_free (words);
1585 lambdafoo.uids = r->value.ptrarray;
1586 g_hash_table_foreach (ht, (GHFunc)htor, &lambdafoo);
1587 g_hash_table_destroy (ht);
1594 static ESExpResult *
1595 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1598 CamelMimeMessage *msg = get_current_message (search);
1603 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1605 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
1606 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1609 r->value.boolean = FALSE;
1611 g_object_unref (msg);
1615 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1616 r->value.ptrarray = g_ptr_array_new ();
1618 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
1620 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1621 CamelMimeMessage *message;
1623 for (i = 0; i < v->len; i++) {
1624 gchar *uid = g_ptr_array_index (v, i);
1626 /* FIXME Pass a GCancellable */
1627 message = camel_folder_get_message_sync (
1628 search->folder, uid, NULL, NULL);
1630 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1631 g_ptr_array_add (r->value.ptrarray, uid);
1634 g_object_unref (message);
1645 static ESExpResult *
1646 search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1651 r(printf("executing user-flag\n"));
1653 /* are we inside a match-all? */
1654 if (search->current) {
1656 /* performs an OR of all words */
1657 for (i=0;i<argc && !truth;i++) {
1658 if (argv[i]->type == ESEXP_RES_STRING
1659 && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
1664 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1665 r->value.boolean = truth;
1667 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1668 r->value.ptrarray = g_ptr_array_new ();
1674 static ESExpResult *
1675 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1679 r(printf ("executing system-flag\n"));
1681 if (search->current) {
1682 gboolean truth = FALSE;
1685 truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
1687 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1688 r->value.boolean = truth;
1690 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1691 r->value.ptrarray = g_ptr_array_new ();
1697 static ESExpResult *
1698 search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1700 const gchar *value = NULL;
1703 r(printf("executing user-tag\n"));
1705 if (search->current && argc == 1)
1706 value = camel_message_info_user_tag (search->current, argv[0]->value.string);
1708 r = e_sexp_result_new (f, ESEXP_RES_STRING);
1709 r->value.string = g_strdup (value ? value : "");
1714 static ESExpResult *
1715 search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1719 r(printf("executing get-sent-date\n"));
1721 /* are we inside a match-all? */
1723 r = e_sexp_result_new (f, ESEXP_RES_INT);
1725 r->value.number = camel_message_info_date_sent (s->current);
1727 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1728 r->value.ptrarray = g_ptr_array_new ();
1734 static ESExpResult *
1735 search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1739 r(printf("executing get-received-date\n"));
1741 /* are we inside a match-all? */
1743 r = e_sexp_result_new (f, ESEXP_RES_INT);
1745 r->value.number = camel_message_info_date_received (s->current);
1747 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1748 r->value.ptrarray = g_ptr_array_new ();
1754 static ESExpResult *
1755 search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1759 r(printf("executing get-current-date\n"));
1761 r = e_sexp_result_new (f, ESEXP_RES_INT);
1762 r->value.number = time (NULL);
1766 static ESExpResult *
1767 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1771 r(printf("executing get-size\n"));
1773 /* are we inside a match-all? */
1775 r = e_sexp_result_new (f, ESEXP_RES_INT);
1776 r->value.number = camel_message_info_size (s->current) / 1024;
1778 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1779 r->value.ptrarray = g_ptr_array_new ();
1785 static ESExpResult *
1786 search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1791 r(printf("executing uid\n"));
1793 /* are we inside a match-all? */
1794 if (search->current) {
1796 const gchar *uid = camel_message_info_uid (search->current);
1798 /* performs an OR of all words */
1799 for (i=0;i<argc && !truth;i++) {
1800 if (argv[i]->type == ESEXP_RES_STRING
1801 && !strcmp (uid, argv[i]->value.string)) {
1806 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1807 r->value.boolean = truth;
1809 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1810 r->value.ptrarray = g_ptr_array_new ();
1811 for (i=0;i<argc;i++) {
1812 if (argv[i]->type == ESEXP_RES_STRING)
1813 g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
1821 read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name)
1825 matches = (GPtrArray *) ref;
1827 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols[0]));
1831 static ESExpResult *
1832 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1834 CamelStore *parent_store;
1836 gboolean same = FALSE;
1838 parent_store = camel_folder_get_parent_store (search->folder);
1840 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1841 if (argv[0]->value.string && search->folder && parent_store && camel_folder_get_full_name (search->folder)) {
1842 /* FIXME Pass a GCancellable */
1843 CamelFolderInfo *fi = camel_store_get_folder_info_sync (parent_store, camel_folder_get_full_name (search->folder), 0, NULL, NULL);
1845 same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
1847 camel_store_free_folder_info (parent_store, fi);
1852 if (search->current) {
1853 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1854 r->value.boolean = same ? TRUE : FALSE;
1856 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1857 r->value.ptrarray = g_ptr_array_new ();
1862 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1864 for (i = 0; i < v->len; i++) {
1865 gchar *uid = g_ptr_array_index (v, i);
1867 g_ptr_array_add (r->value.ptrarray, uid);