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