2 * imap.c -- IMAP2bis/IMAP4 protocol methods
4 * Copyright 1997 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
16 #include "fetchmail.h"
22 #endif /* OPIE_ENABLE */
24 #ifndef strstr /* glibc-2.1 declares this as a macro */
25 extern char *strstr(); /* needed on sysV68 R3V7.1. */
28 /* imap_version values */
29 #define IMAP2 -1 /* IMAP2 or IMAP2BIS, RFC1176 */
30 #define IMAP4 0 /* IMAP4 rev 0, RFC1730 */
31 #define IMAP4rev1 1 /* IMAP4 rev 1, RFC2060 */
33 static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
34 static unsigned int startcount = 1;
35 static int expunged, expunge_period, saved_timeout = 0;
36 static int imap_version, preauth;
38 static char capabilities[MSGBUFSIZE+1];
39 static unsigned int *unseen_messages;
41 static int imap_ok(int sock, char *argbuf)
42 /* parse command response */
44 char buf[MSGBUFSIZE+1];
50 if ((ok = gen_recv(sock, buf, sizeof(buf))))
53 /* all tokens in responses are caseblind */
54 for (cp = buf; *cp; cp++)
58 /* interpret untagged status responses */
59 if (strstr(buf, "* CAPABILITY"))
61 strncpy(capabilities, buf + 12, sizeof(capabilities));
62 capabilities[sizeof(capabilities)-1] = '\0';
64 else if (strstr(buf, "EXISTS"))
68 * Don't trust the message count passed by the server.
69 * Without this check, it might be possible to do a
70 * DNS-spoofing attack that would pass back a ridiculous
71 * count, and allocate a malloc area that would overlap
72 * a portion of the stack.
74 if (count > INT_MAX/sizeof(int))
76 report(stderr, "bogus message count!");
81 * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
82 * we can't wait for the tag matching the IDLE; we have to tell the
83 * server the IDLE is finished by shipping back a DONE when we
84 * see an EXISTS. Only after that will a tagged response be
85 * shipped. The idling flag also gets cleared on a timeout.
87 if (stage == STAGE_IDLE)
89 /* we do our own write and report here to disable tagging */
90 SockWrite(sock, "DONE\r\n", 6);
91 if (outlevel >= O_MONITOR)
92 report(stdout, "IMAP> DONE\n");
94 mytimeout = saved_timeout;
98 /* a space is required to avoid confusion with the \Recent flag */
99 else if (strstr(buf, " RECENT"))
101 recentcount = atoi(buf+2);
103 else if (strstr(buf, "PREAUTH"))
106 * The server may decide to make the mailbox read-only,
107 * which causes fetchmail to go into a endless loop
108 * fetching the same message over and over again.
110 * However, for check_only, we use EXAMINE which will
111 * mark the mailbox read-only as per the RFC.
113 * This checks for the condition and aborts if
114 * the mailbox is read-only.
116 * See RFC 2060 section 6.3.1 (SELECT).
117 * See RFC 2060 section 6.3.2 (EXAMINE).
119 else if (!check_only && strstr(buf, "[READ-ONLY]"))
122 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
135 for (cp = buf; !isspace(*cp); cp++)
140 if (strncasecmp(cp, "OK", 2) == 0)
146 else if (strncasecmp(cp, "BAD", 3) == 0)
148 else if (strncasecmp(cp, "NO", 2) == 0)
150 if (stage == STAGE_GETAUTH)
151 return(PS_AUTHFAIL); /* RFC2060, 6.2.2 */
163 static tSmbNtlmAuthRequest request;
164 static tSmbNtlmAuthChallenge challenge;
165 static tSmbNtlmAuthResponse response;
168 * NTLM support by Grant Edwards.
170 * Handle MS-Exchange NTLM authentication method. This is the same
171 * as the NTLM auth used by Samba for SMB related services. We just
172 * encode the packets in base64 instead of sending them out via a
175 * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
178 static int do_imap_ntlm(int sock, struct query *ctl)
183 gen_send(sock, "AUTHENTICATE NTLM");
185 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
188 if (msgbuf[0] != '+')
191 buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
193 if (outlevel >= O_DEBUG)
194 dumpSmbNtlmAuthRequest(stdout, &request);
196 memset(msgbuf,0,sizeof msgbuf);
197 to64frombits (msgbuf, (unsigned char*)&request, SmbLength(&request));
199 if (outlevel >= O_MONITOR)
200 report(stdout, "IMAP> %s\n", msgbuf);
202 strcat(msgbuf,"\r\n");
203 SockWrite (sock, msgbuf, strlen (msgbuf));
205 if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
208 len = from64tobits ((char*)&challenge, msgbuf, sizeof(challenge));
210 if (outlevel >= O_DEBUG)
211 dumpSmbNtlmAuthChallenge(stdout, &challenge);
213 buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
215 if (outlevel >= O_DEBUG)
216 dumpSmbNtlmAuthResponse(stdout, &response);
218 memset(msgbuf,0,sizeof msgbuf);
219 to64frombits (msgbuf, (unsigned char*)&response, SmbLength(&response));
221 if (outlevel >= O_MONITOR)
222 report(stdout, "IMAP> %s\n", msgbuf);
224 strcat(msgbuf,"\r\n");
225 SockWrite (sock, msgbuf, strlen (msgbuf));
227 if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
230 if (strstr (msgbuf, "OK"))
237 static int imap_canonicalize(char *result, char *raw, int maxlen)
238 /* encode an IMAP password as per RFC1730's quoting conventions */
243 for (i = 0; i < strlen(raw) && i < maxlen; i++)
245 if ((raw[i] == '\\') || (raw[i] == '"'))
247 result[j++] = raw[i];
254 static void capa_probe(int sock, struct query *ctl)
255 /* set capability variables from a CAPA probe */
259 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
260 capabilities[0] = '\0';
261 if ((ok = gen_transact(sock, "CAPABILITY")) == PS_SUCCESS)
265 /* capability checks are supposed to be caseblind */
266 for (cp = capabilities; *cp; cp++)
269 /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
270 should expect a response in mixed-case */
271 if (strstr(capabilities, "IMAP4REV1"))
273 imap_version = IMAP4rev1;
274 if (outlevel >= O_DEBUG)
275 report(stdout, GT_("Protocol identified as IMAP4 rev 1\n"));
279 imap_version = IMAP4;
280 if (outlevel >= O_DEBUG)
281 report(stdout, GT_("Protocol identified as IMAP4 rev 0\n"));
284 else if (ok == PS_ERROR)
286 imap_version = IMAP2;
287 if (outlevel >= O_DEBUG)
288 report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
292 * Handle idling. We depend on coming through here on startup
293 * and after each timeout (including timeouts during idles).
295 if (strstr(capabilities, "IDLE") && ctl->idle)
298 if (outlevel >= O_VERBOSE)
299 report(stdout, GT_("will idle after poll\n"));
303 static int imap_getauth(int sock, struct query *ctl, char *greeting)
304 /* apply for connection authorization */
308 flag did_stls = FALSE;
309 #endif /* SSL_ENABLE */
311 capa_probe(sock, ctl);
314 * If either (a) we saw a PREAUTH token in the greeting, or
315 * (b) the user specified ssh preauthentication, then we're done.
317 if (preauth || ctl->server.authenticate == A_SSH)
319 preauth = FALSE; /* reset for the next session */
323 * Time to authenticate the user.
324 * Try the protocol variants that don't require passwords first.
329 if ((ctl->server.authenticate == A_ANY
330 || ctl->server.authenticate == A_GSSAPI)
331 && strstr(capabilities, "AUTH=GSSAPI"))
332 if(ok = do_gssauth(sock, "AUTHENTICATE", ctl->server.truename, ctl->remotename))
334 /* SASL cancellation of authentication */
336 if(ctl->server.authenticate != A_ANY)
344 if ((ctl->server.authenticate == A_ANY
345 || ctl->server.authenticate == A_KERBEROS_V4
346 || ctl->server.authenticate == A_KERBEROS_V5)
347 && strstr(capabilities, "AUTH=KERBEROS_V4"))
349 if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
351 /* SASL cancellation of authentication */
353 if(ctl->server.authenticate != A_ANY)
359 #endif /* KERBEROS_V4 */
362 if ((!ctl->sslproto || !strcmp(ctl->sslproto,"tls1"))
364 && strstr(capabilities, "STARTTLS"))
368 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
369 ok = gen_transact(sock, "STARTTLS");
371 /* We use "tls1" instead of ctl->sslproto, as we want STARTTLS,
372 * not other SSL protocols
374 if (ok == PS_SUCCESS &&
375 SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
377 if (!ctl->sslproto && !ctl->wehaveauthed)
379 ctl->sslproto = xstrdup("");
380 /* repoll immediately */
384 GT_("SSL connection failed.\n"));
390 * RFC 2595 says this:
392 * "Once TLS has been started, the client MUST discard cached
393 * information about server capabilities and SHOULD re-issue the
394 * CAPABILITY command. This is necessary to protect against
395 * man-in-the-middle attacks which alter the capabilities list prior
396 * to STARTTLS. The server MAY advertise different capabilities
399 capa_probe(sock, ctl);
401 #endif /* SSL_ENABLE */
403 peek_capable = (imap_version >= IMAP4);
406 * Assumption: expunges are cheap, so we want to do them
407 * after every message unless user said otherwise.
409 if (NUM_SPECIFIED(ctl->expunge))
410 expunge_period = NUM_VALUE_OUT(ctl->expunge);
415 * No such luck. OK, now try the variants that mask your password
416 * in a challenge-response.
419 if ((ctl->server.authenticate == A_ANY && strstr(capabilities, "AUTH=CRAM-MD5"))
420 || ctl->server.authenticate == A_CRAM_MD5)
422 if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
424 /* SASL cancellation of authentication */
426 if(ctl->server.authenticate != A_ANY)
434 if ((ctl->server.authenticate == A_ANY
435 || ctl->server.authenticate == A_OTP)
436 && strstr(capabilities, "AUTH=X-OTP"))
437 if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
439 /* SASL cancellation of authentication */
441 if(ctl->server.authenticate != A_ANY)
447 if (ctl->server.authenticate == A_OTP)
450 GT_("Required OTP capability not compiled into fetchmail\n"));
452 #endif /* OPIE_ENABLE */
455 if ((ctl->server.authenticate == A_ANY
456 || ctl->server.authenticate == A_NTLM)
457 && strstr (capabilities, "AUTH=NTLM")) {
458 if ((ok = do_imap_ntlm(sock, ctl)))
460 /* SASL cancellation of authentication */
462 if(ctl->server.authenticate != A_ANY)
469 if (ctl->server.authenticate == A_NTLM)
472 GT_("Required NTLM capability not compiled into fetchmail\n"));
474 #endif /* NTLM_ENABLE */
476 #ifdef __UNUSED__ /* The Cyrus IMAP4rev1 server chokes on this */
477 /* this handles either AUTH=LOGIN or AUTH-LOGIN */
478 if ((imap_version >= IMAP4rev1) && (!strstr(capabilities, "LOGIN")))
481 GT_("Required LOGIN capability not supported by server\n"));
483 #endif /* __UNUSED__ */
486 * We're stuck with sending the password en clair.
487 * The reason for this odd-looking logic is that some
488 * servers return LOGINDISABLED even though login
489 * actually works. So arrange things in such a way that
490 * setting auth passwd makes it ignore this capability.
492 if((ctl->server.authenticate==A_ANY&&!strstr(capabilities,"LOGINDISABLED"))
493 || ctl->server.authenticate == A_PASSWORD)
495 /* these sizes guarantee no buffer overflow */
496 char remotename[NAMELEN*2+1], password[PASSWORDLEN*2+1];
498 imap_canonicalize(remotename, ctl->remotename, NAMELEN);
499 imap_canonicalize(password, ctl->password, PASSWORDLEN);
501 strcpy(shroud, password);
502 ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
505 /* this is for servers which claim to support TLS, but actually
507 if (did_stls && ok == PS_SOCKET && !ctl->sslproto && !ctl->wehaveauthed)
509 ctl->sslproto = xstrdup("");
510 /* repoll immediately */
516 /* SASL cancellation of authentication */
518 if(ctl->server.authenticate != A_ANY)
528 static int internal_expunge(int sock)
529 /* ship an expunge, resetting associated counters */
533 if ((ok = gen_transact(sock, "EXPUNGE")))
536 expunged += deletions;
539 #ifdef IMAP_UID /* not used */
541 #endif /* IMAP_UID */
546 static int imap_idle(int sock)
547 /* start an RFC2177 IDLE */
551 /* special timeout to terminate the IDLE and re-issue it
552 * at least every 28 minutes:
553 * (the server may have an inactivity timeout) */
555 saved_timeout = mytimeout;
556 mytimeout = 1680; /* 28 min */
558 /* enter IDLE mode */
559 ok = gen_transact(sock, "IDLE");
561 if(ok == PS_IDLETIMEOUT) {
562 /* send "DONE" continuation */
563 SockWrite(sock, "DONE\r\n", 6);
564 if (outlevel >= O_MONITOR)
565 report(stdout, "IMAP> DONE\n");
567 /* restore normal timeout value */
568 mytimeout = saved_timeout;
571 /* get OK IDLE message */
572 return imap_ok(sock, NULL);
574 /* not idle timeout */
578 static int imap_getrange(int sock,
581 int *countp, int *newp, int *bytes)
582 /* get range of messages to be fetched */
585 char buf[MSGBUFSIZE+1], *cp;
587 /* find out how many messages are waiting */
593 * We have to have an expunge here, otherwise the re-poll will
594 * infinite-loop picking up un-expunged messages -- unless the
595 * expunge period is one and we've been nuking each message
596 * just after deletion.
600 ok = internal_expunge(sock);
603 report(stderr, GT_("expunge failed\n"));
609 * recentcount is already set here by the last imap command which
610 * returned RECENT on detecting new mail. if recentcount is 0, wait
614 /* some servers do not report RECENT after an EXPUNGE. this check
615 * forces an incorrect recentcount to be ignored. */
616 if (recentcount > count)
618 /* this is a while loop because imap_idle() might return on other
619 * mailbox changes also */
620 while (recentcount == 0 && do_idle) {
622 ok = imap_idle(sock);
625 report(stderr, GT_("re-poll failed\n"));
629 /* if recentcount is 0, return no mail */
630 if (recentcount == 0)
632 if (outlevel >= O_DEBUG)
633 report(stdout, GT_("%d messages waiting after re-poll\n"), count);
638 ok = gen_transact(sock,
639 check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
640 folder ? folder : "INBOX");
643 report(stderr, GT_("mailbox selection failed\n"));
646 else if (outlevel >= O_DEBUG)
647 report(stdout, GT_("%d messages waiting after first poll\n"), count);
649 /* no messages? then we may need to idle until we get some */
650 while (count == 0 && do_idle) {
651 ok = imap_idle(sock);
654 report(stderr, GT_("re-poll failed\n"));
660 * We should have an expunge here to
661 * a) avoid fetching deleted mails during 'fetchall'
662 * b) getting a wrong count of mails during 'no fetchall'
664 if (!check_only && !ctl->keep && count > 0)
666 ok = internal_expunge(sock);
669 report(stderr, GT_("expunge failed\n"));
672 if (outlevel >= O_DEBUG)
673 report(stdout, GT_("%d messages waiting after expunge\n"), count);
681 /* OK, now get a count of unseen messages and their indices */
682 if (!ctl->fetchall && count > 0)
685 free(unseen_messages);
686 unseen_messages = xmalloc(count * sizeof(unsigned int));
687 memset(unseen_messages, 0, count * sizeof(unsigned int));
690 gen_send(sock, "SEARCH UNSEEN");
692 ok = gen_recv(sock, buf, sizeof(buf));
695 report(stderr, GT_("search for unseen messages failed\n"));
698 else if ((cp = strstr(buf, "* SEARCH")))
702 cp += 8; /* skip "* SEARCH" */
703 /* startcount is higher than count so that if there are no
704 * unseen messages, imap_getsizes() will not need to do
706 startcount = count + 1;
708 while (*cp && unseen < count)
710 /* skip whitespace */
711 while (*cp && isspace(*cp))
717 * Message numbers are between 1 and 2^32 inclusive,
718 * so unsigned int is large enough.
720 um=(unsigned int)strtol(cp,&ep,10);
723 unseen_messages[unseen++] = um;
724 if (outlevel >= O_DEBUG)
725 report(stdout, GT_("%u is unseen\n"), um);
734 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
736 if (outlevel >= O_DEBUG && unseen > 0)
737 report(stdout, GT_("%u is first unseen\n"), startcount);
749 static int imap_getsizes(int sock, int count, int *sizes)
750 /* capture the sizes of all messages */
752 char buf [MSGBUFSIZE+1];
755 * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
756 * won't accept 1:1 as valid set syntax. Some implementors
757 * should be taken out and shot for excessive anality.
759 * Microsoft Exchange (brain-dead piece of crap that it is)
760 * sometimes gets its knickers in a knot about bodiless messages.
761 * You may see responses like this:
763 * fetchmail: IMAP> A0004 FETCH 1:9 RFC822.SIZE
764 * fetchmail: IMAP< * 2 FETCH (RFC822.SIZE 1187)
765 * fetchmail: IMAP< * 3 FETCH (RFC822.SIZE 3954)
766 * fetchmail: IMAP< * 4 FETCH (RFC822.SIZE 1944)
767 * fetchmail: IMAP< * 5 FETCH (RFC822.SIZE 2933)
768 * fetchmail: IMAP< * 6 FETCH (RFC822.SIZE 1854)
769 * fetchmail: IMAP< * 7 FETCH (RFC822.SIZE 34054)
770 * fetchmail: IMAP< * 8 FETCH (RFC822.SIZE 5561)
771 * fetchmail: IMAP< * 9 FETCH (RFC822.SIZE 1101)
772 * fetchmail: IMAP< A0004 NO The requested item could not be found.
774 * This means message 1 has only headers. For kicks and grins
775 * you can telnet in and look:
777 * A003 NO The requested item could not be found.
778 * A004 fetch 1 rfc822.header
779 * A004 NO The requested item could not be found.
781 * * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
782 * A006 OK FETCH completed.
784 * To get around this, we terminate the read loop on a NO and count
785 * on the fact that the sizes array has been preinitialized with a
786 * known-bad size value.
788 /* if fetchall is specified, startcount is 1;
789 * else if there is new mail, startcount is first unseen message;
790 * else startcount is greater than count.
792 if (count == startcount)
793 gen_send(sock, "FETCH %d RFC822.SIZE", count);
794 else if (count > startcount)
795 gen_send(sock, "FETCH %d:%d RFC822.SIZE", startcount, count);
796 else /* no unseen messages! */
800 unsigned int num, size;
804 if ((ok = gen_recv(sock, buf, sizeof(buf))))
806 /* we want response matching to be case-insensitive */
807 for (cp = buf; *cp; cp++)
809 /* an untagged NO means that a message was not readable */
810 if (strstr(buf, "* NO"))
812 else if (strstr(buf, "OK") || strstr(buf, "NO"))
814 else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2)
816 if (num > 0 && num <= count)
817 sizes[num - 1] = size;
819 report(stderr, "Warning: ignoring bogus data for message sizes returned by the server.\n");
826 static int imap_is_old(int sock, struct query *ctl, int number)
827 /* is the given message old? */
833 * Expunges change the fetch numbers, but unseen_messages contains
834 * indices from before any expungees were done. So neither the
835 * argument nor the values in message_sequence need to be decremented.
839 for (i = 0; i < unseen; i++)
840 if (unseen_messages[i] == number)
849 static char *skip_token(char *ptr)
851 while(isspace(*ptr)) ptr++;
852 while(!isspace(*ptr) && !iscntrl(*ptr)) ptr++;
853 while(isspace(*ptr)) ptr++;
857 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
858 /* request headers of nth message */
860 char buf [MSGBUFSIZE+1];
863 /* expunges change the fetch numbers */
867 * This is blessed by RFC1176, RFC1730, RFC2060.
868 * According to the RFCs, it should *not* set the \Seen flag.
870 gen_send(sock, "FETCH %d RFC822.HEADER", number);
872 /* looking for FETCH response */
878 if ((ok = gen_recv(sock, buf, sizeof(buf))))
880 ptr = skip_token(buf); /* either "* " or "AXXXX " */
881 if (sscanf(ptr, "%d FETCH (%*s {%d}", &num, lenp) == 2)
883 /* try to recover from chronically fucked-up M$ Exchange servers */
884 else if (!strncmp(ptr, "NO", 2))
886 /* wait for a tagged response */
887 if (strstr (buf, "* NO"))
889 return(PS_TRANSIENT);
891 else if (!strncmp(ptr, "BAD", 3))
893 /* wait for a tagged response */
894 if (strstr (buf, "* BAD"))
896 return(PS_TRANSIENT);
906 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
907 /* request body of nth message */
909 char buf [MSGBUFSIZE+1], *cp;
912 /* expunges change the fetch numbers */
916 * If we're using IMAP4, we can fetch the message without setting its
917 * seen flag. This is good! It means that if the protocol exchange
918 * craps out during the message, it will still be marked `unseen' on
921 * According to RFC2060, and Mark Crispin the IMAP maintainer,
922 * FETCH %d BODY[TEXT] and RFC822.TEXT are "functionally
923 * equivalent". However, we know of at least one server that
924 * treats them differently in the presence of MIME attachments;
925 * the latter form downloads the attachment, the former does not.
926 * The server is InterChange, and the fool who implemented this
927 * misfeature ought to be strung up by his thumbs.
929 * When I tried working around this by disabling use of the 4rev1 form,
930 * I found that doing this breaks operation with M$ Exchange.
931 * Annoyingly enough, Exchange's refusal to cope is technically legal
932 * under RFC2062. Trust Microsoft, the Great Enemy of interoperability
933 * standards, to find a way to make standards compliance irritating....
935 switch (imap_version)
937 case IMAP4rev1: /* RFC 2060 */
938 gen_send(sock, "FETCH %d BODY.PEEK[TEXT]", number);
941 case IMAP4: /* RFC 1730 */
942 gen_send(sock, "FETCH %d RFC822.TEXT.PEEK", number);
945 default: /* RFC 1176 */
946 gen_send(sock, "FETCH %d RFC822.TEXT", number);
950 /* looking for FETCH response */
954 if ((ok = gen_recv(sock, buf, sizeof(buf))))
957 (!strstr(buf+4, "FETCH") || sscanf(buf+2, "%d", &num) != 1);
963 * Try to extract a length from the FETCH response. RFC2060 requires
964 * it to be present, but at least one IMAP server (Novell GroupWise)
967 if ((cp = strchr(buf, '{')))
968 *lenp = atoi(cp + 1);
970 *lenp = -1; /* missing length part in FETCH reponse */
975 static int imap_trail(int sock, struct query *ctl, int number)
976 /* discard tail of FETCH response after reading message text */
978 /* expunges change the fetch numbers */
979 /* number -= expunged; */
983 char buf[MSGBUFSIZE+1];
986 if ((ok = gen_recv(sock, buf, sizeof(buf))))
989 /* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
990 if (strstr(buf, "OK"))
997 static int imap_delete(int sock, struct query *ctl, int number)
998 /* set delete flag for given message */
1002 /* expunges change the fetch numbers */
1006 * Use SILENT if possible as a minor throughput optimization.
1007 * Note: this has been dropped from IMAP4rev1.
1009 * We set Seen because there are some IMAP servers (notably HP
1010 * OpenMail) that do message-receipt DSNs, but only when the seen
1011 * bit is set. This is the appropriate time -- we get here right
1012 * after the local SMTP response that says delivery was
1015 if ((ok = gen_transact(sock,
1016 imap_version == IMAP4
1017 ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
1018 : "STORE %d +FLAGS (\\Seen \\Deleted)",
1025 * We do an expunge after expunge_period messages, rather than
1026 * just before quit, so that a line hit during a long session
1027 * won't result in lots of messages being fetched again during
1030 if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
1031 internal_expunge(sock);
1036 static int imap_mark_seen(int sock, struct query *ctl, int number)
1037 /* mark the given message as seen */
1039 return(gen_transact(sock,
1040 imap_version == IMAP4
1041 ? "STORE %d +FLAGS.SILENT (\\Seen)"
1042 : "STORE %d +FLAGS (\\Seen)",
1046 static int imap_logout(int sock, struct query *ctl)
1047 /* send logout command */
1049 /* if any un-expunged deletions remain, ship an expunge now */
1051 internal_expunge(sock);
1054 /* Memory clean-up */
1055 if (unseen_messages)
1056 free(unseen_messages);
1057 #endif /* USE_SEARCH */
1059 return(gen_transact(sock, "LOGOUT"));
1062 const static struct method imap =
1064 "IMAP", /* Internet Message Access Protocol */
1068 #else /* INET6_ENABLE */
1069 143, /* standard IMAP2bis/IMAP4 port */
1070 993, /* ssl IMAP2bis/IMAP4 port */
1071 #endif /* INET6_ENABLE */
1072 TRUE, /* this is a tagged protocol */
1073 FALSE, /* no message delimiter */
1074 imap_ok, /* parse command response */
1075 imap_getauth, /* get authorization */
1076 imap_getrange, /* query range of messages */
1077 imap_getsizes, /* get sizes of messages (used for ESMTP SIZE option) */
1078 imap_is_old, /* no UID check */
1079 imap_fetch_headers, /* request given message headers */
1080 imap_fetch_body, /* request given message body */
1081 imap_trail, /* eat message trailer */
1082 imap_delete, /* delete the message */
1083 imap_mark_seen, /* how to mark a message as seen */
1084 imap_logout, /* expunge and exit */
1085 TRUE, /* yes, we can re-poll */
1088 int doIMAP(struct query *ctl)
1089 /* retrieve messages using IMAP Version 2bis or Version 4 */
1091 return(do_protocol(ctl, &imap));
1094 /* imap.c ends here */