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