static int expunge_period = 1;
/* mailbox variables initialized in imap_getrange() */
-static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
+static int count = 0, oldcount = 0, recentcount = 0, unseen = 0, deletions = 0;
static unsigned int startcount = 1;
static int expunged = 0;
static unsigned int *unseen_messages;
static int actual_deletions = 0;
/* for "IMAP> IDLE" */
-static int saved_timeout = 0;
+static int saved_timeout = 0, idle_timeout = 0;
+static time_t idle_start_time = 0;
-static int imap_ok(int sock, char *argbuf)
+static int imap_untagged_response(int sock, const char *buf)
+/* interpret untagged status responses */
+{
+ /* For each individual check, use a BLANK before the word to avoid
+ * confusion with the \Recent flag or similar */
+ if (stage == STAGE_GETAUTH
+ && !strncmp(buf, "* CAPABILITY", 12))
+ {
+ strlcpy(capabilities, buf + 12, sizeof(capabilities));
+ }
+ else if (stage == STAGE_GETAUTH
+ && !strncmp(buf, "* PREAUTH", 9))
+ {
+ preauth = TRUE;
+ }
+ else if (stage != STAGE_LOGOUT
+ && !strncmp(buf, "* BYE", 5))
+ {
+ /* log the unexpected bye from server as we expect the
+ * connection to be cut-off after this */
+ if (outlevel > O_SILENT)
+ report(stderr, GT_("Received BYE response from IMAP server: %s"), buf + 5);
+ }
+ 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 ((unsigned)count > INT_MAX/sizeof(int))
+ {
+ report(stderr, GT_("bogus message count!"));
+ return(PS_PROTOCOL);
+ }
+ if ((recentcount = count - oldcount) < 0)
+ recentcount = 0;
+
+ /*
+ * 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_GETRANGE;
+ }
+ }
+ /* we now compute recentcount as a difference between
+ * new and old EXISTS, hence disable RECENT check */
+# if 0
+ else if (strstr(buf, " RECENT"))
+ {
+ recentcount = atoi(buf+2);
+ }
+# endif
+ else if (strstr(buf, " EXPUNGE"))
+ {
+ /* the response "* 10 EXPUNGE" means that the currently
+ * tenth (i.e. only one) message has been deleted */
+ if (atoi(buf+2) > 0)
+ {
+ if (count > 0)
+ count--;
+ if (oldcount > 0)
+ oldcount--;
+ /* We do expect an EXISTS response immediately
+ * after this, so this updation of recentcount is
+ * just a precaution! */
+ if ((recentcount = count - oldcount) < 0)
+ recentcount = 0;
+ actual_deletions++;
+ }
+ }
+ /*
+ * 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 (stage == STAGE_GETRANGE
+ && !check_only && strstr(buf, "[READ-ONLY]"))
+ {
+ return(PS_LOCKBUSY);
+ }
+ else
+ {
+ return(PS_UNTAGGED);
+ }
+ return(PS_SUCCESS);
+}
+
+static int imap_response(int sock, char *argbuf)
/* parse command response */
{
char buf[MSGBUFSIZE+1];
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 */
+ /* untagged responses start with "* " */
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 ((unsigned)count > INT_MAX/sizeof(int))
- {
- report(stderr, GT_("bogus message count!"));
- return(PS_PROTOCOL);
- }
-
- /*
- * 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"))
- {
- recentcount = atoi(buf+2);
- }
- else if (strstr(buf, " EXPUNGE"))
+ ok = imap_untagged_response(sock, buf);
+ if (ok == PS_UNTAGGED)
{
- /* the response "* 10 EXPUNGE" means that the currently
- * tenth (i.e. only one) message has been deleted */
- if (atoi(buf+2) > 0)
+ if (argbuf && stage != STAGE_IDLE && tag[0] != '\0')
{
- if (count > 0)
- count--;
- /* Some servers do not report RECENT after an EXPUNGE.
- * For such servers, assume that the mail being
- * expunged is a recent one. For other servers, we
- * should get an updated RECENT report later and this
- * assumption will have no effect. */
- if (recentcount > 0)
- recentcount--;
- actual_deletions++;
+ /* if there is an unmatched response, pass it back to
+ * the calling function for further analysis. The
+ * calling function should call imap_response() again
+ * to read the remaining response */
+ strcpy(argbuf, buf);
+ return(ok);
}
}
- 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);
- }
+ else if (ok != PS_SUCCESS)
+ return(ok);
+ }
+
+ if (stage == STAGE_IDLE)
+ {
+ /* reduce the timeout: servers may not reset their timeout
+ * when they send some information asynchronously */
+ mytimeout = idle_timeout - (time((time_t *) NULL) - idle_start_time);
+ if (mytimeout <= 0)
+ return(PS_IDLETIMEOUT);
}
} while
(tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
{
if (stage == STAGE_GETAUTH)
return(PS_AUTHFAIL); /* RFC2060, 6.2.2 */
+ else if (stage == STAGE_GETSIZES)
+ return(PS_SUCCESS); /* see comments in imap_getpartialsizes() */
else
return(PS_ERROR);
}
}
}
+static int imap_ok(int sock, char *argbuf)
+/* parse command response */
+{
+ int ok;
+
+ while ((ok = imap_response(sock, argbuf)) == PS_UNTAGGED)
+ ; /* wait for the tagged response */
+ return(ok);
+}
+
#ifdef NTLM_ENABLE
#include "ntlm.h"
strcat(msgbuf,"\r\n");
SockWrite (sock, msgbuf, strlen (msgbuf));
- if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
- return result;
-
- if (strstr (msgbuf, "OK"))
+ result = imap_ok (sock, NULL);
+ if (result == PS_SUCCESS)
return PS_SUCCESS;
else
return PS_AUTHFAIL;
peek_capable = (imap_version >= IMAP4);
}
+static int do_authcert (int sock, char *command, const char *name)
+/* do authentication "external" (authentication provided by client cert) */
+{
+ char buf[256];
+
+ if (name && name[0])
+ {
+ size_t len = strlen(name);
+ if ((len / 3) + ((len % 3) ? 4 : 0) < sizeof(buf))
+ to64frombits (buf, name, strlen(name));
+ else
+ return PS_AUTHFAIL; /* buffer too small. */
+ }
+ else
+ buf[0]=0;
+ return gen_transact(sock, "%s EXTERNAL %s",command,buf);
+}
+
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 */
-
+ int got_tls = 0;
+#endif
(void)greeting;
+
/*
* Assumption: expunges are cheap, so we want to do them
* after every message unless user said otherwise.
}
#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);
+ if (maybe_tls(ctl)) {
+ char *commonname;
+
+ commonname = ctl->server.pollname;
+ if (ctl->server.via)
+ commonname = ctl->server.via;
+ if (ctl->sslcommonname)
+ commonname = ctl->sslcommonname;
+
+ if (strstr(capabilities, "STARTTLS"))
+ {
+ /* Use "tls1" rather than ctl->sslproto because tls1 is the only
+ * protocol that will work with STARTTLS. Don't need to worry
+ * whether TLS is mandatory or opportunistic unless SSLOpen() fails
+ * (see below). */
+ if (gen_transact(sock, "STARTTLS") == PS_SUCCESS
+ && SSLOpen(sock, ctl->sslcert, ctl->sslkey, "tls1", ctl->sslcertck,
+ ctl->sslcertpath, ctl->sslfingerprint, commonname,
+ ctl->server.pollname, &ctl->remotename) != -1)
+ {
+ /*
+ * 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."
+ *
+ * Now that we're confident in our TLS connection we can
+ * guarantee a secure capability re-probe.
+ */
+ got_tls = 1;
+ capa_probe(sock, ctl);
+ if (outlevel >= O_VERBOSE)
+ {
+ report(stdout, GT_("%s: upgrade to TLS succeeded.\n"), commonname);
+ }
+ }
+ }
+
+ if (!got_tls) {
+ if (must_tls(ctl)) {
+ /* Config required TLS but we couldn't guarantee it, so we must
+ * stop. */
+ report(stderr, GT_("%s: upgrade to TLS failed.\n"), commonname);
+ return PS_SOCKET;
+ } else {
+ if (outlevel >= O_VERBOSE) {
+ report(stdout, GT_("%s: opportunistic upgrade to TLS failed, trying to continue\n"), commonname);
+ }
+ /* We don't know whether the connection is in a working state, so
+ * test by issuing a NOOP. */
+ if (gen_transact(sock, "NOOP") != PS_SUCCESS) {
+ /* Not usable. Empty sslproto to force an unencrypted
+ * connection on the next attempt, and repoll. */
+ ctl->sslproto = xstrdup("");
+ return PS_REPOLL;
+ }
+ /* Usable. Proceed with authenticating insecurely. */
+ }
+ }
}
#endif /* SSL_ENABLE */
*/
ok = PS_AUTHFAIL;
+ /* Yahoo hack - we'll just try ID if it was offered by the server,
+ * and IGNORE errors. */
+ {
+ char *tmp = strstr(capabilities, " ID");
+ if (tmp && !isalnum(tmp[3]) && strstr(ctl->server.via ? ctl->server.via : ctl->server.pollname, "yahoo.com")) {
+ (void)gen_transact(sock, "ID (\"guid\" \"1\")");
+ }
+ }
+
+ if ((ctl->server.authenticate == A_ANY
+ || ctl->server.authenticate == A_EXTERNAL)
+ && strstr(capabilities, "AUTH=EXTERNAL"))
+ {
+ ok = do_authcert(sock, "AUTHENTICATE", ctl->remotename);
+ if (ok)
+ {
+ /* SASL cancellation of authentication */
+ gen_send(sock, "*");
+ if (ctl->server.authenticate != A_ANY)
+ return ok;
+ } else {
+ return ok;
+ }
+ }
+
#ifdef GSSAPI
if ((ctl->server.authenticate == A_ANY
|| ctl->server.authenticate == A_GSSAPI)
size_t rnl, pwl;
rnl = 2 * strlen(ctl->remotename) + 1;
pwl = 2 * strlen(ctl->password) + 1;
- remotename = xmalloc(rnl);
- password = xmalloc(pwl);
+ remotename = (char *)xmalloc(rnl);
+ password = (char *)xmalloc(pwl);
imap_canonicalize(remotename, ctl->remotename, rnl);
imap_canonicalize(password, ctl->password, pwl);
snprintf(shroud, sizeof (shroud), "\"%s\"", password);
ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
+ memset(shroud, 0x55, sizeof(shroud));
shroud[0] = '\0';
+ memset(password, 0x55, strlen(password));
free(password);
free(remotename);
-#ifdef SSL_ENABLE
- /* this is for servers which claim to support TLS, but actually
- * don't! */
- if (did_stls && ok == PS_SOCKET && !ctl->sslproto && !ctl->wehaveauthed)
- {
- ctl->sslproto = xstrdup("");
- /* repoll immediately */
- ok = PS_REPOLL;
- }
-#endif
if (ok)
{
/* SASL cancellation of authentication */
/* 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 */
+ mytimeout = idle_timeout = 1680; /* 28 min */
+ time(&idle_start_time);
stage = STAGE_IDLE;
/* enter IDLE mode */
ok = gen_transact(sock, "IDLE");
report(stdout, "IMAP> DONE\n");
/* reset stage and timeout here: we are not idling any more */
mytimeout = saved_timeout;
- stage = STAGE_FETCH;
+ stage = STAGE_GETRANGE;
/* get OK IDLE message */
ok = imap_ok(sock, NULL);
}
* notification out of the blue. This is in compliance
* with RFC 2060 section 5.3. Wait for that with a low
* timeout */
- mytimeout = 28;
+ mytimeout = idle_timeout = 28;
+ time(&idle_start_time);
stage = STAGE_IDLE;
/* We are waiting for notification; no tag needed */
tag[0] = '\0';
/* restore normal timeout value */
set_timeout(0);
mytimeout = saved_timeout;
- stage = STAGE_FETCH;
+ stage = STAGE_GETRANGE;
return(ok);
}
+/* maximum number of numbers we can process in "SEARCH" response */
+# define IMAP_SEARCH_MAX 1000
+
+static int imap_search(int sock, struct query *ctl, int count)
+/* search for unseen messages */
+{
+ int ok, first, last;
+ char buf[MSGBUFSIZE+1], *cp;
+
+ /* Don't count deleted messages. Enabled only for IMAP4 servers or
+ * higher and only when keeping mails. This flag will have an
+ * effect only when user has marked some unread mails for deletion
+ * using another e-mail client. */
+ flag skipdeleted = (imap_version >= IMAP4) && ctl->keep;
+ const char *undeleted;
+
+ /* Skip range search if there are less than or equal to
+ * IMAP_SEARCH_MAX mails. */
+ flag skiprangesearch = (count <= IMAP_SEARCH_MAX);
+
+ /* startcount is higher than count so that if there are no
+ * unseen messages, imap_getsizes() will not need to do
+ * anything! */
+ startcount = count + 1;
+
+ for (first = 1, last = IMAP_SEARCH_MAX; first <= count; first += IMAP_SEARCH_MAX, last += IMAP_SEARCH_MAX)
+ {
+ if (last > count)
+ last = count;
+
+restartsearch:
+ undeleted = (skipdeleted ? " UNDELETED" : "");
+ if (skiprangesearch)
+ gen_send(sock, "SEARCH UNSEEN%s", undeleted);
+ else if (last == first)
+ gen_send(sock, "SEARCH %d UNSEEN%s", last, undeleted);
+ else
+ gen_send(sock, "SEARCH %d:%d UNSEEN%s", first, last, undeleted);
+ while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
+ {
+ if ((cp = strstr(buf, "* SEARCH")))
+ {
+ char *ep;
+
+ cp += 8; /* skip "* SEARCH" */
+ while (*cp && unseen < count)
+ {
+ /* skip whitespace */
+ while (*cp && isspace((unsigned char)*cp))
+ cp++;
+ if (*cp)
+ {
+ unsigned long um;
+
+ errno = 0;
+ um = strtoul(cp,&ep,10);
+ if (errno == 0 && um <= UINT_MAX && um <= (unsigned)count)
+ {
+ unseen_messages[unseen++] = um;
+ if (outlevel >= O_DEBUG)
+ report(stdout, GT_("%lu is unseen\n"), um);
+ if (startcount > um)
+ startcount = um;
+ }
+ cp = ep;
+ }
+ }
+ }
+ }
+ /* if there is a protocol error on the first loop, try a
+ * different search command */
+ if (ok == PS_ERROR && first == 1)
+ {
+ if (skipdeleted)
+ {
+ /* retry with "SEARCH 1:1000 UNSEEN" */
+ skipdeleted = FALSE;
+ goto restartsearch;
+ }
+ if (!skiprangesearch)
+ {
+ /* retry with "SEARCH UNSEEN" */
+ skiprangesearch = TRUE;
+ goto restartsearch;
+ }
+ /* try with "FETCH 1:n FLAGS" */
+ goto fetchflags;
+ }
+ if (ok != PS_SUCCESS)
+ return(ok);
+ /* loop back only when searching in range */
+ if (skiprangesearch)
+ break;
+ }
+ return(PS_SUCCESS);
+
+fetchflags:
+ if (count == 1)
+ gen_send(sock, "FETCH %d FLAGS", count);
+ else
+ gen_send(sock, "FETCH %d:%d FLAGS", 1, count);
+ while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
+ {
+ unsigned int num;
+
+ /* expected response format:
+ * IMAP< * 1 FETCH (FLAGS (\Seen))
+ * IMAP< * 2 FETCH (FLAGS (\Seen \Deleted))
+ * IMAP< * 3 FETCH (FLAGS ())
+ * IMAP< * 4 FETCH (FLAGS (\Recent))
+ * IMAP< * 5 FETCH (UID 10 FLAGS (\Recent))
+ */
+ if (unseen < count
+ && sscanf(buf, "* %u FETCH ", &num) == 1
+ && num >= 1 && num <= count
+ && strstr(buf, "FLAGS ")
+ && !strstr(buf, "\\SEEN")
+ && !strstr(buf, "\\DELETED"))
+ {
+ unseen_messages[unseen++] = num;
+ if (outlevel >= O_DEBUG)
+ report(stdout, GT_("%u is unseen\n"), num);
+ if (startcount > num)
+ startcount = num;
+ }
+ }
+ return(ok);
+}
+
static int imap_getrange(int sock,
struct query *ctl,
const char *folder,
/* get range of messages to be fetched */
{
int ok;
- char buf[MSGBUFSIZE+1], *cp;
/* find out how many messages are waiting */
*bytes = -1;
* 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
+ * returned EXISTS on detecting new mail. if recentcount is 0, wait
* for new mail.
*
* this is a while loop because imap_idle() might return on other
}
else
{
- count = 0;
+ oldcount = count = 0;
ok = gen_transact(sock,
check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
folder ? folder : "INBOX");
"%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) {
- ok = imap_idle(sock);
- if (ok)
- {
- report(stderr, GT_("re-poll failed\n"));
- return(ok);
- }
- }
-
/*
* We should have an expunge here to
* a) avoid fetching deleted mails during 'fetchall'
"%d messages waiting after expunge\n",
count), count);
}
+
+ if (count == 0 && do_idle)
+ {
+ /* no messages? then we may need to idle until we get some */
+ while (count == 0) {
+ ok = imap_idle(sock);
+ if (ok)
+ {
+ report(stderr, GT_("re-poll failed\n"));
+ return(ok);
+ }
+ }
+ if (outlevel >= O_DEBUG)
+ report(stdout, ngettext("%d message waiting after re-poll\n",
+ "%d messages waiting after re-poll\n",
+ count), count);
+ }
}
- *countp = count;
+ *countp = oldcount = count;
recentcount = 0;
startcount = 1;
{
if (unseen_messages)
free(unseen_messages);
- unseen_messages = xmalloc(count * sizeof(unsigned int));
+ unseen_messages = (unsigned int *)xmalloc(count * sizeof(unsigned int));
memset(unseen_messages, 0, count * sizeof(unsigned int));
unseen = 0;
- /* 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)
- {
- report(stderr, GT_("search for unseen messages failed\n"));
- return(PS_PROTOCOL);
- }
- else if ((cp = strstr(buf, "* SEARCH")))
- {
- char *ep;
-
- cp += 8; /* skip "* SEARCH" */
- /* startcount is higher than count so that if there are no
- * unseen messages, imap_getsizes() will not need to do
- * anything! */
- startcount = count + 1;
-
- while (*cp && unseen < count)
- {
- /* skip whitespace */
- while (*cp && isspace((unsigned char)*cp))
- cp++;
- if (*cp)
- {
- unsigned long um;
-
- errno = 0;
- um = strtoul(cp,&ep,10);
- if (errno == 0 && um <= UINT_MAX && um <= (unsigned)count)
- {
- unseen_messages[unseen++] = um;
- if (outlevel >= O_DEBUG)
- report(stdout, GT_("%lu is unseen\n"), um);
- if (startcount > um)
- startcount = um;
- }
- cp = ep;
- }
- }
- }
- } while
- (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
+ ok = imap_search(sock, ctl, count);
+ if (ok != 0)
+ {
+ report(stderr, GT_("search for unseen messages failed\n"));
+ return(ok);
+ }
if (outlevel >= O_DEBUG && unseen > 0)
report(stdout, GT_("%u is first unseen\n"), startcount);
/* capture the sizes of messages #first-#last */
{
char buf [MSGBUFSIZE+1];
+ int ok;
/*
* Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
* * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
* A006 OK FETCH completed.
*
- * To get around this, we terminate the read loop on a NO and count
+ * To get around this, we treat the final NO as success and count
* on the fact that the sizes array has been preinitialized with a
* known-bad size value.
*/
gen_send(sock, "FETCH %d:%d RFC822.SIZE", first, last);
else /* no unseen messages! */
return(PS_SUCCESS);
- for (;;)
+ while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
{
unsigned int size;
- int ok, num;
- char *cp;
+ int num;
- if ((ok = gen_recv(sock, buf, sizeof(buf))))
- return(ok);
- /* we want response matching to be case-insensitive */
- for (cp = buf; *cp; cp++)
- *cp = toupper((unsigned char)*cp);
- /* an untagged NO means that a message was not readable */
- if (strstr(buf, "* NO"))
- ;
- else if (strstr(buf, "OK") || strstr(buf, "NO"))
- break;
- else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %u)", &num, &size) == 2
+ if (sscanf(buf, "* %d FETCH (RFC822.SIZE %u)", &num, &size) == 2
/* some servers (like mail.internode.on.net bld-mail04) return UID information here
*
* IMAP> A0005 FETCH 1 RFC822.SIZE
GT_("Warning: ignoring bogus data for message sizes returned by the server.\n"));
}
}
-
- return(PS_SUCCESS);
+ return(ok);
}
static int imap_getsizes(int sock, int count, int *sizes)
if (num != number)
return(PS_ERROR);
+ /* Understand "NIL" as length => no body present
+ * (MS Exchange, BerliOS Bug #11980) */
+ if (strstr(buf+10, "NIL)")) {
+ *lenp = 0;
+ return PS_SUCCESS;
+ }
+
/*
* Try to extract a length from the FETCH response. RFC2060 requires
* it to be present, but at least one IMAP server (Novell GroupWise)
/* number -= expunged; */
(void)ctl;
- for (;;)
- {
- char buf[MSGBUFSIZE+1], *t;
- int ok;
-
- if ((ok = gen_recv(sock, buf, sizeof(buf))))
- return(ok);
+ (void)tag;
- /* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
- 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);
+ return imap_ok(sock, NULL);
}
static int imap_delete(int sock, struct query *ctl, int number)
/* mark the given message as seen */
{
(void)ctl;
+
+ /* expunges change the message numbers */
+ number -= expunged;
+
return(gen_transact(sock,
imap_version == IMAP4
? "STORE %d +FLAGS.SILENT (\\Seen)"
static const struct method imap =
{
"IMAP", /* Internet Message Access Protocol */
- "imap",
- "imaps",
+ "imap", /* service (plain and TLS) */
+ "imaps", /* service (SSL) */
TRUE, /* this is a tagged protocol */
FALSE, /* no message delimiter */
imap_ok, /* parse command response */