summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mail-search-mime-build.c
blob: cdec68f6dcd744f05132f28317dd07a48fa1f9bd (plain)
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
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "charset-utf8.h"
#include "mail-storage-private.h"
#include "mail-search-parser.h"
#include "mail-search-mime-register.h"
#include "mail-search-mime-build.h"

static int mail_search_mime_build_list(struct mail_search_mime_build_context *ctx,
				  struct mail_search_mime_arg **arg_r);

struct mail_search_mime_arg *
mail_search_mime_build_new(struct mail_search_mime_build_context *ctx,
		      enum mail_search_mime_arg_type type)
{
	struct mail_search_mime_arg *arg;

	arg = p_new(ctx->ctx->pool, struct mail_search_mime_arg, 1);
	arg->type = type;
	return arg;
}

struct mail_search_mime_arg *
mail_search_mime_build_str(struct mail_search_mime_build_context *ctx,
		      enum mail_search_mime_arg_type type)
{
	struct mail_search_mime_arg *sarg;
	const char *value;

	sarg = mail_search_mime_build_new(ctx, type);
	if (mail_search_parse_string(ctx->ctx->parser, &value) < 0)
		return NULL;
	sarg->value.str = p_strdup(ctx->ctx->pool, value);
	return sarg;
}

static int
mail_search_mime_build_key_int(struct mail_search_mime_build_context *ctx,
			  struct mail_search_mime_arg *parent,
			  struct mail_search_mime_arg **arg_r)
{
	struct mail_search_mime_arg *sarg;
	struct mail_search_mime_arg *old_parent = ctx->parent;
	const char *key;
	const struct mail_search_mime_register_arg *reg_arg;
	int ret;

	ctx->parent = parent;

	if ((ret = mail_search_parse_key(ctx->ctx->parser, &key)) <= 0)
		return ret;

	if (strcmp(key, MAIL_SEARCH_PARSER_KEY_LIST) == 0) {
		if (mail_search_mime_build_list(ctx, &sarg) < 0)
			return -1;
		if (sarg->value.subargs == NULL) {
			ctx->ctx->_error = "No MIMEPART keys inside list";
			return -1;
		}

		ctx->parent = old_parent;
		*arg_r = sarg;
		return 1;
	}
	key = t_str_ucase(key);

	reg_arg = mail_search_mime_register_find(key);
	if (reg_arg != NULL)
		sarg = reg_arg->build(ctx);
	else {
		sarg = NULL;
		ctx->ctx->_error = p_strconcat
			(ctx->ctx->pool, "Unknown MIMEPART key ", key, NULL);
	}

	ctx->parent = old_parent;
	*arg_r = sarg;
	return sarg == NULL ? -1 : 1;
}

int mail_search_mime_build_key(struct mail_search_mime_build_context *ctx,
			  struct mail_search_mime_arg *parent,
			  struct mail_search_mime_arg **arg_r)
{
	int ret;

	ret = mail_search_mime_build_key_int(ctx, parent, arg_r);
	if (ret <= 0) {
		if (ret == 0)
			ctx->ctx->_error = "Missing MIMEPART key";
		return -1;
	}
	return 0;
}

static int mail_search_mime_build_list(struct mail_search_mime_build_context *ctx,
				  struct mail_search_mime_arg **arg_r)
{
	struct mail_search_mime_arg *sarg, **subargs;
	enum mail_search_mime_arg_type cur_type = SEARCH_MIME_SUB;
	int ret;

	sarg = p_new(ctx->ctx->pool, struct mail_search_mime_arg, 1);
	sarg->type = cur_type;

	subargs = &sarg->value.subargs;
	while ((ret = mail_search_mime_build_key_int(ctx, sarg, subargs)) > 0) {
		if (cur_type == sarg->type) {
			/* expected type */
		} else if (cur_type == SEARCH_MIME_SUB) {
			/* type changed. everything in this list must now
			   belong to this type. */
			cur_type = sarg->type;
		} else {
			ctx->ctx->_error =
				"Use parenthesis when mixing ANDs and ORs";
			return -1;
		}
		subargs = &(*subargs)->next;
		sarg->type = SEARCH_MIME_SUB;
	}
	if (ret < 0)
		return -1;
	sarg->type = cur_type;
	*arg_r = sarg;
	return 0;
}

int mail_search_mime_build(struct mail_search_build_context *bctx,
		      struct mail_search_mime_part **mpart_r)
{
  struct mail_search_mime_build_context ctx;
	struct mail_search_mime_part *mpart;
	struct mail_search_mime_arg *root;
	int ret;

	*mpart_r = NULL;

	i_zero(&ctx);
	ctx.ctx = bctx;
	ctx.mime_part = mpart =
		p_new(bctx->pool, struct mail_search_mime_part, 1);

	if ((ret=mail_search_mime_build_key(&ctx, NULL, &root)) < 0)
		return ret;

	if (root->type == SEARCH_MIME_SUB && !root->match_not) {
		/* simple SUB root */
		mpart->args = root->value.subargs;
	} else {
		mpart->args = root;
	}

	*mpart_r = mpart;
	return 0;
}

struct mail_search_mime_arg *
mail_search_mime_build_add(pool_t pool,
		      struct mail_search_mime_part *mpart,
		      enum mail_search_mime_arg_type type)
{
	struct mail_search_mime_arg *arg;

	arg = p_new(pool, struct mail_search_mime_arg, 1);
	arg->type = type;

	arg->next = mpart->args;
	mpart->args = arg;
	return arg;
}