File: sieve-match-types.h

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 (236 lines) | stat: -rw-r--r-- 6,167 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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#ifndef __SIEVE_MATCH_TYPES_H
#define __SIEVE_MATCH_TYPES_H

#include "sieve-common.h"
#include "sieve-extensions.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-objects.h"

/*
 * Types
 */

struct sieve_match_type_context;

/*
 * Core match types
 */

enum sieve_match_type_code {
	SIEVE_MATCH_TYPE_IS,
	SIEVE_MATCH_TYPE_CONTAINS,
	SIEVE_MATCH_TYPE_MATCHES,
	SIEVE_MATCH_TYPE_CUSTOM
};

extern const struct sieve_match_type_def is_match_type;
extern const struct sieve_match_type_def contains_match_type;
extern const struct sieve_match_type_def matches_match_type;

/*
 * Match type definition
 */

struct sieve_match_type_def {
	struct sieve_object_def obj_def;

	bool (*validate)
		(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
			struct sieve_match_type_context *ctx);
	bool (*validate_context)
		(struct sieve_validator *valdtr, struct sieve_ast_argument *arg,
			struct sieve_match_type_context *ctx, struct sieve_ast_argument *key_arg);

	/*
	 * Matching
 	 */

	/* Custom implementation */

	int (*match)
		(struct sieve_match_context *mctx, struct sieve_stringlist *value_list,
			struct sieve_stringlist *key_list);

	/* Default match loop */

	void (*match_init)(struct sieve_match_context *mctx);

	int (*match_keys)
		(struct sieve_match_context *mctx, const char *val, size_t val_size,
			struct sieve_stringlist *key_list);
	int (*match_key)
		(struct sieve_match_context *mctx, const char *val, size_t val_size,
			const char *key, size_t key_size);

	void (*match_deinit)(struct sieve_match_context *mctx);
};

/*
 * Match type instance
 */

struct sieve_match_type {
	struct sieve_object object;

	const struct sieve_match_type_def *def;
};

#define SIEVE_MATCH_TYPE_DEFAULT(definition) \
	{ SIEVE_OBJECT_DEFAULT(definition), &(definition) }

#define sieve_match_type_name(mcht) \
	( (mcht)->object.def->identifier )
#define sieve_match_type_is(mcht, definition) \
	( (mcht)->def == &(definition) )

static inline const struct sieve_match_type *sieve_match_type_copy
(pool_t pool, const struct sieve_match_type *cmp_orig)
{
	struct sieve_match_type *cmp = p_new(pool, struct sieve_match_type, 1);

	*cmp = *cmp_orig;

	return cmp;
}

/*
 * Match type context
 */

struct sieve_match_type_context {
	struct sieve_command *command;
	struct sieve_ast_argument *argument;

	const struct sieve_match_type *match_type;

	/* Only filled in when match_type->validate_context() is called */
	const struct sieve_comparator *comparator;

	/* Context data could be used in the future to pass data between validator and
	 * generator in match types that use extra parameters. Currently not
	 * necessary, not even for the relational extension.
	 */
	void *ctx_data;
};

/*
 * Match type registration
 */

void sieve_match_type_register
	(struct sieve_validator *valdtr, const struct sieve_extension *ext,
		const struct sieve_match_type_def *mcht);

/*
 * Match values
 */

struct sieve_match_values;

bool sieve_match_values_set_enabled
	(const struct sieve_runtime_env *renv, bool enable);
bool sieve_match_values_are_enabled
	(const struct sieve_runtime_env *renv);

struct sieve_match_values *sieve_match_values_start
	(const struct sieve_runtime_env *renv);
void sieve_match_values_set
	(struct sieve_match_values *mvalues, unsigned int index, string_t *value);
void sieve_match_values_add
	(struct sieve_match_values *mvalues, string_t *value);
void sieve_match_values_add_char
	(struct sieve_match_values *mvalues, char c);
void sieve_match_values_skip
	(struct sieve_match_values *mvalues, int num);

void sieve_match_values_commit
	(const struct sieve_runtime_env *renv, struct sieve_match_values **mvalues);
void sieve_match_values_abort
	(struct sieve_match_values **mvalues);

void sieve_match_values_get
	(const struct sieve_runtime_env *renv, unsigned int index, string_t **value_r);

/*
 * Match type tagged argument
 */

extern const struct sieve_argument_def match_type_tag;

static inline bool sieve_argument_is_match_type
	(struct sieve_ast_argument *arg)
{
	return ( arg->argument->def == &match_type_tag );
}

void sieve_match_types_link_tags
	(struct sieve_validator *valdtr,
		struct sieve_command_registration *cmd_reg, int id_code);

/*
 * Validation
 */

bool sieve_match_type_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd,
		struct sieve_ast_argument *key_arg,
		const struct sieve_match_type *mcht_default,
		const struct sieve_comparator *cmp_default);

void sieve_match_type_arguments_remove
	(struct sieve_validator *valdtr, struct sieve_command *cmd);

/*
 * Match type operand
 */

extern const struct sieve_operand_def match_type_operand;
extern const struct sieve_operand_class sieve_match_type_operand_class;

#define SIEVE_EXT_DEFINE_MATCH_TYPE(OP) SIEVE_EXT_DEFINE_OBJECT(OP)
#define SIEVE_EXT_DEFINE_MATCH_TYPES(OPS) SIEVE_EXT_DEFINE_OBJECTS(OPS)

static inline bool sieve_operand_is_match_type
(const struct sieve_operand *operand)
{
	return ( operand != NULL && operand->def != NULL &&
		operand->def->class == &sieve_match_type_operand_class );
}

static inline void sieve_opr_match_type_emit
(struct sieve_binary_block *sblock, const struct sieve_match_type *mcht)
{
	sieve_opr_object_emit(sblock, mcht->object.ext, mcht->object.def);
}

static inline bool sieve_opr_match_type_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	return sieve_opr_object_dump
		(denv, &sieve_match_type_operand_class, address, NULL);
}

static inline int sieve_opr_match_type_read
(const struct sieve_runtime_env *renv, sieve_size_t *address,
	struct sieve_match_type *mcht)
{
	if ( !sieve_opr_object_read
		(renv, &sieve_match_type_operand_class, address, &mcht->object) )
		return SIEVE_EXEC_BIN_CORRUPT;

	mcht->def = (const struct sieve_match_type_def *) mcht->object.def;
	return SIEVE_EXEC_OK;
}

/* Common validation implementation */

bool sieve_match_substring_validate_context
	(struct sieve_validator *valdtr, struct sieve_ast_argument *arg,
		struct sieve_match_type_context *ctx,
		struct sieve_ast_argument *key_arg);

#endif /* __SIEVE_MATCH_TYPES_H */