1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 #include <netinet/in.h>
41 #include <sys/socket.h>
44 #include <sys/param.h>
50 /* MAX() is defined by a header included from conky.h
51 * maybe once this is not true anymore, so have an alternative
54 * #define MAX(a, b) ((a > b) ? a : b)
57 char *current_mail_spool;
59 void update_mail_count(struct local_mail_s *mail)
67 /* TODO: use that fine file modification notify on Linux 2.4 */
69 /* don't check mail so often (9.5s is minimum interval) */
70 if (current_update_time - mail->last_update < 9.5) {
73 mail->last_update = current_update_time;
76 if (stat(mail->box, &st)) {
80 ERR("can't stat %s: %s", mail->box, strerror(errno));
87 if (S_ISDIR(st.st_mode)) {
90 struct dirent *dirent;
93 mail->mail_count = mail->new_mail_count = 0;
94 mail->seen_mail_count = mail->unseen_mail_count = 0;
95 mail->flagged_mail_count = mail->unflagged_mail_count = 0;
96 mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
97 mail->replied_mail_count = mail->unreplied_mail_count = 0;
98 mail->draft_mail_count = mail->trashed_mail_count = 0;
99 dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
104 strcpy(dirname, mail->box);
105 strcat(dirname, "/");
106 /* checking the cur subdirectory */
107 strcat(dirname, "cur");
109 dir = opendir(dirname);
111 ERR("cannot open directory");
115 dirent = readdir(dir);
117 /* . and .. are skipped */
118 if (dirent->d_name[0] != '.') {
120 mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
126 strcpy(mailflags, strrchr(dirent->d_name, ','));
127 if (!strchr(mailflags, 'T')) { /* The message is not in the trash */
128 if (strchr(mailflags, 'S')) { /*The message has been seen */
129 mail->seen_mail_count++;
131 mail->unseen_mail_count++;
133 if (strchr(mailflags, 'F')) { /*The message was flagged */
134 mail->flagged_mail_count++;
136 mail->unflagged_mail_count++;
138 if (strchr(mailflags, 'P')) { /*The message was forwarded */
139 mail->forwarded_mail_count++;
141 mail->unforwarded_mail_count++;
143 if (strchr(mailflags, 'R')) { /*The message was replied */
144 mail->replied_mail_count++;
146 mail->unreplied_mail_count++;
148 if (strchr(mailflags, 'D')) { /*The message is a draft */
149 mail->draft_mail_count++;
152 mail->trashed_mail_count++;
156 dirent = readdir(dir);
160 dirname[strlen(dirname) - 3] = '\0';
161 strcat(dirname, "new");
163 dir = opendir(dirname);
165 ERR("cannot open directory");
169 dirent = readdir(dir);
171 /* . and .. are skipped */
172 if (dirent->d_name[0] != '.') {
173 mail->new_mail_count++;
175 mail->unseen_mail_count++; /* new messages cannot have been seen */
177 dirent = readdir(dir);
186 if (st.st_mtime != mail->last_mtime) {
187 /* yippee, modification time has changed, let's read mail count! */
190 int reading_status = 0;
192 /* could lock here but I don't think it's really worth it because
193 * this isn't going to write mail spool */
195 mail->new_mail_count = mail->mail_count = 0;
197 /* these flags are not supported for mbox */
198 mail->seen_mail_count = mail->unseen_mail_count = -1;
199 mail->flagged_mail_count = mail->unflagged_mail_count = -1;
200 mail->forwarded_mail_count = mail->unforwarded_mail_count = -1;
201 mail->replied_mail_count = mail->unreplied_mail_count = -1;
202 mail->draft_mail_count = mail->trashed_mail_count = -1;
204 fp = open_file(mail->box, &rep);
209 /* NOTE: adds mail as new if there isn't Status-field at all */
215 if (fgets(buf, 128, fp) == NULL) {
219 if (strncmp(buf, "From ", 5) == 0) {
220 /* ignore MAILER-DAEMON */
221 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
225 if (reading_status == 1) {
226 mail->new_mail_count++;
232 if (reading_status == 1
233 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
234 int xms = strtol(buf + 17, NULL, 16);
235 /* check that mail isn't marked for deletion */
237 mail->trashed_mail_count++;
239 /* Don't check whether the trashed email is unread */
242 /* check that mail isn't already read */
243 if (!(xms & 0x0001)) {
244 mail->new_mail_count++;
248 /* check for an additional X-Status header */
252 if (reading_status == 1 && strncmp(buf, "Status:", 7) == 0) {
253 /* check that mail isn't already read */
254 if (strchr(buf + 7, 'R') == NULL) {
255 mail->new_mail_count++;
262 if (reading_status >= 1 && strncmp(buf, "X-Status:", 9) == 0) {
263 /* check that mail isn't marked for deletion */
264 if (strchr(buf + 9, 'D') != NULL) {
265 mail->trashed_mail_count++;
266 /* If the mail was previously detected as new,
267 subtract it from the new mail count */
269 mail->new_mail_count--;
278 while (strchr(buf, '\n') == NULL && !feof(fp)) {
285 if (reading_status) {
286 mail->new_mail_count++;
289 mail->last_mtime = st.st_mtime;
293 #define MAXDATASIZE 1000
295 struct mail_s *parse_mail_args(char type, const char *arg)
300 mail = malloc(sizeof(struct mail_s));
301 memset(mail, 0, sizeof(struct mail_s));
303 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
305 if (type == POP3_TYPE) {
306 ERR("Scanning POP3 args failed");
307 } else if (type == IMAP_TYPE) {
308 ERR("Scanning IMAP args failed");
312 // see if password needs prompting
313 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
314 int fp = fileno(stdin);
317 tcgetattr(fp, &term);
318 term.c_lflag &= ~ECHO;
319 tcsetattr(fp, TCSANOW, &term);
320 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
321 scanf("%128s", mail->pass);
323 term.c_lflag |= ECHO;
324 tcsetattr(fp, TCSANOW, &term);
326 // now we check for optional args
327 tmp = strstr(arg, "-r ");
330 sscanf(tmp, "%u", &mail->retries);
332 mail->retries = 5; // 5 retries after failure
334 tmp = strstr(arg, "-i ");
337 sscanf(tmp, "%f", &mail->interval);
339 mail->interval = 300; // 5 minutes
341 tmp = strstr(arg, "-p ");
344 sscanf(tmp, "%lu", &mail->port);
346 if (type == POP3_TYPE) {
347 mail->port = 110; // default pop3 port
348 } else if (type == IMAP_TYPE) {
349 mail->port = 143; // default imap port
352 if (type == IMAP_TYPE) {
353 tmp = strstr(arg, "-f ");
356 sscanf(tmp, "%s", mail->folder);
358 strncpy(mail->folder, "INBOX", 128); // default imap inbox
361 tmp = strstr(arg, "-e ");
366 if (tmp[0] == '\'') {
367 len = strstr(tmp + 1, "'") - tmp - 1;
372 strncpy(mail->command, tmp + 1, len);
374 mail->command[0] = '\0';
376 mail->p_timed_thread = NULL;
380 int imap_command(int sockfd, const char *command, char *response, const char *verify)
382 struct timeval timeout;
384 int res, numbytes = 0;
385 if (send(sockfd, command, strlen(command), 0) == -1) {
389 timeout.tv_sec = 60; // 60 second timeout i guess
392 FD_SET(sockfd, &fdset);
393 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
395 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
400 DBGP2("imap_command() command: %s", command);
401 DBGP2("imap_command() received: %s", response);
402 response[numbytes] = '\0';
403 if (strstr(response, verify) == NULL) {
409 int imap_check_status(char *recvbuf, struct mail_s *mail)
412 reply = strstr(recvbuf, " (MESSAGES ");
413 if (!reply || strlen(reply) < 2) {
417 *strchr(reply, ')') = '\0';
419 ERR("Error parsing IMAP response: %s", recvbuf);
422 timed_thread_lock(mail->p_timed_thread);
423 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
425 timed_thread_unlock(mail->p_timed_thread);
430 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
432 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
433 || (mail->messages > old_messages && mail->unseen > 0))) {
435 if (system(mail->command) == -1) {
441 void *imap_thread(void *arg)
443 int sockfd, numbytes;
444 char recvbuf[MAXDATASIZE];
445 char sendbuf[MAXDATASIZE];
446 unsigned int fail = 0;
447 unsigned long old_unseen = ULONG_MAX;
448 unsigned long old_messages = ULONG_MAX;
449 struct stat stat_buf;
450 struct hostent he, *he_res = 0;
453 struct sockaddr_in their_addr; // connector's address information
454 struct mail_s *mail = (struct mail_s *)arg;
456 int threadfd = timed_thread_readfd(mail->p_timed_thread);
458 #ifdef HAVE_GETHOSTBYNAME_R
459 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
460 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
463 #else /* HAVE_GETHOSTBYNAME_R */
464 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
465 herror("gethostbyname");
468 #endif /* HAVE_GETHOSTBYNAME_R */
469 while (fail < mail->retries) {
470 struct timeval timeout;
475 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
476 mail->user, mail->host, fail + 1, mail->retries);
479 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
486 their_addr.sin_family = AF_INET;
487 // short, network byte order
488 their_addr.sin_port = htons(mail->port);
489 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
490 // zero the rest of the struct
491 memset(&(their_addr.sin_zero), '\0', 8);
493 if (connect(sockfd, (struct sockaddr *) &their_addr,
494 sizeof(struct sockaddr)) == -1) {
500 timeout.tv_sec = 60; // 60 second timeout i guess
503 FD_SET(sockfd, &fdset);
504 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
506 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
512 ERR("IMAP connection failed: timeout");
516 recvbuf[numbytes] = '\0';
517 DBGP2("imap_thread() received: %s", recvbuf);
518 if (strstr(recvbuf, "* OK") != recvbuf) {
519 ERR("IMAP connection failed, probably not an IMAP server");
523 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
524 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
525 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
526 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
527 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
528 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
532 if (strstr(recvbuf, " IDLE ") != NULL) {
536 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
537 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
538 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
539 MAXDATASIZE - strlen(sendbuf) - 1);
540 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
545 if (imap_check_status(recvbuf, mail)) {
549 imap_unseen_command(mail, old_unseen, old_messages);
551 old_unseen = mail->unseen;
552 old_messages = mail->messages;
555 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
556 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
557 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
558 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
563 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
564 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
572 * RFC 2177 says we have to re-idle every 29 minutes.
573 * We'll do it every 20 minutes to be safe.
575 timeout.tv_sec = 1200;
579 FD_SET(sockfd, &fdset);
580 FD_SET(threadfd, &fdset);
581 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
582 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
583 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
584 /* if a valid socket, close it */
587 timed_thread_exit(mail->p_timed_thread);
588 } else if (res > 0) {
589 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
590 perror("recv idling");
598 recvbuf[numbytes] = '\0';
599 DBGP2("imap_thread() received: %s", recvbuf);
600 if (strlen(recvbuf) > 2) {
601 unsigned long messages, recent;
603 char force_check = 0;
604 buf = strstr(buf, "EXISTS");
605 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
606 buf = strstr(buf + 1, "EXISTS");
609 // back up until we reach '*'
610 while (buf >= recvbuf && buf[0] != '*') {
613 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
614 timed_thread_lock(mail->p_timed_thread);
615 if (mail->messages != messages) {
617 mail->messages = messages;
619 timed_thread_unlock(mail->p_timed_thread);
623 buf = strstr(buf, "RECENT");
624 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
625 buf = strstr(buf + 1, "RECENT");
628 // back up until we reach '*'
629 while (buf >= recvbuf && buf[0] != '*') {
632 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
637 * check if we got a FETCH from server, recent was
638 * something other than 0, or we had a timeout
641 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
642 // re-check messages and unseen
643 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
647 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
648 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
649 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
650 MAXDATASIZE - strlen(sendbuf) - 1);
651 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
655 if (imap_check_status(recvbuf, mail)) {
659 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
660 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
666 * check if we got a BYE from server
669 if (buf && strstr(buf, "* BYE")) {
670 // need to re-connect
677 imap_unseen_command(mail, old_unseen, old_messages);
679 old_unseen = mail->unseen;
680 old_messages = mail->messages;
684 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
685 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
690 timeout.tv_sec = 60; // 60 second timeout i guess
693 FD_SET(sockfd, &fdset);
694 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
696 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
702 recvbuf[numbytes] = '\0';
703 DBGP2("imap_thread() received: %s", recvbuf);
704 if (strstr(recvbuf, "a3 OK") == NULL) {
705 ERR("IMAP logout failed: %s", recvbuf);
711 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
712 /* if a valid socket, close it */
715 if (timed_thread_test(mail->p_timed_thread, 0)) {
716 timed_thread_exit(mail->p_timed_thread);
724 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
726 struct timeval timeout;
728 int res, numbytes = 0;
729 if (send(sockfd, command, strlen(command), 0) == -1) {
733 timeout.tv_sec = 60; // 60 second timeout i guess
736 FD_SET(sockfd, &fdset);
737 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
739 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
744 DBGP2("pop3_command() received: %s", response);
745 response[numbytes] = '\0';
746 if (strstr(response, verify) == NULL) {
752 void *pop3_thread(void *arg)
754 int sockfd, numbytes;
755 char recvbuf[MAXDATASIZE];
756 char sendbuf[MAXDATASIZE];
758 unsigned int fail = 0;
759 unsigned long old_unseen = ULONG_MAX;
760 struct stat stat_buf;
761 struct hostent he, *he_res = 0;
764 struct sockaddr_in their_addr; // connector's address information
765 struct mail_s *mail = (struct mail_s *)arg;
767 #ifdef HAVE_GETHOSTBYNAME_R
768 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
769 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
772 #else /* HAVE_GETHOSTBYNAME_R */
773 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
774 herror("gethostbyname");
777 #endif /* HAVE_GETHOSTBYNAME_R */
778 while (fail < mail->retries) {
779 struct timeval timeout;
784 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
785 mail->user, mail->host, fail + 1, mail->retries);
788 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
795 their_addr.sin_family = AF_INET;
796 // short, network byte order
797 their_addr.sin_port = htons(mail->port);
798 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
799 // zero the rest of the struct
800 memset(&(their_addr.sin_zero), '\0', 8);
802 if (connect(sockfd, (struct sockaddr *) &their_addr,
803 sizeof(struct sockaddr)) == -1) {
809 timeout.tv_sec = 60; // 60 second timeout i guess
812 FD_SET(sockfd, &fdset);
813 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
815 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
821 ERR("POP3 connection failed: timeout\n");
825 DBGP2("pop3_thread received: %s", recvbuf);
826 recvbuf[numbytes] = '\0';
827 if (strstr(recvbuf, "+OK ") != recvbuf) {
828 ERR("POP3 connection failed, probably not a POP3 server");
832 strncpy(sendbuf, "USER ", MAXDATASIZE);
833 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
834 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
835 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
840 strncpy(sendbuf, "PASS ", MAXDATASIZE);
841 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
842 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
843 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
844 ERR("POP3 server login failed: %s", recvbuf);
849 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
850 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
856 // now we get the data
859 ERR("Error parsing POP3 response: %s", recvbuf);
863 timed_thread_lock(mail->p_timed_thread);
864 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
865 timed_thread_unlock(mail->p_timed_thread);
868 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
869 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
870 ERR("POP3 logout failed: %s", recvbuf);
875 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
877 if (system(mail->command) == -1) {
882 old_unseen = mail->unseen;
884 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
885 /* if a valid socket, close it */
888 if (timed_thread_test(mail->p_timed_thread, 0)) {
889 timed_thread_exit(mail->p_timed_thread);