File: tag-flags.c

package info (click to toggle)
dovecot 1%3A2.2.13-11~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 38,468 kB
  • sloc: ansic: 341,153; sh: 16,920; makefile: 5,381; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (408 lines) | stat: -rw-r--r-- 10,404 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
404
405
406
407
408
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str-sanitize.h"
#include "array.h"
#include "mail-storage.h"

#include "sieve-code.h"
#include "sieve-stringlist.h"
#include "sieve-extensions.h"
#include "sieve-commands.h"
#include "sieve-result.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-actions.h"
#include "sieve-dump.h"

#include "ext-imap4flags-common.h"

#include <ctype.h>

/*
 * Flags tagged argument
 */

static bool tag_flags_validate
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);
static bool tag_flags_validate_persistent
	(struct sieve_validator *valdtr, struct sieve_command *cmd,
		const struct sieve_extension *ext);
static bool tag_flags_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
		struct sieve_command *cmd);

const struct sieve_argument_def tag_flags = {
	"flags",
	NULL,
	tag_flags_validate,
	NULL, NULL,
	tag_flags_generate
};

const struct sieve_argument_def tag_flags_implicit = {
	"flags-implicit",
	NULL,	NULL, NULL,
	tag_flags_validate_persistent,
	tag_flags_generate
};

/*
 * Side effect
 */

static bool seff_flags_dump_context
	(const struct sieve_side_effect *seffect,
    	const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int seff_flags_read_context
	(const struct sieve_side_effect *seffect,
		const struct sieve_runtime_env *renv, sieve_size_t *address,
		void **context);

static int seff_flags_merge
	(const struct sieve_runtime_env *renv, const struct sieve_action *action,
		const struct sieve_side_effect *old_seffect,
		const struct sieve_side_effect *new_seffect, void **old_context);

static void seff_flags_print
	(const struct sieve_side_effect *seffect, const struct sieve_action *action,
		const struct sieve_result_print_env *rpenv, bool *keep);
static int seff_flags_pre_execute
	(const struct sieve_side_effect *seffect, const struct sieve_action *action,
		const struct sieve_action_exec_env *aenv, void **context, void *tr_context);

const struct sieve_side_effect_def flags_side_effect = {
	SIEVE_OBJECT("flags", &flags_side_effect_operand, 0),
	&act_store,

	seff_flags_dump_context,
	seff_flags_read_context,
	seff_flags_merge,
	seff_flags_print,
	seff_flags_pre_execute,
	NULL, NULL, NULL
};

/*
 * Operand
 */

static const struct sieve_extension_objects ext_side_effects =
	SIEVE_EXT_DEFINE_SIDE_EFFECT(flags_side_effect);

const struct sieve_operand_def flags_side_effect_operand = {
	"flags operand",
	&imap4flags_extension,
	0,
	&sieve_side_effect_operand_class,
	&ext_side_effects
};

/*
 * Tag validation
 */

static bool tag_flags_validate_persistent
(struct sieve_validator *valdtr ATTR_UNUSED, struct sieve_command *cmd,
	const struct sieve_extension *ext)
{
	if ( sieve_command_find_argument(cmd, &tag_flags) == NULL ) {
		sieve_command_add_dynamic_tag(cmd, ext, &tag_flags_implicit, -1);
	}

	return TRUE;
}

static bool tag_flags_validate
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *tag = *arg;

	/* Detach the tag itself */
	*arg = sieve_ast_argument_next(*arg);

	/* Check syntax:
	 *   :flags <list-of-flags: string-list>
	 */
	if ( !sieve_validate_tag_parameter
		(valdtr, cmd, tag, *arg, NULL, 0, SAAT_STRING_LIST, FALSE) ) {
		return FALSE;
	}

	tag->parameters = *arg;

	/* Detach parameter */
	*arg = sieve_ast_arguments_detach(*arg,1);

	return TRUE;
}

/*
 * Code generation
 */

static bool tag_flags_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *param;

	if ( sieve_ast_argument_type(arg) != SAAT_TAG ) {
		return FALSE;
	}

	sieve_opr_side_effect_emit
		(cgenv->sblock, arg->argument->ext, &flags_side_effect);

	if ( sieve_argument_is(arg, tag_flags) ) {
		/* Explicit :flags tag */
		param = arg->parameters;

		/* Call the generation function for the argument */
		if ( param->argument != NULL && param->argument->def != NULL &&
			param->argument->def->generate != NULL &&
			!param->argument->def->generate(cgenv, param, cmd) )
			return FALSE;

	} else if ( sieve_argument_is(arg, tag_flags_implicit) ) {
		/* Implicit flags */
		sieve_opr_omitted_emit(cgenv->sblock);

	} else {
		/* Something else?! */
		i_unreached();
	}

	return TRUE;
}

/*
 * Side effect implementation
 */

/* Context data */

struct seff_flags_context {
	ARRAY(const char *) keywords;
	enum mail_flags flags;
};

/* Context coding */

static bool seff_flags_dump_context
(const struct sieve_side_effect *seffect ATTR_UNUSED,
	const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	return sieve_opr_stringlist_dump_ex(denv, address, "flags", "INTERNAL");
}

static struct seff_flags_context *seff_flags_get_implicit_context
(const struct sieve_extension *this_ext, struct sieve_result *result)
{
	pool_t pool = sieve_result_pool(result);
	struct seff_flags_context *ctx;
	const char *flag;
	struct ext_imap4flags_iter flit;

	ctx = p_new(pool, struct seff_flags_context, 1);
	p_array_init(&ctx->keywords, pool, 2);

	T_BEGIN {

		/* Unpack */
		ext_imap4flags_get_implicit_flags_init(&flit, this_ext, result);
		while ( (flag=ext_imap4flags_iter_get_flag(&flit)) != NULL ) {
			if (flag != NULL && *flag != '\\') {
				/* keyword */
				const char *keyword = p_strdup(pool, flag);
				array_append(&ctx->keywords, &keyword, 1);
			} else {
				/* system flag */
				if (flag == NULL || strcasecmp(flag, "\\flagged") == 0)
					ctx->flags |= MAIL_FLAGGED;
				else if (strcasecmp(flag, "\\answered") == 0)
					ctx->flags |= MAIL_ANSWERED;
				else if (strcasecmp(flag, "\\deleted") == 0)
					ctx->flags |= MAIL_DELETED;
				else if (strcasecmp(flag, "\\seen") == 0)
					ctx->flags |= MAIL_SEEN;
				else if (strcasecmp(flag, "\\draft") == 0)
					ctx->flags |= MAIL_DRAFT;
			}
		}

	} T_END;

	return ctx;
}

static int seff_flags_do_read_context
(const struct sieve_side_effect *seffect,
	const struct sieve_runtime_env *renv, sieve_size_t *address,
	void **se_context)
{
	pool_t pool = sieve_result_pool(renv->result);
	struct seff_flags_context *ctx;
	string_t *flags_item;
	struct sieve_stringlist *flag_list = NULL;
	int ret;

	if ( (ret=sieve_opr_stringlist_read_ex
		(renv, address, "flags", TRUE, &flag_list)) <= 0 )
		return ret;

	if ( flag_list == NULL ) {
		/* Flag list is omitted, use current value of internal
		 * variable to construct side effect context.
		 */
		*se_context = seff_flags_get_implicit_context
			(SIEVE_OBJECT_EXTENSION(seffect), renv->result);
		return SIEVE_EXEC_OK;
	}

	ctx = p_new(pool, struct seff_flags_context, 1);
	p_array_init(&ctx->keywords, pool, 2);

	/* Unpack */
	flags_item = NULL;
	while ( (ret=sieve_stringlist_next_item(flag_list, &flags_item)) > 0 ) {
		const char *flag;
		struct ext_imap4flags_iter flit;

		ext_imap4flags_iter_init(&flit, flags_item);

		while ( (flag=ext_imap4flags_iter_get_flag(&flit)) != NULL ) {
			if (flag != NULL && *flag != '\\') {
				/* keyword */
				const char *keyword = p_strdup(pool, flag);

				/* FIXME: should check for duplicates (cannot trust variables) */
				array_append(&ctx->keywords, &keyword, 1);

			} else {
				/* system flag */
				if (flag == NULL || strcasecmp(flag, "\\flagged") == 0)
					ctx->flags |= MAIL_FLAGGED;
				else if (strcasecmp(flag, "\\answered") == 0)
					ctx->flags |= MAIL_ANSWERED;
				else if (strcasecmp(flag, "\\deleted") == 0)
					ctx->flags |= MAIL_DELETED;
				else if (strcasecmp(flag, "\\seen") == 0)
					ctx->flags |= MAIL_SEEN;
				else if (strcasecmp(flag, "\\draft") == 0)
					ctx->flags |= MAIL_DRAFT;
			}
		}
	}

	if ( ret < 0 )
		return flag_list->exec_status;

	*se_context = (void *) ctx;

	return SIEVE_EXEC_OK;
}

static int seff_flags_read_context
(const struct sieve_side_effect *seffect,
	const struct sieve_runtime_env *renv, sieve_size_t *address,
	void **se_context)
{
	int ret;

	T_BEGIN {
		ret = seff_flags_do_read_context(seffect, renv, address, se_context);
	} T_END;

	return ret;
}

/* Result verification */

static int seff_flags_merge
(const struct sieve_runtime_env *renv ATTR_UNUSED,
	const struct sieve_action *action ATTR_UNUSED,
	const struct sieve_side_effect *old_seffect ATTR_UNUSED,
	const struct sieve_side_effect *new_seffect,
	void **old_context)
{
	if ( new_seffect != NULL )
		*old_context = new_seffect->context;

	return 1;
}

/* Result printing */

static void seff_flags_print
(const struct sieve_side_effect *seffect,
	const struct sieve_action *action ATTR_UNUSED,
	const struct sieve_result_print_env *rpenv,bool *keep ATTR_UNUSED)
{
	struct sieve_result *result = rpenv->result;
	struct seff_flags_context *ctx =
		(struct seff_flags_context *) seffect->context;
	unsigned int i;

	if ( ctx == NULL )
		ctx = seff_flags_get_implicit_context
			(SIEVE_OBJECT_EXTENSION(seffect), result);

	if ( ctx->flags != 0 || array_count(&ctx->keywords) > 0 ) {
		T_BEGIN {
			string_t *flags = t_str_new(128);

			if ( (ctx->flags & MAIL_FLAGGED) > 0 )
				str_printfa(flags, " \\flagged");

			if ( (ctx->flags & MAIL_ANSWERED) > 0 )
				str_printfa(flags, " \\answered");

			if ( (ctx->flags & MAIL_DELETED) > 0 )
				str_printfa(flags, " \\deleted");

			if ( (ctx->flags & MAIL_SEEN) > 0 )
				str_printfa(flags, " \\seen");

			if ( (ctx->flags & MAIL_DRAFT) > 0 )
				str_printfa(flags, " \\draft");

			for ( i = 0; i < array_count(&ctx->keywords); i++ ) {
				const char *const *keyword = array_idx(&ctx->keywords, i);
				str_printfa(flags, " %s", str_sanitize(*keyword, 64));
			}

			sieve_result_seffect_printf(rpenv, "add IMAP flags:%s", str_c(flags));
		} T_END;
	}
}

/* Result execution */

static int seff_flags_pre_execute
(const struct sieve_side_effect *seffect,
	const struct sieve_action *action ATTR_UNUSED,
	const struct sieve_action_exec_env *aenv, void **context, void *tr_context)
{
	struct seff_flags_context *ctx = (struct seff_flags_context *) *context;
	const char *const *keywords;

	if ( ctx == NULL ) {
		ctx = seff_flags_get_implicit_context
			(SIEVE_OBJECT_EXTENSION(seffect), aenv->result);
		*context = (void *) ctx;
	}

	(void)array_append_space(&ctx->keywords);
	keywords = array_idx(&ctx->keywords, 0);

	sieve_act_store_add_flags(aenv, tr_context, keywords, ctx->flags);

	return SIEVE_EXEC_OK;
}