File: ext-notify-common.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 (312 lines) | stat: -rw-r--r-- 7,768 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
/* Copyright (c) 2002-2013 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 "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 = {
	"low",
	NULL,
	tag_importance_validate,
	NULL, NULL, NULL
};

static const struct sieve_argument_def importance_normal_tag = {
	"normal",
	NULL,
	tag_importance_validate,
	NULL, NULL, NULL
};

static const struct sieve_argument_def importance_high_tag = {
	"high",
	NULL,
	tag_importance_validate,
	NULL, NULL, NULL
};

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 ( strncmp(data, "text", 4) == 0 && data[4] == '/' ) {
		return TRUE;
	}

	return FALSE;
}

static buffer_t *cmd_notify_extract_body_text
(const struct sieve_runtime_env *renv)
{
	const struct sieve_extension *this_ext = renv->oprtn->ext;
	struct ext_notify_message_context *mctx;
	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;

	/* Return cached result if available */
	mctx = ext_notify_get_message_context(this_ext, renv->msgctx);
	if ( mctx->body_text != NULL ) {
		return mctx->body_text;
	}

	/* Create buffer */
	mctx->body_text = buffer_create_dynamic(mctx->pool, 1024*64);

	/* Get the message stream */
	if ( mail_get_stream(renv->msgdata->mail, NULL, NULL, &input) < 0 )
		return NULL;

	/* Initialize body decoder */
	decoder = message_decoder_init(NULL, 0);

	parser = message_parser_init(mctx->pool, input, 0, 0);
	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 )
		return NULL;

	/* Return status */
	return mctx->body_text;
}

void ext_notify_construct_message
(const struct sieve_runtime_env *renv, const char *msg_format,
	string_t *out_msg)
{
	const struct sieve_message_data *msgdata = renv->msgdata;
	const char *p;

	if ( msg_format == NULL )
		msg_format = "$from$: $subject$";

	/* Scan message for substitutions */
	p = msg_format;
	while ( *p != '\0' ) {
		const char *const *header;

		if ( strncasecmp(p, "$from$", 6) == 0 ) {
			p += 6;

			/* Fetch sender from oriinal message */
			if ( mail_get_headers_utf8(msgdata->mail, "from", &header) >= 0 )
				 str_append(out_msg, header[0]);

		} else if ( strncasecmp(p, "$env-from$", 10) == 0 ) {
			p += 10;

			if ( msgdata->return_path != NULL )
				str_append(out_msg, msgdata->return_path);

		} else if ( strncasecmp(p, "$subject$", 9) == 0 ) {
			p += 9;

			/* Fetch sender from oriinal message */
			if ( mail_get_headers_utf8(msgdata->mail, "subject", &header) >= 0 )
				 str_append(out_msg, header[0]);

		} 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_n(out_msg, begin, p-begin);
					valid = FALSE;
				}
			} else {
				p += 1;
			}

			if ( valid ) {
				size_t body_size;
				const char *body_text = (const char *)
					buffer_get_data(cmd_notify_extract_body_text(renv), &body_size);

				if ( num > 0 && num < body_size)
					str_append_n(out_msg, body_text, num);
				else
					str_append_n(out_msg, body_text, body_size);
			}
		} else {
			size_t len;

			/* Find next substitution */
			len = strcspn(p + 1, "$") + 1;

			/* Copy normal text */
			str_append_n(out_msg, p, len);
			p += len;
		}
  }
}