1
/* Conky, a system monitor, based on torsmo
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
3
6
* Any original torsmo code is licensed under the BSD license
7
10
* Please see COPYING for details
9
12
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13
* Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
12
15
* All rights reserved.
54
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;
57
101
char *current_mail_spool;
59
void update_mail_count(struct local_mail_s *mail)
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)
73
120
mail->last_update = current_update_time;
76
if (stat(mail->box, &st)) {
123
if (stat(mail->mbox, &st)) {
77
124
static int rep = 0;
80
ERR("can't stat %s: %s", mail->box, strerror(errno));
127
NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
96
143
mail->forwarded_mail_count = mail->unforwarded_mail_count = 0;
97
144
mail->replied_mail_count = mail->unreplied_mail_count = 0;
98
145
mail->draft_mail_count = mail->trashed_mail_count = 0;
99
dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
146
dirname = (char *) malloc(sizeof(char) * (strlen(mail->mbox) + 5));
104
strcpy(dirname, mail->box);
151
strcpy(dirname, mail->mbox);
105
152
strcat(dirname, "/");
106
153
/* checking the cur subdirectory */
107
154
strcat(dirname, "cur");
109
156
dir = opendir(dirname);
111
ERR("cannot open directory");
158
NORM_ERR("cannot open directory");
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;
293
406
#define MAXDATASIZE 1000
295
408
struct mail_s *parse_mail_args(char type, const char *arg)
303
416
if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
305
418
if (type == POP3_TYPE) {
306
ERR("Scanning POP3 args failed");
419
NORM_ERR("Scanning POP3 args failed");
307
420
} else if (type == IMAP_TYPE) {
308
ERR("Scanning IMAP args failed");
421
NORM_ERR("Scanning IMAP args failed");
352
465
if (type == IMAP_TYPE) {
353
466
tmp = strstr(arg, "-f ");
356
sscanf(tmp, "%s", mail->folder);
470
if (tmp[0] == '\'') {
471
len = strstr(tmp + 1, "'") - tmp - 1;
476
strncpy(mail->folder, tmp + 1, len);
358
478
strncpy(mail->folder, "INBOX", 128); // default imap inbox
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;
380
568
int imap_command(int sockfd, const char *command, char *response, const char *verify)
382
struct timeval timeout;
570
struct timeval fetchtimeout;
384
572
int res, numbytes = 0;
385
573
if (send(sockfd, command, strlen(command), 0) == -1) {
389
timeout.tv_sec = 60; // 60 second timeout i guess
577
fetchtimeout.tv_sec = 60; // 60 second timeout i guess
578
fetchtimeout.tv_usec = 0;
392
580
FD_SET(sockfd, &fdset);
393
res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
581
res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
395
583
if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
441
void *imap_thread(void *arg)
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)
443
649
int sockfd, numbytes;
444
650
char recvbuf[MAXDATASIZE];
454
660
struct mail_s *mail = (struct mail_s *)arg;
455
661
int has_idle = 0;
456
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) {
458
671
#ifdef HAVE_GETHOSTBYNAME_R
459
if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
460
ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
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));
463
677
#else /* HAVE_GETHOSTBYNAME_R */
464
if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
465
herror("gethostbyname");
678
if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
679
herror("gethostbyname");
468
683
#endif /* HAVE_GETHOSTBYNAME_R */
469
while (fail < mail->retries) {
470
struct timeval timeout;
475
ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
687
NORM_ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
476
688
mail->user, mail->host, fail + 1, mail->retries);
500
timeout.tv_sec = 60; // 60 second timeout i guess
712
fetchtimeout.tv_sec = 60; // 60 second timeout i guess
713
fetchtimeout.tv_usec = 0;
503
715
FD_SET(sockfd, &fdset);
504
res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
716
res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
506
718
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
512
ERR("IMAP connection failed: timeout");
724
NORM_ERR("IMAP connection failed: timeout");
516
728
recvbuf[numbytes] = '\0';
517
729
DBGP2("imap_thread() received: %s", recvbuf);
518
730
if (strstr(recvbuf, "* OK") != recvbuf) {
519
ERR("IMAP connection failed, probably not an IMAP server");
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) {
523
744
strncpy(sendbuf, "a1 login ", MAXDATASIZE);
524
745
strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
525
746
strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
532
if (strstr(recvbuf, " IDLE ") != NULL) {
536
strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
754
strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
537
755
strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
538
strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
756
strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
539
757
MAXDATASIZE - strlen(sendbuf) - 1);
540
758
if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
552
770
old_messages = mail->messages;
555
strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
773
strncpy(sendbuf, "a4 SELECT \"", MAXDATASIZE);
556
774
strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
557
strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
775
strncat(sendbuf, "\"\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
558
776
if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
572
790
* RFC 2177 says we have to re-idle every 29 minutes.
573
791
* We'll do it every 20 minutes to be safe.
575
timeout.tv_sec = 1200;
793
fetchtimeout.tv_sec = 1200;
794
fetchtimeout.tv_usec = 0;
577
795
DBGP2("idling...");
579
797
FD_SET(sockfd, &fdset);
580
798
FD_SET(threadfd, &fdset);
581
res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
799
res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, &fetchtimeout);
582
800
if (timed_thread_test(mail->p_timed_thread, 1) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
583
801
if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
584
802
/* if a valid socket, close it */
598
816
recvbuf[numbytes] = '\0';
599
817
DBGP2("imap_thread() received: %s", recvbuf);
600
818
if (strlen(recvbuf) > 2) {
601
unsigned long messages, recent;
819
unsigned long messages, recent = 0;
602
820
char *buf = recvbuf;
603
821
char force_check = 0;
604
822
buf = strstr(buf, "EXISTS");
638
856
* something other than 0, or we had a timeout
641
if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0 || force_check) {
859
if (recent > 0 || (buf && strstr(buf, " FETCH ")) || fetchtimeout.tv_sec == 0 || force_check) {
642
860
// re-check messages and unseen
643
861
if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
647
strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
865
strncpy(sendbuf, "a2 STATUS \"", MAXDATASIZE);
648
866
strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
649
strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
867
strncat(sendbuf, "\" (MESSAGES UNSEEN)\r\n",
650
868
MAXDATASIZE - strlen(sendbuf) - 1);
651
869
if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
690
timeout.tv_sec = 60; // 60 second timeout i guess
908
fetchtimeout.tv_sec = 60; // 60 second timeout i guess
909
fetchtimeout.tv_usec = 0;
693
911
FD_SET(sockfd, &fdset);
694
res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
912
res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
696
914
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
697
915
perror("recv a3");
702
920
recvbuf[numbytes] = '\0';
703
921
DBGP2("imap_thread() received: %s", recvbuf);
704
922
if (strstr(recvbuf, "a3 OK") == NULL) {
705
ERR("IMAP logout failed: %s", recvbuf);
923
NORM_ERR("IMAP logout failed: %s", recvbuf);
942
void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
944
struct mail_s *mail = obj->data.opaque;
949
ensure_mail_thread(mail, imap_thread, "imap");
951
if (mail && mail->p_timed_thread) {
952
timed_thread_lock(mail->p_timed_thread);
953
snprintf(p, p_max_size, "%lu", mail->unseen);
954
timed_thread_unlock(mail->p_timed_thread);
958
void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
960
struct mail_s *mail = obj->data.opaque;
965
ensure_mail_thread(mail, imap_thread, "imap");
967
if (mail && mail->p_timed_thread) {
968
timed_thread_lock(mail->p_timed_thread);
969
snprintf(p, p_max_size, "%lu", mail->messages);
970
timed_thread_unlock(mail->p_timed_thread);
724
974
int pop3_command(int sockfd, const char *command, char *response, const char *verify)
726
struct timeval timeout;
976
struct timeval fetchtimeout;
728
978
int res, numbytes = 0;
729
979
if (send(sockfd, command, strlen(command), 0) == -1) {
733
timeout.tv_sec = 60; // 60 second timeout i guess
983
fetchtimeout.tv_sec = 60; // 60 second timeout i guess
984
fetchtimeout.tv_usec = 0;
736
986
FD_SET(sockfd, &fdset);
737
res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
987
res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
739
989
if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
763
1013
char hostbuff[2048];
764
1014
struct sockaddr_in their_addr; // connector's address information
765
1015
struct mail_s *mail = (struct mail_s *)arg;
1016
char resolved_host = 0;
1018
while (fail < mail->retries) {
1019
struct timeval fetchtimeout;
1022
if (!resolved_host) {
767
1023
#ifdef HAVE_GETHOSTBYNAME_R
768
if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
769
ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1024
if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1025
NORM_ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
772
1029
#else /* HAVE_GETHOSTBYNAME_R */
773
if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
774
herror("gethostbyname");
1030
if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1031
herror("gethostbyname");
777
1035
#endif /* HAVE_GETHOSTBYNAME_R */
778
while (fail < mail->retries) {
779
struct timeval timeout;
784
ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1039
NORM_ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
785
1040
mail->user, mail->host, fail + 1, mail->retries);
809
timeout.tv_sec = 60; // 60 second timeout i guess
1064
fetchtimeout.tv_sec = 60; // 60 second timeout i guess
1065
fetchtimeout.tv_usec = 0;
811
1066
FD_ZERO(&fdset);
812
1067
FD_SET(sockfd, &fdset);
813
res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1068
res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
815
1070
if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
821
ERR("POP3 connection failed: timeout\n");
1076
NORM_ERR("POP3 connection failed: timeout\n");
825
1080
DBGP2("pop3_thread received: %s", recvbuf);
826
1081
recvbuf[numbytes] = '\0';
827
1082
if (strstr(recvbuf, "+OK ") != recvbuf) {
828
ERR("POP3 connection failed, probably not a POP3 server");
1083
NORM_ERR("POP3 connection failed, probably not a POP3 server");
841
1096
strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
842
1097
strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
843
1098
if (pop3_command(sockfd, sendbuf, recvbuf, "+OK ")) {
844
ERR("POP3 server login failed: %s", recvbuf);
1099
NORM_ERR("POP3 server login failed: %s", recvbuf);
864
1119
sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
865
1120
timed_thread_unlock(mail->p_timed_thread);
868
1123
strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
869
1124
if (pop3_command(sockfd, sendbuf, recvbuf, "+OK")) {
870
ERR("POP3 logout failed: %s", recvbuf);
1125
NORM_ERR("POP3 logout failed: %s", recvbuf);
875
1130
if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
876
1131
// new mail goodie
877
1132
if (system(mail->command) == -1) {
1152
void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
1154
struct mail_s *mail = obj->data.opaque;
1159
ensure_mail_thread(mail, pop3_thread, "pop3");
1161
if (mail && mail->p_timed_thread) {
1162
timed_thread_lock(mail->p_timed_thread);
1163
snprintf(p, p_max_size, "%lu", mail->unseen);
1164
timed_thread_unlock(mail->p_timed_thread);
1168
void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
1170
struct mail_s *mail = obj->data.opaque;
1175
ensure_mail_thread(mail, pop3_thread, "pop3");
1177
if (mail && mail->p_timed_thread) {
1178
timed_thread_lock(mail->p_timed_thread);
1179
snprintf(p, p_max_size, "%.1f",
1180
mail->used / 1024.0 / 1024.0);
1181
timed_thread_unlock(mail->p_timed_thread);