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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors: Jeffrey Stedfast <fejj@ximian.com>
* Michael Zucchi <NotZed@Ximian.com>
*
* Copyright 2000 Ximian, Inc. (www.ximian.com)
* Copyright 2001 Ximian Inc. (www.ximian.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* (from glibc headers:
POSIX says that <sys/types.h> must be included (by the caller) before <regex.h>. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <gal/util/e-iconv.h>
#include "e-util/e-sexp.h"
#include "camel-mime-message.h"
#include "camel-filter-search.h"
#include "camel-exception.h"
#include "camel-multipart.h"
#include "camel-stream-mem.h"
#include "camel-stream-fs.h"
#include "camel-search-private.h"
#include "camel-url.h"
#define d(x)
typedef struct {
CamelFilterSearchGetMessageFunc get_message;
void *get_message_data;
CamelMimeMessage *message;
CamelMessageInfo *info;
const char *source;
CamelException *ex;
} FilterMessageSearch;
/* ESExp callbacks */
static ESExpResult *header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_soundex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *header_full_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *match_all (struct _ESExp *f, int argc, struct _ESExpTerm **argv, FilterMessageSearch *fms);
static ESExpResult *body_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *body_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *user_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *user_tag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *get_sent_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *get_received_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *get_current_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *get_source (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
static ESExpResult *shell_exec (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
/* builtin functions */
static struct {
char *name;
ESExpFunc *func;
int type; /* set to 1 if a function can perform shortcut evaluation, or
doesn't execute everything, 0 otherwise */
} symbols[] = {
{ "match-all", (ESExpFunc *) match_all, 1 },
{ "body-contains", (ESExpFunc *) body_contains, 0 },
{ "body-regex", (ESExpFunc *) body_regex, 0 },
{ "header-contains", (ESExpFunc *) header_contains, 0 },
{ "header-matches", (ESExpFunc *) header_matches, 0 },
{ "header-starts-with", (ESExpFunc *) header_starts_with, 0 },
{ "header-ends-with", (ESExpFunc *) header_ends_with, 0 },
{ "header-exists", (ESExpFunc *) header_exists, 0 },
{ "header-soundex", (ESExpFunc *) header_soundex, 0 },
{ "header-regex", (ESExpFunc *) header_regex, 0 },
{ "header-full-regex", (ESExpFunc *) header_full_regex, 0 },
{ "user-tag", (ESExpFunc *) user_tag, 0 },
{ "user-flag", (ESExpFunc *) user_flag, 0 },
{ "system-flag", (ESExpFunc *) system_flag, 0 },
{ "get-sent-date", (ESExpFunc *) get_sent_date, 0 },
{ "get-received-date", (ESExpFunc *) get_received_date, 0 },
{ "get-current-date", (ESExpFunc *) get_current_date, 0 },
{ "get-source", (ESExpFunc *) get_source, 0 },
{ "get-size", (ESExpFunc *) get_size, 0 },
{ "shell-exec", (ESExpFunc *) shell_exec, 0 },
};
static CamelMimeMessage *
camel_filter_search_get_message (FilterMessageSearch *fms, struct _ESExp *sexp)
{
if (fms->message)
return fms->message;
fms->message = fms->get_message (fms->get_message_data, fms->ex);
if (fms->message == NULL)
e_sexp_fatal_error (sexp, _("Failed to retrieve message"));
return fms->message;
}
static ESExpResult *
check_header (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms, camel_search_match_t how)
{
gboolean matched = FALSE;
ESExpResult *r;
int i;
if (argc > 1 && argv[0]->type == ESEXP_RES_STRING) {
char *name = argv[0]->value.string;
const char *header;
camel_search_t type = CAMEL_SEARCH_TYPE_ENCODED;
CamelContentType *ct;
const char *charset = NULL;
if (strcasecmp(name, "x-camel-mlist") == 0) {
header = camel_message_info_mlist(fms->info);
type = CAMEL_SEARCH_TYPE_MLIST;
} else {
CamelMimeMessage *message = camel_filter_search_get_message (fms, f);
header = camel_medium_get_header (CAMEL_MEDIUM (message), argv[0]->value.string);
/* FIXME: what about Resent-To, Resent-Cc and Resent-From? */
if (strcasecmp("to", name) == 0 || strcasecmp("cc", name) == 0 || strcasecmp("from", name) == 0)
type = CAMEL_SEARCH_TYPE_ADDRESS_ENCODED;
else {
ct = camel_mime_part_get_content_type (CAMEL_MIME_PART (message));
if (ct)
charset = e_iconv_charset_name(header_content_type_param(ct, "charset"));
}
}
if (header) {
for (i=1; i<argc && !matched; i++) {
if (argv[i]->type == ESEXP_RES_STRING)
matched = camel_search_header_match(header, argv[i]->value.string, how, type, charset);
}
}
}
r = e_sexp_result_new (f, ESEXP_RES_BOOL);
r->value.bool = matched;
return r;
}
static ESExpResult *
header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
return check_header (f, argc, argv, fms, CAMEL_SEARCH_MATCH_CONTAINS);
}
static ESExpResult *
header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
return check_header (f, argc, argv, fms, CAMEL_SEARCH_MATCH_EXACT);
}
static ESExpResult *
header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
return check_header (f, argc, argv, fms, CAMEL_SEARCH_MATCH_STARTS);
}
static ESExpResult *
header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
return check_header (f, argc, argv, fms, CAMEL_SEARCH_MATCH_ENDS);
}
static ESExpResult *
header_soundex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
return check_header (f, argc, argv, fms, CAMEL_SEARCH_MATCH_SOUNDEX);
}
static ESExpResult *
header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
CamelMimeMessage *message;
gboolean matched = FALSE;
ESExpResult *r;
int i;
message = camel_filter_search_get_message (fms, f);
for (i = 0; i < argc && !matched; i++) {
if (argv[i]->type == ESEXP_RES_STRING)
matched = camel_medium_get_header (CAMEL_MEDIUM (message), argv[i]->value.string) != NULL;
}
r = e_sexp_result_new (f, ESEXP_RES_BOOL);
r->value.bool = matched;
return r;
}
static ESExpResult *
header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r = e_sexp_result_new (f, ESEXP_RES_BOOL);
CamelMimeMessage *message;
regex_t pattern;
const char *contents;
message = camel_filter_search_get_message (fms, f);
if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
&& (contents = camel_medium_get_header (CAMEL_MEDIUM (message), argv[0]->value.string))
&& camel_search_build_match_regex(&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, fms->ex) == 0) {
r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
regfree (&pattern);
} else
r->value.bool = FALSE;
return r;
}
static gchar *
get_full_header (CamelMimeMessage *message)
{
CamelMimePart *mp = CAMEL_MIME_PART (message);
GString *str = g_string_new ("");
char *ret;
struct _header_raw *h;
for (h = mp->headers; h; h = h->next) {
if (h->value != NULL) {
g_string_append (str, h->name);
if (isspace (h->value[0]))
g_string_append (str, ":");
else
g_string_append (str, ": ");
g_string_append (str, h->value);
g_string_append_c(str, '\n');
}
}
ret = str->str;
g_string_free (str, FALSE);
return ret;
}
static ESExpResult *
header_full_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r = e_sexp_result_new (f, ESEXP_RES_BOOL);
CamelMimeMessage *message;
regex_t pattern;
char *contents;
if (camel_search_build_match_regex(&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE,
argc, argv, fms->ex) == 0) {
message = camel_filter_search_get_message (fms, f);
contents = get_full_header (message);
r->value.bool = regexec (&pattern, contents, 0, NULL, 0) == 0;
g_free (contents);
regfree (&pattern);
} else
r->value.bool = FALSE;
return r;
}
static ESExpResult *
match_all (struct _ESExp *f, int argc, struct _ESExpTerm **argv, FilterMessageSearch *fms)
{
/* match-all: when dealing with single messages is a no-op */
ESExpResult *r;
if (argc > 0)
return e_sexp_term_eval (f, argv[0]);
r = e_sexp_result_new (f, ESEXP_RES_BOOL);
r->value.bool = FALSE;
return r;
}
static ESExpResult *
body_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r = e_sexp_result_new (f, ESEXP_RES_BOOL);
CamelMimeMessage *message;
regex_t pattern;
if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE, argc, argv, fms->ex) == 0) {
message = camel_filter_search_get_message (fms, f);
r->value.bool = camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern);
regfree (&pattern);
} else
r->value.bool = FALSE;
return r;
}
static ESExpResult *
body_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r = e_sexp_result_new(f, ESEXP_RES_BOOL);
CamelMimeMessage *message;
regex_t pattern;
if (camel_search_build_match_regex(&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE,
argc, argv, fms->ex) == 0) {
message = camel_filter_search_get_message (fms, f);
r->value.bool = camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern);
regfree (&pattern);
} else
r->value.bool = FALSE;
return r;
}
static ESExpResult *
user_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
gboolean truth = FALSE;
int i;
/* performs an OR of all words */
for (i = 0; i < argc && !truth; i++) {
if (argv[i]->type == ESEXP_RES_STRING
&& camel_flag_get (&fms->info->user_flags, argv[i]->value.string)) {
truth = TRUE;
break;
}
}
r = e_sexp_result_new (f, ESEXP_RES_BOOL);
r->value.bool = truth;
return r;
}
static ESExpResult *
system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
if (argc != 1 || argv[0]->type != ESEXP_RES_STRING)
e_sexp_fatal_error(f, _("Invalid arguments to (system-flag)"));
r = e_sexp_result_new (f, ESEXP_RES_BOOL);
r->value.bool = camel_system_flag_get (fms->info->flags, argv[0]->value.string);
return r;
}
static ESExpResult *
user_tag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
const char *tag;
if (argc != 1 || argv[0]->type != ESEXP_RES_STRING)
e_sexp_fatal_error(f, _("Invalid arguments to (user-tag)"));
tag = camel_tag_get (&fms->info->user_tags, argv[0]->value.string);
r = e_sexp_result_new (f, ESEXP_RES_STRING);
r->value.string = g_strdup (tag ? tag : "");
return r;
}
static ESExpResult *
get_sent_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
CamelMimeMessage *message;
ESExpResult *r;
message = camel_filter_search_get_message (fms, f);
r = e_sexp_result_new (f, ESEXP_RES_INT);
r->value.number = camel_mime_message_get_date (message, NULL);
return r;
}
static ESExpResult *
get_received_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
CamelMimeMessage *message;
ESExpResult *r;
message = camel_filter_search_get_message (fms, f);
r = e_sexp_result_new (f, ESEXP_RES_INT);
r->value.number = camel_mime_message_get_date_received (message, NULL);
return r;
}
static ESExpResult *
get_current_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
r = e_sexp_result_new (f, ESEXP_RES_INT);
r->value.number = time (NULL);
return r;
}
static ESExpResult *
get_source (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
CamelMimeMessage *message;
ESExpResult *r;
char *src = NULL;
char *tmp;
if (fms->source) {
CamelURL *url;
url = camel_url_new (fms->source, NULL);
if (url) {
src = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
camel_url_free (url);
}
} else {
message = camel_filter_search_get_message (fms, f);
src = g_strdup (camel_mime_message_get_source (message));
}
/* This is an abusive hack */
if (src && (tmp = strstr (src, "://"))) {
tmp += 3;
tmp = strchr (tmp, '/');
if (tmp)
*tmp = '\0';
}
if (src) {
r = e_sexp_result_new (f, ESEXP_RES_STRING);
r->value.string = src;
} else {
r = e_sexp_result_new (f, ESEXP_RES_UNDEFINED);
}
return r;
}
/* remember, the size comparisons are done at Kbytes */
static ESExpResult *
get_size (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
r = e_sexp_result_new(f, ESEXP_RES_INT);
r->value.number = fms->info->size / 1024;
return r;
}
static int
run_command (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
CamelMimeMessage *message;
CamelStream *stream;
int result, status;
int in_fds[2];
pid_t pid;
if (pipe (in_fds) == -1) {
camel_exception_setv (fms->ex, CAMEL_EXCEPTION_SYSTEM,
_("Failed to create pipe to '%s': %s"),
argv[0]->value.string, g_strerror (errno));
return -1;
}
if (!(pid = fork ())) {
/* child process */
GPtrArray *args;
int maxfd, i;
if (dup2 (in_fds[0], STDIN_FILENO) < 0)
_exit (255);
setsid ();
maxfd = sysconf (_SC_OPEN_MAX);
if (maxfd > 0) {
for (i = 0; i < maxfd; i++) {
if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO)
close (i);
}
}
args = g_ptr_array_new ();
g_ptr_array_add (args, g_basename (argv[0]->value.string));
for (i = 1; i < argc; i++) {
g_ptr_array_add (args, argv[i]->value.string);
}
g_ptr_array_add (args, NULL);
execvp (argv[0]->value.string, (char **) args->pdata);
g_ptr_array_free (args, TRUE);
fprintf (stderr, "Could not execute %s: %s\n", argv[0]->value.string,
g_strerror (errno));
_exit (255);
} else if (pid < 0) {
camel_exception_setv (fms->ex, CAMEL_EXCEPTION_SYSTEM,
_("Failed to create create child process '%s': %s"),
argv[0]->value.string, g_strerror (errno));
return -1;
}
/* parent process */
close (in_fds[0]);
fcntl (in_fds[1], F_SETFL, O_NONBLOCK);
stream = camel_stream_fs_new_with_fd (in_fds[1]);
message = camel_filter_search_get_message (fms, f);
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), stream);
camel_stream_flush (stream);
camel_object_unref (CAMEL_OBJECT (stream));
result = waitpid (pid, &status, 0);
if (result == -1 && errno == EINTR) {
/* child process is hanging... */
kill (pid, SIGTERM);
sleep (1);
result = waitpid (pid, &status, WNOHANG);
if (result == 0) {
/* ...still hanging, set phasers to KILL */
kill (pid, SIGKILL);
sleep (1);
result = waitpid (pid, &status, WNOHANG);
}
}
if (result != -1 && WIFEXITED (status))
return WEXITSTATUS (status);
else
return -1;
}
static ESExpResult *
shell_exec (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
{
ESExpResult *r;
int retval, i;
/* make sure all args are strings */
for (i = 0; i < argc; i++) {
if (argv[i]->type != ESEXP_RES_STRING) {
retval = -1;
goto done;
}
}
retval = run_command (f, argc, argv, fms);
done:
r = e_sexp_result_new (f, ESEXP_RES_INT);
r->value.number = retval;
return r;
}
/**
* camel_filter_search_match:
* @message:
* @info:
* @source:
* @expression:
* @ex:
*
* Returns one of CAMEL_SEARCH_MATCHED, CAMEL_SEARCH_NOMATCH, or CAMEL_SEARCH_ERROR.
**/
int
camel_filter_search_match (CamelFilterSearchGetMessageFunc get_message, void *data,
CamelMessageInfo *info, const char *source,
const char *expression, CamelException *ex)
{
FilterMessageSearch fms;
ESExp *sexp;
ESExpResult *result;
gboolean retval;
int i;
fms.get_message = get_message;
fms.get_message_data = data;
fms.message = NULL;
fms.info = info;
fms.source = source;
fms.ex = ex;
sexp = e_sexp_new ();
for (i = 0; i < sizeof (symbols) / sizeof (symbols[0]); i++) {
if (symbols[i].type == 1)
e_sexp_add_ifunction (sexp, 0, symbols[i].name, (ESExpIFunc *)symbols[i].func, &fms);
else
e_sexp_add_function (sexp, 0, symbols[i].name, symbols[i].func, &fms);
}
e_sexp_input_text (sexp, expression, strlen (expression));
if (e_sexp_parse (sexp) == -1) {
if (!camel_exception_is_set (ex))
camel_exception_setv (ex, 1, _("Error executing filter search: %s: %s"),
e_sexp_error (sexp), expression);
goto error;
}
result = e_sexp_eval (sexp);
if (result == NULL) {
if (!camel_exception_is_set (ex))
camel_exception_setv (ex, 1, _("Error executing filter search: %s: %s"),
e_sexp_error (sexp), expression);
goto error;
}
if (result->type == ESEXP_RES_BOOL)
retval = result->value.bool ? CAMEL_SEARCH_MATCHED : CAMEL_SEARCH_NOMATCH;
else
retval = CAMEL_SEARCH_NOMATCH;
e_sexp_result_free (sexp, result);
e_sexp_unref (sexp);
if (fms.message)
camel_object_unref (CAMEL_OBJECT (fms.message));
return retval;
error:
if (fms.message)
camel_object_unref (CAMEL_OBJECT (fms.message));
e_sexp_unref (sexp);
return CAMEL_SEARCH_ERROR;
}
|