Commit 5de1ee07 5de1ee078ca04fc2dd1bc8c93f6e944ca5d159d6 by Wojciech Polak

Moved to ../libsieve/extensions/

1 parent 4d20e0e5
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 /* Implements "list" sieve extension test. See "Syntax:" below for the
19 description */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <mailutils/libsieve.h>
29
30
31
32 /* Auxiliary functions */
33 struct header_closure {
34 header_t header; /* Message header */
35 int index; /* Header index */
36 char *delim; /* List delimiter */
37 char *value; /* Retrieved header value */
38 char *save; /* Save pointer for strtok_r */
39 };
40
41 static void
42 cleanup (struct header_closure *hc)
43 {
44 free (hc->value);
45 hc->value = hc->save = NULL;
46 }
47
48 static int
49 retrieve_next_header (struct header_closure *hc, char *name, char **pval)
50 {
51 char buf[512];
52 size_t n;
53
54 cleanup (hc);
55 while (!header_get_field_name (hc->header, hc->index, buf, sizeof(buf), &n))
56 {
57 int i = hc->index++;
58 if (strcasecmp (buf, name) == 0)
59 {
60 if (header_aget_field_value (hc->header, i, &hc->value))
61 return 1;
62 *pval = strtok_r (hc->value, hc->delim, &hc->save);
63 if (*pval == NULL)
64 {
65 cleanup (hc);
66 return 1;
67 }
68 return 0;
69 }
70 }
71
72 return 1;
73 }
74
75 static int
76 list_retrieve_header (void *item, void *data, int idx, char **pval)
77 {
78 struct header_closure *hc = data;
79 char *p;
80
81 if (idx == 0)
82 hc->index = 1;
83
84 while (1)
85 {
86 if (!hc->value)
87 {
88 if (retrieve_next_header (hc, (char*) item, &p))
89 return 1;
90 }
91 else
92 {
93 p = strtok_r (NULL, hc->delim, &hc->save);
94 if (!p)
95 {
96 cleanup (hc);
97 continue;
98 }
99 }
100
101 *pval = strdup (p);
102 return 0;
103 }
104
105 return 1;
106 }
107
108
109 /* The test proper */
110
111 /* Syntax: list [COMPARATOR] [MATCH-TYPE]
112 [ :delim <delimiters: string> ]
113 <headers: string-list> <key-list: string-list>
114
115 The "list" test evaluates to true if any of the headers
116 match any key. Each header is regarded as containing a
117 list of keywords. By default, comma is assumed as list
118 separator. This can be overridden by specifying ":delim"
119 tag, whose value is a string consisting of valid list
120 delimiter characters.
121
122 list :matches :delim " ," [ "X-Spam-Keywords", "X-Spamd-Keywords" ]
123 [ "HTML_*", "FORGED_*" ]
124
125
126 */
127
128 static int
129 list_test (sieve_machine_t mach, list_t args, list_t tags)
130 {
131 sieve_value_t *h, *v, *arg;
132 sieve_comparator_t comp = sieve_get_comparator (mach, tags);
133 struct header_closure clos;
134 int result;
135
136 memset (&clos, 0, sizeof clos);
137 if (sieve_tag_lookup (tags, "delim", &arg))
138 clos.delim = arg->v.string;
139 else
140 clos.delim = ",";
141
142 h = sieve_value_get (args, 0);
143 if (!h)
144 {
145 sieve_error (mach, _("list: can't get argument 1"));
146 sieve_abort (mach);
147 }
148 v = sieve_value_get (args, 1);
149 if (!v)
150 {
151 sieve_error (mach, _("list: can't get argument 2"));
152 sieve_abort (mach);
153 }
154
155 message_get_header (sieve_get_message (mach), &clos.header);
156 result = sieve_vlist_compare (h, v, comp, sieve_get_relcmp (mach, tags),
157 list_retrieve_header,
158 &clos, NULL) > 0;
159 cleanup (&clos);
160 return result;
161 }
162
163
164 /* Initialization */
165
166 /* Required arguments: */
167 static sieve_data_type list_req_args[] = {
168 SVT_STRING_LIST,
169 SVT_STRING_LIST,
170 SVT_VOID
171 };
172
173 static sieve_tag_def_t match_part_tags[] = {
174 { "is", SVT_VOID },
175 { "contains", SVT_VOID },
176 { "matches", SVT_VOID },
177 { "regex", SVT_VOID },
178 { "count", SVT_STRING },
179 { "value", SVT_STRING },
180 { "comparator", SVT_STRING },
181 { NULL }
182 };
183
184 static sieve_tag_def_t delim_part_tags[] = {
185 { "delim", SVT_STRING },
186 { NULL }
187 };
188
189 static sieve_tag_group_t list_tag_groups[] = {
190 { match_part_tags, sieve_match_part_checker },
191 { delim_part_tags, NULL },
192 { NULL }
193 };
194
195 /* Initialization function. */
196 int
197 SIEVE_EXPORT(list,init) (sieve_machine_t mach)
198 {
199 return sieve_register_test (mach, "list", list_test,
200 list_req_args, list_tag_groups, 1);
201 }
202
203 /* End of list.c */
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 /* Syntax: timestamp [":before"/":after"] <header-name: string>
19 <date: datestring>
20
21 The "timestamp" test compares the value of a structured date header
22 field with the given date.
23
24 If the tagged argument is ":after" and the date from the header is
25 after the specified date the result is true, otherwise, if the
26 header date is before the given date, the result is false.
27
28 If the tagged argument is ":before" and the date from the header is
29 before the specified date the result is true, otherwise, if the
30 header date is after the given date, the result is false.
31
32 If no tagged argument is supplied, :after is assumed.
33
34 Almost any date format is understood.
35
36 Example: timestamp :before "X-Expire-Timestamp" "now - 5 days"
37
38 This test will return true, if the date in X-Expire-Timestamp is
39 more than 5 days older than the current date. */
40
41 #ifdef HAVE_CONFIG_H
42 # include <config.h>
43 #endif
44
45 #include <stdlib.h>
46 #include <mailutils/libsieve.h>
47
48 /* Handler for the timestamp test */
49 static int
50 timestamp_test (sieve_machine_t mach, list_t args, list_t tags)
51 {
52 sieve_value_t *h, *v;
53 header_t hdr;
54 char *val;
55 time_t now = time (NULL);
56 time_t tlimit, tval;
57 int rc;
58
59 if (sieve_get_debug_level (mach) & MU_SIEVE_DEBUG_TRACE)
60 sieve_debug (mach, "TIMESTAMP\n");
61
62 /* Retrieve required arguments: */
63 /* First argument: header name */
64 h = sieve_value_get (args, 0);
65 if (!h)
66 {
67 sieve_error (mach, "timestamp: can't get argument 1");
68 sieve_abort (mach);
69 }
70 /* Second argument: date displacement */
71 v = sieve_value_get (args, 1);
72 if (!v)
73 {
74 sieve_error (mach, "timestamp: can't get argument 2");
75 sieve_abort (mach);
76 }
77
78 if (mu_parse_date (v->v.string, &tlimit, &now))
79 {
80 sieve_error (mach, "timestamp: can't parse date specification (%s)",
81 v->v.string);
82 sieve_abort (mach);
83 }
84
85 rc = message_get_header (sieve_get_message (mach), &hdr);
86 if (rc)
87 {
88 sieve_error (mach, "message_get_header: %s", mu_strerror (rc));
89 sieve_abort (mach);
90 }
91
92 if (header_aget_value (hdr, h->v.string, &val))
93 return 0;
94
95 if (mu_parse_date (val, &tval, &now))
96 {
97 sieve_error (mach,
98 "timestamp: can't parse header date specification (%s)",
99 val);
100 free (val);
101 sieve_abort (mach);
102 }
103 free (val);
104
105 rc = tval > tlimit;
106
107 if (sieve_tag_lookup (tags, "before", NULL))
108 rc = !rc;
109
110 return rc;
111 }
112
113 /* Required arguments: */
114 static sieve_data_type timestamp_req_args[] = {
115 SVT_STRING,
116 SVT_STRING,
117 SVT_VOID
118 };
119
120 /* Tagged arguments: */
121 static sieve_tag_def_t timestamp_tags[] = {
122 { "after", SVT_VOID },
123 { "before", SVT_VOID },
124 { NULL }
125 };
126
127 static sieve_tag_group_t timestamp_tag_groups[] = {
128 { timestamp_tags, NULL },
129 { NULL }
130 };
131
132 /* Initialization function. It is the only function exported from this
133 module. */
134 int
135 SIEVE_EXPORT(timestamp,init) (sieve_machine_t mach)
136 {
137 return sieve_register_test (mach, "timestamp", timestamp_test,
138 timestamp_req_args, timestamp_tag_groups, 1);
139 }