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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str-sanitize.h"
#include "istream.h"
#include "mail-storage.h"
#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-message.h"
#include "sieve-actions.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-code.h"
#include "sieve-binary.h"
#include "sieve-dump.h"
#include "testsuite-common.h"
#include "testsuite-smtp.h"
#include "testsuite-mailstore.h"
/*
* Commands
*/
/* Test_message command
*
* Syntax:
* test_message ( :smtp / :mailbox <mailbox: string> ) <index: number>
*/
static bool
cmd_test_message_registered(struct sieve_validator *valdtr,
const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg);
static bool
cmd_test_message_validate(struct sieve_validator *valdtr,
struct sieve_command *cmd);
static bool
cmd_test_message_generate(const struct sieve_codegen_env *cgenv,
struct sieve_command *ctx);
const struct sieve_command_def cmd_test_message = {
.identifier = "test_message",
.type = SCT_HYBRID,
.positional_args = 1,
.subtests = 0,
.block_allowed = FALSE,
.block_required = FALSE,
.registered = cmd_test_message_registered,
.validate = cmd_test_message_validate,
.generate = cmd_test_message_generate,
};
/* Test_message_print command
*
* Syntax:
* test_message_print
*/
static bool
cmd_test_message_print_generate(const struct sieve_codegen_env *cgenv,
struct sieve_command *cmd);
const struct sieve_command_def cmd_test_message_print = {
.identifier = "test_message_print",
.type = SCT_COMMAND,
.positional_args = 0,
.subtests = 0,
.block_allowed = FALSE,
.block_required = FALSE,
.generate = cmd_test_message_print_generate,
};
/*
* Operations
*/
/* Test_message_smtp operation */
static bool
cmd_test_message_smtp_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address);
static int
cmd_test_message_smtp_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address);
const struct sieve_operation_def test_message_smtp_operation = {
.mnemonic = "TEST_MESSAGE_SMTP",
.ext_def = &testsuite_extension,
.code = TESTSUITE_OPERATION_TEST_MESSAGE_SMTP,
.dump = cmd_test_message_smtp_operation_dump,
.execute = cmd_test_message_smtp_operation_execute,
};
/* Test_message_mailbox operation */
static bool
cmd_test_message_mailbox_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address);
static int
cmd_test_message_mailbox_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address);
const struct sieve_operation_def test_message_mailbox_operation = {
.mnemonic = "TEST_MESSAGE_MAILBOX",
.ext_def = &testsuite_extension,
.code = TESTSUITE_OPERATION_TEST_MESSAGE_MAILBOX,
.dump = cmd_test_message_mailbox_operation_dump,
.execute = cmd_test_message_mailbox_operation_execute,
};
/* Test_message_print operation */
static bool
cmd_test_message_print_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address);
static int
cmd_test_message_print_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address);
const struct sieve_operation_def test_message_print_operation = {
.mnemonic = "TEST_MESSAGE_PRINT",
.ext_def = &testsuite_extension,
.code = TESTSUITE_OPERATION_TEST_MESSAGE_PRINT,
.dump = cmd_test_message_print_operation_dump,
.execute = cmd_test_message_print_operation_execute,
};
/*
* Compiler context data
*/
enum test_message_source {
MSG_SOURCE_SMTP,
MSG_SOURCE_MAILBOX,
MSG_SOURCE_LAST,
};
const struct sieve_operation_def *test_message_operations[] = {
&test_message_smtp_operation,
&test_message_mailbox_operation,
};
struct cmd_test_message_context_data {
enum test_message_source msg_source;
const char *folder;
};
#define CMD_TEST_MESSAGE_ERROR_DUP_TAG \
"exactly one of the ':smtp' or ':folder' tags must be specified " \
"for the test_message command, but more were found"
/*
* Command tags
*/
static bool
cmd_test_message_validate_smtp_tag(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static bool
cmd_test_message_validate_folder_tag(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static const struct sieve_argument_def test_message_smtp_tag = {
.identifier = "smtp",
.validate = cmd_test_message_validate_smtp_tag,
};
static const struct sieve_argument_def test_message_folder_tag = {
.identifier = "folder",
.validate = cmd_test_message_validate_folder_tag,
};
static bool
cmd_test_message_registered(struct sieve_validator *valdtr,
const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg)
{
/* Register our tags */
sieve_validator_register_tag(valdtr, cmd_reg, ext,
&test_message_folder_tag, 0);
sieve_validator_register_tag(valdtr, cmd_reg, ext,
&test_message_smtp_tag, 0);
return TRUE;
}
static struct cmd_test_message_context_data *
cmd_test_message_validate_tag(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd)
{
struct cmd_test_message_context_data *ctx_data =
(struct cmd_test_message_context_data *)cmd->data;
if (ctx_data != NULL) {
sieve_argument_validate_error(valdtr, *arg,
CMD_TEST_MESSAGE_ERROR_DUP_TAG);
return NULL;
}
ctx_data = p_new(sieve_command_pool(cmd),
struct cmd_test_message_context_data, 1);
cmd->data = ctx_data;
/* Delete this tag */
*arg = sieve_ast_arguments_detach(*arg, 1);
return ctx_data;
}
static bool
cmd_test_message_validate_smtp_tag(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd)
{
struct cmd_test_message_context_data *ctx_data =
cmd_test_message_validate_tag(valdtr, arg, cmd);
/* Return value is NULL on error */
if (ctx_data == NULL)
return FALSE;
/* Assign chosen message source */
ctx_data->msg_source = MSG_SOURCE_SMTP;
return TRUE;
}
static bool
cmd_test_message_validate_folder_tag(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd)
{
struct sieve_ast_argument *tag = *arg;
struct cmd_test_message_context_data *ctx_data =
cmd_test_message_validate_tag(valdtr, arg, cmd);
/* Return value is NULL on error */
if (ctx_data == NULL)
return FALSE;
/* Assign chose message source */
ctx_data->msg_source = MSG_SOURCE_MAILBOX;
/* Check syntax:
* :folder string
*/
if (!sieve_validate_tag_parameter(valdtr, cmd, tag, *arg, NULL, 0,
SAAT_STRING, FALSE)) {
return FALSE;
}
/* Check name validity when folder argument is not a variable */
if ( sieve_argument_is_string_literal(*arg) ) {
const char *folder = sieve_ast_argument_strc(*arg), *error;
if ( !sieve_mailbox_check_name(folder, &error) ) {
sieve_command_validate_error(
valdtr, cmd, "%s command: "
"invalid mailbox `%s' specified: %s",
sieve_command_identifier(cmd),
str_sanitize(folder, 256), error);
return FALSE;
}
}
/* Skip parameter */
*arg = sieve_ast_argument_next(*arg);
return TRUE;
}
/*
* Validation
*/
static bool
cmd_test_message_validate(struct sieve_validator *valdtr,
struct sieve_command *cmd)
{
struct sieve_ast_argument *arg = cmd->first_positional;
if (cmd->data == NULL) {
sieve_command_validate_error(
valdtr, cmd,
"the test_message command requires either "
"the :smtp or the :mailbox tag to be specified");
return FALSE;
}
if (!sieve_validate_positional_argument(valdtr, cmd, arg, "index", 1,
SAAT_NUMBER))
return FALSE;
return sieve_validator_argument_activate(valdtr, cmd, arg, FALSE);
}
/*
* Code generation
*/
static bool
cmd_test_message_generate(const struct sieve_codegen_env *cgenv,
struct sieve_command *cmd)
{
struct cmd_test_message_context_data *ctx_data =
(struct cmd_test_message_context_data *)cmd->data;
i_assert(ctx_data->msg_source < MSG_SOURCE_LAST);
/* Emit operation */
sieve_operation_emit(cgenv->sblock, cmd->ext,
test_message_operations[ctx_data->msg_source]);
/* Emit is_test flag */
sieve_binary_emit_byte(cgenv->sblock,
(cmd->ast_node->type == SAT_TEST ? 1 : 0));
/* Generate arguments */
if (!sieve_generate_arguments(cgenv, cmd, NULL))
return FALSE;
return TRUE;
}
static bool
cmd_test_message_print_generate(const struct sieve_codegen_env *cgenv,
struct sieve_command *cmd)
{
/* Emit operation */
sieve_operation_emit(cgenv->sblock, cmd->ext,
&test_message_print_operation);
return TRUE;
}
/*
* Code dump
*/
static bool
cmd_test_message_smtp_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address)
{
unsigned int is_test;
if (!sieve_binary_read_byte(denv->sblock, address, &is_test))
return FALSE;
sieve_code_dumpf(denv, "TEST_MESSAGE_SMTP (%s):",
(is_test > 0 ? "TEST" : "COMMAND"));
sieve_code_descend(denv);
return sieve_opr_number_dump(denv, address, "index");
}
static bool
cmd_test_message_mailbox_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address)
{
unsigned int is_test;
if (!sieve_binary_read_byte(denv->sblock, address, &is_test))
return FALSE;
sieve_code_dumpf(denv, "TEST_MESSAGE_MAILBOX (%s):",
(is_test > 0 ? "TEST" : "COMMAND"));
sieve_code_descend(denv);
return (sieve_opr_string_dump(denv, address, "folder") &&
sieve_opr_number_dump(denv, address, "index"));
}
static bool
cmd_test_message_print_operation_dump(const struct sieve_dumptime_env *denv,
sieve_size_t *address ATTR_UNUSED)
{
sieve_code_dumpf(denv, "TEST_MESSAGE_PRINT");
return TRUE;
}
/*
* Intepretation
*/
static int
cmd_test_message_smtp_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address)
{
sieve_number_t msg_index;
unsigned int is_test = 0;
bool result;
int ret;
/*
* Read operands
*/
/* Is test */
if (!sieve_binary_read_byte(renv->sblock, address, &is_test)) {
sieve_runtime_trace_error(renv, "invalid is_test flag");
return SIEVE_EXEC_BIN_CORRUPT;
}
/* Index */
ret = sieve_opr_number_read(renv, address, "index", &msg_index);
if (ret <= 0)
return ret;
/*
* Perform operation
*/
if (is_test > 0) {
if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_TESTS)) {
sieve_runtime_trace(
renv, 0, "testsuite: test_message test");
sieve_runtime_trace_descend(renv);
sieve_runtime_trace(
renv, 0,
"check and retrieve smtp message [index=%llu]",
(unsigned long long)msg_index);
}
} else {
if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS)) {
sieve_runtime_trace(
renv, 0, "testsuite: test_message command");
sieve_runtime_trace_descend(renv);
sieve_runtime_trace(
renv, 0,
"retrieve smtp message [index=%llu]",
(unsigned long long)msg_index);
}
}
result = testsuite_smtp_get(renv, msg_index);
if (is_test > 0) {
sieve_interpreter_set_test_result(renv->interp, result);
return SIEVE_EXEC_OK;
}
if (!result) {
return testsuite_test_failf(
renv, "no outgoing SMTP message with index %llu",
(unsigned long long)msg_index);
}
return SIEVE_EXEC_OK;
}
static int
cmd_test_message_mailbox_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address)
{
string_t *folder;
sieve_number_t msg_index;
unsigned int is_test = 0;
bool result;
const char *error;
int ret;
/*
* Read operands
*/
/* Is test */
if (!sieve_binary_read_byte(renv->sblock, address, &is_test)) {
sieve_runtime_trace_error(renv, "invalid is_test flag");
return SIEVE_EXEC_BIN_CORRUPT;
}
/* Folder */
ret = sieve_opr_string_read(renv, address, "folder", &folder);
if (ret <= 0)
return ret;
/* Index */
ret = sieve_opr_number_read(renv, address, "index", &msg_index);
if (ret <= 0)
return ret;
if (!sieve_mailbox_check_name(str_c(folder), &error)) {
return testsuite_test_failf(
renv, "invalid mailbox `%s' specified: %s",
str_c(folder), error);
}
/*
* Perform operation
*/
if (is_test > 0) {
if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_TESTS)) {
sieve_runtime_trace(
renv, 0, "testsuite: test_message test");
sieve_runtime_trace_descend(renv);
sieve_runtime_trace(
renv, 0, "check and retrieve mailbox message "
"[mailbox=`%s' index=%llu]",
str_c(folder), (unsigned long long)msg_index);
}
} else {
if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS)) {
sieve_runtime_trace(
renv, 0, "testsuite: test_message command");
sieve_runtime_trace_descend(renv);
sieve_runtime_trace(
renv, 0, "retrieve mailbox message "
"[mailbox=`%s' index=%llu]",
str_c(folder), (unsigned long long)msg_index);
}
}
result = testsuite_mailstore_mail_index(renv, str_c(folder), msg_index);
if (is_test > 0) {
sieve_interpreter_set_test_result(renv->interp, result);
return SIEVE_EXEC_OK;
}
if (!result) {
return testsuite_test_failf(
renv, "no message in folder '%s' with index %llu",
str_c(folder), (unsigned long long)msg_index);
}
return SIEVE_EXEC_OK;
}
static int
cmd_test_message_print_operation_execute(const struct sieve_runtime_env *renv,
sieve_size_t *address ATTR_UNUSED)
{
struct mail *mail = sieve_message_get_mail(renv->msgctx);
struct istream *input;
const unsigned char *data;
size_t size;
if (mail_get_stream(mail, NULL, NULL, &input) < 0) {
sieve_runtime_error(renv, NULL, "test_message_print: "
"failed to read current message");
return SIEVE_EXEC_OK;
}
printf("\n--MESSAGE: \n");
/* Pipe the message to the outgoing SMTP transport */
while (i_stream_read_more(input, &data, &size) > 0) {
ssize_t wret;
wret = write(1, data, size);
if (wret <= 0)
break;
i_stream_skip(input, wret);
}
printf("\n--MESSAGE--\n");
return SIEVE_EXEC_OK;
}
|