resproc.c
10.7 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* Response processing for IMAP client.
GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2015 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <mailutils/cstr.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/sys/imap.h>
struct mu_kwd mu_imap_response_codes[] = {
/* [ALERT] */
{ "ALERT", MU_IMAP_RESPONSE_ALERT },
/* [BADCHARSET (opt-list)] */
{ "BADCHARSET", MU_IMAP_RESPONSE_BADCHARSET },
/* [CAPABILITY (list)] */
{ "CAPABILITY", MU_IMAP_RESPONSE_CAPABILITY },
/* [PARSE] text */
{ "PARSE", MU_IMAP_RESPONSE_PARSE },
/* [PERMANENTFLAGS (list)] */
{ "PERMANENTFLAGS", MU_IMAP_RESPONSE_PERMANENTFLAGS },
/* [READ-ONLY] */
{ "READ-ONLY", MU_IMAP_RESPONSE_READ_ONLY },
/* [READ-WRITE] */
{ "READ-WRITE", MU_IMAP_RESPONSE_READ_WRITE },
/* [TRYCREATE] */
{ "TRYCREATE", MU_IMAP_RESPONSE_TRYCREATE },
/* [UIDNEXT N] */
{ "UIDNEXT", MU_IMAP_RESPONSE_UIDNEXT },
/* [UIDVALIDITY N] */
{ "UIDVALIDITY", MU_IMAP_RESPONSE_UIDVALIDITY },
/* [UNSEEN N] */
{ "UNSEEN", MU_IMAP_RESPONSE_UNSEEN },
{ NULL }
};
static int
parse_response_code (mu_imap_t imap, mu_list_t resp)
{
struct imap_list_element *arg;
int rcode = -1;
arg = _mu_imap_list_at (resp, 1);
if (!arg)
return -1;
if (_mu_imap_list_element_is_string (arg, "["))
{
arg = _mu_imap_list_at (resp, 2);
if (!arg || arg->type != imap_eltype_string)
return -1;
if (mu_kwd_xlat_name (mu_imap_response_codes, arg->v.string, &rcode))
return -1;
arg = _mu_imap_list_at (resp, 3);
if (!arg || !_mu_imap_list_element_is_string (arg, "]"))
return -1;
}
return rcode;
}
static void
ok_response (mu_imap_t imap, mu_list_t resp, void *data)
{
struct imap_list_element *arg;
int rcode;
size_t n = 0;
char *p;
rcode = parse_response_code (imap, resp);
switch (rcode)
{
case MU_IMAP_RESPONSE_PERMANENTFLAGS:
arg = _mu_imap_list_at (resp, 3);
if (!arg ||
_mu_imap_collect_flags (arg, &imap->mbox_stat.permanent_flags))
break;
imap->mbox_stat.flags |= MU_IMAP_STAT_PERMANENT_FLAGS;
mu_imap_callback (imap, MU_IMAP_CB_PERMANENT_FLAGS, 0, &imap->mbox_stat);
return;
case MU_IMAP_RESPONSE_UIDNEXT:
arg = _mu_imap_list_at (resp, 3);
if (!arg || arg->type != imap_eltype_string)
break;
n = strtoul (arg->v.string, &p, 10);
if (*p == 0)
{
imap->mbox_stat.uidnext = n;
imap->mbox_stat.flags |= MU_IMAP_STAT_UIDNEXT;
mu_imap_callback (imap, MU_IMAP_CB_UIDNEXT, 0, &imap->mbox_stat);
}
return;
case MU_IMAP_RESPONSE_UIDVALIDITY:
arg = _mu_imap_list_at (resp, 3);
if (!arg || arg->type != imap_eltype_string)
break;
n = strtoul (arg->v.string, &p, 10);
if (*p == 0)
{
imap->mbox_stat.uidvalidity = n;
imap->mbox_stat.flags |= MU_IMAP_STAT_UIDVALIDITY;
mu_imap_callback (imap, MU_IMAP_CB_UIDVALIDITY, 0, &imap->mbox_stat);
}
return;
case MU_IMAP_RESPONSE_UNSEEN:
arg = _mu_imap_list_at (resp, 3);
if (!arg || arg->type != imap_eltype_string)
break;
n = strtoul (arg->v.string, &p, 10);
if (*p == 0)
{
imap->mbox_stat.first_unseen = n;
imap->mbox_stat.flags |= MU_IMAP_STAT_FIRST_UNSEEN;
mu_imap_callback (imap, MU_IMAP_CB_FIRST_UNSEEN, 0,
&imap->mbox_stat);
}
return;
}
if (mu_list_tail (resp, (void*) &arg) || arg->type != imap_eltype_string)
arg = NULL;
mu_imap_callback (imap, MU_IMAP_CB_OK, rcode, arg ? arg->v.string : NULL);
if (imap->client_state == MU_IMAP_CLIENT_GREETINGS)
{
imap->client_state = MU_IMAP_CLIENT_READY;
imap->session_state = MU_IMAP_SESSION_NONAUTH;
}
}
static void
default_response (mu_imap_t imap, int code, mu_list_t resp, void *data)
{
struct imap_list_element *arg;
if (mu_list_tail (resp, (void*) &arg) || arg->type != imap_eltype_string)
arg = NULL;
mu_imap_callback (imap, code, parse_response_code (imap, resp),
arg ? arg->v.string : NULL);
}
static void
no_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_response (imap, MU_IMAP_CB_NO, resp, data);
if (imap->client_state == MU_IMAP_CLIENT_GREETINGS)
imap->client_state = MU_IMAP_CLIENT_ERROR;
}
static void
bad_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_response (imap, MU_IMAP_CB_BAD, resp, data);
if (imap->client_state == MU_IMAP_CLIENT_GREETINGS)
imap->client_state = MU_IMAP_CLIENT_ERROR;
}
static void
bye_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_response (imap, MU_IMAP_CB_BYE, resp, data);
imap->client_state = MU_IMAP_CLIENT_CLOSING;
}
static void
preauth_response (mu_imap_t imap, mu_list_t resp, void *data)
{
if (imap->client_state == MU_IMAP_CLIENT_GREETINGS)
{
struct imap_list_element *arg;
if (mu_list_tail (resp, (void*) &arg) || arg->type != imap_eltype_string)
arg = NULL;
mu_imap_callback (imap, MU_IMAP_CB_PREAUTH,
parse_response_code (imap, resp),
arg ? arg->v.string : NULL);
imap->client_state = MU_IMAP_CLIENT_READY;
imap->session_state = MU_IMAP_SESSION_AUTH;
}
else
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
("ignoring unexpected PREAUTH response"));
}
struct response_closure
{
mu_imap_t imap;
mu_imap_response_action_t fun;
void *data;
};
struct resptab
{
char *name;
mu_imap_response_action_t action;
int code;
};
static struct resptab resptab[] = {
{ "OK", ok_response },
{ "NO", no_response },
{ "BAD", bad_response },
{ "PREAUTH", preauth_response },
{ "BYE", bye_response },
{ NULL }
};
static int
_std_unsolicited_response (mu_imap_t imap, size_t count, mu_list_t resp)
{
struct resptab *rp;
struct imap_list_element *arg = _mu_imap_list_at (resp, 0);
if (!arg)
return 1;
if (arg->type == imap_eltype_string)
for (rp = resptab; rp->name; rp++)
{
if (mu_c_strcasecmp (rp->name, arg->v.string) == 0)
{
if (!rp->action)
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE9,
("%s:%d: ignoring %s response",
__FILE__, __LINE__, rp->name));
else
rp->action (imap, resp, NULL);
return 0;
}
}
return 1;
}
static int
_process_unsolicited_response (mu_imap_t imap, mu_list_t resp)
{
size_t count;
struct imap_list_element *arg;
if (mu_list_count (resp, &count))
return 1;
if (_std_unsolicited_response (imap, count, resp) == 0)
return 0;
if (count == 2)
{
size_t n;
char *p;
arg = _mu_imap_list_at (resp, 1);
if (!arg)
return 1;
if (_mu_imap_list_element_is_string (arg, "EXISTS"))
{
arg = _mu_imap_list_at (resp, 0);
if (!arg)
return 1;
n = strtoul (arg->v.string, &p, 10);
if (*p)
return 1;
imap->mbox_stat.message_count = n;
imap->mbox_stat.flags |= MU_IMAP_STAT_MESSAGE_COUNT;
mu_imap_callback (imap, MU_IMAP_CB_MESSAGE_COUNT, 0,
&imap->mbox_stat);
return 0;
}
else if (_mu_imap_list_element_is_string (arg, "RECENT"))
{
arg = _mu_imap_list_at (resp, 0);
if (!arg)
return 1;
n = strtoul (arg->v.string, &p, 10);
if (*p)
return 1;
imap->mbox_stat.recent_count = n;
imap->mbox_stat.flags |= MU_IMAP_STAT_RECENT_COUNT;
mu_imap_callback (imap, MU_IMAP_CB_RECENT_COUNT, 0,
&imap->mbox_stat);
return 0;
}
}
else if (count == 3 &&
_mu_imap_list_nth_element_is_string (resp, 1, "FETCH"))
{
size_t msgno;
arg = _mu_imap_list_at (resp, 0);
if (arg && arg->type == imap_eltype_string)
{
char *p;
msgno = strtoul (arg->v.string, &p, 10);
if (*p)
return 1;
arg = _mu_imap_list_at (resp, 2);
if (arg->type == imap_eltype_list)
{
mu_list_t list;
if (_mu_imap_parse_fetch_response (arg->v.list, &list) == 0)
{
mu_imap_callback (imap, MU_IMAP_CB_FETCH, msgno, list);
mu_list_destroy (&list);
}
return 0;
}
}
}
return 1;
}
int
_mu_imap_process_untagged_response (mu_imap_t imap, mu_list_t list,
mu_imap_response_action_t fun,
void *data)
{
if (_process_unsolicited_response (imap, list))
{
if (fun)
fun (imap, list, data);
else
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE9,
("ignoring unrecognized response"));
}
return 0;
}
static void
default_tagged_response (mu_imap_t imap, int code, mu_list_t resp, void *data)
{
struct imap_list_element *arg;
if (mu_list_tail (resp, (void*) &arg) == 0 &&
arg->type == imap_eltype_string)
_mu_imap_seterrstrz (imap, arg->v.string);
imap->response_code = parse_response_code (imap, resp);
mu_imap_callback (imap, code, imap->response_code,
arg ? arg->v.string : NULL);
}
static void
ok_tagged_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_tagged_response (imap, MU_IMAP_CB_TAGGED_OK, resp, data);
}
static void
no_tagged_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_tagged_response (imap, MU_IMAP_CB_TAGGED_NO, resp, data);
}
static void
bad_tagged_response (mu_imap_t imap, mu_list_t resp, void *data)
{
default_tagged_response (imap, MU_IMAP_CB_TAGGED_BAD, resp, data);
}
static struct resptab tagged_resptab[] = {
{ "OK", ok_tagged_response, MU_IMAP_OK },
{ "NO", no_tagged_response, MU_IMAP_NO },
{ "BAD", bad_tagged_response, MU_IMAP_BAD },
{ NULL }
};
static int
_std_tagged_response (mu_imap_t imap, size_t count, mu_list_t resp)
{
struct resptab *rp;
struct imap_list_element *arg = _mu_imap_list_at (resp, 0);
if (!arg)
return 1;
if (arg->type == imap_eltype_string)
for (rp = tagged_resptab; rp->name; rp++)
{
if (mu_c_strcasecmp (rp->name, arg->v.string) == 0)
{
imap->response = rp->code;
rp->action (imap, resp, NULL);
return 0;
}
}
return 1;
}
int
_mu_imap_process_tagged_response (mu_imap_t imap, mu_list_t resp)
{
size_t count;
if (mu_list_count (resp, &count))
return 1;
return _std_tagged_response (imap, count, resp);
}