1 /* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
2  */
3 
4 /* Extension vnd.dovecot.pipe
5  * -----------------------------
6  *
7  * Authors: Stephan Bosch
8  * Specification: vendor-define; spec-bosch-sieve-extprograms
9  * Implementation: full
10  * Status: experimental
11  *
12  */
13 
14 #include "lib.h"
15 
16 #include "sieve-extensions.h"
17 #include "sieve-commands.h"
18 #include "sieve-binary.h"
19 
20 #include "sieve-validator.h"
21 #include "sieve-interpreter.h"
22 
23 #include "sieve-ext-copy.h"
24 
25 #include "sieve-extprograms-common.h"
26 
27 /*
28  * Extension
29  */
30 
31 static bool ext_pipe_load(const struct sieve_extension *ext, void **context);
32 static void ext_pipe_unload(const struct sieve_extension *ext);
33 static bool ext_pipe_validator_load
34 	(const struct sieve_extension *ext, struct sieve_validator *valdtr);
35 
36 const struct sieve_extension_def sieve_ext_vnd_pipe = {
37 	.name = "vnd.dovecot.pipe",
38 	.load = ext_pipe_load,
39 	.unload = ext_pipe_unload,
40 	.validator_load = ext_pipe_validator_load,
41 	SIEVE_EXT_DEFINE_OPERATION(sieve_opr_pipe),
42 };
43 
44 /*
45  * Context
46  */
47 
ext_pipe_load(const struct sieve_extension * ext,void ** context)48 static bool ext_pipe_load(const struct sieve_extension *ext, void **context)
49 {
50 	if ( *context != NULL ) {
51 		ext_pipe_unload(ext);
52 		*context = NULL;
53 	}
54 
55 	*context = (void *)sieve_extprograms_config_init(ext);
56 	return TRUE;
57 }
58 
ext_pipe_unload(const struct sieve_extension * ext)59 static void ext_pipe_unload(const struct sieve_extension *ext)
60 {
61 	struct sieve_extprograms_config *ext_config =
62 		(struct sieve_extprograms_config *)ext->context;
63 
64 	if ( ext_config == NULL ) return;
65 
66 	sieve_extprograms_config_deinit(&ext_config);
67 }
68 
69 /*
70  * Validation
71  */
72 
73 static bool ext_pipe_validator_validate
74 	(const struct sieve_extension *ext,
75 		struct sieve_validator *valdtr, void *context,
76 		struct sieve_ast_argument *require_arg,
77 		bool required);
78 
79 static const struct sieve_validator_extension pipe_validator_extension = {
80 	.ext = &sieve_ext_vnd_pipe,
81 	.validate = ext_pipe_validator_validate
82 };
83 
ext_pipe_validator_load(const struct sieve_extension * ext,struct sieve_validator * valdtr)84 static bool ext_pipe_validator_load
85 (const struct sieve_extension *ext, struct sieve_validator *valdtr)
86 {
87 	/* Register commands */
88 	sieve_validator_register_command(valdtr, ext, &sieve_cmd_pipe);
89 
90 	/* Register extension to validator */
91 	sieve_validator_extension_register
92 		(valdtr, ext, &pipe_validator_extension, NULL);
93 
94 	return TRUE;
95 }
96 
ext_pipe_validator_validate(const struct sieve_extension * ext,struct sieve_validator * valdtr,void * context ATTR_UNUSED,struct sieve_ast_argument * require_arg ATTR_UNUSED,bool required ATTR_UNUSED)97 static bool ext_pipe_validator_validate
98 (const struct sieve_extension *ext,
99 	struct sieve_validator *valdtr, void *context ATTR_UNUSED,
100 	struct sieve_ast_argument *require_arg ATTR_UNUSED,
101 	bool required ATTR_UNUSED)
102 {
103 	struct sieve_extprograms_config *ext_config =
104 		(struct sieve_extprograms_config *) ext->context;
105 
106 	if ( ext_config != NULL && ext_config->copy_ext != NULL ) {
107 		/* Register :copy command tag */
108 		sieve_ext_copy_register_tag(valdtr,
109 			ext_config->copy_ext, sieve_cmd_pipe.identifier);
110 	}
111 	return TRUE;
112 }
113 
114 
115