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 #define MAXFOLDERSIZE 128
66 struct mail_s { // for imap and pop3
68 unsigned long messages;
79 char folder[MAXFOLDERSIZE];
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 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
420 if (type == POP3_TYPE) {
421 NORM_ERR("Scanning POP3 args failed");
422 } else if (type == IMAP_TYPE) {
423 NORM_ERR("Scanning IMAP args failed");
427 // see if password needs prompting
428 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
429 int fp = fileno(stdin);
432 tcgetattr(fp, &term);
433 term.c_lflag &= ~ECHO;
434 tcsetattr(fp, TCSANOW, &term);
435 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
436 scanf("%128s", mail->pass);
438 term.c_lflag |= ECHO;
439 tcsetattr(fp, TCSANOW, &term);
441 // now we check for optional args
442 tmp = strstr(arg, "-r ");
445 sscanf(tmp, "%u", &mail->retries);
447 mail->retries = 5; // 5 retries after failure
449 tmp = strstr(arg, "-i ");
452 sscanf(tmp, "%f", &mail->interval);
454 mail->interval = 300; // 5 minutes
456 tmp = strstr(arg, "-p ");
459 sscanf(tmp, "%lu", &mail->port);
461 if (type == POP3_TYPE) {
462 mail->port = 110; // default pop3 port
463 } else if (type == IMAP_TYPE) {
464 mail->port = 143; // default imap port
467 if (type == IMAP_TYPE) {
468 tmp = strstr(arg, "-f ");
470 int len = MAXFOLDERSIZE-1;
472 if (tmp[0] == '\'') {
473 len = strstr(tmp + 1, "'") - tmp - 1;
474 if (len > MAXFOLDERSIZE-1) {
475 len = MAXFOLDERSIZE-1;
479 strncpy(mail->folder, tmp, len);
480 mail->folder[len-1] = 0;
482 strncpy(mail->folder, "INBOX", MAXFOLDERSIZE-1); // default imap inbox
483 mail->folder[MAXFOLDERSIZE-1] = 0;
486 tmp = strstr(arg, "-e ");
491 if (tmp[0] == '\'') {
492 len = strstr(tmp + 1, "'") - tmp - 1;
497 strncpy(mail->command, tmp + 1, len);
499 mail->command[0] = '\0';
501 mail->p_timed_thread = NULL;
505 void parse_imap_mail_args(struct text_object *obj, const char *arg)
510 if (!global_mail && !rep) {
511 // something is wrong, warn once then stop
512 NORM_ERR("There's a problem with your mail settings. "
513 "Check that the global mail settings are properly defined"
514 " (line %li).", obj->line);
518 obj->data.opaque = global_mail;
523 obj->data.opaque = parse_mail_args(IMAP_TYPE, arg);
526 void parse_pop3_mail_args(struct text_object *obj, const char *arg)
531 if (!global_mail && !rep) {
532 // something is wrong, warn once then stop
533 NORM_ERR("There's a problem with your mail settings. "
534 "Check that the global mail settings are properly defined"
535 " (line %li).", obj->line);
539 obj->data.opaque = global_mail;
544 obj->data.opaque = parse_mail_args(POP3_TYPE, arg);
547 void parse_global_imap_mail_args(const char *value)
549 global_mail = parse_mail_args(IMAP_TYPE, value);
552 void parse_global_pop3_mail_args(const char *value)
554 global_mail = parse_mail_args(POP3_TYPE, value);
557 void free_mail_obj(struct text_object *obj)
559 if (!obj->data.opaque)
562 if (obj->data.opaque == global_mail) {
563 if (--global_mail_use == 0) {
568 free(obj->data.opaque);
569 obj->data.opaque = 0;
573 int imap_command(int sockfd, const char *command, char *response, const char *verify)
575 struct timeval fetchtimeout;
577 int res, numbytes = 0;
578 if (send(sockfd, command, strlen(command), 0) == -1) {
582 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
583 fetchtimeout.tv_usec = 0;
585 FD_SET(sockfd, &fdset);
586 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
588 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
593 DBGP2("imap_command() command: %s", command);
594 DBGP2("imap_command() received: %s", response);
595 response[numbytes] = '\0';
596 if (strstr(response, verify) == NULL) {
602 int imap_check_status(char *recvbuf, struct mail_s *mail)
605 reply = strstr(recvbuf, " (MESSAGES ");
606 if (!reply || strlen(reply) < 2) {
610 *strchr(reply, ')') = '\0';
612 NORM_ERR("Error parsing IMAP response: %s", recvbuf);
615 timed_thread_lock(mail->p_timed_thread);
616 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
618 timed_thread_unlock(mail->p_timed_thread);
623 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
625 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
626 || (mail->messages > old_messages && mail->unseen > 0))) {
628 if (system(mail->command) == -1) {
634 static void ensure_mail_thread(struct mail_s *mail,
635 void *thread(void *), const char *text)
637 if (mail->p_timed_thread)
640 mail->p_timed_thread = timed_thread_create(thread,
641 mail, mail->interval * 1000000);
642 if (!mail->p_timed_thread) {
643 NORM_ERR("Error creating %s timed thread", text);
645 timed_thread_register(mail->p_timed_thread,
646 &mail->p_timed_thread);
647 if (timed_thread_run(mail->p_timed_thread)) {
648 NORM_ERR("Error running %s timed thread", text);
652 static void *imap_thread(void *arg)
654 int sockfd, numbytes;
655 char recvbuf[MAXDATASIZE];
656 char sendbuf[MAXDATASIZE];
657 unsigned int fail = 0;
658 unsigned long old_unseen = ULONG_MAX;
659 unsigned long old_messages = ULONG_MAX;
660 struct stat stat_buf;
661 struct hostent he, *he_res = 0;
664 struct sockaddr_in their_addr; // connector's address information
665 struct mail_s *mail = (struct mail_s *)arg;
667 int threadfd = timed_thread_readfd(mail->p_timed_thread);
668 char resolved_host = 0;
670 while (fail < mail->retries) {
671 struct timeval fetchtimeout;
675 if (!resolved_host) {
676 #ifdef HAVE_GETHOSTBYNAME_R
677 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
678 NORM_ERR("IMAP 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");
688 #endif /* HAVE_GETHOSTBYNAME_R */
692 NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
693 mail->user, mail->host, fail + 1, mail->retries);
696 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
703 their_addr.sin_family = AF_INET;
704 // short, network byte order
705 their_addr.sin_port = htons(mail->port);
706 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
707 // zero the rest of the struct
708 memset(&(their_addr.sin_zero), '\0', 8);
710 if (connect(sockfd, (struct sockaddr *) &their_addr,
711 sizeof(struct sockaddr)) == -1) {
717 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
718 fetchtimeout.tv_usec = 0;
720 FD_SET(sockfd, &fdset);
721 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
723 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
729 NORM_ERR("IMAP connection failed: timeout");
733 recvbuf[numbytes] = '\0';
734 DBGP2("imap_thread() received: %s", recvbuf);
735 if (strstr(recvbuf, "* OK") != recvbuf) {
736 NORM_ERR("IMAP connection failed, probably not an IMAP server");
740 strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
741 if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
745 if (strstr(recvbuf, " IDLE ") != NULL) {
749 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
750 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
751 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
752 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
753 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
754 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
759 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
760 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
761 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
762 MAXDATASIZE - strlen(sendbuf) - 1);
763 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
768 if (imap_check_status(recvbuf, mail)) {
772 imap_unseen_command(mail, old_unseen, old_messages);
774 old_unseen = mail->unseen;
775 old_messages = mail->messages;
778 strncpy(sendbuf, "a4 SELECT \"", MAXDATASIZE);
779 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
780 strncat(sendbuf, "\"\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
781 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
786 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
787 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
795 * RFC 2177 says we have to re-idle every 29 minutes.
796 * We'll do it every 20 minutes to be safe.
798 fetchtimeout.tv_sec = 1200;
799 fetchtimeout.tv_usec = 0;
802 FD_SET(sockfd, &fdset);
803 FD_SET(threadfd, &fdset);
804 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, 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");
821 recvbuf[numbytes] = '\0';
822 DBGP2("imap_thread() received: %s", recvbuf);
823 if (strlen(recvbuf) > 2) {
824 unsigned long messages, recent = 0;
826 char force_check = 0;
827 buf = strstr(buf, "EXISTS");
828 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
829 buf = strstr(buf + 1, "EXISTS");
832 // back up until we reach '*'
833 while (buf >= recvbuf && buf[0] != '*') {
836 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
837 timed_thread_lock(mail->p_timed_thread);
838 if (mail->messages != messages) {
840 mail->messages = messages;
842 timed_thread_unlock(mail->p_timed_thread);
846 buf = strstr(buf, "RECENT");
847 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
848 buf = strstr(buf + 1, "RECENT");
851 // back up until we reach '*'
852 while (buf >= recvbuf && buf[0] != '*') {
855 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
860 * check if we got a FETCH from server, recent was
861 * something other than 0, or we had a timeout
864 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
865 // re-check messages and unseen
866 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
870 strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
871 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
872 strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
873 MAXDATASIZE - strlen(sendbuf) - 1);
874 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
878 if (imap_check_status(recvbuf, mail)) {
882 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
883 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
889 * check if we got a BYE from server
892 if (buf && strstr(buf, "* BYE")) {
893 // need to re-connect
900 imap_unseen_command(mail, old_unseen, old_messages);
902 old_unseen = mail->unseen;
903 old_messages = mail->messages;
907 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
908 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
913 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
914 fetchtimeout.tv_usec = 0;
916 FD_SET(sockfd, &fdset);
917 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
919 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
925 recvbuf[numbytes] = '\0';
926 DBGP2("imap_thread() received: %s", recvbuf);
927 if (strstr(recvbuf, "a3 OK") == NULL) {
928 NORM_ERR("IMAP logout failed: %s", recvbuf);
934 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
935 /* if a valid socket, close it */
938 if (timed_thread_test(mail->p_timed_thread, 0)) {
939 timed_thread_exit(mail->p_timed_thread);
947 void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
949 struct mail_s *mail = obj->data.opaque;
954 ensure_mail_thread(mail, imap_thread, "imap");
956 if (mail && mail->p_timed_thread) {
957 timed_thread_lock(mail->p_timed_thread);
958 snprintf(p, p_max_size, "%lu", mail->unseen);
959 timed_thread_unlock(mail->p_timed_thread);
963 void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
965 struct mail_s *mail = obj->data.opaque;
970 ensure_mail_thread(mail, imap_thread, "imap");
972 if (mail && mail->p_timed_thread) {
973 timed_thread_lock(mail->p_timed_thread);
974 snprintf(p, p_max_size, "%lu", mail->messages);
975 timed_thread_unlock(mail->p_timed_thread);
979 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
981 struct timeval fetchtimeout;
983 int res, numbytes = 0;
984 if (send(sockfd, command, strlen(command), 0) == -1) {
988 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
989 fetchtimeout.tv_usec = 0;
991 FD_SET(sockfd, &fdset);
992 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
994 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
999 DBGP2("pop3_command() received: %s", response);
1000 response[numbytes] = '\0';
1001 if (strstr(response, verify) == NULL) {
1007 static void *pop3_thread(void *arg)
1009 int sockfd, numbytes;
1010 char recvbuf[MAXDATASIZE];
1011 char sendbuf[MAXDATASIZE];
1013 unsigned int fail = 0;
1014 unsigned long old_unseen = ULONG_MAX;
1015 struct stat stat_buf;
1016 struct hostent he, *he_res = 0;
1018 char hostbuff[2048];
1019 struct sockaddr_in their_addr; // connector's address information
1020 struct mail_s *mail = (struct mail_s *)arg;
1021 char resolved_host = 0;
1023 while (fail < mail->retries) {
1024 struct timeval fetchtimeout;
1027 if (!resolved_host) {
1028 #ifdef HAVE_GETHOSTBYNAME_R
1029 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1030 NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1034 #else /* HAVE_GETHOSTBYNAME_R */
1035 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1036 herror("gethostbyname");
1040 #endif /* HAVE_GETHOSTBYNAME_R */
1044 NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1045 mail->user, mail->host, fail + 1, mail->retries);
1048 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1055 their_addr.sin_family = AF_INET;
1056 // short, network byte order
1057 their_addr.sin_port = htons(mail->port);
1058 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1059 // zero the rest of the struct
1060 memset(&(their_addr.sin_zero), '\0', 8);
1062 if (connect(sockfd, (struct sockaddr *) &their_addr,
1063 sizeof(struct sockaddr)) == -1) {
1069 fetchtimeout.tv_sec = 60; // 60 second timeout i guess
1070 fetchtimeout.tv_usec = 0;
1072 FD_SET(sockfd, &fdset);
1073 res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
1075 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1081 NORM_ERR("POP3 connection failed: timeout\n");
1085 DBGP2("pop3_thread received: %s", recvbuf);
1086 recvbuf[numbytes] = '\0';
1087 if (strstr(recvbuf, "+OK ") != recvbuf) {
1088 NORM_ERR("POP3 connection failed, probably not a POP3 server");
1092 strncpy(sendbuf, "USER ", MAXDATASIZE);
1093 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1094 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1095 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1100 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1101 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1102 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1103 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1104 NORM_ERR("POP3 server login failed: %s", recvbuf);
1109 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1110 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
1111 perror("send STAT");
1116 // now we get the data
1117 reply = recvbuf + 4;
1118 if (reply == NULL) {
1119 NORM_ERR("Error parsing POP3 response: %s", recvbuf);
1123 timed_thread_lock(mail->p_timed_thread);
1124 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1125 timed_thread_unlock(mail->p_timed_thread);
1128 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1129 if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
1130 NORM_ERR("POP3 logout failed: %s", recvbuf);
1135 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1137 if (system(mail->command) == -1) {
1142 old_unseen = mail->unseen;
1144 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1145 /* if a valid socket, close it */
1148 if (timed_thread_test(mail->p_timed_thread, 0)) {
1149 timed_thread_exit(mail->p_timed_thread);
1157 void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
1159 struct mail_s *mail = obj->data.opaque;
1164 ensure_mail_thread(mail, pop3_thread, "pop3");
1166 if (mail && mail->p_timed_thread) {
1167 timed_thread_lock(mail->p_timed_thread);
1168 snprintf(p, p_max_size, "%lu", mail->unseen);
1169 timed_thread_unlock(mail->p_timed_thread);
1173 void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
1175 struct mail_s *mail = obj->data.opaque;
1180 ensure_mail_thread(mail, pop3_thread, "pop3");
1182 if (mail && mail->p_timed_thread) {
1183 timed_thread_lock(mail->p_timed_thread);
1184 snprintf(p, p_max_size, "%.1f",
1185 mail->used / 1024.0 / 1024.0);
1186 timed_thread_unlock(mail->p_timed_thread);