File: cmd-vacation.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 (1257 lines) | stat: -rw-r--r-- 32,486 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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"
#include "strfuncs.h"
#include "md5.h"
#include "hostpid.h"
#include "str-sanitize.h"
#include "ostream.h"
#include "message-address.h"
#include "message-date.h"
#include "ioloop.h"

#include "rfc2822.h"

#include "sieve-common.h"
#include "sieve-stringlist.h"
#include "sieve-code.h"
#include "sieve-address.h"
#include "sieve-extensions.h"
#include "sieve-commands.h"
#include "sieve-actions.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "sieve-result.h"
#include "sieve-message.h"
#include "sieve-smtp.h"

#include "ext-vacation-common.h"

#include <stdio.h>

/*
 * Forward declarations
 */

static const struct sieve_argument_def vacation_days_tag;
static const struct sieve_argument_def vacation_subject_tag;
static const struct sieve_argument_def vacation_from_tag;
static const struct sieve_argument_def vacation_addresses_tag;
static const struct sieve_argument_def vacation_mime_tag;
static const struct sieve_argument_def vacation_handle_tag;

/*
 * Vacation command
 *
 * Syntax:
 *    vacation [":days" number] [":subject" string]
 *                 [":from" string] [":addresses" string-list]
 *                 [":mime"] [":handle" string] <reason: string>
 */

static bool cmd_vacation_registered
	(struct sieve_validator *valdtr, const struct sieve_extension *ext,
		struct sieve_command_registration *cmd_reg);
static bool cmd_vacation_pre_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_vacation_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_vacation_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd);

const struct sieve_command_def vacation_command = {
	"vacation",
	SCT_COMMAND,
	1, 0, FALSE, FALSE,
	cmd_vacation_registered,
	cmd_vacation_pre_validate,
	cmd_vacation_validate,
	NULL,
	cmd_vacation_generate,
	NULL
};

/*
 * Vacation command tags
 */

/* Forward declarations */

static bool cmd_vacation_validate_number_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);
static bool cmd_vacation_validate_string_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);
static bool cmd_vacation_validate_stringlist_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);
static bool cmd_vacation_validate_mime_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);

/* Argument objects */

static const struct sieve_argument_def vacation_days_tag = {
	"days",
	NULL,
	cmd_vacation_validate_number_tag,
	NULL, NULL, NULL,
};

static const struct sieve_argument_def vacation_seconds_tag = {
	"seconds",
	NULL,
	cmd_vacation_validate_number_tag,
	NULL, NULL, NULL,
};

static const struct sieve_argument_def vacation_subject_tag = {
	"subject",
	NULL,
	cmd_vacation_validate_string_tag,
	NULL, NULL, NULL
};

static const struct sieve_argument_def vacation_from_tag = {
	"from",
	NULL,
	cmd_vacation_validate_string_tag,
	NULL, NULL, NULL
};

static const struct sieve_argument_def vacation_addresses_tag = {
	"addresses",
	NULL,
	cmd_vacation_validate_stringlist_tag,
	NULL, NULL, NULL
};

static const struct sieve_argument_def vacation_mime_tag = {
	"mime",
	NULL,
	cmd_vacation_validate_mime_tag,
	NULL, NULL, NULL
};

static const struct sieve_argument_def vacation_handle_tag = {
	"handle",
	NULL,
	cmd_vacation_validate_string_tag,
	NULL, NULL, NULL
};

/* Codes for optional arguments */

enum cmd_vacation_optional {
	OPT_END,
	OPT_SECONDS,
	OPT_SUBJECT,
	OPT_FROM,
	OPT_ADDRESSES,
	OPT_MIME
};

/*
 * Vacation operation
 */

static bool ext_vacation_operation_dump
	(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int ext_vacation_operation_execute
	(const struct sieve_runtime_env *renv, sieve_size_t *address);

const struct sieve_operation_def vacation_operation = {
	"VACATION",
	&vacation_extension,
	0,
	ext_vacation_operation_dump,
	ext_vacation_operation_execute
};

/*
 * Vacation action
 */

/* Forward declarations */

static int act_vacation_check_duplicate
	(const struct sieve_runtime_env *renv,
		const struct sieve_action *act,
		const struct sieve_action *act_other);
int act_vacation_check_conflict
	(const struct sieve_runtime_env *renv,
		const struct sieve_action *act,
		const struct sieve_action *act_other);
static void act_vacation_print
	(const struct sieve_action *action,
		const struct sieve_result_print_env *rpenv, bool *keep);
static int act_vacation_commit
	(const struct sieve_action *action,	const struct sieve_action_exec_env *aenv,
		void *tr_context, bool *keep);

/* Action object */

const struct sieve_action_def act_vacation = {
	"vacation",
	SIEVE_ACTFLAG_SENDS_RESPONSE,
	NULL,
	act_vacation_check_duplicate,
	act_vacation_check_conflict,
	act_vacation_print,
	NULL, NULL,
	act_vacation_commit,
	NULL
};

/* Action context information */

struct act_vacation_context {
	const char *reason;

	sieve_number_t seconds;
	const char *subject;
	const char *handle;
	bool mime;
	const char *from;
	const char *from_normalized;
	const char *const *addresses;
};

/*
 * Command validation context
 */

struct cmd_vacation_context_data {
	string_t *from;
	string_t *subject;

	bool mime;

	struct sieve_ast_argument *handle_arg;
};

/*
 * Tag validation
 */

static bool cmd_vacation_validate_number_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	const struct sieve_extension *ext = sieve_argument_ext(*arg);
	const struct ext_vacation_config *config =
		(const struct ext_vacation_config *) ext->context;
	struct sieve_ast_argument *tag = *arg;
	sieve_number_t period, seconds;

	/* Detach the tag itself */
	*arg = sieve_ast_arguments_detach(*arg,1);

	/* Check syntax:
	 *   :days number
	 */
	if ( !sieve_validate_tag_parameter
		(valdtr, cmd, tag, *arg, NULL, 0, SAAT_NUMBER, FALSE) ) {
		return FALSE;
	}

	period = sieve_ast_argument_number(*arg);
	if ( sieve_argument_is(tag, vacation_days_tag) ) {
		seconds = period * (24*60*60);

	} else if ( sieve_argument_is(tag, vacation_seconds_tag) ) {
		seconds = period;

	} else {
		i_unreached();
	}

	/* Enforce :seconds >= min_period */
	if ( seconds < config->min_period ) {
		seconds = config->min_period;

		sieve_argument_validate_warning(valdtr, *arg,
			"specified :%s value '%lu' is under the minimum",
			sieve_argument_identifier(tag), (unsigned long) period);

	/* Enforce :days <= max_period */
	} else if ( config->max_period > 0 && seconds > config->max_period ) {
		seconds = config->max_period;

		sieve_argument_validate_warning(valdtr, *arg,
			"specified :%s value '%lu' is over the maximum",
			sieve_argument_identifier(tag), (unsigned long) period);
	}

	sieve_ast_argument_number_set(*arg, seconds);

	/* Skip parameter */
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

static bool cmd_vacation_validate_string_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *tag = *arg;
	struct cmd_vacation_context_data *ctx_data =
		(struct cmd_vacation_context_data *) cmd->data;

	/* Detach the tag itself */
	*arg = sieve_ast_arguments_detach(*arg,1);

	/* Check syntax:
	 *   :subject string
	 *   :from string
	 *   :handle string
	 */
	if ( !sieve_validate_tag_parameter
		(valdtr, cmd, tag, *arg, NULL, 0, SAAT_STRING, FALSE) ) {
		return FALSE;
	}

	if ( sieve_argument_is(tag, vacation_from_tag) ) {
		if ( sieve_argument_is_string_literal(*arg) ) {
			string_t *address = sieve_ast_argument_str(*arg);
			const char *error;
	 		bool result;

	 		T_BEGIN {
	 			result = sieve_address_validate(address, &error);

				if ( !result ) {
					sieve_argument_validate_error(valdtr, *arg,
						"specified :from address '%s' is invalid for vacation action: %s",
						str_sanitize(str_c(address), 128), error);
				}
			} T_END;

			if ( !result )
				return FALSE;
		}

		ctx_data->from = sieve_ast_argument_str(*arg);

		/* Skip parameter */
		*arg = sieve_ast_argument_next(*arg);

	} else if ( sieve_argument_is(tag, vacation_subject_tag) ) {
		ctx_data->subject = sieve_ast_argument_str(*arg);

		/* Skip parameter */
		*arg = sieve_ast_argument_next(*arg);

	} else if ( sieve_argument_is(tag, vacation_handle_tag) ) {
		ctx_data->handle_arg = *arg;

		/* Detach optional argument (emitted as mandatory) */
		*arg = sieve_ast_arguments_detach(*arg, 1);
	}

	return TRUE;
}

static bool cmd_vacation_validate_stringlist_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *tag = *arg;

	/* Detach the tag itself */
	*arg = sieve_ast_arguments_detach(*arg,1);

	/* Check syntax:
	 *   :addresses string-list
	 */
	if ( !sieve_validate_tag_parameter
		(valdtr, cmd, tag, *arg, NULL, 0, SAAT_STRING_LIST, FALSE) ) {
		return FALSE;
	}

	/* Skip parameter */
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

static bool cmd_vacation_validate_mime_tag
(struct sieve_validator *valdtr ATTR_UNUSED, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct cmd_vacation_context_data *ctx_data =
		(struct cmd_vacation_context_data *) cmd->data;

	ctx_data->mime = TRUE;

	/* Skip tag */
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

/*
 * Command registration
 */

static bool cmd_vacation_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
	struct sieve_command_registration *cmd_reg)
{
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_days_tag, OPT_SECONDS);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_subject_tag, OPT_SUBJECT);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_from_tag, OPT_FROM);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_addresses_tag, OPT_ADDRESSES);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_mime_tag, OPT_MIME);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &vacation_handle_tag, 0);

	return TRUE;
}

bool ext_vacation_register_seconds_tag
(struct sieve_validator *valdtr, const struct sieve_extension *vacation_ext)
{
	sieve_validator_register_external_tag
		(valdtr, vacation_command.identifier, vacation_ext, &vacation_seconds_tag,
			OPT_SECONDS);

	return TRUE;
}

/*
 * Command validation
 */

static bool cmd_vacation_pre_validate
(struct sieve_validator *valdtr ATTR_UNUSED,
	struct sieve_command *cmd)
{
	struct cmd_vacation_context_data *ctx_data;

	/* Assign context */
	ctx_data = p_new(sieve_command_pool(cmd),
		struct cmd_vacation_context_data, 1);
	cmd->data = ctx_data;

	return TRUE;
}

static const char _handle_empty_subject[] = "<default-subject>";
static const char _handle_empty_from[] = "<default-from>";
static const char _handle_mime_enabled[] = "<MIME>";
static const char _handle_mime_disabled[] = "<NO-MIME>";

static bool cmd_vacation_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
	struct sieve_ast_argument *arg = cmd->first_positional;
	struct cmd_vacation_context_data *ctx_data =
		(struct cmd_vacation_context_data *) cmd->data;

	if ( !sieve_validate_positional_argument
		(valdtr, cmd, arg, "reason", 1, SAAT_STRING) ) {
		return FALSE;
	}

	if ( !sieve_validator_argument_activate(valdtr, cmd, arg, FALSE) )
		return FALSE;

	/* Construct handle if not set explicitly */
	if ( ctx_data->handle_arg == NULL ) {
		T_BEGIN {
			string_t *handle;
			string_t *reason = sieve_ast_argument_str(arg);
			unsigned int size = str_len(reason);

			/* Precalculate the size of it all */
			size += ctx_data->subject == NULL ?
				sizeof(_handle_empty_subject) - 1 : str_len(ctx_data->subject);
			size += ctx_data->from == NULL ?
				sizeof(_handle_empty_from) - 1 : str_len(ctx_data->from);
			size += ctx_data->mime ?
				sizeof(_handle_mime_enabled) - 1 : sizeof(_handle_mime_disabled) - 1;

			/* Construct the string */
			handle = t_str_new(size);
			str_append_str(handle, reason);

			if ( ctx_data->subject != NULL )
				str_append_str(handle, ctx_data->subject);
			else
				str_append(handle, _handle_empty_subject);

			if ( ctx_data->from != NULL )
				str_append_str(handle, ctx_data->from);
			else
				str_append(handle, _handle_empty_from);

			str_append(handle,
				ctx_data->mime ? _handle_mime_enabled : _handle_mime_disabled );

			/* Create positional handle argument */
			ctx_data->handle_arg = sieve_ast_argument_string_create
				(cmd->ast_node, handle, sieve_ast_node_line(cmd->ast_node));
		} T_END;

		if ( !sieve_validator_argument_activate
			(valdtr, cmd, ctx_data->handle_arg, TRUE) )
			return FALSE;
	} else {
		/* Attach explicit handle argument as positional */
		(void)sieve_ast_argument_attach(cmd->ast_node, ctx_data->handle_arg);
	}

	return TRUE;
}

/*
 * Code generation
 */

static bool cmd_vacation_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
	sieve_operation_emit(cgenv->sblock, cmd->ext, &vacation_operation);

	/* Generate arguments */
	if ( !sieve_generate_arguments(cgenv, cmd, NULL) )
		return FALSE;

	return TRUE;
}

/*
 * Code dump
 */

static bool ext_vacation_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	int opt_code = 0;

	sieve_code_dumpf(denv, "VACATION");
	sieve_code_descend(denv);

	/* Dump optional operands */

	for (;;) {
		int opt;
		bool opok = TRUE;

		if ( (opt=sieve_opr_optional_dump(denv, address, &opt_code)) < 0 )
			return FALSE;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_SECONDS:
			opok = sieve_opr_number_dump(denv, address, "seconds");
			break;
		case OPT_SUBJECT:
			opok = sieve_opr_string_dump(denv, address, "subject");
			break;
		case OPT_FROM:
			opok = sieve_opr_string_dump(denv, address, "from");
			break;
		case OPT_ADDRESSES:
			opok = sieve_opr_stringlist_dump(denv, address, "addresses");
			break;
		case OPT_MIME:
			sieve_code_dumpf(denv, "mime");
			break;
		default:
			return FALSE;
		}

		if ( !opok ) return FALSE;
	}

	/* Dump reason and handle operands */
	return
		sieve_opr_string_dump(denv, address, "reason") &&
		sieve_opr_string_dump(denv, address, "handle");
}

/*
 * Code execution
 */

static int ext_vacation_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address)
{
	const struct sieve_extension *this_ext = renv->oprtn->ext;
	const struct ext_vacation_config *config =
		(const struct ext_vacation_config *) this_ext->context;
	struct sieve_side_effects_list *slist = NULL;
	struct act_vacation_context *act;
	pool_t pool;
	int opt_code = 0;
	sieve_number_t seconds = config->default_period;
	bool mime = FALSE;
	struct sieve_stringlist *addresses = NULL;
	string_t *reason, *subject = NULL, *from = NULL, *handle = NULL;
	const char *from_normalized = NULL;
	int ret;

	/*
	 * Read code
	 */

	/* Optional operands */

	for (;;) {
		int opt;

		if ( (opt=sieve_opr_optional_read(renv, address, &opt_code)) < 0 )
			return SIEVE_EXEC_BIN_CORRUPT;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_SECONDS:
			ret = sieve_opr_number_read(renv, address, "seconds", &seconds);
			break;
		case OPT_SUBJECT:
			ret = sieve_opr_string_read(renv, address, "subject", &subject);
			break;
		case OPT_FROM:
			ret = sieve_opr_string_read(renv, address, "from", &from);
			break;
		case OPT_ADDRESSES:
			ret = sieve_opr_stringlist_read(renv, address, "addresses", &addresses);
			break;
		case OPT_MIME:
			mime = TRUE;
			ret = SIEVE_EXEC_OK;
			break;
		default:
			sieve_runtime_trace_error(renv, "unknown optional operand");
			ret = SIEVE_EXEC_BIN_CORRUPT;
		}

		if ( ret <= 0 ) return ret;
	}

	/* Fixed operands */

	if ( (ret=sieve_opr_string_read(renv, address, "reason", &reason)) <= 0 ||
		(ret=sieve_opr_string_read(renv, address, "handle", &handle)) <= 0 ) {
		return ret;
	}

	/*
	 * Perform operation
	 */

	/* Trace */

	if ( sieve_runtime_trace_active(renv, SIEVE_TRLVL_ACTIONS) ) {
		sieve_runtime_trace(renv, 0, "vacation action");
		sieve_runtime_trace_descend(renv);
		sieve_runtime_trace(renv, 0, "auto-reply with message `%s'",
			str_sanitize(str_c(reason), 80));
	}

	/* Check and normalize :from address */
	if ( from != NULL ) {
		const char *error;

		from_normalized = sieve_address_normalize(from, &error);

		if ( from_normalized == NULL) {
			sieve_runtime_error(renv, NULL,
				"specified :from address '%s' is invalid for vacation action: %s",
				str_sanitize(str_c(from), 128), error);
   		}
	}

	/* Add vacation action to the result */

	pool = sieve_result_pool(renv->result);
	act = p_new(pool, struct act_vacation_context, 1);
	act->reason = p_strdup(pool, str_c(reason));
	act->handle = p_strdup(pool, str_c(handle));
	act->seconds = seconds;
	act->mime = mime;
	if ( subject != NULL )
		act->subject = p_strdup(pool, str_c(subject));
	if ( from != NULL ) {
		act->from = p_strdup(pool, str_c(from));
		act->from_normalized = p_strdup(pool, from_normalized);
	}

	/* Normalize all addresses */
	if ( addresses != NULL ) {
		ARRAY(const char *) norm_addresses;
		string_t *raw_address;
		int ret;

		sieve_stringlist_reset(addresses);

		p_array_init(&norm_addresses, pool, 4);

		raw_address = NULL;
		while ( (ret=sieve_stringlist_next_item(addresses, &raw_address)) > 0 ) {
			const char *error;
			const char *addr_norm = sieve_address_normalize(raw_address, &error);

			if ( addr_norm != NULL ) {
				addr_norm = p_strdup(pool, addr_norm);

				array_append(&norm_addresses, &addr_norm, 1);
			} else {
				sieve_runtime_error(renv, NULL,
					"specified :addresses item '%s' is invalid: %s for vacation action "
					"(ignored)",
					str_sanitize(str_c(raw_address),128), error);
			}
		}

		if ( ret < 0 ) {
			sieve_runtime_trace_error(renv, "invalid addresses stringlist");
			return SIEVE_EXEC_BIN_CORRUPT;
		}

		(void)array_append_space(&norm_addresses);
		act->addresses = array_idx(&norm_addresses, 0);
	}

	if ( sieve_result_add_action
		(renv, this_ext, &act_vacation, slist, (void *) act, 0, FALSE) < 0 )
		return SIEVE_EXEC_FAILURE;

	return SIEVE_EXEC_OK;
}

/*
 * Action
 */

/* Runtime verification */

static int act_vacation_check_duplicate
(const struct sieve_runtime_env *renv ATTR_UNUSED,
	const struct sieve_action *act,
	const struct sieve_action *act_other)
{
	if ( !act_other->executed ) {
		sieve_runtime_error(renv, act->location,
			"duplicate vacation action not allowed "
			"(previously triggered one was here: %s)", act_other->location);
		return -1;
	}

	/* Not an error if executed in preceeding script */
	return 1;
}

int act_vacation_check_conflict
(const struct sieve_runtime_env *renv,
	const struct sieve_action *act,
	const struct sieve_action *act_other)
{
	if ( (act_other->def->flags & SIEVE_ACTFLAG_SENDS_RESPONSE) > 0 ) {
		if ( !act_other->executed && !act->executed) {
			sieve_runtime_error(renv, act->location,
				"vacation action conflicts with other action: "
				"the %s action (%s) also sends a response back to the sender",
				act_other->def->name, act_other->location);
			return -1;
		} else {
			/* Not an error if executed in preceeding script */
			return 1;
		}
	}

	return 0;
}

/* Result printing */

static void act_vacation_print
(const struct sieve_action *action ATTR_UNUSED,
	const struct sieve_result_print_env *rpenv, bool *keep ATTR_UNUSED)
{
	struct act_vacation_context *ctx =
		(struct act_vacation_context *) action->context;

	sieve_result_action_printf( rpenv, "send vacation message:");
	sieve_result_printf(rpenv, "    => seconds : %d\n", ctx->seconds);
	if ( ctx->subject != NULL )
		sieve_result_printf(rpenv, "    => subject : %s\n", ctx->subject);
	if ( ctx->from != NULL )
		sieve_result_printf(rpenv, "    => from    : %s\n", ctx->from);
	if ( ctx->handle != NULL )
		sieve_result_printf(rpenv, "    => handle  : %s\n", ctx->handle);
	sieve_result_printf(rpenv, "\nSTART MESSAGE\n%s\nEND MESSAGE\n", ctx->reason);
}

/* Result execution */

/* Headers known to be associated with mailing lists
 */
static const char * const _list_headers[] = {
	"list-id",
	"list-owner",
	"list-subscribe",
	"list-post",
	"list-unsubscribe",
	"list-help",
	"list-archive",
	NULL
};

/* Headers that should be searched for the user's own mail address(es)
 */

static const char * const _my_address_headers[] = {
	"to",
	"cc",
	"bcc",
	"resent-to",
	"resent-cc",
	"resent-bcc",
	NULL
};

static inline bool _is_system_address(const char *address)
{
	if ( strncasecmp(address, "MAILER-DAEMON", 13) == 0 )
		return TRUE;

	if ( strncasecmp(address, "LISTSERV", 8) == 0 )
		return TRUE;

	if ( strncasecmp(address, "majordomo", 9) == 0 )
		return TRUE;

	if ( strstr(address, "-request@") != NULL )
		return TRUE;

	if ( strncmp(address, "owner-", 6) == 0 )
		return TRUE;

	return FALSE;
}

static inline bool _contains_my_address
	(const char * const *headers, const char *my_address)
{
	const char *const *hdsp = headers;
	bool result = FALSE;

	while ( *hdsp != NULL && !result ) {
		const struct message_address *addr;

		T_BEGIN {

			addr = message_address_parse
				(pool_datastack_create(), (const unsigned char *) *hdsp,
					strlen(*hdsp), 256, FALSE);

			while ( addr != NULL && !result ) {
				if (addr->domain != NULL) {
					const char *hdr_address;

					i_assert(addr->mailbox != NULL);

					hdr_address = t_strconcat(addr->mailbox, "@", addr->domain, NULL);
					if ( sieve_address_compare(hdr_address, my_address, TRUE) == 0 ) {
						result = TRUE;
						break;
					}
				}

				addr = addr->next;
			}
		} T_END;

		hdsp++;
	}

	return result;
}

static bool _contains_8bit(const char *text)
{
	const unsigned char *p = (const unsigned char *) text;

	for (; *p != '\0'; p++) {
		if ((*p & 0x80) != 0)
			return TRUE;
	}
	return FALSE;
}

static bool act_vacation_send
(const struct sieve_action_exec_env *aenv, struct act_vacation_context *ctx,
 	const char *reply_to, const char *reply_from, const char *smtp_from)
{
	const struct sieve_message_data *msgdata = aenv->msgdata;
	const struct sieve_script_env *senv = aenv->scriptenv;
	void *smtp_handle;
	struct ostream *output;
	string_t *msg;
 	const char *const *headers;
	const char *outmsgid, *subject;
	int ret;

	/* Check smpt functions just to be sure */

	if ( !sieve_smtp_available(senv) ) {
		sieve_result_global_warning(aenv,
			"vacation action has no means to send mail");
		return TRUE;
	}

	/* Make sure we have a subject for our reply */

	if ( ctx->subject == NULL || *(ctx->subject) == '\0' ) {
		if ( mail_get_headers_utf8
			(msgdata->mail, "subject", &headers) >= 0 && headers[0] != NULL ) {
			subject = t_strconcat("Auto: ", headers[0], NULL);
		}	else {
			subject = "Automated reply";
		}
	}	else {
		subject = ctx->subject;
	}

	subject = str_sanitize(subject, 256);

	/* Open smtp session */

	smtp_handle = sieve_smtp_open(senv, reply_to, smtp_from, &output);
	outmsgid = sieve_message_get_new_id(aenv->svinst);

	/* Produce a proper reply */

	msg = t_str_new(512);
	rfc2822_header_write(msg, "X-Sieve", SIEVE_IMPLEMENTATION);
	rfc2822_header_write(msg, "Message-ID", outmsgid);
	rfc2822_header_write(msg, "Date", message_date_create(ioloop_time));

	if ( ctx->from != NULL && *(ctx->from) != '\0' )
		rfc2822_header_utf8_printf(msg, "From", "%s", ctx->from);
	else if ( reply_from != NULL )
		rfc2822_header_printf(msg, "From", "<%s>", reply_from);
	else
		rfc2822_header_printf(msg, "From", "Postmaster <%s>", senv->postmaster_address);

	/* FIXME: If From header of message has same address, we should use that
	 * instead to properly include the phrase part.
	 */
	rfc2822_header_printf(msg, "To", "<%s>", reply_to);

	if ( _contains_8bit(subject) )
		rfc2822_header_utf8_printf(msg, "Subject", "%s", subject);
	else
		rfc2822_header_printf(msg, "Subject", "%s", subject);

	/* Compose proper in-reply-to and references headers */

	ret = mail_get_headers
		(aenv->msgdata->mail, "references", &headers);

	if ( msgdata->id != NULL ) {
		rfc2822_header_write(msg, "In-Reply-To", msgdata->id);

		if ( ret >= 0 && headers[0] != NULL )
			rfc2822_header_write
				(msg, "References", t_strconcat(headers[0], " ", msgdata->id, NULL));
		else
			rfc2822_header_write(msg, "References", msgdata->id);
	} else if ( ret >= 0 && headers[0] != NULL ) {
		rfc2822_header_write(msg, "References", headers[0]);
	}

	rfc2822_header_write(msg, "Auto-Submitted", "auto-replied (vacation)");
	rfc2822_header_write(msg, "Precedence", "bulk");

	rfc2822_header_write(msg, "MIME-Version", "1.0");

	if ( !ctx->mime ) {
		rfc2822_header_write(msg, "Content-Type", "text/plain; charset=utf-8");
		rfc2822_header_write(msg, "Content-Transfer-Encoding", "8bit");
		str_append(msg, "\r\n");
	}

	str_printfa(msg, "%s\r\n", ctx->reason);
  o_stream_send(output, str_data(msg), str_len(msg));

	/* Close smtp session */
	if ( !sieve_smtp_close(senv, smtp_handle) ) {
		sieve_result_global_error(aenv,
			"failed to send vacation response to <%s> "
			"(refer to server log for more information)",
			str_sanitize(reply_to, 128));
		return FALSE;
	}

	return TRUE;
}

static void act_vacation_hash
(struct act_vacation_context *vctx, const char *sender, unsigned char hash_r[])
{
	const char *rpath = t_str_lcase(sender);
	struct md5_context ctx;

	md5_init(&ctx);
	md5_update(&ctx, rpath, strlen(rpath));

	md5_update(&ctx, vctx->handle, strlen(vctx->handle));

	md5_final(&ctx, hash_r);
}

static int act_vacation_commit
(const struct sieve_action *action, const struct sieve_action_exec_env *aenv,
	void *tr_context ATTR_UNUSED, bool *keep ATTR_UNUSED)
{
	const struct sieve_extension *ext = action->ext;
	const struct ext_vacation_config *config =
		(const struct ext_vacation_config *) ext->context;
	const struct sieve_message_data *msgdata = aenv->msgdata;
	const struct sieve_script_env *senv = aenv->scriptenv;
	struct act_vacation_context *ctx =
		(struct act_vacation_context *) action->context;
	unsigned char dupl_hash[MD5_RESULTLEN];
	struct mail *mail = sieve_message_get_mail(aenv->msgctx);
	const char *sender = sieve_message_get_sender(aenv->msgctx);
	const char *recipient = sieve_message_get_final_recipient(aenv->msgctx);
	const char *const *hdsp;
	const char *const *headers;
	const char *reply_from = NULL, *orig_recipient = NULL, *smtp_from = NULL;
	bool result;

	/* Is the recipient unset?
	 */
	if ( recipient == NULL ) {
		sieve_result_global_warning
			(aenv, "vacation action aborted: envelope recipient is <>");
		return SIEVE_EXEC_OK;
	}

	/* Is the return path unset ?
	 */
	if ( sender == NULL ) {
		sieve_result_global_log(aenv, "discarded vacation reply to <>");
		return SIEVE_EXEC_OK;
	}

	/* Are we perhaps trying to respond to ourselves ?
	 */
	if ( sieve_address_compare(sender, recipient, TRUE) == 0 ) {
		sieve_result_global_log(aenv,
			"discarded vacation reply to own address <%s>",
			str_sanitize(sender, 128));
		return SIEVE_EXEC_OK;
	}

	/* Are we perhaps trying to respond to one of our alternative :addresses?
	 */
	if ( ctx->addresses != NULL ) {
		const char * const *alt_address = ctx->addresses;

		while ( *alt_address != NULL ) {
			if ( sieve_address_compare(sender, *alt_address, TRUE) == 0 ) {
				sieve_result_global_log(aenv,
					"discarded vacation reply to own address <%s> "
					"(as specified using :addresses argument)",
					str_sanitize(sender, 128));
				return SIEVE_EXEC_OK;
			}
			alt_address++;
		}
	}

	/* Did whe respond to this user before? */
	if ( sieve_action_duplicate_check_available(senv) ) {
		act_vacation_hash(ctx, sender, dupl_hash);

		if ( sieve_action_duplicate_check(senv, dupl_hash, sizeof(dupl_hash)) )
		{
			sieve_result_global_log(aenv,
				"discarded duplicate vacation response to <%s>",
				str_sanitize(sender, 128));
			return SIEVE_EXEC_OK;
		}
	}

	/* Are we trying to respond to a mailing list ? */
	hdsp = _list_headers;
	while ( *hdsp != NULL ) {
		if ( mail_get_headers
			(mail, *hdsp, &headers) >= 0 && headers[0] != NULL ) {
			/* Yes, bail out */
			sieve_result_global_log(aenv,
				"discarding vacation response to mailinglist recipient <%s>",
				str_sanitize(sender, 128));
			return SIEVE_EXEC_OK;
		}
		hdsp++;
	}

	/* Is the message that we are replying to an automatic reply ? */
	if ( mail_get_headers
		(msgdata->mail, "auto-submitted", &headers) >= 0 ) {
		/* Theoretically multiple headers could exist, so lets make sure */
		hdsp = headers;
		while ( *hdsp != NULL ) {
			if ( strcasecmp(*hdsp, "no") != 0 ) {
				sieve_result_global_log(aenv,
					"discarding vacation response to auto-submitted message from <%s>",
 					str_sanitize(sender, 128));
					return SIEVE_EXEC_OK;
			}
			hdsp++;
		}
	}

	/* Check for the (non-standard) precedence header */
	if ( mail_get_headers
		(mail, "precedence", &headers) >= 0 ) {
		/* Theoretically multiple headers could exist, so lets make sure */
		hdsp = headers;
		while ( *hdsp != NULL ) {
			if ( strcasecmp(*hdsp, "junk") == 0 || strcasecmp(*hdsp, "bulk") == 0 ||
				strcasecmp(*hdsp, "list") == 0 ) {
				sieve_result_global_log(aenv,
					"discarding vacation response to precedence=%s message from <%s>",
					*hdsp, str_sanitize(sender, 128));
					return SIEVE_EXEC_OK;
			}
			hdsp++;
		}
	}

	/* Do not reply to system addresses */
	if ( _is_system_address(sender) ) {
		sieve_result_global_log(aenv,
			"not sending vacation response to system address <%s>",
			str_sanitize(sender, 128));
		return SIEVE_EXEC_OK;
	}

	/* Fetch original recipient if necessary */
	if ( config->use_original_recipient  )
		orig_recipient = sieve_message_get_orig_recipient(aenv->msgctx);

	/* Is the original message directly addressed to the user or the addresses
	 * specified using the :addresses tag?
	 */
	hdsp = _my_address_headers;
	while ( *hdsp != NULL ) {
		if ( mail_get_headers
			(mail, *hdsp, &headers) >= 0 && headers[0] != NULL ) {

			/* Final recipient directly listed in headers? */
			if ( _contains_my_address(headers, recipient) ) {
				reply_from = recipient;
				smtp_from = recipient;
				break;
			}

			/* Original recipient directly listed in headers? */
			if ( orig_recipient != NULL &&
				_contains_my_address(headers, orig_recipient) ) {
				reply_from = orig_recipient;
				smtp_from = orig_recipient;
				break;
			}

			/* User-provided :addresses listed in headers? */
			if ( ctx->addresses != NULL ) {
				bool found = FALSE;
				const char * const *my_address = ctx->addresses;

				while ( !found && *my_address != NULL ) {
					if ( (found=_contains_my_address(headers, *my_address)) ) {
						reply_from = *my_address;
						/* Avoid letting user determine SMTP sender directly */
						smtp_from =
							( orig_recipient == NULL ? recipient : orig_recipient );
					}
					my_address++;
				}

				if ( found ) break;
			}
		}
		hdsp++;
	}

	/* My address not found in the headers; we got an implicit delivery */
	if ( *hdsp == NULL ) {
		if ( config->dont_check_recipient ) {
			/* Send reply from envelope recipient address */
			reply_from = recipient;

		} else {
			const char *original_recipient = "";

			/* Bail out */

			if ( config->use_original_recipient ) {
				original_recipient = t_strdup_printf("original-recipient=<%s>, ",
					( orig_recipient == NULL ? "UNAVAILABLE" :
						str_sanitize(orig_recipient, 128) ));
			}

			sieve_result_global_log(aenv,
				"discarding vacation response for implicitly delivered message; "
				"no known (envelope) recipient address found in message headers "
				"(recipient=<%s>, %sand%s additional `:addresses' are specified)",
				str_sanitize(recipient, 128), original_recipient,
				(ctx->addresses == NULL ? " no" : ""));

			return SIEVE_EXEC_OK;
		}
	}

	/* Send the message */

	T_BEGIN {
		result = act_vacation_send(aenv, ctx, sender, reply_from,
			(config->send_from_recipient ? smtp_from : NULL));
	} T_END;

	if ( result ) {
		sieve_number_t seconds;

		sieve_result_global_log(aenv, "sent vacation response to <%s>",
			str_sanitize(sender, 128));

		/* Check period limits once more */
		seconds = ctx->seconds;
		if ( seconds < config->min_period )
			seconds = config->min_period;
		else if ( config->max_period > 0 && seconds > config->max_period )
			seconds = config->max_period;

		/* Mark as replied */
		if ( seconds > 0  ) {
			sieve_action_duplicate_mark
				(senv, dupl_hash, sizeof(dupl_hash), ioloop_time + seconds);
		}
	}

	return SIEVE_EXEC_OK;
}