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_relative_months (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
94 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
96 G_DEFINE_TYPE (CamelFolderSearch, camel_folder_search, CAMEL_TYPE_OBJECT)
99 folder_search_dispose (GObject *object)
101 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
103 if (search->sexp != NULL) {
104 e_sexp_unref (search->sexp);
108 /* Chain up to parent's dispose() method. */
109 G_OBJECT_CLASS (camel_folder_search_parent_class)->dispose (object);
113 folder_search_finalize (GObject *object)
115 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
117 g_free (search->last_search);
119 /* Chain up to parent's finalize() method. */
120 G_OBJECT_CLASS (camel_folder_search_parent_class)->finalize (object);
124 camel_folder_search_class_init (CamelFolderSearchClass *class)
126 GObjectClass *object_class;
128 g_type_class_add_private (class, sizeof (CamelFolderSearchPrivate));
130 object_class = G_OBJECT_CLASS (class);
131 object_class->dispose = folder_search_dispose;
132 object_class->finalize = folder_search_finalize;
134 class->not = search_not;
135 class->match_all = search_match_all;
136 class->match_threads = search_match_threads;
137 class->body_contains = search_body_contains;
138 class->body_regex = search_body_regex;
139 class->header_contains = search_header_contains;
140 class->header_matches = search_header_matches;
141 class->header_starts_with = search_header_starts_with;
142 class->header_ends_with = search_header_ends_with;
143 class->header_exists = search_header_exists;
144 class->header_soundex = search_header_soundex;
145 class->header_regex = search_header_regex;
146 class->header_full_regex = search_header_full_regex;
147 class->user_tag = search_user_tag;
148 class->user_flag = search_user_flag;
149 class->system_flag = search_system_flag;
150 class->get_sent_date = search_get_sent_date;
151 class->get_received_date = search_get_received_date;
152 class->get_current_date = search_get_current_date;
153 class->get_relative_months = search_get_relative_months;
154 class->get_size = search_get_size;
155 class->uid = search_uid;
156 class->message_location = search_message_location;
160 camel_folder_search_init (CamelFolderSearch *search)
162 search->priv = G_TYPE_INSTANCE_GET_PRIVATE (
163 search, CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate);
164 search->sexp = e_sexp_new ();
170 gint flags; /* 0x02 = immediate, 0x01 = always enter */
172 /* these have default implementations in e-sexp */
173 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
174 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
175 /* we need to override this one though to implement an 'array not' */
176 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
177 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
178 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
179 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
181 /* these we have to use our own default if there is none */
182 /* they should all be defined in the language? so it parses, or should they not?? */
183 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
184 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
185 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
186 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
187 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
188 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
189 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
190 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
191 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
192 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
193 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
194 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
195 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
196 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
197 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
198 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
199 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
200 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
201 { "get-relative-months", G_STRUCT_OFFSET(CamelFolderSearchClass, get_relative_months), 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,
269 search->folder = folder;
273 * camel_folder_search_set_summary:
275 * @summary: An array of CamelMessageInfo pointers.
277 * Set the array of summary objects representing the span of the search.
279 * If this is not set, then a subclass must provide the functions
280 * for searching headers and for the match-all operator.
283 camel_folder_search_set_summary (CamelFolderSearch *search,
286 search->summary = summary;
290 * camel_folder_search_set_body_index:
294 * Set the index representing the contents of all messages
295 * in this folder. If this is not set, then the folder implementation
296 * should sub-class the CamelFolderSearch and provide its own
297 * body-contains function.
300 camel_folder_search_set_body_index (CamelFolderSearch *search,
303 if (search->body_index)
304 g_object_unref (search->body_index);
305 search->body_index = index;
307 g_object_ref (index);
311 * camel_folder_search_execute_expression:
314 * @error: return location for a #GError, or %NULL
316 * Execute the search expression @expr, returning an array of
317 * all matches as a GPtrArray of uid's of matching messages.
319 * Note that any settings such as set_body_index(), set_folder(),
320 * and so on are reset to %NULL once the search has completed.
322 * TODO: The interface should probably return summary items instead
323 * (since they are much more useful to any client).
325 * Returns: A GPtrArray of strings of all matching messages.
326 * This must only be freed by camel_folder_search_free_result.
329 camel_folder_search_execute_expression (CamelFolderSearch *search,
337 CamelFolderSearchPrivate *p = search->priv;
341 /* only re-parse if the search has changed */
342 if (search->last_search == NULL
343 || strcmp (search->last_search, expr)) {
344 e_sexp_input_text (search->sexp, expr, strlen (expr));
345 if (e_sexp_parse (search->sexp) == -1) {
347 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
348 _("Cannot parse search expression: %s:\n%s"),
349 e_sexp_error (search->sexp), expr);
353 g_free (search->last_search);
354 search->last_search = g_strdup (expr);
356 r = e_sexp_eval (search->sexp);
359 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
360 _("Error executing search expression: %s:\n%s"),
361 e_sexp_error (search->sexp), expr);
365 matches = g_ptr_array_new ();
367 /* now create a folder summary to return?? */
368 if (r->type == ESEXP_RES_ARRAY_PTR) {
369 d(printf("got result ...\n"));
370 if (search->summary) {
371 /* reorder result in summary order */
372 results = g_hash_table_new (g_str_hash, g_str_equal);
373 for (i = 0; i < r->value.ptrarray->len; i++) {
374 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
375 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
377 for (i = 0; i < search->summary->len; i++) {
378 gchar *uid = g_ptr_array_index (search->summary, i);
379 if (g_hash_table_lookup (results, uid)) {
380 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
383 g_hash_table_destroy (results);
385 for (i = 0; i < r->value.ptrarray->len; i++) {
386 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
387 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (g_ptr_array_index (r->value.ptrarray, i)));
391 g_warning("Search returned an invalid result type");
394 e_sexp_result_free (search->sexp, r);
397 camel_folder_thread_messages_unref (p->threads);
399 g_hash_table_destroy (p->threads_hash);
402 p->threads_hash = NULL;
403 search->folder = NULL;
404 search->summary = NULL;
405 search->current = NULL;
406 search->body_index = NULL;
412 * camel_folder_search_count:
415 * @uids: to search against, NULL for all uid's.
416 * @error: return location for a #GError, or %NULL
418 * Run a search. Search must have had Folder already set on it, and
419 * it must implement summaries.
421 * Returns: Number of messages that match the query.
427 camel_folder_search_count (CamelFolderSearch *search,
432 GPtrArray *summary_set;
435 gchar *sql_query, *tmp, *tmp1;
439 CamelFolderSearchPrivate *p = search->priv;
441 g_assert (search->folder);
445 /* We route body-contains search and thread based search through memory and not via db. */
446 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
447 /* setup our search list only contains those we're interested in */
448 search->summary = camel_folder_get_summary (search->folder);
450 summary_set = search->summary;
452 /* only re-parse if the search has changed */
453 if (search->last_search == NULL
454 || strcmp (search->last_search, expr)) {
455 e_sexp_input_text (search->sexp, expr, strlen (expr));
456 if (e_sexp_parse (search->sexp) == -1) {
458 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
459 _("Cannot parse search expression: %s:\n%s"),
460 e_sexp_error (search->sexp), expr);
464 g_free (search->last_search);
465 search->last_search = g_strdup (expr);
467 r = e_sexp_eval (search->sexp);
470 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
471 _("Error executing search expression: %s:\n%s"),
472 e_sexp_error (search->sexp), expr);
476 /* now create a folder summary to return?? */
477 if (r->type == ESEXP_RES_ARRAY_PTR) {
478 d(printf("got result\n"));
480 /* reorder result in summary order */
481 results = g_hash_table_new (g_str_hash, g_str_equal);
482 for (i = 0; i < r->value.ptrarray->len; i++) {
483 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
484 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
487 for (i = 0; i < summary_set->len; i++) {
488 gchar *uid = g_ptr_array_index (summary_set, i);
489 if (g_hash_table_lookup (results, uid))
492 g_hash_table_destroy (results);
495 e_sexp_result_free (search->sexp, r);
498 CamelStore *parent_store;
499 const gchar *full_name;
500 GError *local_error = NULL;
502 full_name = camel_folder_get_full_name (search->folder);
503 parent_store = camel_folder_get_parent_store (search->folder);
505 /* Sync the db, so that we search the db for changes */
506 camel_folder_summary_save_to_db (search->folder->summary, error);
508 dd(printf ("sexp is : [%s]\n", expr));
509 if (g_getenv("SQL_SEARCH_OLD"))
510 sql_query = camel_sexp_to_sql (expr);
512 sql_query = camel_sexp_to_sql_sexp (expr);
513 tmp1 = camel_db_sqlize_string (full_name);
514 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
515 camel_db_free_sqlized_string (tmp1);
517 dd(printf("Equivalent sql %s\n", tmp));
519 cdb = (CamelDB *) (parent_store->cdb_r);
520 camel_db_count_message_info (cdb, tmp, &count, &local_error);
521 if (local_error != NULL) {
522 const gchar *message = local_error->message;
523 if (strncmp(message, "no such table", 13) == 0) {
524 d(g_warning ("Error during searching %s: %s\n", tmp, message));
525 /* Suppress no such table */
526 g_clear_error (&local_error);
528 g_propagate_error (error, local_error);
534 /* these might be allocated by match-threads */
536 camel_folder_thread_messages_unref (p->threads);
538 g_hash_table_destroy (p->threads_hash);
539 if (search->summary_set)
540 g_ptr_array_free (search->summary_set, TRUE);
542 camel_folder_free_summary (search->folder, search->summary);
545 p->threads_hash = NULL;
546 search->folder = NULL;
547 search->summary = NULL;
548 search->summary_set = NULL;
549 search->current = NULL;
550 search->body_index = NULL;
556 do_search_in_memory (const gchar *expr)
558 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
559 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
565 for (i = 0; in_memory_tokens[i]; i++) {
566 if (strstr (expr, in_memory_tokens[i]))
574 * camel_folder_search_search:
577 * @uids: to search against, NULL for all uid's.
578 * @error: return location for a #GError, or %NULL
580 * Run a search. Search must have had Folder already set on it, and
581 * it must implement summaries.
586 camel_folder_search_search (CamelFolderSearch *search,
592 GPtrArray *matches = NULL, *summary_set;
595 gchar *sql_query, *tmp, *tmp1;
598 CamelFolderSearchPrivate *p = search->priv;
600 g_assert (search->folder);
604 /* We route body-contains / thread based search and uid search through memory and not via db. */
605 if (uids || do_search_in_memory (expr)) {
606 /* setup our search list only contains those we're interested in */
607 search->summary = camel_folder_get_summary (search->folder);
610 GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
612 summary_set = search->summary_set = g_ptr_array_new ();
613 for (i = 0; i < uids->len; i++)
614 g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
615 for (i = 0; i < search->summary->len; i++)
616 if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
617 g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
618 g_hash_table_destroy (uids_hash);
620 summary_set = search->summary;
623 /* only re-parse if the search has changed */
624 if (search->last_search == NULL
625 || strcmp (search->last_search, expr)) {
626 e_sexp_input_text (search->sexp, expr, strlen (expr));
627 if (e_sexp_parse (search->sexp) == -1) {
629 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
630 _("Cannot parse search expression: %s:\n%s"),
631 e_sexp_error (search->sexp), expr);
635 g_free (search->last_search);
636 search->last_search = g_strdup (expr);
638 r = e_sexp_eval (search->sexp);
641 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
642 _("Error executing search expression: %s:\n%s"),
643 e_sexp_error (search->sexp), expr);
647 matches = g_ptr_array_new ();
649 /* now create a folder summary to return?? */
650 if (r->type == ESEXP_RES_ARRAY_PTR) {
651 d(printf("got result\n"));
653 /* reorder result in summary order */
654 results = g_hash_table_new (g_str_hash, g_str_equal);
655 for (i = 0; i < r->value.ptrarray->len; i++) {
656 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
657 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
660 for (i = 0; i < summary_set->len; i++) {
661 gchar *uid = g_ptr_array_index (summary_set, i);
662 if (g_hash_table_lookup (results, uid))
663 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
665 g_hash_table_destroy (results);
668 e_sexp_result_free (search->sexp, r);
671 CamelStore *parent_store;
672 const gchar *full_name;
673 GError *local_error = NULL;
675 full_name = camel_folder_get_full_name (search->folder);
676 parent_store = camel_folder_get_parent_store (search->folder);
678 /* Sync the db, so that we search the db for changes */
679 camel_folder_summary_save_to_db (search->folder->summary, error);
681 dd(printf ("sexp is : [%s]\n", expr));
682 if (g_getenv("SQL_SEARCH_OLD"))
683 sql_query = camel_sexp_to_sql (expr);
685 sql_query = camel_sexp_to_sql_sexp (expr);
686 tmp1 = camel_db_sqlize_string (full_name);
687 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
688 camel_db_free_sqlized_string (tmp1);
690 dd(printf("Equivalent sql %s\n", tmp));
692 matches = g_ptr_array_new ();
693 cdb = (CamelDB *) (parent_store->cdb_r);
695 cdb, tmp, (CamelDBSelectCB)
696 read_uid_callback, matches, &local_error);
697 if (local_error != NULL) {
698 const gchar *message = local_error->message;
699 if (strncmp(message, "no such table", 13) == 0) {
700 d(g_warning ("Error during searching %s: %s\n", tmp, message));
701 /* Suppress no such table */
702 g_clear_error (&local_error);
704 g_propagate_error (error, local_error);
711 /* these might be allocated by match-threads */
713 camel_folder_thread_messages_unref (p->threads);
715 g_hash_table_destroy (p->threads_hash);
716 if (search->summary_set)
717 g_ptr_array_free (search->summary_set, TRUE);
719 camel_folder_free_summary (search->folder, search->summary);
722 p->threads_hash = NULL;
723 search->folder = NULL;
724 search->summary = NULL;
725 search->summary_set = NULL;
726 search->current = NULL;
727 search->body_index = NULL;
732 void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *result)
734 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
735 g_ptr_array_free (result, TRUE);
738 /* dummy function, returns false always, or an empty match array */
740 search_dummy (struct _ESExp *f,
742 struct _ESExpResult **argv,
743 CamelFolderSearch *search)
747 if (search->current == NULL) {
748 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
749 r->value.boolean = FALSE;
751 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
752 r->value.ptrarray = g_ptr_array_new ();
758 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
760 search_not (struct _ESExp *f,
762 struct _ESExpResult **argv,
763 CamelFolderSearch *search)
769 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
770 GPtrArray *v = argv[0]->value.ptrarray;
773 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
774 r->value.ptrarray = g_ptr_array_new ();
776 /* not against a single message?*/
777 if (search->current) {
780 uid = camel_message_info_uid (search->current);
781 for (i = 0; !found && i < v->len; i++) {
782 if (strcmp (uid, v->pdata[i]) == 0)
787 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
788 } else if (search->summary == NULL) {
789 g_warning("No summary set, 'not' against an array requires a summary");
791 /* 'not' against the whole summary */
792 GHashTable *have = g_hash_table_new (g_str_hash, g_str_equal);
796 s = (gchar **) v->pdata;
797 for (i = 0; i < v->len; i++)
798 g_hash_table_insert (have, s[i], s[i]);
800 v = search->summary_set ? search->summary_set : search->summary;
801 m = (gchar **) v->pdata;
802 for (i = 0; i < v->len; i++) {
805 if (g_hash_table_lookup (have, uid) == NULL)
806 g_ptr_array_add (r->value.ptrarray, uid);
808 g_hash_table_destroy (have);
813 if (argv[0]->type == ESEXP_RES_BOOL)
814 res = !argv[0]->value.boolean;
816 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
817 r->value.boolean = res;
820 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
821 r->value.boolean = TRUE;
828 search_match_all (struct _ESExp *f,
830 struct _ESExpTerm **argv,
831 CamelFolderSearch *search)
839 g_warning("match-all only takes a single argument, other arguments ignored");
842 /* we are only matching a single message? or already inside a match-all? */
843 if (search->current) {
844 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
846 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
847 r->value.boolean = FALSE;
850 r1 = e_sexp_term_eval (f, argv[0]);
851 if (r1->type == ESEXP_RES_BOOL) {
852 r->value.boolean = r1->value.boolean;
854 g_warning("invalid syntax, matches require a single bool result");
855 /* Translators: The '%s' is an element type name, part of an expressing language */
856 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
857 e_sexp_fatal_error (f, error_msg);
860 e_sexp_result_free (f, r1);
862 r->value.boolean = TRUE;
867 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
868 r->value.ptrarray = g_ptr_array_new ();
870 if (search->summary == NULL) {
871 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
872 g_warning("No summary supplied, match-all doesn't work with no summary");
877 v = search->summary_set ? search->summary_set : search->summary;
879 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
880 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
883 for (i = 0; i < v->len; i++) {
886 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
887 if (!search->current)
889 uid = camel_message_info_uid (search->current);
892 r1 = e_sexp_term_eval (f, argv[0]);
893 if (r1->type == ESEXP_RES_BOOL) {
894 if (r1->value.boolean)
895 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
897 g_warning("invalid syntax, matches require a single bool result");
898 /* Translators: The '%s' is an element type name, part of an expressing language */
899 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
900 e_sexp_fatal_error (f, error_msg);
903 e_sexp_result_free (f, r1);
905 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
907 camel_message_info_free (search->current);
909 search->current = NULL;
914 fill_thread_table (struct _CamelFolderThreadNode *root,
918 g_hash_table_insert (id_hash, (gchar *) camel_message_info_uid (root->message), root);
920 fill_thread_table (root->child, id_hash);
926 add_thread_results (struct _CamelFolderThreadNode *root,
927 GHashTable *result_hash)
930 g_hash_table_insert (result_hash, (gchar *) camel_message_info_uid (root->message), GINT_TO_POINTER (1));
932 add_thread_results (root->child, result_hash);
938 add_results (gchar *uid,
942 g_ptr_array_add (result, uid);
946 search_match_threads (struct _ESExp *f,
948 struct _ESExpTerm **argv,
949 CamelFolderSearch *search)
952 CamelFolderSearchPrivate *p = search->priv;
957 /* not supported in match-all */
958 if (search->current) {
959 /* Translators: Each '%s' is an element type name, part of an expressing language */
960 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
961 e_sexp_fatal_error (f, error_msg);
966 /* Translators: The '%s' is an element type name, part of an expressing language */
967 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
968 e_sexp_fatal_error (f, error_msg);
972 r = e_sexp_term_eval (f, argv[0]);
973 if (r->type != ESEXP_RES_STRING) {
974 /* Translators: The '%s' is an element type name, part of an expressing language */
975 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
976 e_sexp_fatal_error (f, error_msg);
981 if (!strcmp(r->value.string, "none"))
983 else if (!strcmp(r->value.string, "all"))
985 else if (!strcmp(r->value.string, "replies"))
987 else if (!strcmp(r->value.string, "replies_parents"))
989 else if (!strcmp(r->value.string, "single"))
991 e_sexp_result_free (f, r);
993 /* behave as (begin does */
995 for (i = 1; i < argc; i++) {
997 e_sexp_result_free (f, r);
998 r = e_sexp_term_eval (f, argv[i]);
1001 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
1002 /* Translators: The '%s' is an element type name, part of an expressing language */
1003 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
1004 e_sexp_fatal_error (f, error_msg);
1011 if (search->folder == NULL) {
1012 /* Translators: The '%s' is an element type name, part of an expressing language */
1013 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
1014 e_sexp_fatal_error (f, error_msg);
1018 /* cache this, so we only have to re-calculate once per search at most */
1019 if (p->threads == NULL) {
1020 p->threads = camel_folder_thread_messages_new (search->folder, NULL, TRUE);
1021 p->threads_hash = g_hash_table_new (g_str_hash, g_str_equal);
1023 fill_thread_table (p->threads->tree, p->threads_hash);
1026 results = g_hash_table_new (g_str_hash, g_str_equal);
1027 for (i = 0; i < r->value.ptrarray->len; i++) {
1028 struct _CamelFolderThreadNode *node, *scan;
1031 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
1033 node = g_hash_table_lookup (p->threads_hash, (gchar *) g_ptr_array_index (r->value.ptrarray, i));
1034 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
1037 /* select messages in thread according to search criteria */
1039 if (node->child == NULL && node->parent == NULL)
1040 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1044 while (scan && scan->parent) {
1045 scan = scan->parent;
1046 g_hash_table_insert (results, (gchar *) camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
1048 } else if (type == 1) {
1049 while (node && node->parent)
1050 node = node->parent;
1052 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1054 add_thread_results (node->child, results);
1057 e_sexp_result_free (f, r);
1059 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1060 r->value.ptrarray = g_ptr_array_new ();
1062 g_hash_table_foreach (results, (GHFunc) add_results, r->value.ptrarray);
1063 g_hash_table_destroy (results);
1068 static CamelMimeMessage *
1069 get_current_message (CamelFolderSearch *search)
1071 if (!search || !search->folder || !search->current)
1074 /* FIXME Pass a GCancellable */
1075 return camel_folder_get_message_sync (
1076 search->folder, search->current->uid, NULL, NULL);
1079 static ESExpResult *
1080 check_header (struct _ESExp *f,
1082 struct _ESExpResult **argv,
1083 CamelFolderSearch *search,
1084 camel_search_match_t how)
1089 r(printf("executing check-header %d\n", how));
1091 /* are we inside a match-all? */
1092 if (search->current && argc > 1
1093 && argv[0]->type == ESEXP_RES_STRING) {
1095 const gchar *header = NULL, *charset = NULL;
1098 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1099 struct _camel_search_words *words;
1100 CamelMimeMessage *message = NULL;
1101 struct _camel_header_raw *raw_header;
1103 /* only a subset of headers are supported .. */
1104 headername = argv[0]->value.string;
1105 if (!g_ascii_strcasecmp(headername, "subject")) {
1106 header = camel_message_info_subject (search->current);
1107 } else if (!g_ascii_strcasecmp(headername, "date")) {
1108 /* FIXME: not a very useful form of the date */
1109 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1111 } else if (!g_ascii_strcasecmp(headername, "from")) {
1112 header = camel_message_info_from (search->current);
1113 type = CAMEL_SEARCH_TYPE_ADDRESS;
1114 } else if (!g_ascii_strcasecmp(headername, "to")) {
1115 header = camel_message_info_to (search->current);
1116 type = CAMEL_SEARCH_TYPE_ADDRESS;
1117 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1118 header = camel_message_info_cc (search->current);
1119 type = CAMEL_SEARCH_TYPE_ADDRESS;
1120 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1121 header = camel_message_info_mlist (search->current);
1122 type = CAMEL_SEARCH_TYPE_MLIST;
1124 message = get_current_message (search);
1126 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1129 charset = camel_content_type_param (ct, "charset");
1130 charset = camel_iconv_charset_name (charset);
1138 /* performs an OR of all words */
1139 for (i = 1; i < argc && !truth; i++) {
1140 if (argv[i]->type == ESEXP_RES_STRING) {
1141 if (argv[i]->value.string[0] == 0) {
1143 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1144 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1145 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1147 for (j = 0; j < words->len && truth; j++) {
1149 for (raw_header = ((CamelMimePart *) message)->headers; raw_header; raw_header = raw_header->next) {
1150 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1151 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1156 truth = raw_header != NULL;
1158 truth = camel_search_header_match (header, words->words[j]->word, how, type, charset);
1160 camel_search_words_free (words);
1163 for (raw_header = ((CamelMimePart *) message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1164 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1165 truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
1169 truth = camel_search_header_match (header, argv[i]->value.string, how, type, charset);
1175 g_object_unref (message);
1177 /* TODO: else, find all matches */
1179 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1180 r->value.boolean = truth;
1187 l_printf (gchar *node)
1189 printf("%s\t", node);
1193 static ESExpResult *
1194 search_header_contains (struct _ESExp *f,
1196 struct _ESExpResult **argv,
1197 CamelFolderSearch *search)
1199 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1202 static ESExpResult *
1203 search_header_matches (struct _ESExp *f,
1205 struct _ESExpResult **argv,
1206 CamelFolderSearch *search)
1208 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1211 static ESExpResult *
1212 search_header_starts_with (struct _ESExp *f,
1214 struct _ESExpResult **argv,
1215 CamelFolderSearch *search)
1217 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1220 static ESExpResult *
1221 search_header_ends_with (struct _ESExp *f,
1223 struct _ESExpResult **argv,
1224 CamelFolderSearch *search)
1226 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1229 static ESExpResult *
1230 search_header_exists (struct _ESExp *f,
1232 struct _ESExpResult **argv,
1233 CamelFolderSearch *search)
1237 r(printf ("executing header-exists\n"));
1239 if (search->current) {
1240 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1241 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1242 r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
1245 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1246 r->value.ptrarray = g_ptr_array_new ();
1252 static ESExpResult *
1253 search_header_soundex (struct _ESExp *f,
1255 struct _ESExpResult **argv,
1256 CamelFolderSearch *search)
1258 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1261 static ESExpResult *
1262 search_header_regex (struct _ESExp *f,
1264 struct _ESExpResult **argv,
1265 CamelFolderSearch *search)
1268 CamelMimeMessage *msg;
1270 msg = get_current_message (search);
1274 const gchar *contents;
1276 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1278 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1279 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1280 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_ICASE, argc - 1, argv + 1, search->priv->error) == 0) {
1281 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1284 r->value.boolean = FALSE;
1286 g_object_unref (msg);
1288 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1289 r->value.ptrarray = g_ptr_array_new ();
1296 get_full_header (CamelMimeMessage *message)
1298 CamelMimePart *mp = CAMEL_MIME_PART (message);
1299 GString *str = g_string_new ("");
1300 struct _camel_header_raw *h;
1302 for (h = mp->headers; h; h = h->next) {
1303 if (h->value != NULL) {
1304 g_string_append (str, h->name);
1305 if (isspace (h->value[0]))
1306 g_string_append (str, ":");
1308 g_string_append (str, ": ");
1309 g_string_append (str, h->value);
1310 g_string_append_c (str, '\n');
1314 return g_string_free (str, FALSE);
1317 static ESExpResult *
1318 search_header_full_regex (struct _ESExp *f,
1320 struct _ESExpResult **argv,
1321 CamelFolderSearch *search)
1324 CamelMimeMessage *msg;
1326 msg = get_current_message (search);
1331 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1333 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) {
1336 contents = get_full_header (msg);
1337 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1342 r->value.boolean = FALSE;
1344 g_object_unref (msg);
1346 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1347 r->value.ptrarray = g_ptr_array_new ();
1353 /* this is just to OR results together */
1359 /* or, store all unique values */
1363 struct IterData *iter_data)
1365 g_ptr_array_add (iter_data->uids, key);
1368 /* and, only store duplicates */
1372 struct IterData *iter_data)
1374 if (value == iter_data->count)
1375 g_ptr_array_add (iter_data->uids, key);
1379 match_message_index (CamelIndex *idx,
1384 CamelIndexCursor *wc, *nc;
1385 const gchar *word, *name;
1388 wc = camel_index_words (idx);
1390 while (!truth && (word = camel_index_cursor_next (wc))) {
1391 if (camel_ustrstrcase (word,match) != NULL) {
1392 /* perf: could have the wc cursor return the name cursor */
1393 nc = camel_index_find (idx, word);
1395 while (!truth && (name = camel_index_cursor_next (nc)))
1396 truth = strcmp (name, uid) == 0;
1397 g_object_unref (nc);
1401 g_object_unref (wc);
1408 "one two" "three" "four five"
1417 /* returns messages which contain all words listed in words */
1419 match_words_index (CamelFolderSearch *search,
1420 struct _camel_search_words *words,
1423 GPtrArray *result = g_ptr_array_new ();
1424 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1425 struct IterData lambdafoo;
1426 CamelIndexCursor *wc, *nc;
1427 const gchar *word, *name;
1430 /* we can have a maximum of 32 words, as we use it as the AND mask */
1432 wc = camel_index_words (search->body_index);
1434 while ((word = camel_index_cursor_next (wc))) {
1435 for (i = 0; i < words->len; i++) {
1436 if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
1437 /* perf: could have the wc cursor return the name cursor */
1438 nc = camel_index_find (search->body_index, word);
1440 while ((name = camel_index_cursor_next (nc))) {
1443 mask = (GPOINTER_TO_INT (g_hash_table_lookup (ht, name))) | (1 << i);
1444 g_hash_table_insert (ht, (gchar *) camel_pstring_peek (name), GINT_TO_POINTER (mask));
1446 g_object_unref (nc);
1451 g_object_unref (wc);
1453 lambdafoo.uids = result;
1454 lambdafoo.count = (1 << words->len) - 1;
1455 g_hash_table_foreach (ht, (GHFunc) htand, &lambdafoo);
1456 g_hash_table_destroy (ht);
1463 match_words_1message (CamelDataWrapper *object,
1464 struct _camel_search_words *words,
1467 CamelDataWrapper *containee;
1471 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1473 if (containee == NULL)
1476 /* using the object types is more accurate than using the mime/types */
1477 if (CAMEL_IS_MULTIPART (containee)) {
1478 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1479 for (i = 0; i < parts && truth == FALSE; i++) {
1480 CamelDataWrapper *part = (CamelDataWrapper *) camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1482 truth = match_words_1message (part, words, mask);
1484 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1485 /* for messages we only look at its contents */
1486 truth = match_words_1message ((CamelDataWrapper *) containee, words, mask);
1487 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1488 /* for all other text parts, we look inside, otherwise we dont care */
1489 CamelStream *stream;
1490 GByteArray *byte_array;
1492 byte_array = g_byte_array_new ();
1493 stream = camel_stream_mem_new_with_byte_array (byte_array);
1495 /* FIXME The match should be part of a stream op */
1496 /* FIXME Pass a GCancellable and GError here. */
1497 camel_data_wrapper_decode_to_stream_sync (
1498 containee, stream, NULL, NULL);
1499 camel_stream_write (stream, "", 1, NULL, NULL);
1500 for (i = 0; i < words->len; i++) {
1501 /* FIXME: This is horridly slow, and should use a real search algorithm */
1502 if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1504 /* shortcut a match */
1505 if (*mask == (1 << (words->len)) - 1)
1510 g_object_unref (stream);
1517 match_words_message (CamelFolder *folder,
1519 struct _camel_search_words *words,
1520 GCancellable *cancellable,
1524 CamelMimeMessage *msg;
1527 msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
1530 truth = match_words_1message ((CamelDataWrapper *) msg, words, &mask);
1531 g_object_unref (msg);
1538 match_words_messages (CamelFolderSearch *search,
1539 struct _camel_search_words *words,
1540 GCancellable *cancellable,
1544 GPtrArray *matches = g_ptr_array_new ();
1546 if (search->body_index) {
1548 struct _camel_search_words *simple;
1550 simple = camel_search_words_simple (words);
1551 indexed = match_words_index (search, simple, error);
1552 camel_search_words_free (simple);
1554 for (i = 0; i < indexed->len; i++) {
1555 const gchar *uid = g_ptr_array_index (indexed, i);
1557 if (match_words_message (
1558 search->folder, uid, words,
1559 cancellable, error))
1560 g_ptr_array_add (matches, (gchar *) uid);
1563 g_ptr_array_free (indexed, TRUE);
1565 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1567 for (i = 0; i < v->len; i++) {
1568 gchar *uid = g_ptr_array_index (v, i);
1570 if (match_words_message (
1571 search->folder, uid, words,
1572 cancellable, error))
1573 g_ptr_array_add (matches, (gchar *) uid);
1580 static ESExpResult *
1581 search_body_contains (struct _ESExp *f,
1583 struct _ESExpResult **argv,
1584 CamelFolderSearch *search)
1587 GError **error = search->priv->error;
1588 struct _camel_search_words *words;
1590 struct IterData lambdafoo;
1592 if (search->current) {
1595 if (argc == 1 && argv[0]->value.string[0] == 0) {
1598 for (i = 0; i < argc && !truth; i++) {
1599 if (argv[i]->type == ESEXP_RES_STRING) {
1600 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1602 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1603 for (j = 0; j < words->len && truth; j++)
1604 truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
1606 /* TODO: cache current message incase of multiple body search terms */
1607 /* FIXME Pass a GCancellable */
1608 truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
1610 camel_search_words_free (words);
1614 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1615 r->value.boolean = truth;
1617 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1618 r->value.ptrarray = g_ptr_array_new ();
1620 if (argc == 1 && argv[0]->value.string[0] == 0) {
1621 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1623 for (i = 0; i < v->len; i++) {
1624 gchar *uid = g_ptr_array_index (v, i);
1626 g_ptr_array_add (r->value.ptrarray, uid);
1629 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1632 for (i = 0; i < argc; i++) {
1633 if (argv[i]->type == ESEXP_RES_STRING) {
1634 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1635 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1636 matches = match_words_index (search, words, error);
1638 /* FIXME Pass a GCancellable */
1639 matches = match_words_messages (search, words, NULL, error);
1641 for (j = 0; j < matches->len; j++) {
1642 g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
1644 g_ptr_array_free (matches, TRUE);
1645 camel_search_words_free (words);
1648 lambdafoo.uids = r->value.ptrarray;
1649 g_hash_table_foreach (ht, (GHFunc) htor, &lambdafoo);
1650 g_hash_table_destroy (ht);
1657 static ESExpResult *
1658 search_body_regex (struct _ESExp *f,
1660 struct _ESExpResult **argv,
1661 CamelFolderSearch *search)
1664 CamelMimeMessage *msg = get_current_message (search);
1669 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1671 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) {
1672 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1675 r->value.boolean = FALSE;
1677 g_object_unref (msg);
1681 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1682 r->value.ptrarray = g_ptr_array_new ();
1684 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) {
1686 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1687 CamelMimeMessage *message;
1689 for (i = 0; i < v->len; i++) {
1690 gchar *uid = g_ptr_array_index (v, i);
1692 /* FIXME Pass a GCancellable */
1693 message = camel_folder_get_message_sync (
1694 search->folder, uid, NULL, NULL);
1696 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1697 g_ptr_array_add (r->value.ptrarray, uid);
1700 g_object_unref (message);
1711 static ESExpResult *
1712 search_user_flag (struct _ESExp *f,
1714 struct _ESExpResult **argv,
1715 CamelFolderSearch *search)
1720 r(printf("executing user-flag\n"));
1722 /* are we inside a match-all? */
1723 if (search->current) {
1725 /* performs an OR of all words */
1726 for (i = 0; i < argc && !truth; i++) {
1727 if (argv[i]->type == ESEXP_RES_STRING
1728 && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
1733 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1734 r->value.boolean = truth;
1736 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1737 r->value.ptrarray = g_ptr_array_new ();
1743 static ESExpResult *
1744 search_system_flag (struct _ESExp *f,
1746 struct _ESExpResult **argv,
1747 CamelFolderSearch *search)
1751 r(printf ("executing system-flag\n"));
1753 if (search->current) {
1754 gboolean truth = FALSE;
1757 truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
1759 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1760 r->value.boolean = truth;
1762 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1763 r->value.ptrarray = g_ptr_array_new ();
1769 static ESExpResult *
1770 search_user_tag (struct _ESExp *f,
1772 struct _ESExpResult **argv,
1773 CamelFolderSearch *search)
1775 const gchar *value = NULL;
1778 r(printf("executing user-tag\n"));
1780 if (search->current && argc == 1)
1781 value = camel_message_info_user_tag (search->current, argv[0]->value.string);
1783 r = e_sexp_result_new (f, ESEXP_RES_STRING);
1784 r->value.string = g_strdup (value ? value : "");
1789 static ESExpResult *
1790 search_get_sent_date (struct _ESExp *f,
1792 struct _ESExpResult **argv,
1793 CamelFolderSearch *s)
1797 r(printf("executing get-sent-date\n"));
1799 /* are we inside a match-all? */
1801 r = e_sexp_result_new (f, ESEXP_RES_INT);
1803 r->value.number = camel_message_info_date_sent (s->current);
1805 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1806 r->value.ptrarray = g_ptr_array_new ();
1812 static ESExpResult *
1813 search_get_received_date (struct _ESExp *f,
1815 struct _ESExpResult **argv,
1816 CamelFolderSearch *s)
1820 r(printf("executing get-received-date\n"));
1822 /* are we inside a match-all? */
1824 r = e_sexp_result_new (f, ESEXP_RES_INT);
1826 r->value.number = camel_message_info_date_received (s->current);
1828 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1829 r->value.ptrarray = g_ptr_array_new ();
1835 static ESExpResult *
1836 search_get_current_date (struct _ESExp *f,
1838 struct _ESExpResult **argv,
1839 CamelFolderSearch *s)
1843 r(printf("executing get-current-date\n"));
1845 r = e_sexp_result_new (f, ESEXP_RES_INT);
1846 r->value.number = time (NULL);
1850 static ESExpResult *
1851 search_get_relative_months (struct _ESExp *f,
1853 struct _ESExpResult **argv,
1854 CamelFolderSearch *s)
1858 r(printf("executing get-relative-months\n"));
1860 if (argc != 1 || argv[0]->type != ESEXP_RES_INT) {
1861 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1862 r->value.boolean = FALSE;
1864 g_debug ("%s: Expecting 1 argument, an integer, but got %d arguments", G_STRFUNC, argc);
1866 r = e_sexp_result_new (f, ESEXP_RES_INT);
1867 r->value.number = camel_folder_search_util_add_months (time (NULL), argv[0]->value.number);
1873 static ESExpResult *
1874 search_get_size (struct _ESExp *f,
1876 struct _ESExpResult **argv,
1877 CamelFolderSearch *s)
1881 r(printf("executing get-size\n"));
1883 /* are we inside a match-all? */
1885 r = e_sexp_result_new (f, ESEXP_RES_INT);
1886 r->value.number = camel_message_info_size (s->current) / 1024;
1888 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1889 r->value.ptrarray = g_ptr_array_new ();
1895 static ESExpResult *
1896 search_uid (struct _ESExp *f,
1898 struct _ESExpResult **argv,
1899 CamelFolderSearch *search)
1904 r(printf("executing uid\n"));
1906 /* are we inside a match-all? */
1907 if (search->current) {
1909 const gchar *uid = camel_message_info_uid (search->current);
1911 /* performs an OR of all words */
1912 for (i = 0; i < argc && !truth; i++) {
1913 if (argv[i]->type == ESEXP_RES_STRING
1914 && !strcmp (uid, argv[i]->value.string)) {
1919 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1920 r->value.boolean = truth;
1922 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1923 r->value.ptrarray = g_ptr_array_new ();
1924 for (i = 0; i < argc; i++) {
1925 if (argv[i]->type == ESEXP_RES_STRING)
1926 g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
1934 read_uid_callback (gpointer ref,
1941 matches = (GPtrArray *) ref;
1943 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols[0]));
1947 static ESExpResult *
1948 search_message_location (struct _ESExp *f,
1950 struct _ESExpResult **argv,
1951 CamelFolderSearch *search)
1954 gboolean same = FALSE;
1956 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1957 if (argv[0]->value.string && search->folder) {
1963 /* FIXME Folder URI formats are Evolution-specific
1964 * knowledge and doesn't belong here! */
1965 store = camel_folder_get_parent_store (search->folder);
1966 name = camel_folder_get_full_name (search->folder);
1967 uid = camel_service_get_uid (CAMEL_SERVICE (store));
1969 uri = g_strdup_printf ("folder://%s/%s", uid, name);
1970 same = g_str_equal (uri, argv[0]->value.string);
1975 if (search->current) {
1976 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1977 r->value.boolean = same ? TRUE : FALSE;
1979 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1980 r->value.ptrarray = g_ptr_array_new ();
1985 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1987 for (i = 0; i < v->len; i++) {
1988 gchar *uid = g_ptr_array_index (v, i);
1990 g_ptr_array_add (r->value.ptrarray, uid);
1999 * camel_folder_search_util_add_months:
2001 * @months: number of months to add or subtract
2003 * Increases time @t by the given number of months (or decreases, if
2004 * @months is negative).
2006 * Returns: a new #time_t value
2011 camel_folder_search_util_add_months (time_t t,
2014 GDateTime *dt, *dt2;
2020 dt = g_date_time_new_from_unix_utc (t);
2022 /* just for issues, to return something inaccurate, but sane */
2023 res = t + (60 * 60 * 24 * 30 * months);
2025 g_return_val_if_fail (dt != NULL, res);
2027 dt2 = g_date_time_add_months (dt, months);
2028 g_date_time_unref (dt);
2029 g_return_val_if_fail (dt2 != NULL, res);
2031 res = g_date_time_to_unix (dt2);
2032 g_date_time_unref (dt2);