File: ext-variables-namespaces.c

package info (click to toggle)
dovecot 1%3A1.2.15-7%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 30,340 kB
  • ctags: 20,012
  • sloc: ansic: 191,443; sh: 21,091; makefile: 3,330; cpp: 526; perl: 108; xml: 44
file content (233 lines) | stat: -rw-r--r-- 6,814 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2002-2010 Dovecot Sieve authors, see the included COPYING file
 */

#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-binary.h"
#include "sieve-dump.h"

#include "ext-variables-common.h"
#include "ext-variables-namespaces.h"

#include <ctype.h>

/*
 * Namespace registry
 */

void sieve_variables_namespace_register
(const struct sieve_extension *var_ext, struct sieve_validator *valdtr,
	const struct sieve_extension *ext,
	const struct sieve_variables_namespace_def *nspc_def)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, valdtr);

	sieve_validator_object_registry_add(ctx->namespaces, ext, &nspc_def->obj_def);
}

bool ext_variables_namespace_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->namespaces, identifier, NULL);
}

const struct sieve_variables_namespace *ext_variables_namespace_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_namespace *nspc;
	pool_t pool;

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

	pool = sieve_command_pool(cmd);
	nspc = p_new(pool, struct sieve_variables_namespace, 1);
	nspc->object = object;
	nspc->def = (const struct sieve_variables_namespace_def *) object.def;

  return nspc;
}

/*
 * Namespace variable argument
 */

struct arg_namespace_variable {
	const struct sieve_variables_namespace *namespace;

	void *data;
};

static bool arg_namespace_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *context ATTR_UNUSED);

const struct sieve_argument_def namespace_argument = {
	"@namespace",
	NULL, NULL, NULL, NULL,
	arg_namespace_generate
};

bool ext_variables_namespace_argument_activate
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr,
	struct sieve_ast_argument *arg, struct sieve_command *cmd,
	ARRAY_TYPE(sieve_variable_name) *var_name, bool assignment)
{
	pool_t pool = sieve_command_pool(cmd);
	struct sieve_ast *ast = arg->ast;
	const struct sieve_variables_namespace *nspc;
	struct arg_namespace_variable *var;
	const struct sieve_variable_name *name_element = array_idx(var_name, 0);
	void *var_data;

	nspc = ext_variables_namespace_create_instance
		(this_ext, valdtr, cmd, str_c(name_element->identifier));
	if ( nspc == NULL ) {
		sieve_argument_validate_error(valdtr, arg,
			"referring to variable in unknown namespace '%s'",
			str_c(name_element->identifier));
		return FALSE;
	}

	if ( nspc->def != NULL && nspc->def->validate != NULL &&
		!nspc->def->validate
			(valdtr, nspc, arg, cmd, var_name, &var_data, assignment) ) {
		return FALSE;
	}

	var = p_new(pool, struct arg_namespace_variable, 1);
	var->namespace = nspc;
	var->data = var_data;

	arg->argument = sieve_argument_create(ast, &namespace_argument, this_ext, 0);
	arg->argument->data = (void *) var;

	return TRUE;
}

struct sieve_ast_argument *ext_variables_namespace_argument_create
(const struct sieve_extension *this_ext,
	struct sieve_validator *valdtr, struct sieve_ast_argument *parent_arg,
	struct sieve_command *cmd, ARRAY_TYPE(sieve_variable_name) *var_name)
{
	struct sieve_ast *ast = parent_arg->ast;
	struct sieve_ast_argument *new_arg;

	new_arg = sieve_ast_argument_create(ast, sieve_ast_argument_line(parent_arg));
	new_arg->type = SAAT_STRING;

	if ( !ext_variables_namespace_argument_activate
		(this_ext, valdtr, new_arg, cmd, var_name, FALSE) )
		return NULL;

	return new_arg;
}

static bool arg_namespace_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
	struct sieve_command *cmd)
{
	struct sieve_argument *argument = arg->argument;
	struct arg_namespace_variable *var =
		(struct arg_namespace_variable *) argument->data;
	const struct sieve_variables_namespace *nspc = var->namespace;

	if ( nspc->def != NULL && nspc->def->generate != NULL )
		return nspc->def->generate(cgenv, nspc, arg, cmd, var->data);

	return TRUE;
}

/*
 * Namespace variable operands
 */

const struct sieve_operand_class sieve_variables_namespace_operand_class =
	{ "variable-namespace" };

static bool opr_namespace_variable_read
	(const struct sieve_runtime_env *renv, const struct sieve_operand *operand,
		sieve_size_t *address, string_t **str);
static bool opr_namespace_variable_dump
	(const struct sieve_dumptime_env *denv, const struct sieve_operand *operand,
		sieve_size_t *address, const char *field_name);

static const struct sieve_opr_string_interface namespace_variable_interface = {
	opr_namespace_variable_dump,
	opr_namespace_variable_read
};

const struct sieve_operand_def namespace_variable_operand = {
	"namespace",
	&variables_extension,
	EXT_VARIABLES_OPERAND_NAMESPACE_VARIABLE,
	&string_class,
	&namespace_variable_interface
};

void sieve_variables_opr_namespace_variable_emit
(struct sieve_binary *sbin, const struct sieve_extension *var_ext,
	const struct sieve_extension *ext,
	const struct sieve_variables_namespace_def *nspc_def)
{
	sieve_operand_emit(sbin, var_ext, &namespace_variable_operand);
	sieve_opr_object_emit(sbin, ext, &nspc_def->obj_def);
}

static bool opr_namespace_variable_read
(const struct sieve_runtime_env *renv,
	const struct sieve_operand *operand ATTR_UNUSED,
	sieve_size_t *address, string_t **str)
{
	struct sieve_variables_namespace nspc;

	if ( !sieve_opr_object_read
		(renv, &sieve_variables_namespace_operand_class, address, &nspc.object) )
		return FALSE;

	nspc.def = (const struct sieve_variables_namespace_def *) nspc.object.def;

	if ( nspc.def == NULL || nspc.def->read_variable == NULL )
		return FALSE;


	return nspc.def->read_variable(renv, &nspc, address, str);
}

static bool opr_namespace_variable_dump
(const struct sieve_dumptime_env *denv,
	const struct sieve_operand *operand ATTR_UNUSED,
	sieve_size_t *address, const char *field_name)
{
	struct sieve_variables_namespace nspc;
	struct sieve_operand oprnd;

	if ( !sieve_operand_read(denv->sbin, address, &oprnd) ) {
		return FALSE;
	}

	if ( !sieve_opr_object_read_data
		(denv->sbin, &oprnd, &sieve_variables_namespace_operand_class, address,
			&nspc.object) ) {
		return FALSE;
  }

	nspc.def = (const struct sieve_variables_namespace_def *) nspc.object.def;

	if ( nspc.def == NULL || nspc.def->dump_variable == NULL )
		return FALSE;

	return nspc.def->dump_variable(denv, &nspc, address, field_name);
}