/src/pigeonhole/src/lib-sieve/plugins/imap4flags/tst-hasflag.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | |
5 | | #include "sieve-commands.h" |
6 | | #include "sieve-stringlist.h" |
7 | | #include "sieve-code.h" |
8 | | #include "sieve-comparators.h" |
9 | | #include "sieve-match-types.h" |
10 | | #include "sieve-validator.h" |
11 | | #include "sieve-generator.h" |
12 | | #include "sieve-interpreter.h" |
13 | | #include "sieve-dump.h" |
14 | | #include "sieve-match.h" |
15 | | |
16 | | #include "ext-imap4flags-common.h" |
17 | | |
18 | | /* |
19 | | * Hasflag test |
20 | | * |
21 | | * Syntax: |
22 | | * hasflag [MATCH-TYPE] [COMPARATOR] [<variable-list: string-list>] |
23 | | * <list-of-flags: string-list> |
24 | | */ |
25 | | |
26 | | static bool |
27 | | tst_hasflag_registered(struct sieve_validator *valdtr, |
28 | | const struct sieve_extension *ext, |
29 | | struct sieve_command_registration *cmd_reg); |
30 | | static bool |
31 | | tst_hasflag_validate(struct sieve_validator *valdtr, struct sieve_command *tst); |
32 | | static bool |
33 | | tst_hasflag_generate(const struct sieve_codegen_env *cgenv, |
34 | | struct sieve_command *cmd); |
35 | | |
36 | | const struct sieve_command_def tst_hasflag = { |
37 | | .identifier = "hasflag", |
38 | | .type = SCT_TEST, |
39 | | .positional_args = -1, /* We check positional arguments ourselves */ |
40 | | .subtests = 0, |
41 | | .block_allowed = FALSE, |
42 | | .block_required = FALSE, |
43 | | .registered = tst_hasflag_registered, |
44 | | .validate = tst_hasflag_validate, |
45 | | .generate = tst_hasflag_generate, |
46 | | }; |
47 | | |
48 | | /* |
49 | | * Hasflag operation |
50 | | */ |
51 | | |
52 | | static bool |
53 | | tst_hasflag_operation_dump(const struct sieve_dumptime_env *denv, |
54 | | sieve_size_t *address); |
55 | | static int |
56 | | tst_hasflag_operation_execute(const struct sieve_runtime_env *renv, |
57 | | sieve_size_t *address); |
58 | | |
59 | | const struct sieve_operation_def hasflag_operation = { |
60 | | .mnemonic = "HASFLAG", |
61 | | .ext_def = &imap4flags_extension, |
62 | | .code = EXT_IMAP4FLAGS_OPERATION_HASFLAG, |
63 | | .dump = tst_hasflag_operation_dump, |
64 | | .execute = tst_hasflag_operation_execute, |
65 | | }; |
66 | | |
67 | | /* |
68 | | * Optional arguments |
69 | | */ |
70 | | |
71 | | enum tst_hasflag_optional { |
72 | | OPT_VARIABLES = SIEVE_MATCH_OPT_LAST, |
73 | | }; |
74 | | |
75 | | /* |
76 | | * Tag registration |
77 | | */ |
78 | | |
79 | | static bool |
80 | | tst_hasflag_registered(struct sieve_validator *valdtr, |
81 | | const struct sieve_extension *ext ATTR_UNUSED, |
82 | | struct sieve_command_registration *cmd_reg) |
83 | 0 | { |
84 | | /* The order of these is not significant */ |
85 | 0 | sieve_comparators_link_tag(valdtr, cmd_reg, |
86 | 0 | SIEVE_MATCH_OPT_COMPARATOR); |
87 | 0 | sieve_match_types_link_tags(valdtr, cmd_reg, |
88 | 0 | SIEVE_MATCH_OPT_MATCH_TYPE); |
89 | |
|
90 | 0 | return TRUE; |
91 | 0 | } |
92 | | |
93 | | /* |
94 | | * Validation |
95 | | */ |
96 | | |
97 | | static bool |
98 | | tst_hasflag_validate(struct sieve_validator *valdtr, |
99 | | struct sieve_command *tst) |
100 | 0 | { |
101 | 0 | const struct sieve_match_type mcht_default = |
102 | 0 | SIEVE_MATCH_TYPE_DEFAULT(is_match_type); |
103 | 0 | const struct sieve_comparator cmp_default = |
104 | 0 | SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator); |
105 | |
|
106 | 0 | if (!ext_imap4flags_command_validate(valdtr, tst)) |
107 | 0 | return FALSE; |
108 | | |
109 | 0 | struct sieve_ast_argument *vars = tst->first_positional; |
110 | 0 | struct sieve_ast_argument *keys = sieve_ast_argument_next(vars); |
111 | |
|
112 | 0 | if (keys == NULL) { |
113 | 0 | keys = vars; |
114 | 0 | vars = NULL; |
115 | 0 | } else { |
116 | 0 | vars->argument->id_code = OPT_VARIABLES; |
117 | 0 | } |
118 | | |
119 | | /* Validate the key argument to a specified match type */ |
120 | 0 | return sieve_match_type_validate(valdtr, tst, keys, |
121 | 0 | &mcht_default, &cmp_default); |
122 | 0 | } |
123 | | |
124 | | /* |
125 | | * Code generation |
126 | | */ |
127 | | |
128 | | static bool |
129 | | tst_hasflag_generate(const struct sieve_codegen_env *cgenv, |
130 | | struct sieve_command *cmd) |
131 | 0 | { |
132 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, &hasflag_operation); |
133 | | |
134 | | /* Generate arguments */ |
135 | 0 | return sieve_generate_arguments(cgenv, cmd, NULL); |
136 | 0 | } |
137 | | |
138 | | /* |
139 | | * Code dump |
140 | | */ |
141 | | |
142 | | static bool |
143 | | tst_hasflag_operation_dump(const struct sieve_dumptime_env *denv, |
144 | | sieve_size_t *address) |
145 | 0 | { |
146 | 0 | int opt_code = 0; |
147 | |
|
148 | 0 | sieve_code_dumpf(denv, "HASFLAG"); |
149 | 0 | sieve_code_descend(denv); |
150 | | |
151 | | /* Optional operands */ |
152 | |
|
153 | 0 | for (;;) { |
154 | 0 | bool opok = TRUE; |
155 | 0 | int opt; |
156 | |
|
157 | 0 | opt = sieve_match_opr_optional_dump(denv, address, &opt_code); |
158 | 0 | if (opt < 0) |
159 | 0 | return FALSE; |
160 | 0 | if (opt == 0) |
161 | 0 | break; |
162 | | |
163 | 0 | switch (opt_code) { |
164 | 0 | case OPT_VARIABLES: |
165 | 0 | opok = sieve_opr_stringlist_dump(denv, address, |
166 | 0 | "variables"); |
167 | 0 | break; |
168 | 0 | default: |
169 | 0 | return FALSE; |
170 | 0 | } |
171 | | |
172 | 0 | if (!opok) |
173 | 0 | return FALSE; |
174 | 0 | } |
175 | | |
176 | 0 | return sieve_opr_stringlist_dump(denv, address, "list of flags"); |
177 | 0 | } |
178 | | |
179 | | /* |
180 | | * Interpretation |
181 | | */ |
182 | | |
183 | | static int |
184 | | tst_hasflag_operation_execute(const struct sieve_runtime_env *renv, |
185 | | sieve_size_t *address) |
186 | 0 | { |
187 | 0 | const struct sieve_operation *op = renv->oprtn; |
188 | 0 | int opt_code = 0; |
189 | 0 | struct sieve_comparator cmp = |
190 | 0 | SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator); |
191 | 0 | struct sieve_match_type mcht = |
192 | 0 | SIEVE_MATCH_TYPE_DEFAULT(is_match_type); |
193 | 0 | struct sieve_stringlist *flag_list, *variables_list, *value_list, |
194 | 0 | *key_list; |
195 | 0 | int match, ret; |
196 | | |
197 | | /* |
198 | | * Read operands |
199 | | */ |
200 | | |
201 | | /* Optional operands */ |
202 | |
|
203 | 0 | variables_list = NULL; |
204 | 0 | for (;;) { |
205 | 0 | int opt; |
206 | |
|
207 | 0 | opt = sieve_match_opr_optional_read(renv, address, &opt_code, |
208 | 0 | &ret, &cmp, &mcht); |
209 | 0 | if (opt < 0) |
210 | 0 | return ret; |
211 | 0 | if (opt == 0) |
212 | 0 | break; |
213 | | |
214 | 0 | switch (opt_code) { |
215 | 0 | case OPT_VARIABLES: |
216 | 0 | ret = sieve_opr_stringlist_read( |
217 | 0 | renv, address, "variables-list", |
218 | 0 | &variables_list); |
219 | 0 | break; |
220 | 0 | default: |
221 | 0 | sieve_runtime_trace_error( |
222 | 0 | renv, "invalid optional operand"); |
223 | 0 | ret = SIEVE_EXEC_BIN_CORRUPT; |
224 | 0 | } |
225 | | |
226 | 0 | if (ret <= 0) |
227 | 0 | return ret; |
228 | 0 | } |
229 | | |
230 | | /* Fixed operands */ |
231 | | |
232 | 0 | ret = sieve_opr_stringlist_read(renv, address, "flag-list", &flag_list); |
233 | 0 | if (ret <= 0) |
234 | 0 | return ret; |
235 | | |
236 | | /* |
237 | | * Perform operation |
238 | | */ |
239 | | |
240 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, "hasflag test"); |
241 | |
|
242 | 0 | value_list = sieve_ext_imap4flags_get_flags(renv, op->ext, |
243 | 0 | variables_list); |
244 | |
|
245 | 0 | if (sieve_match_type_is(&mcht, is_match_type) || |
246 | 0 | sieve_match_type_is(&mcht, contains_match_type)) { |
247 | 0 | key_list = sieve_ext_imap4flags_get_flags( |
248 | 0 | renv, op->ext, flag_list); |
249 | 0 | } else { |
250 | 0 | key_list = flag_list; |
251 | 0 | } |
252 | | |
253 | | /* Perform match */ |
254 | 0 | match = sieve_match(renv, &mcht, &cmp, value_list, key_list, &ret); |
255 | 0 | if (match < 0) |
256 | 0 | return ret; |
257 | | |
258 | | /* Set test result for subsequent conditional jump */ |
259 | 0 | sieve_interpreter_set_test_result(renv->interp, match > 0); |
260 | 0 | return SIEVE_EXEC_OK; |
261 | 0 | } |