svcb.c
8.17 KB
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
/* sieve callback implementations */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "sv.h"
/** message query callbacks **/
int
sv_getsize (void *mc, int *size)
{
sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
size_t sz = 0;
message_size (m->msg, &sz);
*size = sz;
sv_print (m->ic, SV_PRN_QRY, "getsize -> %d\n", *size);
return SIEVE_OK;
}
/*
A given header can occur multiple times, so we return a pointer
to a null terminated array of pointers to the values found for
the named header.
*/
int
sv_getheader (void *mc, const char *name, const char ***body)
{
sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
m->rc = 0;
if (!m->cache_filled)
{
header_t h = 0;
size_t i = 0;
char *fn = 0;
char *fv = 0;
m->cache_filled = 1;
message_get_header (m->msg, &h);
header_get_field_count (h, &i);
sv_print (m->ic, SV_PRN_QRY, "getheader, filling cache with %d fields\n", i);
for (; i > 0; i--)
{
m->rc = header_aget_field_name (h, i, &fn);
if (m->rc)
break;
m->rc = header_aget_field_value (h, i, &fv);
if (m->rc)
break;
sv_print (m->ic, SV_PRN_QRY, "getheader, cacheing %s=%s\n", fn, fv);
m->rc = sv_field_cache_add (&m->cache, fn, fv);
if (m->rc == 0)
{
fv = 0; /* owned by the cache */
}
if (m->rc)
break;
/* the cache doesn't want it, and we don't need it */
free (fn);
fn = 0;
}
free (fn);
free (fv);
}
if (!m->rc)
{
m->rc = sv_field_cache_get (&m->cache, name, body);
}
if (m->rc)
{
sv_print (m->ic, SV_PRN_QRY, "getheader %s, failed %s\n", name, strerror (m->rc));
}
else
{
const char **b = *body;
int i = 1;
sv_print (m->ic, SV_PRN_QRY, "getheader, %s=%s", name, b[0]);
while (b[0] && b[i])
{
sv_print (m->ic, SV_PRN_QRY, ", %s", b[i]);
i++;
}
sv_print (m->ic, SV_PRN_QRY, "\n");
}
return sv_mu_errno_to_rc (m->rc);
}
/*
name will always be "to" or "from"
envelope_t doesn't seem to allow "to" to be gotten, just "from".
What's up?
int getenvelope(void *mc, const char *name, const char ***body)
{
static const char *buf[2];
if (buf[0] == NULL) { buf[0] = malloc(sizeof(char) * 256); buf[1] = NULL; }
printf("Envelope body of '%s'? ", head);
scanf("%s", buf[0]);
body = buf;
return SIEVE_OK;
}
*/
/* message action callbacks */
/*
The actions arguments are mostly callback data provided during the
setup of the intepreter object, script object, and the execution of
a script.
The args are:
void* ac; // action context, the member of the union Action.u associated
// with this kind of action.
void* ic, // from sieve_interp_alloc(, ic);
void* sc, // from sieve_script_parse(, , sc, );
void* mc, // from sieve_execute_script(, mc);
const char** errmsg // fill it in if you return failure
*/
void
sv_print_action (const char* a, void *ac, void *ic, void *sc, void *mc)
{
//sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
sv_print (ic, SV_PRN_ACT, "action => %s\n", a);
}
int
sv_keep (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
//sv_msg_ctx_t * m = (sv_msg_ctx_t *) mc;
//sieve_keep_context_t * a = (sieve_keep_context_t *) ac;
sv_print_action ("KEEP", ac, ic, sc, mc);
return SIEVE_OK;
}
int
sv_fileinto (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
sieve_fileinto_context_t *a = (sieve_fileinto_context_t *) ac;
sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
sv_interp_ctx_t *i = (sv_interp_ctx_t *) ic;
const char *what = "fileinto";
int res = 0;
sv_print_action ("FILEINTO", ac, ic, sc, mc);
sv_print (i, SV_PRN_ACT, " into <%s>\n", a->mailbox);
if (!i->opt_no_actions)
{
res = sv_mu_save_to (a->mailbox, m->msg, i->ticket, &what);
}
if (res)
{
assert(what);
*errmsg = strerror (res);
sv_print (i, SV_PRN_ACT, " %s failed with [%d] %s\n",
what, res, *errmsg);
}
m->rc = res;
return res ? SIEVE_FAIL : SIEVE_OK;
}
int
sv_redirect (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
sv_print_action ("REDIRECT", ac, ic, sc, mc);
return SIEVE_OK;
}
int
sv_discard (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
sv_interp_ctx_t *i = (sv_interp_ctx_t *) ic;
int res = 0;
sv_print_action ("DISCARD", ac, ic, sc, mc);
if (!i->opt_no_actions)
{
res = sv_mu_mark_deleted (m->msg);
}
if (res)
*errmsg = strerror (res);
return SIEVE_OK;
}
int
sv_reject (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
sv_print_action ("REJECT", ac, ic, sc, mc);
return SIEVE_OK;
}
/*
int sv_notify(void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
sv_print_action("NOTIFY", ac, ic, sc, mc);
return SIEVE_OK;
}
*/
int
sv_autorespond (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
return SIEVE_FAIL;
}
int
sv_send_response (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
{
return SIEVE_FAIL;
}
#if 0
sieve_vacation_t vacation = {
0, /* min response */
0, /* max response */
&sv_autorespond, /* autorespond() */
&sv_send_response /* send_response() */
};
char *markflags[] = { "\\flagged" };
sieve_imapflags_t mark = { markflags, 1 };
#endif
/* sieve error callbacks */
int
sv_parse_error (int lineno, const char *msg, void *ic, void *sc)
{
sv_interp_ctx_t* i = (sv_interp_ctx_t*) ic;
sv_print (i, SV_PRN_ERR, "%s:%d: %s\n", i->opt_script, lineno, msg);
return SIEVE_OK;
}
int
sv_execute_error (const char *msg, void *ic, void *sc, void *mc)
{
sv_interp_ctx_t* i = (sv_interp_ctx_t*) ic;
sv_print (i, SV_PRN_ERR, "sieve execute failed, %s\n", msg);
return SIEVE_OK;
}
int
sv_summary (const char *msg, void *ic, void *sc, void *mc)
{
sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
m->summary = strdup (msg);
return SIEVE_OK;
}
/* register all these callbacks */
int
sv_register_callbacks (sieve_interp_t * i)
{
int res;
res = sieve_register_size (i, &sv_getsize);
if (res != SIEVE_OK)
{
printf ("sieve_register_size() returns %d\n", res);
exit (1);
}
res = sieve_register_header (i, &sv_getheader);
if (res != SIEVE_OK)
{
printf ("sieve_register_header() returns %d\n", res);
exit (1);
}
res = sieve_register_redirect (i, &sv_redirect);
if (res != SIEVE_OK)
{
printf ("sieve_register_redirect() returns %d\n", res);
exit (1);
}
res = sieve_register_keep (i, &sv_keep);
if (res != SIEVE_OK)
{
printf ("sieve_register_keep() returns %d\n", res);
exit (1);
}
#if 0
res = sieve_register_envelope (i, &sv_getenvelope);
if (res != SIEVE_OK)
{
printf ("sieve_register_envelope() returns %d\n", res);
exit (1);
}
#endif
res = sieve_register_discard (i, &sv_discard);
if (res != SIEVE_OK)
{
printf ("sieve_register_discard() returns %d\n", res);
exit (1);
}
res = sieve_register_reject (i, &sv_reject);
if (res != SIEVE_OK)
{
printf ("sieve_register_reject() returns %d\n", res);
exit (1);
}
res = sieve_register_fileinto (i, &sv_fileinto);
if (res != SIEVE_OK)
{
printf ("sieve_register_fileinto() returns %d\n", res);
exit (1);
}
#if 0
res = sieve_register_vacation (i, &sv_vacation);
if (res != SIEVE_OK)
{
printf ("sieve_register_vacation() returns %d\n", res);
exit (1);
}
res = sieve_register_imapflags (i, &mark);
if (res != SIEVE_OK)
{
printf ("sieve_register_imapflags() returns %d\n", res);
exit (1);
}
#endif
#if 0
res = sieve_register_notify (i, &sv_notify);
if (res != SIEVE_OK)
{
printf ("sieve_register_notify() returns %d\n", res);
exit (1);
}
#endif
res = sieve_register_parse_error (i, &sv_parse_error);
if (res != SIEVE_OK)
{
printf ("sieve_register_parse_error() returns %d\n", res);
exit (1);
}
res = sieve_register_execute_error (i, &sv_execute_error);
if (res != SIEVE_OK)
{
printf ("sieve_register_execute_error() returns %d\n", res);
exit (1);
}
res = sieve_register_summary (i, &sv_summary);
if (res != SIEVE_OK)
{
printf ("sieve_register_summary() returns %d\n", res);
exit (1);
}
return res;
}