#if defined(STDC_HEADERS)
#include <stdlib.h>
#include <limits.h>
+#include <errno.h>
#endif
#include "fetchmail.h"
#include "socket.h"
#include "i18n.h"
-#if OPIE_ENABLE
-#endif /* OPIE_ENABLE */
-
-#ifndef strstr /* glibc-2.1 declares this as a macro */
-extern char *strstr(); /* needed on sysV68 R3V7.1. */
-#endif /* strstr */
-
/* imap_version values */
#define IMAP2 -1 /* IMAP2 or IMAP2BIS, RFC1176 */
#define IMAP4 0 /* IMAP4 rev 0, RFC1730 */
#define IMAP4rev1 1 /* IMAP4 rev 1, RFC2060 */
+/* global variables: please reinitialize them explicitly for proper
+ * working in daemon mode */
+
+/* TODO: session variables to be initialized before server greeting */
+static int preauth = FALSE;
+
+/* session variables initialized in capa_probe() or imap_getauth() */
+static char capabilities[MSGBUFSIZE+1];
+static int imap_version = IMAP4;
+static flag do_idle = FALSE, has_idle = FALSE;
+static int expunge_period = 1;
+
+/* mailbox variables initialized in imap_getrange() */
static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
static unsigned int startcount = 1;
-static int expunged, expunge_period, saved_timeout = 0;
-static int imap_version, preauth;
-static flag do_idle;
-static char capabilities[MSGBUFSIZE+1];
+static int expunged = 0;
static unsigned int *unseen_messages;
+/* for "IMAP> EXPUNGE" */
+static int recentcount_ok = 0;
+
+/* for "IMAP> IDLE" */
+static int saved_timeout = 0;
+
static int imap_ok(int sock, char *argbuf)
/* parse command response */
{
/* all tokens in responses are caseblind */
for (cp = buf; *cp; cp++)
- if (islower(*cp))
- *cp = toupper(*cp);
+ if (islower((unsigned char)*cp))
+ *cp = toupper((unsigned char)*cp);
+
+ /* interpret untagged status responses
+ * First check if we really have an untagged response, starting
+ * with "*" SPACE. Then, for each individual check, use a BLANK
+ * before the word to avoid confusion with the \Recent flag or
+ * similar */
+ if (buf[0] == '*' && buf[1] == ' ') {
+ if (strstr(buf, " CAPABILITY")) {
+ strlcpy(capabilities, buf + 12, sizeof(capabilities));
+ }
+ else if (strstr(buf, " EXISTS"))
+ {
+ count = atoi(buf+2);
+ /*
+ * Don't trust the message count passed by the server.
+ * Without this check, it might be possible to do a
+ * DNS-spoofing attack that would pass back a ridiculous
+ * count, and allocate a malloc area that would overlap
+ * a portion of the stack.
+ */
+ if (count > INT_MAX/sizeof(int))
+ {
+ report(stderr, GT_("bogus message count!"));
+ return(PS_PROTOCOL);
+ }
- /* interpret untagged status responses */
- if (strstr(buf, "* CAPABILITY"))
- strncpy(capabilities, buf + 12, sizeof(capabilities));
- else if (strstr(buf, "EXISTS"))
- {
- count = atoi(buf+2);
- /*
- * Don't trust the message count passed by the server.
- * Without this check, it might be possible to do a
- * DNS-spoofing attack that would pass back a ridiculous
- * count, and allocate a malloc area that would overlap
- * a portion of the stack.
- */
- if (count > INT_MAX/sizeof(int))
+ /*
+ * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
+ * we can't wait for the tag matching the IDLE; we have to tell the
+ * server the IDLE is finished by shipping back a DONE when we
+ * see an EXISTS. Only after that will a tagged response be
+ * shipped. The idling flag also gets cleared on a timeout.
+ */
+ if (stage == STAGE_IDLE)
+ {
+ /* If IDLE isn't supported, we were only sending NOOPs anyway. */
+ if (has_idle)
+ {
+ /* we do our own write and report here to disable tagging */
+ SockWrite(sock, "DONE\r\n", 6);
+ if (outlevel >= O_MONITOR)
+ report(stdout, "IMAP> DONE\n");
+ }
+
+ mytimeout = saved_timeout;
+ stage = STAGE_FETCH;
+ }
+ }
+ else if (strstr(buf, " RECENT"))
{
- report(stderr, "bogus message count!");
- return(PS_PROTOCOL);
+ recentcount_ok = 1;
+ recentcount = atoi(buf+2);
}
-
- /*
- * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
- * we can't wait for the tag matching the IDLE; we have to tell the
- * server the IDLE is finished by shipping back a DONE when we
- * see an EXISTS. Only after that will a tagged response be
- * shipped. The idling flag also gets cleared on a timeout.
- */
- if (stage == STAGE_IDLE)
+ else if (strstr(buf, " EXPUNGE"))
{
- /* we do our own write and report here to disable tagging */
- SockWrite(sock, "DONE\r\n", 6);
- if (outlevel >= O_MONITOR)
- report(stdout, "IMAP> DONE\n");
-
- mytimeout = saved_timeout;
- stage = STAGE_FETCH;
+ /* the response "* 10 EXPUNGE" means that the currently
+ * tenth (i.e. only one) message has been deleted */
+ if (atoi(buf+2) > 0)
+ count--;
+ if (count < 0)
+ count = 0;
+ }
+ else if (strstr(buf, " PREAUTH"))
+ {
+ preauth = TRUE;
+ }
+ /*
+ * The server may decide to make the mailbox read-only,
+ * which causes fetchmail to go into a endless loop
+ * fetching the same message over and over again.
+ *
+ * However, for check_only, we use EXAMINE which will
+ * mark the mailbox read-only as per the RFC.
+ *
+ * This checks for the condition and aborts if
+ * the mailbox is read-only.
+ *
+ * See RFC 2060 section 6.3.1 (SELECT).
+ * See RFC 2060 section 6.3.2 (EXAMINE).
+ */
+ else if (!check_only && strstr(buf, "[READ-ONLY]"))
+ {
+ return(PS_LOCKBUSY);
}
}
- /* a space is required to avoid confusion with the \Recent flag */
- else if (strstr(buf, " RECENT"))
- {
- recentcount = atoi(buf+2);
- }
- else if (strstr(buf, "PREAUTH"))
- preauth = TRUE;
- /*
- * The server may decide to make the mailbox read-only,
- * which causes fetchmail to go into a endless loop
- * fetching the same message over and over again.
- *
- * However, for check_only, we use EXAMINE which will
- * mark the mailbox read-only as per the RFC.
- *
- * This checks for the condition and aborts if
- * the mailbox is read-only.
- *
- * See RFC 2060 section 6.3.1 (SELECT).
- * See RFC 2060 section 6.3.2 (EXAMINE).
- */
- else if (!check_only && strstr(buf, "[READ-ONLY]"))
- return(PS_LOCKBUSY);
} while
(tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
char *cp;
/* skip the tag */
- for (cp = buf; !isspace(*cp); cp++)
+ for (cp = buf; !isspace((unsigned char)*cp); cp++)
continue;
- while (isspace(*cp))
+ while (isspace((unsigned char)*cp))
cp++;
if (strncasecmp(cp, "OK", 2) == 0)
}
}
-#if NTLM_ENABLE
+#ifdef NTLM_ENABLE
#include "ntlm.h"
-static tSmbNtlmAuthRequest request;
-static tSmbNtlmAuthChallenge challenge;
-static tSmbNtlmAuthResponse response;
-
/*
* NTLM support by Grant Edwards.
*
static int do_imap_ntlm(int sock, struct query *ctl)
{
+ tSmbNtlmAuthRequest request;
+ tSmbNtlmAuthChallenge challenge;
+ tSmbNtlmAuthResponse response;
+
char msgbuf[2048];
int result,len;
-
+
gen_send(sock, "AUTHENTICATE NTLM");
if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
return(i);
}
-static int imap_getauth(int sock, struct query *ctl, char *greeting)
-/* apply for connection authorization */
+static void capa_probe(int sock, struct query *ctl)
+/* set capability variables from a CAPA probe */
{
- int ok = 0;
-#ifdef SSL_ENABLE
- flag did_stls = FALSE;
-#endif /* SSL_ENABLE */
+ int ok;
/* probe to see if we're running IMAP4 and can use RFC822.PEEK */
capabilities[0] = '\0';
/* capability checks are supposed to be caseblind */
for (cp = capabilities; *cp; cp++)
- *cp = toupper(*cp);
+ *cp = toupper((unsigned char)*cp);
/* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
should expect a response in mixed-case */
if (outlevel >= O_DEBUG)
report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
}
- else
- return(ok);
+
+ /*
+ * Handle idling. We depend on coming through here on startup
+ * and after each timeout (including timeouts during idles).
+ */
+ do_idle = ctl->idle;
+ if (ctl->idle)
+ {
+ if (strstr(capabilities, "IDLE"))
+ has_idle = TRUE;
+ else
+ has_idle = FALSE;
+ if (outlevel >= O_VERBOSE)
+ report(stdout, GT_("will idle after poll\n"));
+ }
peek_capable = (imap_version >= IMAP4);
+}
- /*
+static int imap_getauth(int sock, struct query *ctl, char *greeting)
+/* apply for connection authorization */
+{
+ int ok = 0;
+#ifdef SSL_ENABLE
+ flag did_stls = FALSE;
+#endif /* SSL_ENABLE */
+
+ /*
* Assumption: expunges are cheap, so we want to do them
* after every message unless user said otherwise.
*/
else
expunge_period = 1;
- /*
- * Handle idling. We depend on coming through here on startup
- * and after each timeout (including timeouts during idles).
- */
- if (strstr(capabilities, "IDLE") && ctl->idle)
- {
- do_idle = TRUE;
- if (outlevel >= O_VERBOSE)
- report(stdout, GT_("will idle after poll\n"));
- }
+ capa_probe(sock, ctl);
/*
* If either (a) we saw a PREAUTH token in the greeting, or
return(PS_SUCCESS);
}
+#ifdef SSL_ENABLE
+ if ((!ctl->sslproto || !strcmp(ctl->sslproto,"tls1"))
+ && !ctl->use_ssl
+ && strstr(capabilities, "STARTTLS"))
+ {
+ char *realhost;
+
+ realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
+ ok = gen_transact(sock, "STARTTLS");
+
+ /* We use "tls1" instead of ctl->sslproto, as we want STARTTLS,
+ * not other SSL protocols
+ */
+ if (ok == PS_SUCCESS &&
+ SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
+ {
+ if (!ctl->sslproto && !ctl->wehaveauthed)
+ {
+ ctl->sslproto = xstrdup("");
+ /* repoll immediately */
+ return(PS_REPOLL);
+ }
+ report(stderr,
+ GT_("SSL connection failed.\n"));
+ return PS_SOCKET;
+ }
+ did_stls = TRUE;
+
+ /*
+ * RFC 2595 says this:
+ *
+ * "Once TLS has been started, the client MUST discard cached
+ * information about server capabilities and SHOULD re-issue the
+ * CAPABILITY command. This is necessary to protect against
+ * man-in-the-middle attacks which alter the capabilities list prior
+ * to STARTTLS. The server MAY advertise different capabilities
+ * after STARTTLS."
+ */
+ capa_probe(sock, ctl);
+ }
+#endif /* SSL_ENABLE */
+
/*
* Time to authenticate the user.
* Try the protocol variants that don't require passwords first.
if ((ctl->server.authenticate == A_ANY
|| ctl->server.authenticate == A_GSSAPI)
&& strstr(capabilities, "AUTH=GSSAPI"))
- if(ok = do_gssauth(sock, "AUTHENTICATE", ctl->server.truename, ctl->remotename))
+ {
+ if ((ok = do_gssauth(sock, "AUTHENTICATE", "imap",
+ ctl->server.truename, ctl->remotename)))
{
/* SASL cancellation of authentication */
gen_send(sock, "*");
- if(ctl->server.authenticate != A_ANY)
+ if (ctl->server.authenticate != A_ANY)
return ok;
- }
- else
+ } else {
return ok;
+ }
+ }
#endif /* GSSAPI */
#ifdef KERBEROS_V4
}
#endif /* KERBEROS_V4 */
-#ifdef SSL_ENABLE
- if ((!ctl->sslproto || !strcmp(ctl->sslproto,"tls1"))
- && !ctl->use_ssl
- && strstr(capabilities, "STARTTLS"))
- {
- char *realhost;
-
- realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
- gen_transact(sock, "STARTTLS");
-
- /* We use "tls1" instead of ctl->sslproto, as we want STARTTLS,
- * not other SSL protocols
- */
- if (SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
- {
- if (!ctl->sslproto && !ctl->wehaveauthed)
- {
- ctl->sslproto = xstrdup("");
- /* repoll immediately */
- return(PS_REPOLL);
- }
- report(stderr,
- GT_("SSL connection failed.\n"));
- return(PS_AUTHFAIL);
- }
- did_stls = TRUE;
- }
-#endif /* SSL_ENABLE */
-
/*
* No such luck. OK, now try the variants that mask your password
* in a challenge-response.
*/
- if ((ctl->server.authenticate == A_ANY
- || ctl->server.authenticate == A_CRAM_MD5)
- && strstr(capabilities, "AUTH=CRAM-MD5"))
+ if ((ctl->server.authenticate == A_ANY && strstr(capabilities, "AUTH=CRAM-MD5"))
+ || ctl->server.authenticate == A_CRAM_MD5)
{
if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
{
return ok;
}
-#if OPIE_ENABLE
+#ifdef OPIE_ENABLE
if ((ctl->server.authenticate == A_ANY
|| ctl->server.authenticate == A_OTP)
- && strstr(capabilities, "AUTH=X-OTP"))
+ && strstr(capabilities, "AUTH=X-OTP")) {
if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
{
/* SASL cancellation of authentication */
gen_send(sock, "*");
if(ctl->server.authenticate != A_ANY)
return ok;
- }
- else
+ } else {
return ok;
+ }
+ }
#else
if (ctl->server.authenticate == A_OTP)
{
|| ctl->server.authenticate == A_PASSWORD)
{
/* these sizes guarantee no buffer overflow */
- char remotename[NAMELEN*2+1], password[PASSWORDLEN*2+1];
+ char *remotename, *password;
+ size_t rnl, pwl;
+ rnl = 2 * strlen(ctl->remotename) + 1;
+ pwl = 2 * strlen(ctl->password) + 1;
+ remotename = xmalloc(rnl);
+ password = xmalloc(pwl);
- imap_canonicalize(remotename, ctl->remotename, NAMELEN);
- imap_canonicalize(password, ctl->password, PASSWORDLEN);
+ imap_canonicalize(remotename, ctl->remotename, rnl);
+ imap_canonicalize(password, ctl->password, pwl);
- strcpy(shroud, password);
+ snprintf(shroud, sizeof (shroud), "\"%s\"", password);
ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
shroud[0] = '\0';
+ free(password);
+ free(remotename);
#ifdef SSL_ENABLE
/* this is for servers which claim to support TLS, but actually
* don't! */
{
int ok;
+ recentcount_ok = 0;
+
if ((ok = gen_transact(sock, "EXPUNGE")))
return(ok);
+ /* some servers do not report RECENT after an EXPUNGE. in this case,
+ * the previous value of recentcount is just ignored. */
+ if (!recentcount_ok)
+ recentcount = 0;
+
expunged += deletions;
deletions = 0;
}
static int imap_idle(int sock)
-/* start an RFC2177 IDLE */
+/* start an RFC2177 IDLE, or fake one if unsupported */
{
+ int ok;
+
stage = STAGE_IDLE;
saved_timeout = mytimeout;
- mytimeout = 0;
- return (gen_transact(sock, "IDLE"));
+ if (has_idle) {
+ /* special timeout to terminate the IDLE and re-issue it
+ * at least every 28 minutes:
+ * (the server may have an inactivity timeout) */
+ mytimeout = 1680; /* 28 min */
+ /* enter IDLE mode */
+ ok = gen_transact(sock, "IDLE");
+
+ if (ok == PS_IDLETIMEOUT) {
+ /* send "DONE" continuation */
+ SockWrite(sock, "DONE\r\n", 6);
+ if (outlevel >= O_MONITOR)
+ report(stdout, "IMAP> DONE\n");
+ } else
+ /* not idle timeout */
+ return ok;
+ } else { /* no idle support, fake it */
+ /* when faking an idle, we can't assume the server will
+ * send us the new messages out of the blue (RFC2060);
+ * this timeout is potentially the delay before we notice
+ * new mail (can be small since NOOP checking is cheap) */
+ mytimeout = 28;
+ ok = gen_transact(sock, "NOOP");
+ /* if there's an error (not likely) or we just found mail (stage
+ * has changed, timeout has also been restored), we're done */
+ if (ok != 0 || stage != STAGE_IDLE)
+ return(ok);
+
+ /* wait (briefly) for an unsolicited status update */
+ ok = imap_ok(sock, NULL);
+ /* again, this is new mail or an error */
+ if (ok != PS_IDLETIMEOUT)
+ return(ok);
+ }
+
+ /* restore normal timeout value */
+ mytimeout = saved_timeout;
+ stage = STAGE_FETCH;
+
+ /* get OK IDLE message */
+ if (has_idle)
+ return imap_ok(sock, NULL);
+
+ return PS_SUCCESS;
}
static int imap_getrange(int sock,
if (pass > 1)
{
- /*
- * We have to have an expunge here, otherwise the re-poll will
- * infinite-loop picking up un-expunged messages -- unless the
- * expunge period is one and we've been nuking each message
- * just after deletion.
- */
- ok = 0;
- if (deletions) {
- ok = internal_expunge(sock);
- if (ok)
- {
- report(stderr, GT_("expunge failed\n"));
- return(ok);
- }
- }
-
- /*
+ /* deleted mails have already been expunged by
+ * end_mailbox_poll().
+ *
* recentcount is already set here by the last imap command which
* returned RECENT on detecting new mail. if recentcount is 0, wait
* for new mail.
- */
-
- /* some servers do not report RECENT after an EXPUNGE. this check
- * forces an incorrect recentcount to be ignored. */
- if (recentcount > count)
- recentcount = 0;
- /* this is a while loop because imap_idle() might return on other
+ *
+ * this is a while loop because imap_idle() might return on other
* mailbox changes also */
while (recentcount == 0 && do_idle) {
smtp_close(ctl, 1);
if (recentcount == 0)
count = 0;
if (outlevel >= O_DEBUG)
- report(stdout, GT_("%d messages waiting after re-poll\n"), count);
+ report(stdout, ngettext("%d message waiting after re-poll\n",
+ "%d messages waiting after re-poll\n",
+ count), count);
}
else
{
return(ok);
}
else if (outlevel >= O_DEBUG)
- report(stdout, GT_("%d messages waiting after first poll\n"), count);
+ report(stdout, ngettext("%d message waiting after first poll\n",
+ "%d messages waiting after first poll\n",
+ count), count);
/* no messages? then we may need to idle until we get some */
while (count == 0 && do_idle) {
return(ok);
}
if (outlevel >= O_DEBUG)
- report(stdout, GT_("%d messages waiting after expunge\n"), count);
+ report(stdout, ngettext("%d message waiting after expunge\n",
+ "%d messages waiting after expunge\n",
+ count), count);
}
}
memset(unseen_messages, 0, count * sizeof(unsigned int));
unseen = 0;
- gen_send(sock, "SEARCH UNSEEN");
+ /* don't count deleted messages, in case user enabled keep last time */
+ gen_send(sock, "SEARCH UNSEEN NOT DELETED");
do {
ok = gen_recv(sock, buf, sizeof(buf));
if (ok != 0)
while (*cp && unseen < count)
{
/* skip whitespace */
- while (*cp && isspace(*cp))
+ while (*cp && isspace((unsigned char)*cp))
cp++;
if (*cp)
{
unseen = -1;
*newp = unseen;
- count = 0;
expunged = 0;
deletions = 0;
return(PS_SUCCESS);
}
-static int imap_getsizes(int sock, int count, int *sizes)
-/* capture the sizes of all messages */
+static int imap_getpartialsizes(int sock, int first, int last, int *sizes)
+/* capture the sizes of messages #first-#last */
{
char buf [MSGBUFSIZE+1];
* on the fact that the sizes array has been preinitialized with a
* known-bad size value.
*/
- /* if fetchall is specified, startcount is 1;
- * else if there is new mail, startcount is first unseen message;
- * else startcount is greater than count.
- */
- if (count == startcount)
- gen_send(sock, "FETCH %d RFC822.SIZE", count);
- else if (count > startcount)
- gen_send(sock, "FETCH %d:%d RFC822.SIZE", startcount, count);
+
+ /* expunges change the fetch numbers */
+ first -= expunged;
+ last -= expunged;
+
+ if (last == first)
+ gen_send(sock, "FETCH %d RFC822.SIZE", last);
+ else if (last > first)
+ gen_send(sock, "FETCH %d:%d RFC822.SIZE", first, last);
else /* no unseen messages! */
return(PS_SUCCESS);
for (;;)
return(ok);
/* we want response matching to be case-insensitive */
for (cp = buf; *cp; cp++)
- *cp = toupper(*cp);
+ *cp = toupper((unsigned char)*cp);
/* an untagged NO means that a message was not readable */
if (strstr(buf, "* NO"))
;
break;
else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2)
{
- if (num > 0 && num <= count)
- sizes[num - 1] = size;
+ if (num >= first && num <= last)
+ sizes[num - first] = size;
else
report(stderr, "Warning: ignoring bogus data for message sizes returned by the server.\n");
}
return(PS_SUCCESS);
}
+static int imap_getsizes(int sock, int count, int *sizes)
+/* capture the sizes of all messages */
+{
+ return imap_getpartialsizes(sock, 1, count, sizes);
+}
+
static int imap_is_old(int sock, struct query *ctl, int number)
/* is the given message old? */
{
static char *skip_token(char *ptr)
{
- while(isspace(*ptr)) ptr++;
- while(!isspace(*ptr) && !iscntrl(*ptr)) ptr++;
- while(isspace(*ptr)) ptr++;
+ while(isspace((unsigned char)*ptr)) ptr++;
+ while(!isspace((unsigned char)*ptr) && !iscntrl((unsigned char)*ptr)) ptr++;
+ while(isspace((unsigned char)*ptr)) ptr++;
return(ptr);
}
/*
* Try to extract a length from the FETCH response. RFC2060 requires
* it to be present, but at least one IMAP server (Novell GroupWise)
- * botches this.
+ * botches this. The overflow check is needed because of a broken
+ * server called dbmail that returns huge garbage lengths.
*/
- if ((cp = strchr(buf, '{')))
- *lenp = atoi(cp + 1);
+ if ((cp = strchr(buf, '{'))) {
+ errno = 0;
+ *lenp = (int)strtol(cp + 1, (char **)NULL, 10);
+ if (errno == ERANGE && (*lenp == LONG_MAX || *lenp == LONG_MIN))
+ *lenp = -1; /* length is too big/small for us to handle */
+ }
else
*lenp = -1; /* missing length part in FETCH reponse */
return(PS_SUCCESS);
}
-static int imap_trail(int sock, struct query *ctl, int number)
+static int imap_trail(int sock, struct query *ctl, int number, const char *tag)
/* discard tail of FETCH response after reading message text */
{
/* expunges change the fetch numbers */
for (;;)
{
- char buf[MSGBUFSIZE+1];
+ char buf[MSGBUFSIZE+1], *t;
int ok;
if ((ok = gen_recv(sock, buf, sizeof(buf))))
return(ok);
/* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
- if (strstr(buf, "OK"))
- break;
+ if (strncmp(buf, tag, strlen(tag)) == 0) {
+ t = buf + strlen(tag);
+ t += strspn(t, " \t");
+ if (strncmp(t, "OK", 2) == 0)
+ break;
+ }
}
return(PS_SUCCESS);
number));
}
+static int imap_end_mailbox_poll(int sock, struct query *ctl)
+/* cleanup mailbox before we idle or switch to another one */
+{
+ if (deletions)
+ internal_expunge(sock);
+ return(PS_SUCCESS);
+}
+
static int imap_logout(int sock, struct query *ctl)
/* send logout command */
{
return(gen_transact(sock, "LOGOUT"));
}
-const static struct method imap =
+static const struct method imap =
{
"IMAP", /* Internet Message Access Protocol */
-#if INET6_ENABLE
"imap",
"imaps",
-#else /* INET6_ENABLE */
- 143, /* standard IMAP2bis/IMAP4 port */
- 993, /* ssl IMAP2bis/IMAP4 port */
-#endif /* INET6_ENABLE */
TRUE, /* this is a tagged protocol */
FALSE, /* no message delimiter */
imap_ok, /* parse command response */
imap_getauth, /* get authorization */
imap_getrange, /* query range of messages */
imap_getsizes, /* get sizes of messages (used for ESMTP SIZE option) */
+ imap_getpartialsizes, /* get sizes of subset of messages (used for ESMTP SIZE option) */
imap_is_old, /* no UID check */
imap_fetch_headers, /* request given message headers */
imap_fetch_body, /* request given message body */
imap_trail, /* eat message trailer */
imap_delete, /* delete the message */
imap_mark_seen, /* how to mark a message as seen */
+ imap_end_mailbox_poll, /* end-of-mailbox processing */
imap_logout, /* expunge and exit */
TRUE, /* yes, we can re-poll */
};