/src/pigeonhole/src/lib-sieve/ext-fileinto.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | /* Extension fileinto |
4 | | * ------------------ |
5 | | * |
6 | | * Authors: Stephan Bosch |
7 | | * Specification: RFC 5228 |
8 | | * Implementation: full |
9 | | * Status: testing |
10 | | * |
11 | | */ |
12 | | |
13 | | #include "lib.h" |
14 | | #include "str-sanitize.h" |
15 | | #include "unichar.h" |
16 | | |
17 | | #include "sieve-common.h" |
18 | | #include "sieve-extensions.h" |
19 | | #include "sieve-binary.h" |
20 | | #include "sieve-commands.h" |
21 | | #include "sieve-code.h" |
22 | | #include "sieve-message.h" |
23 | | #include "sieve-actions.h" |
24 | | #include "sieve-validator.h" |
25 | | #include "sieve-generator.h" |
26 | | #include "sieve-interpreter.h" |
27 | | #include "sieve-dump.h" |
28 | | #include "sieve-result.h" |
29 | | |
30 | | /* |
31 | | * Forward declarations |
32 | | */ |
33 | | |
34 | | static const struct sieve_command_def fileinto_command; |
35 | | const struct sieve_operation_def fileinto_operation; |
36 | | const struct sieve_extension_def fileinto_extension; |
37 | | |
38 | | /* |
39 | | * Extension |
40 | | */ |
41 | | |
42 | | static bool |
43 | | ext_fileinto_validator_load(const struct sieve_extension *ext, |
44 | | struct sieve_validator *valdtr); |
45 | | |
46 | | const struct sieve_extension_def fileinto_extension = { |
47 | | .name = "fileinto", |
48 | | .validator_load = ext_fileinto_validator_load, |
49 | | SIEVE_EXT_DEFINE_OPERATION(fileinto_operation), |
50 | | }; |
51 | | |
52 | | static bool |
53 | | ext_fileinto_validator_load(const struct sieve_extension *ext, |
54 | | struct sieve_validator *valdtr) |
55 | 0 | { |
56 | | /* Register new command */ |
57 | 0 | sieve_validator_register_command(valdtr, ext, &fileinto_command); |
58 | |
|
59 | 0 | return TRUE; |
60 | 0 | } |
61 | | |
62 | | /* |
63 | | * Fileinto command |
64 | | * |
65 | | * Syntax: |
66 | | * fileinto <folder: string> |
67 | | */ |
68 | | |
69 | | static bool |
70 | | cmd_fileinto_validate(struct sieve_validator *valdtr, |
71 | | struct sieve_command *cmd); |
72 | | static bool |
73 | | cmd_fileinto_generate(const struct sieve_codegen_env *cgenv, |
74 | | struct sieve_command *ctx); |
75 | | |
76 | | static const struct sieve_command_def fileinto_command = { |
77 | | .identifier = "fileinto", |
78 | | .type = SCT_COMMAND, |
79 | | .positional_args = 1, |
80 | | .subtests = 0, |
81 | | .block_allowed = FALSE, |
82 | | .block_required = FALSE, |
83 | | .validate = cmd_fileinto_validate, |
84 | | .generate = cmd_fileinto_generate, |
85 | | }; |
86 | | |
87 | | /* |
88 | | * Fileinto operation |
89 | | */ |
90 | | |
91 | | static bool |
92 | | ext_fileinto_operation_dump(const struct sieve_dumptime_env *denv, |
93 | | sieve_size_t *address); |
94 | | static int |
95 | | ext_fileinto_operation_execute(const struct sieve_runtime_env *renv, |
96 | | sieve_size_t *address); |
97 | | |
98 | | const struct sieve_operation_def fileinto_operation = { |
99 | | .mnemonic = "FILEINTO", |
100 | | .ext_def = &fileinto_extension, |
101 | | .dump = ext_fileinto_operation_dump, |
102 | | .execute = ext_fileinto_operation_execute, |
103 | | }; |
104 | | |
105 | | /* |
106 | | * Validation |
107 | | */ |
108 | | |
109 | | static bool |
110 | | cmd_fileinto_validate(struct sieve_validator *valdtr, struct sieve_command *cmd) |
111 | 0 | { |
112 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
113 | |
|
114 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "folder", |
115 | 0 | 1, SAAT_STRING)) |
116 | 0 | return FALSE; |
117 | | |
118 | 0 | if (!sieve_validator_argument_activate(valdtr, cmd, arg, FALSE)) |
119 | 0 | return FALSE; |
120 | | |
121 | | /* Check name validity when folder argument is not a variable */ |
122 | 0 | if (sieve_argument_is_string_literal(arg)) { |
123 | 0 | const char *folder = sieve_ast_argument_strc(arg), *error; |
124 | |
|
125 | 0 | if (!sieve_mailbox_check_name(folder, &error)) { |
126 | 0 | sieve_command_validate_error( |
127 | 0 | valdtr, cmd, "fileinto command: " |
128 | 0 | "invalid folder name '%s' specified: %s", |
129 | 0 | str_sanitize(folder, 256), error); |
130 | 0 | return FALSE; |
131 | 0 | } |
132 | 0 | } |
133 | 0 | return TRUE; |
134 | 0 | } |
135 | | |
136 | | /* |
137 | | * Code generation |
138 | | */ |
139 | | |
140 | | static bool |
141 | | cmd_fileinto_generate(const struct sieve_codegen_env *cgenv, |
142 | | struct sieve_command *cmd) |
143 | 0 | { |
144 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, &fileinto_operation); |
145 | | |
146 | | /* Generate arguments */ |
147 | 0 | return sieve_generate_arguments(cgenv, cmd, NULL); |
148 | 0 | } |
149 | | |
150 | | /* |
151 | | * Code dump |
152 | | */ |
153 | | |
154 | | static bool |
155 | | ext_fileinto_operation_dump(const struct sieve_dumptime_env *denv, |
156 | | sieve_size_t *address) |
157 | 0 | { |
158 | 0 | sieve_code_dumpf(denv, "FILEINTO"); |
159 | 0 | sieve_code_descend(denv); |
160 | |
|
161 | 0 | if (sieve_action_opr_optional_dump(denv, address, NULL) != 0) |
162 | 0 | return FALSE; |
163 | | |
164 | 0 | return sieve_opr_string_dump(denv, address, "folder"); |
165 | 0 | } |
166 | | |
167 | | /* |
168 | | * Execution |
169 | | */ |
170 | | |
171 | | static int |
172 | | ext_fileinto_operation_execute(const struct sieve_runtime_env *renv, |
173 | | sieve_size_t *address) |
174 | 0 | { |
175 | 0 | struct sieve_side_effects_list *slist = NULL; |
176 | 0 | string_t *folder; |
177 | 0 | const char *error; |
178 | 0 | bool trace = sieve_runtime_trace_active(renv, SIEVE_TRLVL_ACTIONS); |
179 | 0 | int ret = 0; |
180 | | |
181 | | /* |
182 | | * Read operands |
183 | | */ |
184 | | |
185 | | /* Optional operands (side effects only) */ |
186 | 0 | if (sieve_action_opr_optional_read(renv, address, NULL, |
187 | 0 | &ret, &slist) != 0) |
188 | 0 | return ret; |
189 | | |
190 | | /* Folder operand */ |
191 | 0 | ret = sieve_opr_string_read(renv, address, "folder", &folder); |
192 | 0 | if (ret <= 0) |
193 | 0 | return ret; |
194 | | |
195 | | /* |
196 | | * Perform operation |
197 | | */ |
198 | | |
199 | 0 | if (trace) { |
200 | 0 | sieve_runtime_trace(renv, 0, "fileinto action"); |
201 | 0 | sieve_runtime_trace_descend(renv); |
202 | 0 | } |
203 | |
|
204 | 0 | if (!sieve_mailbox_check_name(str_c(folder), &error)) { |
205 | 0 | sieve_runtime_error( |
206 | 0 | renv, NULL, "fileinto command: " |
207 | 0 | "invalid folder name '%s' specified: %s", |
208 | 0 | str_c(folder), error); |
209 | 0 | return SIEVE_EXEC_FAILURE; |
210 | 0 | } |
211 | | |
212 | 0 | if (trace) { |
213 | 0 | sieve_runtime_trace(renv, 0, "store message in mailbox '%s'", |
214 | 0 | str_sanitize(str_c(folder), 80)); |
215 | 0 | } |
216 | | |
217 | | /* Add action to result */ |
218 | 0 | if (sieve_act_store_add_to_result(renv, "fileinto", slist, |
219 | 0 | str_c(folder)) < 0) |
220 | 0 | return SIEVE_EXEC_FAILURE; |
221 | | |
222 | 0 | sieve_message_snapshot(renv->msgctx); |
223 | 0 | return SIEVE_EXEC_OK; |
224 | 0 | } |