1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <unistd.h> |
4 |
#include <string.h> |
5 |
#include <limits.h> |
6 |
#include <errno.h> |
7 |
|
8 |
#include "config.h" |
9 |
#include "imapfilter.h" |
10 |
#include "data.h" |
11 |
|
12 |
|
13 |
extern account_t *accounts; |
14 |
extern filter_t *filters; |
15 |
|
16 |
unsigned int options; /* Program options. */ |
17 |
char logfile[PATH_MAX]; /* Log file. */ |
18 |
|
19 |
|
20 |
/* |
21 |
* In the beginning there was main()... |
22 |
*/ |
23 |
int main(int argc, char *argv[]) |
24 |
{ |
25 |
int c, r; |
26 |
char *confile = NULL; /* Configuration file. */ |
27 |
account_t *ca; /* Current account. */ |
28 |
mbox_t *cm; /* Current mailbox. */ |
29 |
|
30 |
options = OPTION_DETAILS_NORMAL; |
31 |
*logfile = 0; |
32 |
|
33 |
while ((c = getopt(argc, argv, "c:hl:qv")) != -1) { |
34 |
switch (c) { |
35 |
case 'c': |
36 |
confile = optarg; |
37 |
break; |
38 |
case 'h': |
39 |
usage(); |
40 |
exit(ERROR_UNDEFINED); |
41 |
break; |
42 |
case 'l': |
43 |
strncat(logfile, optarg, PATH_MAX - 1); |
44 |
break; |
45 |
case 'q': |
46 |
options &= OPTION_DETAILS_CLEAR; |
47 |
options |= OPTION_DETAILS_QUIET; |
48 |
break; |
49 |
case 'v': |
50 |
options &= OPTION_DETAILS_CLEAR; |
51 |
options |= OPTION_DETAILS_VERBOSE; |
52 |
break; |
53 |
default: |
54 |
usage(); |
55 |
exit(ERROR_UNDEFINED); |
56 |
break; |
57 |
} |
58 |
} |
59 |
|
60 |
read_config(confile); |
61 |
open_logfile(); |
62 |
|
63 |
for (ca = accounts; ca; ca = ca->next) { |
64 |
|
65 |
#ifndef SSL_TLS |
66 |
if (init_connection(ca->server, ca->port)) { |
67 |
#else |
68 |
if (init_connection(ca->server, ca->port, ca->ssl)) { |
69 |
#endif |
70 |
close_logfile(); |
71 |
exit(ERROR_NETWORK); |
72 |
} |
73 |
r = greeting_response(); |
74 |
|
75 |
if (r == RESPONSE_BYE || check_capabilities()) |
76 |
continue; |
77 |
|
78 |
#ifdef DEBUG |
79 |
test(); |
80 |
#endif |
81 |
|
82 |
if (r != RESPONSE_PREAUTH && login(ca->username, ca->password)) |
83 |
continue; |
84 |
|
85 |
for (cm = ca->mboxes; cm; cm = cm->next) |
86 |
if (!*cm->filters) |
87 |
mailbox_status(cm->name); |
88 |
else if (!select_mailbox(cm->name)) { |
89 |
apply_filters(cm->filters); |
90 |
close_mailbox(); |
91 |
} |
92 |
logout(); |
93 |
|
94 |
close_connection(); |
95 |
|
96 |
overwrite_password(ca->password); |
97 |
} |
98 |
|
99 |
close_logfile(); |
100 |
|
101 |
exit(0); |
102 |
} |
103 |
|
104 |
|
105 |
/* |
106 |
* Print a very brief usage message. |
107 |
*/ |
108 |
void usage(void) |
109 |
{ |
110 |
fprintf(stderr, |
111 |
"usage: imapfilter [-hqv] [-c configfile] [-l logfile]\n"); |
112 |
} |