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>
39 #include <glib/gi18n-lib.h>
41 #include "camel-exception.h"
42 #include "camel-folder-search.h"
43 #include "camel-folder-thread.h"
44 #include "camel-iconv.h"
45 #include "camel-medium.h"
46 #include "camel-mime-message.h"
47 #include "camel-multipart.h"
48 #include "camel-search-private.h"
49 #include "camel-stream-mem.h"
51 #include "camel-debug.h"
52 #include "camel-store.h"
53 #include "camel-vee-folder.h"
54 #include "camel-string-utils.h"
55 #include "camel-search-sql.h"
56 #include "camel-search-sql-sexp.h"
60 #define dd(x) if (camel_debug("search")) x
62 struct _CamelFolderSearchPrivate {
65 CamelFolderThread *threads;
66 GHashTable *threads_hash;
69 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
71 static ESExpResult *search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
75 static ESExpResult *search_header_starts_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
76 static ESExpResult *search_header_ends_with(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_header_exists(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
78 static ESExpResult *search_header_soundex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
79 static ESExpResult *search_header_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
80 static ESExpResult *search_header_full_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
81 static ESExpResult *search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
82 static ESExpResult *search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
83 static ESExpResult *search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
84 static ESExpResult *search_body_regex(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
85 static ESExpResult *search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
87 static ESExpResult *search_system_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
88 static ESExpResult *search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
89 static ESExpResult *search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
90 static ESExpResult *search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
91 static ESExpResult *search_get_size(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
92 static ESExpResult *search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
93 static ESExpResult *search_message_location(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
95 static ESExpResult *search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
97 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
98 static void camel_folder_search_init (CamelFolderSearch *obj);
99 static void camel_folder_search_finalize (CamelObject *obj);
101 static gint read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name);
103 static CamelObjectClass *camel_folder_search_parent;
106 camel_folder_search_class_init (CamelFolderSearchClass *klass)
108 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
110 klass->not = search_not;
112 klass->match_all = search_match_all;
113 klass->match_threads = search_match_threads;
114 klass->body_contains = search_body_contains;
115 klass->body_regex = search_body_regex;
116 klass->header_contains = search_header_contains;
117 klass->header_matches = search_header_matches;
118 klass->header_starts_with = search_header_starts_with;
119 klass->header_ends_with = search_header_ends_with;
120 klass->header_exists = search_header_exists;
121 klass->header_soundex = search_header_soundex;
122 klass->header_regex = search_header_regex;
123 klass->header_full_regex = search_header_full_regex;
124 klass->user_tag = search_user_tag;
125 klass->user_flag = search_user_flag;
126 klass->system_flag = search_system_flag;
127 klass->get_sent_date = search_get_sent_date;
128 klass->get_received_date = search_get_received_date;
129 klass->get_current_date = search_get_current_date;
130 klass->get_size = search_get_size;
131 klass->uid = search_uid;
132 klass->message_location = search_message_location;
136 camel_folder_search_init (CamelFolderSearch *obj)
138 struct _CamelFolderSearchPrivate *p;
140 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
142 obj->sexp = e_sexp_new();
146 camel_folder_search_finalize (CamelObject *obj)
148 CamelFolderSearch *search = (CamelFolderSearch *)obj;
149 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
152 e_sexp_unref(search->sexp);
154 g_free(search->last_search);
159 camel_folder_search_get_type (void)
161 static CamelType type = CAMEL_INVALID_TYPE;
163 if (type == CAMEL_INVALID_TYPE) {
164 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
165 sizeof (CamelFolderSearch),
166 sizeof (CamelFolderSearchClass),
167 (CamelObjectClassInitFunc) camel_folder_search_class_init,
169 (CamelObjectInitFunc) camel_folder_search_init,
170 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
177 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
179 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar *) &((type *) 0)->field))
185 gint flags; /* 0x02 = immediate, 0x01 = always enter */
187 /* these have default implementations in e-sexp */
188 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
189 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
190 /* we need to override this one though to implement an 'array not' */
191 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
192 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
193 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
194 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
196 /* these we have to use our own default if there is none */
197 /* they should all be defined in the language? so it parses, or should they not?? */
198 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
199 { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
200 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
201 { "body-regex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_regex), 1 },
202 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
203 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
204 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
205 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
206 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
207 { "header-soundex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_soundex), 1 },
208 { "header-regex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_regex), 1 },
209 { "header-full-regex", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_full_regex), 1 },
210 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
211 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
212 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
213 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
214 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
215 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
216 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
217 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
218 { "message-location", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
222 camel_folder_search_construct (CamelFolderSearch *search)
225 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
227 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
229 /* c is sure messy sometimes */
230 func = *((gpointer *)(((gchar *)klass)+builtins[i].offset));
231 if (func == NULL && builtins[i].flags&1) {
232 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
233 func = (gpointer)search_dummy;
236 if (builtins[i].flags&2) {
237 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
239 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
246 * camel_folder_search_new:
248 * Create a new CamelFolderSearch object.
250 * A CamelFolderSearch is a subclassable, extensible s-exp
251 * evaluator which enforces a particular set of s-expressions.
252 * Particular methods may be overriden by an implementation to
253 * implement a search for any sort of backend.
255 * Return value: A new CamelFolderSearch widget.
258 camel_folder_search_new (void)
260 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
262 camel_folder_search_construct(new);
267 * camel_folder_search_set_folder:
271 * Set the folder attribute of the search. This is currently unused, but
272 * could be used to perform a slow-search when indexes and so forth are not
273 * available. Or for use by subclasses.
276 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
278 search->folder = folder;
282 * camel_folder_search_set_summary:
284 * @summary: An array of CamelMessageInfo pointers.
286 * Set the array of summary objects representing the span of the search.
288 * If this is not set, then a subclass must provide the functions
289 * for searching headers and for the match-all operator.
292 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
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, CamelIndex *index)
310 if (search->body_index)
311 camel_object_unref((CamelObject *)search->body_index);
312 search->body_index = index;
314 camel_object_ref((CamelObject *)index);
318 * camel_folder_search_execute_expression:
323 * Execute the search expression @expr, returning an array of
324 * all matches as a GPtrArray of uid's of matching messages.
326 * Note that any settings such as set_body_index(), set_folder(),
327 * and so on are reset to #NULL once the search has completed.
329 * TODO: The interface should probably return summary items instead
330 * (since they are much more useful to any client).
332 * Return value: A GPtrArray of strings of all matching messages.
333 * This must only be freed by camel_folder_search_free_result.
336 camel_folder_search_execute_expression(CamelFolderSearch *search, const gchar *expr, CamelException *ex)
342 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
346 /* only re-parse if the search has changed */
347 if (search->last_search == NULL
348 || strcmp(search->last_search, expr)) {
349 e_sexp_input_text(search->sexp, expr, strlen(expr));
350 if (e_sexp_parse(search->sexp) == -1) {
351 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
355 g_free(search->last_search);
356 search->last_search = g_strdup(expr);
358 r = e_sexp_eval(search->sexp);
360 if (!camel_exception_is_set(ex))
361 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
365 matches = g_ptr_array_new();
367 /* now create a folder summary to return?? */
368 if (r->type == ESEXP_RES_ARRAY_PTR) {
369 d(printf("got result ...\n"));
370 if (search->summary) {
371 /* reorder result in summary order */
372 results = g_hash_table_new(g_str_hash, g_str_equal);
373 for (i=0;i<r->value.ptrarray->len;i++) {
374 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
375 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
377 for (i=0;i<search->summary->len;i++) {
378 gchar *uid = g_ptr_array_index(search->summary, i);
379 if (g_hash_table_lookup(results, uid)) {
380 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
383 g_hash_table_destroy(results);
385 for (i=0;i<r->value.ptrarray->len;i++) {
386 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
387 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
391 g_warning("Search returned an invalid result type");
394 e_sexp_result_free(search->sexp, r);
397 camel_folder_thread_messages_unref(p->threads);
399 g_hash_table_destroy(p->threads_hash);
402 p->threads_hash = NULL;
403 search->folder = NULL;
404 search->summary = NULL;
405 search->current = NULL;
406 search->body_index = NULL;
412 * camel_folder_search_count:
415 * @uids: to search against, NULL for all uid's.
418 * Run a search. Search must have had Folder already set on it, and
419 * it must implement summaries.
421 * Return value: Number of messages that match the query.
425 camel_folder_search_count(CamelFolderSearch *search, const gchar *expr, CamelException *ex)
428 GPtrArray *summary_set;
431 gchar *sql_query, *tmp, *tmp1;
435 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
437 g_assert(search->folder);
441 /* We route body-contains search and thread based search through memory and not via db. */
442 if (strstr((const gchar *) expr, "body-contains") || strstr((const gchar *) expr, "match-threads")) {
443 /* setup our search list only contains those we're interested in */
444 search->summary = camel_folder_get_summary(search->folder);
446 summary_set = search->summary;
448 /* only re-parse if the search has changed */
449 if (search->last_search == NULL
450 || strcmp(search->last_search, expr)) {
451 e_sexp_input_text(search->sexp, expr, strlen(expr));
452 if (e_sexp_parse(search->sexp) == -1) {
453 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
457 g_free(search->last_search);
458 search->last_search = g_strdup(expr);
460 r = e_sexp_eval(search->sexp);
462 if (!camel_exception_is_set(ex))
463 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
467 /* now create a folder summary to return?? */
468 if (r->type == ESEXP_RES_ARRAY_PTR) {
469 d(printf("got result\n"));
471 /* reorder result in summary order */
472 results = g_hash_table_new(g_str_hash, g_str_equal);
473 for (i=0;i<r->value.ptrarray->len;i++) {
474 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
475 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
478 for (i=0;i<summary_set->len;i++) {
479 gchar *uid = g_ptr_array_index(summary_set, i);
480 if (g_hash_table_lookup(results, uid))
483 g_hash_table_destroy(results);
486 e_sexp_result_free(search->sexp, r);
489 /* Sync the db, so that we search the db for changes */
490 camel_folder_summary_save_to_db (search->folder->summary, ex);
492 dd(printf ("sexp is : [%s]\n", expr));
493 if (g_getenv("SQL_SEARCH_OLD"))
494 sql_query = camel_sexp_to_sql (expr);
496 sql_query = camel_sexp_to_sql_sexp (expr);
497 tmp1 = camel_db_sqlize_string(search->folder->full_name);
498 tmp = g_strdup_printf ("SELECT COUNT (*) FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
499 camel_db_free_sqlized_string (tmp1);
501 dd(printf("Equivalent sql %s\n", tmp));
503 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
504 camel_db_count_message_info (cdb, tmp, &count, ex);
505 if (ex && camel_exception_is_set(ex)) {
506 const gchar *exception = camel_exception_get_description (ex);
507 if (strncmp(exception, "no such table", 13) == 0) {
508 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
509 camel_exception_clear (ex); /* Suppress no such table */
517 /* these might be allocated by match-threads */
519 camel_folder_thread_messages_unref(p->threads);
521 g_hash_table_destroy(p->threads_hash);
522 if (search->summary_set)
523 g_ptr_array_free(search->summary_set, TRUE);
525 camel_folder_free_summary(search->folder, search->summary);
528 p->threads_hash = NULL;
529 search->folder = NULL;
530 search->summary = NULL;
531 search->summary_set = NULL;
532 search->current = NULL;
533 search->body_index = NULL;
539 do_search_in_memory (const gchar *expr)
541 /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
542 const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
548 for (i = 0; in_memory_tokens [i]; i++) {
549 if (strstr (expr, in_memory_tokens [i]))
557 * camel_folder_search_search:
560 * @uids: to search against, NULL for all uid's.
563 * Run a search. Search must have had Folder already set on it, and
564 * it must implement summaries.
569 camel_folder_search_search(CamelFolderSearch *search, const gchar *expr, GPtrArray *uids, CamelException *ex)
572 GPtrArray *matches = NULL, *summary_set;
575 gchar *sql_query, *tmp, *tmp1;
578 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
580 g_assert(search->folder);
584 /* We route body-contains / thread based search and uid search through memory and not via db. */
585 if (uids || do_search_in_memory (expr)) {
586 /* setup our search list only contains those we're interested in */
587 search->summary = camel_folder_get_summary(search->folder);
590 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
592 summary_set = search->summary_set = g_ptr_array_new();
593 for (i=0;i<uids->len;i++)
594 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
595 for (i=0;i<search->summary->len;i++)
596 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
597 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
598 g_hash_table_destroy(uids_hash);
600 summary_set = search->summary;
603 /* only re-parse if the search has changed */
604 if (search->last_search == NULL
605 || strcmp(search->last_search, expr)) {
606 e_sexp_input_text(search->sexp, expr, strlen(expr));
607 if (e_sexp_parse(search->sexp) == -1) {
608 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
612 g_free(search->last_search);
613 search->last_search = g_strdup(expr);
615 r = e_sexp_eval(search->sexp);
617 if (!camel_exception_is_set(ex))
618 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
622 matches = g_ptr_array_new();
624 /* now create a folder summary to return?? */
625 if (r->type == ESEXP_RES_ARRAY_PTR) {
626 d(printf("got result\n"));
628 /* reorder result in summary order */
629 results = g_hash_table_new(g_str_hash, g_str_equal);
630 for (i=0;i<r->value.ptrarray->len;i++) {
631 d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
632 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
635 for (i=0;i<summary_set->len;i++) {
636 gchar *uid = g_ptr_array_index(summary_set, i);
637 if (g_hash_table_lookup(results, uid))
638 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
640 g_hash_table_destroy(results);
643 e_sexp_result_free(search->sexp, r);
646 /* Sync the db, so that we search the db for changes */
647 camel_folder_summary_save_to_db (search->folder->summary, ex);
649 dd(printf ("sexp is : [%s]\n", expr));
650 if (g_getenv("SQL_SEARCH_OLD"))
651 sql_query = camel_sexp_to_sql (expr);
653 sql_query = camel_sexp_to_sql_sexp (expr);
654 tmp1 = camel_db_sqlize_string(search->folder->full_name);
655 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
656 camel_db_free_sqlized_string (tmp1);
658 dd(printf("Equivalent sql %s\n", tmp));
660 matches = g_ptr_array_new();
661 cdb = (CamelDB *) (search->folder->parent_store->cdb_r);
662 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
663 if (ex && camel_exception_is_set(ex)) {
664 const gchar *exception = camel_exception_get_description (ex);
665 if (strncmp(exception, "no such table", 13) == 0) {
666 d(g_warning ("Error during searching %s: %s\n", tmp, exception));
667 camel_exception_clear (ex); /* Suppress no such table */
675 /* these might be allocated by match-threads */
677 camel_folder_thread_messages_unref(p->threads);
679 g_hash_table_destroy(p->threads_hash);
680 if (search->summary_set)
681 g_ptr_array_free(search->summary_set, TRUE);
683 camel_folder_free_summary(search->folder, search->summary);
686 p->threads_hash = NULL;
687 search->folder = NULL;
688 search->summary = NULL;
689 search->summary_set = NULL;
690 search->current = NULL;
691 search->body_index = NULL;
696 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
698 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
699 g_ptr_array_free(result, TRUE);
702 /* dummy function, returns false always, or an empty match array */
704 search_dummy(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
708 if (search->current == NULL) {
709 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
710 r->value.bool = FALSE;
712 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
713 r->value.ptrarray = g_ptr_array_new();
719 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
721 search_not(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
727 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
728 GPtrArray *v = argv[0]->value.ptrarray;
731 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
732 r->value.ptrarray = g_ptr_array_new();
734 /* not against a single message?*/
735 if (search->current) {
738 uid = camel_message_info_uid(search->current);
739 for (i=0;!found && i<v->len;i++) {
740 if (strcmp(uid, v->pdata[i]) == 0)
745 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
746 } else if (search->summary == NULL) {
747 g_warning("No summary set, 'not' against an array requires a summary");
749 /* 'not' against the whole summary */
750 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
754 s = (gchar **)v->pdata;
755 for (i=0;i<v->len;i++)
756 g_hash_table_insert(have, s[i], s[i]);
758 v = search->summary_set?search->summary_set:search->summary;
759 m = (gchar **)v->pdata;
760 for (i=0;i<v->len;i++) {
763 if (g_hash_table_lookup(have, uid) == NULL)
764 g_ptr_array_add(r->value.ptrarray, uid);
766 g_hash_table_destroy(have);
771 if (argv[0]->type == ESEXP_RES_BOOL)
772 res = ! argv[0]->value.bool;
774 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
778 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
779 r->value.bool = TRUE;
786 search_match_all(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
794 g_warning("match-all only takes a single argument, other arguments ignored");
797 /* we are only matching a single message? or already inside a match-all? */
798 if (search->current) {
799 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
801 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
802 r->value.bool = FALSE;
805 r1 = e_sexp_term_eval(f, argv[0]);
806 if (r1->type == ESEXP_RES_BOOL) {
807 r->value.bool = r1->value.bool;
809 g_warning("invalid syntax, matches require a single bool result");
810 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
811 e_sexp_fatal_error(f, error_msg);
814 e_sexp_result_free(f, r1);
816 r->value.bool = TRUE;
821 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
822 r->value.ptrarray = g_ptr_array_new();
824 if (search->summary == NULL) {
825 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
826 g_warning("No summary supplied, match-all doesn't work with no summary");
831 v = search->summary_set?search->summary_set:search->summary;
833 if (v->len > g_hash_table_size (search->folder->summary->loaded_infos) && !CAMEL_IS_VEE_FOLDER (search->folder)) {
834 camel_folder_summary_reload_from_db (search->folder->summary, search->priv->ex);
838 for (i=0;i<v->len;i++) {
841 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
842 if (!search->current)
844 uid = camel_message_info_uid(search->current);
847 r1 = e_sexp_term_eval(f, argv[0]);
848 if (r1->type == ESEXP_RES_BOOL) {
850 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
852 g_warning("invalid syntax, matches require a single bool result");
853 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
854 e_sexp_fatal_error(f, error_msg);
857 e_sexp_result_free(f, r1);
859 g_ptr_array_add(r->value.ptrarray, (gchar *)uid);
861 camel_message_info_free (search->current);
863 search->current = NULL;
868 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
871 g_hash_table_insert(id_hash, (gchar *)camel_message_info_uid(root->message), root);
873 fill_thread_table(root->child, id_hash);
879 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
882 g_hash_table_insert(result_hash, (gchar *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
884 add_thread_results(root->child, result_hash);
890 add_results(gchar *uid, gpointer dummy, GPtrArray *result)
892 g_ptr_array_add(result, uid);
896 search_match_threads(struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
899 struct _CamelFolderSearchPrivate *p = search->priv;
904 /* not supported in match-all */
905 if (search->current) {
906 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
907 e_sexp_fatal_error(f, error_msg);
912 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
913 e_sexp_fatal_error(f, error_msg);
917 r = e_sexp_term_eval(f, argv[0]);
918 if (r->type != ESEXP_RES_STRING) {
919 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
920 e_sexp_fatal_error(f, error_msg);
925 if (!strcmp(r->value.string, "none"))
927 else if (!strcmp(r->value.string, "all"))
929 else if (!strcmp(r->value.string, "replies"))
931 else if (!strcmp(r->value.string, "replies_parents"))
933 else if (!strcmp(r->value.string, "single"))
935 e_sexp_result_free(f, r);
937 /* behave as (begin does */
939 for (i=1;i<argc;i++) {
941 e_sexp_result_free(f, r);
942 r = e_sexp_term_eval(f, argv[i]);
945 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
946 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
947 e_sexp_fatal_error(f, error_msg);
954 if (search->folder == NULL) {
955 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
956 e_sexp_fatal_error(f, error_msg);
960 /* cache this, so we only have to re-calculate once per search at most */
961 if (p->threads == NULL) {
962 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
963 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
965 fill_thread_table(p->threads->tree, p->threads_hash);
968 results = g_hash_table_new(g_str_hash, g_str_equal);
969 for (i=0;i<r->value.ptrarray->len;i++) {
970 struct _CamelFolderThreadNode *node, *scan;
973 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
975 node = g_hash_table_lookup(p->threads_hash, (gchar *)g_ptr_array_index(r->value.ptrarray, i));
976 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
979 /* select messages in thread according to search criteria */
981 if (node->child == NULL && node->parent == NULL)
982 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
986 while (scan && scan->parent) {
988 g_hash_table_insert(results, (gchar *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
990 } else if (type == 1) {
991 while (node && node->parent)
994 g_hash_table_insert(results, (gchar *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
996 add_thread_results(node->child, results);
999 e_sexp_result_free(f, r);
1001 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1002 r->value.ptrarray = g_ptr_array_new();
1004 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
1005 g_hash_table_destroy(results);
1010 static CamelMimeMessage *
1011 get_current_message (CamelFolderSearch *search)
1013 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1014 CamelMimeMessage *res;
1016 if (!search || !search->folder || !search->current)
1019 res = camel_folder_get_message (search->folder, search->current->uid, &x);
1022 camel_exception_clear (&x);
1027 static ESExpResult *
1028 check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
1033 r(printf("executing check-header %d\n", how));
1035 /* are we inside a match-all? */
1036 if (search->current && argc>1
1037 && argv[0]->type == ESEXP_RES_STRING) {
1039 const gchar *header = NULL, *charset = NULL;
1042 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
1043 struct _camel_search_words *words;
1044 CamelMimeMessage *message = NULL;
1045 struct _camel_header_raw *raw_header;
1047 /* only a subset of headers are supported .. */
1048 headername = argv[0]->value.string;
1049 if (!g_ascii_strcasecmp(headername, "subject")) {
1050 header = camel_message_info_subject(search->current);
1051 } else if (!g_ascii_strcasecmp(headername, "date")) {
1052 /* FIXME: not a very useful form of the date */
1053 sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
1055 } else if (!g_ascii_strcasecmp(headername, "from")) {
1056 header = camel_message_info_from(search->current);
1057 type = CAMEL_SEARCH_TYPE_ADDRESS;
1058 } else if (!g_ascii_strcasecmp(headername, "to")) {
1059 header = camel_message_info_to(search->current);
1060 type = CAMEL_SEARCH_TYPE_ADDRESS;
1061 } else if (!g_ascii_strcasecmp(headername, "cc")) {
1062 header = camel_message_info_cc(search->current);
1063 type = CAMEL_SEARCH_TYPE_ADDRESS;
1064 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
1065 header = camel_message_info_mlist(search->current);
1066 type = CAMEL_SEARCH_TYPE_MLIST;
1068 message = get_current_message (search);
1070 CamelContentType *ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
1073 charset = camel_content_type_param (ct, "charset");
1074 charset = camel_iconv_charset_name (charset);
1082 /* performs an OR of all words */
1083 for (i=1;i<argc && !truth;i++) {
1084 if (argv[i]->type == ESEXP_RES_STRING) {
1085 if (argv[i]->value.string[0] == 0) {
1087 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
1088 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
1089 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1091 for (j=0;j<words->len && truth;j++) {
1093 for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
1094 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1095 if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
1100 truth = raw_header != NULL;
1102 truth = camel_search_header_match(header, words->words[j]->word, how, type, charset);
1104 camel_search_words_free(words);
1107 for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
1108 if (!g_ascii_strcasecmp (raw_header->name, headername)) {
1109 truth = camel_search_header_match(raw_header->value, argv[i]->value.string, how, type, charset);
1113 truth = camel_search_header_match(header, argv[i]->value.string, how, type, charset);
1119 camel_object_unref (message);
1121 /* TODO: else, find all matches */
1123 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1124 r->value.bool = truth;
1131 l_printf(gchar *node)
1133 printf("%s\t", node);
1137 static ESExpResult *
1138 search_header_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1140 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
1143 static ESExpResult *
1144 search_header_matches(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1146 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
1149 static ESExpResult *
1150 search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1152 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
1155 static ESExpResult *
1156 search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1158 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
1161 static ESExpResult *
1162 search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1166 r(printf ("executing header-exists\n"));
1168 if (search->current) {
1169 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1170 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
1171 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
1174 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1175 r->value.ptrarray = g_ptr_array_new();
1181 static ESExpResult *
1182 search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1184 return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
1187 static ESExpResult *
1188 search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1191 CamelMimeMessage *msg;
1193 msg = get_current_message (search);
1197 const gchar *contents;
1199 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1201 if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
1202 && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
1203 && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->ex) == 0) {
1204 r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
1207 r->value.bool = FALSE;
1209 camel_object_unref (msg);
1211 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1212 r->value.ptrarray = g_ptr_array_new();
1219 get_full_header (CamelMimeMessage *message)
1221 CamelMimePart *mp = CAMEL_MIME_PART (message);
1222 GString *str = g_string_new ("");
1223 struct _camel_header_raw *h;
1225 for (h = mp->headers; h; h = h->next) {
1226 if (h->value != NULL) {
1227 g_string_append (str, h->name);
1228 if (isspace (h->value[0]))
1229 g_string_append (str, ":");
1231 g_string_append (str, ": ");
1232 g_string_append (str, h->value);
1233 g_string_append_c (str, '\n');
1237 return g_string_free (str, FALSE);
1240 static ESExpResult *
1241 search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1244 CamelMimeMessage *msg;
1246 msg = get_current_message (search);
1251 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1253 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1256 contents = get_full_header (msg);
1257 r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
1262 r->value.bool = FALSE;
1264 camel_object_unref (msg);
1266 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1267 r->value.ptrarray = g_ptr_array_new();
1273 /* this is just to OR results together */
1279 /* or, store all unique values */
1281 htor(gchar *key, gint value, struct IterData *iter_data)
1283 g_ptr_array_add(iter_data->uids, key);
1286 /* and, only store duplicates */
1288 htand(gchar *key, gint value, struct IterData *iter_data)
1290 if (value == iter_data->count)
1291 g_ptr_array_add(iter_data->uids, key);
1295 match_message_index(CamelIndex *idx, const gchar *uid, const gchar *match, CamelException *ex)
1297 CamelIndexCursor *wc, *nc;
1298 const gchar *word, *name;
1301 wc = camel_index_words(idx);
1303 while (!truth && (word = camel_index_cursor_next(wc))) {
1304 if (camel_ustrstrcase(word,match) != NULL) {
1305 /* perf: could have the wc cursor return the name cursor */
1306 nc = camel_index_find(idx, word);
1308 while (!truth && (name = camel_index_cursor_next(nc)))
1309 truth = strcmp(name, uid) == 0;
1310 camel_object_unref((CamelObject *)nc);
1314 camel_object_unref((CamelObject *)wc);
1321 "one two" "three" "four five"
1330 /* returns messages which contain all words listed in words */
1332 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1334 GPtrArray *result = g_ptr_array_new();
1335 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1336 struct IterData lambdafoo;
1337 CamelIndexCursor *wc, *nc;
1338 const gchar *word, *name;
1341 /* we can have a maximum of 32 words, as we use it as the AND mask */
1343 wc = camel_index_words(search->body_index);
1345 while ((word = camel_index_cursor_next(wc))) {
1346 for (i=0;i<words->len;i++) {
1347 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1348 /* perf: could have the wc cursor return the name cursor */
1349 nc = camel_index_find(search->body_index, word);
1351 while ((name = camel_index_cursor_next(nc))) {
1354 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1355 g_hash_table_insert(ht, (gchar *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1357 camel_object_unref((CamelObject *)nc);
1362 camel_object_unref((CamelObject *)wc);
1364 lambdafoo.uids = result;
1365 lambdafoo.count = (1<<words->len) - 1;
1366 g_hash_table_foreach(ht, (GHFunc)htand, &lambdafoo);
1367 g_hash_table_destroy(ht);
1374 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1376 CamelDataWrapper *containee;
1380 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1382 if (containee == NULL)
1385 /* using the object types is more accurate than using the mime/types */
1386 if (CAMEL_IS_MULTIPART (containee)) {
1387 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1388 for (i = 0; i < parts && truth == FALSE; i++) {
1389 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1391 truth = match_words_1message(part, words, mask);
1393 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1394 /* for messages we only look at its contents */
1395 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1396 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1397 /* for all other text parts, we look inside, otherwise we dont care */
1398 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1400 /* FIXME: The match should be part of a stream op */
1401 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1402 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1403 for (i=0;i<words->len;i++) {
1404 /* FIXME: This is horridly slow, and should use a real search algorithm */
1405 if (camel_ustrstrcase((const gchar *) mem->buffer->data, words->words[i]->word) != NULL) {
1407 /* shortcut a match */
1408 if (*mask == (1<<(words->len))-1)
1413 camel_object_unref (mem);
1420 match_words_message(CamelFolder *folder, const gchar *uid, struct _camel_search_words *words, CamelException *ex)
1423 CamelMimeMessage *msg;
1424 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1427 msg = camel_folder_get_message(folder, uid, &x);
1430 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1431 camel_object_unref((CamelObject *)msg);
1433 camel_exception_clear(&x);
1441 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1444 GPtrArray *matches = g_ptr_array_new();
1446 if (search->body_index) {
1448 struct _camel_search_words *simple;
1450 simple = camel_search_words_simple(words);
1451 indexed = match_words_index(search, simple, ex);
1452 camel_search_words_free(simple);
1454 for (i=0;i<indexed->len;i++) {
1455 const gchar *uid = g_ptr_array_index(indexed, i);
1457 if (match_words_message(search->folder, uid, words, ex))
1458 g_ptr_array_add(matches, (gchar *)uid);
1461 g_ptr_array_free(indexed, TRUE);
1463 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1465 for (i=0;i<v->len;i++) {
1466 gchar *uid = g_ptr_array_index(v, i);
1468 if (match_words_message(search->folder, uid, words, ex))
1469 g_ptr_array_add(matches, (gchar *)uid);
1476 static ESExpResult *
1477 search_body_contains(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1480 CamelException *ex = search->priv->ex;
1481 struct _camel_search_words *words;
1483 struct IterData lambdafoo;
1485 if (search->current) {
1488 if (argc == 1 && argv[0]->value.string[0] == 0) {
1491 for (i=0;i<argc && !truth;i++) {
1492 if (argv[i]->type == ESEXP_RES_STRING) {
1493 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1495 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1496 for (j=0;j<words->len && truth;j++)
1497 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1499 /* TODO: cache current message incase of multiple body search terms */
1500 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1502 camel_search_words_free(words);
1506 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1507 r->value.bool = truth;
1509 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1510 r->value.ptrarray = g_ptr_array_new();
1512 if (argc == 1 && argv[0]->value.string[0] == 0) {
1513 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1515 for (i=0;i<v->len;i++) {
1516 gchar *uid = g_ptr_array_index(v, i);
1518 g_ptr_array_add(r->value.ptrarray, uid);
1521 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1524 for (i=0;i<argc;i++) {
1525 if (argv[i]->type == ESEXP_RES_STRING) {
1526 words = camel_search_words_split((const guchar *) argv[i]->value.string);
1527 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1528 matches = match_words_index(search, words, ex);
1530 matches = match_words_messages(search, words, ex);
1532 for (j=0;j<matches->len;j++) {
1533 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1535 g_ptr_array_free(matches, TRUE);
1536 camel_search_words_free(words);
1539 lambdafoo.uids = r->value.ptrarray;
1540 g_hash_table_foreach(ht, (GHFunc)htor, &lambdafoo);
1541 g_hash_table_destroy(ht);
1548 static ESExpResult *
1549 search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1552 CamelMimeMessage *msg = get_current_message (search);
1557 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1559 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1560 r->value.bool = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
1563 r->value.bool = FALSE;
1565 camel_object_unref (msg);
1569 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1570 r->value.ptrarray = g_ptr_array_new ();
1572 if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->ex) == 0) {
1574 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1575 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1576 CamelMimeMessage *message;
1578 for (i = 0; i < v->len; i++) {
1579 gchar *uid = g_ptr_array_index(v, i);
1581 message = camel_folder_get_message (search->folder, uid, &x);
1583 if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
1584 g_ptr_array_add (r->value.ptrarray, uid);
1587 camel_object_unref ((CamelObject *)message);
1589 camel_exception_clear (&x);
1600 static ESExpResult *
1601 search_user_flag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1606 r(printf("executing user-flag\n"));
1608 /* are we inside a match-all? */
1609 if (search->current) {
1611 /* performs an OR of all words */
1612 for (i=0;i<argc && !truth;i++) {
1613 if (argv[i]->type == ESEXP_RES_STRING
1614 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1619 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1620 r->value.bool = truth;
1622 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1623 r->value.ptrarray = g_ptr_array_new();
1629 static ESExpResult *
1630 search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1634 r(printf ("executing system-flag\n"));
1636 if (search->current) {
1637 gboolean truth = FALSE;
1640 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1642 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1643 r->value.bool = truth;
1645 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1646 r->value.ptrarray = g_ptr_array_new ();
1652 static ESExpResult *
1653 search_user_tag(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1655 const gchar *value = NULL;
1658 r(printf("executing user-tag\n"));
1660 if (search->current && argc == 1)
1661 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1663 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1664 r->value.string = g_strdup (value ? value : "");
1669 static ESExpResult *
1670 search_get_sent_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1674 r(printf("executing get-sent-date\n"));
1676 /* are we inside a match-all? */
1678 r = e_sexp_result_new(f, ESEXP_RES_INT);
1680 r->value.number = camel_message_info_date_sent(s->current);
1682 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1683 r->value.ptrarray = g_ptr_array_new ();
1689 static ESExpResult *
1690 search_get_received_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1694 r(printf("executing get-received-date\n"));
1696 /* are we inside a match-all? */
1698 r = e_sexp_result_new(f, ESEXP_RES_INT);
1700 r->value.number = camel_message_info_date_received(s->current);
1702 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1703 r->value.ptrarray = g_ptr_array_new ();
1709 static ESExpResult *
1710 search_get_current_date(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1714 r(printf("executing get-current-date\n"));
1716 r = e_sexp_result_new(f, ESEXP_RES_INT);
1717 r->value.number = time (NULL);
1721 static ESExpResult *
1722 search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1726 r(printf("executing get-size\n"));
1728 /* are we inside a match-all? */
1730 r = e_sexp_result_new (f, ESEXP_RES_INT);
1731 r->value.number = camel_message_info_size(s->current) / 1024;
1733 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1734 r->value.ptrarray = g_ptr_array_new ();
1740 static ESExpResult *
1741 search_uid(struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1746 r(printf("executing uid\n"));
1748 /* are we inside a match-all? */
1749 if (search->current) {
1751 const gchar *uid = camel_message_info_uid(search->current);
1753 /* performs an OR of all words */
1754 for (i=0;i<argc && !truth;i++) {
1755 if (argv[i]->type == ESEXP_RES_STRING
1756 && !strcmp(uid, argv[i]->value.string)) {
1761 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1762 r->value.bool = truth;
1764 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1765 r->value.ptrarray = g_ptr_array_new();
1766 for (i=0;i<argc;i++) {
1767 if (argv[i]->type == ESEXP_RES_STRING)
1768 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1776 read_uid_callback (gpointer ref, gint ncol, gchar ** cols, gchar **name)
1780 matches = (GPtrArray *) ref;
1782 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (cols [0]));
1786 static ESExpResult *
1787 search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1790 gboolean same = FALSE;
1792 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
1793 if (argv[0]->value.string && search->folder && search->folder->parent_store && camel_folder_get_full_name (search->folder)) {
1794 CamelFolderInfo *fi = camel_store_get_folder_info (search->folder->parent_store, camel_folder_get_full_name (search->folder), 0, NULL);
1796 same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
1798 camel_store_free_folder_info (search->folder->parent_store, fi);
1803 if (search->current) {
1804 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1805 r->value.bool = same ? TRUE : FALSE;
1807 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1808 r->value.ptrarray = g_ptr_array_new ();
1813 GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
1815 for (i = 0; i < v->len; i++) {
1816 gchar *uid = g_ptr_array_index (v, i);
1818 g_ptr_array_add (r->value.ptrarray, uid);