id.c
4.38 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 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 <mailutils/errno.h>
#include <mailutils/assoc.h>
#include <mailutils/stream.h>
#include <mailutils/imap.h>
#include <mailutils/sys/imap.h>
void
_id_free (void *data)
{
char *s = *(char**)data;
free (s);
}
struct id_convert_state
{
int item;
mu_assoc_t assoc;
int ret;
};
static int
_id_convert (void *item, void *data)
{
struct imap_list_element *elt = item;
struct id_convert_state *stp = data;
switch (stp->item)
{
case 0:
if (!(elt->type == imap_eltype_string &&
strcmp (elt->v.string, "ID") == 0))
{
stp->ret = MU_ERR_PARSE;
return 1;
}
stp->item++;
return 0;
case 1:
if (elt->type == imap_eltype_list)
{
mu_iterator_t itr;
mu_list_get_iterator (elt->v.list, &itr);
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
char *key, *val;
mu_iterator_current (itr, (void **) &elt);
if (elt->type != imap_eltype_string)
break;
key = elt->v.string;
elt->v.string = NULL;
mu_iterator_next (itr);
if (mu_iterator_is_done (itr))
break;
mu_iterator_current (itr, (void **) &elt);
if (elt->type != imap_eltype_string)
break;
val = elt->v.string;
elt->v.string = NULL;
mu_assoc_install (stp->assoc, key, &val);
}
}
}
return 1;
}
static int
parse_id_reply (mu_imap_t imap, mu_assoc_t *passoc)
{
int rc;
struct imap_list_element const *response;
struct id_convert_state st;
mu_assoc_t assoc;
rc = mu_assoc_create (&assoc, sizeof (char**), MU_ASSOC_ICASE);
if (rc)
return rc;
mu_assoc_set_free (assoc, _id_free);
rc = mu_list_get (imap->untagged_resp, 0, (void*) &response);
*passoc = assoc;
if (rc == MU_ERR_NOENT)
return 0;
st.item = 0;
st.assoc = assoc;
st.ret = 0;
mu_list_do (response->v.list, _id_convert, &st);
return st.ret;
}
int
mu_imap_id (mu_imap_t imap, char **idenv, mu_assoc_t *passoc)
{
int status;
if (imap == NULL)
return EINVAL;
if (!imap->io)
return MU_ERR_NO_TRANSPORT;
if (imap->state != MU_IMAP_CONNECTED)
return MU_ERR_SEQ;
switch (imap->state)
{
case MU_IMAP_CONNECTED:
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s ID ", imap->tag_str);
MU_IMAP_CHECK_ERROR (imap, status);
if (!idenv)
status = mu_imapio_printf (imap->io, "NIL");
else
{
if (idenv[0])
{
int i;
char *delim = "(";
for (i = 0; idenv[i]; i++)
{
status = mu_imapio_printf (imap->io, "%s\"%s\"",
delim, idenv[i]);
MU_IMAP_CHECK_ERROR (imap, status);
delim = " ";
if (status)
break;
}
status = mu_imapio_printf (imap->io, ")");
}
else
status = mu_imapio_printf (imap->io, "()");
}
MU_IMAP_CHECK_ERROR (imap, status);
status = mu_imapio_printf (imap->io, "\r\n");
MU_IMAP_CHECK_ERROR (imap, status);
MU_IMAP_FCLR (imap, MU_IMAP_RESP);
imap->state = MU_IMAP_ID_RX;
case MU_IMAP_ID_RX:
status = _mu_imap_response (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
switch (imap->resp_code)
{
case MU_IMAP_OK:
imap->imap_state = MU_IMAP_STATE_AUTH;
if (passoc)
status = parse_id_reply (imap, passoc);
break;
case MU_IMAP_NO:
status = EACCES;
break;
case MU_IMAP_BAD:
status = MU_ERR_BADREPLY;
break;
}
imap->state = MU_IMAP_CONNECTED;
break;
default:
status = EINPROGRESS;
}
return status;
}