summaryrefslogtreecommitdiffstats
path: root/src/nmail-async.c
blob: c32896d3b2293b0c77102ebac2ff28063c4cfcd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* $Id$ */

/***
  This file is part of libnewmail

  libnewmail is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  libnewmail is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with libnewmail; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA
***/

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

#include <newmail.h>

/* The query to issue */
int query = NM_QUERY_NCUR|NM_QUERY_NNEW;

/* A counter for the queries running */
int count = 0;

/* The liboop main loop object */
oop_source *oop = NULL;

/* A linked list structure for the spools */
struct spool_ll{
    struct nm_spool *spool;
    struct spool_ll *next;
} *spools = NULL;

/* A function which converts binary mail spool status flags into human readable strings */
char *b2s(int b) {
    if (b < 0) return "fail";
    if (b > 0) return "yes";
    return "no";
}

/* A callback function for oop which terminates the main loop */
void* _finish(oop_source *source, struct timeval tv, void *user) {
    return OOP_HALT;
}

/* A utility function which issues the callback defined above */
void finish(void) {
    oop->on_time(oop, OOP_TIME_NOW, _finish, NULL);
}

/* A callback function which is called whenever a mail spool query finished */
void cb_check(struct nm_spool *s, struct nm_status *status, void *user) {
    struct nm_info i;
    static char txt[256];

    /* Check the status flag */
    if (!status)
        snprintf(txt, sizeof(txt), "%s", nm_strerror(nm_errno, errno, nm_explanation));

    /* Get some information about the mail spool */
    if (nm_info(s, &i) < 0) {
        fprintf(stderr, "nm_open(): %s\n", nm_strerror(nm_errno, errno, nm_explanation));
        goto finish;
    }

    /* Print that information */
    printf("\n%s:\n\tName: %s\n\tType: %s\n\tText: %s\n\tFlags: ", i.path, i.name, i.type, i.text);
    if (i.flags & NM_FLAG_SYNCHRONOUS) printf("SYNCHRONOUS ");
    if (i.flags & NM_FLAG_ASYNCHRONOUS) printf("ASYNCHRONOUS ");
    printf("\n");

    /* Show status information */
    if (status) {
        if (query & NM_QUERY_NCUR)
            printf("\tCurrent: %i\n", status->cur);
        else if (query & NM_QUERY_CUR)
            printf("\tCurrent: %s\n", b2s(status->cur));

        if (query & NM_QUERY_NNEW)
            printf("\tNew: %i\n", status->new);
        else if (query & NM_QUERY_NEW)
            printf("\tNew: %s\n", b2s(status->new));
        
    } else
        fprintf(stderr, "\n\tFAILURE: %s\n", txt);
    
finish:

    count--;
    if (count <= 0)
        finish();
}

/* The callback function for iterating through the mail spools available */
void cb_list(const char *spool, void *user) {
    struct nm_spool* s = NULL;
    struct spool_ll* l = 0;

    /* Open the mail spool */
    if (!(s = nm_open(spool))) {
        fprintf(stderr, "nm_open(\"%s\"): %s\n", spool, nm_strerror(nm_errno, errno, nm_explanation));
        goto finish;
    }

    /* Allocate a linked list entry */
    if (!(l = malloc(sizeof(struct spool_ll)))) {
        fprintf(stderr, "Memory\n");
        goto finish;
    }

    /* Start the spool query */
    if (nm_query_submit(s, query, oop, cb_check, NULL) < 0) {
        fprintf(stderr, "nm_check_submit(\"%s\"): %s\n", spool, nm_strerror(nm_errno, errno, nm_explanation));
        goto finish;
    }

    /* Fill in the linked list entry */
    l->spool = s;
    l->next = spools;
    spools = l;

    count++;
    
    return;
    
finish:

    free(l);

    if (s)
        nm_close(s);
}

int main(int argc, char *argv[]) {
    oop_source_sys *sys;
    count = 1; /* This variable is initialized to 1 because otherwise
                * the main loop is terminated too early when
                * synchronous mail spools are queried */

    /* Create a generic liboop main loop object */
    if (!(sys = oop_sys_new())) {
        fprintf(stderr, "Could not reate OOP system source.\n");
        return 1;
    }

    if (!(oop = oop_sys_source(sys))) {
        fprintf(stderr, "Could not reate OOP system source.\n");
        oop_sys_delete(sys);
        return 1;
    }

    /* Iterate through the mail spools avilable */
    if (nm_list(cb_list, NULL) < 0)
        nm_perror("nm_list()");

    count--;

    /* Wait until all queries finished */
    while (count > 0)
        oop_sys_run(sys);

    /* Shutdown the main loop */
    oop_sys_delete(sys);

    /* Free the linked list */
    while (spools) {
        struct spool_ll *l = spools;
        spools = spools->next;
        nm_close(l->spool);
        free(l);
    }

    return 0;
}