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 */
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");
286 // see if password needs prompting
287 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
288 int fp = fileno(stdin);
291 tcgetattr(fp, &term);
292 term.c_lflag &= ~ECHO;
293 tcsetattr(fp, TCSANOW, &term);
294 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
295 scanf("%128s", mail->pass);
297 term.c_lflag |= ECHO;
298 tcsetattr(fp, TCSANOW, &term);
300 // now we check for optional args
301 tmp = strstr(arg, "-r ");
304 sscanf(tmp, "%u", &mail->retries);
306 mail->retries = 5; // 5 retries after failure
308 tmp = strstr(arg, "-i ");
311 sscanf(tmp, "%f", &mail->interval);
313 mail->interval = 300; // 5 minutes
315 tmp = strstr(arg, "-p ");
318 sscanf(tmp, "%lu", &mail->port);
320 if (type == POP3_TYPE) {
321 mail->port = 110; // default pop3 port
322 } else if (type == IMAP_TYPE) {
323 mail->port = 143; // default imap port
326 if (type == IMAP_TYPE) {
327 tmp = strstr(arg, "-f ");
330 sscanf(tmp, "%s", mail->folder);
332 strncpy(mail->folder, "INBOX", 128); // default imap inbox
335 tmp = strstr(arg, "-e ");
340 if (tmp[0] == '\'') {
341 len = strstr(tmp + 1, "'") - tmp - 1;
346 strncpy(mail->command, tmp + 1, len);
348 mail->command[0] = '\0';
350 mail->p_timed_thread = NULL;
354 int imap_command(int sockfd, const char *command, char *response, const char *verify)
356 struct timeval timeout;
358 int res, numbytes = 0;
359 if (send(sockfd, command, strlen(command), 0) == -1) {
363 timeout.tv_sec = 60; // 60 second timeout i guess
366 FD_SET(sockfd, &fdset);
367 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
369 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
374 DBGP2("imap_command() command: %s", command);
375 DBGP2("imap_command() received: %s", response);
376 response[numbytes] = '\0';
377 if (strstr(response, verify) == NULL) {
383 int imap_check_status(char *recvbuf, struct mail_s *mail)
386 reply = strstr(recvbuf, " (MESSAGES ");
387 if (!reply || strlen(reply) < 2) {
391 *strchr(reply, ')') = '\0';
393 ERR("Error parsing IMAP response: %s", recvbuf);
396 timed_thread_lock(mail->p_timed_thread);
397 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
399 timed_thread_unlock(mail->p_timed_thread);
404 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
406 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
407 || (mail->messages > old_messages && mail->unseen > 0))) {
409 if (system(mail->command) == -1) {
415 void *imap_thread(void *arg)
417 int sockfd, numbytes;
418 char recvbuf[MAXDATASIZE];
419 char sendbuf[MAXDATASIZE];
420 unsigned int fail = 0;
421 unsigned long old_unseen = ULONG_MAX;
422 unsigned long old_messages = ULONG_MAX;
423 struct stat stat_buf;
424 struct hostent he, *he_res = 0;
427 struct sockaddr_in their_addr; // connector's address information
428 struct mail_s *mail = (struct mail_s *)arg;
430 int threadfd = timed_thread_readfd(mail->p_timed_thread);
432 #ifdef HAVE_GETHOSTBYNAME_R
433 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
434 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
437 #else /* HAVE_GETHOSTBYNAME_R */
438 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
439 herror("gethostbyname");
442 #endif /* HAVE_GETHOSTBYNAME_R */
443 while (fail < mail->retries) {
444 struct timeval timeout;
449 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
450 mail->user, mail->host, fail + 1, mail->retries);
453 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
460 their_addr.sin_family = AF_INET;
461 // short, network byte order
462 their_addr.sin_port = htons(mail->port);
463 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
464 // zero the rest of the struct
465 memset(&(their_addr.sin_zero), '\0', 8);
467 if (connect(sockfd, (struct sockaddr *) &their_addr,
468 sizeof(struct sockaddr)) == -1) {
474 timeout.tv_sec = 60; // 60 second timeout i guess
477 FD_SET(sockfd, &fdset);
478 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
480 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
486 ERR("IMAP connection failed: timeout");
490 recvbuf[numbytes] = '\0';
491 DBGP2("imap_thread() received: %s", recvbuf);
492 if (strstr(recvbuf, "* OK") != recvbuf) {
493 ERR("IMAP connection failed, probably not an IMAP server");
497 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
498 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
499 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
500 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
501 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
502 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
506 if (strstr(recvbuf, " IDLE ") != NULL) {
510 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
511 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
512 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
513 MAXDATASIZE - strlen(sendbuf) - 1);
514 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
519 if (imap_check_status(recvbuf, mail)) {
523 imap_unseen_command(mail, old_unseen, old_messages);
525 old_unseen = mail->unseen;
526 old_messages = mail->messages;
529 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
530 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
531 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
532 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
537 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
538 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
546 * RFC 2177 says we have to re-idle every 29 minutes.
547 * We'll do it every 20 minutes to be safe.
549 timeout.tv_sec = 1200;
553 FD_SET(sockfd, &fdset);
554 FD_SET(threadfd, &fdset);
555 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
556 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
557 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
558 /* if a valid socket, close it */
561 timed_thread_exit(mail->p_timed_thread);
562 } else if (res > 0) {
563 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
564 perror("recv idling");
571 recvbuf[numbytes] = '\0';
572 DBGP2("imap_thread() received: %s", recvbuf);
573 if (strlen(recvbuf) > 2) {
574 unsigned long messages, recent;
576 char force_check = 0;
577 buf = strstr(buf, "EXISTS");
578 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
579 buf = strstr(buf + 1, "EXISTS");
582 // back up until we reach '*'
583 while (buf >= recvbuf && buf[0] != '*') {
586 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
587 timed_thread_lock(mail->p_timed_thread);
588 if (mail->messages != messages) {
590 mail->messages = messages;
592 timed_thread_unlock(mail->p_timed_thread);
596 buf = strstr(buf, "RECENT");
597 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
598 buf = strstr(buf + 1, "RECENT");
601 // back up until we reach '*'
602 while (buf >= recvbuf && buf[0] != '*') {
605 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
610 * check if we got a FETCH from server, recent was
611 * something other than 0, or we had a timeout
614 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
615 // re-check messages and unseen
616 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
620 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
621 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
622 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
623 MAXDATASIZE - strlen(sendbuf) - 1);
624 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
628 if (imap_check_status(recvbuf, mail)) {
632 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
633 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
639 * check if we got a BYE from server
642 if (buf && strstr(buf, "* BYE")) {
643 // need to re-connect
647 imap_unseen_command(mail, old_unseen, old_messages);
649 old_unseen = mail->unseen;
650 old_messages = mail->messages;
653 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
654 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
659 timeout.tv_sec = 60; // 60 second timeout i guess
662 FD_SET(sockfd, &fdset);
663 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
665 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
671 recvbuf[numbytes] = '\0';
672 DBGP2("imap_thread() received: %s", recvbuf);
673 if (strstr(recvbuf, "a3 OK") == NULL) {
674 ERR("IMAP logout failed: %s", recvbuf);
680 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
681 /* if a valid socket, close it */
684 if (timed_thread_test(mail->p_timed_thread, 0)) {
685 timed_thread_exit(mail->p_timed_thread);
693 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
695 struct timeval timeout;
697 int res, numbytes = 0;
698 if (send(sockfd, command, strlen(command), 0) == -1) {
702 timeout.tv_sec = 60; // 60 second timeout i guess
705 FD_SET(sockfd, &fdset);
706 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
708 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
713 DBGP2("pop3_command() received: %s", response);
714 response[numbytes] = '\0';
715 if (strstr(response, verify) == NULL) {
721 void *pop3_thread(void *arg)
723 int sockfd, numbytes;
724 char recvbuf[MAXDATASIZE];
725 char sendbuf[MAXDATASIZE];
727 unsigned int fail = 0;
728 unsigned long old_unseen = ULONG_MAX;
729 struct stat stat_buf;
730 struct hostent he, *he_res = 0;
733 struct sockaddr_in their_addr; // connector's address information
734 struct mail_s *mail = (struct mail_s *)arg;
736 #ifdef HAVE_GETHOSTBYNAME_R
737 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
738 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
741 #else /* HAVE_GETHOSTBYNAME_R */
742 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
743 herror("gethostbyname");
746 #endif /* HAVE_GETHOSTBYNAME_R */
747 while (fail < mail->retries) {
748 struct timeval timeout;
753 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
754 mail->user, mail->host, fail + 1, mail->retries);
757 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
764 their_addr.sin_family = AF_INET;
765 // short, network byte order
766 their_addr.sin_port = htons(mail->port);
767 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
768 // zero the rest of the struct
769 memset(&(their_addr.sin_zero), '\0', 8);
771 if (connect(sockfd, (struct sockaddr *) &their_addr,
772 sizeof(struct sockaddr)) == -1) {
778 timeout.tv_sec = 60; // 60 second timeout i guess
781 FD_SET(sockfd, &fdset);
782 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
784 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
790 ERR("POP3 connection failed: timeout\n");
794 DBGP2("pop3_thread received: %s", recvbuf);
795 recvbuf[numbytes] = '\0';
796 if (strstr(recvbuf, "+OK ") != recvbuf) {
797 ERR("POP3 connection failed, probably not a POP3 server");
801 strncpy(sendbuf, "USER ", MAXDATASIZE);
802 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
803 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
804 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
809 strncpy(sendbuf, "PASS ", MAXDATASIZE);
810 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
811 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
812 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
813 ERR("POP3 server login failed: %s", recvbuf);
818 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
819 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
825 // now we get the data
828 ERR("Error parsing POP3 response: %s", recvbuf);
832 timed_thread_lock(mail->p_timed_thread);
833 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
834 timed_thread_unlock(mail->p_timed_thread);
837 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
838 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
839 ERR("POP3 logout failed: %s", recvbuf);
844 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
846 if (system(mail->command) == -1) {
851 old_unseen = mail->unseen;
853 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
854 /* if a valid socket, close it */
857 if (timed_thread_test(mail->p_timed_thread, 0)) {
858 timed_thread_exit(mail->p_timed_thread);