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 */
679 sleep(fail); /* sleep more for the more failures we have */
681 if (!resolved_host) {
682 memset(&hints, 0, sizeof(struct addrinfo));
683 hints.ai_family = AF_UNSPEC;
684 hints.ai_socktype = SOCK_STREAM;
686 hints.ai_protocol = 0;
687 snprintf(portbuf, 8, "%lu", mail->port);
689 res = getaddrinfo(mail->host, portbuf, &hints, &ai);
691 NORM_ERR("IMAP getaddrinfo: %s", gai_strerror(res));
698 for (rp = ai; rp != NULL; rp = rp->ai_next) {
699 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
703 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
716 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
717 fetchtimeout.tv_usec = 0;
719 FD_SET(sockfd, &fdset);
720 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
722 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
728 NORM_ERR("IMAP connection failed: timeout");
732 recvbuf[numbytes] = '\0';
733 DBGP2("imap_thread() received: %s", recvbuf);
734 if (strstr(recvbuf, "* OK") != recvbuf) {
735 NORM_ERR("IMAP connection failed, probably not an IMAP server");
739 strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
740 if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
744 if (strstr(recvbuf, " IDLE ") != NULL) {
748 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
749 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
750 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
751 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
752 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
753 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
758 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
759 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
760 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
761 MAXDATASIZE - strlen(sendbuf) - 1);
762 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
767 if (imap_check_status(recvbuf, mail)) {
771 imap_unseen_command(mail, old_unseen, old_messages);
773 old_unseen = mail->unseen;
774 old_messages = mail->messages;
777 strncpy(sendbuf, "a4 SELECT \"", MAXDATASIZE);
778 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
779 strncat(sendbuf, "\"\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
780 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
785 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
786 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
794 * RFC 2177 says we have to re-idle every 29 minutes.
795 * We'll do it every 10 minutes to be safe.
797 fetchtimeout.tv_sec = 600;
798 fetchtimeout.tv_usec = 0;
801 FD_SET(sockfd, &fdset);
802 FD_SET(threadfd, &fdset);
803 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL,
804 NULL, &fetchtimeout);
806 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
807 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
808 /* if a valid socket, close it */
811 timed_thread_exit(mail->p_timed_thread);
812 } else if (res > 0) {
813 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
814 perror("recv idling");
819 recvbuf[numbytes] = '\0';
820 DBGP2("imap_thread() received: %s", recvbuf);
821 if (strlen(recvbuf) > 2) {
822 unsigned long messages, recent = 0;
824 char force_check = 0;
825 buf = strstr(buf, "EXISTS");
826 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
827 buf = strstr(buf + 1, "EXISTS");
830 // back up until we reach '*'
831 while (buf >= recvbuf && buf[0] != '*') {
834 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
835 timed_thread_lock(mail->p_timed_thread);
836 if (mail->messages != messages) {
839 timed_thread_unlock(mail->p_timed_thread);
843 buf = strstr(buf, "RECENT");
844 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
845 buf = strstr(buf + 1, "RECENT");
848 // back up until we reach '*'
849 while (buf >= recvbuf && buf[0] != '*') {
852 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
857 * check if we got a FETCH from server, recent was
858 * something other than 0, or we had a timeout
861 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
862 // re-check messages and unseen
863 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
867 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
868 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
869 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
870 MAXDATASIZE - strlen(sendbuf) - 1);
871 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
875 if (imap_check_status(recvbuf, mail)) {
879 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
880 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
886 * check if we got a BYE from server
889 if (buf && strstr(buf, "* BYE")) {
890 // need to re-connect
897 imap_unseen_command(mail, old_unseen, old_messages);
899 old_unseen = mail->unseen;
900 old_messages = mail->messages;
904 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
905 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
910 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
911 fetchtimeout.tv_usec = 0;
913 FD_SET(sockfd, &fdset);
914 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
916 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
922 recvbuf[numbytes] = '\0';
923 DBGP2("imap_thread() received: %s", recvbuf);
924 if (strstr(recvbuf, "a3 OK") == NULL) {
925 NORM_ERR("IMAP logout failed: %s", recvbuf);
931 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
932 /* if a valid socket, close it */
935 if (timed_thread_test(mail->p_timed_thread, 0)) {
936 timed_thread_exit(mail->p_timed_thread);
944 void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
946 struct mail_s *mail = obj->data.opaque;
951 ensure_mail_thread(mail, imap_thread, "imap");
953 if (mail && mail->p_timed_thread) {
954 timed_thread_lock(mail->p_timed_thread);
955 snprintf(p, p_max_size, "%lu", mail->unseen);
956 timed_thread_unlock(mail->p_timed_thread);
960 void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
962 struct mail_s *mail = obj->data.opaque;
967 ensure_mail_thread(mail, imap_thread, "imap");
969 if (mail && mail->p_timed_thread) {
970 timed_thread_lock(mail->p_timed_thread);
971 snprintf(p, p_max_size, "%lu", mail->messages);
972 timed_thread_unlock(mail->p_timed_thread);
976 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
978 struct timeval fetchtimeout;
980 int res, numbytes = 0;
981 if (send(sockfd, command, strlen(command), 0) == -1) {
985 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
986 fetchtimeout.tv_usec = 0;
988 FD_SET(sockfd, &fdset);
989 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
991 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
996 DBGP2("pop3_command() received: %s", response);
997 response[numbytes] = '\0';
998 if (strstr(response, verify) == NULL) {
1004 static void *pop3_thread(void *arg)
1006 int sockfd, numbytes;
1007 char recvbuf[MAXDATASIZE];
1008 char sendbuf[MAXDATASIZE];
1010 unsigned int fail = 0;
1011 unsigned long old_unseen = ULONG_MAX;
1012 struct stat stat_buf;
1013 struct mail_s *mail = (struct mail_s *)arg;
1014 char resolved_host = 0;
1015 struct addrinfo hints;
1016 struct addrinfo *ai = 0, *rp;
1019 while (fail < mail->retries) {
1020 struct timeval fetchtimeout;
1025 NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1026 mail->user, mail->host, fail + 1, mail->retries);
1027 resolved_host = 0; /* force us to resolve the hostname again */
1028 sleep(fail); /* sleep more for the more failures we have */
1030 if (!resolved_host) {
1031 memset(&hints, 0, sizeof(struct addrinfo));
1032 hints.ai_family = AF_UNSPEC;
1033 hints.ai_socktype = SOCK_STREAM;
1035 hints.ai_protocol = 0;
1036 snprintf(portbuf, 8, "%lu", mail->port);
1038 res = getaddrinfo(mail->host, portbuf, &hints, &ai);
1040 NORM_ERR("POP3 getaddrinfo: %s", gai_strerror(res));
1047 for (rp = ai; rp != NULL; rp = rp->ai_next) {
1048 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1052 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
1065 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
1066 fetchtimeout.tv_usec = 0;
1068 FD_SET(sockfd, &fdset);
1069 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
1071 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1077 NORM_ERR("POP3 connection failed: timeout\n");
1081 DBGP2("pop3_thread received: %s", recvbuf);
1082 recvbuf[numbytes] = '\0';
1083 if (strstr(recvbuf, "+OK ") != recvbuf) {
1084 NORM_ERR("POP3 connection failed, probably not a POP3 server");
1088 strncpy(sendbuf, "USER ", MAXDATASIZE);
1089 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1090 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1091 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1096 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1097 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1098 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1099 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1100 NORM_ERR("POP3 server login failed: %s", recvbuf);
1105 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1106 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1107 perror("send STAT");
1112 // now we get the data
1113 reply = recvbuf + 4;
1114 if (reply == NULL) {
1115 NORM_ERR("Error parsing POP3 response: %s", recvbuf);
1119 timed_thread_lock(mail->p_timed_thread);
1120 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1121 timed_thread_unlock(mail->p_timed_thread);
1124 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1125 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
1126 NORM_ERR("POP3 logout failed: %s", recvbuf);
1131 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1133 if (system(mail->command) == -1) {
1138 old_unseen = mail->unseen;
1140 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1141 /* if a valid socket, close it */
1144 if (timed_thread_test(mail->p_timed_thread, 0)) {
1145 timed_thread_exit(mail->p_timed_thread);
1153 void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
1155 struct mail_s *mail = obj->data.opaque;
1160 ensure_mail_thread(mail, pop3_thread, "pop3");
1162 if (mail && mail->p_timed_thread) {
1163 timed_thread_lock(mail->p_timed_thread);
1164 snprintf(p, p_max_size, "%lu", mail->unseen);
1165 timed_thread_unlock(mail->p_timed_thread);
1169 void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
1171 struct mail_s *mail = obj->data.opaque;
1176 ensure_mail_thread(mail, pop3_thread, "pop3");
1178 if (mail && mail->p_timed_thread) {
1179 timed_thread_lock(mail->p_timed_thread);
1180 snprintf(p, p_max_size, "%.1f",
1181 mail->used / 1024.0 / 1024.0);
1182 timed_thread_unlock(mail->p_timed_thread);