summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/plugins/variables/ext-variables-modifiers.c
blob: dd21c8899f406633098cf4835c558a9c27fb44b8 (plain)
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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "unichar.h"
#include "str-sanitize.h"

#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-binary.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-runtime.h"

#include "ext-variables-common.h"
#include "ext-variables-limits.h"
#include "ext-variables-modifiers.h"

#include <ctype.h>

/*
 * Core modifiers
 */

extern const struct sieve_variables_modifier_def lower_modifier;
extern const struct sieve_variables_modifier_def upper_modifier;
extern const struct sieve_variables_modifier_def lowerfirst_modifier;
extern const struct sieve_variables_modifier_def upperfirst_modifier;
extern const struct sieve_variables_modifier_def quotewildcard_modifier;
extern const struct sieve_variables_modifier_def length_modifier;

enum ext_variables_modifier_code {
    EXT_VARIABLES_MODIFIER_LOWER,
    EXT_VARIABLES_MODIFIER_UPPER,
    EXT_VARIABLES_MODIFIER_LOWERFIRST,
    EXT_VARIABLES_MODIFIER_UPPERFIRST,
    EXT_VARIABLES_MODIFIER_QUOTEWILDCARD,
    EXT_VARIABLES_MODIFIER_LENGTH
};

const struct sieve_variables_modifier_def *ext_variables_core_modifiers[] = {
	&lower_modifier,
	&upper_modifier,
	&lowerfirst_modifier,
	&upperfirst_modifier,
	&quotewildcard_modifier,
	&length_modifier
};

const unsigned int ext_variables_core_modifiers_count =
    N_ELEMENTS(ext_variables_core_modifiers);

#define ext_variables_modifier_name(modf) \
	(modf)->object->def->name
#define ext_variables_modifiers_equal(modf1, modf2) \
	( (modf1)->def == (modf2)->def )
#define ext_variables_modifiers_equal_precedence(modf1, modf2) \
	( (modf1)->def->precedence == (modf2)->def->precendence )

/*
 * Modifier registry
 */

void sieve_variables_modifier_register
(const struct sieve_extension *var_ext, struct sieve_validator *valdtr,
	const struct sieve_extension *ext,
	const struct sieve_variables_modifier_def *smodf_def)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, valdtr);

	sieve_validator_object_registry_add(ctx->modifiers, ext, &smodf_def->obj_def);
}

bool ext_variables_modifier_exists
(const struct sieve_extension *var_ext, struct sieve_validator *valdtr,
	const char *identifier)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, valdtr);

	return sieve_validator_object_registry_find(ctx->modifiers, identifier, NULL);
}

const struct sieve_variables_modifier *ext_variables_modifier_create_instance
(const struct sieve_extension *var_ext, struct sieve_validator *valdtr,
	struct sieve_command *cmd, const char *identifier)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, valdtr);
	struct sieve_object object;
	struct sieve_variables_modifier *modf;
	pool_t pool;

	if ( !sieve_validator_object_registry_find
		(ctx->modifiers, identifier, &object) )
		return NULL;

	pool = sieve_command_pool(cmd);
	modf = p_new(pool, struct sieve_variables_modifier, 1);
	modf->object = object;
	modf->var_ext = var_ext;
	modf->def = (const struct sieve_variables_modifier_def *) object.def;

  return modf;
}

void ext_variables_register_core_modifiers
(const struct sieve_extension *ext, struct ext_variables_validator_context *ctx)
{
	unsigned int i;

	/* Register core modifiers*/
	for ( i = 0; i < ext_variables_core_modifiers_count; i++ ) {
		sieve_validator_object_registry_add
			(ctx->modifiers, ext, &(ext_variables_core_modifiers[i]->obj_def));
	}
}

/*
 * Core modifiers
 */

/* Forward declarations */

static bool
mod_lower_modify(const struct sieve_variables_modifier *modf,
		 string_t *in, string_t **result);
static bool
mod_upper_modify(const struct sieve_variables_modifier *modf,
		 string_t *in, string_t **result);
static bool
mod_lowerfirst_modify(const struct sieve_variables_modifier *modf,
		      string_t *in, string_t **result);
static bool
mod_upperfirst_modify(const struct sieve_variables_modifier *modf,
		      string_t *in, string_t **result);
static bool
mod_length_modify(const struct sieve_variables_modifier *modf,
		  string_t *in, string_t **result);
static bool
mod_quotewildcard_modify(const struct sieve_variables_modifier *modf,
			 string_t *in, string_t **result);

/* Modifier objects */

const struct sieve_variables_modifier_def lower_modifier = {
	SIEVE_OBJECT("lower", &modifier_operand, EXT_VARIABLES_MODIFIER_LOWER),
	40,
	mod_lower_modify
};

const struct sieve_variables_modifier_def upper_modifier = {
	SIEVE_OBJECT("upper", &modifier_operand, EXT_VARIABLES_MODIFIER_UPPER),
	40,
	mod_upper_modify
};

const struct sieve_variables_modifier_def lowerfirst_modifier = {
	SIEVE_OBJECT
		("lowerfirst", &modifier_operand, EXT_VARIABLES_MODIFIER_LOWERFIRST),
	30,
	mod_lowerfirst_modify
};

const struct sieve_variables_modifier_def upperfirst_modifier = {
	SIEVE_OBJECT
		("upperfirst", &modifier_operand,	EXT_VARIABLES_MODIFIER_UPPERFIRST),
	30,
	mod_upperfirst_modify
};

const struct sieve_variables_modifier_def quotewildcard_modifier = {
	SIEVE_OBJECT
		("quotewildcard", &modifier_operand, EXT_VARIABLES_MODIFIER_QUOTEWILDCARD),
	20,
	mod_quotewildcard_modify
};

const struct sieve_variables_modifier_def length_modifier = {
	SIEVE_OBJECT("length", &modifier_operand, EXT_VARIABLES_MODIFIER_LENGTH),
	10,
	mod_length_modify
};

/* Modifier implementations */

static bool
mod_upperfirst_modify(const struct sieve_variables_modifier *modf ATTR_UNUSED,
		      string_t *in, string_t **result)
{
	char *content;

	if ( str_len(in) == 0 ) {
		*result = in;
		return TRUE;
	}

	*result = t_str_new(str_len(in));
	str_append_str(*result, in);

	content = str_c_modifiable(*result);
	content[0] = i_toupper(content[0]);

	return TRUE;
}

static bool
mod_lowerfirst_modify(const struct sieve_variables_modifier *modf ATTR_UNUSED,
		      string_t *in, string_t **result)
{
	char *content;

	if ( str_len(in) == 0 ) {
		*result = in;
		return TRUE;
	}

	*result = t_str_new(str_len(in));
	str_append_str(*result, in);

	content = str_c_modifiable(*result);
	content[0] = i_tolower(content[0]);

	return TRUE;
}

static bool
mod_upper_modify(const struct sieve_variables_modifier *modf ATTR_UNUSED,
		 string_t *in, string_t **result)
{
	char *content;

	if ( str_len(in) == 0 ) {
		*result = in;
		return TRUE;
	}

	*result = t_str_new(str_len(in));
	str_append_str(*result, in);

	content = str_c_modifiable(*result);
	(void)str_ucase(content);

	return TRUE;
}

static bool
mod_lower_modify(const struct sieve_variables_modifier *modf ATTR_UNUSED,
		 string_t *in, string_t **result)
{
	char *content;

	if ( str_len(in) == 0 ) {
		*result = in;
		return TRUE;
	}

	*result = t_str_new(str_len(in));
	str_append_str(*result, in);

	content = str_c_modifiable(*result);
	(void)str_lcase(content);

	return TRUE;
}

static bool
mod_length_modify(const struct sieve_variables_modifier *modf ATTR_UNUSED,
		  string_t *in, string_t **result)
{
	*result = t_str_new(64);
	str_printfa(*result, "%llu", (unsigned long long)
		uni_utf8_strlen_n(str_data(in), str_len(in)));
	return TRUE;
}

static bool
mod_quotewildcard_modify(const struct sieve_variables_modifier *modf,
			 string_t *in, string_t **result)
{
	size_t max_var_size =
		sieve_variables_get_max_variable_size(modf->var_ext);
	const unsigned char *p, *poff, *pend;
	size_t new_size;

	if ( str_len(in) == 0 ) {
		/* empty string */
		*result = in;
		return TRUE;
	}

	/* allocate new string */
	new_size = str_len(in) + 16;
	if (new_size > max_var_size)
		new_size = max_var_size;
	*result = t_str_new(new_size + 1);

	/* escape string */
	p = str_data(in);
	pend = p + str_len(in);
	poff = p;
	while (p < pend) {
		unsigned int n = uni_utf8_char_bytes((char)*p);

		if (n == 1 && (*p == '*' || *p == '?' || *p == '\\')) {
			str_append_data(*result, poff, p - poff);
			poff = p;

			if (str_len(*result) + 2 > max_var_size)
				break;

			str_append_c(*result, '\\');
		} else if ((str_len(*result) + (p - poff) + n) > max_var_size) {
			break;
		}
		if (p + n > pend) {
			p = pend;
			break;
		}
		p += n;
	}

	str_append_data(*result, poff, p - poff);

	return TRUE;
}

/*
 * Modifier argument
 */

/* [MODIFIER]:
 *   ":lower" / ":upper" / ":lowerfirst" / ":upperfirst" /
 *             ":quotewildcard" / ":length"
 */

/* Forward declarations */

static bool tag_modifier_is_instance_of
	(struct sieve_validator *valdtr, struct sieve_command *cmd,
		const struct sieve_extension *ext, const char *identifier, void **context);

/* Modifier tag object */

static const struct sieve_argument_def modifier_tag = {
	.identifier = "MODIFIER",
	.flags = SIEVE_ARGUMENT_FLAG_MULTIPLE,
	.is_instance_of = tag_modifier_is_instance_of
};

/* Modifier tag implementation */

static bool tag_modifier_is_instance_of
(struct sieve_validator *valdtr, struct sieve_command *cmd,
	const struct sieve_extension *ext, const char *identifier, void **data)
{
	const struct sieve_variables_modifier *modf;

	if ( data == NULL ) {
		return ext_variables_modifier_exists(ext, valdtr, identifier);
	}

	if ( (modf=ext_variables_modifier_create_instance
		(ext, valdtr, cmd, identifier)) == NULL )
		return FALSE;

	*data = (void *) modf;

	return TRUE;
}

/* Registration */

void sieve_variables_modifiers_link_tag
(struct sieve_validator *valdtr, const struct sieve_extension *var_ext,
	struct sieve_command_registration *cmd_reg)
{
	sieve_validator_register_tag(valdtr, cmd_reg, var_ext, &modifier_tag, 0);
}

/* Validation */

bool sieve_variables_modifiers_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd,
	ARRAY_TYPE(sieve_variables_modifier) *modifiers)
{
	struct sieve_ast_argument *arg;

	arg = sieve_command_first_argument(cmd);
	while ( arg != NULL && arg != cmd->first_positional ) {
		const struct sieve_variables_modifier *modfs;
		const struct sieve_variables_modifier *modf;
		unsigned int i, modf_count;
		bool inserted;

		if ( !sieve_argument_is(arg, modifier_tag) ) {
			arg = sieve_ast_argument_next(arg);
			continue;
		}
		modf = (const struct sieve_variables_modifier *)
			arg->argument->data;

		inserted = FALSE;
		modfs = array_get(modifiers, &modf_count);
		for ( i = 0; i < modf_count && !inserted; i++ ) {

			if ( modfs[i].def->precedence == modf->def->precedence ) {
				sieve_argument_validate_error(valdtr, arg,
					"modifiers :%s and :%s specified for the set command conflict "
					"having equal precedence",
					modfs[i].def->obj_def.identifier, modf->def->obj_def.identifier);
				return FALSE;
			}

			if ( modfs[i].def->precedence < modf->def->precedence ) {
				array_insert(modifiers, i, modf, 1);
				inserted = TRUE;
			}
		}

		if ( !inserted )
			array_append(modifiers, modf, 1);

		/* Added to modifier list;
		   self-destruct to prevent implicit code generation */
		arg = sieve_ast_arguments_detach(arg, 1);
	}
	return TRUE;
}

bool sieve_variables_modifiers_generate
(const struct sieve_codegen_env *cgenv,
	ARRAY_TYPE(sieve_variables_modifier) *modifiers)
{
	struct sieve_binary_block *sblock = cgenv->sblock;
	const struct sieve_variables_modifier *modfs;
	unsigned int i, modf_count;

	sieve_binary_emit_byte(sblock, array_count(modifiers));

	modfs = array_get(modifiers, &modf_count);
	for ( i = 0; i < modf_count; i++ ) {
		ext_variables_opr_modifier_emit(sblock,
			modfs[i].object.ext, modfs[i].def);
	}
	return TRUE;
}

/*
 * Modifier coding
 */

const struct sieve_operand_class sieve_variables_modifier_operand_class =
	{ "modifier" };

static const struct sieve_extension_objects core_modifiers =
	SIEVE_VARIABLES_DEFINE_MODIFIERS(ext_variables_core_modifiers);

const struct sieve_operand_def modifier_operand = {
	.name = "modifier",
	.ext_def = &variables_extension,
	.code = EXT_VARIABLES_OPERAND_MODIFIER,
	.class = &sieve_variables_modifier_operand_class,
	.interface = &core_modifiers
};

bool sieve_variables_modifiers_code_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	unsigned int mdfs, i;

	/* Read the number of applied modifiers we need to read */
	if ( !sieve_binary_read_byte(denv->sblock, address, &mdfs) )
		return FALSE;

	/* Print all modifiers (sorted during code generation already) */
	for ( i = 0; i < mdfs; i++ ) {
		if ( !ext_variables_opr_modifier_dump(denv, address) )
			return FALSE;
	}
	return TRUE;
}

int sieve_variables_modifiers_code_read(
	const struct sieve_runtime_env *renv,
	const struct sieve_extension *var_ext, sieve_size_t *address,
	ARRAY_TYPE(sieve_variables_modifier) *modifiers)
{
	unsigned int lprec, mdfs, i;

	if ( !sieve_binary_read_byte(renv->sblock, address, &mdfs) ) {
		sieve_runtime_trace_error(renv, "invalid modifier count");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	t_array_init(modifiers, mdfs);

	lprec = (unsigned int)-1;
	for ( i = 0; i < mdfs; i++ ) {
		struct sieve_variables_modifier modf;

		if ( !ext_variables_opr_modifier_read(renv, var_ext,
						      address, &modf) )
			return SIEVE_EXEC_BIN_CORRUPT;
		if ( modf.def != NULL ) {
			if ( modf.def->precedence >= lprec ) {
				sieve_runtime_trace_error(renv,
					"unsorted modifier precedence");
				return SIEVE_EXEC_BIN_CORRUPT;
			}
			lprec = modf.def->precedence;
		}

		array_append(modifiers, &modf, 1);
	}

	return SIEVE_EXEC_OK;
}

/*
 * Modifier application
 */

int sieve_variables_modifiers_apply
(const struct sieve_runtime_env *renv,
	const struct sieve_extension *var_ext,
	ARRAY_TYPE(sieve_variables_modifier) *modifiers,
	string_t **value)
{	
	const struct ext_variables_config *config =
		ext_variables_get_config(var_ext);
	const struct sieve_variables_modifier *modfs;
	unsigned int i, modf_count;

	/* Hold value within limits */
	if ( str_len(*value) > config->max_variable_size ) {
		/* assume variable originates from code, so copy it first */
		string_t *new_value = t_str_new(config->max_variable_size+3);
		str_append_str(new_value, *value);
		*value = new_value;
		str_truncate_utf8(*value, config->max_variable_size);
	}
	
	if ( !array_is_created(modifiers) )
		return SIEVE_EXEC_OK;

	modfs = array_get(modifiers, &modf_count);
	if ( modf_count == 0 )
		return SIEVE_EXEC_OK;

	for ( i = 0; i < modf_count; i++ ) {
		string_t *new_value;
		const struct sieve_variables_modifier *modf = &modfs[i];

		if ( modf->def != NULL && modf->def->modify != NULL ) {
			if ( !modf->def->modify(modf, *value, &new_value) )
				return SIEVE_EXEC_FAILURE;

			*value = new_value;
			if ( *value == NULL )
				return SIEVE_EXEC_FAILURE;

			sieve_runtime_trace_here
				(renv, SIEVE_TRLVL_COMMANDS,
					"modify :%s \"%s\" => \"%s\"",
					sieve_variables_modifier_name(modf),
					str_sanitize(str_c(*value), 256),
					str_sanitize(str_c(new_value), 256));

			/* Hold value within limits */
			if ( str_len(*value) > config->max_variable_size )
				str_truncate_utf8(*value, config->max_variable_size);
		}
	}
	return SIEVE_EXEC_OK;
}