]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
Override LC_ALL so that a parsable Date results.
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index 13bc170d5c2e2faba02c0f8a6393af678855c771..9ebbe7ab183a43d87981a27b26961b0a6edd701d 100644 (file)
--- a/imap.c
+++ b/imap.c
 
 #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 */
 
 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 +47,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 +97,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 +138,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 +163,7 @@ static int imap_ok(int sock, char *argbuf)
     }
 }
 
-#if NTLM_ENABLE
+#ifdef NTLM_ENABLE
 #include "ntlm.h"
 
 static tSmbNtlmAuthRequest   request;             
@@ -392,7 +393,7 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
     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, "*");
@@ -440,19 +441,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)
     {
@@ -508,13 +510,7 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
        imap_canonicalize(remotename, ctl->remotename, NAMELEN);
        imap_canonicalize(password, ctl->password, PASSWORDLEN);
 
-#ifdef HAVE_SNPRINTF
        snprintf(shroud, sizeof (shroud), "\"%s\"", password);
-#else
-       strcpy(shroud, "\"");
-       strcat(shroud, password);
-       strcat(shroud, "\"");
-#endif
        ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
        shroud[0] = '\0';
 #ifdef SSL_ENABLE
@@ -546,9 +542,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 +652,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) {
@@ -668,7 +667,9 @@ static int imap_getrange(int sock,
        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
     {
@@ -682,7 +683,9 @@ static int imap_getrange(int sock,
            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) {
@@ -708,7 +711,9 @@ static int imap_getrange(int sock,
                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);
        }
     }
 
@@ -747,7 +752,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 +899,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 +1115,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 */