13 |
|
|
14 |
|
|
15 |
extern unsigned int options; |
extern unsigned int options; |
16 |
|
extern unsigned int capabilities; |
17 |
|
|
18 |
|
char *vbuf = NULL; |
19 |
|
size_t vbufs = 0; |
20 |
|
|
21 |
/* |
/* |
22 |
* Read one packet of data that the server sent. |
* Read one packet of data that the server sent. |
38 |
int server_response(unsigned int tag) |
int server_response(unsigned int tag) |
39 |
{ |
{ |
40 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
41 |
|
|
42 |
|
reset_vbuf(); |
43 |
|
|
44 |
do |
do { |
45 |
receive_response(buf); |
receive_response(buf); |
46 |
while (tag && !strcasestr(buf, ultostr(tag, 16))); |
check_vbuf(strlen(buf)); |
47 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
48 |
|
} while (tag && !strcasestr(buf, ultostr(tag, 16))); |
49 |
|
|
50 |
return analyze_response(buf); |
return analyze_response(buf); |
51 |
} |
} |
76 |
{ |
{ |
77 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
78 |
|
|
79 |
do |
reset_vbuf(); |
|
receive_response(buf); |
|
|
while (!strcasestr(buf, ultostr(tag, 16))); |
|
80 |
|
|
81 |
if (!strcasestr(buf, "IMAP4rev1")) { |
do { |
82 |
|
receive_response(buf); |
83 |
|
check_vbuf(strlen(buf)); |
84 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
85 |
|
} while (!strcasestr(buf, ultostr(tag, 16))); |
86 |
|
|
87 |
|
if (!strcasestr(vbuf, "IMAP4rev1")) { |
88 |
error("imapfilter: server does not support IMAP4rev1 protocol\n"); |
error("imapfilter: server does not support IMAP4rev1 protocol\n"); |
89 |
return -2; |
return -2; |
90 |
} |
} |
91 |
|
if (strcasestr(vbuf, "NAMESPACE")) |
92 |
|
capabilities |= CAPABILITY_NAMESPACE; |
93 |
|
|
94 |
|
return analyze_response(vbuf); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
/* |
99 |
|
* Process the data that server sent due to IMAP NAMESPACE client request. |
100 |
|
*/ |
101 |
|
int namespace_response(unsigned int tag, struct namespace_t * namesp) |
102 |
|
{ |
103 |
|
char buf[RESPONSE_BUF]; |
104 |
|
char *c, *d; |
105 |
|
|
106 |
|
reset_vbuf(); |
107 |
|
|
108 |
|
do { |
109 |
|
receive_response(buf); |
110 |
|
check_vbuf(strlen(buf)); |
111 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
112 |
|
} while (!strcasestr(buf, ultostr(tag, 16))); |
113 |
|
|
114 |
|
if ((c = strcasestr(vbuf, "* NAMESPACE"))) { |
115 |
|
c += 12; |
116 |
|
if (strncasecmp(c, "NIL", 3)) { |
117 |
|
c = strchr(c, '"') + 1; |
118 |
|
d = strchr(c, '"') + 1; |
119 |
|
|
120 |
|
strncat(namesp->prefix, c, d - c - 1); |
121 |
|
namesp->delim = *(strchr(d, '"') + 1); |
122 |
|
} |
123 |
|
} |
124 |
return analyze_response(buf); |
return analyze_response(buf); |
125 |
} |
} |
126 |
|
|
137 |
|
|
138 |
exist = recent = unseen = 0; |
exist = recent = unseen = 0; |
139 |
|
|
140 |
do |
reset_vbuf(); |
141 |
|
|
142 |
|
do { |
143 |
receive_response(buf); |
receive_response(buf); |
144 |
while (!strcasestr(buf, ultostr(tag, 16))); |
check_vbuf(strlen(buf)); |
145 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
146 |
|
} while (!strcasestr(buf, ultostr(tag, 16))); |
147 |
|
|
148 |
r = analyze_response(buf); |
r = analyze_response(buf); |
149 |
|
|
150 |
if (r == RESPONSE_NO) |
if (r == RESPONSE_NO) |
151 |
return -2; |
return -2; |
152 |
|
|
153 |
if ((c = strcasestr(buf, "MESSAGES"))) { |
if ((c = strcasestr(vbuf, "MESSAGES"))) { |
154 |
c += 9; |
c += 9; |
155 |
exist = strtoul(c, NULL, 10); |
exist = strtoul(c, NULL, 10); |
156 |
} |
} |
157 |
if ((c = strcasestr(buf, "RECENT"))) { |
if ((c = strcasestr(vbuf, "RECENT"))) { |
158 |
c += 7; |
c += 7; |
159 |
recent = strtoul(c, NULL, 10); |
recent = strtoul(c, NULL, 10); |
160 |
} |
} |
161 |
if ((c = strcasestr(buf, "UNSEEN"))) { |
if ((c = strcasestr(vbuf, "UNSEEN"))) { |
162 |
c += 7; |
c += 7; |
163 |
unseen = strtoul(c, NULL, 10); |
unseen = strtoul(c, NULL, 10); |
164 |
} |
} |
165 |
if (!exist) { |
if (!exist) { |
166 |
info("No messages in mailbox %s.\n", mbox); |
info("No messages in mailbox \"%s\".\n", mbox); |
167 |
return -2; |
return -2; |
168 |
} |
} |
169 |
info("%d message%s, %d recent, %d unseen, in mailbox %s.\n", exist, plural(exist), |
info("%d message%s, %d recent, %d unseen, in mailbox \"%s\".\n", exist, |
170 |
recent, unseen, mbox); |
plural(exist), recent, unseen, mbox); |
171 |
|
|
172 |
return r; |
return r; |
173 |
} |
} |
180 |
{ |
{ |
181 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
182 |
|
|
183 |
do |
reset_vbuf(); |
184 |
|
|
185 |
|
do { |
186 |
receive_response(buf); |
receive_response(buf); |
187 |
while (!strcasestr(buf, ultostr(tag, 16))); |
check_vbuf(strlen(buf)); |
188 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
189 |
|
} while (!strcasestr(buf, ultostr(tag, 16))); |
190 |
|
|
191 |
if (strcasestr(buf, "[READ-ONLY]")) |
if (strcasestr(vbuf, "[READ-ONLY]")) |
192 |
return RESPONSE_READONLY; |
return RESPONSE_READONLY; |
193 |
|
|
194 |
return analyze_response(buf); |
return analyze_response(buf); |
200 |
*/ |
*/ |
201 |
int search_response(unsigned int tag, char **mesgs) |
int search_response(unsigned int tag, char **mesgs) |
202 |
{ |
{ |
203 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
204 |
char *c, *m; |
char *c, *m; |
205 |
int f; |
unsigned int blen; |
|
unsigned int blen, mlen; |
|
|
|
|
|
f = blen = mlen = 0; |
|
206 |
|
|
207 |
|
reset_vbuf(); |
208 |
|
|
209 |
do { |
do { |
210 |
receive_response(buf); |
receive_response(buf); |
211 |
|
check_vbuf(strlen(buf)); |
212 |
if (f) { /* Search results are continued from |
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
|
previous packet. */ |
|
|
c = buf; |
|
|
blen = strlen(buf); |
|
|
mlen = strlen(*mesgs); |
|
|
|
|
|
*mesgs = (char *) xrealloc(*mesgs, mlen + blen + 1); |
|
|
m = *mesgs + mlen; |
|
|
|
|
|
while (*c && (isdigit(*c) || *c == ' ')) |
|
|
*(m++) = *(c++); |
|
|
*m = 0; |
|
|
} else if ((c = strcasestr(buf, "* SEARCH "))) { |
|
|
f = 1; |
|
|
blen = strlen(buf); |
|
|
|
|
|
m = *mesgs = (char *) xmalloc(blen + 1); |
|
|
c += 9; |
|
|
|
|
|
while (*c && (isdigit(*c) || *c == ' ')) |
|
|
*(m++) = *(c++); |
|
|
*m = 0; |
|
|
} |
|
213 |
} while (!strcasestr(buf, ultostr(tag, 16))); |
} while (!strcasestr(buf, ultostr(tag, 16))); |
214 |
|
|
215 |
|
if ((c = strcasestr(vbuf, "* SEARCH "))) { |
216 |
|
blen = strlen(vbuf); |
217 |
|
|
218 |
|
m = *mesgs = (char *) xmalloc(blen + 1); |
219 |
|
|
220 |
|
c += 9; |
221 |
|
|
222 |
|
while (*c && (isdigit(*c) || *c == ' ')) |
223 |
|
*(m++) = *(c++); |
224 |
|
|
225 |
|
*m = 0; |
226 |
|
} |
227 |
|
|
228 |
return analyze_response(buf); |
return analyze_response(buf); |
229 |
} |
} |
230 |
|
|
324 |
*/ |
*/ |
325 |
int copy_response(unsigned int tag) |
int copy_response(unsigned int tag) |
326 |
{ |
{ |
327 |
|
int r = RESPONSE_OK; |
328 |
char buf[RESPONSE_BUF]; |
char buf[RESPONSE_BUF]; |
329 |
|
|
330 |
do |
reset_vbuf(); |
331 |
|
|
332 |
|
do { |
333 |
receive_response(buf); |
receive_response(buf); |
334 |
while (!strcasestr(buf, ultostr(tag, 16))); |
check_vbuf(strlen(buf)); |
335 |
|
strncat(vbuf, buf, vbufs - strlen(vbuf)); |
336 |
|
} while (!strcasestr(buf, ultostr(tag, 16))); |
337 |
|
|
338 |
if (analyze_response(buf) == RESPONSE_NO && strcasestr(buf, "[TRYCREATE]")) |
if ((r = analyze_response(buf)) == RESPONSE_NO |
339 |
|
&& strcasestr(vbuf, "[TRYCREATE]")) |
340 |
return RESPONSE_TRYCREATE; |
return RESPONSE_TRYCREATE; |
341 |
|
|
342 |
return RESPONSE_OK; |
return r; |
343 |
} |
} |
344 |
|
|
345 |
|
|
374 |
|
|
375 |
return r; |
return r; |
376 |
} |
} |
377 |
|
|
378 |
|
|
379 |
|
/* |
380 |
|
* Initialize virtual buffer. |
381 |
|
*/ |
382 |
|
void init_vbuf(void) |
383 |
|
{ |
384 |
|
vbuf = xmalloc(4096); |
385 |
|
*vbuf = 0; |
386 |
|
vbufs = 4096; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
/* |
391 |
|
* Reset virtual buffer. |
392 |
|
*/ |
393 |
|
void reset_vbuf(void) |
394 |
|
{ |
395 |
|
*vbuf = 0; |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
/* |
400 |
|
* Check if virtual buffer is full and make it bigger. |
401 |
|
*/ |
402 |
|
void check_vbuf(size_t s) |
403 |
|
{ |
404 |
|
if (s + strlen(vbuf) >= vbufs) { |
405 |
|
vbufs += 4096; |
406 |
|
vbuf = xrealloc(vbuf, vbufs); |
407 |
|
} |
408 |
|
} |
409 |
|
|