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 <sys/socket.h>
47 /* MAX() is defined by a header included from conky.h
48 * maybe once this is not true anymore, so have an alternative
51 * #define MAX(a, b) ((a > b) ? a : b)
54 char *current_mail_spool;
56 void update_mail_count(struct local_mail_s *mail)
64 /* TODO: use that fine file modification notify on Linux 2.4 */
66 /* don't check mail so often (9.5s is minimum interval) */
67 if (current_update_time - mail->last_update < 9.5) {
70 mail->last_update = current_update_time;
73 if (stat(mail->box, &st)) {
77 ERR("can't stat %s: %s", mail->box, strerror(errno));
84 if (S_ISDIR(st.st_mode)) {
87 struct dirent *dirent;
89 mail->mail_count = mail->new_mail_count = 0;
90 dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
95 strcpy(dirname, mail->box);
97 /* checking the cur subdirectory */
98 strcat(dirname, "cur");
100 dir = opendir(dirname);
102 ERR("cannot open directory");
106 dirent = readdir(dir);
108 /* . and .. are skipped */
109 if (dirent->d_name[0] != '.') {
112 dirent = readdir(dir);
116 dirname[strlen(dirname) - 3] = '\0';
117 strcat(dirname, "new");
119 dir = opendir(dirname);
121 ERR("cannot open directory");
125 dirent = readdir(dir);
127 /* . and .. are skipped */
128 if (dirent->d_name[0] != '.') {
129 mail->new_mail_count++;
132 dirent = readdir(dir);
141 if (st.st_mtime != mail->last_mtime) {
142 /* yippee, modification time has changed, let's read mail count! */
145 int reading_status = 0;
147 /* could lock here but I don't think it's really worth it because
148 * this isn't going to write mail spool */
150 mail->new_mail_count = mail->mail_count = 0;
152 fp = open_file(mail->box, &rep);
157 /* NOTE: adds mail as new if there isn't Status-field at all */
162 if (fgets(buf, 128, fp) == NULL) {
166 if (strncmp(buf, "From ", 5) == 0) {
167 /* ignore MAILER-DAEMON */
168 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
171 if (reading_status) {
172 mail->new_mail_count++;
179 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
180 /* check that mail isn't already read */
181 if (strchr(buf + 21, '0')) {
182 mail->new_mail_count++;
188 if (reading_status && strncmp(buf, "Status:", 7) == 0) {
189 /* check that mail isn't already read */
190 if (strchr(buf + 7, 'R') == NULL) {
191 mail->new_mail_count++;
200 while (strchr(buf, '\n') == NULL && !feof(fp)) {
207 if (reading_status) {
208 mail->new_mail_count++;
211 mail->last_mtime = st.st_mtime;
215 #define MAXDATASIZE 1000
217 struct mail_s *parse_mail_args(char type, const char *arg)
222 mail = malloc(sizeof(struct mail_s));
223 memset(mail, 0, sizeof(struct mail_s));
225 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
227 if (type == POP3_TYPE) {
228 ERR("Scanning IMAP args failed");
229 } else if (type == IMAP_TYPE) {
230 ERR("Scanning POP3 args failed");
233 // see if password needs prompting
234 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
235 int fp = fileno(stdin);
238 tcgetattr(fp, &term);
239 term.c_lflag &= ~ECHO;
240 tcsetattr(fp, TCSANOW, &term);
241 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
242 scanf("%128s", mail->pass);
244 term.c_lflag |= ECHO;
245 tcsetattr(fp, TCSANOW, &term);
247 // now we check for optional args
248 tmp = strstr(arg, "-r ");
251 sscanf(tmp, "%u", &mail->retries);
253 mail->retries = 5; // 5 retries after failure
255 tmp = strstr(arg, "-i ");
258 sscanf(tmp, "%f", &mail->interval);
260 mail->interval = 300; // 5 minutes
262 tmp = strstr(arg, "-p ");
265 sscanf(tmp, "%lu", &mail->port);
267 if (type == POP3_TYPE) {
268 mail->port = 110; // default pop3 port
269 } else if (type == IMAP_TYPE) {
270 mail->port = 143; // default imap port
273 if (type == IMAP_TYPE) {
274 tmp = strstr(arg, "-f ");
277 sscanf(tmp, "%s", mail->folder);
279 strncpy(mail->folder, "INBOX", 128); // default imap inbox
282 tmp = strstr(arg, "-e ");
287 if (tmp[0] == '\'') {
288 len = strstr(tmp + 1, "'") - tmp - 1;
293 strncpy(mail->command, tmp + 1, len);
295 mail->command[0] = '\0';
297 mail->p_timed_thread = NULL;
301 int imap_command(int sockfd, const char *command, char *response, const char *verify)
303 struct timeval timeout;
305 int res, numbytes = 0;
306 if (send(sockfd, command, strlen(command), 0) == -1) {
310 timeout.tv_sec = 60; // 60 second timeout i guess
313 FD_SET(sockfd, &fdset);
314 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
316 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
321 DBGP2("imap_command() command: %s", command);
322 DBGP2("imap_command() received: %s", response);
323 response[numbytes] = '\0';
324 if (strstr(response, verify) == NULL) {
330 int imap_check_status(char *recvbuf, struct mail_s *mail)
333 reply = strstr(recvbuf, " (MESSAGES ");
334 if (!reply || strlen(reply) < 2) {
338 *strchr(reply, ')') = '\0';
340 ERR("Error parsing IMAP response: %s", recvbuf);
343 timed_thread_lock(mail->p_timed_thread);
344 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
346 timed_thread_unlock(mail->p_timed_thread);
351 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
353 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
354 || (mail->messages > old_messages && mail->unseen > 0))) {
356 if (system(mail->command) == -1) {
362 void *imap_thread(void *arg)
364 int sockfd, numbytes;
365 char recvbuf[MAXDATASIZE];
366 char sendbuf[MAXDATASIZE];
367 unsigned int fail = 0;
368 unsigned long old_unseen = ULONG_MAX;
369 unsigned long old_messages = ULONG_MAX;
370 struct stat stat_buf;
371 struct hostent he, *he_res = 0;
374 struct sockaddr_in their_addr; // connector's address information
375 struct mail_s *mail = (struct mail_s *)arg;
377 int threadfd = timed_thread_readfd(mail->p_timed_thread);
379 #ifdef HAVE_GETHOSTBYNAME_R
380 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
381 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
384 #else /* HAVE_GETHOSTBYNAME_R */
385 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
386 herror("gethostbyname");
389 #endif /* HAVE_GETHOSTBYNAME_R */
390 while (fail < mail->retries) {
391 struct timeval timeout;
396 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
397 mail->user, mail->host, fail + 1, mail->retries);
400 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
407 their_addr.sin_family = AF_INET;
408 // short, network byte order
409 their_addr.sin_port = htons(mail->port);
410 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
411 // zero the rest of the struct
412 memset(&(their_addr.sin_zero), '\0', 8);
414 if (connect(sockfd, (struct sockaddr *) &their_addr,
415 sizeof(struct sockaddr)) == -1) {
421 timeout.tv_sec = 60; // 60 second timeout i guess
424 FD_SET(sockfd, &fdset);
425 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
427 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
433 ERR("IMAP connection failed: timeout");
437 recvbuf[numbytes] = '\0';
438 DBGP2("imap_thread() received: %s", recvbuf);
439 if (strstr(recvbuf, "* OK") != recvbuf) {
440 ERR("IMAP connection failed, probably not an IMAP server");
444 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
445 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
446 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
447 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
448 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
449 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
453 if (strstr(recvbuf, " IDLE ") != NULL) {
457 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
458 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
459 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
460 MAXDATASIZE - strlen(sendbuf) - 1);
461 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
466 if (imap_check_status(recvbuf, mail)) {
470 imap_unseen_command(mail, old_unseen, old_messages);
472 old_unseen = mail->unseen;
473 old_messages = mail->messages;
476 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
477 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
478 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
479 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
484 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
485 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
493 * RFC 2177 says we have to re-idle every 29 minutes.
494 * We'll do it every 20 minutes to be safe.
496 timeout.tv_sec = 1200;
500 FD_SET(sockfd, &fdset);
501 FD_SET(threadfd, &fdset);
502 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
503 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
504 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
505 /* if a valid socket, close it */
508 timed_thread_exit(mail->p_timed_thread);
509 } else if (res > 0) {
510 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
511 perror("recv idling");
518 recvbuf[numbytes] = '\0';
519 DBGP2("imap_thread() received: %s", recvbuf);
520 if (strlen(recvbuf) > 2) {
521 unsigned long messages, recent;
523 char force_check = 0;
524 buf = strstr(buf, "EXISTS");
525 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
526 buf = strstr(buf + 1, "EXISTS");
529 // back up until we reach '*'
530 while (buf >= recvbuf && buf[0] != '*') {
533 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
534 timed_thread_lock(mail->p_timed_thread);
535 if (mail->messages != messages) {
537 mail->messages = messages;
539 timed_thread_unlock(mail->p_timed_thread);
543 buf = strstr(buf, "RECENT");
544 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
545 buf = strstr(buf + 1, "RECENT");
548 // back up until we reach '*'
549 while (buf >= recvbuf && buf[0] != '*') {
552 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
557 * check if we got a FETCH from server, recent was
558 * something other than 0, or we had a timeout
561 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
562 // re-check messages and unseen
563 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
567 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
568 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
569 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
570 MAXDATASIZE - strlen(sendbuf) - 1);
571 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
575 if (imap_check_status(recvbuf, mail)) {
579 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
580 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
586 * check if we got a BYE from server
589 if (buf && strstr(buf, "* BYE")) {
590 // need to re-connect
594 imap_unseen_command(mail, old_unseen, old_messages);
596 old_unseen = mail->unseen;
597 old_messages = mail->messages;
600 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
601 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
606 timeout.tv_sec = 60; // 60 second timeout i guess
609 FD_SET(sockfd, &fdset);
610 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
612 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
618 recvbuf[numbytes] = '\0';
619 DBGP2("imap_thread() received: %s", recvbuf);
620 if (strstr(recvbuf, "a3 OK") == NULL) {
621 ERR("IMAP logout failed: %s", recvbuf);
627 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
628 /* if a valid socket, close it */
631 if (timed_thread_test(mail->p_timed_thread, 0)) {
632 timed_thread_exit(mail->p_timed_thread);
640 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
642 struct timeval timeout;
644 int res, numbytes = 0;
645 if (send(sockfd, command, strlen(command), 0) == -1) {
649 timeout.tv_sec = 60; // 60 second timeout i guess
652 FD_SET(sockfd, &fdset);
653 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
655 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
660 DBGP2("pop3_command() received: %s", response);
661 response[numbytes] = '\0';
662 if (strstr(response, verify) == NULL) {
668 void *pop3_thread(void *arg)
670 int sockfd, numbytes;
671 char recvbuf[MAXDATASIZE];
672 char sendbuf[MAXDATASIZE];
674 unsigned int fail = 0;
675 unsigned long old_unseen = ULONG_MAX;
676 struct stat stat_buf;
677 struct hostent he, *he_res = 0;
680 struct sockaddr_in their_addr; // connector's address information
681 struct mail_s *mail = (struct mail_s *)arg;
683 #ifdef HAVE_GETHOSTBYNAME_R
684 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
685 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
688 #else /* HAVE_GETHOSTBYNAME_R */
689 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
690 herror("gethostbyname");
693 #endif /* HAVE_GETHOSTBYNAME_R */
694 while (fail < mail->retries) {
695 struct timeval timeout;
700 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
701 mail->user, mail->host, fail + 1, mail->retries);
704 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
711 their_addr.sin_family = AF_INET;
712 // short, network byte order
713 their_addr.sin_port = htons(mail->port);
714 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
715 // zero the rest of the struct
716 memset(&(their_addr.sin_zero), '\0', 8);
718 if (connect(sockfd, (struct sockaddr *) &their_addr,
719 sizeof(struct sockaddr)) == -1) {
725 timeout.tv_sec = 60; // 60 second timeout i guess
728 FD_SET(sockfd, &fdset);
729 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
731 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
737 ERR("POP3 connection failed: timeout\n");
741 DBGP2("pop3_thread received: %s", recvbuf);
742 recvbuf[numbytes] = '\0';
743 if (strstr(recvbuf, "+OK ") != recvbuf) {
744 ERR("POP3 connection failed, probably not a POP3 server");
748 strncpy(sendbuf, "USER ", MAXDATASIZE);
749 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
750 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
751 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
756 strncpy(sendbuf, "PASS ", MAXDATASIZE);
757 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
758 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
759 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
760 ERR("POP3 server login failed: %s", recvbuf);
765 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
766 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
772 // now we get the data
775 ERR("Error parsing POP3 response: %s", recvbuf);
779 timed_thread_lock(mail->p_timed_thread);
780 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
781 timed_thread_unlock(mail->p_timed_thread);
784 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
785 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
786 ERR("POP3 logout failed: %s", recvbuf);
791 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
793 if (system(mail->command) == -1) {
798 old_unseen = mail->unseen;
800 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
801 /* if a valid socket, close it */
804 if (timed_thread_test(mail->p_timed_thread, 0)) {
805 timed_thread_exit(mail->p_timed_thread);