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