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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "str.h"
#include "utc-offset.h"
#include "mail-index.h"
#include "imap-date.h"
#include "imap-util.h"
#include "imap-quote.h"
#include "mail-search.h"
#include "mail-search-mime.h"
#include <time.h>
static bool
mail_search_subargs_to_imap(string_t *dest, const struct mail_search_arg *args,
const char *prefix, const char **error_r)
{
const struct mail_search_arg *arg;
if (prefix[0] == '\0')
str_append_c(dest, '(');
for (arg = args; arg != NULL; arg = arg->next) {
if (arg->next != NULL)
str_append(dest, prefix);
if (!mail_search_arg_to_imap(dest, arg, error_r))
return FALSE;
if (arg->next != NULL)
str_append_c(dest, ' ');
}
if (prefix[0] == '\0')
str_append_c(dest, ')');
return TRUE;
}
static bool
mail_search_arg_to_imap_date(string_t *dest, const struct mail_search_arg *arg)
{
time_t timestamp = arg->value.time;
const char *str;
if ((arg->value.search_flags &
MAIL_SEARCH_ARG_FLAG_UTC_TIMES) == 0) {
struct tm *tm = localtime(×tamp);
int tz_offset = utc_offset(tm, timestamp);
timestamp -= tz_offset * 60;
}
if (!imap_to_date(timestamp, &str))
return FALSE;
str_printfa(dest, " \"%s\"", str);
return TRUE;
}
static void
mail_search_arg_to_imap_flags(string_t *dest, enum mail_flags flags)
{
static const char *flag_names[] = {
"ANSWERED", "FLAGGED", "DELETED", "SEEN", "DRAFT", "RECENT"
};
i_assert(flags != 0);
if (!bits_is_power_of_two(flags))
str_append_c(dest, '(');
for (unsigned int i = 0; i < N_ELEMENTS(flag_names); i++) {
if ((flags & (1 << i)) != 0) {
str_append(dest, flag_names[i]);
str_append_c(dest, ' ');
}
}
str_truncate(dest, str_len(dest)-1);
if (!bits_is_power_of_two(flags))
str_append_c(dest, ')');
}
bool mail_search_arg_to_imap(string_t *dest, const struct mail_search_arg *arg,
const char **error_r)
{
unsigned int start_pos;
if (arg->match_not)
str_append(dest, "NOT ");
start_pos = str_len(dest);
switch (arg->type) {
case SEARCH_OR:
if (!mail_search_subargs_to_imap(dest, arg->value.subargs,
"OR ", error_r))
return FALSE;
break;
case SEARCH_SUB:
if (!mail_search_subargs_to_imap(dest, arg->value.subargs,
"", error_r))
return FALSE;
break;
case SEARCH_ALL:
str_append(dest, "ALL");
break;
case SEARCH_SEQSET:
imap_write_seq_range(dest, &arg->value.seqset);
break;
case SEARCH_UIDSET:
str_append(dest, "UID ");
imap_write_seq_range(dest, &arg->value.seqset);
break;
case SEARCH_FLAGS:
mail_search_arg_to_imap_flags(dest, arg->value.flags);
break;
case SEARCH_KEYWORDS: {
const struct mail_keywords *kw = arg->initialized.keywords;
const ARRAY_TYPE(keywords) *names_arr;
const char *name;
unsigned int i;
if (kw == NULL || kw->count == 0) {
/* uninitialized / invalid keyword */
str_printfa(dest, "KEYWORD %s", arg->value.str);
break;
}
names_arr = mail_index_get_keywords(kw->index);
if (kw->count > 1)
str_append_c(dest, '(');
for (i = 0; i < kw->count; i++) {
name = array_idx_elem(names_arr, kw->idx[i]);
if (i > 0)
str_append_c(dest, ' ');
str_printfa(dest, "KEYWORD %s", name);
}
if (kw->count > 1)
str_append_c(dest, ')');
break;
}
case SEARCH_BEFORE:
switch (arg->value.date_type) {
case MAIL_SEARCH_DATE_TYPE_SENT:
str_append(dest, "SENTBEFORE");
break;
case MAIL_SEARCH_DATE_TYPE_RECEIVED:
str_append(dest, "BEFORE");
break;
case MAIL_SEARCH_DATE_TYPE_SAVED:
str_append(dest, "SAVEDBEFORE");
break;
}
if (mail_search_arg_to_imap_date(dest, arg))
;
else if (arg->value.date_type != MAIL_SEARCH_DATE_TYPE_RECEIVED ||
arg->value.time > ioloop_time) {
*error_r = t_strdup_printf(
"SEARCH_BEFORE can't be written as IMAP for timestamp %ld (type=%d, utc_times=%d)",
(long)arg->value.time, arg->value.date_type,
(arg->value.search_flags & MAIL_SEARCH_ARG_FLAG_UTC_TIMES) != 0);
return FALSE;
} else {
str_truncate(dest, start_pos);
str_printfa(dest, "OLDER %u",
(unsigned int)(ioloop_time - arg->value.time + 1));
}
break;
case SEARCH_ON:
switch (arg->value.date_type) {
case MAIL_SEARCH_DATE_TYPE_SENT:
str_append(dest, "SENTON");
break;
case MAIL_SEARCH_DATE_TYPE_RECEIVED:
str_append(dest, "ON");
break;
case MAIL_SEARCH_DATE_TYPE_SAVED:
str_append(dest, "SAVEDON");
break;
}
if (!mail_search_arg_to_imap_date(dest, arg)) {
*error_r = t_strdup_printf(
"SEARCH_ON can't be written as IMAP for timestamp %ld (type=%d, utc_times=%d)",
(long)arg->value.time, arg->value.date_type,
(arg->value.search_flags & MAIL_SEARCH_ARG_FLAG_UTC_TIMES) != 0);
return FALSE;
}
break;
case SEARCH_SINCE:
switch (arg->value.date_type) {
case MAIL_SEARCH_DATE_TYPE_SENT:
str_append(dest, "SENTSINCE");
break;
case MAIL_SEARCH_DATE_TYPE_RECEIVED:
str_append(dest, "SINCE");
break;
case MAIL_SEARCH_DATE_TYPE_SAVED:
str_append(dest, "SAVEDSINCE");
break;
}
if (mail_search_arg_to_imap_date(dest, arg))
;
else if (arg->value.date_type != MAIL_SEARCH_DATE_TYPE_RECEIVED ||
arg->value.time >= ioloop_time) {
*error_r = t_strdup_printf(
"SEARCH_SINCE can't be written as IMAP for timestamp %ld (type=%d, utc_times=%d)",
(long)arg->value.time, arg->value.date_type,
(arg->value.search_flags & MAIL_SEARCH_ARG_FLAG_UTC_TIMES) != 0);
return FALSE;
} else {
str_truncate(dest, start_pos);
str_printfa(dest, "YOUNGER %u",
(unsigned int)(ioloop_time - arg->value.time));
}
break;
case SEARCH_SMALLER:
str_printfa(dest, "SMALLER %"PRIuUOFF_T, arg->value.size);
break;
case SEARCH_LARGER:
str_printfa(dest, "LARGER %"PRIuUOFF_T, arg->value.size);
break;
case SEARCH_HEADER:
case SEARCH_HEADER_ADDRESS:
case SEARCH_HEADER_COMPRESS_LWSP:
if (strcasecmp(arg->hdr_field_name, "From") == 0 ||
strcasecmp(arg->hdr_field_name, "To") == 0 ||
strcasecmp(arg->hdr_field_name, "Cc") == 0 ||
strcasecmp(arg->hdr_field_name, "Bcc") == 0 ||
strcasecmp(arg->hdr_field_name, "Subject") == 0)
str_append(dest, t_str_ucase(arg->hdr_field_name));
else {
str_append(dest, "HEADER ");
imap_append_astring(dest, arg->hdr_field_name);
}
str_append_c(dest, ' ');
imap_append_astring(dest, arg->value.str);
break;
case SEARCH_BODY:
str_append(dest, "BODY ");
imap_append_astring(dest, arg->value.str);
break;
case SEARCH_TEXT:
str_append(dest, "TEXT ");
imap_append_astring(dest, arg->value.str);
break;
/* extensions */
case SEARCH_MODSEQ: {
bool extended_output = FALSE;
str_append(dest, "MODSEQ ");
if (arg->value.str != NULL) {
str_printfa(dest, "/flags/%s", arg->value.str);
extended_output = TRUE;
} else if (arg->value.flags != 0) {
str_append(dest, "/flags/");
imap_write_flags(dest, arg->value.flags, NULL);
extended_output = TRUE;
}
if (extended_output) {
str_append_c(dest, ' ');
switch (arg->value.modseq->type) {
case MAIL_SEARCH_MODSEQ_TYPE_ANY:
str_append(dest, "all");
break;
case MAIL_SEARCH_MODSEQ_TYPE_PRIVATE:
str_append(dest, "priv");
break;
case MAIL_SEARCH_MODSEQ_TYPE_SHARED:
str_append(dest, "shared");
break;
}
str_append_c(dest, ' ');
}
str_printfa(dest, "%"PRIu64, arg->value.modseq->modseq);
break;
}
case SEARCH_SAVEDATESUPPORTED:
str_append(dest, "SAVEDATESUPPORTED");
break;
case SEARCH_INTHREAD:
str_append(dest, "INTHREAD ");
imap_append_astring(dest, mail_thread_type_to_str(arg->value.thread_type));
str_append_c(dest, ' ');
if (!mail_search_subargs_to_imap(dest, arg->value.subargs,
"", error_r))
return FALSE;
break;
case SEARCH_GUID:
str_append(dest, "X-GUID ");
imap_append_astring(dest, arg->value.str);
break;
case SEARCH_MAILBOX:
*error_r = "SEARCH_MAILBOX can't be written as IMAP";
return FALSE;
case SEARCH_MAILBOX_GUID:
*error_r = "SEARCH_MAILBOX_GUID can't be written as IMAP";
return FALSE;
case SEARCH_MAILBOX_GLOB:
str_append(dest, "X-MAILBOX ");
imap_append_astring(dest, arg->value.str);
break;
case SEARCH_REAL_UID:
str_append(dest, "X-REAL-UID ");
imap_write_seq_range(dest, &arg->value.seqset);
break;
case SEARCH_MIMEPART:
str_append(dest, "MIMEPART ");
if (!mail_search_mime_part_to_imap(dest,
arg->value.mime_part, error_r))
return FALSE;
break;
case SEARCH_NIL:
str_append(dest, "NIL ");
break;
}
return TRUE;
}
bool mail_search_args_to_imap(string_t *dest, const struct mail_search_arg *args,
const char **error_r)
{
const struct mail_search_arg *arg;
for (arg = args; arg != NULL; arg = arg->next) {
if (!mail_search_arg_to_imap(dest, arg, error_r))
return FALSE;
if (arg->next != NULL)
str_append_c(dest, ' ');
}
return TRUE;
}
|