variables.c
9.27 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
/* Sieve variables extension (RFC 5229)
Copyright (C) 2016 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sieve-priv.h>
#include <ctype.h>
struct sieve_variable
{
char *value;
};
/* FIXME: UTF support */
char *
mod_lower (mu_sieve_machine_t mach, char const *value)
{
char *newval = mu_sieve_malloc (mach, strlen (value) + 1);
char *p;
for (p = newval; *value; p++, value++)
*p = tolower (*value);
*p = 0;
return newval;
}
char *
mod_upper (mu_sieve_machine_t mach, char const *value)
{
char *newval = mu_sieve_malloc (mach, strlen (value) + 1);
char *p;
for (p = newval; *value; p++, value++)
*p = toupper (*value);
*p = 0;
return newval;
}
char *
mod_lowerfirst (mu_sieve_machine_t mach, char const *value)
{
char *newval = mu_sieve_strdup (mach, value);
*newval = tolower (*newval);
return newval;
}
char *
mod_upperfirst (mu_sieve_machine_t mach, char const *value)
{
char *newval = mu_sieve_strdup (mach, value);
*newval = toupper (*newval);
return newval;
}
char *
mod_quotewildcard (mu_sieve_machine_t mach, char const *value)
{
size_t len;
char *newval;
char const *p;
char *q;
len = 0;
for (p = value; *p; p++)
{
switch (*p)
{
case '*':
case '?':
case '\\':
len += 2;
break;
default:
len++;
}
}
newval = mu_sieve_malloc (mach, len + 1);
for (p = value, q = newval; *p;)
{
switch (*p)
{
case '*':
case '?':
case '\\':
*q++ = '\\';
}
*q++ = *p++;
}
*q = 0;
return newval;
}
char *
mod_length (mu_sieve_machine_t mach, char const *value)
{
char *newval, *p;
int rc = mu_asprintf (&newval, "%zu", strlen (value));
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_asprintf", NULL, rc);
mu_sieve_abort (mach);
}
p = mu_sieve_strdup (mach, newval);
free (newval);
return p;
}
static mu_sieve_tag_def_t set_tags[] = {
{ "lower", SVT_VOID },
{ "upper", SVT_VOID },
{ "lowerfirst", SVT_VOID },
{ "upperfirst", SVT_VOID },
{ "quotewildcard", SVT_VOID },
{ "length", SVT_VOID },
{ NULL }
};
struct modprec
{
char *name;
unsigned prec;
char *(*modify) (mu_sieve_machine_t mach, char const *value);
};
static struct modprec modprec[] = {
{ "lower", 40, mod_lower },
{ "upper", 40, mod_upper },
{ "lowerfirst", 30, mod_lowerfirst },
{ "upperfirst", 30, mod_upperfirst },
{ "quotewildcard", 20, mod_quotewildcard },
{ "length", 10, mod_length },
};
static struct modprec *
findprec (char const *name)
{
int i;
for (i = 0; i < MU_ARRAY_SIZE (modprec); i++)
if (strcmp (modprec[i].name, name) == 0)
return &modprec[i];
mu_error (_("%s:%d: INTERNAL ERROR, please report"), __FILE__, __LINE__);
abort ();
}
static int
sieve_action_set (mu_sieve_machine_t mach)
{
size_t i;
char *name;
char *value;
struct sieve_variable *var, **vptr;
int rc;
mu_sieve_get_arg (mach, 0, SVT_STRING, &name);
mu_sieve_get_arg (mach, 1, SVT_STRING, &value);
value = mu_sieve_strdup (mach, value);
for (i = 0; i < mach->tagcount; i++)
{
mu_sieve_value_t *p = mu_sieve_get_tag_n (mach, i);
char *str = findprec (p->tag)->modify (mach, value);
mu_sieve_free (mach, value);
value = str;
}
rc = mu_assoc_install_ref (mach->vartab, name, &vptr);
switch (rc)
{
case 0:
var = malloc (sizeof (*var));
if (!var)
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
mu_sieve_abort (mach);
}
*vptr = var;
break;
case MU_ERR_EXISTS:
var = *vptr;
mu_sieve_free (mach, var->value);
break;
default:
mu_sieve_error (mach, "mu_assoc_ref_install: %s", mu_strerror (rc));
mu_sieve_abort (mach);
}
var->value = value;
return 0;
}
static int
set_tag_checker (mu_sieve_machine_t mach)
{
int i, j;
/* Sort tags by decreasing priority value (RFC 5229, 4.1) */
for (i = 1; i < mach->tagcount; i++)
{
mu_sieve_value_t tmp = *mu_sieve_get_tag_n (mach, i);
int tmp_prec = findprec (tmp.tag)->prec;
for (j = i - 1; j >= 0; j--)
{
mu_sieve_value_t *t = mu_sieve_get_tag_n (mach, j);
int prec = findprec (t->tag)->prec;
if (prec < tmp_prec)
*mu_sieve_get_tag_n (mach, j + 1) = *t;
else if (prec == tmp_prec)
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("%s and %s can't be used together"),
tmp.tag, t->tag);
mu_i_sv_error (mach);
return 1;
}
else
break;
}
*mu_sieve_get_tag_n (mach, j + 1) = tmp;
}
return 0;
}
static mu_sieve_tag_group_t set_tag_groups[] = {
{ set_tags, set_tag_checker },
{ NULL }
};
static mu_sieve_data_type set_args[] = {
SVT_STRING,
SVT_STRING,
SVT_VOID
};
static int
retrieve_string (void *item, void *data, size_t idx, char **pval)
{
if (idx)
return MU_ERR_NOENT;
*pval = strdup ((char*)item);
if (!*pval)
return errno;
return 0;
}
int
fold_string (void *item, void *data, void *prev, void **ret)
{
char *str = item;
size_t count = *(size_t*) prev;
/* The "relational" extension [RELATIONAL] adds a match type called
":count". The count of a single string is 0 if it is the empty
string, or 1 otherwise. The count of a string list is the sum of the
counts of the member strings.
*/
if (*str)
++count;
*(size_t*)ret = count;
return 0;
}
/* RFC 5229, 5. Test string */
int
sieve_test_string (mu_sieve_machine_t mach)
{
mu_sieve_value_t *source, *key_list;
source = mu_sieve_get_arg_untyped (mach, 0);
key_list = mu_sieve_get_arg_untyped (mach, 1);
return mu_sieve_vlist_compare (mach, source, key_list,
retrieve_string, fold_string, mach);
}
static mu_sieve_data_type string_args[] = {
SVT_STRING_LIST,
SVT_STRING_LIST,
SVT_VOID
};
static mu_sieve_tag_group_t string_tag_groups[] = {
{ mu_sieve_match_part_tags, mu_sieve_match_part_checker },
{ NULL }
};
int
mu_i_sv_expand_variables (char const *input, size_t len,
char **exp, void *data)
{
mu_sieve_machine_t mach = data;
if (mu_isdigit (*input))
{
char *p;
size_t idx = 0;
while (len)
{
if (mu_isdigit (*input))
{
int d = *input - '0';
idx = idx * 10 + d;
input++;
len--;
}
else
return 1;
}
if (idx > mach->match_count)
{
*exp = NULL;
return 0;
}
len = mach->match_buf[idx].rm_eo - mach->match_buf[idx].rm_so;
p = malloc (len + 1);
if (!p)
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
mu_sieve_abort (mach);
}
memcpy (p, mach->match_string + mach->match_buf[idx].rm_so, len);
p[len] = 0;
*exp = p;
}
else if (mu_isalpha (*input))
{
size_t i;
char *name;
struct sieve_variable *var;
for (i = 0; i < len; i++)
if (!(mu_isalnum (input[i]) || input[i] == '_'))
return 1;
name = malloc (len + 1);
if (!name)
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
mu_sieve_abort (mach);
}
memcpy (name, input, len);
name[len] = 0;
var = mu_assoc_get (mach->vartab, name);
free (name);
if (var)
{
*exp = strdup (var->value);
if (!*exp)
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
mu_sieve_abort (mach);
}
}
else
*exp = NULL;
}
else
return 1;
return 0;
}
int
mu_sieve_require_variables (mu_sieve_machine_t mach)
{
int rc;
if (mach->vartab)
return 0;
rc = mu_assoc_create (&mach->vartab, MU_ASSOC_ICASE);
if (rc)
mu_sieve_error (mach, "mu_assoc_create: %s", mu_strerror (rc));
mu_assoc_set_destroy_item (mach->vartab, mu_list_free_item);
if (rc == 0)
{
mu_sieve_register_action (mach, "set", sieve_action_set,
set_args, set_tag_groups, 1);
mu_sieve_register_test (mach, "string", sieve_test_string,
string_args, string_tag_groups, 1);
}
return rc;
}
int
mu_sieve_has_variables (mu_sieve_machine_t mach)
{
return mach->vartab != NULL;
}
void
mu_i_sv_copy_variables (mu_sieve_machine_t child, mu_sieve_machine_t parent)
{
mu_iterator_t itr;
mu_sieve_require_variables (child);
mu_assoc_get_iterator (parent->vartab, &itr);
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *name;
struct sieve_variable const *val;
struct sieve_variable *newval;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&val);
newval = malloc (sizeof (*newval));
if (!newval)
mu_sieve_abort (child);
newval->value = mu_sieve_strdup (child, val->value);
mu_assoc_install (child->vartab, name, newval);
}
mu_iterator_destroy (&itr);
}