1 |
#include <stdio.h> |
2 |
#include <unistd.h> |
3 |
#include <string.h> |
4 |
|
5 |
#include "config.h" |
6 |
#include "imapfilter.h" |
7 |
#include "file.h" |
8 |
#include "connect.h" |
9 |
#include "imap.h" |
10 |
#include "log.h" |
11 |
|
12 |
|
13 |
int options; /* Program options. */ |
14 |
char logfile[PATH_MAX]; /* Log file. */ |
15 |
extern int sock; |
16 |
int curacc; /* Current account being proccessed. */ |
17 |
extern int acccnt; |
18 |
|
19 |
/* |
20 |
* In the begining there was main()... |
21 |
*/ |
22 |
int main(int argc, char **argv) |
23 |
{ |
24 |
char *config = NULL; /* Configuration file. */ |
25 |
int c; |
26 |
|
27 |
options = OPTION_DETAILS_NORMAL; |
28 |
logfile[0] = 0; |
29 |
|
30 |
while ((c = getopt(argc, argv, "c:hl:qstv")) != -1) { |
31 |
switch (c) { |
32 |
case 'c': |
33 |
config = optarg; |
34 |
break; |
35 |
case 'h': |
36 |
usage(); |
37 |
exit(1); |
38 |
break; |
39 |
case 'l': |
40 |
strncpy(logfile, optarg, FILENAME_MAX); |
41 |
break; |
42 |
case 'q': |
43 |
options &= OPTION_DETAILS_CLEAR; |
44 |
options |= OPTION_DETAILS_QUITE; |
45 |
break; |
46 |
case 's': |
47 |
options |= OPTION_SHOW_HEADERS; |
48 |
break; |
49 |
case 't': |
50 |
options |= OPTION_TEST_MODE; |
51 |
break; |
52 |
case 'v': |
53 |
options &= OPTION_DETAILS_CLEAR; |
54 |
options |= OPTION_DETAILS_VERBOSE; |
55 |
break; |
56 |
default: |
57 |
usage(); |
58 |
exit(1); |
59 |
break; |
60 |
} |
61 |
} |
62 |
|
63 |
if (read_config(config) || open_logfile()) |
64 |
exit(2); |
65 |
|
66 |
for (curacc = 0; curacc <= acccnt; curacc++) { |
67 |
if (establish_connection()) { |
68 |
close_logfile(); |
69 |
exit(3); |
70 |
} |
71 |
|
72 |
clear_stream(); |
73 |
|
74 |
#ifdef DEBUG |
75 |
imap_noop(); |
76 |
#endif |
77 |
|
78 |
imap_login(); |
79 |
imap_select(); |
80 |
|
81 |
apply_filters(); |
82 |
|
83 |
imap_logout(); |
84 |
|
85 |
close_connection(); |
86 |
} |
87 |
|
88 |
close_logfile(); |
89 |
|
90 |
exit(0); |
91 |
} |
92 |
|
93 |
|
94 |
/* |
95 |
* Search and if found (and not in test mode) delete any messages |
96 |
* that match the defined filters. |
97 |
*/ |
98 |
void apply_filters(void) |
99 |
{ |
100 |
char results[SEARCH_RESULTS_MAX]; |
101 |
|
102 |
if (!imap_search(results) && results[0]) { |
103 |
delete_messages(results); |
104 |
if (!(options & OPTION_TEST_MODE)) |
105 |
imap_expunge(); |
106 |
} |
107 |
} |
108 |
|
109 |
|
110 |
/* |
111 |
* Prints a very brief usage message. |
112 |
*/ |
113 |
void usage(void) |
114 |
{ |
115 |
fprintf(stderr, |
116 |
"usage: imapfilter [-hqstv] [-c configfile] [-l logfile]\n"); |
117 |
} |