aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-folder-search.c
blob: 546dbe66edbf037766e50da9d901ad02443dd6c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
 *  Copyright (C) 2000,2001 Ximian Inc.
 *
 *  Authors: Michael Zucchi <notzed@helixcode.com>
 *
 *  This program is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */

/* This is a helper class for folders to implement the search function.
   It implements enough to do basic searches on folders that can provide
   an in-memory summary and a body index. */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include <sys/types.h>
#include <regex.h>

#include "camel-folder-search.h"
#include "string-utils.h"

#include "camel-exception.h"
#include "camel-medium.h"
#include "camel-multipart.h"
#include "camel-mime-message.h"
#include "camel-stream-mem.h"
#include "e-util/e-memory.h"
#include "camel-search-private.h"

#define d(x) 
#define r(x) 

struct _CamelFolderSearchPrivate {
    GHashTable *mempool_hash;
    CamelException *ex;
};

#define _PRIVATE(o) (((CamelFolderSearch *)(o))->priv)

static ESExpResult *search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_header_starts_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_header_ends_with(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_header_exists(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
static ESExpResult *search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);
static ESExpResult *search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
static ESExpResult *search_system_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
static ESExpResult *search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
static ESExpResult *search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);
static ESExpResult *search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s);

static ESExpResult *search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search);

static void camel_folder_search_class_init (CamelFolderSearchClass *klass);
static void camel_folder_search_init       (CamelFolderSearch *obj);
static void camel_folder_search_finalize   (CamelObject *obj);

static CamelObjectClass *camel_folder_search_parent;

static void
camel_folder_search_class_init (CamelFolderSearchClass *klass)
{
    camel_folder_search_parent = camel_type_get_global_classfuncs (camel_object_get_type ());

    klass->match_all = search_match_all;
    klass->body_contains = search_body_contains;
    klass->header_contains = search_header_contains;
    klass->header_matches = search_header_matches;
    klass->header_starts_with = search_header_starts_with;
    klass->header_ends_with = search_header_ends_with;
    klass->header_exists = search_header_exists;
    klass->user_tag = search_user_tag;
    klass->user_flag = search_user_flag;
    klass->system_flag = search_system_flag;
    klass->get_sent_date = search_get_sent_date;
    klass->get_received_date = search_get_received_date;
    klass->get_current_date = search_get_current_date;
}

static void
camel_folder_search_init (CamelFolderSearch *obj)
{
    struct _CamelFolderSearchPrivate *p;

    p = _PRIVATE(obj) = g_malloc0(sizeof(*p));

    obj->sexp = e_sexp_new();

    /* use a hash of mempools to associate the returned uid lists with
       the backing mempool.  yes pretty weird, but i didn't want to change
       the api just yet */

    p->mempool_hash = g_hash_table_new(0, 0);
}

static void
free_mempool(void *key, void *value, void *data)
{
    GPtrArray *uids = key;
    EMemPool *pool = value;

    g_warning("Search closed with outstanding result unfreed: %p", uids);

    g_ptr_array_free(uids, TRUE);
    e_mempool_destroy(pool);
}

static void
camel_folder_search_finalize (CamelObject *obj)
{
    CamelFolderSearch *search = (CamelFolderSearch *)obj;
    struct _CamelFolderSearchPrivate *p = _PRIVATE(obj);

    if (search->sexp)
        e_sexp_unref(search->sexp);

    g_free(search->last_search);
    g_hash_table_foreach(p->mempool_hash, free_mempool, obj);
    g_hash_table_destroy(p->mempool_hash);
    g_free(p);
}

CamelType
camel_folder_search_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type = camel_type_register (camel_object_get_type (), "CamelFolderSearch",
                        sizeof (CamelFolderSearch),
                        sizeof (CamelFolderSearchClass),
                        (CamelObjectClassInitFunc) camel_folder_search_class_init,
                        NULL,
                        (CamelObjectInitFunc) camel_folder_search_init,
                        (CamelObjectFinalizeFunc) camel_folder_search_finalize);
    }
    
    return type;
}

#ifdef offsetof
#define CAMEL_STRUCT_OFFSET(type, field)        ((gint) offsetof (type, field))
#else
#define CAMEL_STRUCT_OFFSET(type, field)        ((gint) ((gchar*) &((type *) 0)->field))
#endif

struct {
    char *name;
    int offset;
    int flags;      /* 0x02 = immediate, 0x01 = always enter */
} builtins[] = {
    /* these have default implementations in e-sexp */
    { "and", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, and), 2 },
    { "or", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, or), 2 },
    { "not", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, not), 2 },
    { "<", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, lt), 2 },
    { ">", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, gt), 2 },
    { "=", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, eq), 2 },

    /* these we have to use our own default if there is none */
    /* they should all be defined in the language? so it poarses, or should they not?? */
    { "match-all", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, match_all), 3 },
    { "body-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, body_contains), 1 },
    { "header-contains", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_contains), 1 },
    { "header-matches", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_matches), 1 },
    { "header-starts-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_starts_with), 1 },
    { "header-ends-with", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_ends_with), 1 },
    { "header-exists", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, header_exists), 1 },
    { "user-tag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_tag), 1 },
    { "user-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, user_flag), 1 },
    { "system-flag", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, system_flag), 1 },
    { "get-sent-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
    { "get-received-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
    { "get-current-date", CAMEL_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
};

void
camel_folder_search_construct (CamelFolderSearch *search)
{
    int i;
    CamelFolderSearchClass *klass = (CamelFolderSearchClass *)CAMEL_OBJECT_GET_CLASS(search);

    for (i=0;i<sizeof(builtins)/sizeof(builtins[0]);i++) {
        void *func;
        /* c is sure messy sometimes */
        func = *((void **)(((char *)klass)+builtins[i].offset));
        if (func == NULL && builtins[i].flags&1) {
            g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, camel_type_to_name(CAMEL_OBJECT_GET_CLASS(search)->s.type));
            func = (void *)search_dummy;
        }
        if (func != NULL) {
            if (builtins[i].flags&2) {
                e_sexp_add_ifunction(search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
            } else {
                e_sexp_add_function(search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
            }
        }
    }
}

/**
 * camel_folder_search_new:
 *
 * Create a new CamelFolderSearch object.
 * 
 * A CamelFolderSearch is a subclassable, extensible s-exp
 * evaluator which enforces a particular set of s-expressions.
 * Particular methods may be overriden by an implementation to
 * implement a search for any sort of backend.
 *
 * Return value: A new CamelFolderSearch widget.
 **/
CamelFolderSearch *
camel_folder_search_new (void)
{
    CamelFolderSearch *new = CAMEL_FOLDER_SEARCH ( camel_object_new (camel_folder_search_get_type ()));

    camel_folder_search_construct(new);
    return new;
}

/**
 * camel_folder_search_set_folder:
 * @search:
 * @folder: A folder.
 * 
 * Set the folder attribute of the search.  This is currently unused, but
 * could be used to perform a slow-search when indexes and so forth are not
 * available.  Or for use by subclasses.
 **/
void
camel_folder_search_set_folder(CamelFolderSearch *search, CamelFolder *folder)
{
    search->folder = folder;
}

/**
 * camel_folder_search_set_summary:
 * @search: 
 * @summary: An array of CamelMessageInfo pointers.
 * 
 * Set the array of summary objects representing the span of the search.
 *
 * If this is not set, then a subclass must provide the functions
 * for searching headers and for the match-all operator.
 **/
void
camel_folder_search_set_summary(CamelFolderSearch *search, GPtrArray *summary)
{
    search->summary = summary;
}

/**
 * camel_folder_search_set_body_index:
 * @search: 
 * @index: 
 * 
 * Set the index (ibex) representing the contents of all messages
 * in this folder.  If this is not set, then the folder implementation
 * should sub-class the CamelFolderSearch and provide its own
 * body-contains function.
 **/
void
camel_folder_search_set_body_index(CamelFolderSearch *search, ibex *index)
{
    search->body_index = index;
}

/**
 * camel_folder_search_execute_expression:
 * @search: 
 * @expr: 
 * @ex: 
 * 
 * Execute the search expression @expr, returning an array of
 * all matches as a GPtrArray of uid's of matching messages.
 *
 * Note that any settings such as set_body_index(), set_folder(),
 * and so on are reset to #NULL once the search has completed.
 *
 * TODO: The interface should probably return summary items instead
 * (since they are much more useful to any client).
 * 
 * Return value: A GPtrArray of strings of all matching messages.
 * This must only be freed by camel_folder_search_free_result.
 **/
GPtrArray *
camel_folder_search_execute_expression(CamelFolderSearch *search, const char *expr, CamelException *ex)
{
    ESExpResult *r;
    GPtrArray *matches;
    int i;
    GHashTable *results;
    EMemPool *pool;
    struct _CamelFolderSearchPrivate *p = _PRIVATE(search);

    p->ex = ex;

    /* only re-parse if the search has changed */
    if (search->last_search == NULL
        || strcmp(search->last_search, expr)) {
        e_sexp_input_text(search->sexp, expr, strlen(expr));
        if (e_sexp_parse(search->sexp) == -1) {
            camel_exception_setv(ex, 1, _("Cannot parse search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
            return NULL;
        }

        g_free(search->last_search);
        search->last_search = g_strdup(expr);
    }
    r = e_sexp_eval(search->sexp);
    if (r == NULL) {
        if (!camel_exception_is_set(ex))
            camel_exception_setv(ex, 1, _("Error executing search expression: %s:\n%s"), e_sexp_error(search->sexp), expr);
        return NULL;
    }

    matches = g_ptr_array_new();

    /* now create a folder summary to return?? */
    if (r
        && r->type == ESEXP_RES_ARRAY_PTR) {
        d(printf("got result ...\n"));
        /* we use a mempool to store the strings, packed in tight as possible, and freed together */
        /* because the strings are often short (like <8 bytes long), we would be wasting appx 50%
           of memory just storing the size tag that malloc assigns us and alignment padding, so this
           gets around that (and is faster to allocate and free as a bonus) */
        pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
        if (search->summary) {
            /* reorder result in summary order */
            results = g_hash_table_new(g_str_hash, g_str_equal);
            for (i=0;i<r->value.ptrarray->len;i++) {
                d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
                g_hash_table_insert(results, g_ptr_array_index(r->value.ptrarray, i), (void *)1);
            }
            for (i=0;i<search->summary->len;i++) {
                CamelMessageInfo *info = g_ptr_array_index(search->summary, i);
                char *uid = (char *)camel_message_info_uid(info);
                if (g_hash_table_lookup(results, uid)) {
                    g_ptr_array_add(matches, e_mempool_strdup(pool, uid));
                }
            }
            g_hash_table_destroy(results);
        } else {
            for (i=0;i<r->value.ptrarray->len;i++) {
                d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
                g_ptr_array_add(matches, e_mempool_strdup(pool, g_ptr_array_index(r->value.ptrarray, i)));
            }
        }
        e_sexp_result_free(search->sexp, r);
        /* instead of putting the mempool_hash in the structure, we keep the api clean by
           putting a reference to it in a hashtable.  Lets us do some debugging and catch
           unfree'd results as well. */
        g_hash_table_insert(p->mempool_hash, matches, pool);
    } else {
        printf("no result!\n");
    }

    search->folder = NULL;
    search->summary = NULL;
    search->current = NULL;
    search->body_index = NULL;

    return matches;
}

/**
 * camel_folder_search_match_expression:
 * @search: 
 * @expr: 
 * @info: 
 * @ex: 
 * 
 * Returns #TRUE if the expression matches the specific message info @info.
 * Note that the folder and index may need to be set for body searches to
 * operate as well.
 * 
 * Return value: 
 **/
gboolean
camel_folder_search_match_expression(CamelFolderSearch *search, const char *expr, const CamelMessageInfo *info, CamelException *ex)
{
    GPtrArray *uids;
    int ret = FALSE;

    search->match1 = (CamelMessageInfo *)info;

    uids = camel_folder_search_execute_expression(search, expr, ex);
    if (uids) {
        if (uids->len == 1)
            ret = TRUE;
        camel_folder_search_free_result(search, uids);
    }
    search->match1 = NULL;

    return ret;
}

void camel_folder_search_free_result(CamelFolderSearch *search, GPtrArray *result)
{
    int i;
    struct _CamelFolderSearchPrivate *p = _PRIVATE(search);
    EMemPool *pool;

    pool = g_hash_table_lookup(p->mempool_hash, result);
    if (pool) {
        e_mempool_destroy(pool);
        g_hash_table_remove(p->mempool_hash, result);
    } else {
        for (i=0;i<result->len;i++)
            g_free(g_ptr_array_index(result, i));
    }
    g_ptr_array_free(result, TRUE);
}




/* dummy function, returns false always, or an empty match array */
static ESExpResult *
search_dummy(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;

    if (search->current == NULL) {
        r = e_sexp_result_new(f, ESEXP_RES_BOOL);
        r->value.bool = FALSE;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new();
    }

    return r;
}

static ESExpResult *
search_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
{
    int i;
    ESExpResult *r, *r1;

    if (argc>1) {
        g_warning("match-all only takes a single argument, other arguments ignored");
    }
    r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
    r->value.ptrarray = g_ptr_array_new();

    /* we are only matching a single message? */
    if (search->match1) {
        search->current = search->match1;

        d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));

        if (argc>0) {
            r1 = e_sexp_term_eval(f, argv[0]);
            if (r1->type == ESEXP_RES_BOOL) {
                if (r1->value.bool)
                    g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
            } else {
                g_warning("invalid syntax, matches require a single bool result");
                e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
            }
            e_sexp_result_free(f, r1);
        } else {
            g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
        }
        search->current = NULL;

        return r;
    }

    if (search->summary == NULL) {
        /* TODO: make it work - e.g. use the folder and so forth for a slower search */
        g_warning("No summary supplied, match-all doesn't work with no summary");
        g_assert(0);
        return r;
    }

    /* TODO: Could make this a bit faster in the uncommon case (of match-everything) */
    for (i=0;i<search->summary->len;i++) {
        search->current = g_ptr_array_index(search->summary, i);
        if (argc>0) {
            r1 = e_sexp_term_eval(f, argv[0]);
            if (r1->type == ESEXP_RES_BOOL) {
                if (r1->value.bool)
                    g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
            } else {
                g_warning("invalid syntax, matches require a single bool result");
                e_sexp_fatal_error(f, _("(match-all) requires a single bool result"));
            }
            e_sexp_result_free(f, r1);
        } else {
            g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(search->current));
        }
    }
    search->current = NULL;

    return r;
}

static ESExpResult *
check_header(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
{
    ESExpResult *r;
    int truth = FALSE;

    r(printf("executing check-header %d\n", how));

    /* are we inside a match-all? */
    if (search->current && argc>1
        && argv[0]->type == ESEXP_RES_STRING) {
        char *headername;
        const char *header = NULL;
        char strbuf[32];
        int i;

        /* only a subset of headers are supported .. */
        headername = argv[0]->value.string;
        if (!strcasecmp(headername, "subject")) {
            header = camel_message_info_subject(search->current);
        } else if (!strcasecmp(headername, "date")) {
            /* FIXME: not a very useful form of the date */
            sprintf(strbuf, "%d", (int)search->current->date_sent);
            header = strbuf;
        } else if (!strcasecmp(headername, "from")) {
            header = camel_message_info_from(search->current);
        } else if (!strcasecmp(headername, "to")) {
            header = camel_message_info_to(search->current);
        } else if (!strcasecmp(headername, "cc")) {
            header = camel_message_info_cc(search->current);
        } else {
            e_sexp_resultv_free(f, argc, argv);
            e_sexp_fatal_error(f, _("Performing query on unknown header: %s"), headername);
        }

        if (header) {
            /* performs an OR of all words */
            for (i=1;i<argc && !truth;i++) {
                if (argv[i]->type == ESEXP_RES_STRING
                    && camel_search_header_match(header, argv[i]->value.string, how)) {
                    truth = TRUE;
                }
            }
        }
    }
    /* TODO: else, find all matches */

    r = e_sexp_result_new(f, ESEXP_RES_BOOL);
    r->value.bool = truth;

    return r;
}

static ESExpResult *
search_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
}

static ESExpResult *
search_header_matches(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
}

static ESExpResult *
search_header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
}

static ESExpResult *
search_header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    return check_header(f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
}

static ESExpResult *
search_header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;
    
    r(printf ("executing header-exists\n"));
    
    if (search->current) {
        r = e_sexp_result_new(f, ESEXP_RES_BOOL);
        if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
            r->value.bool = camel_medium_get_header(CAMEL_MEDIUM(search->current), argv[0]->value.string) != NULL;
        
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new();
    }
    
    return r;
}

/* this is just to OR results together */
struct _glib_sux_donkeys {
    int count;
    GPtrArray *uids;
};

/* or, store all unique values */
static void
g_lib_sux_htor(char *key, int value, struct _glib_sux_donkeys *fuckup)
{
    g_ptr_array_add(fuckup->uids, key);
}

static int
match_message(CamelFolder *folder, const char *uid, regex_t *pattern, CamelException *ex)
{
    CamelMimeMessage *msg;
    int truth = FALSE;

    msg = camel_folder_get_message(folder, uid, ex);
    if (!camel_exception_is_set(ex) && msg!=NULL) {
        truth = camel_search_message_body_contains((CamelDataWrapper *)msg, pattern);
        camel_object_unref((CamelObject *)msg);
    }
    return truth;
}

static ESExpResult *
search_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;
    int i, j;
    regex_t pattern;

    if (search->current) {
        int truth = FALSE;

        if (search->body_index) {
            for (i=0;i<argc && !truth;i++) {
                if (argv[i]->type == ESEXP_RES_STRING) {
                    truth = ibex_find_name(search->body_index, (char *)camel_message_info_uid(search->current),
                                   argv[i]->value.string);
                } else {
                    e_sexp_resultv_free(f, argc, argv);
                    e_sexp_fatal_error(f, _("Invalid type in body-contains, expecting string"));
                }
            }
        } else if (search->folder) {
            /* we do a 'slow' direct search */
            if (camel_search_build_match_regex(&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc, argv, search->priv->ex) == 0) {
                truth = match_message(search->folder, camel_message_info_uid(search->current), &pattern, search->priv->ex);
                regfree(&pattern);
            }
        } else {
            g_warning("Cannot perform indexed body query with no index or folder set");
        }
        r = e_sexp_result_new(f, ESEXP_RES_BOOL);
        r->value.bool = truth;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);

        if (search->body_index) {
            if (argc==1) {
                /* common case */
                r->value.ptrarray = ibex_find(search->body_index, argv[0]->value.string);
            } else {
                GHashTable *ht = g_hash_table_new(g_str_hash, g_str_equal);
                GPtrArray *pa;
                struct _glib_sux_donkeys lambdafoo;

                /* this sux, perform an or operation on the result(s) of each word */
                for (i=0;i<argc;i++) {
                    if (argv[i]->type == ESEXP_RES_STRING) {
                        pa = ibex_find(search->body_index, argv[i]->value.string);
                        for (j=0;j<pa->len;j++) {
                            g_hash_table_insert(ht, g_ptr_array_index(pa, j), (void *)1);
                        }
                        g_ptr_array_free(pa, FALSE);
                    } else {
                        e_sexp_result_free(f, r);
                        e_sexp_resultv_free(f, argc, argv);
                        e_sexp_fatal_error(f, _("Invalid type in body-contains, expecting string"));
                    }
                }
                lambdafoo.uids = g_ptr_array_new();
                g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
                r->value.ptrarray = lambdafoo.uids;
                g_hash_table_destroy(ht);
            }
        } else if (search->folder) {
            /* do a slow search */
            r->value.ptrarray = g_ptr_array_new();
            if (camel_search_build_match_regex(&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc, argv, search->priv->ex) == 0) {
                if (search->summary) {
                    for (i=0;i<search->summary->len;i++) {
                        CamelMessageInfo *info = g_ptr_array_index(search->summary, i);

                        if (match_message(search->folder, camel_message_info_uid(info), &pattern, search->priv->ex))
                            g_ptr_array_add(r->value.ptrarray, (char *)camel_message_info_uid(info));
                    }
                } /* else?  we could always get the summary from the folder, but then
                     we need to free it later somehow */
                regfree(&pattern);
            }
        } else {
            g_warning("Cannot perform indexed body query with no index or folder set");
            r->value.ptrarray = g_ptr_array_new();
        }
    }

    return r;
}

static ESExpResult *
search_user_flag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;
    int i;

    r(printf("executing user-flag\n"));

    /* are we inside a match-all? */
    if (search->current) {
        int truth = FALSE;
        /* performs an OR of all words */
        for (i=0;i<argc && !truth;i++) {
            if (argv[i]->type == ESEXP_RES_STRING
                && camel_flag_get(&search->current->user_flags, argv[i]->value.string)) {
                truth = TRUE;
                break;
            }
        }
        r = e_sexp_result_new(f, ESEXP_RES_BOOL);
        r->value.bool = truth;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new();
    }

    return r;
}

static ESExpResult *
search_system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;
    
    r(printf ("executing system-flag\n"));
    
    if (search->current) {
        gboolean truth = FALSE;
        
        if (argc == 1)
            truth = camel_system_flag_get (search->current->flags, argv[0]->value.string);
        
        r = e_sexp_result_new(f, ESEXP_RES_BOOL);
        r->value.bool = truth;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new ();
    }
    
    return r;
}

static ESExpResult *search_user_tag(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *search)
{
    ESExpResult *r;

    r(printf("executing user-tag\n"));

    /* are we inside a match-all? */
    if (search->current) {
        const char *value = NULL;
        if (argc == 1) {
            value = camel_tag_get(&search->current->user_tags, argv[0]->value.string);
        }
        r = e_sexp_result_new(f, ESEXP_RES_STRING);
        r->value.string = g_strdup(value?value:"");
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new();
    }

    return r;
}

static ESExpResult *
search_get_sent_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
{
    ESExpResult *r;

    r(printf("executing get-sent-date\n"));

    /* are we inside a match-all? */
    if (s->current) {
        r = e_sexp_result_new(f, ESEXP_RES_INT);

        r->value.number = s->current->date_sent;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new ();
    }

    return r;
}

static ESExpResult *
search_get_received_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
{
    ESExpResult *r;

    r(printf("executing get-received-date\n"));

    /* are we inside a match-all? */
    if (s->current) {
        r = e_sexp_result_new(f, ESEXP_RES_INT);

        r->value.number = s->current->date_received;
    } else {
        r = e_sexp_result_new(f, ESEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new ();
    }

    return r;
}

static ESExpResult *
search_get_current_date(struct _ESExp *f, int argc, struct _ESExpResult **argv, CamelFolderSearch *s)
{
    ESExpResult *r;

    r(printf("executing get-current-date\n"));

    r = e_sexp_result_new(f, ESEXP_RES_INT);
    r->value.number = time (NULL);
    return r;
}