1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <string.h> |
3 |
|
|
#include <stdlib.h> |
4 |
|
|
#include <unistd.h> |
5 |
|
|
#include <errno.h> |
6 |
|
|
|
7 |
|
|
#include "config.h" |
8 |
|
|
#include "imapfilter.h" |
9 |
|
|
#include "imap.h" |
10 |
|
|
#include "connect.h" |
11 |
|
|
#include "log.h" |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
extern int sock; |
15 |
lefcha |
1.9 |
extern account_t *accounts; |
16 |
|
|
extern filter_t *dfilters, *afilters; |
17 |
lefcha |
1.1 |
extern int options; |
18 |
lefcha |
1.5 |
extern unsigned int dlimit, alimit; |
19 |
lefcha |
1.1 |
|
20 |
lefcha |
1.9 |
static int tag = 0xA000; /* Every IMAP command is prefixed with a |
21 |
|
|
unique [:alnum:] string. */ |
22 |
|
|
|
23 |
lefcha |
1.1 |
|
24 |
|
|
/* |
25 |
|
|
* Sends to server data; a command. |
26 |
|
|
*/ |
27 |
lefcha |
1.9 |
int send_command(char *cmd) |
28 |
lefcha |
1.1 |
{ |
29 |
|
|
|
30 |
|
|
#ifdef DEBUG |
31 |
lefcha |
1.9 |
printf("debug: sending command: %s", cmd); |
32 |
lefcha |
1.1 |
#endif |
33 |
|
|
|
34 |
lefcha |
1.9 |
if (write(sock, cmd, strlen(cmd)) == -1) { |
35 |
lefcha |
1.1 |
error("imapfilter: error while sending command; %s", |
36 |
|
|
strerror(errno)); |
37 |
lefcha |
1.9 |
return 1; |
38 |
lefcha |
1.1 |
} |
39 |
|
|
|
40 |
lefcha |
1.9 |
return 0; |
41 |
lefcha |
1.1 |
} |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
/* |
45 |
|
|
* Calls send_command() and get_response(). |
46 |
|
|
*/ |
47 |
lefcha |
1.9 |
int send_command_get_response(char *cmd) |
48 |
lefcha |
1.1 |
{ |
49 |
lefcha |
1.9 |
char buf[RESPONSE_BUF]; |
50 |
lefcha |
1.1 |
|
51 |
lefcha |
1.9 |
if (!send_command(cmd) && !get_response(buf)) |
52 |
|
|
return 0; |
53 |
lefcha |
1.8 |
else |
54 |
lefcha |
1.9 |
return 1; |
55 |
lefcha |
1.1 |
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
#ifdef DEBUG |
59 |
|
|
/* |
60 |
|
|
* IMAP NOOP: does nothing always succeeds. |
61 |
|
|
*/ |
62 |
|
|
int imap_noop(void) |
63 |
|
|
{ |
64 |
lefcha |
1.9 |
char cmd[SMALL_CMD]; |
65 |
lefcha |
1.1 |
|
66 |
|
|
verbose("Client request: NOOP\n"); |
67 |
|
|
|
68 |
lefcha |
1.9 |
snprintf(cmd, SMALL_CMD, "%X NOOP\r\n", tag++); |
69 |
lefcha |
1.1 |
|
70 |
lefcha |
1.9 |
return send_command_get_response(cmd); |
71 |
lefcha |
1.1 |
|
72 |
|
|
} |
73 |
|
|
#endif |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
/* |
77 |
|
|
* IMAP LOGOUT: informs server that client is done. |
78 |
|
|
*/ |
79 |
|
|
int imap_logout(void) |
80 |
|
|
{ |
81 |
lefcha |
1.9 |
char cmd[SMALL_CMD]; |
82 |
lefcha |
1.1 |
|
83 |
|
|
verbose("Client request: LOGOUT\n"); |
84 |
|
|
|
85 |
lefcha |
1.9 |
snprintf(cmd, SMALL_CMD, "%X LOGOUT\r\n", tag++); |
86 |
lefcha |
1.1 |
|
87 |
lefcha |
1.9 |
return send_command_get_response(cmd); |
88 |
lefcha |
1.1 |
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
/* |
92 |
|
|
* IMAP LOGIN: identifies client to server. |
93 |
|
|
*/ |
94 |
lefcha |
1.9 |
int imap_login(account_t * ca) |
95 |
lefcha |
1.1 |
{ |
96 |
lefcha |
1.9 |
char cmd[MEDIUM_CMD]; |
97 |
lefcha |
1.1 |
|
98 |
|
|
verbose("Client request: LOGIN\n"); |
99 |
|
|
|
100 |
lefcha |
1.9 |
snprintf(cmd, MEDIUM_CMD, "%X LOGIN \"%s\" \"%s\"\r\n", |
101 |
|
|
tag++, ca->userid, ca->passwd); |
102 |
lefcha |
1.1 |
|
103 |
lefcha |
1.9 |
return send_command_get_response(cmd); |
104 |
lefcha |
1.1 |
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
/* |
108 |
|
|
* IMAP SELECT: selects a mailbox in which messages can be accessed. |
109 |
|
|
*/ |
110 |
|
|
int imap_select(void) |
111 |
|
|
{ |
112 |
lefcha |
1.9 |
char cmd[SMALL_CMD]; |
113 |
lefcha |
1.1 |
|
114 |
|
|
verbose("Client request: SELECT\n"); |
115 |
|
|
|
116 |
lefcha |
1.9 |
snprintf(cmd, SMALL_CMD, "%X SELECT INBOX\r\n", tag++); |
117 |
lefcha |
1.1 |
|
118 |
lefcha |
1.10 |
return send_command_get_response(cmd); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
|
122 |
|
|
/* |
123 |
|
|
* IMAP STATUS: requests status of the indicated mailbox. |
124 |
|
|
*/ |
125 |
|
|
int imap_status(void) |
126 |
|
|
{ |
127 |
|
|
char cmd[MEDIUM_CMD]; |
128 |
|
|
|
129 |
|
|
verbose("Client request: STATUS\n"); |
130 |
|
|
|
131 |
|
|
snprintf(cmd, MEDIUM_CMD, |
132 |
|
|
"%X STATUS INBOX (MESSAGES RECENT UNSEEN)\r\n", tag++); |
133 |
|
|
|
134 |
|
|
if (!send_command(cmd) && !status_response()) |
135 |
lefcha |
1.9 |
return 0; |
136 |
lefcha |
1.8 |
else |
137 |
lefcha |
1.9 |
return 1; |
138 |
lefcha |
1.1 |
} |
139 |
lefcha |
1.10 |
|
140 |
lefcha |
1.1 |
|
141 |
|
|
/* |
142 |
|
|
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
143 |
|
|
*/ |
144 |
lefcha |
1.9 |
int imap_search(char *res) |
145 |
lefcha |
1.1 |
{ |
146 |
lefcha |
1.9 |
char cmd[BIG_CMD]; |
147 |
lefcha |
1.1 |
|
148 |
|
|
verbose("Client request: SEARCH\n"); |
149 |
|
|
|
150 |
lefcha |
1.9 |
generate_search_filters(cmd); |
151 |
|
|
if (send_command(cmd)) |
152 |
|
|
return 1; |
153 |
|
|
search_response(res); |
154 |
lefcha |
1.5 |
|
155 |
|
|
verbose("Client request: SEARCH\n"); |
156 |
lefcha |
1.1 |
|
157 |
lefcha |
1.9 |
if (alimit && afilters) { |
158 |
|
|
generate_search_limits(cmd); |
159 |
|
|
if (send_command(cmd)) |
160 |
|
|
return 1; |
161 |
|
|
search_response(res); |
162 |
lefcha |
1.5 |
} |
163 |
|
|
|
164 |
lefcha |
1.9 |
return 0; |
165 |
lefcha |
1.1 |
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
/* |
169 |
|
|
* IMAP FETCH: retrieves data associated with a message. |
170 |
|
|
*/ |
171 |
lefcha |
1.9 |
int imap_fetch(unsigned int msg, char *res) |
172 |
lefcha |
1.1 |
{ |
173 |
lefcha |
1.9 |
char cmd[MEDIUM_CMD]; |
174 |
lefcha |
1.1 |
|
175 |
|
|
verbose("Client request: FETCH\n"); |
176 |
|
|
|
177 |
lefcha |
1.9 |
snprintf(cmd, MEDIUM_CMD, |
178 |
lefcha |
1.1 |
"%X FETCH %d BODY[HEADER.FIELDS (\"DATE\" \"FROM\" \"SUBJECT\")]\r\n", |
179 |
lefcha |
1.9 |
tag++, msg); |
180 |
lefcha |
1.1 |
|
181 |
lefcha |
1.9 |
if (!send_command(cmd) && !fetch_response(res)) |
182 |
|
|
return 0; |
183 |
lefcha |
1.8 |
else |
184 |
lefcha |
1.9 |
return 1; |
185 |
lefcha |
1.1 |
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
/* |
189 |
|
|
* IMAP STORE: alters data associated with a message. |
190 |
|
|
*/ |
191 |
lefcha |
1.9 |
int imap_store(unsigned int msg) |
192 |
lefcha |
1.1 |
{ |
193 |
lefcha |
1.9 |
char cmd[MEDIUM_CMD]; |
194 |
lefcha |
1.1 |
|
195 |
|
|
verbose("Client request: STORE\n"); |
196 |
|
|
|
197 |
lefcha |
1.9 |
snprintf(cmd, MEDIUM_CMD, |
198 |
|
|
"%X STORE %d +FLAGS \\Deleted\r\n", tag++, msg); |
199 |
lefcha |
1.1 |
|
200 |
lefcha |
1.9 |
return send_command_get_response(cmd); |
201 |
lefcha |
1.1 |
} |
202 |
|
|
|
203 |
|
|
|
204 |
|
|
/* |
205 |
|
|
* IMAP EXPUNGE: premanently removes any messages with the \Deleted flag set. |
206 |
|
|
*/ |
207 |
|
|
int imap_expunge(void) |
208 |
|
|
{ |
209 |
lefcha |
1.9 |
char cmd[SMALL_CMD]; |
210 |
lefcha |
1.1 |
|
211 |
|
|
verbose("Client request: EXPUNGE\n"); |
212 |
|
|
|
213 |
lefcha |
1.9 |
snprintf(cmd, SMALL_CMD, "%X EXPUNGE\r\n", tag++); |
214 |
lefcha |
1.1 |
|
215 |
lefcha |
1.9 |
return send_command_get_response(cmd); |
216 |
lefcha |
1.1 |
} |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
/* |
220 |
lefcha |
1.9 |
* Prepares according to the filters defined by the user the IMAP SEARCH |
221 |
lefcha |
1.5 |
* command that will be sent. This command will search for DENY, ALLOW, |
222 |
|
|
* and DENY_LIMIT filters. |
223 |
lefcha |
1.1 |
*/ |
224 |
lefcha |
1.9 |
void generate_search_filters(char *cmd) |
225 |
lefcha |
1.1 |
{ |
226 |
|
|
int len; |
227 |
|
|
|
228 |
lefcha |
1.9 |
snprintf(cmd, BIG_CMD, "%X SEARCH", tag++); |
229 |
lefcha |
1.5 |
|
230 |
lefcha |
1.9 |
deny_filters(cmd); |
231 |
lefcha |
1.5 |
|
232 |
|
|
if (dlimit) { |
233 |
lefcha |
1.9 |
len = strlen(cmd); |
234 |
|
|
snprintf(cmd + len, BIG_CMD - len, " LARGER %d", dlimit); |
235 |
|
|
} else if (!dfilters) { /* If no DENY filters were defined, then |
236 |
lefcha |
1.5 |
deny all except the ALLOW filters. */ |
237 |
lefcha |
1.9 |
len = strlen(cmd); |
238 |
|
|
snprintf(cmd + len, BIG_CMD - len, " ALL"); |
239 |
lefcha |
1.7 |
} |
240 |
lefcha |
1.5 |
|
241 |
lefcha |
1.9 |
allow_filters(cmd, "NOT"); |
242 |
lefcha |
1.5 |
|
243 |
lefcha |
1.9 |
len = strlen(cmd); |
244 |
|
|
snprintf(cmd + len, BIG_CMD - len, "\r\n"); |
245 |
lefcha |
1.5 |
} |
246 |
|
|
|
247 |
|
|
|
248 |
|
|
/* |
249 |
lefcha |
1.9 |
* Prepares according to the filters defined by the user the IMAP SEARCH |
250 |
lefcha |
1.5 |
* command that will be sent. This command will search for the ALLOW_LIMIT |
251 |
|
|
* filter. |
252 |
|
|
*/ |
253 |
lefcha |
1.9 |
void generate_search_limits(char *cmd) |
254 |
lefcha |
1.5 |
{ |
255 |
|
|
int len; |
256 |
|
|
|
257 |
lefcha |
1.9 |
snprintf(cmd, BIG_CMD, "%X SEARCH", tag++); |
258 |
lefcha |
1.5 |
|
259 |
lefcha |
1.9 |
allow_filters(cmd, "OR"); |
260 |
lefcha |
1.1 |
|
261 |
lefcha |
1.9 |
len = strlen(cmd); |
262 |
|
|
snprintf(cmd + len, BIG_CMD - len, " LARGER %d", alimit); |
263 |
lefcha |
1.6 |
|
264 |
lefcha |
1.9 |
len = strlen(cmd); |
265 |
|
|
snprintf(cmd + len, BIG_CMD - len, "\r\n"); |
266 |
lefcha |
1.5 |
|
267 |
|
|
} |
268 |
lefcha |
1.2 |
|
269 |
|
|
|
270 |
lefcha |
1.5 |
/* |
271 |
|
|
* Adds to the IMAP SEARCH command all the DENY type filters. |
272 |
|
|
*/ |
273 |
lefcha |
1.9 |
void deny_filters(char *cmd) |
274 |
lefcha |
1.5 |
{ |
275 |
|
|
int len; |
276 |
lefcha |
1.9 |
filter_t *cdf; |
277 |
lefcha |
1.4 |
|
278 |
lefcha |
1.9 |
if (dfilters) { |
279 |
|
|
for (cdf = dfilters; cdf->next; cdf = cdf->next) { |
280 |
|
|
len = strlen(cmd); |
281 |
|
|
snprintf(cmd + len, BIG_CMD - len, |
282 |
lefcha |
1.5 |
" OR %s%s \"%s\"", |
283 |
lefcha |
1.9 |
custom_header(cdf->custom), cdf->name, cdf->body); |
284 |
lefcha |
1.5 |
} |
285 |
|
|
|
286 |
lefcha |
1.9 |
len = strlen(cmd); |
287 |
|
|
snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"", |
288 |
lefcha |
1.5 |
(dlimit ? "OR " : ""), |
289 |
lefcha |
1.9 |
custom_header(cdf->custom), cdf->name, cdf->body); |
290 |
lefcha |
1.5 |
} |
291 |
|
|
} |
292 |
lefcha |
1.4 |
|
293 |
|
|
|
294 |
lefcha |
1.5 |
/* |
295 |
|
|
* Adds to IMAP SEARCH command all the ALLOW type filters. |
296 |
|
|
*/ |
297 |
lefcha |
1.9 |
void allow_filters(char *cmd, char *key) |
298 |
lefcha |
1.5 |
{ |
299 |
|
|
int len; |
300 |
lefcha |
1.9 |
filter_t *caf; |
301 |
lefcha |
1.4 |
|
302 |
lefcha |
1.9 |
if (afilters) { |
303 |
|
|
for (caf = afilters; caf->next; caf = caf->next) { |
304 |
|
|
len = strlen(cmd); |
305 |
|
|
snprintf(cmd + len, BIG_CMD - len, |
306 |
lefcha |
1.5 |
" %s %s%s \"%s\"", key, |
307 |
lefcha |
1.9 |
custom_header(caf->custom), caf->name, caf->body); |
308 |
lefcha |
1.4 |
} |
309 |
lefcha |
1.2 |
|
310 |
lefcha |
1.9 |
len = strlen(cmd); |
311 |
|
|
snprintf(cmd + len, BIG_CMD - len, " %s%s%s \"%s\"", |
312 |
lefcha |
1.6 |
(!strncmp(key, "NOT", 3) ? "NOT " : ""), |
313 |
lefcha |
1.9 |
custom_header(caf->custom), caf->name, caf->body); |
314 |
lefcha |
1.5 |
} |
315 |
lefcha |
1.1 |
} |
316 |
|
|
|
317 |
|
|
|
318 |
|
|
/* |
319 |
lefcha |
1.9 |
* Converts the string with the UID's of the messages to be deleted to |
320 |
|
|
* integers, sends the appropriate command and optionally prints some |
321 |
|
|
* headers of the "to be deleted" messages. |
322 |
lefcha |
1.1 |
*/ |
323 |
lefcha |
1.9 |
void delete_messages(char *msgs) |
324 |
lefcha |
1.1 |
{ |
325 |
lefcha |
1.9 |
unsigned long int m; |
326 |
|
|
char hdr[HEADERS_MAX]; |
327 |
lefcha |
1.1 |
|
328 |
|
|
do { |
329 |
lefcha |
1.9 |
m = strtoul(msgs, &msgs, 0); |
330 |
lefcha |
1.1 |
|
331 |
lefcha |
1.9 |
if (options & OPT_SHOW_HEADERS) { |
332 |
|
|
imap_fetch(m, hdr); |
333 |
lefcha |
1.1 |
|
334 |
lefcha |
1.9 |
if (!(options & OPT_DETAILS_QUITE)) |
335 |
|
|
printf("Deleting message:\n%s", hdr); |
336 |
lefcha |
1.3 |
|
337 |
lefcha |
1.9 |
if (!(options & OPT_TEST_MODE)) |
338 |
|
|
log_info("%s", hdr); |
339 |
lefcha |
1.1 |
} |
340 |
|
|
|
341 |
lefcha |
1.9 |
if (!(options & OPT_TEST_MODE)) |
342 |
|
|
imap_store(m); |
343 |
lefcha |
1.4 |
|
344 |
lefcha |
1.9 |
} while (*msgs); |
345 |
lefcha |
1.1 |
} |