]> Pileus Git - ~andy/fetchmail/blob - imap.c
Remove some obsolete constructs...
[~andy/fetchmail] / imap.c
1 /*
2  * imap.c -- IMAP2bis/IMAP4 protocol methods
3  *
4  * Copyright 1997 by Eric S. Raymond
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include  "config.h"
9 #include  <stdio.h>
10 #include  <string.h>
11 #include  <strings.h>
12 #include  <ctype.h>
13 #include  <stdlib.h>
14 #include  <limits.h>
15 #include  <errno.h>
16 #include  "fetchmail.h"
17 #include  "socket.h"
18
19 #include  "i18n.h"
20
21 /* imap_version values */
22 #define IMAP2           -1      /* IMAP2 or IMAP2BIS, RFC1176 */
23 #define IMAP4           0       /* IMAP4 rev 0, RFC1730 */
24 #define IMAP4rev1       1       /* IMAP4 rev 1, RFC2060 */
25
26 /* global variables: please reinitialize them explicitly for proper
27  * working in daemon mode */
28
29 /* TODO: session variables to be initialized before server greeting */
30 static int preauth = FALSE;
31
32 /* session variables initialized in capa_probe() or imap_getauth() */
33 static char capabilities[MSGBUFSIZE+1];
34 static int imap_version = IMAP4;
35 static flag do_idle = FALSE, has_idle = FALSE;
36 static int expunge_period = 1;
37
38 /* mailbox variables initialized in imap_getrange() */
39 static int count = 0, oldcount = 0, recentcount = 0, unseen = 0, deletions = 0;
40 static unsigned int startcount = 1;
41 static int expunged = 0;
42 static unsigned int *unseen_messages;
43
44 /* for "IMAP> EXPUNGE" */
45 static int actual_deletions = 0;
46
47 /* for "IMAP> IDLE" */
48 static int saved_timeout = 0, idle_timeout = 0;
49 static time_t idle_start_time = 0;
50
51 static int imap_untagged_response(int sock, const char *buf)
52 /* interpret untagged status responses */
53 {
54     /* For each individual check, use a BLANK before the word to avoid
55      * confusion with the \Recent flag or similar */
56     if (stage == STAGE_GETAUTH
57             && !strncmp(buf, "* CAPABILITY", 12))
58     {
59         strlcpy(capabilities, buf + 12, sizeof(capabilities));
60     }
61     else if (stage == STAGE_GETAUTH
62             && !strncmp(buf, "* PREAUTH", 9))
63     {
64         preauth = TRUE;
65     }
66     else if (stage != STAGE_LOGOUT
67             && !strncmp(buf, "* BYE", 5))
68     {
69         /* log the unexpected bye from server as we expect the
70          * connection to be cut-off after this */
71         if (outlevel > O_SILENT)
72             report(stderr, GT_("Received BYE response from IMAP server: %s"), buf + 5);
73     }
74     else if (strstr(buf, " EXISTS"))
75     {
76         char *t; unsigned long u;
77         errno = 0;
78         u = strtoul(buf+2, &t, 10);
79         /*
80          * Don't trust the message count passed by the server.
81          * Without this check, it might be possible to do a
82          * DNS-spoofing attack that would pass back a ridiculous 
83          * count, and allocate a malloc area that would overlap
84          * a portion of the stack.
85          */
86         if (errno /* strtoul failed */
87                 || t == buf+2 /* no valid data */
88                 || u > (unsigned long)(INT_MAX/sizeof(int)) /* too large */)
89         {
90             report(stderr, GT_("bogus message count in \"%s\"!"), buf);
91             return(PS_PROTOCOL);
92         }
93         count = u; /* safe as long as count <= INT_MAX - checked above */
94
95         if ((recentcount = count - oldcount) < 0)
96             recentcount = 0;
97
98         /*
99          * Nasty kluge to handle RFC2177 IDLE.  If we know we're idling
100          * we can't wait for the tag matching the IDLE; we have to tell the
101          * server the IDLE is finished by shipping back a DONE when we
102          * see an EXISTS.  Only after that will a tagged response be
103          * shipped.  The idling flag also gets cleared on a timeout.
104          */
105         if (stage == STAGE_IDLE)
106         {
107             /* If IDLE isn't supported, we were only sending NOOPs anyway. */
108             if (has_idle)
109             {
110                 /* we do our own write and report here to disable tagging */
111                 SockWrite(sock, "DONE\r\n", 6);
112                 if (outlevel >= O_MONITOR)
113                     report(stdout, "IMAP> DONE\n");
114             }
115
116             mytimeout = saved_timeout;
117             stage = STAGE_GETRANGE;
118         }
119     }
120     /* we now compute recentcount as a difference between
121      * new and old EXISTS, hence disable RECENT check */
122 # if 0
123     else if (strstr(buf, " RECENT"))
124     {
125         /* fixme: use strto[u]l and error checking */
126         recentcount = atoi(buf+2);
127     }
128 # endif
129     else if (strstr(buf, " EXPUNGE"))
130     {
131         unsigned long u; char *t;
132         /* the response "* 10 EXPUNGE" means that the currently
133          * tenth (i.e. only one) message has been deleted */
134         errno = 0;
135         u = strtoul(buf+2, &t, 10);
136         if (errno /* conversion error */ || t == buf+2 /* no number found */) {
137             report(stderr, GT_("bogus EXPUNGE count in \"%s\"!"), buf);
138             return PS_PROTOCOL;
139         }
140         if (u > 0)
141         {
142             if (count > 0)
143                 count--;
144             if (oldcount > 0)
145                 oldcount--;
146             /* We do expect an EXISTS response immediately
147              * after this, so this updation of recentcount is
148              * just a precaution! */
149             if ((recentcount = count - oldcount) < 0)
150                 recentcount = 0;
151             actual_deletions++;
152         }
153     }
154     /*
155      * The server may decide to make the mailbox read-only, 
156      * which causes fetchmail to go into a endless loop
157      * fetching the same message over and over again. 
158      * 
159      * However, for check_only, we use EXAMINE which will
160      * mark the mailbox read-only as per the RFC.
161      * 
162      * This checks for the condition and aborts if 
163      * the mailbox is read-only. 
164      *
165      * See RFC 2060 section 6.3.1 (SELECT).
166      * See RFC 2060 section 6.3.2 (EXAMINE).
167      */ 
168     else if (stage == STAGE_GETRANGE
169             && !check_only && strstr(buf, "[READ-ONLY]"))
170     {
171         return(PS_LOCKBUSY);
172     }
173     else
174     {
175         return(PS_UNTAGGED);
176     }
177     return(PS_SUCCESS);
178 }
179
180 static int imap_response(int sock, char *argbuf)
181 /* parse command response */
182 {
183     char buf[MSGBUFSIZE+1];
184
185     do {
186         int     ok;
187         char    *cp;
188
189         if ((ok = gen_recv(sock, buf, sizeof(buf))))
190             return(ok);
191
192         /* all tokens in responses are caseblind */
193         for (cp = buf; *cp; cp++)
194             if (islower((unsigned char)*cp))
195                 *cp = toupper((unsigned char)*cp);
196
197         /* untagged responses start with "* " */
198         if (buf[0] == '*' && buf[1] == ' ') {
199             ok = imap_untagged_response(sock, buf);
200             if (ok == PS_UNTAGGED)
201             {
202                 if (argbuf && stage != STAGE_IDLE && tag[0] != '\0')
203                 {
204                     /* if there is an unmatched response, pass it back to
205                      * the calling function for further analysis. The
206                      * calling function should call imap_response() again
207                      * to read the remaining response */
208                     strcpy(argbuf, buf);
209                     return(ok);
210                 }
211             }
212             else if (ok != PS_SUCCESS)
213                 return(ok);
214         }
215
216         if (stage == STAGE_IDLE)
217         {
218             /* reduce the timeout: servers may not reset their timeout
219              * when they send some information asynchronously */
220             mytimeout = idle_timeout - (time((time_t *) NULL) - idle_start_time);
221             if (mytimeout <= 0)
222                 return(PS_IDLETIMEOUT);
223         }
224     } while
225         (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
226
227     if (tag[0] == '\0')
228     {
229         if (argbuf)
230             strcpy(argbuf, buf);
231         return(PS_SUCCESS); 
232     }
233     else
234     {
235         char    *cp;
236
237         /* skip the tag */
238         for (cp = buf; !isspace((unsigned char)*cp); cp++)
239             continue;
240         while (isspace((unsigned char)*cp))
241             cp++;
242
243         if (strncasecmp(cp, "OK", 2) == 0)
244         {
245             if (argbuf)
246                 strcpy(argbuf, cp);
247             return(PS_SUCCESS);
248         }
249         else if (strncasecmp(cp, "BAD", 3) == 0)
250             return(PS_ERROR);
251         else if (strncasecmp(cp, "NO", 2) == 0)
252         {
253             if (stage == STAGE_GETAUTH) 
254                 return(PS_AUTHFAIL);    /* RFC2060, 6.2.2 */
255             else if (stage == STAGE_GETSIZES)
256                 return(PS_SUCCESS);     /* see comments in imap_getpartialsizes() */
257             else
258                 return(PS_ERROR);
259         }
260         else
261             return(PS_PROTOCOL);
262     }
263 }
264
265 static int imap_ok(int sock, char *argbuf)
266 /* parse command response */
267 {
268     int ok;
269
270     while ((ok = imap_response(sock, argbuf)) == PS_UNTAGGED)
271         ; /* wait for the tagged response */
272     return(ok);
273 }
274
275 #ifdef NTLM_ENABLE
276 #include "ntlm.h"
277
278 /*
279  * NTLM support by Grant Edwards.
280  *
281  * Handle MS-Exchange NTLM authentication method.  This is the same
282  * as the NTLM auth used by Samba for SMB related services. We just
283  * encode the packets in base64 instead of sending them out via a
284  * network interface.
285  * 
286  * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
287  */
288
289 static int do_imap_ntlm(int sock, struct query *ctl)
290 {
291     int result;
292
293     gen_send(sock, "AUTHENTICATE NTLM");
294
295     if ((result = ntlm_helper(sock, ctl, "IMAP")))
296         return result;
297
298     result = imap_ok (sock, NULL);
299     if (result == PS_SUCCESS)
300         return PS_SUCCESS;
301     else
302         return PS_AUTHFAIL;
303 }
304 #endif /* NTLM */
305
306 static void imap_canonicalize(char *result, char *raw, size_t maxlen)
307 /* encode an IMAP password as per RFC1730's quoting conventions */
308 {
309     size_t i, j;
310
311     j = 0;
312     for (i = 0; i < strlen(raw) && i < maxlen; i++)
313     {
314         if ((raw[i] == '\\') || (raw[i] == '"'))
315             result[j++] = '\\';
316         result[j++] = raw[i];
317     }
318     result[j] = '\0';
319 }
320
321 static void capa_probe(int sock, struct query *ctl)
322 /* set capability variables from a CAPA probe */
323 {
324     int ok;
325
326     /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
327     capabilities[0] = '\0';
328     if ((ok = gen_transact(sock, "CAPABILITY")) == PS_SUCCESS)
329     {
330         char    *cp;
331
332         /* capability checks are supposed to be caseblind */
333         for (cp = capabilities; *cp; cp++)
334             *cp = toupper((unsigned char)*cp);
335
336         /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
337            should expect a response in mixed-case */
338         if (strstr(capabilities, "IMAP4REV1"))
339         {
340             imap_version = IMAP4rev1;
341             if (outlevel >= O_DEBUG)
342                 report(stdout, GT_("Protocol identified as IMAP4 rev 1\n"));
343         }
344         else
345         {
346             imap_version = IMAP4;
347             if (outlevel >= O_DEBUG)
348                 report(stdout, GT_("Protocol identified as IMAP4 rev 0\n"));
349         }
350     }
351     else if (ok == PS_ERROR)
352     {
353         imap_version = IMAP2;
354         if (outlevel >= O_DEBUG)
355             report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
356     }
357
358     /* 
359      * Handle idling.  We depend on coming through here on startup
360      * and after each timeout (including timeouts during idles).
361      */
362     do_idle = ctl->idle;
363     if (ctl->idle)
364     {
365         if (strstr(capabilities, "IDLE"))
366             has_idle = TRUE;
367         else
368             has_idle = FALSE;
369         if (outlevel >= O_VERBOSE)
370             report(stdout, GT_("will idle after poll\n"));
371     }
372
373     peek_capable = (imap_version >= IMAP4);
374 }
375
376 static int do_authcert (int sock, const char *command, const char *name)
377 /* do authentication "external" (authentication provided by client cert) */
378 {
379     char buf[256];
380
381     if (name && name[0])
382     {
383         size_t len = strlen(name);
384         if ((len / 3) + ((len % 3) ? 4 : 0)  < sizeof(buf))
385             to64frombits (buf, name, strlen(name));
386         else
387             return PS_AUTHFAIL; /* buffer too small. */
388     }
389     else
390         buf[0]=0;
391     return gen_transact(sock, "%s EXTERNAL %s",command,buf);
392 }
393
394 static int imap_getauth(int sock, struct query *ctl, char *greeting)
395 /* apply for connection authorization */
396 {
397     int ok = 0;
398     (void)greeting;
399
400     /*
401      * Assumption: expunges are cheap, so we want to do them
402      * after every message unless user said otherwise.
403      */
404     if (NUM_SPECIFIED(ctl->expunge))
405         expunge_period = NUM_VALUE_OUT(ctl->expunge);
406     else
407         expunge_period = 1;
408
409     capa_probe(sock, ctl);
410
411     /* 
412      * If either (a) we saw a PREAUTH token in the greeting, or
413      * (b) the user specified ssh preauthentication, then we're done.
414      */
415     if (preauth || ctl->server.authenticate == A_SSH)
416     {
417         preauth = FALSE;  /* reset for the next session */
418         return(PS_SUCCESS);
419     }
420
421 #ifdef SSL_ENABLE
422     if (maybe_tls(ctl)) {
423         char *commonname;
424
425         commonname = ctl->server.pollname;
426         if (ctl->server.via)
427             commonname = ctl->server.via;
428         if (ctl->sslcommonname)
429             commonname = ctl->sslcommonname;
430
431         if (strstr(capabilities, "STARTTLS")
432                 || must_tls(ctl)) /* if TLS is mandatory, ignore capabilities */
433         {
434             /* Use "tls1" rather than ctl->sslproto because tls1 is the only
435              * protocol that will work with STARTTLS.  Don't need to worry
436              * whether TLS is mandatory or opportunistic unless SSLOpen() fails
437              * (see below). */
438             if (gen_transact(sock, "STARTTLS") == PS_SUCCESS
439                     && SSLOpen(sock, ctl->sslcert, ctl->sslkey, "tls1", ctl->sslcertck,
440                         ctl->sslcertfile, ctl->sslcertpath, ctl->sslfingerprint, commonname,
441                         ctl->server.pollname, &ctl->remotename) != -1)
442             {
443                 /*
444                  * RFC 2595 says this:
445                  *
446                  * "Once TLS has been started, the client MUST discard cached
447                  * information about server capabilities and SHOULD re-issue the
448                  * CAPABILITY command.  This is necessary to protect against
449                  * man-in-the-middle attacks which alter the capabilities list prior
450                  * to STARTTLS.  The server MAY advertise different capabilities
451                  * after STARTTLS."
452                  *
453                  * Now that we're confident in our TLS connection we can
454                  * guarantee a secure capability re-probe.
455                  */
456                 capa_probe(sock, ctl);
457                 if (outlevel >= O_VERBOSE)
458                 {
459                     report(stdout, GT_("%s: upgrade to TLS succeeded.\n"), commonname);
460                 }
461             } else if (must_tls(ctl)) {
462                 /* Config required TLS but we couldn't guarantee it, so we must
463                  * stop. */
464                 report(stderr, GT_("%s: upgrade to TLS failed.\n"), commonname);
465                 return PS_SOCKET;
466             } else {
467                 if (outlevel >= O_VERBOSE) {
468                     report(stdout, GT_("%s: opportunistic upgrade to TLS failed, trying to continue\n"), commonname);
469                 }
470                 /* We don't know whether the connection is in a working state, so
471                  * test by issuing a NOOP. */
472                 if (gen_transact(sock, "NOOP") != PS_SUCCESS) {
473                     /* Not usable.  Empty sslproto to force an unencrypted
474                      * connection on the next attempt, and repoll. */
475                     ctl->sslproto = xstrdup("");
476                     return PS_REPOLL;
477                 }
478                 /* Usable.  Proceed with authenticating insecurely. */
479             }
480         }
481     }
482 #endif /* SSL_ENABLE */
483
484     /*
485      * Time to authenticate the user.
486      * Try the protocol variants that don't require passwords first.
487      */
488     ok = PS_AUTHFAIL;
489
490     /* Yahoo hack - we'll just try ID if it was offered by the server,
491      * and IGNORE errors. */
492     {
493         char *tmp = strstr(capabilities, " ID");
494         if (tmp && !isalnum((unsigned char)tmp[3]) && strstr(ctl->server.via ? ctl->server.via : ctl->server.pollname, "yahoo.com")) {
495                 (void)gen_transact(sock, "ID (\"guid\" \"1\")");
496         }
497     }
498
499     if ((ctl->server.authenticate == A_ANY 
500          || ctl->server.authenticate == A_EXTERNAL)
501         && strstr(capabilities, "AUTH=EXTERNAL"))
502     {
503         ok = do_authcert(sock, "AUTHENTICATE", ctl->remotename);
504         if (ok)
505         {
506             /* SASL cancellation of authentication */
507             gen_send(sock, "*");
508             if (ctl->server.authenticate != A_ANY)
509                 return ok;
510         } else {
511             return ok;
512         }
513     }
514
515 #ifdef GSSAPI
516     if (((ctl->server.authenticate == A_ANY && check_gss_creds("imap", ctl->server.truename) == PS_SUCCESS)
517          || ctl->server.authenticate == A_GSSAPI)
518         && strstr(capabilities, "AUTH=GSSAPI"))
519     {
520         if ((ok = do_gssauth(sock, "AUTHENTICATE", "imap",
521                         ctl->server.truename, ctl->remotename)))
522         {
523             if (ctl->server.authenticate != A_ANY)
524                 return ok;
525         } else  {
526             return ok;
527         }
528     }
529 #endif /* GSSAPI */
530
531 #ifdef KERBEROS_V4
532     if ((ctl->server.authenticate == A_ANY 
533          || ctl->server.authenticate == A_KERBEROS_V4
534          || ctl->server.authenticate == A_KERBEROS_V5) 
535         && strstr(capabilities, "AUTH=KERBEROS_V4"))
536     {
537         if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
538         {
539             /* SASL cancellation of authentication */
540             gen_send(sock, "*");
541             if(ctl->server.authenticate != A_ANY)
542                 return ok;
543         }
544         else
545             return ok;
546     }
547 #endif /* KERBEROS_V4 */
548
549     /*
550      * No such luck.  OK, now try the variants that mask your password
551      * in a challenge-response.
552      */
553
554     if ((ctl->server.authenticate == A_ANY && strstr(capabilities, "AUTH=CRAM-MD5"))
555         || ctl->server.authenticate == A_CRAM_MD5)
556     {
557         if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
558         {
559             if(ctl->server.authenticate != A_ANY)
560                 return ok;
561         }
562         else
563             return ok;
564     }
565
566 #ifdef OPIE_ENABLE
567     if ((ctl->server.authenticate == A_ANY 
568          || ctl->server.authenticate == A_OTP)
569         && strstr(capabilities, "AUTH=X-OTP")) {
570         if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
571         {
572             /* SASL cancellation of authentication */
573             gen_send(sock, "*");
574             if(ctl->server.authenticate != A_ANY)
575                 return ok;
576         } else {
577             return ok;
578         }
579     }
580 #else
581     if (ctl->server.authenticate == A_OTP)
582     {
583         report(stderr, 
584            GT_("Required OTP capability not compiled into fetchmail\n"));
585     }
586 #endif /* OPIE_ENABLE */
587
588 #ifdef NTLM_ENABLE
589     if ((ctl->server.authenticate == A_ANY 
590          || ctl->server.authenticate == A_NTLM) 
591         && strstr (capabilities, "AUTH=NTLM")) {
592         if ((ok = do_imap_ntlm(sock, ctl)))
593         {
594             if(ctl->server.authenticate != A_ANY)
595                 return ok;
596         }
597         else
598             return(ok);
599     }
600 #else
601     if (ctl->server.authenticate == A_NTLM)
602     {
603         report(stderr, 
604            GT_("Required NTLM capability not compiled into fetchmail\n"));
605     }
606 #endif /* NTLM_ENABLE */
607
608 #ifdef __UNUSED__       /* The Cyrus IMAP4rev1 server chokes on this */
609     /* this handles either AUTH=LOGIN or AUTH-LOGIN */
610     if ((imap_version >= IMAP4rev1) && (!strstr(capabilities, "LOGIN")))
611     {
612         report(stderr, 
613                GT_("Required LOGIN capability not supported by server\n"));
614     }
615 #endif /* __UNUSED__ */
616
617     /* 
618      * We're stuck with sending the password en clair.
619      * The reason for this odd-looking logic is that some
620      * servers return LOGINDISABLED even though login 
621      * actually works.  So arrange things in such a way that
622      * setting auth passwd makes it ignore this capability.
623      */
624     if((ctl->server.authenticate==A_ANY&&!strstr(capabilities,"LOGINDISABLED"))
625         || ctl->server.authenticate == A_PASSWORD)
626     {
627         /* these sizes guarantee no buffer overflow */
628         char *remotename, *password;
629         size_t rnl, pwl;
630         rnl = 2 * strlen(ctl->remotename) + 1;
631         pwl = 2 * strlen(ctl->password) + 1;
632         remotename = (char *)xmalloc(rnl);
633         password = (char *)xmalloc(pwl);
634
635         imap_canonicalize(remotename, ctl->remotename, rnl);
636         imap_canonicalize(password, ctl->password, pwl);
637
638         snprintf(shroud, sizeof (shroud), "\"%s\"", password);
639         ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
640         memset(shroud, 0x55, sizeof(shroud));
641         shroud[0] = '\0';
642         memset(password, 0x55, strlen(password));
643         free(password);
644         free(remotename);
645         if (ok)
646         {
647             if(ctl->server.authenticate != A_ANY)
648                 return ok;
649         }
650         else
651             return(ok);
652     }
653
654     return(ok);
655 }
656
657 static int internal_expunge(int sock)
658 /* ship an expunge, resetting associated counters */
659 {
660     int ok;
661
662     actual_deletions = 0;
663
664     if ((ok = gen_transact(sock, "EXPUNGE")))
665         return(ok);
666
667     /* if there is a mismatch between the number of mails which should
668      * have been expunged and the number of mails actually expunged,
669      * another email client may be deleting mails. Quit here,
670      * otherwise fetchmail gets out-of-sync with the imap server,
671      * reports the wrong size to the SMTP server on MAIL FROM: and
672      * triggers a "message ... was not the expected length" error on
673      * every subsequent mail */
674     if (deletions > 0 && deletions != actual_deletions)
675     {
676         report(stderr,
677                 GT_("mail expunge mismatch (%d actual != %d expected)\n"),
678                 actual_deletions, deletions);
679         deletions = 0;
680         return(PS_ERROR);
681     }
682
683     expunged += deletions;
684     deletions = 0;
685
686 #ifdef IMAP_UID /* not used */
687     expunge_uids(ctl);
688 #endif /* IMAP_UID */
689
690     return(PS_SUCCESS);
691 }
692
693 static int imap_idle(int sock)
694 /* start an RFC2177 IDLE, or fake one if unsupported */
695 {
696     int ok;
697
698     saved_timeout = mytimeout;
699
700     if (has_idle) {
701         /* special timeout to terminate the IDLE and re-issue it
702          * at least every 28 minutes:
703          * (the server may have an inactivity timeout) */
704         mytimeout = idle_timeout = 1680; /* 28 min */
705         time(&idle_start_time);
706         stage = STAGE_IDLE;
707         /* enter IDLE mode */
708         ok = gen_transact(sock, "IDLE");
709
710         if (ok == PS_IDLETIMEOUT) {
711             /* send "DONE" continuation */
712             SockWrite(sock, "DONE\r\n", 6);
713             if (outlevel >= O_MONITOR)
714                 report(stdout, "IMAP> DONE\n");
715             /* reset stage and timeout here: we are not idling any more */
716             mytimeout = saved_timeout;
717             stage = STAGE_GETRANGE;
718             /* get OK IDLE message */
719             ok = imap_ok(sock, NULL);
720         }
721     } else {  /* no idle support, fake it */
722         /* Note: stage and timeout have not been changed here as NOOP
723          * does not idle */
724         ok = gen_transact(sock, "NOOP");
725
726         /* no error, but no new mail either */
727         if (ok == PS_SUCCESS && recentcount == 0)
728         {
729             /* There are some servers who do send new mail
730              * notification out of the blue. This is in compliance
731              * with RFC 2060 section 5.3. Wait for that with a low
732              * timeout */
733             mytimeout = idle_timeout = 28;
734             time(&idle_start_time);
735             stage = STAGE_IDLE;
736             /* We are waiting for notification; no tag needed */
737             tag[0] = '\0';
738             /* wait (briefly) for an unsolicited status update */
739             ok = imap_ok(sock, NULL);
740             if (ok == PS_IDLETIMEOUT) {
741                 /* no notification came; ok */
742                 ok = PS_SUCCESS;
743             }
744         }
745     }
746
747     /* restore normal timeout value */
748     set_timeout(0);
749     mytimeout = saved_timeout;
750     stage = STAGE_GETRANGE;
751
752     return(ok);
753 }
754
755 /* maximum number of numbers we can process in "SEARCH" response */
756 # define IMAP_SEARCH_MAX 1000
757
758 static int imap_search(int sock, struct query *ctl, int count)
759 /* search for unseen messages */
760 {
761     int ok, first, last;
762     char buf[MSGBUFSIZE+1], *cp;
763
764     /* Don't count deleted messages. Enabled only for IMAP4 servers or
765      * higher and only when keeping mails. This flag will have an
766      * effect only when user has marked some unread mails for deletion
767      * using another e-mail client. */
768     flag skipdeleted = (imap_version >= IMAP4) && ctl->keep;
769     const char *undeleted;
770
771     /* Skip range search if there are less than or equal to
772      * IMAP_SEARCH_MAX mails. */
773     flag skiprangesearch = (count <= IMAP_SEARCH_MAX);
774
775     /* startcount is higher than count so that if there are no
776      * unseen messages, imap_getsizes() will not need to do
777      * anything! */
778     startcount = count + 1;
779
780     for (first = 1, last = IMAP_SEARCH_MAX; first <= count; first += IMAP_SEARCH_MAX, last += IMAP_SEARCH_MAX)
781     {
782         if (last > count)
783             last = count;
784
785 restartsearch:
786         undeleted = (skipdeleted ? " UNDELETED" : "");
787         if (skiprangesearch)
788             gen_send(sock, "SEARCH UNSEEN%s", undeleted);
789         else if (last == first)
790             gen_send(sock, "SEARCH %d UNSEEN%s", last, undeleted);
791         else
792             gen_send(sock, "SEARCH %d:%d UNSEEN%s", first, last, undeleted);
793         while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
794         {
795             if ((cp = strstr(buf, "* SEARCH")))
796             {
797                 char    *ep;
798
799                 cp += 8;        /* skip "* SEARCH" */
800                 while (*cp && unseen < count)
801                 {
802                     /* skip whitespace */
803                     while (*cp && isspace((unsigned char)*cp))
804                         cp++;
805                     if (*cp) 
806                     {
807                         unsigned long um;
808
809                         errno = 0;
810                         um = strtoul(cp,&ep,10);
811                         if (errno == 0 && ep > cp
812                                 && um <= INT_MAX && um <= (unsigned)count)
813                         {
814                             unseen_messages[unseen++] = um;
815                             if (outlevel >= O_DEBUG)
816                                 report(stdout, GT_("%lu is unseen\n"), um);
817                             if (startcount > um)
818                                 startcount = um;
819                         }
820                         cp = ep;
821                     }
822                 }
823             }
824         }
825         /* if there is a protocol error on the first loop, try a
826          * different search command */
827         if (ok == PS_ERROR && first == 1)
828         {
829             if (skipdeleted)
830             {
831                 /* retry with "SEARCH 1:1000 UNSEEN" */
832                 skipdeleted = FALSE;
833                 goto restartsearch;
834             }
835             if (!skiprangesearch)
836             {
837                 /* retry with "SEARCH UNSEEN" */
838                 skiprangesearch = TRUE;
839                 goto restartsearch;
840             }
841             /* try with "FETCH 1:n FLAGS" */
842             goto fetchflags;
843         }
844         if (ok != PS_SUCCESS)
845             return(ok);
846         /* loop back only when searching in range */
847         if (skiprangesearch)
848             break;
849     }
850     return(PS_SUCCESS);
851
852 fetchflags:
853     if (count == 1)
854         gen_send(sock, "FETCH %d FLAGS", count);
855     else
856         gen_send(sock, "FETCH %d:%d FLAGS", 1, count);
857     while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
858     {
859         unsigned int num;
860         int consumed;
861
862         /* expected response format:
863          * IMAP< * 1 FETCH (FLAGS (\Seen))
864          * IMAP< * 2 FETCH (FLAGS (\Seen \Deleted))
865          * IMAP< * 3 FETCH (FLAGS ())
866          * IMAP< * 4 FETCH (FLAGS (\Recent))
867          * IMAP< * 5 FETCH (UID 10 FLAGS (\Recent))
868          */
869         if (unseen < count
870                 && sscanf(buf, "* %u %n", &num, &consumed) == 1
871                 && 0 == strncasecmp(buf+consumed, "FETCH", 5)
872                 && isspace((unsigned char)buf[consumed+5])
873                 && num >= 1 && num <= (unsigned)count
874                 && strstr(buf, "FLAGS ")
875                 && !strstr(buf, "\\SEEN")
876                 && !strstr(buf, "\\DELETED"))
877         {
878             unseen_messages[unseen++] = num;
879             if (outlevel >= O_DEBUG)
880                 report(stdout, GT_("%u is unseen\n"), num);
881             if (startcount > num)
882                 startcount = num;
883         }
884     }
885     return(ok);
886 }
887
888 static int imap_getrange(int sock, 
889                          struct query *ctl, 
890                          const char *folder, 
891                          int *countp, int *newp, int *bytes)
892 /* get range of messages to be fetched */
893 {
894     int ok;
895
896     /* find out how many messages are waiting */
897     *bytes = -1;
898
899     if (pass > 1)
900     {
901         /* deleted mails have already been expunged by
902          * end_mailbox_poll().
903          *
904          * recentcount is already set here by the last imap command which
905          * returned EXISTS on detecting new mail. if recentcount is 0, wait
906          * for new mail.
907          *
908          * this is a while loop because imap_idle() might return on other
909          * mailbox changes also */
910         while (recentcount == 0 && do_idle) {
911             smtp_close(ctl, 1);
912             ok = imap_idle(sock);
913             if (ok)
914             {
915                 report(stderr, GT_("re-poll failed\n"));
916                 return(ok);
917             }
918         }
919         /* if recentcount is 0, return no mail */
920         if (recentcount == 0)
921                 count = 0;
922         if (outlevel >= O_DEBUG)
923             report(stdout, ngettext("%d message waiting after re-poll\n",
924                                     "%d messages waiting after re-poll\n",
925                                     count), count);
926     }
927     else
928     {
929         oldcount = count = 0;
930         ok = gen_transact(sock, 
931                           check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
932                           folder ? folder : "INBOX");
933         /* imap_ok returns PS_LOCKBUSY for READ-ONLY folders,
934          * which we can safely use in fetchall keep only */
935         if (ok == PS_LOCKBUSY && ctl->fetchall && ctl-> keep)
936             ok = 0;
937
938         if (ok != 0)
939         {
940             report(stderr, GT_("mailbox selection failed\n"));
941             return(ok);
942         }
943         else if (outlevel >= O_DEBUG)
944             report(stdout, ngettext("%d message waiting after first poll\n",
945                                     "%d messages waiting after first poll\n",
946                                     count), count);
947
948         /*
949          * We should have an expunge here to
950          * a) avoid fetching deleted mails during 'fetchall'
951          * b) getting a wrong count of mails during 'no fetchall'
952          */
953         if (!check_only && !ctl->keep && count > 0)
954         {
955             ok = internal_expunge(sock);
956             if (ok)
957             {
958                 report(stderr, GT_("expunge failed\n"));
959                 return(ok);
960             }
961             if (outlevel >= O_DEBUG)
962                 report(stdout, ngettext("%d message waiting after expunge\n",
963                                         "%d messages waiting after expunge\n",
964                                         count), count);
965         }
966
967         if (count == 0 && do_idle)
968         {
969             /* no messages?  then we may need to idle until we get some */
970             while (count == 0) {
971                 ok = imap_idle(sock);
972                 if (ok)
973                 {
974                     report(stderr, GT_("re-poll failed\n"));
975                     return(ok);
976                 }
977             }
978             if (outlevel >= O_DEBUG)
979                 report(stdout, ngettext("%d message waiting after re-poll\n",
980                                         "%d messages waiting after re-poll\n",
981                                         count), count);
982         }
983     }
984
985     *countp = oldcount = count;
986     recentcount = 0;
987     startcount = 1;
988
989     /* OK, now get a count of unseen messages and their indices */
990     if (!ctl->fetchall && count > 0)
991     {
992         if (unseen_messages)
993             free(unseen_messages);
994         unseen_messages = (unsigned int *)xmalloc(count * sizeof(unsigned int));
995         memset(unseen_messages, 0, count * sizeof(unsigned int));
996         unseen = 0;
997
998         ok = imap_search(sock, ctl, count);
999         if (ok != 0)
1000         {
1001             report(stderr, GT_("search for unseen messages failed\n"));
1002             return(ok);
1003         }
1004
1005         if (outlevel >= O_DEBUG && unseen > 0)
1006             report(stdout, GT_("%u is first unseen\n"), startcount);
1007     } else
1008         unseen = -1;
1009
1010     *newp = unseen;
1011     expunged = 0;
1012     deletions = 0;
1013
1014     return(PS_SUCCESS);
1015 }
1016
1017 static int imap_getpartialsizes(int sock, int first, int last, int *sizes)
1018 /* capture the sizes of messages #first-#last */
1019 {
1020     char buf [MSGBUFSIZE+1];
1021     int ok;
1022
1023     /*
1024      * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
1025      * won't accept 1:1 as valid set syntax.  Some implementors
1026      * should be taken out and shot for excessive anality.
1027      *
1028      * Microsoft Exchange (brain-dead piece of crap that it is) 
1029      * sometimes gets its knickers in a knot about bodiless messages.
1030      * You may see responses like this:
1031      *
1032      *  fetchmail: IMAP> A0004 FETCH 1:9 RFC822.SIZE
1033      *  fetchmail: IMAP< * 2 FETCH (RFC822.SIZE 1187)
1034      *  fetchmail: IMAP< * 3 FETCH (RFC822.SIZE 3954)
1035      *  fetchmail: IMAP< * 4 FETCH (RFC822.SIZE 1944)
1036      *  fetchmail: IMAP< * 5 FETCH (RFC822.SIZE 2933)
1037      *  fetchmail: IMAP< * 6 FETCH (RFC822.SIZE 1854)
1038      *  fetchmail: IMAP< * 7 FETCH (RFC822.SIZE 34054)
1039      *  fetchmail: IMAP< * 8 FETCH (RFC822.SIZE 5561)
1040      *  fetchmail: IMAP< * 9 FETCH (RFC822.SIZE 1101)
1041      *  fetchmail: IMAP< A0004 NO The requested item could not be found.
1042      *
1043      * This means message 1 has only headers.  For kicks and grins
1044      * you can telnet in and look:
1045      *  A003 FETCH 1 FULL
1046      *  A003 NO The requested item could not be found.
1047      *  A004 fetch 1 rfc822.header
1048      *  A004 NO The requested item could not be found.
1049      *  A006 FETCH 1 BODY
1050      *  * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
1051      *  A006 OK FETCH completed.
1052      *
1053      * To get around this, we treat the final NO as success and count
1054      * on the fact that the sizes array has been preinitialized with a
1055      * known-bad size value.
1056      */
1057
1058     /* expunges change the fetch numbers */
1059     first -= expunged;
1060     last -= expunged;
1061
1062     if (last == first)
1063         gen_send(sock, "FETCH %d RFC822.SIZE", last);
1064     else if (last > first)
1065         gen_send(sock, "FETCH %d:%d RFC822.SIZE", first, last);
1066     else /* no unseen messages! */
1067         return(PS_SUCCESS);
1068     while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
1069     {
1070         unsigned int size;
1071         int num;
1072         int consumed;
1073         char *ptr;
1074
1075         /* expected response formats:
1076          * IMAP> A0005 FETCH 1 RFC822.SIZE
1077          * IMAP< * 1 FETCH (RFC822.SIZE 1187)
1078          * IMAP< * 1 FETCH (UID 16 RFC822.SIZE 1447)
1079          */
1080         if (sscanf(buf, "* %d %n", &num, &consumed) == 1
1081             && 0 == strncasecmp(buf + consumed, "FETCH", 5)
1082             && isspace((unsigned char)buf[consumed + 5])
1083                 && (ptr = strstr(buf, "RFC822.SIZE "))
1084                 && sscanf(ptr, "RFC822.SIZE %u", &size) == 1)
1085         {
1086             if (num >= first && num <= last)
1087                 sizes[num - first] = size;
1088             else
1089                 report(stderr,
1090                         GT_("Warning: ignoring bogus data for message sizes returned by the server.\n"));
1091         }
1092     }
1093     return(ok);
1094 }
1095
1096 static int imap_getsizes(int sock, int count, int *sizes)
1097 /* capture the sizes of all messages */
1098 {
1099     return imap_getpartialsizes(sock, 1, count, sizes);
1100 }
1101
1102 static int imap_is_old(int sock, struct query *ctl, int number)
1103 /* is the given message old? */
1104 {
1105     flag seen = TRUE;
1106     int i;
1107
1108     (void)sock;
1109     (void)ctl;
1110     /* 
1111      * Expunges change the fetch numbers, but unseen_messages contains
1112      * indices from before any expungees were done.  So neither the
1113      * argument nor the values in message_sequence need to be decremented.
1114      */
1115
1116     seen = TRUE;
1117     for (i = 0; i < unseen; i++)
1118         if (unseen_messages[i] == (unsigned)number)
1119         {
1120             seen = FALSE;
1121             break;
1122         }
1123
1124     return(seen);
1125 }
1126
1127 #if 0
1128 static char *skip_token(char *ptr)
1129 {
1130     while(isspace((unsigned char)*ptr)) ptr++;
1131     while(!isspace((unsigned char)*ptr) && !iscntrl((unsigned char)*ptr)) ptr++;
1132     while(isspace((unsigned char)*ptr)) ptr++;
1133     return(ptr);
1134 }
1135 #endif
1136
1137 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
1138 /* request headers of nth message */
1139 {
1140     char buf [MSGBUFSIZE+1];
1141     int num;
1142     int ok;
1143     char *ptr;
1144
1145     (void)ctl;
1146     /* expunges change the fetch numbers */
1147     number -= expunged;
1148
1149     /*
1150      * This is blessed by RFC1176, RFC1730, RFC2060.
1151      * According to the RFCs, it should *not* set the \Seen flag.
1152      */
1153     gen_send(sock, "FETCH %d RFC822.HEADER", number);
1154
1155     /* looking for FETCH response */
1156     if ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
1157     {
1158                 int consumed;
1159         /* expected response formats:
1160          * IMAP> A0006 FETCH 1 RFC822.HEADER
1161          * IMAP< * 1 FETCH (RFC822.HEADER {1360}
1162          * IMAP< * 1 FETCH (UID 16 RFC822.HEADER {1360}
1163          * IMAP< * 1 FETCH (UID 16 RFC822.SIZE 4029 RFC822.HEADER {1360}
1164          */
1165         if (sscanf(buf, "* %d %n", &num, &consumed) == 1
1166             && 0 == strncasecmp(buf + consumed, "FETCH", 5)
1167             && isspace((unsigned char)buf[5+consumed])
1168                 && num == number
1169                 && (ptr = strstr(buf, "RFC822.HEADER"))
1170                 && sscanf(ptr, "RFC822.HEADER {%d}%n", lenp, &consumed) == 1
1171                 && ptr[consumed-1] == '}')
1172         {
1173             return(PS_SUCCESS);
1174         }
1175
1176         /* wait for a tagged response */
1177         imap_ok (sock, 0);
1178
1179         /* try to recover for some responses */
1180         if (!strncmp(buf, "* NO", 4) ||
1181                 !strncmp(buf, "* BAD", 5))
1182         {
1183             return(PS_TRANSIENT);
1184         }
1185
1186         /* a response which does not match any of the above */
1187         if (outlevel > O_SILENT)
1188             report(stderr, GT_("Incorrect FETCH response: %s.\n"), buf);
1189         return(PS_ERROR);
1190     }
1191     else if (ok == PS_SUCCESS)
1192     {
1193         /* an unexpected tagged response */
1194         if (outlevel > O_SILENT)
1195             report(stderr, GT_("Incorrect FETCH response: %s.\n"), buf);
1196         return(PS_ERROR);
1197     }
1198     return(ok);
1199 }
1200
1201 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
1202 /* request body of nth message */
1203 {
1204     char buf [MSGBUFSIZE+1], *cp;
1205     int num;
1206
1207     (void)ctl;
1208     /* expunges change the fetch numbers */
1209     number -= expunged;
1210
1211     /*
1212      * If we're using IMAP4, we can fetch the message without setting its
1213      * seen flag.  This is good!  It means that if the protocol exchange
1214      * craps out during the message, it will still be marked `unseen' on
1215      * the server.
1216      *
1217      * According to RFC2060, and Mark Crispin the IMAP maintainer,
1218      * FETCH %d BODY[TEXT] and RFC822.TEXT are "functionally 
1219      * equivalent".  However, we know of at least one server that
1220      * treats them differently in the presence of MIME attachments;
1221      * the latter form downloads the attachment, the former does not.
1222      * The server is InterChange, and the fool who implemented this
1223      * misfeature ought to be strung up by his thumbs.  
1224      *
1225      * When I tried working around this by disabling use of the 4rev1 form,
1226      * I found that doing this breaks operation with M$ Exchange.
1227      * Annoyingly enough, Exchange's refusal to cope is technically legal
1228      * under RFC2062.  Trust Microsoft, the Great Enemy of interoperability
1229      * standards, to find a way to make standards compliance irritating....
1230      */
1231     switch (imap_version)
1232     {
1233     case IMAP4rev1:     /* RFC 2060 */
1234         gen_send(sock, "FETCH %d BODY.PEEK[TEXT]", number);
1235         break;
1236
1237     case IMAP4:         /* RFC 1730 */
1238         gen_send(sock, "FETCH %d RFC822.TEXT.PEEK", number);
1239         break;
1240
1241     default:            /* RFC 1176 */
1242         gen_send(sock, "FETCH %d RFC822.TEXT", number);
1243         break;
1244     }
1245
1246     /* looking for FETCH response */
1247     do {
1248         int     ok;
1249
1250         if ((ok = gen_recv(sock, buf, sizeof(buf))))
1251             return(ok);
1252     } while
1253         (!strstr(buf+4, "FETCH") || sscanf(buf+2, "%d", &num) != 1);
1254
1255     if (num != number)
1256         return(PS_ERROR);
1257
1258     /* Understand "NIL" as length => no body present
1259      * (MS Exchange, BerliOS Bug #11980) */
1260     if (strstr(buf+10, "NIL)")) {
1261             *lenp = 0;
1262             return PS_SUCCESS;
1263     }
1264
1265     /* Understand the empty string. Seen on Yahoo. */
1266     /* XXX FIXME: we should be able to handle strings here. */
1267     if (strstr(buf+10, "\"\")")) {
1268             *lenp = 0;
1269             return PS_SUCCESS;
1270     }
1271
1272     /*
1273      * Try to extract a length from the FETCH response.  RFC2060 requires
1274      * it to be present, but at least one IMAP server (Novell GroupWise)
1275      * botches this.  The overflow check is needed because of a broken
1276      * server called dbmail that returns huge garbage lengths.
1277      */
1278     if ((cp = strchr(buf, '{'))) {
1279         long l; char *t;
1280         errno = 0;
1281         ++ cp;
1282         l = strtol(cp, &t, 10);
1283         if (errno || t == cp || (t && !strchr(t, '}')) /* parse error */
1284                     || l < 0 || l > INT_MAX /* range check */) {
1285             *lenp = -1;
1286         } else {
1287             *lenp = l;
1288         }
1289     } else {
1290         *lenp = -1;     /* missing length part in FETCH reponse */
1291     }
1292
1293     return PS_SUCCESS;
1294 }
1295
1296 static int imap_trail(int sock, struct query *ctl, const char *tag)
1297 /* discard tail of FETCH response after reading message text */
1298 {
1299     /* expunges change the fetch numbers */
1300     /* number -= expunged; */
1301
1302     (void)ctl;
1303     (void)tag;
1304
1305     return imap_ok(sock, NULL);
1306 }
1307
1308 static int imap_delete(int sock, struct query *ctl, int number)
1309 /* set delete flag for given message */
1310 {
1311     int ok;
1312
1313     (void)ctl;
1314     /* expunges change the fetch numbers */
1315     number -= expunged;
1316
1317     /*
1318      * Use SILENT if possible as a minor throughput optimization.
1319      * Note: this has been dropped from IMAP4rev1.
1320      *
1321      * We set Seen because there are some IMAP servers (notably HP
1322      * OpenMail) that do message-receipt DSNs, but only when the seen
1323      * bit is set.  This is the appropriate time -- we get here right
1324      * after the local SMTP response that says delivery was
1325      * successful.
1326      */
1327     if ((ok = gen_transact(sock,
1328                         imap_version == IMAP4 
1329                                 ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
1330                                 : "STORE %d +FLAGS (\\Seen \\Deleted)", 
1331                         number)))
1332         return(ok);
1333     else
1334         deletions++;
1335
1336     /*
1337      * We do an expunge after expunge_period messages, rather than
1338      * just before quit, so that a line hit during a long session
1339      * won't result in lots of messages being fetched again during
1340      * the next session.
1341      */
1342     if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
1343     {
1344         if ((ok = internal_expunge(sock)))
1345             return(ok);
1346     }
1347
1348     return(PS_SUCCESS);
1349 }
1350
1351 static int imap_mark_seen(int sock, struct query *ctl, int number)
1352 /* mark the given message as seen */
1353 {
1354     (void)ctl;
1355
1356     /* expunges change the message numbers */
1357     number -= expunged;
1358
1359     return(gen_transact(sock,
1360         imap_version == IMAP4
1361         ? "STORE %d +FLAGS.SILENT (\\Seen)"
1362         : "STORE %d +FLAGS (\\Seen)",
1363         number));
1364 }
1365
1366 static int imap_end_mailbox_poll(int sock, struct query *ctl)
1367 /* cleanup mailbox before we idle or switch to another one */
1368 {
1369     (void)ctl;
1370     if (deletions)
1371         internal_expunge(sock);
1372     return(PS_SUCCESS);
1373 }
1374
1375 static int imap_logout(int sock, struct query *ctl)
1376 /* send logout command */
1377 {
1378     (void)ctl;
1379     /* if any un-expunged deletions remain, ship an expunge now */
1380     if (deletions)
1381         internal_expunge(sock);
1382
1383 #ifdef USE_SEARCH
1384     /* Memory clean-up */
1385     if (unseen_messages)
1386         free(unseen_messages);
1387 #endif /* USE_SEARCH */
1388
1389     return(gen_transact(sock, "LOGOUT"));
1390 }
1391
1392 static const struct method imap =
1393 {
1394     "IMAP",             /* Internet Message Access Protocol */
1395     "imap",             /* service (plain and TLS) */
1396     "imaps",            /* service (SSL) */
1397     TRUE,               /* this is a tagged protocol */
1398     FALSE,              /* no message delimiter */
1399     imap_ok,            /* parse command response */
1400     imap_getauth,       /* get authorization */
1401     imap_getrange,      /* query range of messages */
1402     imap_getsizes,      /* get sizes of messages (used for ESMTP SIZE option) */
1403     imap_getpartialsizes,       /* get sizes of subset of messages (used for ESMTP SIZE option) */
1404     imap_is_old,        /* no UID check */
1405     imap_fetch_headers, /* request given message headers */
1406     imap_fetch_body,    /* request given message body */
1407     imap_trail,         /* eat message trailer */
1408     imap_delete,        /* delete the message */
1409     imap_mark_seen,     /* how to mark a message as seen */
1410     imap_end_mailbox_poll,      /* end-of-mailbox processing */
1411     imap_logout,        /* expunge and exit */
1412     TRUE,               /* yes, we can re-poll */
1413 };
1414
1415 int doIMAP(struct query *ctl)
1416 /* retrieve messages using IMAP Version 2bis or Version 4 */
1417 {
1418     return(do_protocol(ctl, &imap));
1419 }
1420
1421 /* imap.c ends here */