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 struct _CamelFolderSearchPrivate {
63 CamelFolderThread *threads;
64 GHashTable *threads_hash;
67 static ESExpResult *search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
69 static ESExpResult *search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
70 static ESExpResult *search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
78 static ESExpResult *search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
79 static ESExpResult *search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
81 static ESExpResult *search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
82 static ESExpResult *search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
83 static ESExpResult *search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
84 static ESExpResult *search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
85 static ESExpResult *search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
93 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
95 G_DEFINE_TYPE (CamelFolderSearch, camel_folder_search, CAMEL_TYPE_OBJECT)
98 folder_search_dispose (GObject *object)
100 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
102 if (search->sexp != NULL) {
103 e_sexp_unref (search->sexp);
107 /* Chain up to parent's dispose() method. */
108 G_OBJECT_CLASS (camel_folder_search_parent_class)->dispose (object);
112 folder_search_finalize (GObject *object)
114 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
116 g_free (search->last_search);
118 /* Chain up to parent's finalize() method. */
119 G_OBJECT_CLASS (camel_folder_search_parent_class)->finalize (object);
123 camel_folder_search_class_init (CamelFolderSearchClass *class)
125 GObjectClass *object_class;
127 g_type_class_add_private (class, sizeof (CamelFolderSearchPrivate));
129 object_class = G_OBJECT_CLASS (class);
130 object_class->dispose = folder_search_dispose;
131 object_class->finalize = folder_search_finalize;
133 class->not = search_not;
134 class->match_all = search_match_all;
135 class->match_threads = search_match_threads;
136 class->body_contains = search_body_contains;
137 class->body_regex = search_body_regex;
138 class->header_contains = search_header_contains;
139 class->header_matches = search_header_matches;
140 class->header_starts_with = search_header_starts_with;
141 class->header_ends_with = search_header_ends_with;
142 class->header_exists = search_header_exists;
143 class->header_soundex = search_header_soundex;
144 class->header_regex = search_header_regex;
145 class->header_full_regex = search_header_full_regex;
146 class->user_tag = search_user_tag;
147 class->user_flag = search_user_flag;
148 class->system_flag = search_system_flag;
149 class->get_sent_date = search_get_sent_date;
150 class->get_received_date = search_get_received_date;
151 class->get_current_date = search_get_current_date;
152 class->get_size = search_get_size;
153 class->uid = search_uid;
154 class->message_location = search_message_location;
158 camel_folder_search_init (CamelFolderSearch *search)
160 search->priv = G_TYPE_INSTANCE_GET_PRIVATE (
161 search, CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate);
162 search->sexp = e_sexp_new ();
168 gint flags; /* 0x02 = immediate, 0x01 = always enter */
170 /* these have default implementations in e-sexp */
171 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
172 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
173 /* we need to override this one though to implement an 'array not' */
174 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
175 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
176 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
177 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
179 /* these we have to use our own default if there is none */
180 /* they should all be defined in the language? so it parses, or should they not?? */
181 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
182 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
183 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
184 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
185 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
186 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
187 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
188 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
189 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
190 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
191 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
192 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
193 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
194 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
195 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
196 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
197 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
198 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
199 { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
200 { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
201 { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
205 camel_folder_search_construct (CamelFolderSearch *search)
208 CamelFolderSearchClass *class;
210 class = CAMEL_FOLDER_SEARCH_GET_CLASS (search);
212 for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
214 /* c is sure messy sometimes */
215 func = *((gpointer *)(((gchar *)class)+builtins[i].offset));
216 if (func == NULL && builtins[i].flags&1) {
217 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, G_OBJECT_TYPE_NAME (search));
218 func = (gpointer)search_dummy;
221 if (builtins[i].flags&2) {
222 e_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
224 e_sexp_add_function (search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
231 * camel_folder_search_new:
233 * Create a new CamelFolderSearch object.
235 * A CamelFolderSearch is a subclassable, extensible s-exp
236 * evaluator which enforces a particular set of s-expressions.
237 * Particular methods may be overriden by an implementation to
238 * implement a search for any sort of backend.
240 * Returns: A new CamelFolderSearch widget.
243 camel_folder_search_new (void)
245 CamelFolderSearch *new;
247 new = g_object_new (CAMEL_TYPE_FOLDER_SEARCH, NULL);
248 camel_folder_search_construct (new);
254 * camel_folder_search_set_folder:
258 * Set the folder attribute of the search. This is currently unused, but
259 * could be used to perform a slow-search when indexes and so forth are not
260 * available. Or for use by subclasses.
263 camel_folder_search_set_folder (CamelFolderSearch *search, CamelFolder *folder)
265 search->folder = folder;
269 * camel_folder_search_set_summary:
271 * @summary: An array of CamelMessageInfo pointers.
273 * Set the array of summary objects representing the span of the search.
275 * If this is not set, then a subclass must provide the functions
276 * for searching headers and for the match-all operator.
279 camel_folder_search_set_summary (CamelFolderSearch *search, GPtrArray *summary)
281 search->summary = summary;
285 * camel_folder_search_set_body_index:
289 * Set the index representing the contents of all messages
290 * in this folder. If this is not set, then the folder implementation
291 * should sub-class the CamelFolderSearch and provide its own
292 * body-contains function.
295 camel_folder_search_set_body_index (CamelFolderSearch *search, CamelIndex *index)
297 if (search->body_index)
298 g_object_unref (search->body_index);
299 search->body_index = index;
301 g_object_ref (index);
305 * camel_folder_search_execute_expression:
308 * @error: return location for a #GError, or %NULL
310 * Execute the search expression @expr, returning an array of
311 * all matches as a GPtrArray of uid's of matching messages.
313 * Note that any settings such as set_body_index(), set_folder(),
314 * and so on are reset to #NULL once the search has completed.
316 * TODO: The interface should probably return summary items instead
317 * (since they are much more useful to any client).
319 * Returns: A GPtrArray of strings of all matching messages.
320 * This must only be freed by camel_folder_search_free_result.
323 camel_folder_search_execute_expression (CamelFolderSearch *search,
331 CamelFolderSearchPrivate *p = search->priv;
335 /* only re-parse if the search has changed */
336 if (search->last_search == NULL
337 || strcmp (search->last_search, expr)) {
338 e_sexp_input_text (search->sexp, expr, strlen (expr));
339 if (e_sexp_parse (search->sexp) == -1) {
341 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
342 _("Cannot parse search expression: %s:\n%s"),
343 e_sexp_error (search->sexp), expr);
347 g_free (search->last_search);
348 search->last_search = g_strdup (expr);
350 r = e_sexp_eval (search->sexp);
353 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
354 _("Error executing search expression: %s:\n%s"),
355 e_sexp_error (search->sexp), expr);
359 matches = g_ptr_array_new ();
361 /* now create a folder summary to return?? */
362 if (r->type == ESEXP_RES_ARRAY_PTR) {
363 d(printf("got result ...\n"));
364 if (search->summary) {
365 /* reorder result in summary order */
366 results = g_hash_table_new (g_str_hash, g_str_equal);
367 for (i=0;i<r->value.ptrarray->len;i++) {
368 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
369 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
371 for (i=0;i<search->summary->len;i++) {
372 gchar *uid = g_ptr_array_index (search->summary, i);
373 if (g_hash_table_lookup (results, uid)) {
374 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
377 g_hash_table_destroy (results);
379 for (i=0;i<r->value.ptrarray->len;i++) {
380 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
381 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (g_ptr_array_index (r->value.ptrarray, i)));
385 g_warning("Search returned an invalid result type");
388 e_sexp_result_free (search->sexp, r);
391 camel_folder_thread_messages_unref (p->threads);
393 g_hash_table_destroy (p->threads_hash);
396 p->threads_hash = NULL;
397 search->folder = NULL;
398 search->summary = NULL;
399 search->current = NULL;
400 search->body_index = NULL;
406 * camel_folder_search_count:
409 * @uids: to search against, NULL for all uid's.
410 * @error: return location for a #GError, or %NULL
412 * Run a search. Search must have had Folder already set on it, and
413 * it must implement summaries.
415 * Returns: Number of messages that match the query.
421 camel_folder_search_count (CamelFolderSearch *search,
426 GPtrArray *summary_set;
429 gchar *sql_query, *tmp, *tmp1;
433 CamelFolderSearchPrivate *p = search->priv;
435 g_assert (search->folder);
439 /* We route body-contains search and thread based search through memory and not via db. */
440 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
441 /* setup our search list only contains those we're interested in */
442 search->summary = camel_folder_get_summary (search->folder);
444 summary_set = search->summary;
446 /* only re-parse if the search has changed */
447 if (search->last_search == NULL
448 || strcmp (search->last_search, expr)) {
449 e_sexp_input_text (search->sexp, expr, strlen (expr));
450 if (e_sexp_parse (search->sexp) == -1) {
452 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
453 _("Cannot parse search expression: %s:\n%s"),
454 e_sexp_error (search->sexp), expr);
458 g_free (search->last_search);
459 search->last_search = g_strdup (expr);
461 r = e_sexp_eval (search->sexp);
464 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
465 _("Error executing search expression: %s:\n%s"),
466 e_sexp_error (search->sexp), expr);
470 /* now create a folder summary to return?? */
471 if (r->type == ESEXP_RES_ARRAY_PTR) {
472 d(printf("got result\n"));
474 /* reorder result in summary order */
475 results = g_hash_table_new (g_str_hash, g_str_equal);
476 for (i=0;i<r->value.ptrarray->len;i++) {
477 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
478 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
481 for (i=0;i<summary_set->len;i++) {
482 gchar *uid = g_ptr_array_index (summary_set, i);
483 if (g_hash_table_lookup (results, uid))
486 g_hash_table_destroy (results);
489 e_sexp_result_free (search->sexp, r);
492 CamelStore *parent_store;
493 const gchar *full_name;
494 GError *local_error = NULL;
496 full_name = camel_folder_get_full_name (search->folder);
497 parent_store = camel_folder_get_parent_store (search->folder);
499 /* Sync the db, so that we search the db for changes */
500 camel_folder_summary_save_to_db (search->folder->summary, error);
502 dd(printf ("sexp is : [%s]\n", expr));
503 if (g_getenv("SQL_SEARCH_OLD"))
504 sql_query = camel_sexp_to_sql (expr);
506 sql_query = camel_sexp_to_sql_sexp (expr);
507 tmp1 = camel_db_sqlize_string (full_name);
508 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
509 camel_db_free_sqlized_string (tmp1);
511 dd(printf("Equivalent sql %s\n", tmp));
513 cdb = (CamelDB *) (parent_store->cdb_r);
514 camel_db_count_message_info (cdb, tmp, &count, &local_error);
515 if (local_error != NULL) {
516 const gchar *message = local_error->message;
517 if (strncmp(message, "no such table", 13) == 0) {
518 d(g_warning ("Error during searching %s: %s\n", tmp, message));
519 /* Suppress no such table */
520 g_clear_error (&local_error);
522 g_propagate_error (error, local_error);
528 /* these might be allocated by match-threads */
530 camel_folder_thread_messages_unref (p->threads);
532 g_hash_table_destroy (p->threads_hash);
533 if (search->summary_set)
534 g_ptr_array_free (search->summary_set, TRUE);
536 camel_folder_free_summary (search->folder, search->summary);
539 p->threads_hash = NULL;
540 search->folder = NULL;
541 search->summary = NULL;
542 search->summary_set = NULL;
543 search->current = NULL;
544 search->body_index = NULL;
550 do_search_in_memory (const gchar *expr)
552 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
553 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
559 for (i = 0; in_memory_tokens[i]; i++) {
560 if (strstr (expr, in_memory_tokens[i]))
568 * camel_folder_search_search:
571 * @uids: to search against, NULL for all uid's.
572 * @error: return location for a #GError, or %NULL
574 * Run a search. Search must have had Folder already set on it, and
575 * it must implement summaries.
580 camel_folder_search_search (CamelFolderSearch *search,
586 GPtrArray *matches = NULL, *summary_set;
589 gchar *sql_query, *tmp, *tmp1;
592 CamelFolderSearchPrivate *p = search->priv;
594 g_assert (search->folder);
598 /* We route body-contains / thread based search and uid search through memory and not via db. */
599 if (uids || do_search_in_memory (expr)) {
600 /* setup our search list only contains those we're interested in */
601 search->summary = camel_folder_get_summary (search->folder);
604 GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
606 summary_set = search->summary_set = g_ptr_array_new ();
607 for (i=0;i<uids->len;i++)
608 g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
609 for (i=0;i<search->summary->len;i++)
610 if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
611 g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
612 g_hash_table_destroy (uids_hash);
614 summary_set = search->summary;
617 /* only re-parse if the search has changed */
618 if (search->last_search == NULL
619 || strcmp (search->last_search, expr)) {
620 e_sexp_input_text (search->sexp, expr, strlen (expr));
621 if (e_sexp_parse (search->sexp) == -1) {
623 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
624 _("Cannot parse search expression: %s:\n%s"),
625 e_sexp_error (search->sexp), expr);
629 g_free (search->last_search);
630 search->last_search = g_strdup (expr);
632 r = e_sexp_eval (search->sexp);
635 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
636 _("Error executing search expression: %s:\n%s"),
637 e_sexp_error (search->sexp), expr);
641 matches = g_ptr_array_new ();
643 /* now create a folder summary to return?? */
644 if (r->type == ESEXP_RES_ARRAY_PTR) {
645 d(printf("got result\n"));
647 /* reorder result in summary order */
648 results = g_hash_table_new (g_str_hash, g_str_equal);
649 for (i=0;i<r->value.ptrarray->len;i++) {
650 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
651 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
654 for (i=0;i<summary_set->len;i++) {
655 gchar *uid = g_ptr_array_index (summary_set, i);
656 if (g_hash_table_lookup (results, uid))
657 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
659 g_hash_table_destroy (results);
662 e_sexp_result_free (search->sexp, r);
665 CamelStore *parent_store;
666 const gchar *full_name;
667 GError *local_error = NULL;
669 full_name = camel_folder_get_full_name (search->folder);
670 parent_store = camel_folder_get_parent_store (search->folder);
672 /* Sync the db, so that we search the db for changes */
673 camel_folder_summary_save_to_db (search->folder->summary, error);
675 dd(printf ("sexp is : [%s]\n", expr));
676 if (g_getenv("SQL_SEARCH_OLD"))
677 sql_query = camel_sexp_to_sql (expr);
679 sql_query = camel_sexp_to_sql_sexp (expr);
680 tmp1 = camel_db_sqlize_string (full_name);
681 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
682 camel_db_free_sqlized_string (tmp1);
684 dd(printf("Equivalent sql %s\n", tmp));
686 matches = g_ptr_array_new ();
687 cdb = (CamelDB *) (parent_store->cdb_r);
689 cdb, tmp, (CamelDBSelectCB)
690 read_uid_callback, matches, &local_error);
691 if (local_error != NULL) {
692 const gchar *message = local_error->message;
693 if (strncmp(message, "no such table", 13) == 0) {
694 d(g_warning ("Error during searching %s: %s\n", tmp, message));
695 /* Suppress no such table */
696 g_clear_error (&local_error);
698 g_propagate_error (error, local_error);
705 /* these might be allocated by match-threads */
707 camel_folder_thread_messages_unref (p->threads);
709 g_hash_table_destroy (p->threads_hash);
710 if (search->summary_set)
711 g_ptr_array_free (search->summary_set, TRUE);
713 camel_folder_free_summary (search->folder, search->summary);
716 p->threads_hash = NULL;
717 search->folder = NULL;
718 search->summary = NULL;
719 search->summary_set = NULL;
720 search->current = NULL;
721 search->body_index = NULL;
726 void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *result)
728 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
729 g_ptr_array_free (result, TRUE);
732 /* dummy function, returns false always, or an empty match array */
734 search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
738 if (search->current == NULL) {
739 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
740 r->value.boolean = FALSE;
742 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
743 r->value.ptrarray = g_ptr_array_new ();
749 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
751 search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
757 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
758 GPtrArray *v = argv[0]->value.ptrarray;
761 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
762 r->value.ptrarray = g_ptr_array_new ();
764 /* not against a single message?*/
765 if (search->current) {
768 uid = camel_message_info_uid (search->current);
769 for (i=0;!found && i<v->len;i++) {
770 if (strcmp (uid, v->pdata[i]) == 0)
775 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
776 } else if (search->summary == NULL) {
777 g_warning("No summary set, 'not' against an array requires a summary");
779 /* 'not' against the whole summary */
780 GHashTable *have = g_hash_table_new (g_str_hash, g_str_equal);
784 s = (gchar **)v->pdata;
785 for (i=0;i<v->len;i++)
786 g_hash_table_insert (have, s[i], s[i]);
788 v = search->summary_set?search->summary_set:search->summary;
789 m = (gchar **)v->pdata;
790 for (i=0;i<v->len;i++) {
793 if (g_hash_table_lookup (have, uid) == NULL)
794 g_ptr_array_add (r->value.ptrarray, uid);
796 g_hash_table_destroy (have);
801 if (argv[0]->type == ESEXP_RES_BOOL)
802 res = !argv[0]->value.boolean;
804 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
805 r->value.boolean = res;
808 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
809 r->value.boolean = TRUE;
816 search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
824 g_warning("match-all only takes a single argument, other arguments ignored");
827 /* we are only matching a single message? or already inside a match-all? */
828 if (search->current) {
829 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
831 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
832 r->value.boolean = FALSE;
835 r1 = e_sexp_term_eval (f, argv[0]);
836 if (r1->type == ESEXP_RES_BOOL) {
837 r->value.boolean = r1->value.boolean;
839 g_warning("invalid syntax, matches require a single bool result");
840 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
841 e_sexp_fatal_error (f, error_msg);
844 e_sexp_result_free (f, r1);
846 r->value.boolean = TRUE;
851 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
852 r->value.ptrarray = g_ptr_array_new ();
854 if (search->summary == NULL) {
855 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
856 g_warning("No summary supplied, match-all doesn't work with no summary");
861 v = search->summary_set?search->summary_set:search->summary;
863 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
864 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
867 for (i=0;i<v->len;i++) {
870 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
871 if (!search->current)
873 uid = camel_message_info_uid (search->current);
876 r1 = e_sexp_term_eval (f, argv[0]);
877 if (r1->type == ESEXP_RES_BOOL) {
878 if (r1->value.boolean)
879 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
881 g_warning("invalid syntax, matches require a single bool result");
882 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
883 e_sexp_fatal_error (f, error_msg);
886 e_sexp_result_free (f, r1);
888 g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
890 camel_message_info_free (search->current);
892 search->current = NULL;
897 fill_thread_table (struct _CamelFolderThreadNode *root, GHashTable *id_hash)
900 g_hash_table_insert (id_hash, (gchar *)camel_message_info_uid (root->message), root);
902 fill_thread_table (root->child, id_hash);
908 add_thread_results (struct _CamelFolderThreadNode *root, GHashTable *result_hash)
911 g_hash_table_insert (result_hash, (gchar *)camel_message_info_uid (root->message), GINT_TO_POINTER (1));
913 add_thread_results (root->child, result_hash);
919 add_results (gchar *uid, gpointer dummy, GPtrArray *result)
921 g_ptr_array_add (result, uid);
925 search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
928 CamelFolderSearchPrivate *p = search->priv;
933 /* not supported in match-all */
934 if (search->current) {
935 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
936 e_sexp_fatal_error (f, error_msg);
941 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
942 e_sexp_fatal_error (f, error_msg);
946 r = e_sexp_term_eval (f, argv[0]);
947 if (r->type != ESEXP_RES_STRING) {
948 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
949 e_sexp_fatal_error (f, error_msg);
954 if (!strcmp(r->value.string, "none"))
956 else if (!strcmp(r->value.string, "all"))
958 else if (!strcmp(r->value.string, "replies"))
960 else if (!strcmp(r->value.string, "replies_parents"))
962 else if (!strcmp(r->value.string, "single"))
964 e_sexp_result_free (f, r);
966 /* behave as (begin does */
968 for (i=1;i<argc;i++) {
970 e_sexp_result_free (f, r);
971 r = e_sexp_term_eval (f, argv[i]);
974 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
975 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
976 e_sexp_fatal_error (f, error_msg);
983 if (search->folder == NULL) {
984 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
985 e_sexp_fatal_error (f, error_msg);
989 /* cache this, so we only have to re-calculate once per search at most */
990 if (p->threads == NULL) {
991 p->threads = camel_folder_thread_messages_new (search->folder, NULL, TRUE);
992 p->threads_hash = g_hash_table_new (g_str_hash, g_str_equal);
994 fill_thread_table (p->threads->tree, p->threads_hash);
997 results = g_hash_table_new (g_str_hash, g_str_equal);
998 for (i=0;i<r->value.ptrarray->len;i++) {
999 struct _CamelFolderThreadNode *node, *scan;
1002 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
1004 node = g_hash_table_lookup (p->threads_hash, (gchar *)g_ptr_array_index (r->value.ptrarray, i));
1005 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
1008 /* select messages in thread according to search criteria */
1010 if (node->child == NULL && node->parent == NULL)
1011 g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1015 while (scan && scan->parent) {
1016 scan = scan->parent;
1017 g_hash_table_insert (results, (gchar *)camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
1019 } else if (type == 1) {
1020 while (node && node->parent)
1021 node = node->parent;
1023 g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1025 add_thread_results (node->child, results);
1028 e_sexp_result_free (f, r);
1030 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1031 r->value.ptrarray = g_ptr_array_new ();
1033 g_hash_table_foreach (results, (GHFunc)add_results, r->value.ptrarray);
1034 g_hash_table_destroy (results);
1039 static CamelMimeMessage *
1040 get_current_message (CamelFolderSearch *search)
1042 if (!search || !search->folder || !search->current)
1045 /* FIXME Pass a GCancellable */
1046 return camel_folder_get_message_sync (
1047 search->folder, search->current->uid, NULL, NULL);
1050 static ESExpResult *
1051 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1056 r(printf("executing check-header %d\n", how));
1058 /* are we inside a match-all? */
1059 if (search->current && argc>1
1060 && argv[0]->type == ESEXP_RES_STRING) {
1062 const gchar *header = NULL, *charset = NULL;
1065 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1066 struct _camel_search_words *words;
1067 CamelMimeMessage *message = NULL;
1068 struct _camel_header_raw *raw_header;
1070 /* only a subset of headers are supported .. */
1071 headername = argv[0]->value.string;
1072 if (!g_ascii_strcasecmp(headername, "subject")) {
1073 header = camel_message_info_subject (search->current);
1074 } else if (!g_ascii_strcasecmp(headername, "date")) {
1075 /* FIXME: not a very useful form of the date */
1076 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1078 } else if (!g_ascii_strcasecmp(headername, "from")) {
1079 header = camel_message_info_from (search->current);
1080 type = CAMEL_SEARCH_TYPE_ADDRESS;
1081 } else if (!g_ascii_strcasecmp(headername, "to")) {
1082 header = camel_message_info_to (search->current);
1083 type = CAMEL_SEARCH_TYPE_ADDRESS;
1084 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1085 header = camel_message_info_cc (search->current);
1086 type = CAMEL_SEARCH_TYPE_ADDRESS;
1087 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1088 header = camel_message_info_mlist (search->current);
1089 type = CAMEL_SEARCH_TYPE_MLIST;
1091 message = get_current_message (search);
1093 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1096 charset = camel_content_type_param (ct, "charset");
1097 charset = camel_iconv_charset_name (charset);
1105 /* performs an OR of all words */
1106 for (i=1;i<argc && !truth;i++) {
1107 if (argv[i]->type == ESEXP_RES_STRING) {
1108 if (argv[i]->value.string[0] == 0) {
1110 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1111 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1112 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1114 for (j=0;j<words->len && truth;j++) {
1116 for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1117 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1118 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1123 truth = raw_header != NULL;
1125 truth = camel_search_header_match (header, words->words[j]->word, how, type, charset);
1127 camel_search_words_free (words);
1130 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1131 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1132 truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
1136 truth = camel_search_header_match (header, argv[i]->value.string, how, type, charset);
1142 g_object_unref (message);
1144 /* TODO: else, find all matches */
1146 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1147 r->value.boolean = truth;
1154 l_printf (gchar *node)
1156 printf("%s\t", node);
1160 static ESExpResult *
1161 search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1163 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1166 static ESExpResult *
1167 search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1169 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1172 static ESExpResult *
1173 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1175 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1178 static ESExpResult *
1179 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1181 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1184 static ESExpResult *
1185 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1189 r(printf ("executing header-exists\n"));
1191 if (search->current) {
1192 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1193 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1194 r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
1197 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1198 r->value.ptrarray = g_ptr_array_new ();
1204 static ESExpResult *
1205 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1207 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1210 static ESExpResult *
1211 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1214 CamelMimeMessage *msg;
1216 msg = get_current_message (search);
1220 const gchar *contents;
1222 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1224 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1225 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1226 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->error) == 0) {
1227 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1230 r->value.boolean = FALSE;
1232 g_object_unref (msg);
1234 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1235 r->value.ptrarray = g_ptr_array_new ();
1242 get_full_header (CamelMimeMessage *message)
1244 CamelMimePart *mp = CAMEL_MIME_PART (message);
1245 GString *str = g_string_new ("");
1246 struct _camel_header_raw *h;
1248 for (h = mp->headers; h; h = h->next) {
1249 if (h->value != NULL) {
1250 g_string_append (str, h->name);
1251 if (isspace (h->value[0]))
1252 g_string_append (str, ":");
1254 g_string_append (str, ": ");
1255 g_string_append (str, h->value);
1256 g_string_append_c (str, '\n');
1260 return g_string_free (str, FALSE);
1263 static ESExpResult *
1264 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1267 CamelMimeMessage *msg;
1269 msg = get_current_message (search);
1274 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1276 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) {
1279 contents = get_full_header (msg);
1280 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1285 r->value.boolean = FALSE;
1287 g_object_unref (msg);
1289 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1290 r->value.ptrarray = g_ptr_array_new ();
1296 /* this is just to OR results together */
1302 /* or, store all unique values */
1304 htor (gchar *key, gint value, struct IterData *iter_data)
1306 g_ptr_array_add (iter_data->uids, key);
1309 /* and, only store duplicates */
1311 htand (gchar *key, gint value, struct IterData *iter_data)
1313 if (value == iter_data->count)
1314 g_ptr_array_add (iter_data->uids, key);
1318 match_message_index (CamelIndex *idx,
1323 CamelIndexCursor *wc, *nc;
1324 const gchar *word, *name;
1327 wc = camel_index_words (idx);
1329 while (!truth && (word = camel_index_cursor_next (wc))) {
1330 if (camel_ustrstrcase (word,match) != NULL) {
1331 /* perf: could have the wc cursor return the name cursor */
1332 nc = camel_index_find (idx, word);
1334 while (!truth && (name = camel_index_cursor_next (nc)))
1335 truth = strcmp (name, uid) == 0;
1336 g_object_unref (nc);
1340 g_object_unref (wc);
1347 "one two" "three" "four five"
1356 /* returns messages which contain all words listed in words */
1358 match_words_index (CamelFolderSearch *search,
1359 struct _camel_search_words *words,
1362 GPtrArray *result = g_ptr_array_new ();
1363 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1364 struct IterData lambdafoo;
1365 CamelIndexCursor *wc, *nc;
1366 const gchar *word, *name;
1369 /* we can have a maximum of 32 words, as we use it as the AND mask */
1371 wc = camel_index_words (search->body_index);
1373 while ((word = camel_index_cursor_next (wc))) {
1374 for (i=0;i<words->len;i++) {
1375 if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
1376 /* perf: could have the wc cursor return the name cursor */
1377 nc = camel_index_find (search->body_index, word);
1379 while ((name = camel_index_cursor_next (nc))) {
1382 mask = (GPOINTER_TO_INT (g_hash_table_lookup (ht, name))) | (1 << i);
1383 g_hash_table_insert (ht, (gchar *) camel_pstring_peek (name), GINT_TO_POINTER (mask));
1385 g_object_unref (nc);
1390 g_object_unref (wc);
1392 lambdafoo.uids = result;
1393 lambdafoo.count = (1 << words->len) - 1;
1394 g_hash_table_foreach (ht, (GHFunc)htand, &lambdafoo);
1395 g_hash_table_destroy (ht);
1402 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1404 CamelDataWrapper *containee;
1408 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1410 if (containee == NULL)
1413 /* using the object types is more accurate than using the mime/types */
1414 if (CAMEL_IS_MULTIPART (containee)) {
1415 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1416 for (i = 0; i < parts && truth == FALSE; i++) {
1417 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1419 truth = match_words_1message (part, words, mask);
1421 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1422 /* for messages we only look at its contents */
1423 truth = match_words_1message ((CamelDataWrapper *)containee, words, mask);
1424 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1425 /* for all other text parts, we look inside, otherwise we dont care */
1426 CamelStream *stream;
1427 GByteArray *byte_array;
1429 byte_array = g_byte_array_new ();
1430 stream = camel_stream_mem_new_with_byte_array (byte_array);
1432 /* FIXME The match should be part of a stream op */
1433 /* FIXME Pass a GCancellable and GError here. */
1434 camel_data_wrapper_decode_to_stream_sync (
1435 containee, stream, NULL, NULL);
1436 camel_stream_write (stream, "", 1, NULL, NULL);
1437 for (i=0;i<words->len;i++) {
1438 /* FIXME: This is horridly slow, and should use a real search algorithm */
1439 if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1441 /* shortcut a match */
1442 if (*mask == (1 << (words->len))-1)
1447 g_object_unref (stream);
1454 match_words_message (CamelFolder *folder,
1456 struct _camel_search_words *words,
1457 GCancellable *cancellable,
1461 CamelMimeMessage *msg;
1464 msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
1467 truth = match_words_1message ((CamelDataWrapper *)msg, words, &mask);
1468 g_object_unref (msg);
1475 match_words_messages (CamelFolderSearch *search,
1476 struct _camel_search_words *words,
1477 GCancellable *cancellable,
1481 GPtrArray *matches = g_ptr_array_new ();
1483 if (search->body_index) {
1485 struct _camel_search_words *simple;
1487 simple = camel_search_words_simple (words);
1488 indexed = match_words_index (search, simple, error);
1489 camel_search_words_free (simple);
1491 for (i=0;i<indexed->len;i++) {
1492 const gchar *uid = g_ptr_array_index (indexed, i);
1494 if (match_words_message (
1495 search->folder, uid, words,
1496 cancellable, error))
1497 g_ptr_array_add (matches, (gchar *)uid);
1500 g_ptr_array_free (indexed, TRUE);
1502 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1504 for (i=0;i<v->len;i++) {
1505 gchar *uid = g_ptr_array_index (v, i);
1507 if (match_words_message (
1508 search->folder, uid, words,
1509 cancellable, error))
1510 g_ptr_array_add (matches, (gchar *)uid);
1517 static ESExpResult *
1518 search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1521 GError **error = search->priv->error;
1522 struct _camel_search_words *words;
1524 struct IterData lambdafoo;
1526 if (search->current) {
1529 if (argc == 1 && argv[0]->value.string[0] == 0) {
1532 for (i=0;i<argc && !truth;i++) {
1533 if (argv[i]->type == ESEXP_RES_STRING) {
1534 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1536 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1537 for (j=0;j<words->len && truth;j++)
1538 truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
1540 /* TODO: cache current message incase of multiple body search terms */
1541 /* FIXME Pass a GCancellable */
1542 truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
1544 camel_search_words_free (words);
1548 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1549 r->value.boolean = truth;
1551 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1552 r->value.ptrarray = g_ptr_array_new ();
1554 if (argc == 1 && argv[0]->value.string[0] == 0) {
1555 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1557 for (i=0;i<v->len;i++) {
1558 gchar *uid = g_ptr_array_index (v, i);
1560 g_ptr_array_add (r->value.ptrarray, uid);
1563 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1566 for (i=0;i<argc;i++) {
1567 if (argv[i]->type == ESEXP_RES_STRING) {
1568 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1569 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1570 matches = match_words_index (search, words, error);
1572 /* FIXME Pass a GCancellable */
1573 matches = match_words_messages (search, words, NULL, error);
1575 for (j=0;j<matches->len;j++) {
1576 g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
1578 g_ptr_array_free (matches, TRUE);
1579 camel_search_words_free (words);
1582 lambdafoo.uids = r->value.ptrarray;
1583 g_hash_table_foreach (ht, (GHFunc)htor, &lambdafoo);
1584 g_hash_table_destroy (ht);
1591 static ESExpResult *
1592 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1595 CamelMimeMessage *msg = get_current_message (search);
1600 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1602 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) {
1603 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1606 r->value.boolean = FALSE;
1608 g_object_unref (msg);
1612 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1613 r->value.ptrarray = g_ptr_array_new ();
1615 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) {
1617 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1618 CamelMimeMessage *message;
1620 for (i = 0; i < v->len; i++) {
1621 gchar *uid = g_ptr_array_index (v, i);
1623 /* FIXME Pass a GCancellable */
1624 message = camel_folder_get_message_sync (
1625 search->folder, uid, NULL, NULL);
1627 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1628 g_ptr_array_add (r->value.ptrarray, uid);
1631 g_object_unref (message);
1642 static ESExpResult *
1643 search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1648 r(printf("executing user-flag\n"));
1650 /* are we inside a match-all? */
1651 if (search->current) {
1653 /* performs an OR of all words */
1654 for (i=0;i<argc && !truth;i++) {
1655 if (argv[i]->type == ESEXP_RES_STRING
1656 && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
1661 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1662 r->value.boolean = truth;
1664 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1665 r->value.ptrarray = g_ptr_array_new ();
1671 static ESExpResult *
1672 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1676 r(printf ("executing system-flag\n"));
1678 if (search->current) {
1679 gboolean truth = FALSE;
1682 truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
1684 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1685 r->value.boolean = truth;
1687 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1688 r->value.ptrarray = g_ptr_array_new ();
1694 static ESExpResult *
1695 search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1697 const gchar *value = NULL;
1700 r(printf("executing user-tag\n"));
1702 if (search->current && argc == 1)
1703 value = camel_message_info_user_tag (search->current, argv[0]->value.string);
1705 r = e_sexp_result_new (f, ESEXP_RES_STRING);
1706 r->value.string = g_strdup (value ? value : "");
1711 static ESExpResult *
1712 search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1716 r(printf("executing get-sent-date\n"));
1718 /* are we inside a match-all? */
1720 r = e_sexp_result_new (f, ESEXP_RES_INT);
1722 r->value.number = camel_message_info_date_sent (s->current);
1724 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1725 r->value.ptrarray = g_ptr_array_new ();
1731 static ESExpResult *
1732 search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1736 r(printf("executing get-received-date\n"));
1738 /* are we inside a match-all? */
1740 r = e_sexp_result_new (f, ESEXP_RES_INT);
1742 r->value.number = camel_message_info_date_received (s->current);
1744 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1745 r->value.ptrarray = g_ptr_array_new ();
1751 static ESExpResult *
1752 search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1756 r(printf("executing get-current-date\n"));
1758 r = e_sexp_result_new (f, ESEXP_RES_INT);
1759 r->value.number = time (NULL);
1763 static ESExpResult *
1764 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1768 r(printf("executing get-size\n"));
1770 /* are we inside a match-all? */
1772 r = e_sexp_result_new (f, ESEXP_RES_INT);
1773 r->value.number = camel_message_info_size (s->current) / 1024;
1775 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1776 r->value.ptrarray = g_ptr_array_new ();
1782 static ESExpResult *
1783 search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1788 r(printf("executing uid\n"));
1790 /* are we inside a match-all? */
1791 if (search->current) {
1793 const gchar *uid = camel_message_info_uid (search->current);
1795 /* performs an OR of all words */
1796 for (i=0;i<argc && !truth;i++) {
1797 if (argv[i]->type == ESEXP_RES_STRING
1798 && !strcmp (uid, argv[i]->value.string)) {
1803 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1804 r->value.boolean = truth;
1806 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1807 r->value.ptrarray = g_ptr_array_new ();
1808 for (i=0;i<argc;i++) {
1809 if (argv[i]->type == ESEXP_RES_STRING)
1810 g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
1818 read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name)
1822 matches = (GPtrArray *) ref;
1824 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols[0]));
1828 static ESExpResult *
1829 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1832 gboolean same = FALSE;
1834 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1835 if (argv[0]->value.string && search->folder) {
1836 const gchar *furi = camel_folder_get_uri (search->folder);
1838 same = g_str_equal (furi ? furi : "", argv[0]->value.string);
1842 if (search->current) {
1843 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1844 r->value.boolean = same ? TRUE : FALSE;
1846 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1847 r->value.ptrarray = g_ptr_array_new ();
1852 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1854 for (i = 0; i < v->len; i++) {
1855 gchar *uid = g_ptr_array_index (v, i);
1857 g_ptr_array_add (r->value.ptrarray, uid);