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/>.
40 #include <sys/socket.h>
43 #include <sys/param.h>
49 /* MAX() is defined by a header included from conky.h
50 * maybe once this is not true anymore, so have an alternative
53 * #define MAX(a, b) ((a > b) ? a : b)
56 char *current_mail_spool;
58 void update_mail_count(struct local_mail_s *mail)
66 /* TODO: use that fine file modification notify on Linux 2.4 */
68 /* don't check mail so often (9.5s is minimum interval) */
69 if (current_update_time - mail->last_update < 9.5) {
72 mail->last_update = current_update_time;
75 if (stat(mail->box, &st)) {
79 ERR("can't stat %s: %s", mail->box, strerror(errno));
86 if (S_ISDIR(st.st_mode)) {
89 struct dirent *dirent;
92 mail->mail_count = mail->new_mail_count = 0;
93 mail->seen_mail_count = mail->unseen_mail_count = 0;
94 mail->flagged_mail_count = mail->unflagged_mail_count = 0;
95 mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
96 mail->replied_mail_count = mail->unreplied_mail_count = 0;
97 mail->draft_mail_count = mail->trashed_mail_count = 0;
98 dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
103 strcpy(dirname, mail->box);
104 strcat(dirname, "/");
105 /* checking the cur subdirectory */
106 strcat(dirname, "cur");
108 dir = opendir(dirname);
110 ERR("cannot open directory");
114 dirent = readdir(dir);
116 /* . and .. are skipped */
117 if (dirent->d_name[0] != '.') {
119 mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
125 strcpy(mailflags, strrchr(dirent->d_name, ','));
126 if (!strchr(mailflags, 'T')) { /* The message is not in the trash */
127 if (strchr(mailflags, 'S')) { /*The message has been seen */
128 mail->seen_mail_count++;
130 mail->unseen_mail_count++;
132 if (strchr(mailflags, 'F')) { /*The message was flagged */
133 mail->flagged_mail_count++;
135 mail->unflagged_mail_count++;
137 if (strchr(mailflags, 'P')) { /*The message was forwarded */
138 mail->forwarded_mail_count++;
140 mail->unforwarded_mail_count++;
142 if (strchr(mailflags, 'R')) { /*The message was replied */
143 mail->replied_mail_count++;
145 mail->unreplied_mail_count++;
147 if (strchr(mailflags, 'D')) { /*The message is a draft */
148 mail->draft_mail_count++;
151 mail->trashed_mail_count++;
155 dirent = readdir(dir);
159 dirname[strlen(dirname) - 3] = '\0';
160 strcat(dirname, "new");
162 dir = opendir(dirname);
164 ERR("cannot open directory");
168 dirent = readdir(dir);
170 /* . and .. are skipped */
171 if (dirent->d_name[0] != '.') {
172 mail->new_mail_count++;
174 mail->unseen_mail_count++; /* new messages cannot have been seen */
176 dirent = readdir(dir);
185 if (st.st_mtime != mail->last_mtime) {
186 /* yippee, modification time has changed, let's read mail count! */
189 int reading_status = 0;
191 /* could lock here but I don't think it's really worth it because
192 * this isn't going to write mail spool */
194 mail->new_mail_count = mail->mail_count = 0;
196 /* these flags are not supported for mbox */
197 mail->seen_mail_count = mail->unseen_mail_count = -1;
198 mail->flagged_mail_count = mail->unflagged_mail_count = -1;
199 mail->forwarded_mail_count = mail->unforwarded_mail_count = -1;
200 mail->replied_mail_count = mail->unreplied_mail_count = -1;
201 mail->draft_mail_count = mail->trashed_mail_count = -1;
203 fp = open_file(mail->box, &rep);
208 /* NOTE: adds mail as new if there isn't Status-field at all */
213 if (fgets(buf, 128, fp) == NULL) {
217 if (strncmp(buf, "From ", 5) == 0) {
218 /* ignore MAILER-DAEMON */
219 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
222 if (reading_status) {
223 mail->new_mail_count++;
230 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
231 /* check that mail isn't already read */
232 if (strchr(buf + 21, '0')) {
233 mail->new_mail_count++;
239 if (reading_status && strncmp(buf, "Status:", 7) == 0) {
240 /* check that mail isn't already read */
241 if (strchr(buf + 7, 'R') == NULL) {
242 mail->new_mail_count++;
251 while (strchr(buf, '\n') == NULL && !feof(fp)) {
258 if (reading_status) {
259 mail->new_mail_count++;
262 mail->last_mtime = st.st_mtime;
266 #define MAXDATASIZE 1000
268 struct mail_s *parse_mail_args(char type, const char *arg)
273 mail = malloc(sizeof(struct mail_s));
274 memset(mail, 0, sizeof(struct mail_s));
276 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
278 if (type == POP3_TYPE) {
279 ERR("Scanning IMAP args failed");
280 } else if (type == IMAP_TYPE) {
281 ERR("Scanning POP3 args failed");
284 // see if password needs prompting
285 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
286 int fp = fileno(stdin);
289 tcgetattr(fp, &term);
290 term.c_lflag &= ~ECHO;
291 tcsetattr(fp, TCSANOW, &term);
292 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
293 scanf("%128s", mail->pass);
295 term.c_lflag |= ECHO;
296 tcsetattr(fp, TCSANOW, &term);
298 // now we check for optional args
299 tmp = strstr(arg, "-r ");
302 sscanf(tmp, "%u", &mail->retries);
304 mail->retries = 5; // 5 retries after failure
306 tmp = strstr(arg, "-i ");
309 sscanf(tmp, "%f", &mail->interval);
311 mail->interval = 300; // 5 minutes
313 tmp = strstr(arg, "-p ");
316 sscanf(tmp, "%lu", &mail->port);
318 if (type == POP3_TYPE) {
319 mail->port = 110; // default pop3 port
320 } else if (type == IMAP_TYPE) {
321 mail->port = 143; // default imap port
324 if (type == IMAP_TYPE) {
325 tmp = strstr(arg, "-f ");
328 sscanf(tmp, "%s", mail->folder);
330 strncpy(mail->folder, "INBOX", 128); // default imap inbox
333 tmp = strstr(arg, "-e ");
338 if (tmp[0] == '\'') {
339 len = strstr(tmp + 1, "'") - tmp - 1;
344 strncpy(mail->command, tmp + 1, len);
346 mail->command[0] = '\0';
348 mail->p_timed_thread = NULL;
352 int imap_command(int sockfd, const char *command, char *response, const char *verify)
354 struct timeval timeout;
356 int res, numbytes = 0;
357 if (send(sockfd, command, strlen(command), 0) == -1) {
361 timeout.tv_sec = 60; // 60 second timeout i guess
364 FD_SET(sockfd, &fdset);
365 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
367 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
372 DBGP2("imap_command() command: %s", command);
373 DBGP2("imap_command() received: %s", response);
374 response[numbytes] = '\0';
375 if (strstr(response, verify) == NULL) {
381 int imap_check_status(char *recvbuf, struct mail_s *mail)
384 reply = strstr(recvbuf, " (MESSAGES ");
385 if (!reply || strlen(reply) < 2) {
389 *strchr(reply, ')') = '\0';
391 ERR("Error parsing IMAP response: %s", recvbuf);
394 timed_thread_lock(mail->p_timed_thread);
395 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
397 timed_thread_unlock(mail->p_timed_thread);
402 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
404 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
405 || (mail->messages > old_messages && mail->unseen > 0))) {
407 if (system(mail->command) == -1) {
413 void *imap_thread(void *arg)
415 int sockfd, numbytes;
416 char recvbuf[MAXDATASIZE];
417 char sendbuf[MAXDATASIZE];
418 unsigned int fail = 0;
419 unsigned long old_unseen = ULONG_MAX;
420 unsigned long old_messages = ULONG_MAX;
421 struct stat stat_buf;
422 struct hostent he, *he_res = 0;
425 struct sockaddr_in their_addr; // connector's address information
426 struct mail_s *mail = (struct mail_s *)arg;
428 int threadfd = timed_thread_readfd(mail->p_timed_thread);
430 #ifdef HAVE_GETHOSTBYNAME_R
431 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
432 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
435 #else /* HAVE_GETHOSTBYNAME_R */
436 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
437 herror("gethostbyname");
440 #endif /* HAVE_GETHOSTBYNAME_R */
441 while (fail < mail->retries) {
442 struct timeval timeout;
447 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
448 mail->user, mail->host, fail + 1, mail->retries);
451 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
458 their_addr.sin_family = AF_INET;
459 // short, network byte order
460 their_addr.sin_port = htons(mail->port);
461 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
462 // zero the rest of the struct
463 memset(&(their_addr.sin_zero), '\0', 8);
465 if (connect(sockfd, (struct sockaddr *) &their_addr,
466 sizeof(struct sockaddr)) == -1) {
472 timeout.tv_sec = 60; // 60 second timeout i guess
475 FD_SET(sockfd, &fdset);
476 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
478 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
484 ERR("IMAP connection failed: timeout");
488 recvbuf[numbytes] = '\0';
489 DBGP2("imap_thread() received: %s", recvbuf);
490 if (strstr(recvbuf, "* OK") != recvbuf) {
491 ERR("IMAP connection failed, probably not an IMAP server");
495 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
496 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
497 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
498 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
499 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
500 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
504 if (strstr(recvbuf, " IDLE ") != NULL) {
508 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
509 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
510 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
511 MAXDATASIZE - strlen(sendbuf) - 1);
512 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
517 if (imap_check_status(recvbuf, mail)) {
521 imap_unseen_command(mail, old_unseen, old_messages);
523 old_unseen = mail->unseen;
524 old_messages = mail->messages;
527 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
528 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
529 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
530 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
535 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
536 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
544 * RFC 2177 says we have to re-idle every 29 minutes.
545 * We'll do it every 20 minutes to be safe.
547 timeout.tv_sec = 1200;
551 FD_SET(sockfd, &fdset);
552 FD_SET(threadfd, &fdset);
553 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
554 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
555 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
556 /* if a valid socket, close it */
559 timed_thread_exit(mail->p_timed_thread);
560 } else if (res > 0) {
561 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
562 perror("recv idling");
569 recvbuf[numbytes] = '\0';
570 DBGP2("imap_thread() received: %s", recvbuf);
571 if (strlen(recvbuf) > 2) {
572 unsigned long messages, recent;
574 char force_check = 0;
575 buf = strstr(buf, "EXISTS");
576 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
577 buf = strstr(buf + 1, "EXISTS");
580 // back up until we reach '*'
581 while (buf >= recvbuf && buf[0] != '*') {
584 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
585 timed_thread_lock(mail->p_timed_thread);
586 if (mail->messages != messages) {
588 mail->messages = messages;
590 timed_thread_unlock(mail->p_timed_thread);
594 buf = strstr(buf, "RECENT");
595 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
596 buf = strstr(buf + 1, "RECENT");
599 // back up until we reach '*'
600 while (buf >= recvbuf && buf[0] != '*') {
603 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
608 * check if we got a FETCH from server, recent was
609 * something other than 0, or we had a timeout
612 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
613 // re-check messages and unseen
614 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
618 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
619 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
620 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
621 MAXDATASIZE - strlen(sendbuf) - 1);
622 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
626 if (imap_check_status(recvbuf, mail)) {
630 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
631 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
637 * check if we got a BYE from server
640 if (buf && strstr(buf, "* BYE")) {
641 // need to re-connect
645 imap_unseen_command(mail, old_unseen, old_messages);
647 old_unseen = mail->unseen;
648 old_messages = mail->messages;
651 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
652 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
657 timeout.tv_sec = 60; // 60 second timeout i guess
660 FD_SET(sockfd, &fdset);
661 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
663 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
669 recvbuf[numbytes] = '\0';
670 DBGP2("imap_thread() received: %s", recvbuf);
671 if (strstr(recvbuf, "a3 OK") == NULL) {
672 ERR("IMAP logout failed: %s", recvbuf);
678 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
679 /* if a valid socket, close it */
682 if (timed_thread_test(mail->p_timed_thread, 0)) {
683 timed_thread_exit(mail->p_timed_thread);
691 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
693 struct timeval timeout;
695 int res, numbytes = 0;
696 if (send(sockfd, command, strlen(command), 0) == -1) {
700 timeout.tv_sec = 60; // 60 second timeout i guess
703 FD_SET(sockfd, &fdset);
704 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
706 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
711 DBGP2("pop3_command() received: %s", response);
712 response[numbytes] = '\0';
713 if (strstr(response, verify) == NULL) {
719 void *pop3_thread(void *arg)
721 int sockfd, numbytes;
722 char recvbuf[MAXDATASIZE];
723 char sendbuf[MAXDATASIZE];
725 unsigned int fail = 0;
726 unsigned long old_unseen = ULONG_MAX;
727 struct stat stat_buf;
728 struct hostent he, *he_res = 0;
731 struct sockaddr_in their_addr; // connector's address information
732 struct mail_s *mail = (struct mail_s *)arg;
734 #ifdef HAVE_GETHOSTBYNAME_R
735 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
736 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
739 #else /* HAVE_GETHOSTBYNAME_R */
740 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
741 herror("gethostbyname");
744 #endif /* HAVE_GETHOSTBYNAME_R */
745 while (fail < mail->retries) {
746 struct timeval timeout;
751 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
752 mail->user, mail->host, fail + 1, mail->retries);
755 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
762 their_addr.sin_family = AF_INET;
763 // short, network byte order
764 their_addr.sin_port = htons(mail->port);
765 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
766 // zero the rest of the struct
767 memset(&(their_addr.sin_zero), '\0', 8);
769 if (connect(sockfd, (struct sockaddr *) &their_addr,
770 sizeof(struct sockaddr)) == -1) {
776 timeout.tv_sec = 60; // 60 second timeout i guess
779 FD_SET(sockfd, &fdset);
780 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
782 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
788 ERR("POP3 connection failed: timeout\n");
792 DBGP2("pop3_thread received: %s", recvbuf);
793 recvbuf[numbytes] = '\0';
794 if (strstr(recvbuf, "+OK ") != recvbuf) {
795 ERR("POP3 connection failed, probably not a POP3 server");
799 strncpy(sendbuf, "USER ", MAXDATASIZE);
800 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
801 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
802 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
807 strncpy(sendbuf, "PASS ", MAXDATASIZE);
808 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
809 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
810 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
811 ERR("POP3 server login failed: %s", recvbuf);
816 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
817 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
823 // now we get the data
826 ERR("Error parsing POP3 response: %s", recvbuf);
830 timed_thread_lock(mail->p_timed_thread);
831 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
832 timed_thread_unlock(mail->p_timed_thread);
835 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
836 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
837 ERR("POP3 logout failed: %s", recvbuf);
842 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
844 if (system(mail->command) == -1) {
849 old_unseen = mail->unseen;
851 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
852 /* if a valid socket, close it */
855 if (timed_thread_test(mail->p_timed_thread, 0)) {
856 timed_thread_exit(mail->p_timed_thread);