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