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-2008 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 */
214 if (fgets(buf, 128, fp) == NULL) {
218 if (strncmp(buf, "From ", 5) == 0) {
219 /* ignore MAILER-DAEMON */
220 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
223 if (reading_status) {
224 mail->new_mail_count++;
231 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
232 /* check that mail isn't already read */
233 if (strchr(buf + 21, '0')) {
234 mail->new_mail_count++;
240 if (reading_status && strncmp(buf, "Status:", 7) == 0) {
241 /* check that mail isn't already read */
242 if (strchr(buf + 7, 'R') == NULL) {
243 mail->new_mail_count++;
252 while (strchr(buf, '\n') == NULL && !feof(fp)) {
259 if (reading_status) {
260 mail->new_mail_count++;
263 mail->last_mtime = st.st_mtime;
267 #define MAXDATASIZE 1000
269 struct mail_s *parse_mail_args(char type, const char *arg)
274 mail = malloc(sizeof(struct mail_s));
275 memset(mail, 0, sizeof(struct mail_s));
277 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
279 if (type == POP3_TYPE) {
280 ERR("Scanning IMAP args failed");
281 } else if (type == IMAP_TYPE) {
282 ERR("Scanning POP3 args failed");
285 // see if password needs prompting
286 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
287 int fp = fileno(stdin);
290 tcgetattr(fp, &term);
291 term.c_lflag &= ~ECHO;
292 tcsetattr(fp, TCSANOW, &term);
293 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
294 scanf("%128s", mail->pass);
296 term.c_lflag |= ECHO;
297 tcsetattr(fp, TCSANOW, &term);
299 // now we check for optional args
300 tmp = strstr(arg, "-r ");
303 sscanf(tmp, "%u", &mail->retries);
305 mail->retries = 5; // 5 retries after failure
307 tmp = strstr(arg, "-i ");
310 sscanf(tmp, "%f", &mail->interval);
312 mail->interval = 300; // 5 minutes
314 tmp = strstr(arg, "-p ");
317 sscanf(tmp, "%lu", &mail->port);
319 if (type == POP3_TYPE) {
320 mail->port = 110; // default pop3 port
321 } else if (type == IMAP_TYPE) {
322 mail->port = 143; // default imap port
325 if (type == IMAP_TYPE) {
326 tmp = strstr(arg, "-f ");
329 sscanf(tmp, "%s", mail->folder);
331 strncpy(mail->folder, "INBOX", 128); // default imap inbox
334 tmp = strstr(arg, "-e ");
339 if (tmp[0] == '\'') {
340 len = strstr(tmp + 1, "'") - tmp - 1;
345 strncpy(mail->command, tmp + 1, len);
347 mail->command[0] = '\0';
349 mail->p_timed_thread = NULL;
353 int imap_command(int sockfd, const char *command, char *response, const char *verify)
355 struct timeval timeout;
357 int res, numbytes = 0;
358 if (send(sockfd, command, strlen(command), 0) == -1) {
362 timeout.tv_sec = 60; // 60 second timeout i guess
365 FD_SET(sockfd, &fdset);
366 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
368 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
373 DBGP2("imap_command() command: %s", command);
374 DBGP2("imap_command() received: %s", response);
375 response[numbytes] = '\0';
376 if (strstr(response, verify) == NULL) {
382 int imap_check_status(char *recvbuf, struct mail_s *mail)
385 reply = strstr(recvbuf, " (MESSAGES ");
386 if (!reply || strlen(reply) < 2) {
390 *strchr(reply, ')') = '\0';
392 ERR("Error parsing IMAP response: %s", recvbuf);
395 timed_thread_lock(mail->p_timed_thread);
396 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
398 timed_thread_unlock(mail->p_timed_thread);
403 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
405 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
406 || (mail->messages > old_messages && mail->unseen > 0))) {
408 if (system(mail->command) == -1) {
414 void *imap_thread(void *arg)
416 int sockfd, numbytes;
417 char recvbuf[MAXDATASIZE];
418 char sendbuf[MAXDATASIZE];
419 unsigned int fail = 0;
420 unsigned long old_unseen = ULONG_MAX;
421 unsigned long old_messages = ULONG_MAX;
422 struct stat stat_buf;
423 struct hostent he, *he_res = 0;
426 struct sockaddr_in their_addr; // connector's address information
427 struct mail_s *mail = (struct mail_s *)arg;
429 int threadfd = timed_thread_readfd(mail->p_timed_thread);
431 #ifdef HAVE_GETHOSTBYNAME_R
432 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
433 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
436 #else /* HAVE_GETHOSTBYNAME_R */
437 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
438 herror("gethostbyname");
441 #endif /* HAVE_GETHOSTBYNAME_R */
442 while (fail < mail->retries) {
443 struct timeval timeout;
448 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
449 mail->user, mail->host, fail + 1, mail->retries);
452 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
459 their_addr.sin_family = AF_INET;
460 // short, network byte order
461 their_addr.sin_port = htons(mail->port);
462 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
463 // zero the rest of the struct
464 memset(&(their_addr.sin_zero), '\0', 8);
466 if (connect(sockfd, (struct sockaddr *) &their_addr,
467 sizeof(struct sockaddr)) == -1) {
473 timeout.tv_sec = 60; // 60 second timeout i guess
476 FD_SET(sockfd, &fdset);
477 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
479 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
485 ERR("IMAP connection failed: timeout");
489 recvbuf[numbytes] = '\0';
490 DBGP2("imap_thread() received: %s", recvbuf);
491 if (strstr(recvbuf, "* OK") != recvbuf) {
492 ERR("IMAP connection failed, probably not an IMAP server");
496 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
497 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
498 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
499 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
500 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
501 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
505 if (strstr(recvbuf, " IDLE ") != NULL) {
509 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
510 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
511 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
512 MAXDATASIZE - strlen(sendbuf) - 1);
513 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
518 if (imap_check_status(recvbuf, mail)) {
522 imap_unseen_command(mail, old_unseen, old_messages);
524 old_unseen = mail->unseen;
525 old_messages = mail->messages;
528 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
529 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
530 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
531 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
536 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
537 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
545 * RFC 2177 says we have to re-idle every 29 minutes.
546 * We'll do it every 20 minutes to be safe.
548 timeout.tv_sec = 1200;
552 FD_SET(sockfd, &fdset);
553 FD_SET(threadfd, &fdset);
554 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
555 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
556 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
557 /* if a valid socket, close it */
560 timed_thread_exit(mail->p_timed_thread);
561 } else if (res > 0) {
562 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
563 perror("recv idling");
570 recvbuf[numbytes] = '\0';
571 DBGP2("imap_thread() received: %s", recvbuf);
572 if (strlen(recvbuf) > 2) {
573 unsigned long messages, recent;
575 char force_check = 0;
576 buf = strstr(buf, "EXISTS");
577 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
578 buf = strstr(buf + 1, "EXISTS");
581 // back up until we reach '*'
582 while (buf >= recvbuf && buf[0] != '*') {
585 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
586 timed_thread_lock(mail->p_timed_thread);
587 if (mail->messages != messages) {
589 mail->messages = messages;
591 timed_thread_unlock(mail->p_timed_thread);
595 buf = strstr(buf, "RECENT");
596 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
597 buf = strstr(buf + 1, "RECENT");
600 // back up until we reach '*'
601 while (buf >= recvbuf && buf[0] != '*') {
604 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
609 * check if we got a FETCH from server, recent was
610 * something other than 0, or we had a timeout
613 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
614 // re-check messages and unseen
615 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
619 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
620 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
621 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
622 MAXDATASIZE - strlen(sendbuf) - 1);
623 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
627 if (imap_check_status(recvbuf, mail)) {
631 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
632 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
638 * check if we got a BYE from server
641 if (buf && strstr(buf, "* BYE")) {
642 // need to re-connect
646 imap_unseen_command(mail, old_unseen, old_messages);
648 old_unseen = mail->unseen;
649 old_messages = mail->messages;
652 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
653 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
658 timeout.tv_sec = 60; // 60 second timeout i guess
661 FD_SET(sockfd, &fdset);
662 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
664 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
670 recvbuf[numbytes] = '\0';
671 DBGP2("imap_thread() received: %s", recvbuf);
672 if (strstr(recvbuf, "a3 OK") == NULL) {
673 ERR("IMAP logout failed: %s", recvbuf);
679 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
680 /* if a valid socket, close it */
683 if (timed_thread_test(mail->p_timed_thread, 0)) {
684 timed_thread_exit(mail->p_timed_thread);
692 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
694 struct timeval timeout;
696 int res, numbytes = 0;
697 if (send(sockfd, command, strlen(command), 0) == -1) {
701 timeout.tv_sec = 60; // 60 second timeout i guess
704 FD_SET(sockfd, &fdset);
705 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
707 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
712 DBGP2("pop3_command() received: %s", response);
713 response[numbytes] = '\0';
714 if (strstr(response, verify) == NULL) {
720 void *pop3_thread(void *arg)
722 int sockfd, numbytes;
723 char recvbuf[MAXDATASIZE];
724 char sendbuf[MAXDATASIZE];
726 unsigned int fail = 0;
727 unsigned long old_unseen = ULONG_MAX;
728 struct stat stat_buf;
729 struct hostent he, *he_res = 0;
732 struct sockaddr_in their_addr; // connector's address information
733 struct mail_s *mail = (struct mail_s *)arg;
735 #ifdef HAVE_GETHOSTBYNAME_R
736 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
737 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
740 #else /* HAVE_GETHOSTBYNAME_R */
741 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
742 herror("gethostbyname");
745 #endif /* HAVE_GETHOSTBYNAME_R */
746 while (fail < mail->retries) {
747 struct timeval timeout;
752 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
753 mail->user, mail->host, fail + 1, mail->retries);
756 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
763 their_addr.sin_family = AF_INET;
764 // short, network byte order
765 their_addr.sin_port = htons(mail->port);
766 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
767 // zero the rest of the struct
768 memset(&(their_addr.sin_zero), '\0', 8);
770 if (connect(sockfd, (struct sockaddr *) &their_addr,
771 sizeof(struct sockaddr)) == -1) {
777 timeout.tv_sec = 60; // 60 second timeout i guess
780 FD_SET(sockfd, &fdset);
781 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
783 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
789 ERR("POP3 connection failed: timeout\n");
793 DBGP2("pop3_thread received: %s", recvbuf);
794 recvbuf[numbytes] = '\0';
795 if (strstr(recvbuf, "+OK ") != recvbuf) {
796 ERR("POP3 connection failed, probably not a POP3 server");
800 strncpy(sendbuf, "USER ", MAXDATASIZE);
801 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
802 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
803 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
808 strncpy(sendbuf, "PASS ", MAXDATASIZE);
809 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
810 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
811 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
812 ERR("POP3 server login failed: %s", recvbuf);
817 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
818 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
824 // now we get the data
827 ERR("Error parsing POP3 response: %s", recvbuf);
831 timed_thread_lock(mail->p_timed_thread);
832 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
833 timed_thread_unlock(mail->p_timed_thread);
836 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
837 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
838 ERR("POP3 logout failed: %s", recvbuf);
843 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
845 if (system(mail->command) == -1) {
850 old_unseen = mail->unseen;
852 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
853 /* if a valid socket, close it */
856 if (timed_thread_test(mail->p_timed_thread, 0)) {
857 timed_thread_exit(mail->p_timed_thread);