resproc.c
8.71 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
/* Response processing for IMAP client.
GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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, 4);
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, &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, &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, &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, &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->state == MU_IMAP_GREETINGS)
{
imap->state = MU_IMAP_CONNECTED;
imap->imap_state = MU_IMAP_STATE_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->state == MU_IMAP_GREETINGS)
imap->state = MU_IMAP_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->state == MU_IMAP_GREETINGS)
imap->state = MU_IMAP_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->state = MU_IMAP_CLOSING;
}
static void
preauth_response (mu_imap_t imap, mu_list_t resp, void *data)
{
if (imap->state == MU_IMAP_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->state = MU_IMAP_CONNECTED;
imap->imap_state = MU_IMAP_STATE_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;
};
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, resp,
&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, resp,
&imap->mbox_stat);
return 0;
}
}
return 1;
}
static int
_process_response (void *item, void *data)
{
struct imap_list_element *elt = item;
struct response_closure *clos = data;
if (elt->type != imap_eltype_list)
{
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
("ignoring string response \"%s\"", elt->v.string));
}
else if (_process_unsolicited_response (clos->imap, elt->v.list))
{
if (clos->fun)
clos->fun (clos->imap, elt->v.list, clos->data);
else
mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
("ignoring unexpected response"));
}
return 0;
}
int
mu_imap_foreach_response (mu_imap_t imap, mu_imap_response_action_t fun,
void *data)
{
struct response_closure clos;
clos.imap = imap;
clos.fun = fun;
clos.data = data;
return mu_list_foreach (imap->untagged_resp, _process_response, &clos);
}