summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mail-search-parser-imap.c
blob: 12f48b3a1e5227d717da3a3079091f5de41ba1aa (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
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "imap-arg.h"
#include "mail-search-parser-private.h"

struct imap_arg_stack {
	struct imap_arg_stack *prev;

	const struct imap_arg *args;
};

struct imap_mail_search_parser {
	struct mail_search_parser parser;

	struct imap_arg_stack root, *cur;
};

static int imap_search_parse_key(struct mail_search_parser *_parser,
				 const char **key_r)
{
	struct imap_mail_search_parser *parser =
		(struct imap_mail_search_parser *)_parser;
	const struct imap_arg *arg = parser->cur->args;
	struct imap_arg_stack *stack;

	switch (arg->type) {
	case IMAP_ARG_NIL:
	case IMAP_ARG_ATOM:
		*key_r = imap_arg_as_astring(arg);
		break;
	case IMAP_ARG_STRING:
	case IMAP_ARG_LITERAL:
		_parser->error = t_strconcat(
			"Unexpected string as search key: ",
			imap_arg_as_astring(arg), NULL);
		return -1;
	case IMAP_ARG_LIST:
		stack = p_new(_parser->pool, struct imap_arg_stack, 1);
		stack->prev = parser->cur;
		stack->args = imap_arg_as_list(arg);

		parser->cur->args++;
		parser->cur = stack;

		*key_r = MAIL_SEARCH_PARSER_KEY_LIST;
		return 1;
	case IMAP_ARG_EOL:
		parser->cur = parser->cur->prev;
		return 0;
	case IMAP_ARG_LITERAL_SIZE:
	case IMAP_ARG_LITERAL_SIZE_NONSYNC:
		i_unreached();
	}
	parser->cur->args++;
	return 1;
}

static int imap_search_parse_string(struct mail_search_parser *_parser,
				    const char **value_r)
{
	struct imap_mail_search_parser *parser =
		(struct imap_mail_search_parser *)_parser;
	const struct imap_arg *arg = parser->cur->args;

	switch (arg->type) {
	case IMAP_ARG_NIL:
	case IMAP_ARG_ATOM:
	case IMAP_ARG_STRING:
	case IMAP_ARG_LITERAL:
		*value_r = imap_arg_as_astring(arg);
		break;
	case IMAP_ARG_LIST:
		_parser->error = "Unexpected (";
		return -1;
	case IMAP_ARG_EOL:
		_parser->error = "Missing parameter for search key";
		return -1;
	case IMAP_ARG_LITERAL_SIZE:
	case IMAP_ARG_LITERAL_SIZE_NONSYNC:
		i_unreached();
	}
	parser->cur->args++;
	return 1;
}

static bool
imap_search_parse_skip_next(struct mail_search_parser *_parser, const char *str)
{
	struct imap_mail_search_parser *parser =
		(struct imap_mail_search_parser *)_parser;
	const char *arg;

	if (!imap_arg_get_astring(parser->cur->args, &arg))
		return FALSE;
	if (strcasecmp(arg, str) != 0)
		return FALSE;

	parser->cur->args++;
	return TRUE;
}

static const struct mail_search_parser_vfuncs mail_search_parser_imap_vfuncs = {
	imap_search_parse_key,
	imap_search_parse_string,
	imap_search_parse_skip_next
};

struct mail_search_parser *
mail_search_parser_init_imap(const struct imap_arg *args)
{
	struct imap_mail_search_parser *parser;
	pool_t pool;

	pool = pool_alloconly_create("imap search parser", 1024);
	parser = p_new(pool, struct imap_mail_search_parser, 1);
	parser->parser.pool = pool;
	parser->parser.v = mail_search_parser_imap_vfuncs;
	parser->root.args = args;
	parser->cur = &parser->root;
	return &parser->parser;
}