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)
64 struct mail_s { // for imap and pop3
66 unsigned long messages;
78 timed_thread *p_timed_thread;
87 int unseen_mail_count;
88 int flagged_mail_count;
89 int unflagged_mail_count;
90 int forwarded_mail_count;
91 int unforwarded_mail_count;
92 int replied_mail_count;
93 int unreplied_mail_count;
95 int trashed_mail_count;
101 char *current_mail_spool;
103 static struct mail_s *global_mail;
104 static int global_mail_use = 0;
106 static void update_mail_count(struct local_mail_s *mail)
114 /* TODO: use that fine file modification notify on Linux 2.4 */
116 /* don't check mail so often (9.5s is minimum interval) */
117 if (current_update_time - mail->last_update < 9.5) {
120 mail->last_update = current_update_time;
123 if (stat(mail->mbox, &st)) {
127 NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
134 if (S_ISDIR(st.st_mode)) {
137 struct dirent *dirent;
140 mail->mail_count = mail->new_mail_count = 0;
141 mail->seen_mail_count = mail->unseen_mail_count = 0;
142 mail->flagged_mail_count = mail->unflagged_mail_count = 0;
143 mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
144 mail->replied_mail_count = mail->unreplied_mail_count = 0;
145 mail->draft_mail_count = mail->trashed_mail_count = 0;
146 dirname = (char *) malloc(sizeof(char) * (strlen(mail->mbox) + 5));
151 strcpy(dirname, mail->mbox);
152 strcat(dirname, "/");
153 /* checking the cur subdirectory */
154 strcat(dirname, "cur");
156 dir = opendir(dirname);
158 NORM_ERR("cannot open directory");
162 dirent = readdir(dir);
164 /* . and .. are skipped */
165 if (dirent->d_name[0] != '.') {
167 mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
173 strcpy(mailflags, strrchr(dirent->d_name, ','));
174 if (!strchr(mailflags, 'T')) { /* The message is not in the trash */
175 if (strchr(mailflags, 'S')) { /*The message has been seen */
176 mail->seen_mail_count++;
178 mail->unseen_mail_count++;
180 if (strchr(mailflags, 'F')) { /*The message was flagged */
181 mail->flagged_mail_count++;
183 mail->unflagged_mail_count++;
185 if (strchr(mailflags, 'P')) { /*The message was forwarded */
186 mail->forwarded_mail_count++;
188 mail->unforwarded_mail_count++;
190 if (strchr(mailflags, 'R')) { /*The message was replied */
191 mail->replied_mail_count++;
193 mail->unreplied_mail_count++;
195 if (strchr(mailflags, 'D')) { /*The message is a draft */
196 mail->draft_mail_count++;
199 mail->trashed_mail_count++;
203 dirent = readdir(dir);
207 dirname[strlen(dirname) - 3] = '\0';
208 strcat(dirname, "new");
210 dir = opendir(dirname);
212 NORM_ERR("cannot open directory");
216 dirent = readdir(dir);
218 /* . and .. are skipped */
219 if (dirent->d_name[0] != '.') {
220 mail->new_mail_count++;
222 mail->unseen_mail_count++; /* new messages cannot have been seen */
224 dirent = readdir(dir);
233 if (st.st_mtime != mail->last_mtime) {
234 /* yippee, modification time has changed, let's read mail count! */
237 int reading_status = 0;
239 /* could lock here but I don't think it's really worth it because
240 * this isn't going to write mail spool */
242 mail->new_mail_count = mail->mail_count = 0;
244 /* these flags are not supported for mbox */
245 mail->seen_mail_count = mail->unseen_mail_count = -1;
246 mail->flagged_mail_count = mail->unflagged_mail_count = -1;
247 mail->forwarded_mail_count = mail->unforwarded_mail_count = -1;
248 mail->replied_mail_count = mail->unreplied_mail_count = -1;
249 mail->draft_mail_count = mail->trashed_mail_count = -1;
251 fp = open_file(mail->mbox, &rep);
256 /* NOTE: adds mail as new if there isn't Status-field at all */
262 if (fgets(buf, 128, fp) == NULL) {
266 if (strncmp(buf, "From ", 5) == 0) {
267 /* ignore MAILER-DAEMON */
268 if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
272 if (reading_status == 1) {
273 mail->new_mail_count++;
279 if (reading_status == 1
280 && strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
281 int xms = strtol(buf + 17, NULL, 16);
282 /* check that mail isn't marked for deletion */
284 mail->trashed_mail_count++;
286 /* Don't check whether the trashed email is unread */
289 /* check that mail isn't already read */
290 if (!(xms & 0x0001)) {
291 mail->new_mail_count++;
295 /* check for an additional X-Status header */
299 if (reading_status == 1 && strncmp(buf, "Status:", 7) == 0) {
300 /* check that mail isn't already read */
301 if (strchr(buf + 7, 'R') == NULL) {
302 mail->new_mail_count++;
309 if (reading_status >= 1 && strncmp(buf, "X-Status:", 9) == 0) {
310 /* check that mail isn't marked for deletion */
311 if (strchr(buf + 9, 'D') != NULL) {
312 mail->trashed_mail_count++;
313 /* If the mail was previously detected as new,
314 subtract it from the new mail count */
316 mail->new_mail_count--;
325 while (strchr(buf, '\n') == NULL && !feof(fp)) {
332 if (reading_status) {
333 mail->new_mail_count++;
336 mail->last_mtime = st.st_mtime;
340 void parse_local_mail_args(struct text_object *obj, const char *arg)
343 char mbox[256], dst[256];
344 struct local_mail_s *locmail;
348 /* Kapil: Changed from MAIL_FILE to
349 current_mail_spool since the latter
350 is a copy of the former if undefined
351 but the latter should take precedence
353 strncpy(mbox, current_mail_spool, sizeof(mbox));
355 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
357 strncpy(mbox, arg, sizeof(mbox));
361 variable_substitute(mbox, dst, sizeof(dst));
363 locmail = malloc(sizeof(struct local_mail_s));
364 memset(locmail, 0, sizeof(struct local_mail_s));
365 locmail->mbox = strndup(dst, text_buffer_size);
366 locmail->interval = n1;
367 obj->data.opaque = locmail;
370 #define PRINT_MAILS_GENERATOR(x) \
371 void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
373 struct local_mail_s *locmail = obj->data.opaque; \
376 update_mail_count(locmail); \
377 snprintf(p, p_max_size, "%d", locmail->x##mail_count); \
380 PRINT_MAILS_GENERATOR()
381 PRINT_MAILS_GENERATOR(new_)
382 PRINT_MAILS_GENERATOR(seen_)
383 PRINT_MAILS_GENERATOR(unseen_)
384 PRINT_MAILS_GENERATOR(flagged_)
385 PRINT_MAILS_GENERATOR(unflagged_)
386 PRINT_MAILS_GENERATOR(forwarded_)
387 PRINT_MAILS_GENERATOR(unforwarded_)
388 PRINT_MAILS_GENERATOR(replied_)
389 PRINT_MAILS_GENERATOR(unreplied_)
390 PRINT_MAILS_GENERATOR(draft_)
391 PRINT_MAILS_GENERATOR(trashed_)
393 void free_local_mails(struct text_object *obj)
395 struct local_mail_s *locmail = obj->data.opaque;
402 free(obj->data.opaque);
403 obj->data.opaque = 0;
406 #define MAXDATASIZE 1000
408 struct mail_s *parse_mail_args(char type, const char *arg)
413 mail = malloc(sizeof(struct mail_s));
414 memset(mail, 0, sizeof(struct mail_s));
416 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
418 if (type == POP3_TYPE) {
419 NORM_ERR("Scanning POP3 args failed");
420 } else if (type == IMAP_TYPE) {
421 NORM_ERR("Scanning IMAP args failed");
425 // see if password needs prompting
426 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
427 int fp = fileno(stdin);
430 tcgetattr(fp, &term);
431 term.c_lflag &= ~ECHO;
432 tcsetattr(fp, TCSANOW, &term);
433 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
434 scanf("%128s", mail->pass);
436 term.c_lflag |= ECHO;
437 tcsetattr(fp, TCSANOW, &term);
439 // now we check for optional args
440 tmp = strstr(arg, "-r ");
443 sscanf(tmp, "%u", &mail->retries);
445 mail->retries = 5; // 5 retries after failure
447 tmp = strstr(arg, "-i ");
450 sscanf(tmp, "%f", &mail->interval);
452 mail->interval = 300; // 5 minutes
454 tmp = strstr(arg, "-p ");
457 sscanf(tmp, "%lu", &mail->port);
459 if (type == POP3_TYPE) {
460 mail->port = 110; // default pop3 port
461 } else if (type == IMAP_TYPE) {
462 mail->port = 143; // default imap port
465 if (type == IMAP_TYPE) {
466 tmp = strstr(arg, "-f ");
470 if (tmp[0] == '\'') {
471 len = strstr(tmp + 1, "'") - tmp - 1;
476 strncpy(mail->folder, tmp + 1, len);
478 strncpy(mail->folder, "INBOX", 128); // default imap inbox
481 tmp = strstr(arg, "-e ");
486 if (tmp[0] == '\'') {
487 len = strstr(tmp + 1, "'") - tmp - 1;
492 strncpy(mail->command, tmp + 1, len);
494 mail->command[0] = '\0';
496 mail->p_timed_thread = NULL;
500 void parse_imap_mail_args(struct text_object *obj, const char *arg)
505 if (!global_mail && !rep) {
506 // something is wrong, warn once then stop
507 NORM_ERR("There's a problem with your mail settings. "
508 "Check that the global mail settings are properly defined"
509 " (line %li).", obj->line);
513 obj->data.opaque = global_mail;
518 obj->data.opaque = parse_mail_args(IMAP_TYPE, arg);
521 void parse_pop3_mail_args(struct text_object *obj, const char *arg)
526 if (!global_mail && !rep) {
527 // something is wrong, warn once then stop
528 NORM_ERR("There's a problem with your mail settings. "
529 "Check that the global mail settings are properly defined"
530 " (line %li).", obj->line);
534 obj->data.opaque = global_mail;
539 obj->data.opaque = parse_mail_args(POP3_TYPE, arg);
542 void parse_global_imap_mail_args(const char *value)
544 global_mail = parse_mail_args(IMAP_TYPE, value);
547 void parse_global_pop3_mail_args(const char *value)
549 global_mail = parse_mail_args(POP3_TYPE, value);
552 void free_mail_obj(struct text_object *obj)
554 if (!obj->data.opaque)
557 if (obj->data.opaque == global_mail) {
558 if (--global_mail_use == 0) {
563 free(obj->data.opaque);
564 obj->data.opaque = 0;
568 int imap_command(int sockfd, const char *command, char *response, const char *verify)
570 struct timeval fetchtimeout;
572 int res, numbytes = 0;
573 if (send(sockfd, command, strlen(command), 0) == -1) {
577 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
578 fetchtimeout.tv_usec = 0;
580 FD_SET(sockfd, &fdset);
581 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
583 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
588 DBGP2("imap_command() command: %s", command);
589 DBGP2("imap_command() received: %s", response);
590 response[numbytes] = '\0';
591 if (strstr(response, verify) == NULL) {
597 int imap_check_status(char *recvbuf, struct mail_s *mail)
600 reply = strstr(recvbuf, " (MESSAGES ");
601 if (!reply || strlen(reply) < 2) {
605 *strchr(reply, ')') = '\0';
607 NORM_ERR("Error parsing IMAP response: %s", recvbuf);
610 timed_thread_lock(mail->p_timed_thread);
611 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
613 timed_thread_unlock(mail->p_timed_thread);
618 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
620 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
621 || (mail->messages > old_messages && mail->unseen > 0))) {
623 if (system(mail->command) == -1) {
629 static void ensure_mail_thread(struct mail_s *mail,
630 void *thread(void *), const char *text)
632 if (mail->p_timed_thread)
635 mail->p_timed_thread = timed_thread_create(thread,
636 mail, mail->interval * 1000000);
637 if (!mail->p_timed_thread) {
638 NORM_ERR("Error creating %s timed thread", text);
640 timed_thread_register(mail->p_timed_thread,
641 &mail->p_timed_thread);
642 if (timed_thread_run(mail->p_timed_thread)) {
643 NORM_ERR("Error running %s timed thread", text);
647 static void *imap_thread(void *arg)
649 int sockfd, numbytes;
650 char recvbuf[MAXDATASIZE];
651 char sendbuf[MAXDATASIZE];
652 unsigned int fail = 0;
653 unsigned long old_unseen = ULONG_MAX;
654 unsigned long old_messages = ULONG_MAX;
655 struct stat stat_buf;
656 struct hostent he, *he_res = 0;
659 struct sockaddr_in their_addr; // connector's address information
660 struct mail_s *mail = (struct mail_s *)arg;
662 int threadfd = timed_thread_readfd(mail->p_timed_thread);
663 char resolved_host = 0;
665 while (fail < mail->retries) {
666 struct timeval fetchtimeout;
670 if (!resolved_host) {
671 #ifdef HAVE_GETHOSTBYNAME_R
672 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
673 NORM_ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
677 #else /* HAVE_GETHOSTBYNAME_R */
678 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
679 herror("gethostbyname");
683 #endif /* HAVE_GETHOSTBYNAME_R */
687 NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
688 mail->user, mail->host, fail + 1, mail->retries);
691 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
698 their_addr.sin_family = AF_INET;
699 // short, network byte order
700 their_addr.sin_port = htons(mail->port);
701 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
702 // zero the rest of the struct
703 memset(&(their_addr.sin_zero), '\0', 8);
705 if (connect(sockfd, (struct sockaddr *) &their_addr,
706 sizeof(struct sockaddr)) == -1) {
712 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
713 fetchtimeout.tv_usec = 0;
715 FD_SET(sockfd, &fdset);
716 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
718 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
724 NORM_ERR("IMAP connection failed: timeout");
728 recvbuf[numbytes] = '\0';
729 DBGP2("imap_thread() received: %s", recvbuf);
730 if (strstr(recvbuf, "* OK") != recvbuf) {
731 NORM_ERR("IMAP connection failed, probably not an IMAP server");
735 strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
736 if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
740 if (strstr(recvbuf, " IDLE ") != NULL) {
744 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
745 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
746 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
747 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
748 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
749 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
754 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
755 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
756 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
757 MAXDATASIZE - strlen(sendbuf) - 1);
758 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
763 if (imap_check_status(recvbuf, mail)) {
767 imap_unseen_command(mail, old_unseen, old_messages);
769 old_unseen = mail->unseen;
770 old_messages = mail->messages;
773 strncpy(sendbuf, "a4 SELECT \"", MAXDATASIZE);
774 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
775 strncat(sendbuf, "\"\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
776 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
781 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
782 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
790 * RFC 2177 says we have to re-idle every 29 minutes.
791 * We'll do it every 10 minutes to be safe.
793 fetchtimeout.tv_sec = 600;
794 fetchtimeout.tv_usec = 0;
797 FD_SET(sockfd, &fdset);
798 FD_SET(threadfd, &fdset);
799 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, &fetchtimeout);
800 if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
801 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
802 /* if a valid socket, close it */
805 timed_thread_exit(mail->p_timed_thread);
806 } else if (res > 0) {
807 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
808 perror("recv idling");
812 } else if (fetchtimeout.tv_sec > 0) {
816 recvbuf[numbytes] = '\0';
817 DBGP2("imap_thread() received: %s", recvbuf);
818 if (strlen(recvbuf) > 2) {
819 unsigned long messages, recent = 0;
821 char force_check = 0;
822 buf = strstr(buf, "EXISTS");
823 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
824 buf = strstr(buf + 1, "EXISTS");
827 // back up until we reach '*'
828 while (buf >= recvbuf && buf[0] != '*') {
831 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
832 timed_thread_lock(mail->p_timed_thread);
833 if (mail->messages != messages) {
836 timed_thread_unlock(mail->p_timed_thread);
840 buf = strstr(buf, "RECENT");
841 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
842 buf = strstr(buf + 1, "RECENT");
845 // back up until we reach '*'
846 while (buf >= recvbuf && buf[0] != '*') {
849 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
854 * check if we got a FETCH from server, recent was
855 * something other than 0, or we had a timeout
858 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
859 // re-check messages and unseen
860 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
864 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
865 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
866 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
867 MAXDATASIZE - strlen(sendbuf) - 1);
868 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
872 if (imap_check_status(recvbuf, mail)) {
876 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
877 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
883 * check if we got a BYE from server
886 if (buf && strstr(buf, "* BYE")) {
887 // need to re-connect
894 imap_unseen_command(mail, old_unseen, old_messages);
896 old_unseen = mail->unseen;
897 old_messages = mail->messages;
901 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
902 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
907 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
908 fetchtimeout.tv_usec = 0;
910 FD_SET(sockfd, &fdset);
911 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
913 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
919 recvbuf[numbytes] = '\0';
920 DBGP2("imap_thread() received: %s", recvbuf);
921 if (strstr(recvbuf, "a3 OK") == NULL) {
922 NORM_ERR("IMAP logout failed: %s", recvbuf);
928 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
929 /* if a valid socket, close it */
932 if (timed_thread_test(mail->p_timed_thread, 0)) {
933 timed_thread_exit(mail->p_timed_thread);
941 void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
943 struct mail_s *mail = obj->data.opaque;
948 ensure_mail_thread(mail, imap_thread, "imap");
950 if (mail && mail->p_timed_thread) {
951 timed_thread_lock(mail->p_timed_thread);
952 snprintf(p, p_max_size, "%lu", mail->unseen);
953 timed_thread_unlock(mail->p_timed_thread);
957 void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
959 struct mail_s *mail = obj->data.opaque;
964 ensure_mail_thread(mail, imap_thread, "imap");
966 if (mail && mail->p_timed_thread) {
967 timed_thread_lock(mail->p_timed_thread);
968 snprintf(p, p_max_size, "%lu", mail->messages);
969 timed_thread_unlock(mail->p_timed_thread);
973 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
975 struct timeval fetchtimeout;
977 int res, numbytes = 0;
978 if (send(sockfd, command, strlen(command), 0) == -1) {
982 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
983 fetchtimeout.tv_usec = 0;
985 FD_SET(sockfd, &fdset);
986 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
988 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
993 DBGP2("pop3_command() received: %s", response);
994 response[numbytes] = '\0';
995 if (strstr(response, verify) == NULL) {
1001 static void *pop3_thread(void *arg)
1003 int sockfd, numbytes;
1004 char recvbuf[MAXDATASIZE];
1005 char sendbuf[MAXDATASIZE];
1007 unsigned int fail = 0;
1008 unsigned long old_unseen = ULONG_MAX;
1009 struct stat stat_buf;
1010 struct hostent he, *he_res = 0;
1012 char hostbuff[2048];
1013 struct sockaddr_in their_addr; // connector's address information
1014 struct mail_s *mail = (struct mail_s *)arg;
1015 char resolved_host = 0;
1017 while (fail < mail->retries) {
1018 struct timeval fetchtimeout;
1021 if (!resolved_host) {
1022 #ifdef HAVE_GETHOSTBYNAME_R
1023 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1024 NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1028 #else /* HAVE_GETHOSTBYNAME_R */
1029 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1030 herror("gethostbyname");
1034 #endif /* HAVE_GETHOSTBYNAME_R */
1038 NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1039 mail->user, mail->host, fail + 1, mail->retries);
1042 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1049 their_addr.sin_family = AF_INET;
1050 // short, network byte order
1051 their_addr.sin_port = htons(mail->port);
1052 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1053 // zero the rest of the struct
1054 memset(&(their_addr.sin_zero), '\0', 8);
1056 if (connect(sockfd, (struct sockaddr *) &their_addr,
1057 sizeof(struct sockaddr)) == -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);