File: ext-imapflags.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (190 lines) | stat: -rw-r--r-- 4,746 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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

/* Extension imapflags
 * --------------------
 *
 * Authors: Stephan Bosch
 * Specification: draft-melnikov-sieve-imapflags-03.txt
 * Implementation: full, but deprecated; provided for backwards compatibility
 * Status: testing
 *
 */

#include "lib.h"
#include "mempool.h"
#include "str.h"

#include "sieve-common.h"

#include "sieve-ast.h"
#include "sieve-code.h"
#include "sieve-extensions.h"
#include "sieve-actions.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"

#include "ext-imap4flags-common.h"

/*
 * Commands
 */

static bool cmd_mark_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);

/* Mark command
 *
 * Syntax:
 *   mark
 */

static const struct sieve_command_def cmd_mark = {
    "mark",
    SCT_COMMAND,
    0, 0, FALSE, FALSE,
    NULL, NULL,
    cmd_mark_validate,
    NULL, NULL, NULL
};

/* Unmark command
 *
 * Syntax:
 *   unmark
 */
static const struct sieve_command_def cmd_unmark = {
    "unmark",
    SCT_COMMAND,
    0, 0, FALSE, FALSE,
    NULL, NULL,
    cmd_mark_validate,
    NULL, NULL, NULL
};

/*
 * Extension
 */

static bool ext_imapflags_load
	(const struct sieve_extension *ext, void **context);
static bool ext_imapflags_validator_load
	(const struct sieve_extension *ext, struct sieve_validator *valdtr);
static bool ext_imapflags_interpreter_load
	(const struct sieve_extension *ext, const struct sieve_runtime_env *renv,
		sieve_size_t *address);

const struct sieve_extension_def imapflags_extension = {
	.name = "imapflags",
	.load = ext_imapflags_load,
	.validator_load = ext_imapflags_validator_load,
	.interpreter_load = ext_imapflags_interpreter_load
};

static bool ext_imapflags_load
(const struct sieve_extension *ext, void **context)
{
	if ( *context == NULL ) {
		/* Make sure real extension is registered, it is needed by the binary */
		*context = (void *)
			sieve_extension_require(ext->svinst, &imap4flags_extension, FALSE);
	}

	return TRUE;
}

/*
 * Validator
 */

static bool ext_imapflags_validator_extension_validate
	(const struct sieve_extension *ext, struct sieve_validator *valdtr,
		void *context, struct sieve_ast_argument *require_arg);

const struct sieve_validator_extension imapflags_validator_extension = {
	&imapflags_extension,
	ext_imapflags_validator_extension_validate,
	NULL
};

static bool ext_imapflags_validator_load
(const struct sieve_extension *ext, struct sieve_validator *valdtr)
{
	const struct sieve_extension *master_ext =
		(const struct sieve_extension *) ext->context;

	sieve_validator_extension_register
		(valdtr, ext, &imapflags_validator_extension, NULL);

	/* Register commands */
	sieve_validator_register_command(valdtr, master_ext, &cmd_setflag);
	sieve_validator_register_command(valdtr, master_ext, &cmd_addflag);
	sieve_validator_register_command(valdtr, master_ext, &cmd_removeflag);

	sieve_validator_register_command(valdtr, master_ext, &cmd_mark);
	sieve_validator_register_command(valdtr, master_ext, &cmd_unmark);

    /* Attach implicit flags tag to keep and fileinto commands */
    ext_imap4flags_attach_flags_tag(valdtr, master_ext, "keep", TRUE);
    ext_imap4flags_attach_flags_tag(valdtr, master_ext, "fileinto", TRUE);

	return TRUE;
}

static bool ext_imapflags_validator_extension_validate
(const struct sieve_extension *ext, struct sieve_validator *valdtr,
	void *context ATTR_UNUSED, struct sieve_ast_argument *require_arg)
{
	const struct sieve_extension *master_ext =
		(const struct sieve_extension *) ext->context;

	if ( sieve_validator_extension_loaded(valdtr, master_ext) ) {
		sieve_argument_validate_error(valdtr, require_arg,
			"the (deprecated) imapflags extension cannot be used "
			"together with the imap4flags extension");
		return FALSE;
	}

	return TRUE;
}

/*
 * Interpreter
 */

static bool ext_imapflags_interpreter_load
(const struct sieve_extension *ext, const struct sieve_runtime_env *renv,
	sieve_size_t *address ATTR_UNUSED)
{
	const struct sieve_extension *master_ext =
		(const struct sieve_extension *) ext->context;

	sieve_interpreter_extension_register
		(renv->interp, master_ext, &imap4flags_interpreter_extension, NULL);

	return TRUE;
}

/*
 * Command validation
 */

static bool cmd_mark_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
	if ( sieve_command_is(cmd, cmd_mark) )
		cmd->def = &cmd_addflag;
	else
		cmd->def = &cmd_removeflag;

	cmd->first_positional = sieve_ast_argument_cstring_create
		(cmd->ast_node, "\\flagged", cmd->ast_node->source_line);

	if ( !sieve_validator_argument_activate
		(valdtr, cmd, cmd->first_positional, FALSE) )
		return FALSE;

	return TRUE;
}