~ubuntu-branches/debian/sid/conky/sid

« back to all changes in this revision

Viewing changes to src/mail.c

  • Committer: Bazaar Package Importer
  • Author(s): Cesare Tirabassi
  • Date: 2010-03-28 21:19:51 UTC
  • mfrom: (1.3.1 upstream) (16.1.17 lucid)
  • Revision ID: james.westby@ubuntu.com-20100328211951-ejq536k2r858srli
Tags: 1.8.0-1
* New upstream release:
  - add AF_UNIX socket support
  - fix sigsegv if config file is deleted (LP: #525926)
  - the following debian bugs are closed by this upload:
    + change automake1.10 to automake1.11 (Closes: #550929)
    + hwmon made compatible with kernels >= 2.6.31 (Closes: #556926)
    + text_buffer_size change is well documented (Closes: #519401)
    + fix diskio for not existing devices (Closes: #536557)
    + fix wrong mixer values on some systems (Closes: #540282)
    + fix minor memory leak (Closes: #566524)
    + fix some documentation error re. graphs (Closes: #564518)
    + add -p/--pause startup option (Closes: #513440)
    + fix documentation about mixer values (Closes: #538760)
* This release is based on the Ubuntu package with the following changes
  necessary for Debian (Closes: #536320):
  - change control and rules to build correctly on non-Linux arches
    (Closes: #536326)
  - updated NEWS, descriptions in control and changelog
  - change archive area to contrib
* Change priority of metapackage to extra
* My utmost thanks go to Kapil Hari Paranjape for his packaging work and to
  Luca Falavigna for being so kind to review and sponsor this release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
3
 *
 
4
 * Conky, a system monitor, based on torsmo
2
5
 *
3
6
 * Any original torsmo code is licensed under the BSD license
4
7
 *
7
10
 * Please see COPYING for details
8
11
 *
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.
11
14
 *      (see AUTHORS)
12
15
 * All rights reserved.
13
16
 *
29
32
#include "conky.h"
30
33
#include "common.h"
31
34
#include "logging.h"
32
 
#include "mail.h"
 
35
#include "text_object.h"
 
36
#include "timed_thread.h"
33
37
 
34
38
#include <errno.h>
35
39
#include <stdio.h>
54
58
 * #define MAX(a, b)  ((a > b) ? a : b)
55
59
 */
56
60
 
 
61
#define POP3_TYPE 1
 
62
#define IMAP_TYPE 2
 
63
 
 
64
struct mail_s {                 // for imap and pop3
 
65
        unsigned long unseen;
 
66
        unsigned long messages;
 
67
        unsigned long used;
 
68
        unsigned long quota;
 
69
        unsigned long port;
 
70
        unsigned int retries;
 
71
        float interval;
 
72
        double last_update;
 
73
        char host[128];
 
74
        char user[128];
 
75
        char pass[128];
 
76
        char command[1024];
 
77
        char folder[128];
 
78
        timed_thread *p_timed_thread;
 
79
        char secure;
 
80
};
 
81
 
 
82
struct local_mail_s {
 
83
        char *mbox;
 
84
        int mail_count;
 
85
        int new_mail_count;
 
86
        int seen_mail_count;
 
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;
 
94
        int draft_mail_count;
 
95
        int trashed_mail_count;
 
96
        float interval;
 
97
        time_t last_mtime;
 
98
        double last_update;
 
99
};
 
100
 
57
101
char *current_mail_spool;
58
102
 
59
 
void update_mail_count(struct local_mail_s *mail)
 
103
static struct mail_s *global_mail;
 
104
static int global_mail_use = 0;
 
105
 
 
106
static void update_mail_count(struct local_mail_s *mail)
60
107
{
61
108
        struct stat st;
62
109
 
73
120
                mail->last_update = current_update_time;
74
121
        }
75
122
 
76
 
        if (stat(mail->box, &st)) {
 
123
        if (stat(mail->mbox, &st)) {
77
124
                static int rep = 0;
78
125
 
79
126
                if (!rep) {
80
 
                        ERR("can't stat %s: %s", mail->box, strerror(errno));
 
127
                        NORM_ERR("can't stat %s: %s", mail->mbox, strerror(errno));
81
128
                        rep = 1;
82
129
                }
83
130
                return;
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));
100
147
                if (!dirname) {
101
 
                        ERR("malloc");
 
148
                        NORM_ERR("malloc");
102
149
                        return;
103
150
                }
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");
108
155
 
109
156
                dir = opendir(dirname);
110
157
                if (!dir) {
111
 
                        ERR("cannot open directory");
 
158
                        NORM_ERR("cannot open directory");
112
159
                        free(dirname);
113
160
                        return;
114
161
                }
119
166
                                mail->mail_count++;
120
167
                                mailflags = (char *) malloc(sizeof(char) * strlen(strrchr(dirent->d_name, ',')));
121
168
                                if (!mailflags) {
122
 
                                        ERR("malloc");
 
169
                                        NORM_ERR("malloc");
123
170
                                        free(dirname);
124
171
                                        return;
125
172
                                }
162
209
 
163
210
                dir = opendir(dirname);
164
211
                if (!dir) {
165
 
                        ERR("cannot open directory");
 
212
                        NORM_ERR("cannot open directory");
166
213
                        free(dirname);
167
214
                        return;
168
215
                }
201
248
                mail->replied_mail_count = mail->unreplied_mail_count = -1;
202
249
                mail->draft_mail_count = mail->trashed_mail_count = -1;
203
250
 
204
 
                fp = open_file(mail->box, &rep);
 
251
                fp = open_file(mail->mbox, &rep);
205
252
                if (!fp) {
206
253
                        return;
207
254
                }
290
337
        }
291
338
}
292
339
 
 
340
void parse_local_mail_args(struct text_object *obj, const char *arg)
 
341
{
 
342
        float n1;
 
343
        char mbox[256], dst[256];
 
344
        struct local_mail_s *locmail;
 
345
 
 
346
        if (!arg) {
 
347
                n1 = 9.5;
 
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
 
352
                   if defined */
 
353
                strncpy(mbox, current_mail_spool, sizeof(mbox));
 
354
        } else {
 
355
                if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
 
356
                        n1 = 9.5;
 
357
                        strncpy(mbox, arg, sizeof(mbox));
 
358
                }
 
359
        }
 
360
 
 
361
        variable_substitute(mbox, dst, sizeof(dst));
 
362
 
 
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;
 
368
}
 
369
 
 
370
#define PRINT_MAILS_GENERATOR(x) \
 
371
void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
 
372
{ \
 
373
        struct local_mail_s *locmail = obj->data.opaque; \
 
374
        if (!locmail) \
 
375
                return; \
 
376
        update_mail_count(locmail); \
 
377
        snprintf(p, p_max_size, "%d", locmail->x##mail_count); \
 
378
}
 
379
 
 
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_)
 
392
 
 
393
void free_local_mails(struct text_object *obj)
 
394
{
 
395
        struct local_mail_s *locmail = obj->data.opaque;
 
396
 
 
397
        if (!locmail)
 
398
                return;
 
399
 
 
400
        if (locmail->mbox)
 
401
                free(locmail->mbox);
 
402
        free(obj->data.opaque);
 
403
        obj->data.opaque = 0;
 
404
}
 
405
 
293
406
#define MAXDATASIZE 1000
294
407
 
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)
304
417
                        != 3) {
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");
309
422
                }
310
423
                return 0;
311
424
        }
352
465
        if (type == IMAP_TYPE) {
353
466
                tmp = strstr(arg, "-f ");
354
467
                if (tmp) {
 
468
                        int len = 1024;
355
469
                        tmp += 3;
356
 
                        sscanf(tmp, "%s", mail->folder);
 
470
                        if (tmp[0] == '\'') {
 
471
                                len = strstr(tmp + 1, "'") - tmp - 1;
 
472
                                if (len > 1024) {
 
473
                                        len = 1024;
 
474
                                }
 
475
                        }
 
476
                        strncpy(mail->folder, tmp + 1, len);
357
477
                } else {
358
478
                        strncpy(mail->folder, "INBOX", 128);    // default imap inbox
359
479
                }
377
497
        return mail;
378
498
}
379
499
 
 
500
void parse_imap_mail_args(struct text_object *obj, const char *arg)
 
501
{
 
502
        static int rep = 0;
 
503
 
 
504
        if (!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);
 
510
                        rep = 1;
 
511
                        return;
 
512
                }
 
513
                obj->data.opaque = global_mail;
 
514
                global_mail_use++;
 
515
                return;
 
516
        }
 
517
        // proccss
 
518
        obj->data.opaque = parse_mail_args(IMAP_TYPE, arg);
 
519
}
 
520
 
 
521
void parse_pop3_mail_args(struct text_object *obj, const char *arg)
 
522
{
 
523
        static int rep = 0;
 
524
 
 
525
        if (!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);
 
531
                        rep = 1;
 
532
                        return;
 
533
                }
 
534
                obj->data.opaque = global_mail;
 
535
                global_mail_use++;
 
536
                return;
 
537
        }
 
538
        // proccss
 
539
        obj->data.opaque = parse_mail_args(POP3_TYPE, arg);
 
540
}
 
541
 
 
542
void parse_global_imap_mail_args(const char *value)
 
543
{
 
544
        global_mail = parse_mail_args(IMAP_TYPE, value);
 
545
}
 
546
 
 
547
void parse_global_pop3_mail_args(const char *value)
 
548
{
 
549
        global_mail = parse_mail_args(POP3_TYPE, value);
 
550
}
 
551
 
 
552
void free_mail_obj(struct text_object *obj)
 
553
{
 
554
        if (!obj->data.opaque)
 
555
                return;
 
556
 
 
557
        if (obj->data.opaque == global_mail) {
 
558
                if (--global_mail_use == 0) {
 
559
                        free(global_mail);
 
560
                        global_mail = 0;
 
561
                }
 
562
        } else {
 
563
                free(obj->data.opaque);
 
564
                obj->data.opaque = 0;
 
565
        }
 
566
}
 
567
 
380
568
int imap_command(int sockfd, const char *command, char *response, const char *verify)
381
569
{
382
 
        struct timeval timeout;
 
570
        struct timeval fetchtimeout;
383
571
        fd_set fdset;
384
572
        int res, numbytes = 0;
385
573
        if (send(sockfd, command, strlen(command), 0) == -1) {
386
574
                perror("send");
387
575
                return -1;
388
576
        }
389
 
        timeout.tv_sec = 60;    // 60 second timeout i guess
390
 
        timeout.tv_usec = 0;
 
577
        fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
 
578
        fetchtimeout.tv_usec = 0;
391
579
        FD_ZERO(&fdset);
392
580
        FD_SET(sockfd, &fdset);
393
 
        res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
 
581
        res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
394
582
        if (res > 0) {
395
583
                if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
396
584
                        perror("recv");
416
604
        reply += 2;
417
605
        *strchr(reply, ')') = '\0';
418
606
        if (reply == NULL) {
419
 
                ERR("Error parsing IMAP response: %s", recvbuf);
 
607
                NORM_ERR("Error parsing IMAP response: %s", recvbuf);
420
608
                return -1;
421
609
        } else {
422
610
                timed_thread_lock(mail->p_timed_thread);
438
626
        }
439
627
}
440
628
 
441
 
void *imap_thread(void *arg)
 
629
static void ensure_mail_thread(struct mail_s *mail,
 
630
                void *thread(void *), const char *text)
 
631
{
 
632
        if (mail->p_timed_thread)
 
633
                return;
 
634
 
 
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);
 
639
        }
 
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);
 
644
        }
 
645
}
 
646
 
 
647
static void *imap_thread(void *arg)
442
648
{
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);
457
 
 
 
663
        char resolved_host = 0;
 
664
 
 
665
        while (fail < mail->retries) {
 
666
                struct timeval fetchtimeout;
 
667
                int res;
 
668
                fd_set fdset;
 
669
 
 
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));
461
 
                exit(1);
462
 
        }
 
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));
 
674
                                fail++;
 
675
                                break;
 
676
                        }
463
677
#else /* HAVE_GETHOSTBYNAME_R */
464
 
        if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
465
 
                herror("gethostbyname");
466
 
                exit(1);
467
 
        }
 
678
                        if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
 
679
                                herror("gethostbyname");
 
680
                                fail++;
 
681
                                break;
 
682
                        }
468
683
#endif /* HAVE_GETHOSTBYNAME_R */
469
 
        while (fail < mail->retries) {
470
 
                struct timeval timeout;
471
 
                int res;
472
 
                fd_set fdset;
473
 
 
 
684
                        resolved_host = 1;
 
685
                }
474
686
                if (fail > 0) {
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);
477
689
                }
478
690
                do {
497
709
                                break;
498
710
                        }
499
711
 
500
 
                        timeout.tv_sec = 60;    // 60 second timeout i guess
501
 
                        timeout.tv_usec = 0;
 
712
                        fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
 
713
                        fetchtimeout.tv_usec = 0;
502
714
                        FD_ZERO(&fdset);
503
715
                        FD_SET(sockfd, &fdset);
504
 
                        res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
 
716
                        res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
505
717
                        if (res > 0) {
506
718
                                if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
507
719
                                        perror("recv");
509
721
                                        break;
510
722
                                }
511
723
                        } else {
512
 
                                ERR("IMAP connection failed: timeout");
 
724
                                NORM_ERR("IMAP connection failed: timeout");
513
725
                                fail++;
514
726
                                break;
515
727
                        }
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");
520
 
                                fail++;
521
 
                                break;
522
 
                        }
 
731
                                NORM_ERR("IMAP connection failed, probably not an IMAP server");
 
732
                                fail++;
 
733
                                break;
 
734
                        }
 
735
                        strncpy(sendbuf, "abc CAPABILITY\r\n", MAXDATASIZE);
 
736
                        if (imap_command(sockfd, sendbuf, recvbuf, "abc OK")) {
 
737
                                fail++;
 
738
                                break;
 
739
                        }
 
740
                        if (strstr(recvbuf, " IDLE ") != NULL) {
 
741
                                has_idle = 1;
 
742
                        }
 
743
 
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);
529
750
                                fail++;
530
751
                                break;
531
752
                        }
532
 
                        if (strstr(recvbuf, " IDLE ") != NULL) {
533
 
                                has_idle = 1;
534
 
                        }
535
753
 
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")) {
541
759
                                fail++;
552
770
                        old_messages = mail->messages;
553
771
 
554
772
                        if (has_idle) {
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")) {
559
777
                                        fail++;
560
778
                                        break;
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.
574
792
                                         */
575
 
                                        timeout.tv_sec = 1200;
576
 
                                        timeout.tv_usec = 0;
 
793
                                        fetchtimeout.tv_sec = 1200;
 
794
                                        fetchtimeout.tv_usec = 0;
577
795
                                        DBGP2("idling...");
578
796
                                        FD_ZERO(&fdset);
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
639
857
                                                 */
640
858
                                                buf = recvbuf;
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")) {
644
862
                                                                fail++;
645
863
                                                                break;
646
864
                                                        }
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")) {
652
870
                                                                fail++;
687
905
                                        fail++;
688
906
                                        break;
689
907
                                }
690
 
                                timeout.tv_sec = 60;    // 60 second timeout i guess
691
 
                                timeout.tv_usec = 0;
 
908
                                fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
 
909
                                fetchtimeout.tv_usec = 0;
692
910
                                FD_ZERO(&fdset);
693
911
                                FD_SET(sockfd, &fdset);
694
 
                                res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
 
912
                                res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
695
913
                                if (res > 0) {
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);
706
924
                                        fail++;
707
925
                                        break;
708
926
                                }
721
939
        return 0;
722
940
}
723
941
 
 
942
void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
 
943
{
 
944
        struct mail_s *mail = obj->data.opaque;
 
945
 
 
946
        if (!mail)
 
947
                return;
 
948
 
 
949
        ensure_mail_thread(mail, imap_thread, "imap");
 
950
 
 
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);
 
955
        }
 
956
}
 
957
 
 
958
void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
 
959
{
 
960
        struct mail_s *mail = obj->data.opaque;
 
961
 
 
962
        if (!mail)
 
963
                return;
 
964
 
 
965
        ensure_mail_thread(mail, imap_thread, "imap");
 
966
 
 
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);
 
971
        }
 
972
}
 
973
 
724
974
int pop3_command(int sockfd, const char *command, char *response, const char *verify)
725
975
{
726
 
        struct timeval timeout;
 
976
        struct timeval fetchtimeout;
727
977
        fd_set fdset;
728
978
        int res, numbytes = 0;
729
979
        if (send(sockfd, command, strlen(command), 0) == -1) {
730
980
                perror("send");
731
981
                return -1;
732
982
        }
733
 
        timeout.tv_sec = 60;    // 60 second timeout i guess
734
 
        timeout.tv_usec = 0;
 
983
        fetchtimeout.tv_sec = 60;       // 60 second timeout i guess
 
984
        fetchtimeout.tv_usec = 0;
735
985
        FD_ZERO(&fdset);
736
986
        FD_SET(sockfd, &fdset);
737
 
        res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
 
987
        res = select(sockfd + 1, &fdset, NULL, NULL, &fetchtimeout);
738
988
        if (res > 0) {
739
989
                if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
740
990
                        perror("recv");
749
999
        return 0;
750
1000
}
751
1001
 
752
 
void *pop3_thread(void *arg)
 
1002
static void *pop3_thread(void *arg)
753
1003
{
754
1004
        int sockfd, numbytes;
755
1005
        char recvbuf[MAXDATASIZE];
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;
766
1017
 
 
1018
        while (fail < mail->retries) {
 
1019
                struct timeval fetchtimeout;
 
1020
                int res;
 
1021
                fd_set fdset;
 
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));
770
 
                exit(1);
771
 
        }
 
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));
 
1026
                                fail++;
 
1027
                                break;
 
1028
                        }
772
1029
#else /* HAVE_GETHOSTBYNAME_R */
773
 
        if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
774
 
                herror("gethostbyname");
775
 
                exit(1);
 
1030
                        if ((he_res = gethostbyname(mail->host)) == NULL) {     // get the host info
 
1031
                                herror("gethostbyname");
 
1032
                fail++;
 
1033
                break;
776
1034
        }
777
1035
#endif /* HAVE_GETHOSTBYNAME_R */
778
 
        while (fail < mail->retries) {
779
 
                struct timeval timeout;
780
 
                int res;
781
 
                fd_set fdset;
782
 
 
 
1036
        resolved_host = 1;
 
1037
}
783
1038
                if (fail > 0) {
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);
786
1041
                }
787
1042
                do {
806
1061
                                break;
807
1062
                        }
808
1063
 
809
 
                        timeout.tv_sec = 60;    // 60 second timeout i guess
810
 
                        timeout.tv_usec = 0;
 
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);
814
1069
                        if (res > 0) {
815
1070
                                if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
816
1071
                                        perror("recv");
818
1073
                                        break;
819
1074
                                }
820
1075
                        } else {
821
 
                                ERR("POP3 connection failed: timeout\n");
 
1076
                                NORM_ERR("POP3 connection failed: timeout\n");
822
1077
                                fail++;
823
1078
                                break;
824
1079
                        }
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");
829
1084
                                fail++;
830
1085
                                break;
831
1086
                        }
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);
845
1100
                                fail++;
846
1101
                                break;
847
1102
                        }
856
1111
                        // now we get the data
857
1112
                        reply = recvbuf + 4;
858
1113
                        if (reply == NULL) {
859
 
                                ERR("Error parsing POP3 response: %s", recvbuf);
 
1114
                                NORM_ERR("Error parsing POP3 response: %s", recvbuf);
860
1115
                                fail++;
861
1116
                                break;
862
1117
                        } else {
864
1119
                                sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
865
1120
                                timed_thread_unlock(mail->p_timed_thread);
866
1121
                        }
867
 
                        
 
1122
 
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);
871
1126
                                fail++;
872
1127
                                break;
873
1128
                        }
874
 
                        
 
1129
 
875
1130
                        if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
876
1131
                                // new mail goodie
877
1132
                                if (system(mail->command) == -1) {
894
1149
        return 0;
895
1150
}
896
1151
 
 
1152
void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
 
1153
{
 
1154
        struct mail_s *mail = obj->data.opaque;
 
1155
 
 
1156
        if (!mail)
 
1157
                return;
 
1158
 
 
1159
        ensure_mail_thread(mail, pop3_thread, "pop3");
 
1160
 
 
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);
 
1165
        }
 
1166
}
 
1167
 
 
1168
void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
 
1169
{
 
1170
        struct mail_s *mail = obj->data.opaque;
 
1171
 
 
1172
        if (!mail)
 
1173
                return;
 
1174
 
 
1175
        ensure_mail_thread(mail, pop3_thread, "pop3");
 
1176
 
 
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);
 
1182
        }
 
1183
}