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;
91 mail->mail_count = mail->new_mail_count = 0;
92 dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
97 strcpy(dirname, mail->box);
99 /* checking the cur subdirectory */
100 strcat(dirname, "cur");
102 dir = opendir(dirname);
104 ERR("cannot open directory");
108 dirent = readdir(dir);
110 /* . and .. are skipped */
111 if (dirent->d_name[0] != '.') {
114 dirent = readdir(dir);
118 dirname[strlen(dirname) - 3] = '\0';
119 strcat(dirname, "new");
121 dir = opendir(dirname);
123 ERR("cannot open directory");
127 dirent = readdir(dir);
129 /* . and .. are skipped */
130 if (dirent->d_name[0] != '.') {
131 mail->new_mail_count++;
134 dirent = readdir(dir);
143 if (st.st_mtime != mail->last_mtime) {
144 /* yippee, modification time has changed, let's read mail count! */
147 int reading_status = 0;
149 /* could lock here but I don't think it's really worth it because
150 * this isn't going to write mail spool */
152 mail->new_mail_count = mail->mail_count = 0;
154 fp = open_file(mail->box, &rep);
159 /* NOTE: adds mail as new if there isn't Status-field at all */
164 if (fgets(buf, 128, fp) == NULL) {
168 if (strncmp(buf, "From ", 5) == 0) {
169 /* ignore MAILER-DAEMON */
170 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
173 if (reading_status) {
174 mail->new_mail_count++;
181 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
182 /* check that mail isn't already read */
183 if (strchr(buf + 21, '0')) {
184 mail->new_mail_count++;
190 if (reading_status && strncmp(buf, "Status:", 7) == 0) {
191 /* check that mail isn't already read */
192 if (strchr(buf + 7, 'R') == NULL) {
193 mail->new_mail_count++;
202 while (strchr(buf, '\n') == NULL && !feof(fp)) {
209 if (reading_status) {
210 mail->new_mail_count++;
213 mail->last_mtime = st.st_mtime;
217 #define MAXDATASIZE 1000
219 struct mail_s *parse_mail_args(char type, const char *arg)
224 mail = malloc(sizeof(struct mail_s));
225 memset(mail, 0, sizeof(struct mail_s));
227 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
229 if (type == POP3_TYPE) {
230 ERR("Scanning IMAP args failed");
231 } else if (type == IMAP_TYPE) {
232 ERR("Scanning POP3 args failed");
235 // see if password needs prompting
236 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
237 int fp = fileno(stdin);
240 tcgetattr(fp, &term);
241 term.c_lflag &= ~ECHO;
242 tcsetattr(fp, TCSANOW, &term);
243 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
244 scanf("%128s", mail->pass);
246 term.c_lflag |= ECHO;
247 tcsetattr(fp, TCSANOW, &term);
249 // now we check for optional args
250 tmp = strstr(arg, "-r ");
253 sscanf(tmp, "%u", &mail->retries);
255 mail->retries = 5; // 5 retries after failure
257 tmp = strstr(arg, "-i ");
260 sscanf(tmp, "%f", &mail->interval);
262 mail->interval = 300; // 5 minutes
264 tmp = strstr(arg, "-p ");
267 sscanf(tmp, "%lu", &mail->port);
269 if (type == POP3_TYPE) {
270 mail->port = 110; // default pop3 port
271 } else if (type == IMAP_TYPE) {
272 mail->port = 143; // default imap port
275 if (type == IMAP_TYPE) {
276 tmp = strstr(arg, "-f ");
279 sscanf(tmp, "%s", mail->folder);
281 strncpy(mail->folder, "INBOX", 128); // default imap inbox
284 tmp = strstr(arg, "-e ");
289 if (tmp[0] == '\'') {
290 len = strstr(tmp + 1, "'") - tmp - 1;
295 strncpy(mail->command, tmp + 1, len);
297 mail->command[0] = '\0';
299 mail->p_timed_thread = NULL;
303 int imap_command(int sockfd, const char *command, char *response, const char *verify)
305 struct timeval timeout;
307 int res, numbytes = 0;
308 if (send(sockfd, command, strlen(command), 0) == -1) {
312 timeout.tv_sec = 60; // 60 second timeout i guess
315 FD_SET(sockfd, &fdset);
316 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
318 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
323 DBGP2("imap_command() command: %s", command);
324 DBGP2("imap_command() received: %s", response);
325 response[numbytes] = '\0';
326 if (strstr(response, verify) == NULL) {
332 int imap_check_status(char *recvbuf, struct mail_s *mail)
335 reply = strstr(recvbuf, " (MESSAGES ");
336 if (!reply || strlen(reply) < 2) {
340 *strchr(reply, ')') = '\0';
342 ERR("Error parsing IMAP response: %s", recvbuf);
345 timed_thread_lock(mail->p_timed_thread);
346 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
348 timed_thread_unlock(mail->p_timed_thread);
353 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
355 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
356 || (mail->messages > old_messages && mail->unseen > 0))) {
358 if (system(mail->command) == -1) {
364 void *imap_thread(void *arg)
366 int sockfd, numbytes;
367 char recvbuf[MAXDATASIZE];
368 char sendbuf[MAXDATASIZE];
369 unsigned int fail = 0;
370 unsigned long old_unseen = ULONG_MAX;
371 unsigned long old_messages = ULONG_MAX;
372 struct stat stat_buf;
373 struct hostent he, *he_res = 0;
376 struct sockaddr_in their_addr; // connector's address information
377 struct mail_s *mail = (struct mail_s *)arg;
379 int threadfd = timed_thread_readfd(mail->p_timed_thread);
381 #ifdef HAVE_GETHOSTBYNAME_R
382 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
383 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
386 #else /* HAVE_GETHOSTBYNAME_R */
387 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
388 herror("gethostbyname");
391 #endif /* HAVE_GETHOSTBYNAME_R */
392 while (fail < mail->retries) {
393 struct timeval timeout;
398 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
399 mail->user, mail->host, fail + 1, mail->retries);
402 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
409 their_addr.sin_family = AF_INET;
410 // short, network byte order
411 their_addr.sin_port = htons(mail->port);
412 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
413 // zero the rest of the struct
414 memset(&(their_addr.sin_zero), '\0', 8);
416 if (connect(sockfd, (struct sockaddr *) &their_addr,
417 sizeof(struct sockaddr)) == -1) {
423 timeout.tv_sec = 60; // 60 second timeout i guess
426 FD_SET(sockfd, &fdset);
427 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
429 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
435 ERR("IMAP connection failed: timeout");
439 recvbuf[numbytes] = '\0';
440 DBGP2("imap_thread() received: %s", recvbuf);
441 if (strstr(recvbuf, "* OK") != recvbuf) {
442 ERR("IMAP connection failed, probably not an IMAP server");
446 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
447 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
448 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
449 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
450 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
451 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
455 if (strstr(recvbuf, " IDLE ") != NULL) {
459 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
460 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
461 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
462 MAXDATASIZE - strlen(sendbuf) - 1);
463 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
468 if (imap_check_status(recvbuf, mail)) {
472 imap_unseen_command(mail, old_unseen, old_messages);
474 old_unseen = mail->unseen;
475 old_messages = mail->messages;
478 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
479 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
480 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
481 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
486 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
487 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
495 * RFC 2177 says we have to re-idle every 29 minutes.
496 * We'll do it every 20 minutes to be safe.
498 timeout.tv_sec = 1200;
502 FD_SET(sockfd, &fdset);
503 FD_SET(threadfd, &fdset);
504 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
505 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
506 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
507 /* if a valid socket, close it */
510 timed_thread_exit(mail->p_timed_thread);
511 } else if (res > 0) {
512 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
513 perror("recv idling");
520 recvbuf[numbytes] = '\0';
521 DBGP2("imap_thread() received: %s", recvbuf);
522 if (strlen(recvbuf) > 2) {
523 unsigned long messages, recent;
525 char force_check = 0;
526 buf = strstr(buf, "EXISTS");
527 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
528 buf = strstr(buf + 1, "EXISTS");
531 // back up until we reach '*'
532 while (buf >= recvbuf && buf[0] != '*') {
535 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
536 timed_thread_lock(mail->p_timed_thread);
537 if (mail->messages != messages) {
539 mail->messages = messages;
541 timed_thread_unlock(mail->p_timed_thread);
545 buf = strstr(buf, "RECENT");
546 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
547 buf = strstr(buf + 1, "RECENT");
550 // back up until we reach '*'
551 while (buf >= recvbuf && buf[0] != '*') {
554 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
559 * check if we got a FETCH from server, recent was
560 * something other than 0, or we had a timeout
563 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
564 // re-check messages and unseen
565 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
569 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
570 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
571 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
572 MAXDATASIZE - strlen(sendbuf) - 1);
573 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
577 if (imap_check_status(recvbuf, mail)) {
581 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
582 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
588 * check if we got a BYE from server
591 if (buf && strstr(buf, "* BYE")) {
592 // need to re-connect
596 imap_unseen_command(mail, old_unseen, old_messages);
598 old_unseen = mail->unseen;
599 old_messages = mail->messages;
602 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
603 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
608 timeout.tv_sec = 60; // 60 second timeout i guess
611 FD_SET(sockfd, &fdset);
612 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
614 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
620 recvbuf[numbytes] = '\0';
621 DBGP2("imap_thread() received: %s", recvbuf);
622 if (strstr(recvbuf, "a3 OK") == NULL) {
623 ERR("IMAP logout failed: %s", recvbuf);
629 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
630 /* if a valid socket, close it */
633 if (timed_thread_test(mail->p_timed_thread, 0)) {
634 timed_thread_exit(mail->p_timed_thread);
642 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
644 struct timeval timeout;
646 int res, numbytes = 0;
647 if (send(sockfd, command, strlen(command), 0) == -1) {
651 timeout.tv_sec = 60; // 60 second timeout i guess
654 FD_SET(sockfd, &fdset);
655 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
657 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
662 DBGP2("pop3_command() received: %s", response);
663 response[numbytes] = '\0';
664 if (strstr(response, verify) == NULL) {
670 void *pop3_thread(void *arg)
672 int sockfd, numbytes;
673 char recvbuf[MAXDATASIZE];
674 char sendbuf[MAXDATASIZE];
676 unsigned int fail = 0;
677 unsigned long old_unseen = ULONG_MAX;
678 struct stat stat_buf;
679 struct hostent he, *he_res = 0;
682 struct sockaddr_in their_addr; // connector's address information
683 struct mail_s *mail = (struct mail_s *)arg;
685 #ifdef HAVE_GETHOSTBYNAME_R
686 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
687 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
690 #else /* HAVE_GETHOSTBYNAME_R */
691 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
692 herror("gethostbyname");
695 #endif /* HAVE_GETHOSTBYNAME_R */
696 while (fail < mail->retries) {
697 struct timeval timeout;
702 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
703 mail->user, mail->host, fail + 1, mail->retries);
706 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
713 their_addr.sin_family = AF_INET;
714 // short, network byte order
715 their_addr.sin_port = htons(mail->port);
716 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
717 // zero the rest of the struct
718 memset(&(their_addr.sin_zero), '\0', 8);
720 if (connect(sockfd, (struct sockaddr *) &their_addr,
721 sizeof(struct sockaddr)) == -1) {
727 timeout.tv_sec = 60; // 60 second timeout i guess
730 FD_SET(sockfd, &fdset);
731 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
733 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
739 ERR("POP3 connection failed: timeout\n");
743 DBGP2("pop3_thread received: %s", recvbuf);
744 recvbuf[numbytes] = '\0';
745 if (strstr(recvbuf, "+OK ") != recvbuf) {
746 ERR("POP3 connection failed, probably not a POP3 server");
750 strncpy(sendbuf, "USER ", MAXDATASIZE);
751 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
752 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
753 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
758 strncpy(sendbuf, "PASS ", MAXDATASIZE);
759 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
760 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
761 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
762 ERR("POP3 server login failed: %s", recvbuf);
767 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
768 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
774 // now we get the data
777 ERR("Error parsing POP3 response: %s", recvbuf);
781 timed_thread_lock(mail->p_timed_thread);
782 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
783 timed_thread_unlock(mail->p_timed_thread);
786 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
787 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
788 ERR("POP3 logout failed: %s", recvbuf);
793 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
795 if (system(mail->command) == -1) {
800 old_unseen = mail->unseen;
802 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
803 /* if a valid socket, close it */
806 if (timed_thread_test(mail->p_timed_thread, 0)) {
807 timed_thread_exit(mail->p_timed_thread);