/[imapfilter]/imapfilter/response.c
ViewVC logotype

Annotation of /imapfilter/response.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (hide annotations)
Tue Jun 18 21:21:47 2002 UTC (21 years, 10 months ago) by lefcha
Branch: MAIN
Changes since 1.19: +198 -144 lines
File MIME type: text/plain
Added rcopy/rmove support.

1 lefcha 1.1 #include <stdio.h>
2     #include <unistd.h>
3     #include <stdlib.h>
4 lefcha 1.9 #include <ctype.h>
5 lefcha 1.4 #include <errno.h>
6 lefcha 1.1 #include <string.h>
7     #include <sys/time.h>
8     #include <sys/types.h>
9     #include <regex.h>
10    
11     #include "config.h"
12     #include "imapfilter.h"
13    
14    
15 lefcha 1.20 extern int sockpri;
16 lefcha 1.1 extern unsigned int options;
17 lefcha 1.18 extern unsigned int capabilities;
18 lefcha 1.1
19 lefcha 1.20 static char *vbuf = NULL; /* Virtual buffer. */
20     static size_t vbufs = 0; /* Virtual buffer size. */
21    
22 lefcha 1.1
23     /*
24 lefcha 1.14 * Read one packet of data that the server sent.
25 lefcha 1.1 */
26 lefcha 1.20 void receive_response(int *sock, char *buf)
27 lefcha 1.1 {
28 lefcha 1.20 socket_read(sock, buf);
29 lefcha 1.1
30     #ifdef DEBUG
31 lefcha 1.14 printf("\n%s\n", buf);
32 lefcha 1.1 #endif
33 lefcha 1.11
34 lefcha 1.1 }
35    
36    
37     /*
38     * Get server response to client's request.
39     */
40 lefcha 1.20 int server_response(int *sock, unsigned int tag)
41 lefcha 1.1 {
42 lefcha 1.14 char buf[RESPONSE_BUF];
43 lefcha 1.6
44 lefcha 1.20 reset_vbuf();
45    
46     do {
47     receive_response(sock, buf);
48     check_vbuf(strlen(buf));
49     strncat(vbuf, buf, vbufs - strlen(vbuf));
50     } while (tag && !strcasestr(buf, ultostr(tag, 16)));
51 lefcha 1.6
52 lefcha 1.14 return analyze_response(buf);
53 lefcha 1.1 }
54    
55    
56     /*
57 lefcha 1.9 * Process the greeting that server sends during connection.
58     */
59 lefcha 1.20 int greeting_response(int *sock)
60 lefcha 1.9 {
61 lefcha 1.14 char buf[RESPONSE_BUF];
62 lefcha 1.9
63 lefcha 1.20 receive_response(sock, buf);
64 lefcha 1.9
65 lefcha 1.14 if (strcasestr(buf, "BYE"))
66 lefcha 1.9 return RESPONSE_BYE;
67 lefcha 1.14 else if (strcasestr(buf, "PREAUTH"))
68     return RESPONSE_PREAUTH;
69 lefcha 1.9
70     return RESPONSE_OK;
71     }
72    
73 lefcha 1.13
74 lefcha 1.9 /*
75     * Process the data that server sent due to IMAP CAPABILITY client request.
76     */
77 lefcha 1.20 int capability_response(int *sock, unsigned int tag)
78 lefcha 1.9 {
79 lefcha 1.14 char buf[RESPONSE_BUF];
80 lefcha 1.20
81     reset_vbuf();
82 lefcha 1.9
83 lefcha 1.20 do {
84     receive_response(sock, buf);
85     check_vbuf(strlen(buf));
86     strncat(vbuf, buf, vbufs - strlen(vbuf));
87     } while (!strcasestr(buf, ultostr(tag, 16)));
88 lefcha 1.9
89 lefcha 1.20 if (!strcasestr(vbuf, "IMAP4rev1")) {
90 lefcha 1.9 error("imapfilter: server does not support IMAP4rev1 protocol\n");
91     return -2;
92     }
93 lefcha 1.20 if (strcasestr(vbuf, "NAMESPACE"))
94 lefcha 1.18 capabilities |= CAPABILITY_NAMESPACE;
95    
96     return analyze_response(buf);
97     }
98    
99    
100     /*
101     * Process the data that server sent due to IMAP NAMESPACE client request.
102     */
103 lefcha 1.20 int namespace_response(int *sock, unsigned int tag, namesp_t *nsp)
104 lefcha 1.18 {
105     char buf[RESPONSE_BUF];
106     char *c, *d;
107    
108 lefcha 1.20 reset_vbuf();
109    
110     do {
111     check_vbuf(strlen(buf));
112     strncat(vbuf, buf, vbufs - strlen(vbuf));
113     receive_response(sock, buf);
114     } while (!strcasestr(buf, ultostr(tag, 16)));
115 lefcha 1.18
116 lefcha 1.20 if ((c = strcasestr(vbuf, "* NAMESPACE"))) {
117 lefcha 1.18 c += 12;
118     if (strncasecmp(c, "NIL", 3)) {
119     c = strchr(c, '"') + 1;
120     d = strchr(c, '"') + 1;
121    
122 lefcha 1.20 strncat(nsp->prefix, c, d - c - 1);
123     nsp->delim = *(strchr(d, '"') + 1);
124 lefcha 1.18 }
125     }
126 lefcha 1.20 #ifdef DEBUG
127     printf("debug: namespace: '%s' '%c'\n", nsp->prefix, nsp->delim);
128     #endif
129 lefcha 1.14 return analyze_response(buf);
130 lefcha 1.9 }
131    
132    
133     /*
134 lefcha 1.3 * Process the data that server sent due to IMAP STATUS client request.
135 lefcha 1.1 */
136 lefcha 1.20 int status_response(int *sock, unsigned int tag, char *mbox)
137 lefcha 1.1 {
138 lefcha 1.17 int r;
139 lefcha 1.14 char buf[RESPONSE_BUF];
140 lefcha 1.1 unsigned int exist, recent, unseen;
141 lefcha 1.9 char *c;
142 lefcha 1.1
143     exist = recent = unseen = 0;
144 lefcha 1.20
145     reset_vbuf();
146 lefcha 1.1
147 lefcha 1.20 do {
148     receive_response(sock, buf);
149     check_vbuf(strlen(buf));
150     strncat(vbuf, buf, vbufs - strlen(vbuf));
151     } while (!strcasestr(buf, ultostr(tag, 16)));
152 lefcha 1.13
153 lefcha 1.17 r = analyze_response(buf);
154    
155     if (r == RESPONSE_NO)
156     return -2;
157    
158 lefcha 1.20 if ((c = strcasestr(vbuf, "MESSAGES"))) {
159 lefcha 1.9 c += 9;
160     exist = strtoul(c, NULL, 10);
161     }
162 lefcha 1.20 if ((c = strcasestr(vbuf, "RECENT"))) {
163 lefcha 1.9 c += 7;
164     recent = strtoul(c, NULL, 10);
165     }
166 lefcha 1.20 if ((c = strcasestr(vbuf, "UNSEEN"))) {
167 lefcha 1.9 c += 7;
168     unseen = strtoul(c, NULL, 10);
169     }
170 lefcha 1.8 if (!exist) {
171 lefcha 1.19 info("No messages in mailbox \"%s\".\n", mbox);
172 lefcha 1.8 return -2;
173     }
174 lefcha 1.19 info("%d message%s, %d recent, %d unseen, in mailbox \"%s\".\n", exist,
175 lefcha 1.18 plural(exist), recent, unseen, mbox);
176 lefcha 1.9
177 lefcha 1.17 return r;
178 lefcha 1.9 }
179    
180    
181     /*
182     * Process the data that server sent due to IMAP SELECT client request.
183     */
184 lefcha 1.20 int select_response(int *sock, unsigned int tag)
185 lefcha 1.9 {
186 lefcha 1.14 char buf[RESPONSE_BUF];
187 lefcha 1.9
188 lefcha 1.20 reset_vbuf();
189    
190     do {
191     receive_response(sock, buf);
192     check_vbuf(strlen(buf));
193     strncat(vbuf, buf, vbufs - strlen(vbuf));
194     } while (!strcasestr(buf, ultostr(tag, 16)));
195 lefcha 1.9
196 lefcha 1.20 if (strcasestr(vbuf, "[READ-ONLY]"))
197 lefcha 1.9 return RESPONSE_READONLY;
198 lefcha 1.1
199 lefcha 1.14 return analyze_response(buf);
200 lefcha 1.1 }
201    
202    
203     /*
204 lefcha 1.3 * Process the data that server sent due to IMAP SEARCH client request.
205 lefcha 1.1 */
206 lefcha 1.20 int search_response(int *sock, unsigned int tag, char **mesgs)
207 lefcha 1.1 {
208 lefcha 1.14 char buf[RESPONSE_BUF];
209 lefcha 1.13 char *c, *m;
210 lefcha 1.20 unsigned int blen;
211 lefcha 1.13
212 lefcha 1.20 reset_vbuf();
213    
214     do {
215     receive_response(sock, buf);
216     check_vbuf(strlen(buf));
217     strncat(vbuf, buf, vbufs - strlen(vbuf));
218     } while (!strcasestr(buf, ultostr(tag, 16)));
219 lefcha 1.1
220 lefcha 1.20 if ((c = strcasestr(vbuf, "* SEARCH "))) {
221     blen = strlen(vbuf);
222    
223     m = *mesgs = (char *) xmalloc(blen + 1);
224    
225     c += 9;
226    
227     while (*c && (isdigit(*c) || *c == ' '))
228     *(m++) = *(c++);
229 lefcha 1.14
230 lefcha 1.20 *m = 0;
231     }
232 lefcha 1.1
233 lefcha 1.14 return analyze_response(buf);
234 lefcha 1.1 }
235    
236    
237     /*
238 lefcha 1.3 * Process the data that server sent due to IMAP FETCH client request.
239 lefcha 1.1 */
240 lefcha 1.20 int fetch_response(int *sock, int reset, char *fetch, unsigned int tag)
241 lefcha 1.1 {
242 lefcha 1.14 char buf[RESPONSE_BUF];
243 lefcha 1.20 unsigned int i;
244     static unsigned int s;
245 lefcha 1.15 char *b;
246 lefcha 1.14
247 lefcha 1.20 if (reset) {
248     s = 0;
249     return 0;
250     }
251    
252     i = 0;
253    
254     receive_response(sock, buf);
255    
256     b = buf;
257    
258     if (!s) {
259     b = strchr(b, '{');
260     b++;
261     s = atoi(b) - 2;
262     b = strchr(b, '}');
263     b += 3;
264     }
265    
266     while (*b && s--)
267     fetch[i++] = *(b++);
268    
269     fetch[i] = 0;
270    
271     return analyze_response(buf);
272     }
273    
274 lefcha 1.6
275 lefcha 1.20 /*
276     * Process the data that server sent due to IMAP FETCH RFC822.SIZE client
277     * request.
278     */
279     int fetchsize_response(int *sock, unsigned int *size, unsigned int tag)
280     {
281     char buf[RESPONSE_BUF];
282     char *c;
283    
284     *size = 0;
285    
286     reset_vbuf();
287    
288 lefcha 1.13 do {
289 lefcha 1.20 receive_response(sock, buf);
290     check_vbuf(strlen(buf));
291     strncat(vbuf, buf, vbufs - strlen(vbuf));
292     } while (!strcasestr(buf, ultostr(tag, 16)));
293    
294     if ((c = strcasestr(vbuf, "FETCH (RFC822.SIZE "))) {
295     c += 19;
296     *size = strtoul(c, NULL, 10);
297     }
298    
299     return analyze_response(buf);
300     }
301 lefcha 1.13
302 lefcha 1.6
303 lefcha 1.20 /*
304     * Process the data that server sent due to IMAP APPEND client request.
305     */
306     int append_response(int *sock, unsigned int tag)
307     {
308     int r = RESPONSE_OK;
309     char buf[RESPONSE_BUF];
310    
311     reset_vbuf();
312    
313     do {
314     receive_response(sock, buf);
315     check_vbuf(strlen(buf));
316     strncat(vbuf, buf, vbufs - strlen(vbuf));
317 lefcha 1.14 } while (!strcasestr(buf, ultostr(tag, 16)));
318 lefcha 1.20
319     if ((r = analyze_response(buf)) == RESPONSE_NO &&
320     strcasestr(vbuf, "[TRYCREATE]"))
321     return RESPONSE_TRYCREATE;
322 lefcha 1.6
323 lefcha 1.20 return r;
324 lefcha 1.1 }
325    
326    
327     /*
328 lefcha 1.3 * Process the data that server sent due to IMAP COPY client request.
329 lefcha 1.1 */
330 lefcha 1.20 int copy_response(int *sock, unsigned int tag)
331 lefcha 1.1 {
332 lefcha 1.19 int r = RESPONSE_OK;
333 lefcha 1.20 char buf[RESPONSE_BUF];
334 lefcha 1.19
335 lefcha 1.20 reset_vbuf();
336 lefcha 1.1
337 lefcha 1.20 do {
338     receive_response(sock, buf);
339     check_vbuf(strlen(buf));
340     strncat(vbuf, buf, vbufs - strlen(vbuf));
341     } while (!strcasestr(buf, ultostr(tag, 16)));
342 lefcha 1.1
343 lefcha 1.19 if ((r = analyze_response(buf)) == RESPONSE_NO &&
344 lefcha 1.20 strcasestr(vbuf, "[TRYCREATE]"))
345 lefcha 1.9 return RESPONSE_TRYCREATE;
346 lefcha 1.1
347 lefcha 1.19 return r;
348 lefcha 1.1 }
349    
350    
351     /*
352 lefcha 1.3 * Check if response of server to client's request was succesfully
353 lefcha 1.1 * delivered or there was some kind of error.
354     */
355 lefcha 1.14 int analyze_response(char *buf)
356 lefcha 1.1 {
357 lefcha 1.9 int r = RESPONSE_OK;
358 lefcha 1.1 regex_t creg;
359     regmatch_t match[3];
360 lefcha 1.9 const char *reg = "[[:xdigit:]]{6,6} ((OK|NO|BAD)[[:print:]]*)\r\n";
361 lefcha 1.14 char result[RESULT_BUF];
362 lefcha 1.1
363 lefcha 1.4 result[0] = 0;
364    
365 lefcha 1.9 regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
366 lefcha 1.6
367 lefcha 1.14 if (!regexec(&creg, buf, 3, match, 0)) {
368     strncat(result, buf + match[1].rm_so,
369     min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1));
370 lefcha 1.1
371 lefcha 1.14 if (!strncasecmp(buf + match[2].rm_so, "NO", 2))
372 lefcha 1.9 r = RESPONSE_NO;
373 lefcha 1.14 else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3))
374 lefcha 1.9 r = RESPONSE_BAD;
375 lefcha 1.6
376 lefcha 1.1 verbose("Server response: %s\n", result);
377 lefcha 1.20 } else
378     r = RESPONSE_NONE;
379    
380 lefcha 1.6 regfree(&creg);
381 lefcha 1.1
382     return r;
383 lefcha 1.20 }
384    
385    
386     /*
387     * Initialize virtual buffer.
388     */
389     void init_vbuf(void)
390     {
391     vbuf = xmalloc(4096);
392     *vbuf = 0;
393     vbufs = 4096;
394     }
395    
396    
397     /*
398     * Reset virtual buffer.
399     */
400     void reset_vbuf(void)
401     {
402     *vbuf = 0;
403     }
404    
405    
406     /*
407     * Check if virtual buffer is full and make it bigger.
408     */
409     void check_vbuf(size_t n)
410     {
411     if (n + strlen(vbuf) >= vbufs) {
412     vbufs += 4096;
413     vbuf = xrealloc(vbuf, vbufs);
414     }
415 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26