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
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "string.h"
#include "ostream.h"
#include "hash.h"
#include "mail-storage.h"
#include "sieve.h"
#include "sieve-code.h"
#include "sieve-commands.h"
#include "sieve-extensions.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-binary.h"
#include "sieve-dump.h"
#include "testsuite-common.h"
#include "testsuite-objects.h"
#include "testsuite-message.h"
/*
* Testsuite core objects
*/
enum testsuite_object_code {
TESTSUITE_OBJECT_MESSAGE,
TESTSUITE_OBJECT_ENVELOPE
};
const struct testsuite_object_def *testsuite_core_objects[] = {
&message_testsuite_object, &envelope_testsuite_object
};
const unsigned int testsuite_core_objects_count =
N_ELEMENTS(testsuite_core_objects);
/*
* Testsuite object registry
*/
static inline struct sieve_validator_object_registry *_get_object_registry
(struct sieve_validator *valdtr)
{
struct testsuite_validator_context *ctx =
testsuite_validator_context_get(valdtr);
return ctx->object_registrations;
}
void testsuite_object_register
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
const struct testsuite_object_def *tobj_def)
{
struct sieve_validator_object_registry *regs = _get_object_registry(valdtr);
sieve_validator_object_registry_add(regs, ext, &tobj_def->obj_def);
}
static const struct testsuite_object *testsuite_object_create
(struct sieve_validator *valdtr, struct sieve_command *cmd,
const char *identifier)
{
struct sieve_validator_object_registry *regs = _get_object_registry(valdtr);
struct sieve_object object;
struct testsuite_object *tobj;
if ( !sieve_validator_object_registry_find(regs, identifier, &object) )
return NULL;
tobj = p_new(sieve_command_pool(cmd), struct testsuite_object, 1);
tobj->object = object;
tobj->def = (const struct testsuite_object_def *) object.def;
return tobj;
}
void testsuite_register_core_objects
(struct testsuite_validator_context *ctx)
{
struct sieve_validator_object_registry *regs = ctx->object_registrations;
unsigned int i;
/* Register core testsuite objects */
for ( i = 0; i < testsuite_core_objects_count; i++ ) {
const struct testsuite_object_def *tobj_def = testsuite_core_objects[i];
sieve_validator_object_registry_add
(regs, testsuite_ext, &tobj_def->obj_def);
}
}
/*
* Testsuite object code
*/
const struct sieve_operand_class sieve_testsuite_object_operand_class =
{ "testsuite object" };
static const struct sieve_extension_objects core_testsuite_objects =
SIEVE_EXT_DEFINE_OBJECTS(testsuite_core_objects);
const struct sieve_operand_def testsuite_object_operand = {
.name = "testsuite-object",
.ext_def = &testsuite_extension,
.code = TESTSUITE_OPERAND_OBJECT,
.class = &sieve_testsuite_object_operand_class,
.interface = &core_testsuite_objects
};
static void testsuite_object_emit
(struct sieve_binary_block *sblock, const struct testsuite_object *tobj,
int member_id)
{
sieve_opr_object_emit(sblock, tobj->object.ext, tobj->object.def);
if ( tobj->def != NULL && tobj->def->get_member_id != NULL ) {
(void) sieve_binary_emit_byte(sblock, (unsigned char) member_id);
}
}
bool testsuite_object_read
(struct sieve_binary_block *sblock, sieve_size_t *address,
struct testsuite_object *tobj)
{
struct sieve_operand oprnd;
if ( !sieve_operand_read(sblock, address, NULL, &oprnd) )
return FALSE;
if ( !sieve_opr_object_read_data
(sblock, &oprnd, &sieve_testsuite_object_operand_class, address,
&tobj->object) )
return FALSE;
tobj->def = (const struct testsuite_object_def *) tobj->object.def;
i_assert(tobj->def != NULL);
return TRUE;
}
bool testsuite_object_read_member
(struct sieve_binary_block *sblock, sieve_size_t *address,
struct testsuite_object *tobj, int *member_id_r)
{
if ( !testsuite_object_read(sblock, address, tobj) )
return FALSE;
*member_id_r = -1;
if ( tobj->def->get_member_id != NULL ) {
if ( !sieve_binary_read_code(sblock, address, member_id_r) )
return FALSE;
}
return TRUE;
}
const char *testsuite_object_member_name
(const struct testsuite_object *object, int member_id)
{
const struct testsuite_object_def *obj_def = object->def;
const char *member = NULL;
if ( obj_def->get_member_id != NULL ) {
if ( obj_def->get_member_name != NULL )
member = obj_def->get_member_name(member_id);
} else
return obj_def->obj_def.identifier;
if ( member == NULL )
return t_strdup_printf("%s.%d", obj_def->obj_def.identifier, member_id);
return t_strdup_printf("%s.%s", obj_def->obj_def.identifier, member);
}
bool testsuite_object_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
struct testsuite_object object;
int member_id;
sieve_code_mark(denv);
if ( !testsuite_object_read_member
(denv->sblock, address, &object, &member_id) )
return FALSE;
sieve_code_dumpf(denv, "%s: %s",
sieve_testsuite_object_operand_class.name,
testsuite_object_member_name(&object, member_id));
return TRUE;
}
/*
* Testsuite object argument
*/
static bool arg_testsuite_object_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
struct sieve_command *cmd);
const struct sieve_argument_def testsuite_object_argument = {
.identifier = "testsuite-object",
.generate = arg_testsuite_object_generate
};
struct testsuite_object_argctx {
const struct testsuite_object *object;
int member;
};
bool testsuite_object_argument_activate
(struct sieve_validator *valdtr, struct sieve_ast_argument *arg,
struct sieve_command *cmd)
{
const char *objname = sieve_ast_argument_strc(arg);
const struct testsuite_object *tobj;
int member_id;
const char *member;
struct testsuite_object_argctx *ctx;
/* Parse the object specifier */
member = strchr(objname, '.');
if ( member != NULL ) {
objname = t_strdup_until(objname, member);
member++;
}
/* Find the object */
tobj = testsuite_object_create(valdtr, cmd, objname);
if ( tobj == NULL ) {
sieve_argument_validate_error(valdtr, arg,
"unknown testsuite object '%s'", objname);
return FALSE;
}
/* Find the object member */
member_id = -1;
if ( member != NULL ) {
if ( tobj->def == NULL || tobj->def->get_member_id == NULL ||
(member_id=tobj->def->get_member_id(member)) == -1 ) {
sieve_argument_validate_error(valdtr, arg,
"member '%s' does not exist for testsuite object '%s'", member, objname);
return FALSE;
}
}
/* Assign argument context */
ctx = p_new(sieve_command_pool(cmd), struct testsuite_object_argctx, 1);
ctx->object = tobj;
ctx->member = member_id;
arg->argument = sieve_argument_create
(arg->ast, &testsuite_object_argument, testsuite_ext, 0);
arg->argument->data = (void *) ctx;
return TRUE;
}
static bool arg_testsuite_object_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
struct sieve_command *cmd ATTR_UNUSED)
{
struct testsuite_object_argctx *ctx =
(struct testsuite_object_argctx *) arg->argument->data;
testsuite_object_emit(cgenv->sblock, ctx->object, ctx->member);
return TRUE;
}
/*
* Testsuite core object implementation
*/
static bool tsto_message_set_member
(const struct sieve_runtime_env *renv, int id, string_t *value);
static int tsto_envelope_get_member_id(const char *identifier);
static const char *tsto_envelope_get_member_name(int id);
static bool tsto_envelope_set_member
(const struct sieve_runtime_env *renv, int id, string_t *value);
const struct testsuite_object_def message_testsuite_object = {
SIEVE_OBJECT("message",
&testsuite_object_operand, TESTSUITE_OBJECT_MESSAGE),
.set_member = tsto_message_set_member
};
const struct testsuite_object_def envelope_testsuite_object = {
SIEVE_OBJECT("envelope",
&testsuite_object_operand, TESTSUITE_OBJECT_ENVELOPE),
.get_member_id = tsto_envelope_get_member_id,
.get_member_name = tsto_envelope_get_member_name,
.set_member = tsto_envelope_set_member
};
enum testsuite_object_envelope_field {
TESTSUITE_OBJECT_ENVELOPE_FROM,
TESTSUITE_OBJECT_ENVELOPE_TO,
TESTSUITE_OBJECT_ENVELOPE_ORIG_TO,
TESTSUITE_OBJECT_ENVELOPE_AUTH_USER
};
static bool tsto_message_set_member
(const struct sieve_runtime_env *renv, int id, string_t *value)
{
if ( id != -1 ) return FALSE;
testsuite_message_set_string(renv, value);
return TRUE;
}
static int tsto_envelope_get_member_id(const char *identifier)
{
if ( strcasecmp(identifier, "from") == 0 )
return TESTSUITE_OBJECT_ENVELOPE_FROM;
if ( strcasecmp(identifier, "to") == 0 )
return TESTSUITE_OBJECT_ENVELOPE_TO;
if ( strcasecmp(identifier, "orig_to") == 0 )
return TESTSUITE_OBJECT_ENVELOPE_ORIG_TO;
if ( strcasecmp(identifier, "auth") == 0 )
return TESTSUITE_OBJECT_ENVELOPE_AUTH_USER;
return -1;
}
static const char *tsto_envelope_get_member_name(int id)
{
switch ( id ) {
case TESTSUITE_OBJECT_ENVELOPE_FROM:
return "from";
case TESTSUITE_OBJECT_ENVELOPE_TO:
return "to";
case TESTSUITE_OBJECT_ENVELOPE_ORIG_TO:
return "orig_to";
case TESTSUITE_OBJECT_ENVELOPE_AUTH_USER:
return "auth";
}
return NULL;
}
static bool tsto_envelope_set_member
(const struct sieve_runtime_env *renv, int id, string_t *value)
{
switch ( id ) {
case TESTSUITE_OBJECT_ENVELOPE_FROM:
testsuite_envelope_set_sender(renv, str_c(value));
return TRUE;
case TESTSUITE_OBJECT_ENVELOPE_TO:
testsuite_envelope_set_recipient(renv, str_c(value));
return TRUE;
case TESTSUITE_OBJECT_ENVELOPE_ORIG_TO:
testsuite_envelope_set_orig_recipient(renv, str_c(value));
return TRUE;
case TESTSUITE_OBJECT_ENVELOPE_AUTH_USER:
testsuite_envelope_set_auth_user(renv, str_c(value));
return TRUE;
}
return FALSE;
}
|