summaryrefslogtreecommitdiffstats
path: root/src/lib-imap/imap-util.c
blob: dc1ae244669d872c1c03b9d1bcfb82fbeb0a8c40 (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
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
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "unichar.h"
#include "mail-types.h"
#include "imap-parser.h"
#include "imap-util.h"

void imap_write_flags(string_t *dest, enum mail_flags flags,
		      const char *const *keywords)
{
	size_t size;

	size = str_len(dest);
	if ((flags & MAIL_ANSWERED) != 0)
		str_append(dest, "\\Answered ");
	if ((flags & MAIL_FLAGGED) != 0)
		str_append(dest, "\\Flagged ");
	if ((flags & MAIL_DELETED) != 0)
		str_append(dest, "\\Deleted ");
	if ((flags & MAIL_SEEN) != 0)
		str_append(dest, "\\Seen ");
	if ((flags & MAIL_DRAFT) != 0)
		str_append(dest, "\\Draft ");
	if ((flags & MAIL_RECENT) != 0)
		str_append(dest, "\\Recent ");

	if (keywords != NULL) {
		/* we have keywords too */
		while (*keywords != NULL) {
			str_append(dest, *keywords);
			str_append_c(dest, ' ');
			keywords++;
		}
	}

	if (str_len(dest) != size)
		str_truncate(dest, str_len(dest)-1);
}

enum mail_flags imap_parse_system_flag(const char *str)
{
	if (strcasecmp(str, "\\Answered") == 0)
		return MAIL_ANSWERED;
	else if (strcasecmp(str, "\\Flagged") == 0)
		return MAIL_FLAGGED;
	else if (strcasecmp(str, "\\Deleted") == 0)
		return MAIL_DELETED;
	else if (strcasecmp(str, "\\Seen") == 0)
		return MAIL_SEEN;
	else if (strcasecmp(str, "\\Draft") == 0)
		return MAIL_DRAFT;
	else if (strcasecmp(str, "\\Recent") == 0)
		return MAIL_RECENT;
	else
		return 0;
}

void imap_write_seq_range(string_t *dest, const ARRAY_TYPE(seq_range) *array)
{
	const struct seq_range *range;
	unsigned int i, count;

	range = array_get(array, &count);
	for (i = 0; i < count; i++) {
		if (i > 0)
			str_append_c(dest, ',');
		str_printfa(dest, "%u", range[i].seq1);
		if (range[i].seq1 != range[i].seq2)
			str_printfa(dest, ":%u", range[i].seq2);
	}
}

void imap_write_arg(string_t *dest, const struct imap_arg *arg)
{
	switch (arg->type) {
	case IMAP_ARG_NIL:
		str_append(dest, "NIL");
		break;
	case IMAP_ARG_ATOM:
		str_append(dest, imap_arg_as_astring(arg));
		break;
	case IMAP_ARG_STRING: {
		const char *strarg = imap_arg_as_astring(arg);
		str_append_c(dest, '"');
		str_append_escaped(dest, strarg, strlen(strarg));
		str_append_c(dest, '"');
		break;
	}
	case IMAP_ARG_LITERAL: {
		const char *strarg = imap_arg_as_astring(arg);
		str_printfa(dest, "{%zu}\r\n",
			    strlen(strarg));
		str_append(dest, strarg);
		break;
	}
	case IMAP_ARG_LIST:
		str_append_c(dest, '(');
		imap_write_args(dest, imap_arg_as_list(arg));
		str_append_c(dest, ')');
		break;
	case IMAP_ARG_LITERAL_SIZE:
	case IMAP_ARG_LITERAL_SIZE_NONSYNC:
		str_printfa(dest, "<%"PRIuUOFF_T" byte literal>",
			    imap_arg_as_literal_size(arg));
		break;
	case IMAP_ARG_EOL:
		i_unreached();
	}
}

void imap_write_args(string_t *dest, const struct imap_arg *args)
{
	bool first = TRUE;

	for (; !IMAP_ARG_IS_EOL(args); args++) {
		if (first)
			first = FALSE;
		else
			str_append_c(dest, ' ');
		imap_write_arg(dest, args);
	}
}

static void imap_human_args_fix_control_chars(char *str)
{
	size_t i;

	for (i = 0; str[i] != '\0'; i++) {
		if (str[i] < 0x20 || str[i] == 0x7f)
			str[i] = '?';
	}
}

void imap_write_args_for_human(string_t *dest, const struct imap_arg *args)
{
	bool first = TRUE;

	for (; !IMAP_ARG_IS_EOL(args); args++) {
		if (first)
			first = FALSE;
		else
			str_append_c(dest, ' ');

		switch (args->type) {
		case IMAP_ARG_NIL:
			str_append(dest, "NIL");
			break;
		case IMAP_ARG_ATOM:
			/* atom has only printable us-ascii chars */
			str_append(dest, imap_arg_as_astring(args));
			break;
		case IMAP_ARG_STRING:
		case IMAP_ARG_LITERAL: {
			const char *strarg = imap_arg_as_astring(args);

			if (strpbrk(strarg, "\r\n") != NULL) {
				str_printfa(dest, "<%zu byte multi-line literal>",
					    strlen(strarg));
				break;
			}
			strarg = str_escape(strarg);

			str_append_c(dest, '"');
			size_t start_pos = str_len(dest);
			/* append only valid UTF-8 chars */
			if (uni_utf8_get_valid_data((const unsigned char *)strarg,
						    strlen(strarg), dest))
				str_append(dest, strarg);
			/* replace all control chars */
			imap_human_args_fix_control_chars(
				str_c_modifiable(dest) + start_pos);
			str_append_c(dest, '"');
			break;
		}
		case IMAP_ARG_LIST:
			str_append_c(dest, '(');
			imap_write_args_for_human(dest, imap_arg_as_list(args));
			str_append_c(dest, ')');
			break;
		case IMAP_ARG_LITERAL_SIZE:
		case IMAP_ARG_LITERAL_SIZE_NONSYNC:
			str_printfa(dest, "<%"PRIuUOFF_T" byte literal>",
				    imap_arg_as_literal_size(args));
			break;
		case IMAP_ARG_EOL:
			i_unreached();
		}
	}
}

const char *imap_args_to_str(const struct imap_arg *args)
{
	string_t *str;

	str = t_str_new(128);
	imap_write_args(str, args);
	return str_c(str);
}