]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
First round ofmlong-delayed bug fixes.
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index 9d4a506d973cd2b472376b10a068136235a6c239..3e6d80fc8ed4a45d6413b7235a4dde3af80e59d8 100644 (file)
--- a/imap.c
+++ b/imap.c
@@ -57,7 +57,10 @@ static int imap_ok(int sock, char *argbuf)
 
        /* interpret untagged status responses */
        if (strstr(buf, "* CAPABILITY"))
+       {
            strncpy(capabilities, buf + 12, sizeof(capabilities));
+           capabilities[sizeof(capabilities)-1] = '\0';
+       }
        else if (strstr(buf, "EXISTS"))
        {
            count = atoi(buf+2);
@@ -413,9 +416,8 @@ static int imap_getauth(int sock, struct query *ctl, char *greeting)
      * 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)))
        {
@@ -544,11 +546,33 @@ static int internal_expunge(int sock)
 static int imap_idle(int sock)
 /* start an RFC2177 IDLE */
 {
+    int ok;
+
+    /* special timeout to terminate the IDLE and re-issue it
+     * at least every 28 minutes:
+     * (the server may have an inactivity timeout) */
     stage = STAGE_IDLE;
     saved_timeout = mytimeout;
-    mytimeout = 0;
+    mytimeout = 1680; /* 28 min */
 
-    return (gen_transact(sock, "IDLE"));
+    /* 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");
+
+       /* restore normal timeout value */
+       mytimeout = saved_timeout;
+       stage = STAGE_FETCH;
+
+       /* get OK IDLE message */
+       return imap_ok(sock, NULL);
+    } else
+       /* not idle timeout */
+       return ok;
 }
 
 static int imap_getrange(int sock,