Fix IMAP IDLE with Google's IMAP service.
[monky] / src / mail.c
index 7a068a3..ec1fbc9 100644 (file)
@@ -1,4 +1,5 @@
 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
  *
  * Conky, a system monitor, based on torsmo
  *
@@ -25,8 +26,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * vim: ts=4 sw=4 noet ai cindent syntax=c
- *
  */
 
 #include "config.h"
@@ -77,11 +76,11 @@ void update_mail_count(struct local_mail_s *mail)
                mail->last_update = current_update_time;
        }
 
-       if (stat(mail->box, &st)) {
+       if (stat(mail->mbox, &st)) {
                static int rep = 0;
 
                if (!rep) {
-                       ERR("can't stat %s: %s", mail->box, strerror(errno));
+                       NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
                        rep = 1;
                }
                return;
@@ -100,19 +99,19 @@ void update_mail_count(struct local_mail_s *mail)
                mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
                mail->replied_mail_count = mail->unreplied_mail_count = 0;
                mail->draft_mail_count = mail->trashed_mail_count = 0;
-               dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
+               dirname = (char *) malloc(sizeof(char) * (strlen(mail->mbox) + 5));
                if (!dirname) {
-                       ERR("malloc");
+                       NORM_ERR("malloc");
                        return;
                }
-               strcpy(dirname, mail->box);
+               strcpy(dirname, mail->mbox);
                strcat(dirname, "/");
                /* checking the cur subdirectory */
                strcat(dirname, "cur");
 
                dir = opendir(dirname);
                if (!dir) {
-                       ERR("cannot open directory");
+                       NORM_ERR("cannot open directory");
                        free(dirname);
                        return;
                }
@@ -123,7 +122,7 @@ void update_mail_count(struct local_mail_s *mail)
                                mail->mail_count++;
                                mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
                                if (!mailflags) {
-                                       ERR("malloc");
+                                       NORM_ERR("malloc");
                                        free(dirname);
                                        return;
                                }
@@ -166,7 +165,7 @@ void update_mail_count(struct local_mail_s *mail)
 
                dir = opendir(dirname);
                if (!dir) {
-                       ERR("cannot open directory");
+                       NORM_ERR("cannot open directory");
                        free(dirname);
                        return;
                }
@@ -205,7 +204,7 @@ void update_mail_count(struct local_mail_s *mail)
                mail->replied_mail_count = mail->unreplied_mail_count = -1;
                mail->draft_mail_count = mail->trashed_mail_count = -1;
 
-               fp = open_file(mail->box, &rep);
+               fp = open_file(mail->mbox, &rep);
                if (!fp) {
                        return;
                }
@@ -307,9 +306,9 @@ struct mail_s *parse_mail_args(char type, const char *arg)
        if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
                        != 3) {
                if (type == POP3_TYPE) {
-                       ERR("Scanning POP3 args failed");
+                       NORM_ERR("Scanning POP3 args failed");
                } else if (type == IMAP_TYPE) {
-                       ERR("Scanning IMAP args failed");
+                       NORM_ERR("Scanning IMAP args failed");
                }
                return 0;
        }
@@ -383,18 +382,18 @@ struct mail_s *parse_mail_args(char type, const char *arg)
 
 int imap_command(int sockfd, const char *command, char *response, const char *verify)
 {
-       struct timeval timeout;
+       struct timeval fetchtimeout;
        fd_set fdset;
        int res, numbytes = 0;
        if (send(sockfd, command, strlen(command), 0) == -1) {
                perror("send");
                return -1;
        }
-       timeout.tv_sec = 60;    // 60 second timeout i guess
-       timeout.tv_usec = 0;
+       fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
+       fetchtimeout.tv_usec = 0;
        FD_ZERO(&fdset);
        FD_SET(sockfd, &fdset);
-       res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
+       res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
        if (res > 0) {
                if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
                        perror("recv");
@@ -420,7 +419,7 @@ int imap_check_status(char *recvbuf, struct mail_s *mail)
        reply += 2;
        *strchr(reply, ')') = '\0';
        if (reply == NULL) {
-               ERR("Error parsing IMAP response: %s", recvbuf);
+               NORM_ERR("Error parsing IMAP response: %s", recvbuf);
                return -1;
        } else {
                timed_thread_lock(mail->p_timed_thread);
@@ -461,7 +460,7 @@ void *imap_thread(void *arg)
 
 #ifdef HAVE_GETHOSTBYNAME_R
        if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
-               ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
+               NORM_ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
                exit(EXIT_FAILURE);
        }
 #else /* HAVE_GETHOSTBYNAME_R */
@@ -471,12 +470,12 @@ void *imap_thread(void *arg)
        }
 #endif /* HAVE_GETHOSTBYNAME_R */
        while (fail < mail->retries) {
-               struct timeval timeout;
+               struct timeval fetchtimeout;
                int res;
                fd_set fdset;
 
                if (fail > 0) {
-                       ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
+                       NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
                                        mail->user, mail->host, fail + 1, mail->retries);
                }
                do {
@@ -501,11 +500,11 @@ void *imap_thread(void *arg)
                                break;
                        }
 
-                       timeout.tv_sec = 60;    // 60 second timeout i guess
-                       timeout.tv_usec = 0;
+                       fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
+                       fetchtimeout.tv_usec = 0;
                        FD_ZERO(&fdset);
                        FD_SET(sockfd, &fdset);
-                       res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
+                       res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
                        if (res > 0) {
                                if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
                                        perror("recv");
@@ -513,17 +512,26 @@ void *imap_thread(void *arg)
                                        break;
                                }
                        } else {
-                               ERR("IMAP connection failed: timeout");
+                               NORM_ERR("IMAP connection failed: timeout");
                                fail++;
                                break;
                        }
                        recvbuf[numbytes] = '\0';
                        DBGP2("imap_thread() received: %s", recvbuf);
                        if (strstr(recvbuf, "* OK") != recvbuf) {
-                               ERR("IMAP connection failed, probably not an IMAP server");
+                               NORM_ERR("IMAP connection failed, probably not an IMAP server");
                                fail++;
                                break;
                        }
+                       strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
+                       if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
+                               fail++;
+                               break;
+                       }
+                       if (strstr(recvbuf, " IDLE ") != NULL) {
+                               has_idle = 1;
+                       }
+
                        strncpy(sendbuf, "a1 login ", MAXDATASIZE);
                        strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
                        strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
@@ -533,9 +541,6 @@ void *imap_thread(void *arg)
                                fail++;
                                break;
                        }
-                       if (strstr(recvbuf, " IDLE ") != NULL) {
-                               has_idle = 1;
-                       }
 
                        strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
                        strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
@@ -576,8 +581,8 @@ void *imap_thread(void *arg)
                                         * RFC 2177 says we have to re-idle every 29 minutes.
                                         * We'll do it every 20 minutes to be safe.
                                         */
-                                       timeout.tv_sec = 1200;
-                                       timeout.tv_usec = 0;
+                                       fetchtimeout.tv_sec = 1200;
+                                       fetchtimeout.tv_usec = 0;
                                        DBGP2("idling...");
                                        FD_ZERO(&fdset);
                                        FD_SET(sockfd, &fdset);
@@ -642,7 +647,7 @@ void *imap_thread(void *arg)
                                                 * something other than 0, or we had a timeout
                                                 */
                                                buf = recvbuf;
-                                               if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
+                                               if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
                                                        // re-check messages and unseen
                                                        if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
                                                                fail++;
@@ -691,11 +696,11 @@ void *imap_thread(void *arg)
                                        fail++;
                                        break;
                                }
-                               timeout.tv_sec = 60;    // 60 second timeout i guess
-                               timeout.tv_usec = 0;
+                               fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
+                               fetchtimeout.tv_usec = 0;
                                FD_ZERO(&fdset);
                                FD_SET(sockfd, &fdset);
-                               res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
+                               res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
                                if (res > 0) {
                                        if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
                                                perror("recv a3");
@@ -706,7 +711,7 @@ void *imap_thread(void *arg)
                                recvbuf[numbytes] = '\0';
                                DBGP2("imap_thread() received: %s", recvbuf);
                                if (strstr(recvbuf, "a3 OK") == NULL) {
-                                       ERR("IMAP logout failed: %s", recvbuf);
+                                       NORM_ERR("IMAP logout failed: %s", recvbuf);
                                        fail++;
                                        break;
                                }
@@ -727,18 +732,18 @@ void *imap_thread(void *arg)
 
 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
 {
-       struct timeval timeout;
+       struct timeval fetchtimeout;
        fd_set fdset;
        int res, numbytes = 0;
        if (send(sockfd, command, strlen(command), 0) == -1) {
                perror("send");
                return -1;
        }
-       timeout.tv_sec = 60;    // 60 second timeout i guess
-       timeout.tv_usec = 0;
+       fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
+       fetchtimeout.tv_usec = 0;
        FD_ZERO(&fdset);
        FD_SET(sockfd, &fdset);
-       res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
+       res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
        if (res > 0) {
                if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
                        perror("recv");
@@ -770,7 +775,7 @@ void *pop3_thread(void *arg)
 
 #ifdef HAVE_GETHOSTBYNAME_R
        if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
-               ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
+               NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
                exit(EXIT_FAILURE);
        }
 #else /* HAVE_GETHOSTBYNAME_R */
@@ -780,12 +785,12 @@ void *pop3_thread(void *arg)
        }
 #endif /* HAVE_GETHOSTBYNAME_R */
        while (fail < mail->retries) {
-               struct timeval timeout;
+               struct timeval fetchtimeout;
                int res;
                fd_set fdset;
 
                if (fail > 0) {
-                       ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
+                       NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
                                        mail->user, mail->host, fail + 1, mail->retries);
                }
                do {
@@ -810,11 +815,11 @@ void *pop3_thread(void *arg)
                                break;
                        }
 
-                       timeout.tv_sec = 60;    // 60 second timeout i guess
-                       timeout.tv_usec = 0;
+                       fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
+                       fetchtimeout.tv_usec = 0;
                        FD_ZERO(&fdset);
                        FD_SET(sockfd, &fdset);
-                       res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
+                       res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
                        if (res > 0) {
                                if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
                                        perror("recv");
@@ -822,14 +827,14 @@ void *pop3_thread(void *arg)
                                        break;
                                }
                        } else {
-                               ERR("POP3 connection failed: timeout\n");
+                               NORM_ERR("POP3 connection failed: timeout\n");
                                fail++;
                                break;
                        }
                        DBGP2("pop3_thread received: %s", recvbuf);
                        recvbuf[numbytes] = '\0';
                        if (strstr(recvbuf, "+OK ") != recvbuf) {
-                               ERR("POP3 connection failed, probably not a POP3 server");
+                               NORM_ERR("POP3 connection failed, probably not a POP3 server");
                                fail++;
                                break;
                        }
@@ -845,7 +850,7 @@ void *pop3_thread(void *arg)
                        strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
                        strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
                        if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
-                               ERR("POP3 server login failed: %s", recvbuf);
+                               NORM_ERR("POP3 server login failed: %s", recvbuf);
                                fail++;
                                break;
                        }
@@ -860,7 +865,7 @@ void *pop3_thread(void *arg)
                        // now we get the data
                        reply = recvbuf + 4;
                        if (reply == NULL) {
-                               ERR("Error parsing POP3 response: %s", recvbuf);
+                               NORM_ERR("Error parsing POP3 response: %s", recvbuf);
                                fail++;
                                break;
                        } else {
@@ -871,7 +876,7 @@ void *pop3_thread(void *arg)
                        
                        strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
                        if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
-                               ERR("POP3 logout failed: %s", recvbuf);
+                               NORM_ERR("POP3 logout failed: %s", recvbuf);
                                fail++;
                                break;
                        }