Codebase list fdm / 46b0117
Make tag an action. Start numbering included files from line 1 not 0. Nicholas Marriott 17 years ago
8 changed file(s) with 197 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
3737 deliver-stdout.c deliver-append-string.c strb.c deliver-add-header.c \
3838 deliver-exec.c child-fetch.c parent-fetch.c child-deliver.c \
3939 parent-deliver.c mail-state.c netrc.c shm-mmap.c shm-sysv.c \
40 deliver-tag.c \
4041 y.tab.c lex.yy.c
4142
4243 DEFS= -DBUILD="\"$(VERSION) ($(DATE))\""
2121 deliver-stdout.c deliver-append-string.c strb.c deliver-add-header.c \
2222 deliver-exec.c child-fetch.c parent-fetch.c child-deliver.c \
2323 parent-deliver.c mail-state.c netrc.c shm-mmap.c shm-sysv.c \
24 deliver-tag.c \
2425 parse.y lex.l
2526
2627 LEX= lex
2828 - option for using PCRE
2929 - audit close/closedir error checks
3030 - document compound actions
31 - lambda actions & make tag an action (special-case stop = 0 amd warn)
31 - document lambda actions
0 /* $Id$ */
1
2 /*
3 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
14 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
15 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/types.h>
19
20 #include <string.h>
21 #include <vis.h>
22
23 #include "fdm.h"
24 #include "deliver.h"
25
26 int deliver_tag_deliver(struct deliver_ctx *, struct actitem *);
27 void deliver_tag_desc(struct actitem *, char *, size_t);
28
29 struct deliver deliver_tag = {
30 "tag",
31 DELIVER_INCHILD,
32 deliver_tag_deliver,
33 deliver_tag_desc
34 };
35
36 int
37 deliver_tag_deliver(struct deliver_ctx *dctx, struct actitem *ti)
38 {
39 struct account *a = dctx->account;
40 struct mail *m = dctx->mail;
41 struct deliver_tag_data *data = ti->data;
42 char *tk, *tv;
43
44 tk = replacestr(&data->key, m->tags, m, &m->rml);
45 if (data->value.str != NULL)
46 tv = replacestr(&data->value, m->tags, m, &m->rml);
47 else
48 tv = xstrdup("");
49
50 if (tk == NULL || tv == NULL) {
51 if (tk != NULL)
52 xfree(tk);
53 if (tv != NULL)
54 xfree(tv);
55 return (DELIVER_SUCCESS);
56 }
57
58 if (*tk != '\0') {
59 log_debug2("%s: tagging message: %s (%s)", a->name, tk, tv);
60 add_tag(&m->tags, tk, "%s", tv);
61 }
62
63 xfree(tk);
64 xfree(tv);
65
66 return (DELIVER_SUCCESS);
67 }
68
69 void
70 deliver_tag_desc(struct actitem *ti, char *buf, size_t len)
71 {
72 struct deliver_tag_data *data = ti->data;
73
74 if (data->value.str == NULL)
75 xsnprintf(buf, len, "tag \"%s\"", data->key.str);
76 else {
77 xsnprintf(buf, len,
78 "tag \"%s\" value \"%s\"", data->key.str, data->value.str);
79 }
80 }
120120 struct replstr str;
121121 };
122122
123 /* Deliver tag data. */
124 struct deliver_tag_data {
125 struct replstr key;
126 struct replstr value;
127 };
128
123129 /* deliver-smtp.c */
124130 extern struct deliver deliver_smtp;
125131
126 /* deliver-smtp.c */
132 /* deliver-stdout.c */
127133 extern struct deliver deliver_stdout;
134
135 /* deliver-tag.c */
136 extern struct deliver deliver_tag;
128137
129138 /* deliver-pipe.c */
130139 extern struct deliver deliver_pipe;
614614 int find_uid; /* find uids from headers */
615615
616616 int stop; /* stop matching at this rule */
617
618 struct replstr key;
619 struct replstr value;
620617
621618 struct rules rules;
622619 struct action *lambda;
5050 struct users *users;
5151 u_int i;
5252 int should_free, this, error = MAIL_CONTINUE;
53 char *an, *tkey, *tvalue, desc[DESCBUFSIZE];
53 char *an, desc[DESCBUFSIZE];
5454
5555 set_wrapped(m, ' ');
5656
234234 */
235235 mctx->rule = TAILQ_FIRST(&mctx->rule->rules);
236236 return (MAIL_CONTINUE);
237 }
238
239 /*
240 * Tag mail if necessary.
241 */
242 if (mctx->rule->key.str != NULL) {
243 tkey = replacestr(&mctx->rule->key, m->tags, m, &m->rml);
244 tvalue = replacestr(&mctx->rule->value, m->tags, m, &m->rml);
245
246 if (tkey != NULL && *tkey != '\0' && tvalue != NULL) {
247 log_debug2("%s: tagging message: %s (%s)", a->name,
248 tkey, tvalue);
249 add_tag(&m->tags, tkey, "%s", tvalue);
250 }
251
252 if (tkey != NULL)
253 xfree(tkey);
254 if (tvalue != NULL)
255 xfree(tvalue);
256237 }
257238
258239 /*
377377 size_t off;
378378
379379 if (r->expr == NULL)
380 strlcpy(s, "all", sizeof s);
380 strlcpy(s, "all ", sizeof s);
381381 else {
382382 *s = '\0';
383383 off = 0;
409409 su = xstrdup("");
410410 if (r->lambda != NULL) {
411411 make_actlist(r->lambda->list, desc, sizeof desc);
412 log_debug2("added rule %u:%s%s matches=%s lambda=%s", r->idx,
412 log_debug2("added rule %u:%s%s matches=%slambda=%s", r->idx,
413413 sa, su, s, desc);
414414 } else if (r->actions != NULL) {
415415 ss = fmt_strings(NULL, (struct strings *) r->actions);
416 log_debug2("added rule %u:%s%s matches=%s actions=%s", r->idx,
416 log_debug2("added rule %u:%s%s matches=%sactions=%s", r->idx,
417417 sa, su, s, ss);
418418 xfree(ss);
419 } else if (r->key.str != NULL) {
420 log_debug2("added rule %u:%s matches=%s tag=%s (%s)", r->idx,
421 sa, s, r->key.str, r->value.str);
422419 } else
423 log_debug2("added rule %u:%s matches=%s nested", r->idx, sa, s);
420 log_debug2("added rule %u:%s matches=%snested", r->idx, sa, s);
424421 xfree(sa);
425422 xfree(su);
426423 }
507504 } else if (ti->deliver == &deliver_mbox) {
508505 struct deliver_mbox_data *data = ti->data;
509506 xfree(data->path.str);
507 } else if (ti->deliver == &deliver_tag) {
508 struct deliver_tag_data *data = ti->data;
509 xfree(data->key.str);
510 if (data->value.str != NULL)
511 xfree(data->value.str);
510512 } else if (ti->deliver == &deliver_smtp) {
511513 struct deliver_smtp_data *data = ti->data;
512514 xfree(data->to.str);
537539 free_strings((struct strings *) r->actions);
538540 ARRAY_FREEALL(r->actions);
539541 }
540
541 if (r->key.str != NULL)
542 xfree(r->key.str);
543 if (r->value.str != NULL)
544 xfree(r->value.str);
545542
546543 if (r->lambda != NULL)
547544 free_action(r->lambda);
977974 curfile = $2;
978975 log_debug2("including file %s", curfile);
979976 yyrestart(yyin);
980 yylineno = 0;
977 yylineno = 1;
981978 }
982979
983980 /** SIZE: <number> (long long) */
18791876
18801877 data->add_from = $2;
18811878 }
1879 | TOKTAG strv
1880 {
1881 struct deliver_tag_data *data;
1882
1883 if (*$2 == '\0')
1884 yyerror("invalid tag");
1885
1886 $$ = xcalloc(1, sizeof *$$);
1887 $$->deliver = &deliver_tag;
1888
1889 data = xcalloc(1, sizeof *data);
1890 $$->data = data;
1891
1892 data->key.str = $2;
1893 data->value.str = NULL;
1894 }
1895 | TOKTAG strv TOKVALUE strv
1896 {
1897 struct deliver_tag_data *data;
1898
1899 if (*$2 == '\0')
1900 yyerror("invalid tag");
1901
1902 $$ = xcalloc(1, sizeof *$$);
1903 $$->deliver = &deliver_tag;
1904
1905 data = xcalloc(1, sizeof *data);
1906 $$->data = data;
1907
1908 data->key.str = $2;
1909 data->value.str = $4;
1910 }
18821911 | TOKDROP
18831912 {
18841913 $$ = xcalloc(1, sizeof *$$);
24792508 perform: TOKTAG strv
24802509 /** [$2: strv (char *)] */
24812510 {
2511 struct action *t;
2512 struct actitem *ti;
2513 struct deliver_tag_data *data;
2514 char *file;
2515
2516 file = curfile == NULL ? conf.conf_file : curfile;
2517 log_warnx("%s: \"match ... tag\" is deprecated, please use "
2518 "\"match ... action tag\" at line %d", file, yylineno);
2519
24822520 if (*$2 == '\0')
24832521 yyerror("invalid tag");
24842522
2523 if (*$2 == '\0')
2524 yyerror("invalid tag");
2525
2526 ti = xcalloc(1, sizeof *$$);
2527 ti->deliver = &deliver_tag;
2528
2529 data = xcalloc(1, sizeof *data);
2530 ti->data = data;
2531
2532 data->key.str = $2;
2533 data->value.str = NULL;
2534
24852535 $$ = xcalloc(1, sizeof *$$);
24862536 $$->idx = ruleidx++;
2487 $$->lambda = NULL;
24882537 $$->actions = NULL;
2489 $$->key.str = $2;
2490 $$->value.str = xstrdup("");
24912538 TAILQ_INIT(&$$->rules);
24922539 $$->stop = 0;
24932540 $$->users = NULL;
24942541 $$->find_uid = 0;
24952542
2543 t = $$->lambda = xcalloc(1, sizeof *$$->lambda);
2544 xsnprintf(t->name, sizeof t->name, "<rule %u>", $$->idx);
2545 t->users = NULL;
2546 t->find_uid = 0;
2547 t->list = xmalloc(sizeof *t->list);
2548 TAILQ_INIT(t->list);
2549 TAILQ_INSERT_HEAD(t->list, ti, entry);
2550 ti->idx = 0;
2551
24962552 if (currule == NULL)
24972553 TAILQ_INSERT_TAIL(&conf.rules, $$, entry);
24982554 else
25012557 | TOKTAG strv TOKVALUE strv
25022558 /** [$2: strv (char *)] [$4: strv (char *)] */
25032559 {
2560 struct action *t;
2561 struct actitem *ti;
2562 struct deliver_tag_data *data;
2563 char *file;
2564
2565 file = curfile == NULL ? conf.conf_file : curfile;
2566 log_warnx("%s: \"match ... tag\" is deprecated, please use "
2567 "\"match ... action tag\" at line %d", file, yylineno);
2568
25042569 if (*$2 == '\0')
25052570 yyerror("invalid tag");
2571
2572 if (*$2 == '\0')
2573 yyerror("invalid tag");
2574
2575 ti = xcalloc(1, sizeof *$$);
2576 ti->deliver = &deliver_tag;
2577
2578 data = xcalloc(1, sizeof *data);
2579 ti->data = data;
2580
2581 data->key.str = $2;
2582 data->value.str = $4;
25062583
25072584 $$ = xcalloc(1, sizeof *$$);
25082585 $$->idx = ruleidx++;
25092586 $$->actions = NULL;
2510 $$->key.str = $2;
2511 $$->value.str = $4;
25122587 TAILQ_INIT(&$$->rules);
25132588 $$->stop = 0;
25142589 $$->users = NULL;
25152590 $$->find_uid = 0;
25162591
2592 t = $$->lambda = xcalloc(1, sizeof *$$->lambda);
2593 xsnprintf(t->name, sizeof t->name, "<rule %u>", $$->idx);
2594 t->users = NULL;
2595 t->find_uid = 0;
2596 t->list = xmalloc(sizeof *t->list);
2597 TAILQ_INIT(t->list);
2598 TAILQ_INSERT_HEAD(t->list, ti, entry);
2599 ti->idx = 0;
2600
25172601 if (currule == NULL)
25182602 TAILQ_INSERT_TAIL(&conf.rules, $$, entry);
25192603 else
25262610 $$ = xcalloc(1, sizeof *$$);
25272611 $$->idx = ruleidx++;
25282612 $$->actions = NULL;
2529 $$->key.str = NULL;
2530 $$->value.str = NULL;
25312613 TAILQ_INIT(&$$->rules);
25322614 $$->stop = !$4;
25332615 $$->users = $1.users;
25542636 $$ = xcalloc(1, sizeof *$$);
25552637 $$->idx = ruleidx++;
25562638 $$->actions = NULL;
2557 $$->key.str = NULL;
2558 $$->value.str = NULL;
25592639 TAILQ_INIT(&$$->rules);
25602640 $$->stop = !$6;
25612641 $$->users = $1.users;
25802660 $$->idx = ruleidx++;
25812661 $$->lambda = NULL;
25822662 $$->actions = $2;
2583 $$->key.str = NULL;
2584 $$->value.str = NULL;
25852663 TAILQ_INIT(&$$->rules);
25862664 $$->stop = !$3;
25872665 $$->users = $1.users;
25982676 $$->idx = ruleidx++;
25992677 $$->lambda = NULL;
26002678 $$->actions = NULL;
2601 $$->key.str = NULL;
2602 $$->value.str = NULL;
26032679 TAILQ_INIT(&$$->rules);
26042680 $$->stop = 0;
26052681 $$->users = NULL;