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 CamelSExpResult *search_not (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
69 static CamelSExpResult *search_header_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
70 static CamelSExpResult *search_header_matches (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
71 static CamelSExpResult *search_header_starts_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
72 static CamelSExpResult *search_header_ends_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
73 static CamelSExpResult *search_header_exists (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
74 static CamelSExpResult *search_header_soundex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
75 static CamelSExpResult *search_header_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
76 static CamelSExpResult *search_header_full_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
77 static CamelSExpResult *search_match_all (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *search);
78 static CamelSExpResult *search_match_threads (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *s);
79 static CamelSExpResult *search_body_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
80 static CamelSExpResult *search_body_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
81 static CamelSExpResult *search_user_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
82 static CamelSExpResult *search_user_tag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
83 static CamelSExpResult *search_system_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
84 static CamelSExpResult *search_get_sent_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
85 static CamelSExpResult *search_get_received_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
86 static CamelSExpResult *search_get_current_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
87 static CamelSExpResult *search_get_relative_months (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
88 static CamelSExpResult *search_get_size (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
89 static CamelSExpResult *search_uid (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
90 static CamelSExpResult *search_message_location (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
92 static CamelSExpResult *search_dummy (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **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 g_object_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 = camel_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 camel_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (CamelSExpIFunc) func, search);
227 camel_sexp_add_function (search->sexp, 0, builtins[i].name, (CamelSExpFunc) 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 camel_sexp_input_text (search->sexp, expr, strlen (expr));
345 if (camel_sexp_parse (search->sexp) == -1) {
347 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
348 _("Cannot parse search expression: %s:\n%s"),
349 camel_sexp_error (search->sexp), expr);
353 g_free (search->last_search);
354 search->last_search = g_strdup (expr);
356 r = camel_sexp_eval (search->sexp);
359 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
360 _("Error executing search expression: %s:\n%s"),
361 camel_sexp_error (search->sexp), expr);
365 matches = g_ptr_array_new ();
367 /* now create a folder summary to return?? */
368 if (r->type == CAMEL_SEXP_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 camel_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 * @error: return location for a #GError, or %NULL
417 * Run a search. Search must have had Folder already set on it, and
418 * it must implement summaries.
420 * Returns: Number of messages that match the query.
426 camel_folder_search_count (CamelFolderSearch *search,
431 GPtrArray *summary_set;
434 gchar *sql_query, *tmp, *tmp1;
438 CamelFolderSearchPrivate *p = search->priv;
440 g_assert (search->folder);
444 /* We route body-contains search and thread based search through memory and not via db. */
445 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
446 /* setup our search list only contains those we're interested in */
447 search->summary = camel_folder_get_summary (search->folder);
449 summary_set = search->summary;
451 /* only re-parse if the search has changed */
452 if (search->last_search == NULL
453 || strcmp (search->last_search, expr)) {
454 camel_sexp_input_text (search->sexp, expr, strlen (expr));
455 if (camel_sexp_parse (search->sexp) == -1) {
457 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
458 _("Cannot parse search expression: %s:\n%s"),
459 camel_sexp_error (search->sexp), expr);
463 g_free (search->last_search);
464 search->last_search = g_strdup (expr);
466 r = camel_sexp_eval (search->sexp);
469 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
470 _("Error executing search expression: %s:\n%s"),
471 camel_sexp_error (search->sexp), expr);
475 /* now create a folder summary to return?? */
476 if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
477 d(printf("got result\n"));
479 /* reorder result in summary order */
480 results = g_hash_table_new (g_str_hash, g_str_equal);
481 for (i = 0; i < r->value.ptrarray->len; i++) {
482 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
483 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
486 for (i = 0; i < summary_set->len; i++) {
487 gchar *uid = g_ptr_array_index (summary_set, i);
488 if (g_hash_table_lookup (results, uid))
491 g_hash_table_destroy (results);
494 camel_sexp_result_free (search->sexp, r);
497 CamelStore *parent_store;
498 const gchar *full_name;
499 GError *local_error = NULL;
501 full_name = camel_folder_get_full_name (search->folder);
502 parent_store = camel_folder_get_parent_store (search->folder);
504 /* Sync the db, so that we search the db for changes */
505 camel_folder_summary_save_to_db (search->folder->summary, error);
507 dd(printf ("sexp is : [%s]\n", expr));
508 if (g_getenv("SQL_SEARCH_OLD"))
509 sql_query = camel_sexp_to_sql (expr);
511 sql_query = camel_sexp_to_sql_sexp (expr);
512 tmp1 = camel_db_sqlize_string (full_name);
513 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
514 camel_db_free_sqlized_string (tmp1);
516 dd(printf("Equivalent sql %s\n", tmp));
518 cdb = (CamelDB *) (parent_store->cdb_r);
519 camel_db_count_message_info (cdb, tmp, &count, &local_error);
520 if (local_error != NULL) {
521 const gchar *message = local_error->message;
522 if (strncmp(message, "no such table", 13) == 0) {
523 d(g_warning ("Error during searching %s: %s\n", tmp, message));
524 /* Suppress no such table */
525 g_clear_error (&local_error);
527 g_propagate_error (error, local_error);
533 /* these might be allocated by match-threads */
535 camel_folder_thread_messages_unref (p->threads);
537 g_hash_table_destroy (p->threads_hash);
538 if (search->summary_set)
539 g_ptr_array_free (search->summary_set, TRUE);
541 camel_folder_free_summary (search->folder, search->summary);
544 p->threads_hash = NULL;
545 search->folder = NULL;
546 search->summary = NULL;
547 search->summary_set = NULL;
548 search->current = NULL;
549 search->body_index = NULL;
555 do_search_in_memory (const gchar *expr)
557 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
558 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
564 for (i = 0; in_memory_tokens[i]; i++) {
565 if (strstr (expr, in_memory_tokens[i]))
573 * camel_folder_search_search:
576 * @uids: to search against, NULL for all uid's.
577 * @error: return location for a #GError, or %NULL
579 * Run a search. Search must have had Folder already set on it, and
580 * it must implement summaries.
585 camel_folder_search_search (CamelFolderSearch *search,
591 GPtrArray *matches = NULL, *summary_set;
594 gchar *sql_query, *tmp, *tmp1;
597 CamelFolderSearchPrivate *p = search->priv;
599 g_assert (search->folder);
603 /* We route body-contains / thread based search and uid search through memory and not via db. */
604 if (uids || do_search_in_memory (expr)) {
605 /* setup our search list only contains those we're interested in */
606 search->summary = camel_folder_get_summary (search->folder);
609 GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
611 summary_set = search->summary_set = g_ptr_array_new ();
612 for (i = 0; i < uids->len; i++)
613 g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
614 for (i = 0; i < search->summary->len; i++)
615 if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
616 g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
617 g_hash_table_destroy (uids_hash);
619 summary_set = search->summary;
622 /* only re-parse if the search has changed */
623 if (search->last_search == NULL
624 || strcmp (search->last_search, expr)) {
625 camel_sexp_input_text (search->sexp, expr, strlen (expr));
626 if (camel_sexp_parse (search->sexp) == -1) {
628 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
629 _("Cannot parse search expression: %s:\n%s"),
630 camel_sexp_error (search->sexp), expr);
634 g_free (search->last_search);
635 search->last_search = g_strdup (expr);
637 r = camel_sexp_eval (search->sexp);
640 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
641 _("Error executing search expression: %s:\n%s"),
642 camel_sexp_error (search->sexp), expr);
646 matches = g_ptr_array_new ();
648 /* now create a folder summary to return?? */
649 if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
650 d(printf("got result\n"));
652 /* reorder result in summary order */
653 results = g_hash_table_new (g_str_hash, g_str_equal);
654 for (i = 0; i < r->value.ptrarray->len; i++) {
655 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
656 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
659 for (i = 0; i < summary_set->len; i++) {
660 gchar *uid = g_ptr_array_index (summary_set, i);
661 if (g_hash_table_lookup (results, uid))
662 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
664 g_hash_table_destroy (results);
667 camel_sexp_result_free (search->sexp, r);
670 CamelStore *parent_store;
671 const gchar *full_name;
672 GError *local_error = NULL;
674 full_name = camel_folder_get_full_name (search->folder);
675 parent_store = camel_folder_get_parent_store (search->folder);
677 /* Sync the db, so that we search the db for changes */
678 camel_folder_summary_save_to_db (search->folder->summary, error);
680 dd(printf ("sexp is : [%s]\n", expr));
681 if (g_getenv("SQL_SEARCH_OLD"))
682 sql_query = camel_sexp_to_sql (expr);
684 sql_query = camel_sexp_to_sql_sexp (expr);
685 tmp1 = camel_db_sqlize_string (full_name);
686 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
687 camel_db_free_sqlized_string (tmp1);
689 dd(printf("Equivalent sql %s\n", tmp));
691 matches = g_ptr_array_new ();
692 cdb = (CamelDB *) (parent_store->cdb_r);
694 cdb, tmp, (CamelDBSelectCB)
695 read_uid_callback, matches, &local_error);
696 if (local_error != NULL) {
697 const gchar *message = local_error->message;
698 if (strncmp(message, "no such table", 13) == 0) {
699 d(g_warning ("Error during searching %s: %s\n", tmp, message));
700 /* Suppress no such table */
701 g_clear_error (&local_error);
703 g_propagate_error (error, local_error);
710 /* these might be allocated by match-threads */
712 camel_folder_thread_messages_unref (p->threads);
714 g_hash_table_destroy (p->threads_hash);
715 if (search->summary_set)
716 g_ptr_array_free (search->summary_set, TRUE);
718 camel_folder_free_summary (search->folder, search->summary);
721 p->threads_hash = NULL;
722 search->folder = NULL;
723 search->summary = NULL;
724 search->summary_set = NULL;
725 search->current = NULL;
726 search->body_index = NULL;
731 void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *result)
733 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
734 g_ptr_array_free (result, TRUE);
737 /* dummy function, returns false always, or an empty match array */
738 static CamelSExpResult *
739 search_dummy (struct _CamelSExp *f,
741 struct _CamelSExpResult **argv,
742 CamelFolderSearch *search)
746 if (search->current == NULL) {
747 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
748 r->value.boolean = FALSE;
750 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
751 r->value.ptrarray = g_ptr_array_new ();
757 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
758 static CamelSExpResult *
759 search_not (struct _CamelSExp *f,
761 struct _CamelSExpResult **argv,
762 CamelFolderSearch *search)
768 if (argv[0]->type == CAMEL_SEXP_RES_ARRAY_PTR) {
769 GPtrArray *v = argv[0]->value.ptrarray;
772 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
773 r->value.ptrarray = g_ptr_array_new ();
775 /* not against a single message?*/
776 if (search->current) {
779 uid = camel_message_info_uid (search->current);
780 for (i = 0; !found && i < v->len; i++) {
781 if (strcmp (uid, v->pdata[i]) == 0)
786 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
787 } else if (search->summary == NULL) {
788 g_warning("No summary set, 'not' against an array requires a summary");
790 /* 'not' against the whole summary */
791 GHashTable *have = g_hash_table_new (g_str_hash, g_str_equal);
795 s = (gchar **) v->pdata;
796 for (i = 0; i < v->len; i++)
797 g_hash_table_insert (have, s[i], s[i]);
799 v = search->summary_set ? search->summary_set : search->summary;
800 m = (gchar **) v->pdata;
801 for (i = 0; i < v->len; i++) {
804 if (g_hash_table_lookup (have, uid) == NULL)
805 g_ptr_array_add (r->value.ptrarray, uid);
807 g_hash_table_destroy (have);
812 if (argv[0]->type == CAMEL_SEXP_RES_BOOL)
813 res = !argv[0]->value.boolean;
815 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
816 r->value.boolean = res;
819 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
820 r->value.boolean = TRUE;
826 static CamelSExpResult *
827 search_match_all (struct _CamelSExp *f,
829 struct _CamelSExpTerm **argv,
830 CamelFolderSearch *search)
833 CamelSExpResult *r, *r1;
838 g_warning("match-all only takes a single argument, other arguments ignored");
841 /* we are only matching a single message? or already inside a match-all? */
842 if (search->current) {
843 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
845 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
846 r->value.boolean = FALSE;
849 r1 = camel_sexp_term_eval (f, argv[0]);
850 if (r1->type == CAMEL_SEXP_RES_BOOL) {
851 r->value.boolean = r1->value.boolean;
853 g_warning("invalid syntax, matches require a single bool result");
854 /* Translators: The '%s' is an element type name, part of an expressing language */
855 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
856 camel_sexp_fatal_error (f, error_msg);
859 camel_sexp_result_free (f, r1);
861 r->value.boolean = TRUE;
866 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
867 r->value.ptrarray = g_ptr_array_new ();
869 if (search->summary == NULL) {
870 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
871 g_warning("No summary supplied, match-all doesn't work with no summary");
876 v = search->summary_set ? search->summary_set : search->summary;
878 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
879 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
882 for (i = 0; i < v->len; i++) {
885 search->current = camel_folder_summary_get (search->folder->summary, v->pdata[i]);
886 if (!search->current)
888 uid = camel_message_info_uid (search->current);
891 r1 = camel_sexp_term_eval (f, argv[0]);
892 if (r1->type == CAMEL_SEXP_RES_BOOL) {
893 if (r1->value.boolean)
894 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
896 g_warning("invalid syntax, matches require a single bool result");
897 /* Translators: The '%s' is an element type name, part of an expressing language */
898 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
899 camel_sexp_fatal_error (f, error_msg);
902 camel_sexp_result_free (f, r1);
904 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
906 camel_message_info_free (search->current);
908 search->current = NULL;
913 fill_thread_table (struct _CamelFolderThreadNode *root,
917 g_hash_table_insert (id_hash, (gchar *) camel_message_info_uid (root->message), root);
919 fill_thread_table (root->child, id_hash);
925 add_thread_results (struct _CamelFolderThreadNode *root,
926 GHashTable *result_hash)
929 g_hash_table_insert (result_hash, (gchar *) camel_message_info_uid (root->message), GINT_TO_POINTER (1));
931 add_thread_results (root->child, result_hash);
937 add_results (gchar *uid,
941 g_ptr_array_add (result, uid);
944 static CamelSExpResult *
945 search_match_threads (struct _CamelSExp *f,
947 struct _CamelSExpTerm **argv,
948 CamelFolderSearch *search)
951 CamelFolderSearchPrivate *p = search->priv;
956 /* not supported in match-all */
957 if (search->current) {
958 /* Translators: Each '%s' is an element type name, part of an expressing language */
959 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
960 camel_sexp_fatal_error (f, error_msg);
965 /* Translators: The '%s' is an element type name, part of an expressing language */
966 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
967 camel_sexp_fatal_error (f, error_msg);
971 r = camel_sexp_term_eval (f, argv[0]);
972 if (r->type != CAMEL_SEXP_RES_STRING) {
973 /* Translators: The '%s' is an element type name, part of an expressing language */
974 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
975 camel_sexp_fatal_error (f, error_msg);
980 if (!strcmp(r->value.string, "none"))
982 else if (!strcmp(r->value.string, "all"))
984 else if (!strcmp(r->value.string, "replies"))
986 else if (!strcmp(r->value.string, "replies_parents"))
988 else if (!strcmp(r->value.string, "single"))
990 camel_sexp_result_free (f, r);
992 /* behave as (begin does */
994 for (i = 1; i < argc; i++) {
996 camel_sexp_result_free (f, r);
997 r = camel_sexp_term_eval (f, argv[i]);
1000 if (r == NULL || r->type != CAMEL_SEXP_RES_ARRAY_PTR) {
1001 /* Translators: The '%s' is an element type name, part of an expressing language */
1002 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
1003 camel_sexp_fatal_error (f, error_msg);
1010 if (search->folder == NULL) {
1011 /* Translators: The '%s' is an element type name, part of an expressing language */
1012 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
1013 camel_sexp_fatal_error (f, error_msg);
1017 /* cache this, so we only have to re-calculate once per search at most */
1018 if (p->threads == NULL) {
1019 p->threads = camel_folder_thread_messages_new (search->folder, NULL, TRUE);
1020 p->threads_hash = g_hash_table_new (g_str_hash, g_str_equal);
1022 fill_thread_table (p->threads->tree, p->threads_hash);
1025 results = g_hash_table_new (g_str_hash, g_str_equal);
1026 for (i = 0; i < r->value.ptrarray->len; i++) {
1027 struct _CamelFolderThreadNode *node, *scan;
1030 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
1032 node = g_hash_table_lookup (p->threads_hash, (gchar *) g_ptr_array_index (r->value.ptrarray, i));
1033 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
1036 /* select messages in thread according to search criteria */
1038 if (node->child == NULL && node->parent == NULL)
1039 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1043 while (scan && scan->parent) {
1044 scan = scan->parent;
1045 g_hash_table_insert (results, (gchar *) camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
1047 } else if (type == 1) {
1048 while (node && node->parent)
1049 node = node->parent;
1051 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1053 add_thread_results (node->child, results);
1056 camel_sexp_result_free (f, r);
1058 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1059 r->value.ptrarray = g_ptr_array_new ();
1061 g_hash_table_foreach (results, (GHFunc) add_results, r->value.ptrarray);
1062 g_hash_table_destroy (results);
1067 static CamelMimeMessage *
1068 get_current_message (CamelFolderSearch *search)
1070 if (!search || !search->folder || !search->current)
1073 /* FIXME Pass a GCancellable */
1074 return camel_folder_get_message_sync (
1075 search->folder, search->current->uid, NULL, NULL);
1078 static CamelSExpResult *
1079 check_header (struct _CamelSExp *f,
1081 struct _CamelSExpResult **argv,
1082 CamelFolderSearch *search,
1083 camel_search_match_t how)
1088 r(printf("executing check-header %d\n", how));
1090 /* are we inside a match-all? */
1091 if (search->current && argc > 1
1092 && argv[0]->type == CAMEL_SEXP_RES_STRING) {
1094 const gchar *header = NULL, *charset = NULL;
1097 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1098 struct _camel_search_words *words;
1099 CamelMimeMessage *message = NULL;
1100 struct _camel_header_raw *raw_header;
1102 /* only a subset of headers are supported .. */
1103 headername = argv[0]->value.string;
1104 if (!g_ascii_strcasecmp(headername, "subject")) {
1105 header = camel_message_info_subject (search->current);
1106 } else if (!g_ascii_strcasecmp(headername, "date")) {
1107 /* FIXME: not a very useful form of the date */
1108 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1110 } else if (!g_ascii_strcasecmp(headername, "from")) {
1111 header = camel_message_info_from (search->current);
1112 type = CAMEL_SEARCH_TYPE_ADDRESS;
1113 } else if (!g_ascii_strcasecmp(headername, "to")) {
1114 header = camel_message_info_to (search->current);
1115 type = CAMEL_SEARCH_TYPE_ADDRESS;
1116 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1117 header = camel_message_info_cc (search->current);
1118 type = CAMEL_SEARCH_TYPE_ADDRESS;
1119 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1120 header = camel_message_info_mlist (search->current);
1121 type = CAMEL_SEARCH_TYPE_MLIST;
1123 message = get_current_message (search);
1125 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1128 charset = camel_content_type_param (ct, "charset");
1129 charset = camel_iconv_charset_name (charset);
1137 /* performs an OR of all words */
1138 for (i = 1; i < argc && !truth; i++) {
1139 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1140 if (argv[i]->value.string[0] == 0) {
1142 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1143 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1144 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1146 for (j = 0; j < words->len && truth; j++) {
1148 for (raw_header = ((CamelMimePart *) message)->headers; raw_header; raw_header = raw_header->next) {
1149 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1150 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1155 truth = raw_header != NULL;
1157 truth = camel_search_header_match (header, words->words[j]->word, how, type, charset);
1159 camel_search_words_free (words);
1162 for (raw_header = ((CamelMimePart *) message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1163 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1164 truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
1168 truth = camel_search_header_match (header, argv[i]->value.string, how, type, charset);
1174 g_object_unref (message);
1176 /* TODO: else, find all matches */
1178 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1179 r->value.boolean = truth;
1186 l_printf (gchar *node)
1188 printf("%s\t", node);
1192 static CamelSExpResult *
1193 search_header_contains (struct _CamelSExp *f,
1195 struct _CamelSExpResult **argv,
1196 CamelFolderSearch *search)
1198 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1201 static CamelSExpResult *
1202 search_header_matches (struct _CamelSExp *f,
1204 struct _CamelSExpResult **argv,
1205 CamelFolderSearch *search)
1207 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1210 static CamelSExpResult *
1211 search_header_starts_with (struct _CamelSExp *f,
1213 struct _CamelSExpResult **argv,
1214 CamelFolderSearch *search)
1216 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1219 static CamelSExpResult *
1220 search_header_ends_with (struct _CamelSExp *f,
1222 struct _CamelSExpResult **argv,
1223 CamelFolderSearch *search)
1225 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1228 static CamelSExpResult *
1229 search_header_exists (struct _CamelSExp *f,
1231 struct _CamelSExpResult **argv,
1232 CamelFolderSearch *search)
1236 r(printf ("executing header-exists\n"));
1238 if (search->current) {
1239 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1240 if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING)
1241 r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
1244 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1245 r->value.ptrarray = g_ptr_array_new ();
1251 static CamelSExpResult *
1252 search_header_soundex (struct _CamelSExp *f,
1254 struct _CamelSExpResult **argv,
1255 CamelFolderSearch *search)
1257 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1260 static CamelSExpResult *
1261 search_header_regex (struct _CamelSExp *f,
1263 struct _CamelSExpResult **argv,
1264 CamelFolderSearch *search)
1267 CamelMimeMessage *msg;
1269 msg = get_current_message (search);
1273 const gchar *contents;
1275 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1277 if (argc > 1 && argv[0]->type == CAMEL_SEXP_RES_STRING
1278 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1279 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_ICASE, argc - 1, argv + 1, search->priv->error) == 0) {
1280 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1283 r->value.boolean = FALSE;
1285 g_object_unref (msg);
1287 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1288 r->value.ptrarray = g_ptr_array_new ();
1295 get_full_header (CamelMimeMessage *message)
1297 CamelMimePart *mp = CAMEL_MIME_PART (message);
1298 GString *str = g_string_new ("");
1299 struct _camel_header_raw *h;
1301 for (h = mp->headers; h; h = h->next) {
1302 if (h->value != NULL) {
1303 g_string_append (str, h->name);
1304 if (isspace (h->value[0]))
1305 g_string_append (str, ":");
1307 g_string_append (str, ": ");
1308 g_string_append (str, h->value);
1309 g_string_append_c (str, '\n');
1313 return g_string_free (str, FALSE);
1316 static CamelSExpResult *
1317 search_header_full_regex (struct _CamelSExp *f,
1319 struct _CamelSExpResult **argv,
1320 CamelFolderSearch *search)
1323 CamelMimeMessage *msg;
1325 msg = get_current_message (search);
1330 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1332 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) {
1335 contents = get_full_header (msg);
1336 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1341 r->value.boolean = FALSE;
1343 g_object_unref (msg);
1345 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1346 r->value.ptrarray = g_ptr_array_new ();
1352 /* this is just to OR results together */
1358 /* or, store all unique values */
1362 struct IterData *iter_data)
1364 g_ptr_array_add (iter_data->uids, key);
1367 /* and, only store duplicates */
1371 struct IterData *iter_data)
1373 if (value == iter_data->count)
1374 g_ptr_array_add (iter_data->uids, key);
1378 match_message_index (CamelIndex *idx,
1383 CamelIndexCursor *wc, *nc;
1384 const gchar *word, *name;
1387 wc = camel_index_words (idx);
1389 while (!truth && (word = camel_index_cursor_next (wc))) {
1390 if (camel_ustrstrcase (word,match) != NULL) {
1391 /* perf: could have the wc cursor return the name cursor */
1392 nc = camel_index_find (idx, word);
1394 while (!truth && (name = camel_index_cursor_next (nc)))
1395 truth = strcmp (name, uid) == 0;
1396 g_object_unref (nc);
1400 g_object_unref (wc);
1407 "one two" "three" "four five"
1416 /* returns messages which contain all words listed in words */
1418 match_words_index (CamelFolderSearch *search,
1419 struct _camel_search_words *words,
1422 GPtrArray *result = g_ptr_array_new ();
1423 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1424 struct IterData lambdafoo;
1425 CamelIndexCursor *wc, *nc;
1426 const gchar *word, *name;
1429 /* we can have a maximum of 32 words, as we use it as the AND mask */
1431 wc = camel_index_words (search->body_index);
1433 while ((word = camel_index_cursor_next (wc))) {
1434 for (i = 0; i < words->len; i++) {
1435 if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
1436 /* perf: could have the wc cursor return the name cursor */
1437 nc = camel_index_find (search->body_index, word);
1439 while ((name = camel_index_cursor_next (nc))) {
1442 mask = (GPOINTER_TO_INT (g_hash_table_lookup (ht, name))) | (1 << i);
1443 g_hash_table_insert (ht, (gchar *) camel_pstring_peek (name), GINT_TO_POINTER (mask));
1445 g_object_unref (nc);
1450 g_object_unref (wc);
1452 lambdafoo.uids = result;
1453 lambdafoo.count = (1 << words->len) - 1;
1454 g_hash_table_foreach (ht, (GHFunc) htand, &lambdafoo);
1455 g_hash_table_destroy (ht);
1462 match_words_1message (CamelDataWrapper *object,
1463 struct _camel_search_words *words,
1466 CamelDataWrapper *containee;
1470 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1472 if (containee == NULL)
1475 /* using the object types is more accurate than using the mime/types */
1476 if (CAMEL_IS_MULTIPART (containee)) {
1477 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1478 for (i = 0; i < parts && truth == FALSE; i++) {
1479 CamelDataWrapper *part = (CamelDataWrapper *) camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1481 truth = match_words_1message (part, words, mask);
1483 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1484 /* for messages we only look at its contents */
1485 truth = match_words_1message ((CamelDataWrapper *) containee, words, mask);
1486 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1487 /* for all other text parts, we look inside, otherwise we dont care */
1488 CamelStream *stream;
1489 GByteArray *byte_array;
1491 byte_array = g_byte_array_new ();
1492 stream = camel_stream_mem_new_with_byte_array (byte_array);
1494 /* FIXME The match should be part of a stream op */
1495 /* FIXME Pass a GCancellable and GError here. */
1496 camel_data_wrapper_decode_to_stream_sync (
1497 containee, stream, NULL, NULL);
1498 camel_stream_write (stream, "", 1, NULL, NULL);
1499 for (i = 0; i < words->len; i++) {
1500 /* FIXME: This is horridly slow, and should use a real search algorithm */
1501 if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1503 /* shortcut a match */
1504 if (*mask == (1 << (words->len)) - 1)
1509 g_object_unref (stream);
1516 match_words_message (CamelFolder *folder,
1518 struct _camel_search_words *words,
1519 GCancellable *cancellable,
1523 CamelMimeMessage *msg;
1526 msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
1529 truth = match_words_1message ((CamelDataWrapper *) msg, words, &mask);
1530 g_object_unref (msg);
1537 match_words_messages (CamelFolderSearch *search,
1538 struct _camel_search_words *words,
1539 GCancellable *cancellable,
1543 GPtrArray *matches = g_ptr_array_new ();
1545 if (search->body_index) {
1547 struct _camel_search_words *simple;
1549 simple = camel_search_words_simple (words);
1550 indexed = match_words_index (search, simple, error);
1551 camel_search_words_free (simple);
1553 for (i = 0; i < indexed->len; i++) {
1554 const gchar *uid = g_ptr_array_index (indexed, i);
1556 if (match_words_message (
1557 search->folder, uid, words,
1558 cancellable, error))
1559 g_ptr_array_add (matches, (gchar *) uid);
1562 g_ptr_array_free (indexed, TRUE);
1564 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1566 for (i = 0; i < v->len; i++) {
1567 gchar *uid = g_ptr_array_index (v, i);
1569 if (match_words_message (
1570 search->folder, uid, words,
1571 cancellable, error))
1572 g_ptr_array_add (matches, (gchar *) uid);
1579 static CamelSExpResult *
1580 search_body_contains (struct _CamelSExp *f,
1582 struct _CamelSExpResult **argv,
1583 CamelFolderSearch *search)
1586 GError **error = search->priv->error;
1587 struct _camel_search_words *words;
1589 struct IterData lambdafoo;
1591 if (search->current) {
1594 if (argc == 1 && argv[0]->value.string[0] == 0) {
1597 for (i = 0; i < argc && !truth; i++) {
1598 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1599 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1601 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1602 for (j = 0; j < words->len && truth; j++)
1603 truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
1605 /* TODO: cache current message incase of multiple body search terms */
1606 /* FIXME Pass a GCancellable */
1607 truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
1609 camel_search_words_free (words);
1613 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1614 r->value.boolean = truth;
1616 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1617 r->value.ptrarray = g_ptr_array_new ();
1619 if (argc == 1 && argv[0]->value.string[0] == 0) {
1620 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1622 for (i = 0; i < v->len; i++) {
1623 gchar *uid = g_ptr_array_index (v, i);
1625 g_ptr_array_add (r->value.ptrarray, uid);
1628 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1631 for (i = 0; i < argc; i++) {
1632 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1633 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1634 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1635 matches = match_words_index (search, words, error);
1637 /* FIXME Pass a GCancellable */
1638 matches = match_words_messages (search, words, NULL, error);
1640 for (j = 0; j < matches->len; j++) {
1641 g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
1643 g_ptr_array_free (matches, TRUE);
1644 camel_search_words_free (words);
1647 lambdafoo.uids = r->value.ptrarray;
1648 g_hash_table_foreach (ht, (GHFunc) htor, &lambdafoo);
1649 g_hash_table_destroy (ht);
1656 static CamelSExpResult *
1657 search_body_regex (struct _CamelSExp *f,
1659 struct _CamelSExpResult **argv,
1660 CamelFolderSearch *search)
1663 CamelMimeMessage *msg = get_current_message (search);
1668 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1670 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) {
1671 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1674 r->value.boolean = FALSE;
1676 g_object_unref (msg);
1680 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1681 r->value.ptrarray = g_ptr_array_new ();
1683 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) {
1685 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1686 CamelMimeMessage *message;
1688 for (i = 0; i < v->len; i++) {
1689 gchar *uid = g_ptr_array_index (v, i);
1691 /* FIXME Pass a GCancellable */
1692 message = camel_folder_get_message_sync (
1693 search->folder, uid, NULL, NULL);
1695 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1696 g_ptr_array_add (r->value.ptrarray, uid);
1699 g_object_unref (message);
1710 static CamelSExpResult *
1711 search_user_flag (struct _CamelSExp *f,
1713 struct _CamelSExpResult **argv,
1714 CamelFolderSearch *search)
1719 r(printf("executing user-flag\n"));
1721 /* are we inside a match-all? */
1722 if (search->current) {
1724 /* performs an OR of all words */
1725 for (i = 0; i < argc && !truth; i++) {
1726 if (argv[i]->type == CAMEL_SEXP_RES_STRING
1727 && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
1732 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1733 r->value.boolean = truth;
1735 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1736 r->value.ptrarray = g_ptr_array_new ();
1742 static CamelSExpResult *
1743 search_system_flag (struct _CamelSExp *f,
1745 struct _CamelSExpResult **argv,
1746 CamelFolderSearch *search)
1750 r(printf ("executing system-flag\n"));
1752 if (search->current) {
1753 gboolean truth = FALSE;
1756 truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
1758 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1759 r->value.boolean = truth;
1761 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1762 r->value.ptrarray = g_ptr_array_new ();
1768 static CamelSExpResult *
1769 search_user_tag (struct _CamelSExp *f,
1771 struct _CamelSExpResult **argv,
1772 CamelFolderSearch *search)
1774 const gchar *value = NULL;
1777 r(printf("executing user-tag\n"));
1779 if (search->current && argc == 1)
1780 value = camel_message_info_user_tag (search->current, argv[0]->value.string);
1782 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_STRING);
1783 r->value.string = g_strdup (value ? value : "");
1788 static CamelSExpResult *
1789 search_get_sent_date (struct _CamelSExp *f,
1791 struct _CamelSExpResult **argv,
1792 CamelFolderSearch *s)
1796 r(printf("executing get-sent-date\n"));
1798 /* are we inside a match-all? */
1800 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1802 r->value.number = camel_message_info_date_sent (s->current);
1804 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1805 r->value.ptrarray = g_ptr_array_new ();
1811 static CamelSExpResult *
1812 search_get_received_date (struct _CamelSExp *f,
1814 struct _CamelSExpResult **argv,
1815 CamelFolderSearch *s)
1819 r(printf("executing get-received-date\n"));
1821 /* are we inside a match-all? */
1823 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1825 r->value.number = camel_message_info_date_received (s->current);
1827 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1828 r->value.ptrarray = g_ptr_array_new ();
1834 static CamelSExpResult *
1835 search_get_current_date (struct _CamelSExp *f,
1837 struct _CamelSExpResult **argv,
1838 CamelFolderSearch *s)
1842 r(printf("executing get-current-date\n"));
1844 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1845 r->value.number = time (NULL);
1849 static CamelSExpResult *
1850 search_get_relative_months (struct _CamelSExp *f,
1852 struct _CamelSExpResult **argv,
1853 CamelFolderSearch *s)
1857 r(printf("executing get-relative-months\n"));
1859 if (argc != 1 || argv[0]->type != CAMEL_SEXP_RES_INT) {
1860 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1861 r->value.boolean = FALSE;
1863 g_debug ("%s: Expecting 1 argument, an integer, but got %d arguments", G_STRFUNC, argc);
1865 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1866 r->value.number = camel_folder_search_util_add_months (time (NULL), argv[0]->value.number);
1872 static CamelSExpResult *
1873 search_get_size (struct _CamelSExp *f,
1875 struct _CamelSExpResult **argv,
1876 CamelFolderSearch *s)
1880 r(printf("executing get-size\n"));
1882 /* are we inside a match-all? */
1884 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1885 r->value.number = camel_message_info_size (s->current) / 1024;
1887 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1888 r->value.ptrarray = g_ptr_array_new ();
1894 static CamelSExpResult *
1895 search_uid (struct _CamelSExp *f,
1897 struct _CamelSExpResult **argv,
1898 CamelFolderSearch *search)
1903 r(printf("executing uid\n"));
1905 /* are we inside a match-all? */
1906 if (search->current) {
1908 const gchar *uid = camel_message_info_uid (search->current);
1910 /* performs an OR of all words */
1911 for (i = 0; i < argc && !truth; i++) {
1912 if (argv[i]->type == CAMEL_SEXP_RES_STRING
1913 && !strcmp (uid, argv[i]->value.string)) {
1918 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1919 r->value.boolean = truth;
1921 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1922 r->value.ptrarray = g_ptr_array_new ();
1923 for (i = 0; i < argc; i++) {
1924 if (argv[i]->type == CAMEL_SEXP_RES_STRING)
1925 g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
1933 read_uid_callback (gpointer ref,
1940 matches = (GPtrArray *) ref;
1942 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols[0]));
1946 static CamelSExpResult *
1947 search_message_location (struct _CamelSExp *f,
1949 struct _CamelSExpResult **argv,
1950 CamelFolderSearch *search)
1953 gboolean same = FALSE;
1955 if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING) {
1956 if (argv[0]->value.string && search->folder) {
1962 /* FIXME Folder URI formats are Evolution-specific
1963 * knowledge and doesn't belong here! */
1964 store = camel_folder_get_parent_store (search->folder);
1965 name = camel_folder_get_full_name (search->folder);
1966 uid = camel_service_get_uid (CAMEL_SERVICE (store));
1968 uri = g_strdup_printf ("folder://%s/%s", uid, name);
1969 same = g_str_equal (uri, argv[0]->value.string);
1974 if (search->current) {
1975 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1976 r->value.boolean = same ? TRUE : FALSE;
1978 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1979 r->value.ptrarray = g_ptr_array_new ();
1984 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1986 for (i = 0; i < v->len; i++) {
1987 gchar *uid = g_ptr_array_index (v, i);
1989 g_ptr_array_add (r->value.ptrarray, uid);
1998 * camel_folder_search_util_add_months:
2000 * @months: number of months to add or subtract
2002 * Increases time @t by the given number of months (or decreases, if
2003 * @months is negative).
2005 * Returns: a new #time_t value
2010 camel_folder_search_util_add_months (time_t t,
2013 GDateTime *dt, *dt2;
2019 dt = g_date_time_new_from_unix_utc (t);
2021 /* just for issues, to return something inaccurate, but sane */
2022 res = t + (60 * 60 * 24 * 30 * months);
2024 g_return_val_if_fail (dt != NULL, res);
2026 dt2 = g_date_time_add_months (dt, months);
2027 g_date_time_unref (dt);
2028 g_return_val_if_fail (dt2 != NULL, res);
2030 res = g_date_time_to_unix (dt2);
2031 g_date_time_unref (dt2);