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() received: %s", response);
322 response[numbytes] = '\0';
323 if (strstr(response, verify) == NULL) {
329 int imap_check_status(char *recvbuf, struct mail_s *mail)
332 reply = strstr(recvbuf, " (MESSAGES ");
333 if (!reply || strlen(reply) < 2) {
337 *strchr(reply, ')') = '\0';
339 ERR("Error parsing IMAP response: %s", recvbuf);
342 timed_thread_lock(mail->p_timed_thread);
343 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
345 timed_thread_unlock(mail->p_timed_thread);
350 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
352 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
353 || (mail->messages > old_messages && mail->unseen > 0))) {
355 if (system(mail->command) == -1) {
361 void *imap_thread(void *arg)
363 int sockfd, numbytes;
364 char recvbuf[MAXDATASIZE];
365 char sendbuf[MAXDATASIZE];
366 unsigned int fail = 0;
367 unsigned long old_unseen = ULONG_MAX;
368 unsigned long old_messages = ULONG_MAX;
369 struct stat stat_buf;
370 struct hostent he, *he_res = 0;
373 struct sockaddr_in their_addr; // connector's address information
374 struct mail_s *mail = (struct mail_s *)arg;
376 int threadfd = timed_thread_readfd(mail->p_timed_thread);
378 #ifdef HAVE_GETHOSTBYNAME_R
379 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
380 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
383 #else /* HAVE_GETHOSTBYNAME_R */
384 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
385 herror("gethostbyname");
388 #endif /* HAVE_GETHOSTBYNAME_R */
389 while (fail < mail->retries) {
390 struct timeval timeout;
395 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
396 mail->user, mail->host, fail + 1, mail->retries);
399 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
406 their_addr.sin_family = AF_INET;
407 // short, network byte order
408 their_addr.sin_port = htons(mail->port);
409 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
410 // zero the rest of the struct
411 memset(&(their_addr.sin_zero), '\0', 8);
413 if (connect(sockfd, (struct sockaddr *) &their_addr,
414 sizeof(struct sockaddr)) == -1) {
420 timeout.tv_sec = 60; // 60 second timeout i guess
423 FD_SET(sockfd, &fdset);
424 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
426 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
432 ERR("IMAP connection failed: timeout");
436 recvbuf[numbytes] = '\0';
437 DBGP2("imap_thread() received: %s", recvbuf);
438 if (strstr(recvbuf, "* OK") != recvbuf) {
439 ERR("IMAP connection failed, probably not an IMAP server");
443 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
444 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
445 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
446 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
447 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
448 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
452 if (strstr(recvbuf, " IDLE ") != NULL) {
456 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
457 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
458 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
459 MAXDATASIZE - strlen(sendbuf) - 1);
460 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
465 if (imap_check_status(recvbuf, mail)) {
469 imap_unseen_command(mail, old_unseen, old_messages);
471 old_unseen = mail->unseen;
472 old_messages = mail->messages;
475 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
476 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
477 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
478 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
483 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
484 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
492 * RFC 2177 says we have to re-idle every 29 minutes.
493 * We'll do it every 20 minutes to be safe.
495 timeout.tv_sec = 1200;
498 FD_SET(sockfd, &fdset);
499 FD_SET(threadfd, &fdset);
500 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
501 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
502 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
503 /* if a valid socket, close it */
506 timed_thread_exit(mail->p_timed_thread);
507 } else if (res > 0) {
508 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
509 perror("recv idling");
516 recvbuf[numbytes] = '\0';
517 DBGP2("imap_thread() received: %s", recvbuf);
518 if (strlen(recvbuf) > 2) {
519 unsigned long messages, recent;
521 buf = strstr(buf, "EXISTS");
522 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
523 buf = strstr(buf + 1, "EXISTS");
526 // back up until we reach '*'
527 while (buf >= recvbuf && buf[0] != '*') {
530 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
531 timed_thread_lock(mail->p_timed_thread);
532 mail->messages = messages;
533 timed_thread_unlock(mail->p_timed_thread);
537 buf = strstr(buf, "RECENT");
538 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
539 buf = strstr(buf + 1, "RECENT");
542 // back up until we reach '*'
543 while (buf >= recvbuf && buf[0] != '*') {
546 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
551 * check if we got a FETCH from server, recent was
552 * something other than 0, or we had a timeout
555 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0) {
556 // re-check messages and unseen
557 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
561 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
562 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
563 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
564 MAXDATASIZE - strlen(sendbuf) - 1);
565 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
569 if (imap_check_status(recvbuf, mail)) {
573 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
574 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
580 * check if we got a BYE from server
583 if (buf && strstr(buf, "* BYE")) {
584 // need to re-connect
588 imap_unseen_command(mail, old_unseen, old_messages);
590 old_unseen = mail->unseen;
591 old_messages = mail->messages;
594 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
595 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
600 timeout.tv_sec = 60; // 60 second timeout i guess
603 FD_SET(sockfd, &fdset);
604 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
606 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
612 recvbuf[numbytes] = '\0';
613 DBGP2("imap_thread() received: %s", recvbuf);
614 if (strstr(recvbuf, "a3 OK") == NULL) {
615 ERR("IMAP logout failed: %s", recvbuf);
621 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
622 /* if a valid socket, close it */
625 if (timed_thread_test(mail->p_timed_thread, 0)) {
626 timed_thread_exit(mail->p_timed_thread);
634 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
636 struct timeval timeout;
638 int res, numbytes = 0;
639 if (send(sockfd, command, strlen(command), 0) == -1) {
643 timeout.tv_sec = 60; // 60 second timeout i guess
646 FD_SET(sockfd, &fdset);
647 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
649 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
654 DBGP2("pop3_command() received: %s", response);
655 response[numbytes] = '\0';
656 if (strstr(response, verify) == NULL) {
662 void *pop3_thread(void *arg)
664 int sockfd, numbytes;
665 char recvbuf[MAXDATASIZE];
666 char sendbuf[MAXDATASIZE];
668 unsigned int fail = 0;
669 unsigned long old_unseen = ULONG_MAX;
670 struct stat stat_buf;
671 struct hostent he, *he_res = 0;
674 struct sockaddr_in their_addr; // connector's address information
675 struct mail_s *mail = (struct mail_s *)arg;
677 #ifdef HAVE_GETHOSTBYNAME_R
678 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
679 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
682 #else /* HAVE_GETHOSTBYNAME_R */
683 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
684 herror("gethostbyname");
687 #endif /* HAVE_GETHOSTBYNAME_R */
688 while (fail < mail->retries) {
689 struct timeval timeout;
694 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
695 mail->user, mail->host, fail + 1, mail->retries);
698 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
705 their_addr.sin_family = AF_INET;
706 // short, network byte order
707 their_addr.sin_port = htons(mail->port);
708 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
709 // zero the rest of the struct
710 memset(&(their_addr.sin_zero), '\0', 8);
712 if (connect(sockfd, (struct sockaddr *) &their_addr,
713 sizeof(struct sockaddr)) == -1) {
719 timeout.tv_sec = 60; // 60 second timeout i guess
722 FD_SET(sockfd, &fdset);
723 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
725 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
731 ERR("POP3 connection failed: timeout\n");
735 DBGP2("pop3_thread received: %s", recvbuf);
736 recvbuf[numbytes] = '\0';
737 if (strstr(recvbuf, "+OK ") != recvbuf) {
738 ERR("POP3 connection failed, probably not a POP3 server");
742 strncpy(sendbuf, "USER ", MAXDATASIZE);
743 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
744 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
745 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
750 strncpy(sendbuf, "PASS ", MAXDATASIZE);
751 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
752 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
753 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
754 ERR("POP3 server login failed: %s", recvbuf);
759 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
760 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
766 // now we get the data
769 ERR("Error parsing POP3 response: %s", recvbuf);
773 timed_thread_lock(mail->p_timed_thread);
774 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
775 timed_thread_unlock(mail->p_timed_thread);
778 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
779 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
780 ERR("POP3 logout failed: %s", recvbuf);
785 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
787 if (system(mail->command) == -1) {
792 old_unseen = mail->unseen;
794 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
795 /* if a valid socket, close it */
798 if (timed_thread_test(mail->p_timed_thread, 0)) {
799 timed_thread_exit(mail->p_timed_thread);