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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
#include "imap-common.h"
#include "array.h"
#include "buffer.h"
#include "str.h"
#include "str-sanitize.h"
#include "imap-resp-code.h"
#include "imap-parser.h"
#include "imap-sync.h"
#include "imap-utf7.h"
#include "imap-util.h"
#include "mail-storage.h"
#include "mail-namespace.h"
#include "imap-commands-util.h"
struct mail_namespace *
client_find_namespace_full(struct client *client,
const char **mailbox, const char **client_error_r)
{
struct mail_namespace *namespaces = client->user->namespaces;
struct mail_namespace *ns;
string_t *utf8_name;
utf8_name = t_str_new(64);
if (imap_utf7_to_utf8(*mailbox, utf8_name) < 0) {
*client_error_r = "NO Mailbox name is not valid mUTF-7";
return NULL;
}
ns = mail_namespace_find(namespaces, str_c(utf8_name));
if ((ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0 &&
ns->prefix_len == 0) {
/* this matched only the autocreated prefix="" namespace.
give a nice human-readable error message */
*client_error_r = t_strdup_printf(
"NO Client tried to access nonexistent namespace. "
"(Mailbox name should probably be prefixed with: %s)",
mail_namespace_find_inbox(namespaces)->prefix);
return NULL;
}
if ((client->set->parsed_workarounds &
WORKAROUND_TB_EXTRA_MAILBOX_SEP) != 0 &&
str_len(utf8_name) > 0 &&
str_c(utf8_name)[str_len(utf8_name)-1] == mail_namespace_get_sep(ns)) {
/* drop the extra trailing hierarchy separator */
str_truncate(utf8_name, str_len(utf8_name)-1);
}
*mailbox = str_c(utf8_name);
return ns;
}
struct mail_namespace *
client_find_namespace(struct client_command_context *cmd, const char **mailbox)
{
struct mail_namespace *ns;
const char *client_error;
ns = client_find_namespace_full(cmd->client, mailbox, &client_error);
if (ns == NULL)
client_send_tagline(cmd, client_error);
return ns;
}
bool client_verify_open_mailbox(struct client_command_context *cmd)
{
if (cmd->client->mailbox != NULL) {
event_add_str(cmd->global_event, "mailbox",
mailbox_get_vname(cmd->client->mailbox));
return TRUE;
} else {
client_send_tagline(cmd, "BAD No mailbox selected.");
return FALSE;
}
}
void imap_client_close_mailbox(struct client *client)
{
struct mailbox *box;
i_assert(client->mailbox != NULL);
if (array_is_created(&client->fetch_failed_uids))
array_clear(&client->fetch_failed_uids);
client_search_updates_free(client);
array_free(&client->search_saved_uidset);
box = client->mailbox;
client->mailbox = NULL;
mailbox_free(&box);
client_update_mailbox_flags(client, NULL);
}
int client_open_save_dest_box(struct client_command_context *cmd,
const char *name, struct mailbox **destbox_r)
{
struct mail_namespace *ns;
struct mailbox *box;
const char *error_string;
enum mail_error error;
ns = client_find_namespace(cmd, &name);
if (ns == NULL)
return -1;
if (cmd->client->mailbox != NULL &&
mailbox_equals(cmd->client->mailbox, ns, name)) {
*destbox_r = cmd->client->mailbox;
return 0;
}
box = mailbox_alloc(ns->list, name, MAILBOX_FLAG_SAVEONLY);
if (mailbox_open(box) < 0) {
error_string = mailbox_get_last_error(box, &error);
if (error == MAIL_ERROR_NOTFOUND) {
client_send_tagline(cmd, t_strdup_printf(
"NO [TRYCREATE] %s", error_string));
} else {
client_send_box_error(cmd, box);
}
mailbox_free(&box);
return -1;
}
if (mailbox_enable(box, client_enabled_mailbox_features(cmd->client)) < 0) {
client_send_box_error(cmd, box);
mailbox_free(&box);
return -1;
}
*destbox_r = box;
return 0;
}
const char *imap_client_command_get_reason(struct client_command_context *cmd)
{
return cmd->args[0] == '\0' ? cmd->name :
t_strdup_printf("%s %s", cmd->name, cmd->human_args);
}
const char *
imap_get_error_string(struct client_command_context *cmd,
const char *error_string, enum mail_error error)
{
const char *resp_code = NULL;
switch (error) {
case MAIL_ERROR_NONE:
break;
case MAIL_ERROR_TEMP:
case MAIL_ERROR_LOOKUP_ABORTED: /* BUG: shouldn't be visible here */
resp_code = IMAP_RESP_CODE_SERVERBUG;
break;
case MAIL_ERROR_UNAVAILABLE:
resp_code = IMAP_RESP_CODE_UNAVAILABLE;
break;
case MAIL_ERROR_NOTPOSSIBLE:
case MAIL_ERROR_PARAMS:
resp_code = IMAP_RESP_CODE_CANNOT;
break;
case MAIL_ERROR_PERM:
resp_code = IMAP_RESP_CODE_NOPERM;
break;
case MAIL_ERROR_NOQUOTA:
resp_code = IMAP_RESP_CODE_OVERQUOTA;
break;
case MAIL_ERROR_NOTFOUND:
if ((cmd->cmd_flags & COMMAND_FLAG_USE_NONEXISTENT) != 0)
resp_code = IMAP_RESP_CODE_NONEXISTENT;
break;
case MAIL_ERROR_EXISTS:
resp_code = IMAP_RESP_CODE_ALREADYEXISTS;
break;
case MAIL_ERROR_EXPUNGED:
resp_code = IMAP_RESP_CODE_EXPUNGEISSUED;
break;
case MAIL_ERROR_INUSE:
resp_code = IMAP_RESP_CODE_INUSE;
break;
case MAIL_ERROR_CONVERSION:
case MAIL_ERROR_INVALIDDATA:
break;
case MAIL_ERROR_LIMIT:
resp_code = IMAP_RESP_CODE_LIMIT;
break;
}
if (resp_code == NULL || *error_string == '[')
return t_strconcat("NO ", error_string, NULL);
else
return t_strdup_printf("NO [%s] %s", resp_code, error_string);
}
void client_send_error(struct client_command_context *cmd,
const char *error_string, enum mail_error error)
{
client_send_tagline(cmd, imap_get_error_string(cmd, error_string,
error));
client_disconnect_if_inconsistent(cmd->client);
}
void client_send_list_error(struct client_command_context *cmd,
struct mailbox_list *list)
{
const char *error_string;
enum mail_error error;
error_string = mailbox_list_get_last_error(list, &error);
client_send_tagline(cmd, imap_get_error_string(cmd, error_string,
error));
}
void client_disconnect_if_inconsistent(struct client *client)
{
if (client->mailbox != NULL &&
mailbox_is_inconsistent(client->mailbox)) {
/* we can't do forced CLOSE, so have to disconnect */
client_disconnect_with_error(client,
"IMAP session state is inconsistent, please relogin.");
}
}
void client_send_box_error(struct client_command_context *cmd,
struct mailbox *box)
{
client_send_storage_error(cmd, mailbox_get_storage(box));
}
void client_send_storage_error(struct client_command_context *cmd,
struct mail_storage *storage)
{
const char *error_string;
enum mail_error error;
error_string = mail_storage_get_last_error(storage, &error);
client_send_error(cmd, error_string, error);
}
void client_send_untagged_storage_error(struct client *client,
struct mail_storage *storage)
{
const char *error_string;
enum mail_error error;
error_string = mail_storage_get_last_error(storage, &error);
client_send_line(client, t_strconcat("* NO ", error_string, NULL));
client_disconnect_if_inconsistent(client);
}
bool client_parse_mail_flags(struct client_command_context *cmd,
const struct imap_arg *args,
enum mail_flags *flags_r,
const char *const **keywords_r)
{
const char *atom;
enum mail_flags flag;
ARRAY(const char *) keywords;
*flags_r = 0;
*keywords_r = NULL;
p_array_init(&keywords, cmd->pool, 16);
while (!IMAP_ARG_IS_EOL(args)) {
if (!imap_arg_get_atom(args, &atom)) {
client_send_command_error(cmd,
"Flags list contains non-atoms.");
return FALSE;
}
if (*atom == '\\') {
/* system flag */
atom = t_str_ucase(atom);
flag = imap_parse_system_flag(atom);
if (flag != 0 && flag != MAIL_RECENT)
*flags_r |= flag;
else {
client_send_command_error(cmd, t_strconcat(
"Invalid system flag ", atom, NULL));
return FALSE;
}
} else {
/* keyword validity checks are done by lib-storage */
array_push_back(&keywords, &atom);
}
args++;
}
if (array_count(&keywords) == 0)
*keywords_r = NULL;
else {
array_append_zero(&keywords); /* NULL-terminate */
*keywords_r = array_front(&keywords);
}
return TRUE;
}
void client_send_mailbox_flags(struct client *client, bool selecting)
{
struct mailbox_status status;
unsigned int count = array_count(client->keywords.names);
const char *const *keywords;
string_t *str;
if (!selecting && count == client->keywords.announce_count) {
/* no changes to keywords and we're not selecting a mailbox */
return;
}
client->keywords.announce_count = count;
mailbox_get_open_status(client->mailbox, STATUS_PERMANENT_FLAGS,
&status);
keywords = count == 0 ? NULL :
array_front(client->keywords.names);
str = t_str_new(128);
str_append(str, "* FLAGS (");
imap_write_flags(str, status.flags, keywords);
str_append_c(str, ')');
client_send_line(client, str_c(str));
if (!status.permanent_keywords)
keywords = NULL;
str_truncate(str, 0);
str_append(str, "* OK [PERMANENTFLAGS (");
imap_write_flags(str, status.permanent_flags, keywords);
if (status.allow_new_keywords) {
if (status.permanent_flags != 0 || keywords != NULL)
str_append_c(str, ' ');
str_append(str, "\\*");
}
str_append(str, ")] ");
if (mailbox_is_readonly(client->mailbox))
str_append(str, "Read-only mailbox.");
else
str_append(str, "Flags permitted.");
client_send_line(client, str_c(str));
}
void client_update_mailbox_flags(struct client *client,
const ARRAY_TYPE(keywords) *keywords)
{
client->keywords.names = keywords;
client->keywords.announce_count = 0;
}
const char *const *
client_get_keyword_names(struct client *client, ARRAY_TYPE(keywords) *dest,
const ARRAY_TYPE(keyword_indexes) *src)
{
unsigned int kw_index;
const char *const *all_names;
unsigned int all_count;
client_send_mailbox_flags(client, FALSE);
/* convert indexes to names */
all_names = array_get(client->keywords.names, &all_count);
array_clear(dest);
array_foreach_elem(src, kw_index) {
i_assert(kw_index < all_count);
array_push_back(dest, &all_names[kw_index]);
}
array_append_zero(dest);
return array_front(dest);
}
void msgset_generator_init(struct msgset_generator_context *ctx, string_t *str)
{
i_zero(ctx);
ctx->str = str;
ctx->last_uid = (uint32_t)-1;
}
void msgset_generator_next(struct msgset_generator_context *ctx, uint32_t uid)
{
i_assert(uid > 0);
if (uid-1 != ctx->last_uid) {
if (ctx->first_uid == 0)
;
else if (ctx->first_uid == ctx->last_uid)
str_printfa(ctx->str, "%u,", ctx->first_uid);
else {
str_printfa(ctx->str, "%u:%u,",
ctx->first_uid, ctx->last_uid);
}
ctx->first_uid = uid;
}
ctx->last_uid = uid;
}
void msgset_generator_finish(struct msgset_generator_context *ctx)
{
if (ctx->first_uid == ctx->last_uid)
str_printfa(ctx->str, "%u", ctx->first_uid);
else
str_printfa(ctx->str, "%u:%u", ctx->first_uid, ctx->last_uid);
}
|