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-medium.h"
45 #include "camel-mime-message.h"
46 #include "camel-multipart.h"
47 #include "camel-search-private.h"
48 #include "camel-stream-mem.h"
50 #include "camel-store.h"
51 #include "camel-vee-folder.h"
52 #include "camel-string-utils.h"
53 #include "camel-search-sql.h"
58 struct _CamelFolderSearchPrivate {
61 CamelFolderThread *threads;
62 GHashTable *threads_hash;
65 #define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)
67 static ESExpResult *search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
69 static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
70 static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
71 static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
72 static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
73 static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
74 static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
75 static ESExpResult *search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
76 static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
77 static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
78 static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
79 static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
80 static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
81 static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
82 static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
83 static ESExpResult *search_get_size(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
84 static ESExpResult *search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
86 static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
88 static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
89 static void camel_folder_search_init (CamelFolderSearch *obj);
90 static void camel_folder_search_finalize (CamelObject *obj);
92 static int read_uid_callback (void * ref, int ncol, char ** cols, char **name);
94 static CamelObjectClass *camel_folder_search_parent;
97 camel_folder_search_class_init (CamelFolderSearchClass *klass)
99 camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
101 klass->not = search_not;
103 klass->match_all = search_match_all;
104 klass->match_threads = search_match_threads;
105 klass->body_contains = search_body_contains;
106 klass->header_contains = search_header_contains;
107 klass->header_matches = search_header_matches;
108 klass->header_starts_with = search_header_starts_with;
109 klass->header_ends_with = search_header_ends_with;
110 klass->header_exists = search_header_exists;
111 klass->user_tag = search_user_tag;
112 klass->user_flag = search_user_flag;
113 klass->system_flag = search_system_flag;
114 klass->get_sent_date = search_get_sent_date;
115 klass->get_received_date = search_get_received_date;
116 klass->get_current_date = search_get_current_date;
117 klass->get_size = search_get_size;
118 klass->uid = search_uid;
122 camel_folder_search_init (CamelFolderSearch *obj)
124 struct _CamelFolderSearchPrivate *p;
126 p = _PRIVATE(obj) = g_malloc0(sizeof(*p));
128 obj->sexp = e_sexp_new();
132 camel_folder_search_finalize (CamelObject *obj)
134 CamelFolderSearch *search = (CamelFolderSearch *)obj;
135 struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);
138 e_sexp_unref(search->sexp);
140 g_free(search->last_search);
145 camel_folder_search_get_type (void)
147 static CamelType type = CAMEL_INVALID_TYPE;
149 if (type == CAMEL_INVALID_TYPE) {
150 type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
151 sizeof (CamelFolderSearch),
152 sizeof (CamelFolderSearchClass),
153 (CamelObjectClassInitFunc) camel_folder_search_class_init,
155 (CamelObjectInitFunc) camel_folder_search_init,
156 (CamelObjectFinalizeFunc) camel_folder_search_finalize);
163 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) offsetof (type, field))
165 #define CAMEL_STRUCT_OFFSET(type, field) ((gint) ((gchar*) &((type *) 0)->field))
171 int flags; /* 0x02 = immediate, 0x01 = always enter */
173 /* these have default implementations in e-sexp */
174 { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
175 { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
176 /* we need to override this one though to implement an 'array not' */
177 { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 0 },
178 { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
179 { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
180 { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },
182 /* these we have to use our own default if there is none */
183 /* they should all be defined in the language? so it parses, or should they not?? */
184 { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
185 { "match-threads", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_threads), 3 },
186 { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
187 { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
188 { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
189 { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
190 { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
191 { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
192 { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
193 { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
194 { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
195 { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
196 { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
197 { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
198 { "get-size", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
199 { "uid", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
203 camel_folder_search_construct (CamelFolderSearch *search)
206 CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);
208 for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
210 /* c is sure messy sometimes */
211 func = *((void **)(((char *)klass)+builtins[i].offset));
212 if (func == NULL && builtins[i].flags&1) {
213 g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)));
214 func = (void *)search_dummy;
217 if (builtins[i].flags&2) {
218 e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
220 e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
227 * camel_folder_search_new:
229 * Create a new CamelFolderSearch object.
231 * A CamelFolderSearch is a subclassable, extensible s-exp
232 * evaluator which enforces a particular set of s-expressions.
233 * Particular methods may be overriden by an implementation to
234 * implement a search for any sort of backend.
236 * Return value: A new CamelFolderSearch widget.
239 camel_folder_search_new (void)
241 CamelFolderSearch *new = CAMEL_FOLDER_SEARCH (camel_object_new (camel_folder_search_get_type ()));
243 camel_folder_search_construct(new);
248 * camel_folder_search_set_folder:
252 * Set the folder attribute of the search. This is currently unused, but
253 * could be used to perform a slow-search when indexes and so forth are not
254 * available. Or for use by subclasses.
257 camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
259 search->folder = folder;
263 * camel_folder_search_set_summary:
265 * @summary: An array of CamelMessageInfo pointers.
267 * Set the array of summary objects representing the span of the search.
269 * If this is not set, then a subclass must provide the functions
270 * for searching headers and for the match-all operator.
273 camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
275 search->summary = summary;
279 * camel_folder_search_set_body_index:
283 * Set the index representing the contents of all messages
284 * in this folder. If this is not set, then the folder implementation
285 * should sub-class the CamelFolderSearch and provide its own
286 * body-contains function.
289 camel_folder_search_set_body_index(CamelFolderSearch *search, CamelIndex *index)
291 if (search->body_index)
292 camel_object_unref((CamelObject *)search->body_index);
293 search->body_index = index;
295 camel_object_ref((CamelObject *)index);
299 * camel_folder_search_execute_expression:
304 * Execute the search expression @expr, returning an array of
305 * all matches as a GPtrArray of uid's of matching messages.
307 * Note that any settings such as set_body_index(), set_folder(),
308 * and so on are reset to #NULL once the search has completed.
310 * TODO: The interface should probably return summary items instead
311 * (since they are much more useful to any client).
313 * Return value: A GPtrArray of strings of all matching messages.
314 * This must only be freed by camel_folder_search_free_result.
317 camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
323 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
327 /* only re-parse if the search has changed */
328 if (search->last_search == NULL
329 || strcmp(search->last_search, expr)) {
330 e_sexp_input_text(search->sexp, expr, strlen(expr));
331 if (e_sexp_parse(search->sexp) == -1) {
332 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
336 g_free(search->last_search);
337 search->last_search = g_strdup(expr);
339 r = e_sexp_eval(search->sexp);
341 if (!camel_exception_is_set(ex))
342 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
346 matches = g_ptr_array_new();
348 /* now create a folder summary to return?? */
349 if (r->type == ESEXP_RES_ARRAY_PTR) {
350 d(printf("got result ...\n"));
351 if (search->summary) {
352 /* reorder result in summary order */
353 results = g_hash_table_new(g_str_hash, g_str_equal);
354 for (i=0;i<r->value.ptrarray->len;i++) {
355 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
356 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
358 for (i=0;i<search->summary->len;i++) {
359 char *uid = g_ptr_array_index(search->summary, i);
360 if (g_hash_table_lookup(results, uid)) {
361 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
364 g_hash_table_destroy(results);
366 for (i=0;i<r->value.ptrarray->len;i++) {
367 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
368 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(g_ptr_array_index(r->value.ptrarray, i)));
372 g_warning("Search returned an invalid result type");
375 e_sexp_result_free(search->sexp, r);
378 camel_folder_thread_messages_unref(p->threads);
380 g_hash_table_destroy(p->threads_hash);
383 p->threads_hash = NULL;
384 search->folder = NULL;
385 search->summary = NULL;
386 search->current = NULL;
387 search->body_index = NULL;
393 * camel_folder_search_search:
396 * @uids: to search against, NULL for all uid's.
399 * Run a search. Search must have had Folder already set on it, and
400 * it must implement summaries.
405 camel_folder_search_search(CamelFolderSearch *search, const char *expr, GPtrArray *uids, CamelException *ex)
408 GPtrArray *matches = NULL, *summary_set;
411 char *sql_query, *tmp, *tmp1;
414 struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
416 g_assert(search->folder);
418 /* Sync the db, so that we search the db for changes */
419 camel_folder_summary_save_to_db (search->folder->summary, ex);
423 /* We route body-contains search and uid search through memory and not via db. */
424 if (uids || strstr((const char *) expr, "body-contains")) {
425 /* setup our search list only contains those we're interested in */
426 search->summary = camel_folder_get_summary(search->folder);
429 GHashTable *uids_hash = g_hash_table_new(g_str_hash, g_str_equal);
431 summary_set = search->summary_set = g_ptr_array_new();
432 for (i=0;i<uids->len;i++)
433 g_hash_table_insert(uids_hash, uids->pdata[i], uids->pdata[i]);
434 for (i=0;i<search->summary->len;i++)
435 if (g_hash_table_lookup(uids_hash, search->summary->pdata[i]))
436 g_ptr_array_add(search->summary_set, search->summary->pdata[i]);
437 g_hash_table_destroy(uids_hash);
439 summary_set = search->summary;
442 /* only re-parse if the search has changed */
443 if (search->last_search == NULL
444 || strcmp(search->last_search, expr)) {
445 e_sexp_input_text(search->sexp, expr, strlen(expr));
446 if (e_sexp_parse(search->sexp) == -1) {
447 camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
451 g_free(search->last_search);
452 search->last_search = g_strdup(expr);
454 r = e_sexp_eval(search->sexp);
456 if (!camel_exception_is_set(ex))
457 camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
461 matches = g_ptr_array_new();
463 /* now create a folder summary to return?? */
464 if (r->type == ESEXP_RES_ARRAY_PTR) {
465 d(printf("got result\n"));
467 /* reorder result in summary order */
468 results = g_hash_table_new(g_str_hash, g_str_equal);
469 for (i=0;i<r->value.ptrarray->len;i++) {
470 d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
471 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER (1));
474 for (i=0;i<summary_set->len;i++) {
475 char *uid = g_ptr_array_index(summary_set, i);
476 if (g_hash_table_lookup(results, uid))
477 g_ptr_array_add(matches, (gpointer) camel_pstring_strdup(uid));
479 g_hash_table_destroy(results);
482 e_sexp_result_free(search->sexp, r);
485 d(printf ("sexp is : [%s]\n", expr));
486 sql_query = camel_sexp_to_sql (expr);
487 tmp1 = camel_db_sqlize_string(search->folder->full_name);
488 tmp = g_strdup_printf ("SELECT uid FROM %s %s %s", tmp1, sql_query ? "WHERE":"", sql_query?sql_query:"");
489 camel_db_free_sqlized_string (tmp1);
491 d(printf("Equivalent sql %s\n", tmp));
493 matches = g_ptr_array_new();
494 cdb = (CamelDB *) (search->folder->cdb);
495 camel_db_select (cdb, tmp, (CamelDBSelectCB) read_uid_callback, matches, ex);
501 /* these might be allocated by match-threads */
503 camel_folder_thread_messages_unref(p->threads);
505 g_hash_table_destroy(p->threads_hash);
506 if (search->summary_set)
507 g_ptr_array_free(search->summary_set, TRUE);
509 camel_folder_free_summary(search->folder, search->summary);
512 p->threads_hash = NULL;
513 search->folder = NULL;
514 search->summary = NULL;
515 search->summary_set = NULL;
516 search->current = NULL;
517 search->body_index = NULL;
522 void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
524 g_ptr_array_foreach (result, (GFunc) camel_pstring_free, NULL);
525 g_ptr_array_free(result, TRUE);
528 /* dummy function, returns false always, or an empty match array */
530 search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
534 if (search->current == NULL) {
535 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
536 r->value.bool = FALSE;
538 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
539 r->value.ptrarray = g_ptr_array_new();
545 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
547 search_not(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
553 if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
554 GPtrArray *v = argv[0]->value.ptrarray;
557 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
558 r->value.ptrarray = g_ptr_array_new();
560 /* not against a single message?*/
561 if (search->current) {
564 uid = camel_message_info_uid(search->current);
565 for (i=0;!found && i<v->len;i++) {
566 if (strcmp(uid, v->pdata[i]) == 0)
571 g_ptr_array_add(r->value.ptrarray, (char *)uid);
572 } else if (search->summary == NULL) {
573 g_warning("No summary set, 'not' against an array requires a summary");
575 /* 'not' against the whole summary */
576 GHashTable *have = g_hash_table_new(g_str_hash, g_str_equal);
580 s = (char **)v->pdata;
581 for (i=0;i<v->len;i++)
582 g_hash_table_insert(have, s[i], s[i]);
584 v = search->summary_set?search->summary_set:search->summary;
585 m = (char **)v->pdata;
586 for (i=0;i<v->len;i++) {
589 if (g_hash_table_lookup(have, uid) == NULL)
590 g_ptr_array_add(r->value.ptrarray, uid);
592 g_hash_table_destroy(have);
597 if (argv[0]->type == ESEXP_RES_BOOL)
598 res = ! argv[0]->value.bool;
600 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
604 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
605 r->value.bool = TRUE;
612 search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
620 g_warning("match-all only takes a single argument, other arguments ignored");
623 /* we are only matching a single message? or already inside a match-all? */
624 if (search->current) {
625 d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
627 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
628 r->value.bool = FALSE;
631 r1 = e_sexp_term_eval(f, argv[0]);
632 if (r1->type == ESEXP_RES_BOOL) {
633 r->value.bool = r1->value.bool;
635 g_warning("invalid syntax, matches require a single bool result");
636 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
637 e_sexp_fatal_error(f, error_msg);
640 e_sexp_result_free(f, r1);
642 r->value.bool = TRUE;
647 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
648 r->value.ptrarray = g_ptr_array_new();
650 if (search->summary == NULL) {
651 /* TODO: make it work - e.g. use the folder and so forth for a slower search */
652 g_warning("No summary supplied, match-all doesn't work with no summary");
657 v = search->summary_set?search->summary_set:search->summary;
659 if (v->len != g_hash_table_size (search->folder->summary->loaded_infos) && !CAMEL_IS_VEE_FOLDER (search->folder)) {
660 camel_folder_summary_reload_from_db (search->folder->summary, search->priv->ex);
663 e_sexp_term_eval (f, argv [0]);
665 for (i=0;i<v->len;i++) {
668 search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
669 uid = camel_message_info_uid(search->current);
672 r1 = e_sexp_term_eval(f, argv[0]);
673 if (r1->type == ESEXP_RES_BOOL) {
675 g_ptr_array_add(r->value.ptrarray, (char *)uid);
677 g_warning("invalid syntax, matches require a single bool result");
678 error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
679 e_sexp_fatal_error(f, error_msg);
682 e_sexp_result_free(f, r1);
684 g_ptr_array_add(r->value.ptrarray, (char *)uid);
686 camel_message_info_free (search->current);
688 search->current = NULL;
693 fill_thread_table(struct _CamelFolderThreadNode *root, GHashTable *id_hash)
696 g_hash_table_insert(id_hash, (char *)camel_message_info_uid(root->message), root);
698 fill_thread_table(root->child, id_hash);
704 add_thread_results(struct _CamelFolderThreadNode *root, GHashTable *result_hash)
707 g_hash_table_insert(result_hash, (char *)camel_message_info_uid(root->message), GINT_TO_POINTER (1));
709 add_thread_results(root->child, result_hash);
715 add_results(char *uid, void *dummy, GPtrArray *result)
717 g_ptr_array_add(result, uid);
721 search_match_threads(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
724 struct _CamelFolderSearchPrivate *p = search->priv;
729 /* not supported in match-all */
730 if (search->current) {
731 error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
732 e_sexp_fatal_error(f, error_msg);
737 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
738 e_sexp_fatal_error(f, error_msg);
742 r = e_sexp_term_eval(f, argv[0]);
743 if (r->type != ESEXP_RES_STRING) {
744 error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
745 e_sexp_fatal_error(f, error_msg);
750 if (!strcmp(r->value.string, "none"))
752 else if (!strcmp(r->value.string, "all"))
754 else if (!strcmp(r->value.string, "replies"))
756 else if (!strcmp(r->value.string, "replies_parents"))
758 else if (!strcmp(r->value.string, "single"))
760 e_sexp_result_free(f, r);
762 /* behave as (begin does */
764 for (i=1;i<argc;i++) {
766 e_sexp_result_free(f, r);
767 r = e_sexp_term_eval(f, argv[i]);
770 if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
771 error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
772 e_sexp_fatal_error(f, error_msg);
779 if (search->folder == NULL) {
780 error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
781 e_sexp_fatal_error(f, error_msg);
785 /* cache this, so we only have to re-calculate once per search at most */
786 if (p->threads == NULL) {
787 p->threads = camel_folder_thread_messages_new(search->folder, NULL, TRUE);
788 p->threads_hash = g_hash_table_new(g_str_hash, g_str_equal);
790 fill_thread_table(p->threads->tree, p->threads_hash);
793 results = g_hash_table_new(g_str_hash, g_str_equal);
794 for (i=0;i<r->value.ptrarray->len;i++) {
795 struct _CamelFolderThreadNode *node, *scan;
798 g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), GINT_TO_POINTER(1));
800 node = g_hash_table_lookup(p->threads_hash, (char *)g_ptr_array_index(r->value.ptrarray, i));
801 if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
804 /* select messages in thread according to search criteria */
806 if (node->child == NULL && node->parent == NULL)
807 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
811 while (scan && scan->parent) {
813 g_hash_table_insert(results, (char *)camel_message_info_uid(scan->message), GINT_TO_POINTER(1));
815 } else if (type == 1) {
816 while (node && node->parent)
819 g_hash_table_insert(results, (char *)camel_message_info_uid(node->message), GINT_TO_POINTER(1));
821 add_thread_results(node->child, results);
824 e_sexp_result_free(f, r);
826 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
827 r->value.ptrarray = g_ptr_array_new();
829 g_hash_table_foreach(results, (GHFunc)add_results, r->value.ptrarray);
830 g_hash_table_destroy(results);
836 check_header (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
841 r(printf("executing check-header %d\n", how));
843 /* are we inside a match-all? */
844 if (search->current && argc>1
845 && argv[0]->type == ESEXP_RES_STRING) {
847 const char *header = NULL;
850 camel_search_t type = CAMEL_SEARCH_TYPE_ASIS;
851 struct _camel_search_words *words;
853 /* only a subset of headers are supported .. */
854 headername = argv[0]->value.string;
855 if (!g_ascii_strcasecmp(headername, "subject")) {
856 header = camel_message_info_subject(search->current);
857 } else if (!g_ascii_strcasecmp(headername, "date")) {
858 /* FIXME: not a very useful form of the date */
859 sprintf(strbuf, "%d", (int)camel_message_info_date_sent(search->current));
861 } else if (!g_ascii_strcasecmp(headername, "from")) {
862 header = camel_message_info_from(search->current);
863 type = CAMEL_SEARCH_TYPE_ADDRESS;
864 } else if (!g_ascii_strcasecmp(headername, "to")) {
865 header = camel_message_info_to(search->current);
866 type = CAMEL_SEARCH_TYPE_ADDRESS;
867 } else if (!g_ascii_strcasecmp(headername, "cc")) {
868 header = camel_message_info_cc(search->current);
869 type = CAMEL_SEARCH_TYPE_ADDRESS;
870 } else if (!g_ascii_strcasecmp(headername, "x-camel-mlist")) {
871 header = camel_message_info_mlist(search->current);
872 type = CAMEL_SEARCH_TYPE_MLIST;
874 e_sexp_resultv_free(f, argc, argv);
875 e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
881 /* performs an OR of all words */
882 for (i=1;i<argc && !truth;i++) {
883 if (argv[i]->type == ESEXP_RES_STRING) {
884 if (argv[i]->value.string[0] == 0) {
886 } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
887 /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
888 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
890 for (j=0;j<words->len && truth;j++) {
891 truth = camel_search_header_match(header, words->words[j]->word, how, type, NULL);
893 camel_search_words_free(words);
895 truth = camel_search_header_match(header, argv[i]->value.string, how, type, NULL);
900 /* TODO: else, find all matches */
902 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
903 r->value.bool = truth;
912 printf("%s\t", node);
917 search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
919 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
923 search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
925 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
929 search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
931 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
935 search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
937 return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
941 search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
945 r(printf ("executing header-exists\n"));
947 if (search->current) {
948 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
949 if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
950 r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
953 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
954 r->value.ptrarray = g_ptr_array_new();
960 /* this is just to OR results together */
961 struct _glib_sux_donkeys {
966 /* or, store all unique values */
968 g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
970 g_ptr_array_add(fuckup->uids, key);
973 /* and, only store duplicates */
975 g_lib_sux_htand(char *key, int value, struct _glib_sux_donkeys *fuckup)
977 if (value == fuckup->count)
978 g_ptr_array_add(fuckup->uids, key);
982 match_message_index(CamelIndex *idx, const char *uid, const char *match, CamelException *ex)
984 CamelIndexCursor *wc, *nc;
985 const char *word, *name;
988 wc = camel_index_words(idx);
990 while (!truth && (word = camel_index_cursor_next(wc))) {
991 if (camel_ustrstrcase(word,match) != NULL) {
992 /* perf: could have the wc cursor return the name cursor */
993 nc = camel_index_find(idx, word);
995 while (!truth && (name = camel_index_cursor_next(nc)))
996 truth = strcmp(name, uid) == 0;
997 camel_object_unref((CamelObject *)nc);
1001 camel_object_unref((CamelObject *)wc);
1008 "one two" "three" "four five"
1017 /* returns messages which contain all words listed in words */
1019 match_words_index(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1021 GPtrArray *result = g_ptr_array_new();
1022 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1023 struct _glib_sux_donkeys lambdafoo;
1024 CamelIndexCursor *wc, *nc;
1025 const char *word, *name;
1028 /* we can have a maximum of 32 words, as we use it as the AND mask */
1030 wc = camel_index_words(search->body_index);
1032 while ((word = camel_index_cursor_next(wc))) {
1033 for (i=0;i<words->len;i++) {
1034 if (camel_ustrstrcase(word, words->words[i]->word) != NULL) {
1035 /* perf: could have the wc cursor return the name cursor */
1036 nc = camel_index_find(search->body_index, word);
1038 while ((name = camel_index_cursor_next(nc))) {
1041 mask = (GPOINTER_TO_INT(g_hash_table_lookup(ht, name))) | (1<<i);
1042 g_hash_table_insert(ht, (char *) camel_pstring_peek(name), GINT_TO_POINTER(mask));
1044 camel_object_unref((CamelObject *)nc);
1049 camel_object_unref((CamelObject *)wc);
1051 lambdafoo.uids = result;
1052 lambdafoo.count = (1<<words->len) - 1;
1053 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htand, &lambdafoo);
1054 g_hash_table_destroy(ht);
1061 match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
1063 CamelDataWrapper *containee;
1067 containee = camel_medium_get_content_object (CAMEL_MEDIUM (object));
1069 if (containee == NULL)
1072 /* using the object types is more accurate than using the mime/types */
1073 if (CAMEL_IS_MULTIPART (containee)) {
1074 parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
1075 for (i = 0; i < parts && truth == FALSE; i++) {
1076 CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
1078 truth = match_words_1message(part, words, mask);
1080 } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
1081 /* for messages we only look at its contents */
1082 truth = match_words_1message((CamelDataWrapper *)containee, words, mask);
1083 } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
1084 /* for all other text parts, we look inside, otherwise we dont care */
1085 CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new ();
1087 /* FIXME: The match should be part of a stream op */
1088 camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem));
1089 camel_stream_write (CAMEL_STREAM (mem), "", 1);
1090 for (i=0;i<words->len;i++) {
1091 /* FIXME: This is horridly slow, and should use a real search algorithm */
1092 if (camel_ustrstrcase((const char *) mem->buffer->data, words->words[i]->word) != NULL) {
1094 /* shortcut a match */
1095 if (*mask == (1<<(words->len))-1)
1100 camel_object_unref (mem);
1107 match_words_message(CamelFolder *folder, const char *uid, struct _camel_search_words *words, CamelException *ex)
1110 CamelMimeMessage *msg;
1111 CamelException x = CAMEL_EXCEPTION_INITIALISER;
1114 msg = camel_folder_get_message(folder, uid, &x);
1117 truth = match_words_1message((CamelDataWrapper *)msg, words, &mask);
1118 camel_object_unref((CamelObject *)msg);
1120 camel_exception_clear(&x);
1128 match_words_messages(CamelFolderSearch *search, struct _camel_search_words *words, CamelException *ex)
1131 GPtrArray *matches = g_ptr_array_new();
1133 if (search->body_index) {
1135 struct _camel_search_words *simple;
1137 simple = camel_search_words_simple(words);
1138 indexed = match_words_index(search, simple, ex);
1139 camel_search_words_free(simple);
1141 for (i=0;i<indexed->len;i++) {
1142 const char *uid = g_ptr_array_index(indexed, i);
1144 if (match_words_message(search->folder, uid, words, ex))
1145 g_ptr_array_add(matches, (char *)uid);
1148 g_ptr_array_free(indexed, TRUE);
1150 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1152 for (i=0;i<v->len;i++) {
1153 char *uid = g_ptr_array_index(v, i);
1155 if (match_words_message(search->folder, uid, words, ex))
1156 g_ptr_array_add(matches, (char *)uid);
1163 static ESExpResult *
1164 search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1167 CamelException *ex = search->priv->ex;
1168 struct _camel_search_words *words;
1170 struct _glib_sux_donkeys lambdafoo;
1172 if (search->current) {
1175 if (argc == 1 && argv[0]->value.string[0] == 0) {
1178 for (i=0;i<argc && !truth;i++) {
1179 if (argv[i]->type == ESEXP_RES_STRING) {
1180 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1182 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1183 for (j=0;j<words->len && truth;j++)
1184 truth = match_message_index(search->body_index, camel_message_info_uid(search->current), words->words[j]->word, ex);
1186 /* TODO: cache current message incase of multiple body search terms */
1187 truth = match_words_message(search->folder, camel_message_info_uid(search->current), words, ex);
1189 camel_search_words_free(words);
1193 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1194 r->value.bool = truth;
1196 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1197 r->value.ptrarray = g_ptr_array_new();
1199 if (argc == 1 && argv[0]->value.string[0] == 0) {
1200 GPtrArray *v = search->summary_set?search->summary_set:search->summary;
1202 for (i=0;i<v->len;i++) {
1203 char *uid = g_ptr_array_index(v, i);
1205 g_ptr_array_add(r->value.ptrarray, uid);
1208 GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
1211 for (i=0;i<argc;i++) {
1212 if (argv[i]->type == ESEXP_RES_STRING) {
1213 words = camel_search_words_split((const unsigned char *) argv[i]->value.string);
1214 if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
1215 matches = match_words_index(search, words, ex);
1217 matches = match_words_messages(search, words, ex);
1219 for (j=0;j<matches->len;j++) {
1220 g_hash_table_insert(ht, matches->pdata[j], matches->pdata[j]);
1222 g_ptr_array_free(matches, TRUE);
1223 camel_search_words_free(words);
1226 lambdafoo.uids = r->value.ptrarray;
1227 g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
1228 g_hash_table_destroy(ht);
1235 static ESExpResult *
1236 search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1241 r(printf("executing user-flag\n"));
1243 /* are we inside a match-all? */
1244 if (search->current) {
1246 /* performs an OR of all words */
1247 for (i=0;i<argc && !truth;i++) {
1248 if (argv[i]->type == ESEXP_RES_STRING
1249 && camel_message_info_user_flag(search->current, argv[i]->value.string)) {
1254 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1255 r->value.bool = truth;
1257 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1258 r->value.ptrarray = g_ptr_array_new();
1264 static ESExpResult *
1265 search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1269 r(printf ("executing system-flag\n"));
1271 if (search->current) {
1272 gboolean truth = FALSE;
1275 truth = camel_system_flag_get (camel_message_info_flags(search->current), argv[0]->value.string);
1277 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1278 r->value.bool = truth;
1280 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1281 r->value.ptrarray = g_ptr_array_new ();
1287 static ESExpResult *
1288 search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1290 const char *value = NULL;
1293 r(printf("executing user-tag\n"));
1295 if (search->current && argc == 1)
1296 value = camel_message_info_user_tag(search->current, argv[0]->value.string);
1298 r = e_sexp_result_new(f, ESEXP_RES_STRING);
1299 r->value.string = g_strdup (value ? value : "");
1304 static ESExpResult *
1305 search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1309 r(printf("executing get-sent-date\n"));
1311 /* are we inside a match-all? */
1313 r = e_sexp_result_new(f, ESEXP_RES_INT);
1315 r->value.number = camel_message_info_date_sent(s->current);
1317 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1318 r->value.ptrarray = g_ptr_array_new ();
1324 static ESExpResult *
1325 search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1329 r(printf("executing get-received-date\n"));
1331 /* are we inside a match-all? */
1333 r = e_sexp_result_new(f, ESEXP_RES_INT);
1335 r->value.number = camel_message_info_date_received(s->current);
1337 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1338 r->value.ptrarray = g_ptr_array_new ();
1344 static ESExpResult *
1345 search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1349 r(printf("executing get-current-date\n"));
1351 r = e_sexp_result_new(f, ESEXP_RES_INT);
1352 r->value.number = time (NULL);
1356 static ESExpResult *
1357 search_get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
1361 r(printf("executing get-size\n"));
1363 /* are we inside a match-all? */
1365 r = e_sexp_result_new (f, ESEXP_RES_INT);
1366 r->value.number = camel_message_info_size(s->current) / 1024;
1368 r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
1369 r->value.ptrarray = g_ptr_array_new ();
1375 static ESExpResult *
1376 search_uid(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
1381 r(printf("executing uid\n"));
1383 /* are we inside a match-all? */
1384 if (search->current) {
1386 const char *uid = camel_message_info_uid(search->current);
1388 /* performs an OR of all words */
1389 for (i=0;i<argc && !truth;i++) {
1390 if (argv[i]->type == ESEXP_RES_STRING
1391 && !strcmp(uid, argv[i]->value.string)) {
1396 r = e_sexp_result_new(f, ESEXP_RES_BOOL);
1397 r->value.bool = truth;
1399 r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
1400 r->value.ptrarray = g_ptr_array_new();
1401 for (i=0;i<argc;i++) {
1402 if (argv[i]->type == ESEXP_RES_STRING)
1403 g_ptr_array_add(r->value.ptrarray, argv[i]->value.string);
1411 read_uid_callback (void * ref, int ncol, char ** cols, char **name)
1415 matches = (GPtrArray *) ref;
1417 g_ptr_array_add (matches, (GFunc) camel_pstring_strdup (cols [0]));