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-2010 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/>.
35 #include "text_object.h"
36 #include "timed_thread.h"
43 #include <netinet/in.h>
45 #include <sys/socket.h>
48 #include <sys/param.h>
54 /* MAX() is defined by a header included from conky.h
55 * maybe once this is not true anymore, so have an alternative
58 * #define MAX(a, b) ((a > b) ? a : b)
66 struct mail_s { // for imap and pop3
68 unsigned long messages;
78 char command[MAXSIZE];
80 timed_thread *p_timed_thread;
89 int unseen_mail_count;
90 int flagged_mail_count;
91 int unflagged_mail_count;
92 int forwarded_mail_count;
93 int unforwarded_mail_count;
94 int replied_mail_count;
95 int unreplied_mail_count;
97 int trashed_mail_count;
103 char *current_mail_spool;
105 static struct mail_s *global_mail;
106 static int global_mail_use = 0;
108 static void update_mail_count(struct local_mail_s *mail)
116 /* TODO: use that fine file modification notify on Linux 2.4 */
118 /* don't check mail so often (9.5s is minimum interval) */
119 if (current_update_time - mail->last_update < 9.5) {
122 mail->last_update = current_update_time;
125 if (stat(mail->mbox, &st)) {
129 NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
136 if (S_ISDIR(st.st_mode)) {
139 struct dirent *dirent;
142 mail->mail_count = mail->new_mail_count = 0;
143 mail->seen_mail_count = mail->unseen_mail_count = 0;
144 mail->flagged_mail_count = mail->unflagged_mail_count = 0;
145 mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
146 mail->replied_mail_count = mail->unreplied_mail_count = 0;
147 mail->draft_mail_count = mail->trashed_mail_count = 0;
148 dirname = (char *) malloc(sizeof(char) * (strlen(mail->mbox) + 5));
153 strcpy(dirname, mail->mbox);
154 strcat(dirname, "/");
155 /* checking the cur subdirectory */
156 strcat(dirname, "cur");
158 dir = opendir(dirname);
160 NORM_ERR("cannot open directory");
164 dirent = readdir(dir);
166 /* . and .. are skipped */
167 if (dirent->d_name[0] != '.') {
169 mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
175 strcpy(mailflags, strrchr(dirent->d_name, ','));
176 if (!strchr(mailflags, 'T')) { /* The message is not in the trash */
177 if (strchr(mailflags, 'S')) { /*The message has been seen */
178 mail->seen_mail_count++;
180 mail->unseen_mail_count++;
182 if (strchr(mailflags, 'F')) { /*The message was flagged */
183 mail->flagged_mail_count++;
185 mail->unflagged_mail_count++;
187 if (strchr(mailflags, 'P')) { /*The message was forwarded */
188 mail->forwarded_mail_count++;
190 mail->unforwarded_mail_count++;
192 if (strchr(mailflags, 'R')) { /*The message was replied */
193 mail->replied_mail_count++;
195 mail->unreplied_mail_count++;
197 if (strchr(mailflags, 'D')) { /*The message is a draft */
198 mail->draft_mail_count++;
201 mail->trashed_mail_count++;
205 dirent = readdir(dir);
209 dirname[strlen(dirname) - 3] = '\0';
210 strcat(dirname, "new");
212 dir = opendir(dirname);
214 NORM_ERR("cannot open directory");
218 dirent = readdir(dir);
220 /* . and .. are skipped */
221 if (dirent->d_name[0] != '.') {
222 mail->new_mail_count++;
224 mail->unseen_mail_count++; /* new messages cannot have been seen */
226 dirent = readdir(dir);
235 if (st.st_mtime != mail->last_mtime) {
236 /* yippee, modification time has changed, let's read mail count! */
239 int reading_status = 0;
241 /* could lock here but I don't think it's really worth it because
242 * this isn't going to write mail spool */
244 mail->new_mail_count = mail->mail_count = 0;
246 /* these flags are not supported for mbox */
247 mail->seen_mail_count = mail->unseen_mail_count = -1;
248 mail->flagged_mail_count = mail->unflagged_mail_count = -1;
249 mail->forwarded_mail_count = mail->unforwarded_mail_count = -1;
250 mail->replied_mail_count = mail->unreplied_mail_count = -1;
251 mail->draft_mail_count = mail->trashed_mail_count = -1;
253 fp = open_file(mail->mbox, &rep);
258 /* NOTE: adds mail as new if there isn't Status-field at all */
264 if (fgets(buf, 128, fp) == NULL) {
268 if (strncmp(buf, "From ", 5) == 0) {
269 /* ignore MAILER-DAEMON */
270 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
274 if (reading_status == 1) {
275 mail->new_mail_count++;
281 if (reading_status == 1
282 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
283 int xms = strtol(buf + 17, NULL, 16);
284 /* check that mail isn't marked for deletion */
286 mail->trashed_mail_count++;
288 /* Don't check whether the trashed email is unread */
291 /* check that mail isn't already read */
292 if (!(xms & 0x0001)) {
293 mail->new_mail_count++;
297 /* check for an additional X-Status header */
301 if (reading_status == 1 && strncmp(buf, "Status:", 7) == 0) {
302 /* check that mail isn't already read */
303 if (strchr(buf + 7, 'R') == NULL) {
304 mail->new_mail_count++;
311 if (reading_status >= 1 && strncmp(buf, "X-Status:", 9) == 0) {
312 /* check that mail isn't marked for deletion */
313 if (strchr(buf + 9, 'D') != NULL) {
314 mail->trashed_mail_count++;
315 /* If the mail was previously detected as new,
316 subtract it from the new mail count */
318 mail->new_mail_count--;
327 while (strchr(buf, '\n') == NULL && !feof(fp)) {
334 if (reading_status) {
335 mail->new_mail_count++;
338 mail->last_mtime = st.st_mtime;
342 void parse_local_mail_args(struct text_object *obj, const char *arg)
345 char mbox[256], dst[256];
346 struct local_mail_s *locmail;
350 /* Kapil: Changed from MAIL_FILE to
351 current_mail_spool since the latter
352 is a copy of the former if undefined
353 but the latter should take precedence
355 strncpy(mbox, current_mail_spool, sizeof(mbox));
357 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
359 strncpy(mbox, arg, sizeof(mbox));
363 variable_substitute(mbox, dst, sizeof(dst));
365 locmail = malloc(sizeof(struct local_mail_s));
366 memset(locmail, 0, sizeof(struct local_mail_s));
367 locmail->mbox = strndup(dst, text_buffer_size);
368 locmail->interval = n1;
369 obj->data.opaque = locmail;
372 #define PRINT_MAILS_GENERATOR(x) \
373 void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
375 struct local_mail_s *locmail = obj->data.opaque; \
378 update_mail_count(locmail); \
379 snprintf(p, p_max_size, "%d", locmail->x##mail_count); \
382 PRINT_MAILS_GENERATOR()
383 PRINT_MAILS_GENERATOR(new_)
384 PRINT_MAILS_GENERATOR(seen_)
385 PRINT_MAILS_GENERATOR(unseen_)
386 PRINT_MAILS_GENERATOR(flagged_)
387 PRINT_MAILS_GENERATOR(unflagged_)
388 PRINT_MAILS_GENERATOR(forwarded_)
389 PRINT_MAILS_GENERATOR(unforwarded_)
390 PRINT_MAILS_GENERATOR(replied_)
391 PRINT_MAILS_GENERATOR(unreplied_)
392 PRINT_MAILS_GENERATOR(draft_)
393 PRINT_MAILS_GENERATOR(trashed_)
395 void free_local_mails(struct text_object *obj)
397 struct local_mail_s *locmail = obj->data.opaque;
404 free(obj->data.opaque);
405 obj->data.opaque = 0;
408 #define MAXDATASIZE 1000
410 struct mail_s *parse_mail_args(char type, const char *arg)
415 mail = malloc(sizeof(struct mail_s));
416 memset(mail, 0, sizeof(struct mail_s));
418 #define lenstr "%1023s"
419 if (sscanf(arg, lenstr " " lenstr " " lenstr, mail->host, mail->user, mail->pass)
421 if (type == POP3_TYPE) {
422 NORM_ERR("Scanning POP3 args failed");
423 } else if (type == IMAP_TYPE) {
424 NORM_ERR("Scanning IMAP args failed");
428 // see if password needs prompting
429 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
430 int fp = fileno(stdin);
433 tcgetattr(fp, &term);
434 term.c_lflag &= ~ECHO;
435 tcsetattr(fp, TCSANOW, &term);
436 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
437 scanf(lenstr, mail->pass);
440 term.c_lflag |= ECHO;
441 tcsetattr(fp, TCSANOW, &term);
443 // now we check for optional args
444 tmp = strstr(arg, "-r ");
447 sscanf(tmp, "%u", &mail->retries);
449 mail->retries = 5; // 5 retries after failure
451 tmp = strstr(arg, "-i ");
454 sscanf(tmp, "%f", &mail->interval);
456 mail->interval = 300; // 5 minutes
458 tmp = strstr(arg, "-p ");
461 sscanf(tmp, "%lu", &mail->port);
463 if (type == POP3_TYPE) {
464 mail->port = 110; // default pop3 port
465 } else if (type == IMAP_TYPE) {
466 mail->port = 143; // default imap port
469 if (type == IMAP_TYPE) {
470 tmp = strstr(arg, "-f ");
472 int len = MAXSIZE - 1;
474 if (tmp[0] == '\'') {
475 len = strstr(tmp + 1, "'") - tmp;
480 strncpy(mail->folder, tmp + 1, len - 1);
482 strncpy(mail->folder, "INBOX", MAXSIZE - 1); // default imap inbox
485 tmp = strstr(arg, "-e ");
487 int len = MAXSIZE - 1;
490 if (tmp[0] == '\'') {
491 len = strstr(tmp + 1, "'") - tmp;
496 strncpy(mail->command, tmp + 1, len - 1);
498 mail->command[0] = '\0';
500 DBGP("mail args parsed: folder: '%s' command: '%s' user: '%s' host: '%s'\n",
501 mail->folder, mail->command, mail->user, mail->host);
502 mail->p_timed_thread = NULL;
506 void parse_imap_mail_args(struct text_object *obj, const char *arg)
511 if (!global_mail && !rep) {
512 // something is wrong, warn once then stop
513 NORM_ERR("There's a problem with your mail settings. "
514 "Check that the global mail settings are properly defined"
515 " (line %li).", obj->line);
519 obj->data.opaque = global_mail;
524 obj->data.opaque = parse_mail_args(IMAP_TYPE, arg);
527 void parse_pop3_mail_args(struct text_object *obj, const char *arg)
532 if (!global_mail && !rep) {
533 // something is wrong, warn once then stop
534 NORM_ERR("There's a problem with your mail settings. "
535 "Check that the global mail settings are properly defined"
536 " (line %li).", obj->line);
540 obj->data.opaque = global_mail;
545 obj->data.opaque = parse_mail_args(POP3_TYPE, arg);
548 void parse_global_imap_mail_args(const char *value)
550 global_mail = parse_mail_args(IMAP_TYPE, value);
553 void parse_global_pop3_mail_args(const char *value)
555 global_mail = parse_mail_args(POP3_TYPE, value);
558 void free_mail_obj(struct text_object *obj)
560 if (!obj->data.opaque)
563 if (obj->data.opaque == global_mail) {
564 if (--global_mail_use == 0) {
569 free(obj->data.opaque);
570 obj->data.opaque = 0;
574 int imap_command(int sockfd, const char *command, char *response, const char *verify)
576 struct timeval fetchtimeout;
578 int res, numbytes = 0;
579 if (send(sockfd, command, strlen(command), 0) == -1) {
583 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
584 fetchtimeout.tv_usec = 0;
586 FD_SET(sockfd, &fdset);
587 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
589 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
594 DBGP2("imap_command() command: %s", command);
595 DBGP2("imap_command() received: %s", response);
596 response[numbytes] = '\0';
597 if (strstr(response, verify) == NULL) {
603 int imap_check_status(char *recvbuf, struct mail_s *mail)
606 reply = strstr(recvbuf, " (MESSAGES ");
607 if (!reply || strlen(reply) < 2) {
611 *strchr(reply, ')') = '\0';
613 NORM_ERR("Error parsing IMAP response: %s", recvbuf);
616 timed_thread_lock(mail->p_timed_thread);
617 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
619 timed_thread_unlock(mail->p_timed_thread);
624 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
626 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
627 || (mail->messages > old_messages && mail->unseen > 0))) {
629 if (system(mail->command) == -1) {
635 static void ensure_mail_thread(struct mail_s *mail,
636 void *thread(void *), const char *text)
638 if (mail->p_timed_thread)
641 mail->p_timed_thread = timed_thread_create(thread,
642 mail, mail->interval * 1000000);
643 if (!mail->p_timed_thread) {
644 NORM_ERR("Error creating %s timed thread", text);
646 timed_thread_register(mail->p_timed_thread,
647 &mail->p_timed_thread);
648 if (timed_thread_run(mail->p_timed_thread)) {
649 NORM_ERR("Error running %s timed thread", text);
653 static void *imap_thread(void *arg)
655 int sockfd, numbytes;
656 char recvbuf[MAXDATASIZE];
657 char sendbuf[MAXDATASIZE];
658 unsigned int fail = 0;
659 unsigned long old_unseen = ULONG_MAX;
660 unsigned long old_messages = ULONG_MAX;
661 struct stat stat_buf;
662 struct mail_s *mail = (struct mail_s *)arg;
664 int threadfd = timed_thread_readfd(mail->p_timed_thread);
665 char resolved_host = 0;
666 struct addrinfo hints;
667 struct addrinfo *ai = 0, *rp;
670 while (fail < mail->retries) {
671 struct timeval fetchtimeout;
676 NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
677 mail->user, mail->host, fail + 1, mail->retries);
678 resolved_host = 0; /* force us to resolve the hostname again */
680 if (!resolved_host) {
681 memset(&hints, 0, sizeof(struct addrinfo));
682 hints.ai_family = AF_UNSPEC;
683 hints.ai_socktype = SOCK_STREAM;
685 hints.ai_protocol = 0;
686 snprintf(portbuf, 8, "%lu", mail->port);
688 res = getaddrinfo(mail->host, portbuf, &hints, &ai);
690 NORM_ERR("IMAP getaddrinfo: %s", gai_strerror(res));
697 for (rp = ai; rp != NULL; rp = rp->ai_next) {
698 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
702 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
715 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
716 fetchtimeout.tv_usec = 0;
718 FD_SET(sockfd, &fdset);
719 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
721 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
727 NORM_ERR("IMAP connection failed: timeout");
731 recvbuf[numbytes] = '\0';
732 DBGP2("imap_thread() received: %s", recvbuf);
733 if (strstr(recvbuf, "* OK") != recvbuf) {
734 NORM_ERR("IMAP connection failed, probably not an IMAP server");
738 strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
739 if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
743 if (strstr(recvbuf, " IDLE ") != NULL) {
747 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
748 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
749 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
750 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
751 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
752 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
757 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
758 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
759 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
760 MAXDATASIZE - strlen(sendbuf) - 1);
761 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
766 if (imap_check_status(recvbuf, mail)) {
770 imap_unseen_command(mail, old_unseen, old_messages);
772 old_unseen = mail->unseen;
773 old_messages = mail->messages;
776 strncpy(sendbuf, "a4 SELECT \"", MAXDATASIZE);
777 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
778 strncat(sendbuf, "\"\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
779 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
784 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
785 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
793 * RFC 2177 says we have to re-idle every 29 minutes.
794 * We'll do it every 10 minutes to be safe.
796 fetchtimeout.tv_sec = 600;
797 fetchtimeout.tv_usec = 0;
800 FD_SET(sockfd, &fdset);
801 FD_SET(threadfd, &fdset);
802 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL,
803 NULL, &fetchtimeout);
805 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
806 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
807 /* if a valid socket, close it */
810 timed_thread_exit(mail->p_timed_thread);
811 } else if (res > 0) {
812 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
813 perror("recv idling");
818 recvbuf[numbytes] = '\0';
819 DBGP2("imap_thread() received: %s", recvbuf);
820 if (strlen(recvbuf) > 2) {
821 unsigned long messages, recent = 0;
823 char force_check = 0;
824 buf = strstr(buf, "EXISTS");
825 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
826 buf = strstr(buf + 1, "EXISTS");
829 // back up until we reach '*'
830 while (buf >= recvbuf && buf[0] != '*') {
833 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
834 timed_thread_lock(mail->p_timed_thread);
835 if (mail->messages != messages) {
838 timed_thread_unlock(mail->p_timed_thread);
842 buf = strstr(buf, "RECENT");
843 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
844 buf = strstr(buf + 1, "RECENT");
847 // back up until we reach '*'
848 while (buf >= recvbuf && buf[0] != '*') {
851 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
856 * check if we got a FETCH from server, recent was
857 * something other than 0, or we had a timeout
860 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
861 // re-check messages and unseen
862 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
866 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
867 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
868 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
869 MAXDATASIZE - strlen(sendbuf) - 1);
870 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
874 if (imap_check_status(recvbuf, mail)) {
878 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
879 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
885 * check if we got a BYE from server
888 if (buf && strstr(buf, "* BYE")) {
889 // need to re-connect
896 imap_unseen_command(mail, old_unseen, old_messages);
898 old_unseen = mail->unseen;
899 old_messages = mail->messages;
903 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
904 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
909 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
910 fetchtimeout.tv_usec = 0;
912 FD_SET(sockfd, &fdset);
913 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
915 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
921 recvbuf[numbytes] = '\0';
922 DBGP2("imap_thread() received: %s", recvbuf);
923 if (strstr(recvbuf, "a3 OK") == NULL) {
924 NORM_ERR("IMAP logout failed: %s", recvbuf);
930 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
931 /* if a valid socket, close it */
934 if (timed_thread_test(mail->p_timed_thread, 0)) {
935 timed_thread_exit(mail->p_timed_thread);
943 void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
945 struct mail_s *mail = obj->data.opaque;
950 ensure_mail_thread(mail, imap_thread, "imap");
952 if (mail && mail->p_timed_thread) {
953 timed_thread_lock(mail->p_timed_thread);
954 snprintf(p, p_max_size, "%lu", mail->unseen);
955 timed_thread_unlock(mail->p_timed_thread);
959 void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
961 struct mail_s *mail = obj->data.opaque;
966 ensure_mail_thread(mail, imap_thread, "imap");
968 if (mail && mail->p_timed_thread) {
969 timed_thread_lock(mail->p_timed_thread);
970 snprintf(p, p_max_size, "%lu", mail->messages);
971 timed_thread_unlock(mail->p_timed_thread);
975 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
977 struct timeval fetchtimeout;
979 int res, numbytes = 0;
980 if (send(sockfd, command, strlen(command), 0) == -1) {
984 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
985 fetchtimeout.tv_usec = 0;
987 FD_SET(sockfd, &fdset);
988 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
990 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
995 DBGP2("pop3_command() received: %s", response);
996 response[numbytes] = '\0';
997 if (strstr(response, verify) == NULL) {
1003 static void *pop3_thread(void *arg)
1005 int sockfd, numbytes;
1006 char recvbuf[MAXDATASIZE];
1007 char sendbuf[MAXDATASIZE];
1009 unsigned int fail = 0;
1010 unsigned long old_unseen = ULONG_MAX;
1011 struct stat stat_buf;
1012 struct mail_s *mail = (struct mail_s *)arg;
1013 char resolved_host = 0;
1014 struct addrinfo hints;
1015 struct addrinfo *ai = 0, *rp;
1018 while (fail < mail->retries) {
1019 struct timeval fetchtimeout;
1024 NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1025 mail->user, mail->host, fail + 1, mail->retries);
1026 resolved_host = 0; /* force us to resolve the hostname again */
1028 if (!resolved_host) {
1029 memset(&hints, 0, sizeof(struct addrinfo));
1030 hints.ai_family = AF_UNSPEC;
1031 hints.ai_socktype = SOCK_STREAM;
1033 hints.ai_protocol = 0;
1034 snprintf(portbuf, 8, "%lu", mail->port);
1036 res = getaddrinfo(mail->host, portbuf, &hints, &ai);
1038 NORM_ERR("POP3 getaddrinfo: %s", gai_strerror(res));
1045 for (rp = ai; rp != NULL; rp = rp->ai_next) {
1046 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1050 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
1063 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
1064 fetchtimeout.tv_usec = 0;
1066 FD_SET(sockfd, &fdset);
1067 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
1069 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1075 NORM_ERR("POP3 connection failed: timeout\n");
1079 DBGP2("pop3_thread received: %s", recvbuf);
1080 recvbuf[numbytes] = '\0';
1081 if (strstr(recvbuf, "+OK ") != recvbuf) {
1082 NORM_ERR("POP3 connection failed, probably not a POP3 server");
1086 strncpy(sendbuf, "USER ", MAXDATASIZE);
1087 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1088 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1089 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1094 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1095 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1096 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1097 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1098 NORM_ERR("POP3 server login failed: %s", recvbuf);
1103 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1104 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1105 perror("send STAT");
1110 // now we get the data
1111 reply = recvbuf + 4;
1112 if (reply == NULL) {
1113 NORM_ERR("Error parsing POP3 response: %s", recvbuf);
1117 timed_thread_lock(mail->p_timed_thread);
1118 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1119 timed_thread_unlock(mail->p_timed_thread);
1122 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1123 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
1124 NORM_ERR("POP3 logout failed: %s", recvbuf);
1129 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1131 if (system(mail->command) == -1) {
1136 old_unseen = mail->unseen;
1138 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1139 /* if a valid socket, close it */
1142 if (timed_thread_test(mail->p_timed_thread, 0)) {
1143 timed_thread_exit(mail->p_timed_thread);
1151 void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
1153 struct mail_s *mail = obj->data.opaque;
1158 ensure_mail_thread(mail, pop3_thread, "pop3");
1160 if (mail && mail->p_timed_thread) {
1161 timed_thread_lock(mail->p_timed_thread);
1162 snprintf(p, p_max_size, "%lu", mail->unseen);
1163 timed_thread_unlock(mail->p_timed_thread);
1167 void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
1169 struct mail_s *mail = obj->data.opaque;
1174 ensure_mail_thread(mail, pop3_thread, "pop3");
1176 if (mail && mail->p_timed_thread) {
1177 timed_thread_lock(mail->p_timed_thread);
1178 snprintf(p, p_max_size, "%.1f",
1179 mail->used / 1024.0 / 1024.0);
1180 timed_thread_unlock(mail->p_timed_thread);