1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 #include <netinet/in.h>
44 #include <sys/socket.h>
47 #include <sys/param.h>
53 /* MAX() is defined by a header included from conky.h
54 * maybe once this is not true anymore, so have an alternative
57 * #define MAX(a, b) ((a > b) ? a : b)
60 char *current_mail_spool;
62 void update_mail_count(struct local_mail_s *mail)
70 /* TODO: use that fine file modification notify on Linux 2.4 */
72 /* don't check mail so often (9.5s is minimum interval) */
73 if (current_update_time - mail->last_update < 9.5) {
76 mail->last_update = current_update_time;
79 if (stat(mail->mbox, &st)) {
83 NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
90 if (S_ISDIR(st.st_mode)) {
93 struct dirent *dirent;
96 mail->mail_count = mail->new_mail_count = 0;
97 mail->seen_mail_count = mail->unseen_mail_count = 0;
98 mail->flagged_mail_count = mail->unflagged_mail_count = 0;
99 mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
100 mail->replied_mail_count = mail->unreplied_mail_count = 0;
101 mail->draft_mail_count = mail->trashed_mail_count = 0;
102 dirname = (char *) malloc(sizeof(char) * (strlen(mail->mbox) + 5));
107 strcpy(dirname, mail->mbox);
108 strcat(dirname, "/");
109 /* checking the cur subdirectory */
110 strcat(dirname, "cur");
112 dir = opendir(dirname);
114 NORM_ERR("cannot open directory");
118 dirent = readdir(dir);
120 /* . and .. are skipped */
121 if (dirent->d_name[0] != '.') {
123 mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
129 strcpy(mailflags, strrchr(dirent->d_name, ','));
130 if (!strchr(mailflags, 'T')) { /* The message is not in the trash */
131 if (strchr(mailflags, 'S')) { /*The message has been seen */
132 mail->seen_mail_count++;
134 mail->unseen_mail_count++;
136 if (strchr(mailflags, 'F')) { /*The message was flagged */
137 mail->flagged_mail_count++;
139 mail->unflagged_mail_count++;
141 if (strchr(mailflags, 'P')) { /*The message was forwarded */
142 mail->forwarded_mail_count++;
144 mail->unforwarded_mail_count++;
146 if (strchr(mailflags, 'R')) { /*The message was replied */
147 mail->replied_mail_count++;
149 mail->unreplied_mail_count++;
151 if (strchr(mailflags, 'D')) { /*The message is a draft */
152 mail->draft_mail_count++;
155 mail->trashed_mail_count++;
159 dirent = readdir(dir);
163 dirname[strlen(dirname) - 3] = '\0';
164 strcat(dirname, "new");
166 dir = opendir(dirname);
168 NORM_ERR("cannot open directory");
172 dirent = readdir(dir);
174 /* . and .. are skipped */
175 if (dirent->d_name[0] != '.') {
176 mail->new_mail_count++;
178 mail->unseen_mail_count++; /* new messages cannot have been seen */
180 dirent = readdir(dir);
189 if (st.st_mtime != mail->last_mtime) {
190 /* yippee, modification time has changed, let's read mail count! */
193 int reading_status = 0;
195 /* could lock here but I don't think it's really worth it because
196 * this isn't going to write mail spool */
198 mail->new_mail_count = mail->mail_count = 0;
200 /* these flags are not supported for mbox */
201 mail->seen_mail_count = mail->unseen_mail_count = -1;
202 mail->flagged_mail_count = mail->unflagged_mail_count = -1;
203 mail->forwarded_mail_count = mail->unforwarded_mail_count = -1;
204 mail->replied_mail_count = mail->unreplied_mail_count = -1;
205 mail->draft_mail_count = mail->trashed_mail_count = -1;
207 fp = open_file(mail->mbox, &rep);
212 /* NOTE: adds mail as new if there isn't Status-field at all */
218 if (fgets(buf, 128, fp) == NULL) {
222 if (strncmp(buf, "From ", 5) == 0) {
223 /* ignore MAILER-DAEMON */
224 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
228 if (reading_status == 1) {
229 mail->new_mail_count++;
235 if (reading_status == 1
236 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
237 int xms = strtol(buf + 17, NULL, 16);
238 /* check that mail isn't marked for deletion */
240 mail->trashed_mail_count++;
242 /* Don't check whether the trashed email is unread */
245 /* check that mail isn't already read */
246 if (!(xms & 0x0001)) {
247 mail->new_mail_count++;
251 /* check for an additional X-Status header */
255 if (reading_status == 1 && strncmp(buf, "Status:", 7) == 0) {
256 /* check that mail isn't already read */
257 if (strchr(buf + 7, 'R') == NULL) {
258 mail->new_mail_count++;
265 if (reading_status >= 1 && strncmp(buf, "X-Status:", 9) == 0) {
266 /* check that mail isn't marked for deletion */
267 if (strchr(buf + 9, 'D') != NULL) {
268 mail->trashed_mail_count++;
269 /* If the mail was previously detected as new,
270 subtract it from the new mail count */
272 mail->new_mail_count--;
281 while (strchr(buf, '\n') == NULL && !feof(fp)) {
288 if (reading_status) {
289 mail->new_mail_count++;
292 mail->last_mtime = st.st_mtime;
296 #define MAXDATASIZE 1000
298 struct mail_s *parse_mail_args(char type, const char *arg)
303 mail = malloc(sizeof(struct mail_s));
304 memset(mail, 0, sizeof(struct mail_s));
306 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
308 if (type == POP3_TYPE) {
309 NORM_ERR("Scanning POP3 args failed");
310 } else if (type == IMAP_TYPE) {
311 NORM_ERR("Scanning IMAP args failed");
315 // see if password needs prompting
316 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
317 int fp = fileno(stdin);
320 tcgetattr(fp, &term);
321 term.c_lflag &= ~ECHO;
322 tcsetattr(fp, TCSANOW, &term);
323 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
324 scanf("%128s", mail->pass);
326 term.c_lflag |= ECHO;
327 tcsetattr(fp, TCSANOW, &term);
329 // now we check for optional args
330 tmp = strstr(arg, "-r ");
333 sscanf(tmp, "%u", &mail->retries);
335 mail->retries = 5; // 5 retries after failure
337 tmp = strstr(arg, "-i ");
340 sscanf(tmp, "%f", &mail->interval);
342 mail->interval = 300; // 5 minutes
344 tmp = strstr(arg, "-p ");
347 sscanf(tmp, "%lu", &mail->port);
349 if (type == POP3_TYPE) {
350 mail->port = 110; // default pop3 port
351 } else if (type == IMAP_TYPE) {
352 mail->port = 143; // default imap port
355 if (type == IMAP_TYPE) {
356 tmp = strstr(arg, "-f ");
359 sscanf(tmp, "%s", mail->folder);
361 strncpy(mail->folder, "INBOX", 128); // default imap inbox
364 tmp = strstr(arg, "-e ");
369 if (tmp[0] == '\'') {
370 len = strstr(tmp + 1, "'") - tmp - 1;
375 strncpy(mail->command, tmp + 1, len);
377 mail->command[0] = '\0';
379 mail->p_timed_thread = NULL;
383 int imap_command(int sockfd, const char *command, char *response, const char *verify)
385 struct timeval fetchtimeout;
387 int res, numbytes = 0;
388 if (send(sockfd, command, strlen(command), 0) == -1) {
392 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
393 fetchtimeout.tv_usec = 0;
395 FD_SET(sockfd, &fdset);
396 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
398 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
403 DBGP2("imap_command() command: %s", command);
404 DBGP2("imap_command() received: %s", response);
405 response[numbytes] = '\0';
406 if (strstr(response, verify) == NULL) {
412 int imap_check_status(char *recvbuf, struct mail_s *mail)
415 reply = strstr(recvbuf, " (MESSAGES ");
416 if (!reply || strlen(reply) < 2) {
420 *strchr(reply, ')') = '\0';
422 NORM_ERR("Error parsing IMAP response: %s", recvbuf);
425 timed_thread_lock(mail->p_timed_thread);
426 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
428 timed_thread_unlock(mail->p_timed_thread);
433 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
435 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
436 || (mail->messages > old_messages && mail->unseen > 0))) {
438 if (system(mail->command) == -1) {
444 void *imap_thread(void *arg)
446 int sockfd, numbytes;
447 char recvbuf[MAXDATASIZE];
448 char sendbuf[MAXDATASIZE];
449 unsigned int fail = 0;
450 unsigned long old_unseen = ULONG_MAX;
451 unsigned long old_messages = ULONG_MAX;
452 struct stat stat_buf;
453 struct hostent he, *he_res = 0;
456 struct sockaddr_in their_addr; // connector's address information
457 struct mail_s *mail = (struct mail_s *)arg;
459 int threadfd = timed_thread_readfd(mail->p_timed_thread);
461 #ifdef HAVE_GETHOSTBYNAME_R
462 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
463 NORM_ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
466 #else /* HAVE_GETHOSTBYNAME_R */
467 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
468 herror("gethostbyname");
471 #endif /* HAVE_GETHOSTBYNAME_R */
472 while (fail < mail->retries) {
473 struct timeval fetchtimeout;
478 NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
479 mail->user, mail->host, fail + 1, mail->retries);
482 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
489 their_addr.sin_family = AF_INET;
490 // short, network byte order
491 their_addr.sin_port = htons(mail->port);
492 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
493 // zero the rest of the struct
494 memset(&(their_addr.sin_zero), '\0', 8);
496 if (connect(sockfd, (struct sockaddr *) &their_addr,
497 sizeof(struct sockaddr)) == -1) {
503 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
504 fetchtimeout.tv_usec = 0;
506 FD_SET(sockfd, &fdset);
507 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
509 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
515 NORM_ERR("IMAP connection failed: timeout");
519 recvbuf[numbytes] = '\0';
520 DBGP2("imap_thread() received: %s", recvbuf);
521 if (strstr(recvbuf, "* OK") != recvbuf) {
522 NORM_ERR("IMAP connection failed, probably not an IMAP server");
526 strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
527 if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
531 if (strstr(recvbuf, " IDLE ") != NULL) {
535 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
536 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
537 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
538 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
539 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
540 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
545 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
546 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
547 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
548 MAXDATASIZE - strlen(sendbuf) - 1);
549 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
554 if (imap_check_status(recvbuf, mail)) {
558 imap_unseen_command(mail, old_unseen, old_messages);
560 old_unseen = mail->unseen;
561 old_messages = mail->messages;
564 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
565 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
566 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
567 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
572 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
573 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
581 * RFC 2177 says we have to re-idle every 29 minutes.
582 * We'll do it every 20 minutes to be safe.
584 fetchtimeout.tv_sec = 1200;
585 fetchtimeout.tv_usec = 0;
588 FD_SET(sockfd, &fdset);
589 FD_SET(threadfd, &fdset);
590 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
591 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
592 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
593 /* if a valid socket, close it */
596 timed_thread_exit(mail->p_timed_thread);
597 } else if (res > 0) {
598 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
599 perror("recv idling");
607 recvbuf[numbytes] = '\0';
608 DBGP2("imap_thread() received: %s", recvbuf);
609 if (strlen(recvbuf) > 2) {
610 unsigned long messages, recent;
612 char force_check = 0;
613 buf = strstr(buf, "EXISTS");
614 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
615 buf = strstr(buf + 1, "EXISTS");
618 // back up until we reach '*'
619 while (buf >= recvbuf && buf[0] != '*') {
622 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
623 timed_thread_lock(mail->p_timed_thread);
624 if (mail->messages != messages) {
626 mail->messages = messages;
628 timed_thread_unlock(mail->p_timed_thread);
632 buf = strstr(buf, "RECENT");
633 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
634 buf = strstr(buf + 1, "RECENT");
637 // back up until we reach '*'
638 while (buf >= recvbuf && buf[0] != '*') {
641 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
646 * check if we got a FETCH from server, recent was
647 * something other than 0, or we had a timeout
650 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
651 // re-check messages and unseen
652 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
656 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
657 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
658 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
659 MAXDATASIZE - strlen(sendbuf) - 1);
660 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
664 if (imap_check_status(recvbuf, mail)) {
668 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
669 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
675 * check if we got a BYE from server
678 if (buf && strstr(buf, "* BYE")) {
679 // need to re-connect
686 imap_unseen_command(mail, old_unseen, old_messages);
688 old_unseen = mail->unseen;
689 old_messages = mail->messages;
693 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
694 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
699 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
700 fetchtimeout.tv_usec = 0;
702 FD_SET(sockfd, &fdset);
703 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
705 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
711 recvbuf[numbytes] = '\0';
712 DBGP2("imap_thread() received: %s", recvbuf);
713 if (strstr(recvbuf, "a3 OK") == NULL) {
714 NORM_ERR("IMAP logout failed: %s", recvbuf);
720 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
721 /* if a valid socket, close it */
724 if (timed_thread_test(mail->p_timed_thread, 0)) {
725 timed_thread_exit(mail->p_timed_thread);
733 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
735 struct timeval fetchtimeout;
737 int res, numbytes = 0;
738 if (send(sockfd, command, strlen(command), 0) == -1) {
742 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
743 fetchtimeout.tv_usec = 0;
745 FD_SET(sockfd, &fdset);
746 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
748 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
753 DBGP2("pop3_command() received: %s", response);
754 response[numbytes] = '\0';
755 if (strstr(response, verify) == NULL) {
761 void *pop3_thread(void *arg)
763 int sockfd, numbytes;
764 char recvbuf[MAXDATASIZE];
765 char sendbuf[MAXDATASIZE];
767 unsigned int fail = 0;
768 unsigned long old_unseen = ULONG_MAX;
769 struct stat stat_buf;
770 struct hostent he, *he_res = 0;
773 struct sockaddr_in their_addr; // connector's address information
774 struct mail_s *mail = (struct mail_s *)arg;
776 #ifdef HAVE_GETHOSTBYNAME_R
777 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
778 NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
781 #else /* HAVE_GETHOSTBYNAME_R */
782 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
783 herror("gethostbyname");
786 #endif /* HAVE_GETHOSTBYNAME_R */
787 while (fail < mail->retries) {
788 struct timeval fetchtimeout;
793 NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
794 mail->user, mail->host, fail + 1, mail->retries);
797 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
804 their_addr.sin_family = AF_INET;
805 // short, network byte order
806 their_addr.sin_port = htons(mail->port);
807 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
808 // zero the rest of the struct
809 memset(&(their_addr.sin_zero), '\0', 8);
811 if (connect(sockfd, (struct sockaddr *) &their_addr,
812 sizeof(struct sockaddr)) == -1) {
818 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
819 fetchtimeout.tv_usec = 0;
821 FD_SET(sockfd, &fdset);
822 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
824 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
830 NORM_ERR("POP3 connection failed: timeout\n");
834 DBGP2("pop3_thread received: %s", recvbuf);
835 recvbuf[numbytes] = '\0';
836 if (strstr(recvbuf, "+OK ") != recvbuf) {
837 NORM_ERR("POP3 connection failed, probably not a POP3 server");
841 strncpy(sendbuf, "USER ", MAXDATASIZE);
842 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
843 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
844 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
849 strncpy(sendbuf, "PASS ", MAXDATASIZE);
850 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
851 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
852 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
853 NORM_ERR("POP3 server login failed: %s", recvbuf);
858 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
859 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
865 // now we get the data
868 NORM_ERR("Error parsing POP3 response: %s", recvbuf);
872 timed_thread_lock(mail->p_timed_thread);
873 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
874 timed_thread_unlock(mail->p_timed_thread);
877 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
878 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
879 NORM_ERR("POP3 logout failed: %s", recvbuf);
884 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
886 if (system(mail->command) == -1) {
891 old_unseen = mail->unseen;
893 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
894 /* if a valid socket, close it */
897 if (timed_thread_test(mail->p_timed_thread, 0)) {
898 timed_thread_exit(mail->p_timed_thread);