1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <unistd.h> |
4 |
#include <string.h> |
5 |
#include <limits.h> |
6 |
#include <errno.h> |
7 |
|
8 |
#include "config.h" |
9 |
#include "imapfilter.h" |
10 |
#include "data.h" |
11 |
|
12 |
|
13 |
extern account_t *accounts; |
14 |
extern filter_t *filters; |
15 |
|
16 |
unsigned int options; /* Program options. */ |
17 |
unsigned int capabilities; /* Capabilities of mail server. */ |
18 |
char logfile[PATH_MAX]; /* Log file. */ |
19 |
char *home = NULL; /* User's home directory. */ |
20 |
|
21 |
|
22 |
/* |
23 |
* In the beginning there was main()... |
24 |
*/ |
25 |
int main(int argc, char *argv[]) |
26 |
{ |
27 |
int c, r; |
28 |
char *confile = NULL; /* Configuration file. */ |
29 |
account_t *ca; /* Current account. */ |
30 |
mbox_t *cm; /* Current mailbox. */ |
31 |
|
32 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
33 |
*logfile = 0; |
34 |
home = getenv("HOME"); |
35 |
|
36 |
#ifndef ENCRYPTED_PASSWORDS |
37 |
while ((c = getopt(argc, argv, "c:hl:qv")) != -1) { |
38 |
#else |
39 |
while ((c = getopt(argc, argv, "c:hl:pqv")) != -1) { |
40 |
#endif |
41 |
switch (c) { |
42 |
case 'c': |
43 |
confile = optarg; |
44 |
break; |
45 |
case 'h': |
46 |
usage(); |
47 |
exit(ERROR_UNDEFINED); |
48 |
break; |
49 |
case 'l': |
50 |
strncat(logfile, optarg, PATH_MAX - 1); |
51 |
break; |
52 |
#ifdef ENCRYPTED_PASSWORDS |
53 |
case 'p': |
54 |
options |= OPTION_PASSWORD_EDITOR; |
55 |
break; |
56 |
#endif |
57 |
case 'q': |
58 |
options &= OPTION_DETAILS_CLEAR; |
59 |
options |= OPTION_DETAILS_QUIET; |
60 |
break; |
61 |
case 'v': |
62 |
options &= OPTION_DETAILS_CLEAR; |
63 |
options |= OPTION_DETAILS_VERBOSE; |
64 |
break; |
65 |
default: |
66 |
usage(); |
67 |
exit(ERROR_UNDEFINED); |
68 |
break; |
69 |
} |
70 |
} |
71 |
|
72 |
catch_signals(); |
73 |
|
74 |
create_homedir(); |
75 |
|
76 |
read_config(confile); |
77 |
open_logfile(); |
78 |
|
79 |
#ifdef ENCRYPTED_PASSWORDS |
80 |
read_passwords(); |
81 |
|
82 |
if ((options & OPTION_PASSWORD_EDITOR)) |
83 |
password_editor(); |
84 |
#endif |
85 |
|
86 |
for (ca = accounts; ca; ca = ca->next) { |
87 |
|
88 |
#ifndef SSL_TLS |
89 |
if (init_connection(ca->server, ca->port)) |
90 |
#else |
91 |
if (init_connection(ca->server, ca->port, ca->ssl)) |
92 |
#endif |
93 |
continue; |
94 |
|
95 |
r = greeting_response(); |
96 |
|
97 |
if (r == RESPONSE_BYE || check_capabilities()) |
98 |
continue; |
99 |
|
100 |
#ifdef DEBUG |
101 |
test(); |
102 |
#endif |
103 |
|
104 |
if (r != RESPONSE_PREAUTH) { |
105 |
if (ca->passwdattr == PASSWORD_NONE) { |
106 |
printf("Enter password for %s@%s: ", ca->username, ca->server); |
107 |
get_password(ca->password, PASSWORD_LEN); |
108 |
} |
109 |
if (login(ca->username, ca->password)) |
110 |
continue; |
111 |
} |
112 |
check_namespace(); |
113 |
|
114 |
for (cm = ca->mboxes; cm; cm = cm->next) |
115 |
if (!*cm->filters) |
116 |
mailbox_status(cm->name); |
117 |
else if (!select_mailbox(cm->name)) { |
118 |
apply_filters(cm->filters); |
119 |
close_mailbox(); |
120 |
} |
121 |
logout(); |
122 |
|
123 |
close_connection(); |
124 |
} |
125 |
|
126 |
overwrite_passwords(); |
127 |
|
128 |
close_logfile(); |
129 |
|
130 |
exit(0); |
131 |
} |
132 |
|
133 |
|
134 |
/* |
135 |
* Print a very brief usage message. |
136 |
*/ |
137 |
void usage(void) |
138 |
{ |
139 |
#ifndef ENCRYPTED_PASSWORDS |
140 |
fprintf(stderr, |
141 |
"usage: imapfilter [-hpqv] [-c configfile] [-l logfile]\n"); |
142 |
#else |
143 |
fprintf(stderr, |
144 |
"usage: imapfilter [-hqv] [-c configfile] [-l logfile]\n"); |
145 |
#endif |
146 |
} |