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
|
/*
* Copyright 2000 HelixCode (http://www.helixcode.com).
*
* Author :
* 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
*/
#include <glib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <camel/gmime-utils.h>
#include "camel/camel-mime-message.h"
#include "camel/camel-mime-part.h"
#include "camel/camel-stream.h"
#include "camel/camel-stream-fs.h"
#include "camel/camel.h"
#include "camel-mbox-folder.h"
#include "camel-mbox-summary.h"
#include "camel-mbox-search.h"
#define HAVE_FILTER
#ifdef HAVE_FILTER
#include "e-sexp.h"
#define HAVE_IBEX
#ifdef HAVE_IBEX
#include "ibex.h"
#endif
#define p(x) /* parse debug */
#define r(x) /* run debug */
#define d(x) /* general debug */
/*
Matching operators:
list = (body-contains string+)
bool = (body-contains string+)
Returns a list of all messages containing any of the strings in the message.
If within a match-all, then returns true for the current message.
list = (match-all bool-expr)
Returns a list of all messages for which the bool expression is true.
The bool-expr is evaluated for each message in turn.
It is more efficient not to perform body-content comparisons inside a
match-all operator.
int = (date-sent)
Returns a time_t of the date-sent of the message.
bool = (header-contains string string+)
Returns true if the current message (inside a match-all operator)
has a header 'string1', which contains any of the following strings.
*/
struct _searchcontext {
int id; /* id of this search */
int cancelled; /* search cancelled? */
CamelFolder *folder;
#ifdef HAVE_IBEX
ibex *index; /* index of content for this folder */
#endif
CamelMboxSummary *summary;
CamelMboxMessageInfo *message_current; /* when performing a (match operation */
};
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 ESExpResult *
func_body_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, void *data)
{
ESExpResult *r;
int i, j;
struct _searchcontext *ctx = data;
if (ctx->message_current) {
int truth = FALSE;
r = e_sexp_result_new(ESEXP_RES_BOOL);
if (ctx->index) {
for (i=0;i<argc && !truth;i++) {
if (argv[i]->type == ESEXP_RES_STRING) {
truth = ibex_find_name(ctx->index, ctx->message_current->info.uid, argv[i]->value.string);
} else {
g_warning("Invalid type passed to body-contains match function");
}
}
} else {
g_warning("Cannot perform indexed query with no index");
}
r->value.bool = truth;
} else {
r = e_sexp_result_new(ESEXP_RES_ARRAY_PTR);
if (ctx->index) {
if (argc==1) {
/* common case */
r->value.ptrarray = ibex_find(ctx->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(ctx->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);
}
}
lambdafoo.uids = g_ptr_array_new();
g_hash_table_foreach(ht, (GHFunc)g_lib_sux_htor, &lambdafoo);
r->value.ptrarray = lambdafoo.uids;
}
} else {
r->value.ptrarray = g_ptr_array_new();
}
}
return r;
}
static ESExpResult *
func_date_sent(struct _ESExp *f, int argc, struct _ESExpResult **argv, void *data)
{
ESExpResult *r;
struct _searchcontext *ctx = data;
r = e_sexp_result_new(ESEXP_RES_INT);
if (ctx->message_current) {
g_warning("FIXME: implement date parsing ...");
/* r->value.number = get_date(ctx->message_current); */
} else {
r->value.number = time(0);
}
return r;
}
static ESExpResult *
func_match_all(struct _ESExp *f, int argc, struct _ESExpTerm **argv, void *data)
{
int i;
ESExpResult *r, *r1;
struct _searchcontext *ctx = data;
if (argc>1) {
g_warning("match-all only takes a single argument, other arguments ignored");
}
r = e_sexp_result_new(ESEXP_RES_ARRAY_PTR);
r->value.ptrarray = g_ptr_array_new();
for (i=0;i<ctx->summary->messages->len;i++) {
if (argc>0) {
ctx->message_current = g_ptr_array_index(ctx->summary->messages, i);
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, ctx->message_current->info.uid);
} else {
g_warning("invalid syntax, matches require a single bool result");
}
e_sexp_result_free(r1);
} else {
g_ptr_array_add(r->value.ptrarray, ctx->message_current->info.uid);
}
}
ctx->message_current = NULL;
return r;
}
static ESExpResult *
func_header_contains(struct _ESExp *f, int argc, struct _ESExpResult **argv, void *data)
{
ESExpResult *r;
struct _searchcontext *ctx = data;
int truth = FALSE;
r(printf("executing header-contains\n"));
/* are we inside a match-all? */
if (ctx->message_current && argc>1
&& argv[0]->type == ESEXP_RES_STRING) {
char *headername, *header = NULL;
char strbuf[32];
int i;
/* only a subset of headers are supported .. */
headername = argv[0]->value.string;
if (!strcasecmp(headername, "subject")) {
header = ctx->message_current->info.subject;
} else if (!strcasecmp(headername, "date")) {
sprintf(strbuf, "%d", (int)ctx->message_current->info.date_sent);
header = strbuf;
} else if (!strcasecmp(headername, "from")) {
header = ctx->message_current->info.from;
} else {
g_warning("Performing query on unknown header: %s", headername);
}
if (header) {
for (i=1;i<argc && !truth;i++) {
if (argv[i]->type == ESEXP_RES_STRING
&& strstr(header, argv[i]->value.string)) {
printf("%s got a match with %s of %s\n", ctx->message_current->info.uid, header, argv[i]->value.string);
truth = TRUE;
break;
}
}
}
}
r = e_sexp_result_new(ESEXP_RES_BOOL);
r->value.bool = truth;
return r;
}
/* '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[] = {
{ "body-contains", func_body_contains, 0 },
{ "date-sent", func_date_sent, 0 },
{ "match-all", (ESExpFunc *)func_match_all, 1 },
{ "header-contains", func_header_contains, 0 },
};
int camel_mbox_folder_search_by_expression(CamelFolder *folder, const char *expression,
CamelSearchFunc *func, void *data, CamelException *ex)
{
int i;
struct _searchcontext *ctx;
GList *matches = NULL;
ESExp *f;
ESExpResult *r;
CamelMboxFolder *mbox_folder = (CamelMboxFolder *)folder;
/* setup our expression evaluator */
f = e_sexp_new();
ctx = g_malloc0(sizeof(*ctx));
ctx->id = ((CamelMboxFolder *)folder)->search_id++;
/* setup out context */
ctx->folder = folder;
ctx->summary = mbox_folder->summary;
if (ctx->summary == NULL || camel_exception_get_id (ex)) {
printf ("Cannot get summary\n"
"Full description : %s\n", camel_exception_get_description (ex));
g_free(ctx);
gtk_object_unref((GtkObject *)f);
return -1;
}
/* FIXME: the index should be global to the folder */
ctx->message_current = NULL;
ctx->index = CAMEL_MBOX_FOLDER(folder)->index;
if (!ctx->index) {
g_warning("No folder index, searches will not function fully");
}
((CamelMboxFolder *)folder)->searches = g_list_append(((CamelMboxFolder *)folder)->searches, ctx);
for(i=0;i<sizeof(symbols)/sizeof(symbols[0]);i++) {
if (symbols[i].type == 1) {
e_sexp_add_ifunction(f, 0, symbols[i].name, (ESExpIFunc *)symbols[i].func, ctx);
} else {
e_sexp_add_function(f, 0, symbols[i].name, symbols[i].func, ctx);
}
}
e_sexp_input_text(f, expression, strlen(expression));
e_sexp_parse(f);
r = e_sexp_eval(f);
/* now create a folder summary to return?? */
if (r
&& r->type == ESEXP_RES_ARRAY_PTR) {
d(printf("got result ...\n"));
for (i=0;i<r->value.ptrarray->len;i++) {
d(printf("adding match: %s\n", (char *)g_ptr_array_index(r->value.ptrarray, i)));
matches = g_list_prepend(matches, g_strdup(g_ptr_array_index(r->value.ptrarray, i)));
}
if (!ctx->cancelled) {
func(folder, ctx->id, TRUE, matches, data);
}
g_list_free(matches);
e_sexp_result_free(r);
} else {
printf("no result!\n");
}
gtk_object_unref((GtkObject *)f);
i = ctx->id;
((CamelMboxFolder *)folder)->searches = g_list_remove(((CamelMboxFolder *)folder)->searches, ctx);
g_free(ctx);
return i;
}
static struct _searchcontext *
find_context(CamelMboxFolder *f, int id)
{
struct _searchcontext *ctx;
GList *l;
l = f->searches;
while (l) {
ctx = l->data;
if (ctx->id == id) {
return ctx;
}
l = g_list_next(l);
}
return NULL;
}
gboolean camel_mbox_folder_search_complete(CamelFolder *folder, int searchid, int wait, CamelException *ex)
{
struct _searchcontext *ctx;
ctx = find_context((CamelMboxFolder *)folder, searchid);
if (ctx)
return ctx->cancelled;
/* if its been removed, its complete ... */
return TRUE;
}
void camel_mbox_folder_search_cancel(CamelFolder *folder, int searchid, CamelException *ex)
{
struct _searchcontext *ctx;
ctx = find_context((CamelMboxFolder *)folder, searchid);
if (ctx) {
ctx->cancelled = TRUE;
return;
}
/* FIXME: set exception, return */
}
#else /* HAVE_FILTER */
int camel_mbox_folder_search_by_expression(CamelFolder *folder, const char *expression,
CamelSearchFunc *func, void *data, CamelException *ex)
{
return -1;
}
gboolean camel_mbox_folder_search_complete(CamelFolder *folder, int searchid, int wait, CamelException *ex)
{
return TRUE;
}
void camel_mbox_folder_search_cancel(CamelFolder *folder, int searchid, CamelException *ex)
{
/* empty */
}
#endif /*! HAVE_FILTER */
|