File: imap-filter.c

package info (click to toggle)
dovecot 1:2.3.4.1-5+deb10u1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 46,880 kB
  • sloc: ansic: 499,137; makefile: 7,352; sh: 5,592; cpp: 1,555; perl: 303; python: 73; xml: 44; pascal: 27
file content (254 lines) | stat: -rw-r--r-- 6,133 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
/* Copyright (c) 2017-2018 Pigeonhole authors, see the included COPYING file */

#include "imap-common.h"
#include "str.h"
#include "ostream.h"
#include "imap-resp-code.h"
#include "imap-search-args.h"

#include "imap-filter.h"
#include "imap-filter-sieve.h"

static void imap_filter_args_check(struct imap_filter_context *ctx,
				   const struct mail_search_arg *sargs)
{
	for (; sargs != NULL; sargs = sargs->next) {
		switch (sargs->type) {
		case SEARCH_SEQSET:
			ctx->have_seqsets = TRUE;
			break;
		case SEARCH_MODSEQ:
			ctx->have_modseqs = TRUE;
			break;
		case SEARCH_OR:
		case SEARCH_SUB:
			imap_filter_args_check(ctx, sargs->value.subargs);
			break;
		default:
			break;
		}
	}
}

static bool
imap_filter_mail(struct client_command_context *cmd, struct mail *mail)
{
	struct imap_filter_context *ctx = cmd->context;
	struct client *client = cmd->client;
	string_t *errors = NULL;
	bool have_warnings = FALSE;
	int ret;

	// FIXME: return fatal error status when no mail filter activity will
	// work (e.g. when binary is corrupt)
	ret = imap_sieve_filter_run_mail(ctx->sieve, mail,
					 &errors, &have_warnings);

	o_stream_nsend_str(client->output,
		t_strdup_printf("* %u FILTERED (TAG %s) UID %u ",
				mail->seq, cmd->tag, mail->uid));
	if (ret < 0 || have_warnings) {
		o_stream_nsend_str(client->output,
			t_strdup_printf("%s {%"PRIuSIZE_T"}\r\n",
					(ret < 0 ? "ERRORS" : "WARNINGS"),
					str_len(errors)));
		o_stream_nsend(client->output,
			       str_data(errors), str_len(errors));
		o_stream_nsend_str(client->output, "\r\n");
	} else {
		o_stream_nsend_str(client->output, "OK\r\n");
	}

	/* Handle the result */
	if (ret < 0) {
		/* Sieve error; keep */
	} else {
		if (ret > 0) {
			/* Discard */
			mail_update_flags(mail, MODIFY_ADD, MAIL_DELETED);
		}
	}

	return TRUE;
}

static bool imap_filter_more(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	struct mail *mail;
	enum mailbox_sync_flags sync_flags;
	const char *ok_reply;
	bool tryagain, lost_data;

	if (cmd->cancel) {
		(void)imap_filter_deinit(ctx);
		return TRUE;
	}

	while (mailbox_search_next_nonblock(ctx->search_ctx,
					    &mail, &tryagain)) {
		if (!imap_filter_mail(cmd, mail))
			break;
	}
	if (tryagain)
		return FALSE;

	lost_data = mailbox_search_seen_lost_data(ctx->search_ctx);
	if (imap_filter_deinit(ctx) < 0) {
		client_send_box_error(cmd, cmd->client->mailbox);
		return TRUE;
	}

	sync_flags = MAILBOX_SYNC_FLAG_FAST;
	if (!cmd->uid || ctx->have_seqsets)
		sync_flags |= MAILBOX_SYNC_FLAG_NO_EXPUNGES;
	ok_reply = t_strdup_printf("OK %sFilter completed",
		lost_data ? "["IMAP_RESP_CODE_EXPUNGEISSUED"] " : "");
	return cmd_sync(cmd, sync_flags, 0, ok_reply);
}

static void imap_filter_more_callback(struct client_command_context *cmd)
{
	struct client *client = cmd->client;
	bool finished;

	o_stream_cork(client->output);
	finished = command_exec(cmd);
	o_stream_uncork(client->output);

	if (!finished)
		(void)client_handle_unfinished_cmd(cmd);
	else
		client_command_free(&cmd);
	cmd_sync_delayed(client);

	if (client->disconnected)
		client_destroy(client, NULL);
	else
		client_continue_pending_input(client);
}

static bool
imap_filter_start(struct imap_filter_context *ctx,
		  struct mail_search_args *sargs)
{
	struct client_command_context *cmd = ctx->cmd;

	imap_filter_args_check(ctx, sargs->args);

	if (ctx->have_modseqs)
		(void)client_enable(cmd->client, MAILBOX_FEATURE_CONDSTORE);

	ctx->box = cmd->client->mailbox;
	ctx->trans = mailbox_transaction_begin(ctx->box, 0,
					       imap_client_command_get_reason(cmd));
	ctx->sargs = sargs;
	ctx->search_ctx = mailbox_search_init(ctx->trans, sargs, NULL, 0, NULL);

	cmd->func = imap_filter_more;
	cmd->context = ctx;

	if (imap_filter_more(cmd))
		return TRUE;

	/* we may have moved onto syncing by now */
	if (cmd->func == imap_filter_more) {
		ctx->to = timeout_add(0, imap_filter_more_callback, cmd);
		cmd->state = CLIENT_COMMAND_STATE_WAIT_EXTERNAL;
	}
	return FALSE;
}

static bool
imap_filter_parse_search(struct imap_filter_context *ctx,
			const struct imap_arg *args)
{
	struct client_command_context *cmd = ctx->cmd;
	struct mail_search_args *sargs;
	const char *charset;
	int ret;

	if (imap_arg_atom_equals(args, "CHARSET")) {
		/* CHARSET specified */
		if (!imap_arg_get_astring(&args[1], &charset)) {
			client_send_command_error(cmd,
				"Invalid charset argument.");
			imap_filter_context_free(ctx);
			return TRUE;
		}
		args += 2;
	} else {
		charset = "UTF-8";
	}

	ret = imap_search_args_build(cmd, args, charset, &sargs);
	if (ret <= 0) {
		imap_filter_context_free(ctx);
		return ret < 0;
	}

	return imap_filter_start(ctx, sargs);
}

bool imap_filter_search(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	const struct imap_arg *args;
	const char *error;
	enum imap_parser_error parse_error;
	int ret;

	ret = imap_parser_read_args(ctx->parser, 0, 0, &args);
	if (ret < 0) {
		if (ret == -2)
			return FALSE;
		error = imap_parser_get_error(ctx->parser, &parse_error);
		switch (parse_error) {
		case IMAP_PARSE_ERROR_NONE:
			i_unreached();
		case IMAP_PARSE_ERROR_LITERAL_TOO_BIG:
			client_disconnect_with_error(ctx->cmd->client, error);
			break;
		default:
			client_send_command_error(ctx->cmd, error);
			break;
		}
		return TRUE;
	}
	return imap_filter_parse_search(ctx, args);
}

int imap_filter_deinit(struct imap_filter_context *ctx)
{
	int ret = 0;

	o_stream_set_flush_callback(ctx->cmd->client->output,
				    client_output, ctx->cmd->client);
	ctx->cmd->client->input_lock = NULL;
	imap_parser_unref(&ctx->parser);

	if (ctx->search_ctx != NULL &&
	    mailbox_search_deinit(&ctx->search_ctx) < 0)
		ret = -1;

	if (ctx->trans != NULL)
		(void)mailbox_transaction_commit(&ctx->trans);

	timeout_remove(&ctx->to);
	if (ctx->sargs != NULL) {
		mail_search_args_deinit(ctx->sargs);
		mail_search_args_unref(&ctx->sargs);
	}
	imap_filter_context_free(ctx);

	ctx->cmd->context = NULL;
	return ret;
}

void imap_filter_context_free(struct imap_filter_context *ctx)
{
	imap_filter_sieve_context_free(&ctx->sieve);
}