]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
Merge Fabrice Bellet's patch to fix recentcount/count after IMAP
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index b667c4a6deebba79f79d3db3db6934dc4883d35b..ff727e7e95ac48c4a1284ec98544562df8a649b8 100644 (file)
--- a/imap.c
+++ b/imap.c
 
 #include  "i18n.h"
 
-#if OPIE_ENABLE
+#ifdef OPIE_ENABLE
 #endif /* OPIE_ENABLE */
 
 #ifndef strstr         /* glibc-2.1 declares this as a macro */
-extern char *strstr(); /* needed on sysV68 R3V7.1. */
+extern char *strstr(const char *, const char *);       /* needed on sysV68 R3V7.1. */
 #endif /* strstr */
 
 /* imap_version values */
@@ -32,6 +32,7 @@ extern char *strstr();        /* needed on sysV68 R3V7.1. */
 #define IMAP4rev1      1       /* IMAP4 rev 1, RFC2060 */
 
 static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
+static int recentcount_ok = 0;
 static unsigned int startcount = 1;
 static int expunged, expunge_period, saved_timeout = 0;
 static int imap_version, preauth;
@@ -53,8 +54,8 @@ static int imap_ok(int sock, char *argbuf)
 
        /* 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 */
        if (strstr(buf, "* CAPABILITY"))
@@ -103,8 +104,15 @@ static int imap_ok(int sock, char *argbuf)
        /* a space is required to avoid confusion with the \Recent flag */
        else if (strstr(buf, " RECENT"))
        {
+            recentcount_ok = 1;
            recentcount = atoi(buf+2);
        }
+        else if (strstr(buf, "EXPUNGE") && !strstr(buf, "OK"))
+        {
+            count -= atoi(buf+2);
+            if (count < 0)
+                count = 0;
+        }
        else if (strstr(buf, "PREAUTH"))
            preauth = TRUE;
        /*
@@ -137,9 +145,9 @@ static int imap_ok(int sock, char *argbuf)
        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)
@@ -162,7 +170,7 @@ static int imap_ok(int sock, char *argbuf)
     }
 }
 
-#if NTLM_ENABLE
+#ifdef NTLM_ENABLE
 #include "ntlm.h"
 
 static tSmbNtlmAuthRequest   request;             
@@ -440,19 +448,20 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
            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)
     {
@@ -546,9 +555,16 @@ static int internal_expunge(int sock)
 {
     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;
 
@@ -649,10 +665,6 @@ static int imap_getrange(int sock,
         * 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
         * mailbox changes also */
        while (recentcount == 0 && do_idle) {
@@ -747,7 +759,7 @@ static int imap_getrange(int sock,
                while (*cp && unseen < count)
                {
                    /* skip whitespace */
-                   while (*cp && isspace(*cp))
+                   while (*cp && isspace((unsigned char)*cp))
                        cp++;
                    if (*cp) 
                    {
@@ -894,9 +906,9 @@ static int imap_is_old(int sock, struct query *ctl, int number)
 
 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);
 }
 
@@ -1110,10 +1122,10 @@ static int imap_logout(int sock, struct query *ctl)
     return(gen_transact(sock, "LOGOUT"));
 }
 
-const static struct method imap =
+static const struct method imap =
 {
     "IMAP",            /* Internet Message Access Protocol */
-#if INET6_ENABLE
+#ifdef INET6_ENABLE
     "imap",
     "imaps",
 #else /* INET6_ENABLE */