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"))
60 strncpy(capabilities, buf + 12, sizeof(capabilities));
61 else if (strstr(buf, "EXISTS"))
65 * Don't trust the message count passed by the server.
66 * Without this check, it might be possible to do a
67 * DNS-spoofing attack that would pass back a ridiculous
68 * count, and allocate a malloc area that would overlap
69 * a portion of the stack.
71 if (count > INT_MAX/sizeof(int))
73 report(stderr, "bogus message count!");
78 * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
79 * we can't wait for the tag matching the IDLE; we have to tell the
80 * server the IDLE is finished by shipping back a DONE when we
81 * see an EXISTS. Only after that will a tagged response be
82 * shipped. The idling flag also gets cleared on a timeout.
84 if (stage == STAGE_IDLE)
86 /* we do our own write and report here to disable tagging */
87 SockWrite(sock, "DONE\r\n", 6);
88 if (outlevel >= O_MONITOR)
89 report(stdout, "IMAP> DONE\n");
91 mytimeout = saved_timeout;
95 /* a space is required to avoid confusion with the \Recent flag */
96 else if (strstr(buf, " RECENT"))
98 recentcount = atoi(buf+2);
100 else if (strstr(buf, "PREAUTH"))
103 * The server may decide to make the mailbox read-only,
104 * which causes fetchmail to go into a endless loop
105 * fetching the same message over and over again.
107 * However, for check_only, we use EXAMINE which will
108 * mark the mailbox read-only as per the RFC.
110 * This checks for the condition and aborts if
111 * the mailbox is read-only.
113 * See RFC 2060 section 6.3.1 (SELECT).
114 * See RFC 2060 section 6.3.2 (EXAMINE).
116 else if (!check_only && strstr(buf, "[READ-ONLY]"))
119 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
132 for (cp = buf; !isspace(*cp); cp++)
137 if (strncasecmp(cp, "OK", 2) == 0)
143 else if (strncasecmp(cp, "BAD", 3) == 0)
145 else if (strncasecmp(cp, "NO", 2) == 0)
147 if (stage == STAGE_GETAUTH)
148 return(PS_AUTHFAIL); /* RFC2060, 6.2.2 */
160 static tSmbNtlmAuthRequest request;
161 static tSmbNtlmAuthChallenge challenge;
162 static tSmbNtlmAuthResponse response;
165 * NTLM support by Grant Edwards.
167 * Handle MS-Exchange NTLM authentication method. This is the same
168 * as the NTLM auth used by Samba for SMB related services. We just
169 * encode the packets in base64 instead of sending them out via a
172 * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
175 static int do_imap_ntlm(int sock, struct query *ctl)
180 gen_send(sock, "AUTHENTICATE NTLM");
182 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
185 if (msgbuf[0] != '+')
188 buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
190 if (outlevel >= O_DEBUG)
191 dumpSmbNtlmAuthRequest(stdout, &request);
193 memset(msgbuf,0,sizeof msgbuf);
194 to64frombits (msgbuf, (unsigned char*)&request, SmbLength(&request));
196 if (outlevel >= O_MONITOR)
197 report(stdout, "IMAP> %s\n", msgbuf);
199 strcat(msgbuf,"\r\n");
200 SockWrite (sock, msgbuf, strlen (msgbuf));
202 if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
205 len = from64tobits ((char*)&challenge, msgbuf, sizeof(challenge));
207 if (outlevel >= O_DEBUG)
208 dumpSmbNtlmAuthChallenge(stdout, &challenge);
210 buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
212 if (outlevel >= O_DEBUG)
213 dumpSmbNtlmAuthResponse(stdout, &response);
215 memset(msgbuf,0,sizeof msgbuf);
216 to64frombits (msgbuf, (unsigned char*)&response, SmbLength(&response));
218 if (outlevel >= O_MONITOR)
219 report(stdout, "IMAP> %s\n", msgbuf);
221 strcat(msgbuf,"\r\n");
222 SockWrite (sock, msgbuf, strlen (msgbuf));
224 if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
227 if (strstr (msgbuf, "OK"))
234 static int imap_canonicalize(char *result, char *raw, int maxlen)
235 /* encode an IMAP password as per RFC1730's quoting conventions */
240 for (i = 0; i < strlen(raw) && i < maxlen; i++)
242 if ((raw[i] == '\\') || (raw[i] == '"'))
244 result[j++] = raw[i];
251 static int imap_getauth(int sock, struct query *ctl, char *greeting)
252 /* apply for connection authorization */
256 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
257 capabilities[0] = '\0';
258 if ((ok = gen_transact(sock, "CAPABILITY")) == PS_SUCCESS)
262 /* capability checks are supposed to be caseblind */
263 for (cp = capabilities; *cp; cp++)
266 /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
267 should expect a response in mixed-case */
268 if (strstr(capabilities, "IMAP4REV1"))
270 imap_version = IMAP4rev1;
271 if (outlevel >= O_DEBUG)
272 report(stdout, GT_("Protocol identified as IMAP4 rev 1\n"));
276 imap_version = IMAP4;
277 if (outlevel >= O_DEBUG)
278 report(stdout, GT_("Protocol identified as IMAP4 rev 0\n"));
281 else if (ok == PS_ERROR)
283 imap_version = IMAP2;
284 if (outlevel >= O_DEBUG)
285 report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
290 peek_capable = (imap_version >= IMAP4);
293 * Assumption: expunges are cheap, so we want to do them
294 * after every message unless user said otherwise.
296 if (NUM_SPECIFIED(ctl->expunge))
297 expunge_period = NUM_VALUE_OUT(ctl->expunge);
302 * Handle idling. We depend on coming through here on startup
303 * and after each timeout (including timeouts during idles).
305 if (strstr(capabilities, "IDLE") && ctl->idle)
308 if (outlevel >= O_VERBOSE)
309 report(stdout, GT_("will idle after poll\n"));
313 * If either (a) we saw a PREAUTH token in the greeting, or
314 * (b) the user specified ssh preauthentication, then we're done.
316 if (preauth || ctl->server.authenticate == A_SSH)
318 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->server.authenticate == A_ANY)
364 && strstr(capabilities, "STARTTLS"))
368 realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
369 gen_transact(sock, "STARTTLS");
371 /* We use "tls1" instead of ctl->sslproto, as we want STARTTLS,
372 * not other SSL protocols
374 if (SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
377 GT_("SSL connection failed.\n"));
381 #endif /* SSL_ENABLE */
384 * No such luck. OK, now try the variants that mask your password
385 * in a challenge-response.
388 if ((ctl->server.authenticate == A_ANY
389 || ctl->server.authenticate == A_CRAM_MD5)
390 && strstr(capabilities, "AUTH=CRAM-MD5"))
392 if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
394 /* SASL cancellation of authentication */
396 if(ctl->server.authenticate != A_ANY)
404 if ((ctl->server.authenticate == A_ANY
405 || ctl->server.authenticate == A_OTP)
406 && strstr(capabilities, "AUTH=X-OTP"))
407 if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
409 /* SASL cancellation of authentication */
411 if(ctl->server.authenticate != A_ANY)
417 if (ctl->server.authenticate == A_OTP)
420 GT_("Required OTP capability not compiled into fetchmail\n"));
422 #endif /* OPIE_ENABLE */
425 if ((ctl->server.authenticate == A_ANY
426 || ctl->server.authenticate == A_NTLM)
427 && strstr (capabilities, "AUTH=NTLM")) {
428 if ((ok = do_imap_ntlm(sock, ctl)))
430 /* SASL cancellation of authentication */
432 if(ctl->server.authenticate != A_ANY)
439 if (ctl->server.authenticate == A_NTLM)
442 GT_("Required NTLM capability not compiled into fetchmail\n"));
444 #endif /* NTLM_ENABLE */
446 #ifdef __UNUSED__ /* The Cyrus IMAP4rev1 server chokes on this */
447 /* this handles either AUTH=LOGIN or AUTH-LOGIN */
448 if ((imap_version >= IMAP4rev1) && (!strstr(capabilities, "LOGIN")))
451 GT_("Required LOGIN capability not supported by server\n"));
453 #endif /* __UNUSED__ */
456 * We're stuck with sending the password en clair.
457 * The reason for this odd-looking logic is that some
458 * servers return LOGINDISABLED even though login
459 * actually works. So arrange things in such a way that
460 * setting auth passwd makes it ignore this capability.
462 if((ctl->server.authenticate==A_ANY&&!strstr(capabilities,"LOGINDISABLED"))
463 || ctl->server.authenticate == A_PASSWORD)
465 /* these sizes guarantee no buffer overflow */
466 char remotename[NAMELEN*2+1], password[PASSWORDLEN*2+1];
468 imap_canonicalize(remotename, ctl->remotename, NAMELEN);
469 imap_canonicalize(password, ctl->password, PASSWORDLEN);
471 strcpy(shroud, password);
472 ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
476 /* SASL cancellation of authentication */
478 if(ctl->server.authenticate != A_ANY)
488 static int internal_expunge(int sock)
489 /* ship an expunge, resetting associated counters */
493 if ((ok = gen_transact(sock, "EXPUNGE")))
496 expunged += deletions;
499 #ifdef IMAP_UID /* not used */
501 #endif /* IMAP_UID */
506 static int imap_idle(int sock)
507 /* start an RFC2177 IDLE */
510 saved_timeout = mytimeout;
513 return (gen_transact(sock, "IDLE"));
516 static int imap_getrange(int sock,
519 int *countp, int *newp, int *bytes)
520 /* get range of messages to be fetched */
523 char buf[MSGBUFSIZE+1], *cp;
525 /* find out how many messages are waiting */
531 * We have to have an expunge here, otherwise the re-poll will
532 * infinite-loop picking up un-expunged messages -- unless the
533 * expunge period is one and we've been nuking each message
534 * just after deletion.
538 ok = internal_expunge(sock);
541 report(stderr, GT_("expunge failed\n"));
547 * recentcount is already set here by the last imap command which
548 * returned RECENT on detecting new mail. if recentcount is 0, wait
552 /* some servers do not report RECENT after an EXPUNGE. this check
553 * forces an incorrect recentcount to be ignored. */
554 if (recentcount > count)
556 /* this is a while loop because imap_idle() might return on other
557 * mailbox changes also */
558 while (recentcount == 0 && do_idle) {
560 ok = imap_idle(sock);
563 report(stderr, GT_("re-poll failed\n"));
567 /* if recentcount is 0, return no mail */
568 if (recentcount == 0)
570 if (outlevel >= O_DEBUG)
571 report(stdout, GT_("%d messages waiting after re-poll\n"), count);
576 ok = gen_transact(sock,
577 check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
578 folder ? folder : "INBOX");
581 report(stderr, GT_("mailbox selection failed\n"));
584 else if (outlevel >= O_DEBUG)
585 report(stdout, GT_("%d messages waiting after first poll\n"), count);
587 /* no messages? then we may need to idle until we get some */
588 while (count == 0 && do_idle) {
589 ok = imap_idle(sock);
592 report(stderr, GT_("re-poll failed\n"));
598 * We should have an expunge here to
599 * a) avoid fetching deleted mails during 'fetchall'
600 * b) getting a wrong count of mails during 'no fetchall'
602 if (!check_only && !ctl->keep && count > 0)
604 ok = internal_expunge(sock);
607 report(stderr, GT_("expunge failed\n"));
610 if (outlevel >= O_DEBUG)
611 report(stdout, GT_("%d messages waiting after expunge\n"), count);
619 /* OK, now get a count of unseen messages and their indices */
620 if (!ctl->fetchall && count > 0)
623 free(unseen_messages);
624 unseen_messages = xmalloc(count * sizeof(unsigned int));
625 memset(unseen_messages, 0, count * sizeof(unsigned int));
628 gen_send(sock, "SEARCH UNSEEN");
630 ok = gen_recv(sock, buf, sizeof(buf));
633 report(stderr, GT_("search for unseen messages failed\n"));
636 else if ((cp = strstr(buf, "* SEARCH")))
640 cp += 8; /* skip "* SEARCH" */
641 /* startcount is higher than count so that if there are no
642 * unseen messages, imap_getsizes() will not need to do
644 startcount = count + 1;
646 while (*cp && unseen < count)
648 /* skip whitespace */
649 while (*cp && isspace(*cp))
655 * Message numbers are between 1 and 2^32 inclusive,
656 * so unsigned int is large enough.
658 um=(unsigned int)strtol(cp,&ep,10);
661 unseen_messages[unseen++] = um;
662 if (outlevel >= O_DEBUG)
663 report(stdout, GT_("%u is unseen\n"), um);
672 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
674 if (outlevel >= O_DEBUG && unseen > 0)
675 report(stdout, GT_("%u is first unseen\n"), startcount);
687 static int imap_getsizes(int sock, int count, int *sizes)
688 /* capture the sizes of all messages */
690 char buf [MSGBUFSIZE+1];
693 * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
694 * won't accept 1:1 as valid set syntax. Some implementors
695 * should be taken out and shot for excessive anality.
697 * Microsoft Exchange (brain-dead piece of crap that it is)
698 * sometimes gets its knickers in a knot about bodiless messages.
699 * You may see responses like this:
701 * fetchmail: IMAP> A0004 FETCH 1:9 RFC822.SIZE
702 * fetchmail: IMAP< * 2 FETCH (RFC822.SIZE 1187)
703 * fetchmail: IMAP< * 3 FETCH (RFC822.SIZE 3954)
704 * fetchmail: IMAP< * 4 FETCH (RFC822.SIZE 1944)
705 * fetchmail: IMAP< * 5 FETCH (RFC822.SIZE 2933)
706 * fetchmail: IMAP< * 6 FETCH (RFC822.SIZE 1854)
707 * fetchmail: IMAP< * 7 FETCH (RFC822.SIZE 34054)
708 * fetchmail: IMAP< * 8 FETCH (RFC822.SIZE 5561)
709 * fetchmail: IMAP< * 9 FETCH (RFC822.SIZE 1101)
710 * fetchmail: IMAP< A0004 NO The requested item could not be found.
712 * This means message 1 has only headers. For kicks and grins
713 * you can telnet in and look:
715 * A003 NO The requested item could not be found.
716 * A004 fetch 1 rfc822.header
717 * A004 NO The requested item could not be found.
719 * * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
720 * A006 OK FETCH completed.
722 * To get around this, we terminate the read loop on a NO and count
723 * on the fact that the sizes array has been preinitialized with a
724 * known-bad size value.
726 /* if fetchall is specified, startcount is 1;
727 * else if there is new mail, startcount is first unseen message;
728 * else startcount is greater than count.
730 if (count == startcount)
731 gen_send(sock, "FETCH %d RFC822.SIZE", count);
732 else if (count > startcount)
733 gen_send(sock, "FETCH %d:%d RFC822.SIZE", startcount, count);
734 else /* no unseen messages! */
738 unsigned int num, size;
742 if ((ok = gen_recv(sock, buf, sizeof(buf))))
744 /* we want response matching to be case-insensitive */
745 for (cp = buf; *cp; cp++)
747 /* an untagged NO means that a message was not readable */
748 if (strstr(buf, "* NO"))
750 else if (strstr(buf, "OK") || strstr(buf, "NO"))
752 else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2)
754 if (num > 0 && num <= count)
755 sizes[num - 1] = size;
757 report(stderr, "Warning: ignoring bogus data for message sizes returned by the server.\n");
764 static int imap_is_old(int sock, struct query *ctl, int number)
765 /* is the given message old? */
771 * Expunges change the fetch numbers, but unseen_messages contains
772 * indices from before any expungees were done. So neither the
773 * argument nor the values in message_sequence need to be decremented.
777 for (i = 0; i < unseen; i++)
778 if (unseen_messages[i] == number)
787 static char *skip_token(char *ptr)
789 while(isspace(*ptr)) ptr++;
790 while(!isspace(*ptr) && !iscntrl(*ptr)) ptr++;
791 while(isspace(*ptr)) ptr++;
795 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
796 /* request headers of nth message */
798 char buf [MSGBUFSIZE+1];
801 /* expunges change the fetch numbers */
805 * This is blessed by RFC1176, RFC1730, RFC2060.
806 * According to the RFCs, it should *not* set the \Seen flag.
808 gen_send(sock, "FETCH %d RFC822.HEADER", number);
810 /* looking for FETCH response */
816 if ((ok = gen_recv(sock, buf, sizeof(buf))))
818 ptr = skip_token(buf); /* either "* " or "AXXXX " */
819 if (sscanf(ptr, "%d FETCH (%*s {%d}", &num, lenp) == 2)
821 /* try to recover from chronically fucked-up M$ Exchange servers */
822 else if (!strncmp(ptr, "NO", 2))
824 /* wait for a tagged response */
825 if (strstr (buf, "* NO"))
827 return(PS_TRANSIENT);
829 else if (!strncmp(ptr, "BAD", 3))
831 /* wait for a tagged response */
832 if (strstr (buf, "* BAD"))
834 return(PS_TRANSIENT);
844 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
845 /* request body of nth message */
847 char buf [MSGBUFSIZE+1], *cp;
850 /* expunges change the fetch numbers */
854 * If we're using IMAP4, we can fetch the message without setting its
855 * seen flag. This is good! It means that if the protocol exchange
856 * craps out during the message, it will still be marked `unseen' on
859 * However...*don't* do this if we're using keep to suppress deletion!
860 * In that case, marking the seen flag is the only way to prevent the
861 * message from being re-fetched on subsequent runs (and according
862 * to RFC2060 p.43 this fetch should set Seen as a side effect).
864 * According to RFC2060, and Mark Crispin the IMAP maintainer,
865 * FETCH %d BODY[TEXT] and RFC822.TEXT are "functionally
866 * equivalent". However, we know of at least one server that
867 * treats them differently in the presence of MIME attachments;
868 * the latter form downloads the attachment, the former does not.
869 * The server is InterChange, and the fool who implemented this
870 * misfeature ought to be strung up by his thumbs.
872 * When I tried working around this by disabling use of the 4rev1 form,
873 * I found that doing this breaks operation with M$ Exchange.
874 * Annoyingly enough, Exchange's refusal to cope is technically legal
875 * under RFC2062. Trust Microsoft, the Great Enemy of interoperability
876 * standards, to find a way to make standards compliance irritating....
878 switch (imap_version)
880 case IMAP4rev1: /* RFC 2060 */
882 gen_send(sock, "FETCH %d BODY.PEEK[TEXT]", number);
884 gen_send(sock, "FETCH %d BODY[TEXT]", number);
887 case IMAP4: /* RFC 1730 */
889 gen_send(sock, "FETCH %d RFC822.TEXT.PEEK", number);
891 gen_send(sock, "FETCH %d RFC822.TEXT", number);
894 default: /* RFC 1176 */
895 gen_send(sock, "FETCH %d RFC822.TEXT", number);
899 /* looking for FETCH response */
903 if ((ok = gen_recv(sock, buf, sizeof(buf))))
906 (!strstr(buf+4, "FETCH") || sscanf(buf+2, "%d", &num) != 1);
912 * Try to extract a length from the FETCH response. RFC2060 requires
913 * it to be present, but at least one IMAP server (Novell GroupWise)
916 if ((cp = strchr(buf, '{')))
917 *lenp = atoi(cp + 1);
919 *lenp = -1; /* missing length part in FETCH reponse */
924 static int imap_trail(int sock, struct query *ctl, int number)
925 /* discard tail of FETCH response after reading message text */
927 /* expunges change the fetch numbers */
928 /* number -= expunged; */
932 char buf[MSGBUFSIZE+1];
935 if ((ok = gen_recv(sock, buf, sizeof(buf))))
938 /* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
939 if (strstr(buf, "OK"))
944 * Any IMAP server that fails to set Seen on a BODY[TEXT]
945 * fetch violates RFC2060 p.43 (top). This becomes an issue
946 * when keep is on, because seen messages aren't deleted and
947 * get refetched on each poll. As a workaround, if keep is on
948 * we can set the Seen flag explicitly.
950 * This code isn't used yet because we don't know of any IMAP
951 * servers broken in this way.
954 if ((ok = gen_transact(sock,
955 imap_version == IMAP4
956 ? "STORE %d +FLAGS.SILENT (\\Seen)"
957 : "STORE %d +FLAGS (\\Seen)",
960 #endif /* __UNUSED__ */
966 static int imap_delete(int sock, struct query *ctl, int number)
967 /* set delete flag for given message */
971 /* expunges change the fetch numbers */
975 * Use SILENT if possible as a minor throughput optimization.
976 * Note: this has been dropped from IMAP4rev1.
978 * We set Seen because there are some IMAP servers (notably HP
979 * OpenMail) that do message-receipt DSNs, but only when the seen
980 * bit is set. This is the appropriate time -- we get here right
981 * after the local SMTP response that says delivery was
984 if ((ok = gen_transact(sock,
985 imap_version == IMAP4
986 ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
987 : "STORE %d +FLAGS (\\Seen \\Deleted)",
994 * We do an expunge after expunge_period messages, rather than
995 * just before quit, so that a line hit during a long session
996 * won't result in lots of messages being fetched again during
999 if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
1000 internal_expunge(sock);
1005 static int imap_logout(int sock, struct query *ctl)
1006 /* send logout command */
1008 /* if any un-expunged deletions remain, ship an expunge now */
1010 internal_expunge(sock);
1013 /* Memory clean-up */
1014 if (unseen_messages)
1015 free(unseen_messages);
1016 #endif /* USE_SEARCH */
1018 return(gen_transact(sock, "LOGOUT"));
1021 const static struct method imap =
1023 "IMAP", /* Internet Message Access Protocol */
1027 #else /* INET6_ENABLE */
1028 143, /* standard IMAP2bis/IMAP4 port */
1029 993, /* ssl IMAP2bis/IMAP4 port */
1030 #endif /* INET6_ENABLE */
1031 TRUE, /* this is a tagged protocol */
1032 FALSE, /* no message delimiter */
1033 imap_ok, /* parse command response */
1034 imap_getauth, /* get authorization */
1035 imap_getrange, /* query range of messages */
1036 imap_getsizes, /* get sizes of messages (used for ESMTP SIZE option) */
1037 imap_is_old, /* no UID check */
1038 imap_fetch_headers, /* request given message headers */
1039 imap_fetch_body, /* request given message body */
1040 imap_trail, /* eat message trailer */
1041 imap_delete, /* delete the message */
1042 imap_logout, /* expunge and exit */
1043 TRUE, /* yes, we can re-poll */
1046 int doIMAP(struct query *ctl)
1047 /* retrieve messages using IMAP Version 2bis or Version 4 */
1049 return(do_protocol(ctl, &imap));
1052 /* imap.c ends here */