File: sieve-commands.c

package info (click to toggle)
dovecot 1%3A2.2.27-3%2Bdeb9u5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,792 kB
  • sloc: ansic: 430,517; sh: 17,438; makefile: 6,587; cpp: 1,557; perl: 295; python: 67; xml: 44; pascal: 27
file content (403 lines) | stat: -rw-r--r-- 10,114 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2016 Pigeonhole authors, see the included COPYING file
 */

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

#include "rfc2822.h"

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

/*
 * Literal arguments
 */

/* Forward declarations */

static bool arg_number_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
		struct sieve_command *context);
static bool arg_string_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
		struct sieve_command *context);
static bool arg_string_list_validate
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *context);
static bool arg_string_list_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
		struct sieve_command *context);

/* Argument objects */

const struct sieve_argument_def number_argument = {
	.identifier = "@number",
	.generate = arg_number_generate
};

const struct sieve_argument_def string_argument = {
	.identifier = "@string",
	.generate = arg_string_generate
};

const struct sieve_argument_def string_list_argument = {
	.identifier = "@string-list",
	.validate = arg_string_list_validate,
	.generate = arg_string_list_generate
};

/* Argument implementations */

static bool arg_number_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd ATTR_UNUSED)
{
	sieve_opr_number_emit(cgenv->sblock, sieve_ast_argument_number(arg));

	return TRUE;
}

static bool arg_string_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd ATTR_UNUSED)
{
	sieve_opr_string_emit(cgenv->sblock, sieve_ast_argument_str(arg));

	return TRUE;
}

static bool arg_string_list_validate
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *stritem;

	stritem = sieve_ast_strlist_first(*arg);
	while ( stritem != NULL ) {
		if ( !sieve_validator_argument_activate(valdtr, cmd, stritem, FALSE) )
			return FALSE;

		stritem = sieve_ast_strlist_next(stritem);
	}

	return TRUE;
}

static bool emit_string_list_operand
(const struct sieve_codegen_env *cgenv, const struct sieve_ast_argument *strlist,
	struct sieve_command *cmd)
{
	void *list_context;
	struct sieve_ast_argument *stritem;

	sieve_opr_stringlist_emit_start
		(cgenv->sblock, sieve_ast_strlist_count(strlist), &list_context);

	stritem = sieve_ast_strlist_first(strlist);
	while ( stritem != NULL ) {
		if ( !sieve_generate_argument(cgenv, stritem, cmd) )
			return FALSE;

		stritem = sieve_ast_strlist_next(stritem);
	}

	sieve_opr_stringlist_emit_end(cgenv->sblock, list_context);

	return TRUE;
}

static bool arg_string_list_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd)
{
	if ( sieve_ast_argument_type(arg) == SAAT_STRING ) {
		return ( sieve_generate_argument(cgenv, arg, cmd) );

	} else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
		bool result = TRUE;

		if ( sieve_ast_strlist_count(arg) == 1 )
			return ( sieve_generate_argument
				(cgenv, sieve_ast_strlist_first(arg), cmd) );
		else {
			T_BEGIN {
				result=emit_string_list_operand(cgenv, arg, cmd);
			} T_END;
		}

		return result;
	}

	return FALSE;
}

/*
 * Abstract arguments
 *
 *   (Generated by processing and not by parsing the grammar)
 */

/* Catenated string */

struct sieve_arg_catenated_string {
	struct sieve_ast_arg_list *str_parts;
};

struct sieve_arg_catenated_string *sieve_arg_catenated_string_create
(struct sieve_ast_argument *orig_arg)
{
	pool_t pool = sieve_ast_pool(orig_arg->ast);
	struct sieve_ast_arg_list *arglist;
	struct sieve_arg_catenated_string *catstr;

	arglist = sieve_ast_arg_list_create(pool);

	catstr = p_new(pool, struct sieve_arg_catenated_string, 1);
	catstr->str_parts = arglist;
	(orig_arg)->argument->data = (void *) catstr;

	return catstr;
}

void sieve_arg_catenated_string_add_element
(struct sieve_arg_catenated_string *catstr,
	struct sieve_ast_argument *element)
{
	sieve_ast_arg_list_add(catstr->str_parts, element);
}

#define _cat_string_first(catstr) __AST_LIST_FIRST((catstr)->str_parts)
#define _cat_string_count(catstr) __AST_LIST_COUNT((catstr)->str_parts)
#define _cat_string_next(item) __AST_LIST_NEXT(item)

bool sieve_arg_catenated_string_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd)
{
	struct sieve_arg_catenated_string *catstr =
		(struct sieve_arg_catenated_string *) arg->argument->data;
	struct sieve_ast_argument *strpart;

	if ( _cat_string_count(catstr) == 1 )
		sieve_generate_argument(cgenv, _cat_string_first(catstr), cmd);
	else {
		sieve_opr_catenated_string_emit(cgenv->sblock, _cat_string_count(catstr));

		strpart = _cat_string_first(catstr);
		while ( strpart != NULL ) {
			if ( !sieve_generate_argument(cgenv, strpart, cmd) )
				return FALSE;

			strpart = _cat_string_next(strpart);
		}
	}

	return TRUE;
}

/*
 * Argument creation
 */

struct sieve_argument *sieve_argument_create
(struct sieve_ast *ast, const struct sieve_argument_def *def,
	const struct sieve_extension *ext, int id_code)
{
	struct sieve_argument *arg;
	pool_t pool;

	pool = sieve_ast_pool(ast);
	arg = p_new(pool, struct sieve_argument, 1);
	arg->def = def;
	arg->ext = ext;
	arg->id_code = id_code;

	return arg;
}

/*
 * Core tests and commands
 */

const struct sieve_command_def *sieve_core_tests[] = {
	&tst_false, &tst_true,
	&tst_not, &tst_anyof, &tst_allof,
	&tst_address, &tst_header, &tst_exists, &tst_size
};

const unsigned int sieve_core_tests_count = N_ELEMENTS(sieve_core_tests);

const struct sieve_command_def *sieve_core_commands[] = {
	&cmd_require,
	&cmd_stop, &cmd_if, &cmd_elsif, &cmd_else,
	&cmd_keep, &cmd_discard, &cmd_redirect
};

const unsigned int sieve_core_commands_count = N_ELEMENTS(sieve_core_commands);

/*
 * Command context
 */

struct sieve_command *sieve_command_prev
(struct sieve_command *cmd)
{
	struct sieve_ast_node *node = sieve_ast_node_prev(cmd->ast_node);

	if ( node != NULL ) {
		return node->command;
	}

	return NULL;
}

struct sieve_command *sieve_command_parent
(struct sieve_command *cmd)
{
	struct sieve_ast_node *node = sieve_ast_node_parent(cmd->ast_node);

	return ( node != NULL ? node->command : NULL );
}

struct sieve_command *sieve_command_create
(struct sieve_ast_node *cmd_node, const struct sieve_extension *ext,
	const struct sieve_command_def *cmd_def,
	struct sieve_command_registration *cmd_reg)
{
	struct sieve_command *cmd;

	cmd = p_new(sieve_ast_node_pool(cmd_node), struct sieve_command, 1);

	cmd->ast_node = cmd_node;
	cmd->def = cmd_def;
	cmd->ext = ext;
	cmd->reg = cmd_reg;

	cmd->block_exit_command = NULL;

	return cmd;
}

const char *sieve_command_def_type_name
(const struct sieve_command_def *cmd_def)
{
	switch ( cmd_def->type ) {
	case SCT_NONE: return "command of unspecified type (bug)";
	case SCT_TEST: return "test";
	case SCT_COMMAND: return "command";
	case SCT_HYBRID: return "command or test";
	default:
		break;
	}
	return "??COMMAND-TYPE??";
}

const char *sieve_command_type_name
	(const struct sieve_command *cmd)
{
	switch ( cmd->def->type ) {
	case SCT_NONE: return "command of unspecified type (bug)";
	case SCT_TEST: return "test";
	case SCT_COMMAND: return "command";
	case SCT_HYBRID:
		if ( cmd->ast_node->type == SAT_TEST )
			return "test";
		return "command";
	default:
		break;
	}
	return "??COMMAND-TYPE??";
}

struct sieve_ast_argument *sieve_command_add_dynamic_tag
(struct sieve_command *cmd, const struct sieve_extension *ext,
	const struct sieve_argument_def *tag, int id_code)
{
	struct sieve_ast_argument *arg;

	if ( cmd->first_positional != NULL )
		arg = sieve_ast_argument_tag_insert
			(cmd->first_positional, tag->identifier, cmd->ast_node->source_line);
	else
		arg = sieve_ast_argument_tag_create
			(cmd->ast_node, tag->identifier, cmd->ast_node->source_line);

	arg->argument = sieve_argument_create(cmd->ast_node->ast, tag, ext, id_code);

	return arg;
}

struct sieve_ast_argument *sieve_command_find_argument
(struct sieve_command *cmd, const struct sieve_argument_def *arg_def)
{
	struct sieve_ast_argument *arg = sieve_ast_argument_first(cmd->ast_node);

	/* Visit tagged and optional arguments */
	while ( arg != NULL ) {
		if ( arg->argument != NULL && arg->argument->def == arg_def )
			return arg;

		arg = sieve_ast_argument_next(arg);
	}

	return arg;
}

/* Use this function with caution. The command commits to exiting the block.
 * When it for some reason does not, the interpretation will break later on,
 * because exiting jumps are not generated when they would otherwise be
 * necessary.
 */
void sieve_command_exit_block_unconditionally
	(struct sieve_command *cmd)
{
	struct sieve_command *parent = sieve_command_parent(cmd);

	/* Only the first unconditional exit is of importance */
	if ( parent != NULL && parent->block_exit_command == NULL )
		parent->block_exit_command = cmd;
}

bool sieve_command_block_exits_unconditionally
	(struct sieve_command *cmd)
{
	return ( cmd->block_exit_command != NULL );
}

/*
 * Command utility functions
 */

/* NOTE: this may be moved */

static int _verify_header_name_item
(void *context, struct sieve_ast_argument *header)
{
	struct sieve_validator *valdtr = (struct sieve_validator *) context;
	string_t *name = sieve_ast_argument_str(header);

	if ( sieve_argument_is_string_literal(header) &&
		!rfc2822_header_field_name_verify(str_c(name), str_len(name)) ) {
		sieve_argument_validate_warning
			(valdtr, header, "specified header field name '%s' is invalid",
				str_sanitize(str_c(name), 80));

		return 0;
	}

	return 1;
}

bool sieve_command_verify_headers_argument
(struct sieve_validator *valdtr, struct sieve_ast_argument *headers)
{
	return ( sieve_ast_stringlist_map
		(&headers, (void *) valdtr, _verify_header_name_item) >= 0 );
}