sieve-gram.y
18.6 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
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
%{
/* sieve.y -- sieve parser
* Larry Greenfield
* $Id$
*/
/***********************************************************
Copyright 1999 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Carnegie Mellon
University not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "xalloc.h"
#include "comparator.h"
#include "interp.h"
#include "script.h"
#include "tree.h"
#include "util.h"
#include "imparse.h"
/* definitions */
extern int addrparse(void);
struct vtags {
int days;
stringlist_t *addresses;
char *subject;
int mime;
};
struct htags {
char *comparator;
int comptag;
};
struct aetags {
int addrtag;
char *comparator;
int comptag;
};
static commandlist_t *ret;
static sieve_script_t *parse_script;
static int check_reqs(stringlist_t *sl);
static test_t *build_address(int t, struct aetags *ae,
stringlist_t *sl, patternlist_t *pl);
static test_t *build_header(int t, struct htags *h,
stringlist_t *sl, patternlist_t *pl);
static commandlist_t *build_vacation(int t, struct vtags *h, char *s);
static struct aetags *new_aetags(void);
static struct aetags *canon_aetags(struct aetags *ae);
static void free_aetags(struct aetags *ae);
static struct htags *new_htags(void);
static struct htags *canon_htags(struct htags *h);
static void free_htags(struct htags *h);
static struct vtags *new_vtags(void);
static struct vtags *canon_vtags(struct vtags *v);
static void free_vtags(struct vtags *v);
static int verify_mailboxes(stringlist_t *sl);
static int verify_addresses(stringlist_t *sl);
static int verify_flags(stringlist_t *sl);
#ifdef ENABLE_REGEX
static patternlist_t *verify_regexs(stringlist_t *sl, char *comp);
#endif
static int ok_header(char *s);
int yyerror(char *msg);
extern int yylex(void);
#define YYERROR_VERBOSE /* i want better error messages! */
%}
%union {
int nval;
char *sval;
stringlist_t *sl;
test_t *test;
testlist_t *testl;
commandlist_t *cl;
struct vtags *vtag;
struct aetags *aetag;
struct htags *htag;
}
%token <nval> NUMBER
%token <sval> STRING
%token IF ELSIF ELSE
%token REJCT FILEINTO FORWARD KEEP STOP DISCARD VACATION REQUIRE
%token SETFLAG ADDFLAG REMOVEFLAG MARK UNMARK
%token NOTIFY DENOTIFY
%token ANYOF ALLOF EXISTS SFALSE STRUE HEADER NOT SIZE ADDRESS ENVELOPE
%token COMPARATOR IS CONTAINS MATCHES REGEX OVER UNDER
%token ALL LOCALPART DOMAIN USER DETAIL
%token DAYS ADDRESSES SUBJECT MIME
%token LOW MEDIUM HIGH
%type <cl> commands command action elsif block
%type <sl> stringlist strings
%type <test> test
%type <nval> comptag sizetag addrparttag addrorenv
%type <testl> testlist tests
%type <htag> htags
%type <aetag> aetags
%type <vtag> vtags
%type <sl> optional_headers
%type <sval> priority
%%
start: /* empty */ { ret = NULL; }
| reqs { ret = NULL; }
| reqs commands { ret = $2; }
;
reqs: /* empty */
| require reqs
;
require: REQUIRE stringlist ';' { if (!check_reqs($2)) {
yyerror("unsupported feature");
YYERROR;
} }
;
commands: command { $$ = $1; }
| command commands { $1->next = $2; $$ = $1; }
;
command: action ';' { $$ = $1; }
| IF test block elsif { $$ = new_if($2, $3, $4); }
| error ';' { $$ = new_command(STOP); }
;
elsif: /* empty */ { $$ = NULL; }
| ELSIF test block elsif { $$ = new_if($2, $3, $4); }
| ELSE block { $$ = $2; }
;
action: REJCT STRING { if (!parse_script->support.reject) {
yyerror("reject not required");
YYERROR;
}
$$ = new_command(REJCT); $$->u.str = $2; }
| FILEINTO stringlist { if (!parse_script->support.fileinto) {
yyerror("fileinto not required");
YYERROR;
}
if (!verify_mailboxes($2)) {
YYERROR; /* vm should call yyerror() */
}
$$ = new_command(FILEINTO);
$$->u.sl = $2; }
| FORWARD stringlist { $$ = new_command(FORWARD);
if (!verify_addresses($2)) {
YYERROR; /* va should call yyerror() */
}
$$->u.sl = $2; }
| KEEP { $$ = new_command(KEEP); }
| STOP { $$ = new_command(STOP); }
| DISCARD { $$ = new_command(DISCARD); }
| VACATION vtags STRING { if (!parse_script->support.vacation) {
yyerror("vacation not required");
$$ = new_command(VACATION);
YYERROR;
} else {
$$ = build_vacation(VACATION,
canon_vtags($2), $3);
} }
| SETFLAG stringlist { if (!parse_script->support.imapflags) {
yyerror("imapflags not required");
YYERROR;
}
if (!verify_flags($2)) {
YYERROR; /* vf should call yyerror() */
}
$$ = new_command(SETFLAG);
$$->u.sl = $2; }
| ADDFLAG stringlist { if (!parse_script->support.imapflags) {
yyerror("imapflags not required");
YYERROR;
}
if (!verify_flags($2)) {
YYERROR; /* vf should call yyerror() */
}
$$ = new_command(ADDFLAG);
$$->u.sl = $2; }
| REMOVEFLAG stringlist { if (!parse_script->support.imapflags) {
yyerror("imapflags not required");
YYERROR;
}
if (!verify_flags($2)) {
YYERROR; /* vf should call yyerror() */
}
$$ = new_command(REMOVEFLAG);
$$->u.sl = $2; }
| MARK { if (!parse_script->support.imapflags) {
yyerror("imapflags not required");
YYERROR;
}
$$ = new_command(MARK); }
| UNMARK { if (!parse_script->support.imapflags) {
yyerror("imapflags not required");
YYERROR;
}
$$ = new_command(UNMARK); }
| NOTIFY priority STRING optional_headers
{
if (!parse_script->support.notify) {
yyerror("notify not required");
YYERROR;
}
$$ = new_command(NOTIFY);
$$->u.n.priority = $2;
$$->u.n.message = $3;
$$->u.n.headers_list = $4;
}
| DENOTIFY { if (!parse_script->support.notify) {
yyerror("notify not required");
YYERROR;
}
$$ = new_command(DENOTIFY); }
;
priority: /* nothing */ { $$ = "medium"; }
| LOW { $$ = "low"; }
| MEDIUM { $$ = "medium"; }
| HIGH { $$ = "high"; }
;
optional_headers: /* empty */ {
$$ = NULL;
}
| stringlist { $$ = $1; }
;
vtags: /* empty */ { $$ = new_vtags(); }
| vtags DAYS NUMBER { if ($$->days != -1) {
yyerror("duplicate :days"); YYERROR; }
else { $$->days = $3; } }
| vtags ADDRESSES stringlist { if ($$->addresses != NULL) {
yyerror("duplicate :addresses");
YYERROR;
} else if (!verify_addresses($3)) {
YYERROR;
} else {
$$->addresses = $3; } }
| vtags SUBJECT STRING { if ($$->subject != NULL) {
yyerror("duplicate :subject");
YYERROR;
} else if (!ok_header($3)) {
YYERROR;
} else { $$->subject = $3; } }
| vtags MIME { if ($$->mime != -1) {
yyerror("duplicate :mime");
YYERROR; }
else { $$->mime = MIME; } }
;
stringlist: '[' strings ']' { $$ = $2; }
| STRING { $$ = new_sl($1, NULL); }
;
strings: STRING { $$ = new_sl($1, NULL); }
| STRING ',' strings { $$ = new_sl($1, $3); }
;
block: '{' commands '}' { $$ = $2; }
| '{' '}' { $$ = NULL; }
;
test: ANYOF testlist { $$ = new_test(ANYOF); $$->u.tl = $2; }
| ALLOF testlist { $$ = new_test(ALLOF); $$->u.tl = $2; }
| EXISTS stringlist { $$ = new_test(EXISTS); $$->u.sl = $2; }
| SFALSE { $$ = new_test(SFALSE); }
| STRUE { $$ = new_test(STRUE); }
| HEADER htags stringlist stringlist
{ patternlist_t *pl;
$2 = canon_htags($2);
#ifdef ENABLE_REGEX
if ($2->comptag == REGEX) {
pl = verify_regexs($4, $2->comparator);
if (!pl) { YYERROR; }
}
else
#endif
pl = (patternlist_t *) $4;
$$ = build_header(HEADER, $2, $3, pl);
if ($$ == NULL) { YYERROR; } }
| addrorenv aetags stringlist stringlist
{ patternlist_t *pl;
$2 = canon_aetags($2);
#ifdef ENABLE_REGEX
if ($2->comptag == REGEX) {
pl = verify_regexs($4, $2->comparator);
if (!pl) { YYERROR; }
}
else
#endif
pl = (patternlist_t *) $4;
$$ = build_address($1, $2, $3, pl);
if ($$ == NULL) { YYERROR; } }
| NOT test { $$ = new_test(NOT); $$->u.t = $2; }
| SIZE sizetag NUMBER { $$ = new_test(SIZE); $$->u.sz.t = $2;
$$->u.sz.n = $3; }
| error { $$ = NULL; }
;
addrorenv: ADDRESS { $$ = ADDRESS; }
| ENVELOPE { $$ = ENVELOPE; }
;
aetags: /* empty */ { $$ = new_aetags(); }
| aetags addrparttag { $$ = $1;
if ($$->addrtag != -1) {
yyerror("duplicate or conflicting address part tag");
YYERROR; }
else { $$->addrtag = $2; } }
| aetags comptag { $$ = $1;
if ($$->comptag != -1) {
yyerror("duplicate comparator type tag"); YYERROR; }
else { $$->comptag = $2; } }
| aetags COMPARATOR STRING { $$ = $1;
if ($$->comparator != NULL) {
yyerror("duplicate comparator tag"); YYERROR; }
else { $$->comparator = $3; } }
;
htags: /* empty */ { $$ = new_htags(); }
| htags comptag { $$ = $1;
if ($$->comptag != -1) {
yyerror("duplicate comparator type tag"); YYERROR; }
else { $$->comptag = $2; } }
| htags COMPARATOR STRING { $$ = $1;
if ($$->comparator != NULL) {
yyerror("duplicate comparator tag");
YYERROR; }
else { $$->comparator = $3; } }
;
addrparttag: ALL { $$ = ALL; }
| LOCALPART { $$ = LOCALPART; }
| DOMAIN { $$ = DOMAIN; }
| USER { if (!parse_script->support.subaddress) {
yyerror("subaddress not required");
YYERROR;
}
$$ = USER; }
| DETAIL { if (!parse_script->support.subaddress) {
yyerror("subaddress not required");
YYERROR;
}
$$ = DETAIL; }
;
comptag: IS { $$ = IS; }
| CONTAINS { $$ = CONTAINS; }
| MATCHES { $$ = MATCHES; }
| REGEX { if (!parse_script->support.regex) {
yyerror("regex not required");
YYERROR;
}
$$ = REGEX; }
;
sizetag: OVER { $$ = OVER; }
| UNDER { $$ = UNDER; }
testlist: '(' tests ')' { $$ = $2; }
;
tests: test { $$ = new_testlist($1, NULL); }
| test ',' tests { $$ = new_testlist($1, $3); }
;
%%
commandlist_t *sieve_parse(sieve_script_t *script, FILE *f)
{
commandlist_t *t;
extern FILE *yyin;
yyin = f;
parse_script = script;
if (yyparse()) {
t = NULL;
} else {
t = ret;
}
ret = NULL;
return t;
}
int yyerror(char *msg)
{
extern int yylineno;
int ret;
parse_script->err++;
if (parse_script->interp.err) {
ret = parse_script->interp.err(yylineno, msg,
parse_script->interp.interp_context,
parse_script->script_context);
}
return 0;
}
static int check_reqs(stringlist_t *sl)
{
int i = 1;
stringlist_t *s;
while (sl != NULL) {
s = sl;
sl = sl->next;
i &= script_require(parse_script, s->s);
if (s->s) free(s->s);
free(s);
}
return i;
}
static test_t *build_address(int t, struct aetags *ae,
stringlist_t *sl, patternlist_t *pl)
{
test_t *ret = new_test(t); /* can be either ADDRESS or ENVELOPE */
assert((t == ADDRESS) || (t == ENVELOPE));
if (ret) {
ret->u.ae.comptag = ae->comptag;
ret->u.ae.comp = lookup_comp(ae->comparator, ae->comptag);
ret->u.ae.sl = sl;
ret->u.ae.pl = pl;
ret->u.ae.addrpart = ae->addrtag;
free_aetags(ae);
if (ret->u.ae.comp == NULL) {
free_test(ret);
ret = NULL;
}
}
return ret;
}
static test_t *build_header(int t, struct htags *h,
stringlist_t *sl, patternlist_t *pl)
{
test_t *ret = new_test(t); /* can be HEADER */
assert(t == HEADER);
if (ret) {
ret->u.h.comptag = h->comptag;
ret->u.h.comp = lookup_comp(h->comparator, h->comptag);
ret->u.h.sl = sl;
ret->u.h.pl = pl;
free_htags(h);
if (ret->u.h.comp == NULL) {
free_test(ret);
ret = NULL;
}
}
return ret;
}
static commandlist_t *build_vacation(int t, struct vtags *v, char *reason)
{
commandlist_t *ret = new_command(t);
assert(t == VACATION);
if (ret) {
ret->u.v.subject = v->subject; v->subject = NULL;
ret->u.v.days = v->days;
ret->u.v.mime = v->mime;
ret->u.v.addresses = v->addresses; v->addresses = NULL;
free_vtags(v);
ret->u.v.message = reason;
}
return ret;
}
static struct aetags *new_aetags(void)
{
struct aetags *r = (struct aetags *) xmalloc(sizeof(struct aetags));
r->addrtag = r->comptag = -1;
r->comparator = NULL;
return r;
}
static struct aetags *canon_aetags(struct aetags *ae)
{
if (ae->addrtag == -1) { ae->addrtag = ALL; }
if (ae->comparator == NULL) { ae->comparator = strdup("i;ascii-casemap"); }
if (ae->comptag == -1) { ae->comptag = IS; }
return ae;
}
static void free_aetags(struct aetags *ae)
{
free(ae->comparator);
free(ae);
}
static struct htags *new_htags(void)
{
struct htags *r = (struct htags *) xmalloc(sizeof(struct htags));
r->comptag = -1;
r->comparator = NULL;
return r;
}
static struct htags *canon_htags(struct htags *h)
{
if (h->comparator == NULL) { h->comparator = strdup("i;ascii-casemap"); }
if (h->comptag == -1) { h->comptag = IS; }
return h;
}
static void free_htags(struct htags *h)
{
free(h->comparator);
free(h);
}
static struct vtags *new_vtags(void)
{
struct vtags *r = (struct vtags *) xmalloc(sizeof(struct vtags));
r->days = -1;
r->addresses = NULL;
r->subject = NULL;
r->mime = -1;
return r;
}
static struct vtags *canon_vtags(struct vtags *v)
{
assert(parse_script->interp.vacation != NULL);
if (v->days == -1) { v->days = 7; }
if (v->days < parse_script->interp.vacation->min_response)
{ v->days = parse_script->interp.vacation->min_response; }
if (v->days > parse_script->interp.vacation->max_response)
{ v->days = parse_script->interp.vacation->max_response; }
if (v->mime == -1) { v->mime = 0; }
return v;
}
static void free_vtags(struct vtags *v)
{
if (v->addresses) { free_sl(v->addresses); }
if (v->subject) { free(v->subject); }
free(v);
}
char *addrptr; /* pointer to address string for address lexer */
char addrerr[500]; /* buffer for address parser error messages */
static int verify_address(char *s)
{
char errbuf[500];
addrptr = s;
if (addrparse()) {
sprintf(errbuf, "address '%s': %s", s, addrerr);
yyerror(errbuf);
return 0;
}
return 1;
}
static int verify_addresses(stringlist_t *sl)
{
for (; sl != NULL && verify_address(sl->s); sl = sl->next) ;
return (sl == NULL);
}
static int verify_mailbox(char *s)
{
/* if not a mailbox, call yyerror */
return 1;
}
static int verify_mailboxes(stringlist_t *sl)
{
for (; sl != NULL && verify_mailbox(sl->s); sl = sl->next) ;
return (sl == NULL);
}
static int verify_flag(char *f)
{
char errbuf[100];
if (f[0] == '\\') {
lcase(f);
if (strcmp(f, "\\seen") && strcmp(f, "\\answered") &&
strcmp(f, "\\flagged") && strcmp(f, "\\draft") &&
strcmp(f, "\\deleted")) {
sprintf(errbuf, "flag '%s': not a system flag", f);
yyerror(errbuf);
return 0;
}
return 1;
}
if (!imparse_isatom(f)) {
sprintf(errbuf, "flag '%s': not a valid keyword", f);
yyerror(errbuf);
return 0;
}
return 1;
}
static int verify_flags(stringlist_t *sl)
{
for (; sl != NULL && verify_flag(sl->s); sl = sl->next) ;
return (sl == NULL);
}
#ifdef ENABLE_REGEX
static regex_t *verify_regex(char *s, int cflags)
{
int ret;
char errbuf[100];
regex_t *reg = (regex_t *) xmalloc(sizeof(regex_t));
if ((ret = regcomp(reg, s, cflags)) != 0) {
(void) regerror(ret, reg, errbuf, sizeof(errbuf));
yyerror(errbuf);
free(reg);
return NULL;
}
return reg;
}
static patternlist_t *verify_regexs(stringlist_t *sl, char *comp)
{
stringlist_t *sl2;
patternlist_t *pl = NULL;
int cflags = REG_EXTENDED | REG_NOSUB;
regex_t *reg;
if (!strcmp(comp, "i;ascii-casemap")) {
cflags |= REG_ICASE;
}
for (sl2 = sl; sl2 != NULL; sl2 = sl2->next) {
if ((reg = verify_regex(sl2->s, cflags)) == NULL) {
free_pl(pl, REGEX);
break;
}
pl = (patternlist_t *) new_pl(reg, pl);
}
if (sl2 == NULL) {
free_sl(sl);
return pl;
}
return NULL;
}
#endif
/* is it ok to put this in an RFC822 header body? */
static int ok_header(char *s)
{
return 1;
}