1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 /* This is a helper class for folders to implement the search function.
23 * It implements enough to do basic searches on folders that can provide
24 * an in-memory summary and a body index. */
30 /* POSIX requires <sys/types.h> be included before <regex.h> */
31 #include <sys/types.h>
38 #include <glib/gi18n-lib.h>
40 #include "camel-folder-search.h"
41 #include "camel-folder-thread.h"
42 #include "camel-iconv.h"
43 #include "camel-medium.h"
44 #include "camel-mime-message.h"
45 #include "camel-multipart.h"
46 #include "camel-search-private.h"
47 #include "camel-stream-mem.h"
49 #include "camel-debug.h"
50 #include "camel-store.h"
51 #include "camel-vee-folder.h"
52 #include "camel-string-utils.h"
53 #include "camel-search-sql.h"
54 #include "camel-search-sql-sexp.h"
58 #define dd(x) if (camel_debug("search")) x
60 #define CAMEL_FOLDER_SEARCH_GET_PRIVATE(obj) \
61 (G_TYPE_INSTANCE_GET_PRIVATE \
62 ((obj), CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate))
64 struct _CamelFolderSearchPrivate {
67 CamelFolderThread *threads;
68 GHashTable *threads_hash;
71 static CamelSExpResult *search_not (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
73 static CamelSExpResult *search_header_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
74 static CamelSExpResult *search_header_matches (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
75 static CamelSExpResult *search_header_starts_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
76 static CamelSExpResult *search_header_ends_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
77 static CamelSExpResult *search_header_exists (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
78 static CamelSExpResult *search_header_soundex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
79 static CamelSExpResult *search_header_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
80 static CamelSExpResult *search_header_full_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
81 static CamelSExpResult *search_match_all (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *search);
82 static CamelSExpResult *search_match_threads (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *s);
83 static CamelSExpResult *search_body_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
84 static CamelSExpResult *search_body_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
85 static CamelSExpResult *search_user_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
86 static CamelSExpResult *search_user_tag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
87 static CamelSExpResult *search_system_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
88 static CamelSExpResult *search_get_sent_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
89 static CamelSExpResult *search_get_received_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
90 static CamelSExpResult *search_get_current_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
91 static CamelSExpResult *search_get_relative_months (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
92 static CamelSExpResult *search_get_size (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
93 static CamelSExpResult *search_uid (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
94 static CamelSExpResult *search_message_location (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
96 static CamelSExpResult *search_dummy (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
98 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
100 G_DEFINE_TYPE (CamelFolderSearch, camel_folder_search, CAMEL_TYPE_OBJECT)
103 folder_search_dispose (GObject *object)
105 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
107 if (search->sexp != NULL) {
108 g_object_unref (search->sexp);
112 /* Chain up to parent's dispose() method. */
113 G_OBJECT_CLASS (camel_folder_search_parent_class)->dispose (object);
117 folder_search_finalize (GObject *object)
119 CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
121 g_free (search->last_search);
123 /* Chain up to parent's finalize() method. */
124 G_OBJECT_CLASS (camel_folder_search_parent_class)->finalize (object);
128 camel_folder_search_class_init (CamelFolderSearchClass *class)
130 GObjectClass *object_class;
132 g_type_class_add_private (class, sizeof (CamelFolderSearchPrivate));
134 object_class = G_OBJECT_CLASS (class);
135 object_class->dispose = folder_search_dispose;
136 object_class->finalize = folder_search_finalize;
138 class->not = search_not;
139 class->match_all = search_match_all;
140 class->match_threads = search_match_threads;
141 class->body_contains = search_body_contains;
142 class->body_regex = search_body_regex;
143 class->header_contains = search_header_contains;
144 class->header_matches = search_header_matches;
145 class->header_starts_with = search_header_starts_with;
146 class->header_ends_with = search_header_ends_with;
147 class->header_exists = search_header_exists;
148 class->header_soundex = search_header_soundex;
149 class->header_regex = search_header_regex;
150 class->header_full_regex = search_header_full_regex;
151 class->user_tag = search_user_tag;
152 class->user_flag = search_user_flag;
153 class->system_flag = search_system_flag;
154 class->get_sent_date = search_get_sent_date;
155 class->get_received_date = search_get_received_date;
156 class->get_current_date = search_get_current_date;
157 class->get_relative_months = search_get_relative_months;
158 class->get_size = search_get_size;
159 class->uid = search_uid;
160 class->message_location = search_message_location;
164 camel_folder_search_init (CamelFolderSearch *search)
166 search->priv = CAMEL_FOLDER_SEARCH_GET_PRIVATE (search);
167 search->sexp = camel_sexp_new ();
173 gint flags; /* 0x02 = immediate, 0x01 = always enter */
175 /* these have default implementations in e-sexp */
176 { "and", G_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
177 { "or", G_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
178 /* we need to override this one though to implement an 'array not' */
179 { "not", G_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
180 { "<", G_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
181 { ">", G_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
182 { "=", G_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
184 /* these we have to use our own default if there is none */
185 /* they should all be defined in the language? so it parses, or should they not?? */
186 { "match-all", G_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
187 { "match-threads", G_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
188 { "body-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
189 { "body-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
190 { "header-contains", G_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
191 { "header-matches", G_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
192 { "header-starts-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
193 { "header-ends-with", G_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
194 { "header-exists", G_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
195 { "header-soundex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
196 { "header-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
197 { "header-full-regex", G_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
198 { "user-tag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
199 { "user-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
200 { "system-flag", G_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
201 { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
202 { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
203 { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
204 { "get-relative-months", G_STRUCT_OFFSET(CamelFolderSearchClass, get_relative_months), 1 },
205 { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
206 { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
207 { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
211 camel_folder_search_construct (CamelFolderSearch *search)
214 CamelFolderSearchClass *class;
216 class = CAMEL_FOLDER_SEARCH_GET_CLASS (search);
218 for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
220 /* c is sure messy sometimes */
221 func = *((gpointer *)(((gchar *) class) + builtins[i].offset));
222 if (func == NULL && builtins[i].flags&1) {
223 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, G_OBJECT_TYPE_NAME (search));
224 func = (gpointer) search_dummy;
227 if (builtins[i].flags&2) {
228 camel_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (CamelSExpIFunc) func, search);
230 camel_sexp_add_function (search->sexp, 0, builtins[i].name, (CamelSExpFunc) func, search);
237 * camel_folder_search_new:
239 * Create a new CamelFolderSearch object.
241 * A CamelFolderSearch is a subclassable, extensible s-exp
242 * evaluator which enforces a particular set of s-expressions.
243 * Particular methods may be overriden by an implementation to
244 * implement a search for any sort of backend.
246 * Returns: A new CamelFolderSearch widget.
249 camel_folder_search_new (void)
251 CamelFolderSearch *new;
253 new = g_object_new (CAMEL_TYPE_FOLDER_SEARCH, NULL);
254 camel_folder_search_construct (new);
260 * camel_folder_search_set_folder:
264 * Set the folder attribute of the search. This is currently unused, but
265 * could be used to perform a slow-search when indexes and so forth are not
266 * available. Or for use by subclasses.
269 camel_folder_search_set_folder (CamelFolderSearch *search,
272 g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
273 g_return_if_fail (CAMEL_IS_FOLDER (folder));
275 search->folder = folder;
279 * camel_folder_search_set_summary:
281 * @summary: An array of CamelMessageInfo pointers.
283 * Set the array of summary objects representing the span of the search.
285 * If this is not set, then a subclass must provide the functions
286 * for searching headers and for the match-all operator.
289 camel_folder_search_set_summary (CamelFolderSearch *search,
292 g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
294 search->summary = summary;
298 * camel_folder_search_set_body_index:
302 * Set the index representing the contents of all messages
303 * in this folder. If this is not set, then the folder implementation
304 * should sub-class the CamelFolderSearch and provide its own
305 * body-contains function.
308 camel_folder_search_set_body_index (CamelFolderSearch *search,
309 CamelIndex *body_index)
311 g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
313 if (body_index != NULL) {
314 g_return_if_fail (CAMEL_IS_INDEX (body_index));
315 g_object_ref (body_index);
318 if (search->body_index != NULL)
319 g_object_unref (search->body_index);
321 search->body_index = body_index;
325 * camel_folder_search_execute_expression:
328 * @error: return location for a #GError, or %NULL
330 * Execute the search expression @expr, returning an array of
331 * all matches as a GPtrArray of uid's of matching messages.
333 * Note that any settings such as set_body_index(), set_folder(),
334 * and so on are reset to %NULL once the search has completed.
336 * TODO: The interface should probably return summary items instead
337 * (since they are much more useful to any client).
339 * Returns: A GPtrArray of strings of all matching messages.
340 * This must only be freed by camel_folder_search_free_result.
343 camel_folder_search_execute_expression (CamelFolderSearch *search,
351 CamelFolderSearchPrivate *p;
353 g_return_val_if_fail (search != NULL, NULL);
358 /* only re-parse if the search has changed */
359 if (search->last_search == NULL
360 || strcmp (search->last_search, expr)) {
361 camel_sexp_input_text (search->sexp, expr, strlen (expr));
362 if (camel_sexp_parse (search->sexp) == -1) {
364 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
365 _("Cannot parse search expression: %s:\n%s"),
366 camel_sexp_error (search->sexp), expr);
370 g_free (search->last_search);
371 search->last_search = g_strdup (expr);
373 r = camel_sexp_eval (search->sexp);
376 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
377 _("Error executing search expression: %s:\n%s"),
378 camel_sexp_error (search->sexp), expr);
382 matches = g_ptr_array_new ();
384 /* now create a folder summary to return?? */
385 if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
386 d(printf("got result ...\n"));
387 if (search->summary) {
388 /* reorder result in summary order */
389 results = g_hash_table_new (g_str_hash, g_str_equal);
390 for (i = 0; i < r->value.ptrarray->len; i++) {
391 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
392 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
394 for (i = 0; i < search->summary->len; i++) {
395 gchar *uid = g_ptr_array_index (search->summary, i);
396 if (g_hash_table_lookup (results, uid)) {
397 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
400 g_hash_table_destroy (results);
402 for (i = 0; i < r->value.ptrarray->len; i++) {
403 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
404 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (g_ptr_array_index (r->value.ptrarray, i)));
408 g_warning("Search returned an invalid result type");
411 camel_sexp_result_free (search->sexp, r);
414 camel_folder_thread_messages_unref (p->threads);
416 g_hash_table_destroy (p->threads_hash);
419 p->threads_hash = NULL;
420 search->folder = NULL;
421 search->summary = NULL;
422 search->current = NULL;
423 search->body_index = NULL;
429 * camel_folder_search_count:
432 * @error: return location for a #GError, or %NULL
434 * Run a search. Search must have had Folder already set on it, and
435 * it must implement summaries.
437 * Returns: Number of messages that match the query.
443 camel_folder_search_count (CamelFolderSearch *search,
448 GPtrArray *summary_set;
451 gchar *sql_query, *tmp, *tmp1;
455 CamelFolderSearchPrivate *p;
457 g_return_val_if_fail (search != NULL, 0);
461 g_assert (search->folder);
465 /* We route body-contains search and thread based search through memory and not via db. */
466 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
467 /* setup our search list only contains those we're interested in */
468 search->summary = camel_folder_get_summary (search->folder);
470 summary_set = search->summary;
472 /* only re-parse if the search has changed */
473 if (search->last_search == NULL
474 || strcmp (search->last_search, expr)) {
475 camel_sexp_input_text (search->sexp, expr, strlen (expr));
476 if (camel_sexp_parse (search->sexp) == -1) {
478 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
479 _("Cannot parse search expression: %s:\n%s"),
480 camel_sexp_error (search->sexp), expr);
484 g_free (search->last_search);
485 search->last_search = g_strdup (expr);
487 r = camel_sexp_eval (search->sexp);
490 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
491 _("Error executing search expression: %s:\n%s"),
492 camel_sexp_error (search->sexp), expr);
496 /* now create a folder summary to return?? */
497 if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
498 d(printf("got result\n"));
500 /* reorder result in summary order */
501 results = g_hash_table_new (g_str_hash, g_str_equal);
502 for (i = 0; i < r->value.ptrarray->len; i++) {
503 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
504 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
507 for (i = 0; i < summary_set->len; i++) {
508 gchar *uid = g_ptr_array_index (summary_set, i);
509 if (g_hash_table_lookup (results, uid))
512 g_hash_table_destroy (results);
515 camel_sexp_result_free (search->sexp, r);
518 CamelStore *parent_store;
519 const gchar *full_name;
520 GError *local_error = NULL;
522 full_name = camel_folder_get_full_name (search->folder);
523 parent_store = camel_folder_get_parent_store (search->folder);
525 /* Sync the db, so that we search the db for changes */
526 camel_folder_summary_save_to_db (search->folder->summary, error);
528 dd(printf ("sexp is : [%s]\n", expr));
529 if (g_getenv("SQL_SEARCH_OLD"))
530 sql_query = camel_sexp_to_sql (expr);
532 sql_query = camel_sexp_to_sql_sexp (expr);
533 tmp1 = camel_db_sqlize_string (full_name);
534 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
535 camel_db_free_sqlized_string (tmp1);
537 dd(printf("Equivalent sql %s\n", tmp));
539 cdb = (CamelDB *) (parent_store->cdb_r);
540 camel_db_count_message_info (cdb, tmp, &count, &local_error);
541 if (local_error != NULL) {
542 const gchar *message = local_error->message;
543 if (strncmp(message, "no such table", 13) == 0) {
544 d(g_warning ("Error during searching %s: %s\n", tmp, message));
545 /* Suppress no such table */
546 g_clear_error (&local_error);
548 g_propagate_error (error, local_error);
554 /* these might be allocated by match-threads */
556 camel_folder_thread_messages_unref (p->threads);
558 g_hash_table_destroy (p->threads_hash);
559 if (search->summary_set)
560 g_ptr_array_free (search->summary_set, TRUE);
562 camel_folder_free_summary (search->folder, search->summary);
565 p->threads_hash = NULL;
566 search->folder = NULL;
567 search->summary = NULL;
568 search->summary_set = NULL;
569 search->current = NULL;
570 search->body_index = NULL;
576 do_search_in_memory (const gchar *expr)
578 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
579 const gchar *in_memory_tokens[] = {
595 for (i = 0; in_memory_tokens[i]; i++) {
596 if (strstr (expr, in_memory_tokens[i]))
604 * camel_folder_search_search:
607 * @uids: to search against, NULL for all uid's.
608 * @error: return location for a #GError, or %NULL
610 * Run a search. Search must have had Folder already set on it, and
611 * it must implement summaries.
616 camel_folder_search_search (CamelFolderSearch *search,
622 GPtrArray *matches = NULL, *summary_set;
625 gchar *sql_query, *tmp, *tmp1;
628 CamelFolderSearchPrivate *p;
630 g_return_val_if_fail (search != NULL, NULL);
634 g_assert (search->folder);
638 /* We route body-contains / thread based search and uid search through memory and not via db. */
639 if (uids || do_search_in_memory (expr)) {
640 /* setup our search list only contains those we're interested in */
641 search->summary = camel_folder_get_summary (search->folder);
644 GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
646 summary_set = search->summary_set = g_ptr_array_new ();
647 for (i = 0; i < uids->len; i++)
648 g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
649 for (i = 0; i < search->summary->len; i++)
650 if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
651 g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
652 g_hash_table_destroy (uids_hash);
654 summary_set = search->summary;
657 /* only re-parse if the search has changed */
658 if (search->last_search == NULL
659 || strcmp (search->last_search, expr)) {
660 camel_sexp_input_text (search->sexp, expr, strlen (expr));
661 if (camel_sexp_parse (search->sexp) == -1) {
663 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
664 _("Cannot parse search expression: %s:\n%s"),
665 camel_sexp_error (search->sexp), expr);
669 g_free (search->last_search);
670 search->last_search = g_strdup (expr);
672 r = camel_sexp_eval (search->sexp);
675 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
676 _("Error executing search expression: %s:\n%s"),
677 camel_sexp_error (search->sexp), expr);
681 matches = g_ptr_array_new ();
683 /* now create a folder summary to return?? */
684 if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
685 d(printf("got result\n"));
687 /* reorder result in summary order */
688 results = g_hash_table_new (g_str_hash, g_str_equal);
689 for (i = 0; i < r->value.ptrarray->len; i++) {
690 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
691 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
694 for (i = 0; i < summary_set->len; i++) {
695 gchar *uid = g_ptr_array_index (summary_set, i);
696 if (g_hash_table_lookup (results, uid))
697 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
699 g_hash_table_destroy (results);
702 camel_sexp_result_free (search->sexp, r);
705 CamelStore *parent_store;
706 const gchar *full_name;
707 GError *local_error = NULL;
709 full_name = camel_folder_get_full_name (search->folder);
710 parent_store = camel_folder_get_parent_store (search->folder);
712 /* Sync the db, so that we search the db for changes */
713 camel_folder_summary_save_to_db (search->folder->summary, error);
715 dd(printf ("sexp is : [%s]\n", expr));
716 if (g_getenv("SQL_SEARCH_OLD"))
717 sql_query = camel_sexp_to_sql (expr);
719 sql_query = camel_sexp_to_sql_sexp (expr);
720 tmp1 = camel_db_sqlize_string (full_name);
721 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
722 camel_db_free_sqlized_string (tmp1);
724 dd(printf("Equivalent sql %s\n", tmp));
726 matches = g_ptr_array_new ();
727 cdb = (CamelDB *) (parent_store->cdb_r);
729 cdb, tmp, (CamelDBSelectCB)
730 read_uid_callback, matches, &local_error);
731 if (local_error != NULL) {
732 const gchar *message = local_error->message;
733 if (strncmp(message, "no such table", 13) == 0) {
734 d(g_warning ("Error during searching %s: %s\n", tmp, message));
735 /* Suppress no such table */
736 g_clear_error (&local_error);
738 g_propagate_error (error, local_error);
745 /* these might be allocated by match-threads */
747 camel_folder_thread_messages_unref (p->threads);
749 g_hash_table_destroy (p->threads_hash);
750 if (search->summary_set)
751 g_ptr_array_free (search->summary_set, TRUE);
753 camel_folder_free_summary (search->folder, search->summary);
756 p->threads_hash = NULL;
757 search->folder = NULL;
758 search->summary = NULL;
759 search->summary_set = NULL;
760 search->current = NULL;
761 search->body_index = NULL;
766 void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *result)
768 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
769 g_ptr_array_free (result, TRUE);
772 /* dummy function, returns false always, or an empty match array */
773 static CamelSExpResult *
774 search_dummy (struct _CamelSExp *f,
776 struct _CamelSExpResult **argv,
777 CamelFolderSearch *search)
781 if (search->current == NULL) {
782 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
783 r->value.boolean = FALSE;
785 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
786 r->value.ptrarray = g_ptr_array_new ();
792 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
793 static CamelSExpResult *
794 search_not (struct _CamelSExp *f,
796 struct _CamelSExpResult **argv,
797 CamelFolderSearch *search)
803 if (argv[0]->type == CAMEL_SEXP_RES_ARRAY_PTR) {
804 GPtrArray *v = argv[0]->value.ptrarray;
807 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
808 r->value.ptrarray = g_ptr_array_new ();
810 /* not against a single message?*/
811 if (search->current) {
814 uid = camel_message_info_uid (search->current);
815 for (i = 0; !found && i < v->len; i++) {
816 if (strcmp (uid, v->pdata[i]) == 0)
821 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
822 } else if (search->summary == NULL) {
823 g_warning("No summary set, 'not' against an array requires a summary");
825 /* 'not' against the whole summary */
826 GHashTable *have = g_hash_table_new (g_str_hash, g_str_equal);
830 s = (gchar **) v->pdata;
831 for (i = 0; i < v->len; i++)
832 g_hash_table_insert (have, s[i], s[i]);
834 v = search->summary_set ? search->summary_set : search->summary;
835 m = (gchar **) v->pdata;
836 for (i = 0; i < v->len; i++) {
839 if (g_hash_table_lookup (have, uid) == NULL)
840 g_ptr_array_add (r->value.ptrarray, uid);
842 g_hash_table_destroy (have);
847 if (argv[0]->type == CAMEL_SEXP_RES_BOOL)
848 res = !argv[0]->value.boolean;
850 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
851 r->value.boolean = res;
854 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
855 r->value.boolean = TRUE;
861 static CamelSExpResult *
862 search_match_all (struct _CamelSExp *f,
864 struct _CamelSExpTerm **argv,
865 CamelFolderSearch *search)
868 CamelSExpResult *r, *r1;
873 g_warning("match-all only takes a single argument, other arguments ignored");
876 /* we are only matching a single message? or already inside a match-all? */
877 if (search->current) {
878 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
880 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
881 r->value.boolean = FALSE;
884 r1 = camel_sexp_term_eval (f, argv[0]);
885 if (r1->type == CAMEL_SEXP_RES_BOOL) {
886 r->value.boolean = r1->value.boolean;
888 g_warning("invalid syntax, matches require a single bool result");
889 /* Translators: The '%s' is an element type name, part of an expressing language */
890 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
891 camel_sexp_fatal_error (f, error_msg);
894 camel_sexp_result_free (f, r1);
896 r->value.boolean = TRUE;
901 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
902 r->value.ptrarray = g_ptr_array_new ();
904 if (search->summary == NULL) {
905 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
906 g_warning("No summary supplied, match-all doesn't work with no summary");
911 v = search->summary_set ? search->summary_set : search->summary;
913 if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
914 camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
917 for (i = 0; i < v->len; i++) {
920 search->current = camel_folder_summary_get (search->folder->summary, v->pdata[i]);
921 if (!search->current)
923 uid = camel_message_info_uid (search->current);
926 r1 = camel_sexp_term_eval (f, argv[0]);
927 if (r1->type == CAMEL_SEXP_RES_BOOL) {
928 if (r1->value.boolean)
929 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
931 g_warning("invalid syntax, matches require a single bool result");
932 /* Translators: The '%s' is an element type name, part of an expressing language */
933 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
934 camel_sexp_fatal_error (f, error_msg);
937 camel_sexp_result_free (f, r1);
939 g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
941 camel_message_info_free (search->current);
943 search->current = NULL;
948 fill_thread_table (struct _CamelFolderThreadNode *root,
952 g_hash_table_insert (id_hash, (gchar *) camel_message_info_uid (root->message), root);
954 fill_thread_table (root->child, id_hash);
960 add_thread_results (struct _CamelFolderThreadNode *root,
961 GHashTable *result_hash)
964 g_hash_table_insert (result_hash, (gchar *) camel_message_info_uid (root->message), GINT_TO_POINTER (1));
966 add_thread_results (root->child, result_hash);
972 add_results (gchar *uid,
976 g_ptr_array_add (result, uid);
979 static CamelSExpResult *
980 search_match_threads (struct _CamelSExp *f,
982 struct _CamelSExpTerm **argv,
983 CamelFolderSearch *search)
986 CamelFolderSearchPrivate *p = search->priv;
991 /* not supported in match-all */
992 if (search->current) {
993 /* Translators: Each '%s' is an element type name, part of an expressing language */
994 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
995 camel_sexp_fatal_error (f, error_msg);
1000 /* Translators: The '%s' is an element type name, part of an expressing language */
1001 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
1002 camel_sexp_fatal_error (f, error_msg);
1006 r = camel_sexp_term_eval (f, argv[0]);
1007 if (r->type != CAMEL_SEXP_RES_STRING) {
1008 /* Translators: The '%s' is an element type name, part of an expressing language */
1009 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
1010 camel_sexp_fatal_error (f, error_msg);
1015 if (!strcmp(r->value.string, "none"))
1017 else if (!strcmp(r->value.string, "all"))
1019 else if (!strcmp(r->value.string, "replies"))
1021 else if (!strcmp(r->value.string, "replies_parents"))
1023 else if (!strcmp(r->value.string, "single"))
1025 camel_sexp_result_free (f, r);
1027 /* behave as (begin does */
1029 for (i = 1; i < argc; i++) {
1031 camel_sexp_result_free (f, r);
1032 r = camel_sexp_term_eval (f, argv[i]);
1035 if (r == NULL || r->type != CAMEL_SEXP_RES_ARRAY_PTR) {
1036 /* Translators: The '%s' is an element type name, part of an expressing language */
1037 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
1038 camel_sexp_fatal_error (f, error_msg);
1045 if (search->folder == NULL) {
1046 /* Translators: The '%s' is an element type name, part of an expressing language */
1047 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
1048 camel_sexp_fatal_error (f, error_msg);
1052 /* cache this, so we only have to re-calculate once per search at most */
1053 if (p->threads == NULL) {
1054 p->threads = camel_folder_thread_messages_new (search->folder, NULL, TRUE);
1055 p->threads_hash = g_hash_table_new (g_str_hash, g_str_equal);
1057 fill_thread_table (p->threads->tree, p->threads_hash);
1060 results = g_hash_table_new (g_str_hash, g_str_equal);
1061 for (i = 0; i < r->value.ptrarray->len; i++) {
1062 struct _CamelFolderThreadNode *node, *scan;
1065 g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
1067 node = g_hash_table_lookup (p->threads_hash, (gchar *) g_ptr_array_index (r->value.ptrarray, i));
1068 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
1071 /* select messages in thread according to search criteria */
1073 if (node->child == NULL && node->parent == NULL)
1074 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1078 while (scan && scan->parent) {
1079 scan = scan->parent;
1080 g_hash_table_insert (results, (gchar *) camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
1082 } else if (type == 1) {
1083 while (node && node->parent)
1084 node = node->parent;
1086 g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
1088 add_thread_results (node->child, results);
1091 camel_sexp_result_free (f, r);
1093 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1094 r->value.ptrarray = g_ptr_array_new ();
1096 g_hash_table_foreach (results, (GHFunc) add_results, r->value.ptrarray);
1097 g_hash_table_destroy (results);
1102 static CamelMimeMessage *
1103 get_current_message (CamelFolderSearch *search)
1105 if (!search || !search->folder || !search->current)
1108 /* FIXME Pass a GCancellable */
1109 return camel_folder_get_message_sync (
1110 search->folder, search->current->uid, NULL, NULL);
1113 static CamelSExpResult *
1114 check_header (struct _CamelSExp *f,
1116 struct _CamelSExpResult **argv,
1117 CamelFolderSearch *search,
1118 camel_search_match_t how)
1123 r(printf("executing check-header %d\n", how));
1125 /* are we inside a match-all? */
1126 if (search->current && argc > 1
1127 && argv[0]->type == CAMEL_SEXP_RES_STRING) {
1129 const gchar *header = NULL, *charset = NULL;
1132 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1133 struct _camel_search_words *words;
1134 CamelMimeMessage *message = NULL;
1135 struct _camel_header_raw *raw_header;
1137 /* only a subset of headers are supported .. */
1138 headername = argv[0]->value.string;
1139 if (!g_ascii_strcasecmp(headername, "subject")) {
1140 header = camel_message_info_subject (search->current);
1141 } else if (!g_ascii_strcasecmp(headername, "date")) {
1142 /* FIXME: not a very useful form of the date */
1143 sprintf(strbuf, "%d", (gint)camel_message_info_date_sent(search->current));
1145 } else if (!g_ascii_strcasecmp(headername, "from")) {
1146 header = camel_message_info_from (search->current);
1147 type = CAMEL_SEARCH_TYPE_ADDRESS;
1148 } else if (!g_ascii_strcasecmp(headername, "to")) {
1149 header = camel_message_info_to (search->current);
1150 type = CAMEL_SEARCH_TYPE_ADDRESS;
1151 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1152 header = camel_message_info_cc (search->current);
1153 type = CAMEL_SEARCH_TYPE_ADDRESS;
1154 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1155 header = camel_message_info_mlist (search->current);
1156 type = CAMEL_SEARCH_TYPE_MLIST;
1158 message = get_current_message (search);
1160 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1163 charset = camel_content_type_param (ct, "charset");
1164 charset = camel_iconv_charset_name (charset);
1172 /* performs an OR of all words */
1173 for (i = 1; i < argc && !truth; i++) {
1174 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1175 if (argv[i]->value.string[0] == 0) {
1177 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1178 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1179 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1181 for (j = 0; j < words->len && truth; j++) {
1183 for (raw_header = ((CamelMimePart *) message)->headers; raw_header; raw_header = raw_header->next) {
1184 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1185 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1190 truth = raw_header != NULL;
1192 truth = camel_search_header_match (header, words->words[j]->word, how, type, charset);
1194 camel_search_words_free (words);
1197 for (raw_header = ((CamelMimePart *) message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1198 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1199 truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
1203 truth = camel_search_header_match (header, argv[i]->value.string, how, type, charset);
1209 g_object_unref (message);
1211 /* TODO: else, find all matches */
1213 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1214 r->value.boolean = truth;
1221 l_printf (gchar *node)
1223 printf("%s\t", node);
1227 static CamelSExpResult *
1228 search_header_contains (struct _CamelSExp *f,
1230 struct _CamelSExpResult **argv,
1231 CamelFolderSearch *search)
1233 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1236 static CamelSExpResult *
1237 search_header_matches (struct _CamelSExp *f,
1239 struct _CamelSExpResult **argv,
1240 CamelFolderSearch *search)
1242 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1245 static CamelSExpResult *
1246 search_header_starts_with (struct _CamelSExp *f,
1248 struct _CamelSExpResult **argv,
1249 CamelFolderSearch *search)
1251 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1254 static CamelSExpResult *
1255 search_header_ends_with (struct _CamelSExp *f,
1257 struct _CamelSExpResult **argv,
1258 CamelFolderSearch *search)
1260 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1263 static CamelSExpResult *
1264 search_header_exists (struct _CamelSExp *f,
1266 struct _CamelSExpResult **argv,
1267 CamelFolderSearch *search)
1271 r(printf ("executing header-exists\n"));
1273 if (search->current) {
1274 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1275 if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING)
1276 r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
1279 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1280 r->value.ptrarray = g_ptr_array_new ();
1286 static CamelSExpResult *
1287 search_header_soundex (struct _CamelSExp *f,
1289 struct _CamelSExpResult **argv,
1290 CamelFolderSearch *search)
1292 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1295 static CamelSExpResult *
1296 search_header_regex (struct _CamelSExp *f,
1298 struct _CamelSExpResult **argv,
1299 CamelFolderSearch *search)
1302 CamelMimeMessage *msg;
1304 msg = get_current_message (search);
1308 const gchar *contents;
1310 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1312 if (argc > 1 && argv[0]->type == CAMEL_SEXP_RES_STRING
1313 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1314 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_ICASE, argc - 1, argv + 1, search->priv->error) == 0) {
1315 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1318 r->value.boolean = FALSE;
1320 g_object_unref (msg);
1322 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1323 r->value.ptrarray = g_ptr_array_new ();
1330 get_full_header (CamelMimeMessage *message)
1332 CamelMimePart *mp = CAMEL_MIME_PART (message);
1333 GString *str = g_string_new ("");
1334 struct _camel_header_raw *h;
1336 for (h = mp->headers; h; h = h->next) {
1337 if (h->value != NULL) {
1338 g_string_append (str, h->name);
1339 if (isspace (h->value[0]))
1340 g_string_append (str, ":");
1342 g_string_append (str, ": ");
1343 g_string_append (str, h->value);
1344 g_string_append_c (str, '\n');
1348 return g_string_free (str, FALSE);
1351 static CamelSExpResult *
1352 search_header_full_regex (struct _CamelSExp *f,
1354 struct _CamelSExpResult **argv,
1355 CamelFolderSearch *search)
1358 CamelMimeMessage *msg;
1360 msg = get_current_message (search);
1365 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1367 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) {
1370 contents = get_full_header (msg);
1371 r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
1376 r->value.boolean = FALSE;
1378 g_object_unref (msg);
1380 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1381 r->value.ptrarray = g_ptr_array_new ();
1387 /* this is just to OR results together */
1393 /* or, store all unique values */
1397 struct IterData *iter_data)
1399 g_ptr_array_add (iter_data->uids, key);
1402 /* and, only store duplicates */
1406 struct IterData *iter_data)
1408 if (value == iter_data->count)
1409 g_ptr_array_add (iter_data->uids, key);
1413 match_message_index (CamelIndex *idx,
1418 CamelIndexCursor *wc, *nc;
1419 const gchar *word, *name;
1422 wc = camel_index_words (idx);
1424 while (!truth && (word = camel_index_cursor_next (wc))) {
1425 if (camel_ustrstrcase (word,match) != NULL) {
1426 /* perf: could have the wc cursor return the name cursor */
1427 nc = camel_index_find (idx, word);
1429 while (!truth && (name = camel_index_cursor_next (nc)))
1430 truth = strcmp (name, uid) == 0;
1431 g_object_unref (nc);
1435 g_object_unref (wc);
1442 "one two" "three" "four five"
1451 /* returns messages which contain all words listed in words */
1453 match_words_index (CamelFolderSearch *search,
1454 struct _camel_search_words *words,
1457 GPtrArray *result = g_ptr_array_new ();
1458 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1459 struct IterData lambdafoo;
1460 CamelIndexCursor *wc, *nc;
1461 const gchar *word, *name;
1464 /* we can have a maximum of 32 words, as we use it as the AND mask */
1466 wc = camel_index_words (search->body_index);
1468 while ((word = camel_index_cursor_next (wc))) {
1469 for (i = 0; i < words->len; i++) {
1470 if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
1471 /* perf: could have the wc cursor return the name cursor */
1472 nc = camel_index_find (search->body_index, word);
1474 while ((name = camel_index_cursor_next (nc))) {
1477 mask = (GPOINTER_TO_INT (g_hash_table_lookup (ht, name))) | (1 << i);
1478 g_hash_table_insert (ht, (gchar *) camel_pstring_peek (name), GINT_TO_POINTER (mask));
1480 g_object_unref (nc);
1485 g_object_unref (wc);
1487 lambdafoo.uids = result;
1488 lambdafoo.count = (1 << words->len) - 1;
1489 g_hash_table_foreach (ht, (GHFunc) htand, &lambdafoo);
1490 g_hash_table_destroy (ht);
1497 match_words_1message (CamelDataWrapper *object,
1498 struct _camel_search_words *words,
1501 CamelDataWrapper *containee;
1505 containee = camel_medium_get_content (CAMEL_MEDIUM (object));
1507 if (containee == NULL)
1510 /* using the object types is more accurate than using the mime/types */
1511 if (CAMEL_IS_MULTIPART (containee)) {
1512 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1513 for (i = 0; i < parts && truth == FALSE; i++) {
1514 CamelDataWrapper *part = (CamelDataWrapper *) camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1516 truth = match_words_1message (part, words, mask);
1518 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1519 /* for messages we only look at its contents */
1520 truth = match_words_1message ((CamelDataWrapper *) containee, words, mask);
1521 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1522 /* for all other text parts, we look inside, otherwise we dont care */
1523 CamelStream *stream;
1524 GByteArray *byte_array;
1526 byte_array = g_byte_array_new ();
1527 stream = camel_stream_mem_new_with_byte_array (byte_array);
1529 /* FIXME The match should be part of a stream op */
1530 /* FIXME Pass a GCancellable and GError here. */
1531 camel_data_wrapper_decode_to_stream_sync (
1532 containee, stream, NULL, NULL);
1533 camel_stream_write (stream, "", 1, NULL, NULL);
1534 for (i = 0; i < words->len; i++) {
1535 /* FIXME: This is horridly slow, and should use a real search algorithm */
1536 if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
1538 /* shortcut a match */
1539 if (*mask == (1 << (words->len)) - 1)
1544 g_object_unref (stream);
1551 match_words_message (CamelFolder *folder,
1553 struct _camel_search_words *words,
1554 GCancellable *cancellable,
1558 CamelMimeMessage *msg;
1561 msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
1564 truth = match_words_1message ((CamelDataWrapper *) msg, words, &mask);
1565 g_object_unref (msg);
1572 match_words_messages (CamelFolderSearch *search,
1573 struct _camel_search_words *words,
1574 GCancellable *cancellable,
1578 GPtrArray *matches = g_ptr_array_new ();
1580 if (search->body_index) {
1582 struct _camel_search_words *simple;
1584 simple = camel_search_words_simple (words);
1585 indexed = match_words_index (search, simple, error);
1586 camel_search_words_free (simple);
1588 for (i = 0; i < indexed->len; i++) {
1589 const gchar *uid = g_ptr_array_index (indexed, i);
1591 if (match_words_message (
1592 search->folder, uid, words,
1593 cancellable, error))
1594 g_ptr_array_add (matches, (gchar *) uid);
1597 g_ptr_array_free (indexed, TRUE);
1599 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1601 for (i = 0; i < v->len; i++) {
1602 gchar *uid = g_ptr_array_index (v, i);
1604 if (match_words_message (
1605 search->folder, uid, words,
1606 cancellable, error))
1607 g_ptr_array_add (matches, (gchar *) uid);
1614 static CamelSExpResult *
1615 search_body_contains (struct _CamelSExp *f,
1617 struct _CamelSExpResult **argv,
1618 CamelFolderSearch *search)
1621 GError **error = search->priv->error;
1622 struct _camel_search_words *words;
1624 struct IterData lambdafoo;
1626 if (search->current) {
1629 if (argc == 1 && argv[0]->value.string[0] == 0) {
1632 for (i = 0; i < argc && !truth; i++) {
1633 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1634 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1636 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1637 for (j = 0; j < words->len && truth; j++)
1638 truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
1640 /* TODO: cache current message incase of multiple body search terms */
1641 /* FIXME Pass a GCancellable */
1642 truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
1644 camel_search_words_free (words);
1648 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1649 r->value.boolean = truth;
1651 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1652 r->value.ptrarray = g_ptr_array_new ();
1654 if (argc == 1 && argv[0]->value.string[0] == 0) {
1655 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1657 for (i = 0; i < v->len; i++) {
1658 gchar *uid = g_ptr_array_index (v, i);
1660 g_ptr_array_add (r->value.ptrarray, uid);
1663 GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
1666 for (i = 0; i < argc; i++) {
1667 if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
1668 words = camel_search_words_split ((const guchar *) argv[i]->value.string);
1669 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1670 matches = match_words_index (search, words, error);
1672 /* FIXME Pass a GCancellable */
1673 matches = match_words_messages (search, words, NULL, error);
1675 for (j = 0; j < matches->len; j++) {
1676 g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
1678 g_ptr_array_free (matches, TRUE);
1679 camel_search_words_free (words);
1682 lambdafoo.uids = r->value.ptrarray;
1683 g_hash_table_foreach (ht, (GHFunc) htor, &lambdafoo);
1684 g_hash_table_destroy (ht);
1691 static CamelSExpResult *
1692 search_body_regex (struct _CamelSExp *f,
1694 struct _CamelSExpResult **argv,
1695 CamelFolderSearch *search)
1698 CamelMimeMessage *msg = get_current_message (search);
1703 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1705 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) {
1706 r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1709 r->value.boolean = FALSE;
1711 g_object_unref (msg);
1715 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1716 r->value.ptrarray = g_ptr_array_new ();
1718 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) {
1720 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1721 CamelMimeMessage *message;
1723 for (i = 0; i < v->len; i++) {
1724 gchar *uid = g_ptr_array_index (v, i);
1726 /* FIXME Pass a GCancellable */
1727 message = camel_folder_get_message_sync (
1728 search->folder, uid, NULL, NULL);
1730 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1731 g_ptr_array_add (r->value.ptrarray, uid);
1734 g_object_unref (message);
1745 static CamelSExpResult *
1746 search_user_flag (struct _CamelSExp *f,
1748 struct _CamelSExpResult **argv,
1749 CamelFolderSearch *search)
1754 r(printf("executing user-flag\n"));
1756 /* are we inside a match-all? */
1757 if (search->current) {
1759 /* performs an OR of all words */
1760 for (i = 0; i < argc && !truth; i++) {
1761 if (argv[i]->type == CAMEL_SEXP_RES_STRING
1762 && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
1767 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1768 r->value.boolean = truth;
1770 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1771 r->value.ptrarray = g_ptr_array_new ();
1777 static CamelSExpResult *
1778 search_system_flag (struct _CamelSExp *f,
1780 struct _CamelSExpResult **argv,
1781 CamelFolderSearch *search)
1785 r(printf ("executing system-flag\n"));
1787 if (search->current) {
1788 gboolean truth = FALSE;
1791 truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
1793 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1794 r->value.boolean = truth;
1796 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1797 r->value.ptrarray = g_ptr_array_new ();
1803 static CamelSExpResult *
1804 search_user_tag (struct _CamelSExp *f,
1806 struct _CamelSExpResult **argv,
1807 CamelFolderSearch *search)
1809 const gchar *value = NULL;
1812 r(printf("executing user-tag\n"));
1814 if (search->current && argc == 1)
1815 value = camel_message_info_user_tag (search->current, argv[0]->value.string);
1817 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_STRING);
1818 r->value.string = g_strdup (value ? value : "");
1823 static CamelSExpResult *
1824 search_get_sent_date (struct _CamelSExp *f,
1826 struct _CamelSExpResult **argv,
1827 CamelFolderSearch *s)
1831 r(printf("executing get-sent-date\n"));
1833 /* are we inside a match-all? */
1835 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1837 r->value.number = camel_message_info_date_sent (s->current);
1839 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1840 r->value.ptrarray = g_ptr_array_new ();
1846 static CamelSExpResult *
1847 search_get_received_date (struct _CamelSExp *f,
1849 struct _CamelSExpResult **argv,
1850 CamelFolderSearch *s)
1854 r(printf("executing get-received-date\n"));
1856 /* are we inside a match-all? */
1858 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1860 r->value.number = camel_message_info_date_received (s->current);
1862 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1863 r->value.ptrarray = g_ptr_array_new ();
1869 static CamelSExpResult *
1870 search_get_current_date (struct _CamelSExp *f,
1872 struct _CamelSExpResult **argv,
1873 CamelFolderSearch *s)
1877 r(printf("executing get-current-date\n"));
1879 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1880 r->value.number = time (NULL);
1884 static CamelSExpResult *
1885 search_get_relative_months (struct _CamelSExp *f,
1887 struct _CamelSExpResult **argv,
1888 CamelFolderSearch *s)
1892 r(printf("executing get-relative-months\n"));
1894 if (argc != 1 || argv[0]->type != CAMEL_SEXP_RES_INT) {
1895 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1896 r->value.boolean = FALSE;
1898 g_debug ("%s: Expecting 1 argument, an integer, but got %d arguments", G_STRFUNC, argc);
1900 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1901 r->value.number = camel_folder_search_util_add_months (time (NULL), argv[0]->value.number);
1907 static CamelSExpResult *
1908 search_get_size (struct _CamelSExp *f,
1910 struct _CamelSExpResult **argv,
1911 CamelFolderSearch *s)
1915 r(printf("executing get-size\n"));
1917 /* are we inside a match-all? */
1919 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
1920 r->value.number = camel_message_info_size (s->current) / 1024;
1922 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1923 r->value.ptrarray = g_ptr_array_new ();
1929 static CamelSExpResult *
1930 search_uid (struct _CamelSExp *f,
1932 struct _CamelSExpResult **argv,
1933 CamelFolderSearch *search)
1938 r(printf("executing uid\n"));
1940 /* are we inside a match-all? */
1941 if (search->current) {
1943 const gchar *uid = camel_message_info_uid (search->current);
1945 /* performs an OR of all words */
1946 for (i = 0; i < argc && !truth; i++) {
1947 if (argv[i]->type == CAMEL_SEXP_RES_STRING
1948 && !strcmp (uid, argv[i]->value.string)) {
1953 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
1954 r->value.boolean = truth;
1956 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
1957 r->value.ptrarray = g_ptr_array_new ();
1958 for (i = 0; i < argc; i++) {
1959 if (argv[i]->type == CAMEL_SEXP_RES_STRING)
1960 g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
1968 read_uid_callback (gpointer ref,
1975 matches = (GPtrArray *) ref;
1977 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols[0]));
1981 static CamelSExpResult *
1982 search_message_location (struct _CamelSExp *f,
1984 struct _CamelSExpResult **argv,
1985 CamelFolderSearch *search)
1988 gboolean same = FALSE;
1990 if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING) {
1991 if (argv[0]->value.string && search->folder) {
1997 /* FIXME Folder URI formats are Evolution-specific
1998 * knowledge and doesn't belong here! */
1999 store = camel_folder_get_parent_store (search->folder);
2000 name = camel_folder_get_full_name (search->folder);
2001 uid = camel_service_get_uid (CAMEL_SERVICE (store));
2003 uri = g_strdup_printf ("folder://%s/%s", uid, name);
2004 same = g_str_equal (uri, argv[0]->value.string);
2009 if (search->current) {
2010 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
2011 r->value.boolean = same ? TRUE : FALSE;
2013 r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
2014 r->value.ptrarray = g_ptr_array_new ();
2019 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
2021 for (i = 0; i < v->len; i++) {
2022 gchar *uid = g_ptr_array_index (v, i);
2024 g_ptr_array_add (r->value.ptrarray, uid);
2033 * camel_folder_search_util_add_months:
2035 * @months: number of months to add or subtract
2037 * Increases time @t by the given number of months (or decreases, if
2038 * @months is negative).
2040 * Returns: a new #time_t value
2045 camel_folder_search_util_add_months (time_t t,
2048 GDateTime *dt, *dt2;
2054 dt = g_date_time_new_from_unix_utc (t);
2056 /* just for issues, to return something inaccurate, but sane */
2057 res = t + (60 * 60 * 24 * 30 * months);
2059 g_return_val_if_fail (dt != NULL, res);
2061 dt2 = g_date_time_add_months (dt, months);
2062 g_date_time_unref (dt);
2063 g_return_val_if_fail (dt2 != NULL, res);
2065 res = g_date_time_to_unix (dt2);
2066 g_date_time_unref (dt2);