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
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str.h"
#include "istream.h"
#include "rfc822-parser.h"
#include "message-parser.h"
#include "message-decoder.h"
#include "mail-storage.h"
#include "sieve-common.h"
#include "sieve-code.h"
#include "sieve-message.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 "ext-notify-common.h"
#include <ctype.h>
/*
* Importance argument
*/
static bool
tag_importance_validate(struct sieve_validator *valdtr,
struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static const struct sieve_argument_def importance_low_tag = {
.identifier = "low",
.validate = tag_importance_validate,
};
static const struct sieve_argument_def importance_normal_tag = {
.identifier = "normal",
.validate = tag_importance_validate,
};
static const struct sieve_argument_def importance_high_tag = {
.identifier = "high",
.validate = tag_importance_validate,
};
static bool
tag_importance_validate(struct sieve_validator *valdtr ATTR_UNUSED,
struct sieve_ast_argument **arg,
struct sieve_command *cmd ATTR_UNUSED)
{
struct sieve_ast_argument *tag = *arg;
if (sieve_argument_is(tag, importance_low_tag))
sieve_ast_argument_number_substitute(tag, 3);
else if (sieve_argument_is(tag, importance_normal_tag))
sieve_ast_argument_number_substitute(tag, 2);
else
sieve_ast_argument_number_substitute(tag, 1);
tag->argument = sieve_argument_create(tag->ast, &number_argument,
tag->argument->ext,
tag->argument->id_code);
/* Skip parameter */
*arg = sieve_ast_argument_next(*arg);
return TRUE;
}
void ext_notify_register_importance_tags(
struct sieve_validator *valdtr,
struct sieve_command_registration *cmd_reg,
const struct sieve_extension *ext, unsigned int id_code)
{
sieve_validator_register_tag(valdtr, cmd_reg, ext,
&importance_low_tag, id_code);
sieve_validator_register_tag(valdtr, cmd_reg, ext,
&importance_normal_tag, id_code);
sieve_validator_register_tag(valdtr, cmd_reg, ext,
&importance_high_tag, id_code);
}
/*
* Body extraction
*/
/* FIXME: overlaps somewhat with body extension */
struct ext_notify_message_context {
pool_t pool;
buffer_t *body_text;
};
static struct ext_notify_message_context *
ext_notify_get_message_context(const struct sieve_extension *this_ext,
struct sieve_message_context *msgctx)
{
struct ext_notify_message_context *ctx;
/* Get message context (contains cached message body information) */
ctx = (struct ext_notify_message_context *)
sieve_message_context_extension_get(msgctx, this_ext);
/* Create it if it does not exist already */
if (ctx == NULL) {
pool_t pool = sieve_message_context_pool(msgctx);
ctx = p_new(pool, struct ext_notify_message_context, 1);
ctx->pool = pool;
ctx->body_text = NULL;
/* Register context */
sieve_message_context_extension_set(msgctx, this_ext,
(void *)ctx);
}
return ctx;
}
static bool _is_text_content(const struct message_header_line *hdr)
{
struct rfc822_parser_context parser;
string_t *content_type;
const char *data;
/* Initialize parsing */
rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
(void)rfc822_skip_lwsp(&parser);
/* Parse content type */
content_type = t_str_new(64);
if (rfc822_parse_content_type(&parser, content_type) < 0)
return FALSE;
/* Content-type value must end here, otherwise it is invalid after all
*/
(void)rfc822_skip_lwsp(&parser);
if (parser.data != parser.end && *parser.data != ';')
return FALSE;
/* Success */
data = str_c(content_type);
if (str_begins(data, "text/"))
return TRUE;
return FALSE;
}
static int
cmd_notify_extract_body_text(const struct sieve_runtime_env *renv,
const char **body_text_r, size_t *body_size_r)
{
const struct sieve_execute_env *eenv = renv->exec_env;
const struct sieve_extension *this_ext = renv->oprtn->ext;
struct ext_notify_message_context *mctx;
struct mail *mail = eenv->msgdata->mail;
struct message_parser_ctx *parser;
struct message_decoder_context *decoder;
struct message_part *parts;
struct message_block block, decoded;
struct istream *input;
bool is_text, save_body;
int ret = 1;
*body_text_r = NULL;
*body_size_r = 0;
/* Return cached result if available */
mctx = ext_notify_get_message_context(this_ext, renv->msgctx);
if (mctx->body_text != NULL) {
*body_text_r = (const char *)
buffer_get_data(mctx->body_text, body_size_r);
return SIEVE_EXEC_OK;
}
/* Create buffer */
mctx->body_text = buffer_create_dynamic(mctx->pool, 1024*64);
/* Get the message stream */
if (mail_get_stream(mail, NULL, NULL, &input) < 0) {
return sieve_runtime_mail_error(renv, mail, "notify action: "
"failed to read input message");
}
/* Initialize body decoder */
decoder = message_decoder_init(NULL, 0);
struct message_parser_settings mparser_set = {
.hdr_flags = 0,
.flags = 0,
};
parser = message_parser_init(mctx->pool, input, &mparser_set);
is_text = TRUE;
save_body = FALSE;
while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
if (block.hdr != NULL || block.size == 0) {
/* Decode block */
(void)message_decoder_decode_next_block(decoder, &block,
&decoded);
/* Check for end of headers */
if (block.hdr == NULL) {
save_body = is_text;
continue;
}
/* We're interested of only Content-Type: header */
if (strcasecmp(block.hdr->name, "Content-Type") != 0)
continue;
/* Header can have folding whitespace. Acquire the full
value before continuing */
if (block.hdr->continues) {
block.hdr->use_full_value = TRUE;
continue;
}
/* Is it a text part? */
T_BEGIN {
is_text = _is_text_content(block.hdr);
} T_END;
continue;
}
/* Read text body */
if (save_body) {
(void)message_decoder_decode_next_block(decoder, &block,
&decoded);
buffer_append(mctx->body_text, decoded.data,
decoded.size);
is_text = TRUE;
}
}
/* Cleanup */
(void)message_parser_deinit(&parser, &parts);
message_decoder_deinit(&decoder);
if (ret < 0 && input->stream_errno != 0) {
sieve_runtime_critical(renv, NULL, "notify action: "
"failed to read input message",
"notify action: read(%s) failed: %s",
i_stream_get_name(input),
i_stream_get_error(input));
return SIEVE_EXEC_TEMP_FAILURE;
}
/* Return status */
*body_text_r = (const char *)buffer_get_data(mctx->body_text,
body_size_r);
return SIEVE_EXEC_OK;
}
int ext_notify_construct_message(const struct sieve_runtime_env *renv,
const char *msg_format,
string_t *out_msg)
{
const struct sieve_execute_env *eenv = renv->exec_env;
const struct sieve_message_data *msgdata = eenv->msgdata;
struct sieve_message_context *msgctx = renv->msgctx;
const struct smtp_address *return_path =
sieve_message_get_sender(msgctx);
const char *p;
int ret;
if (msg_format == NULL)
msg_format = "$from$: $subject$";
/* Scan message for substitutions */
p = msg_format;
while (*p != '\0') {
const char *header;
if (strncasecmp(p, "$from$", 6) == 0) {
p += 6;
/* Fetch sender from original message */
if ((ret = mail_get_first_header_utf8(
msgdata->mail, "from", &header)) < 0) {
return sieve_runtime_mail_error(
renv, msgdata->mail,
"failed to read header field `from'");
}
if (ret > 0)
str_append(out_msg, header);
} else if (strncasecmp(p, "$env-from$", 10) == 0) {
p += 10;
if (return_path != NULL)
smtp_address_write(out_msg, return_path);
} else if (strncasecmp(p, "$subject$", 9) == 0) {
p += 9;
/* Fetch sender from oriinal message */
if ((ret = mail_get_first_header_utf8(
msgdata->mail, "subject", &header)) < 0) {
return sieve_runtime_mail_error(
renv, msgdata->mail,
"failed to read header field `subject'");
}
if (ret > 0)
str_append(out_msg, header);
} else if (strncasecmp(p, "$text", 5) == 0 &&
(p[5] == '[' || p[5] == '$')) {
size_t num = 0;
const char *begin = p;
bool valid = TRUE;
p += 5;
if (*p == '[') {
p += 1;
while (i_isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
if (*p++ != ']' || *p++ != '$') {
str_append_data(out_msg, begin,
p-begin);
valid = FALSE;
}
} else {
p += 1;
}
if (valid) {
size_t body_size;
const char *body_text;
if ((ret = cmd_notify_extract_body_text(
renv, &body_text, &body_size)) <= 0)
return ret;
if (num > 0 && num < body_size) {
str_append_data(out_msg, body_text,
num);
} else {
str_append_data(out_msg, body_text,
body_size);
}
}
} else {
size_t len;
/* Find next substitution */
len = strcspn(p + 1, "$") + 1;
/* Copy normal text */
str_append_data(out_msg, p, len);
p += len;
}
}
return SIEVE_EXEC_OK;
}
|