1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <stdlib.h> |
3 |
|
|
#include <string.h> |
4 |
lefcha |
1.3 |
#include <errno.h> |
5 |
lefcha |
1.1 |
|
6 |
|
|
#include "config.h" |
7 |
|
|
#include "imapfilter.h" |
8 |
|
|
#include "data.h" |
9 |
|
|
|
10 |
|
|
|
11 |
lefcha |
1.21 |
extern int sockpri, sockaux; |
12 |
lefcha |
1.1 |
extern unsigned int options; |
13 |
lefcha |
1.18 |
extern unsigned int capabilities; |
14 |
|
|
|
15 |
lefcha |
1.21 |
namesp_t nsppri, nspaux; /* Primary and auxiliary namespace. */ |
16 |
lefcha |
1.1 |
|
17 |
lefcha |
1.3 |
|
18 |
lefcha |
1.2 |
#ifdef DEBUG |
19 |
lefcha |
1.1 |
/* |
20 |
|
|
* Test/ping server. |
21 |
|
|
*/ |
22 |
lefcha |
1.21 |
int test(int *sock) |
23 |
lefcha |
1.1 |
{ |
24 |
lefcha |
1.21 |
return server_response(sock, imap_noop(sock)); |
25 |
lefcha |
1.1 |
} |
26 |
lefcha |
1.25 |
|
27 |
lefcha |
1.2 |
#endif |
28 |
lefcha |
1.1 |
|
29 |
lefcha |
1.10 |
|
30 |
lefcha |
1.1 |
/* |
31 |
lefcha |
1.10 |
* Check server's capabilities. |
32 |
lefcha |
1.1 |
*/ |
33 |
lefcha |
1.21 |
int check_capabilities(int *sock) |
34 |
lefcha |
1.1 |
{ |
35 |
lefcha |
1.18 |
capabilities = CAPABILITY_NONE; |
36 |
|
|
|
37 |
lefcha |
1.21 |
return capability_response(sock, imap_capability(sock)); |
38 |
lefcha |
1.1 |
} |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/* |
42 |
lefcha |
1.18 |
* Get namespace of mail server's mailboxes. |
43 |
|
|
*/ |
44 |
lefcha |
1.25 |
int check_namespace(int *sock, namesp_t * nsp) |
45 |
lefcha |
1.18 |
{ |
46 |
lefcha |
1.21 |
nsp->prefix[0] = nsp->delim = 0; |
47 |
lefcha |
1.18 |
|
48 |
|
|
if (!(options & OPTION_NAMESPACE) || |
49 |
|
|
!(capabilities & CAPABILITY_NAMESPACE)) |
50 |
|
|
return 0; |
51 |
|
|
else |
52 |
lefcha |
1.21 |
return namespace_response(sock, imap_namespace(sock), nsp); |
53 |
lefcha |
1.18 |
} |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
/* |
57 |
lefcha |
1.10 |
* Login to server. |
58 |
lefcha |
1.8 |
*/ |
59 |
lefcha |
1.21 |
int login(int *sock, char *user, char *pass) |
60 |
lefcha |
1.8 |
{ |
61 |
lefcha |
1.10 |
log_info(LOG_USERNAME, user); |
62 |
lefcha |
1.8 |
|
63 |
lefcha |
1.21 |
return server_response(sock, imap_login(sock, user, pass)); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
/* |
69 |
|
|
* Check if a mailbox exists. |
70 |
|
|
*/ |
71 |
lefcha |
1.25 |
int check_mailbox(int *sock, char *mbox, namesp_t * nsp) |
72 |
lefcha |
1.21 |
{ |
73 |
lefcha |
1.25 |
return server_response(sock, imap_examine(sock, |
74 |
lefcha |
1.21 |
apply_namespace(mbox, |
75 |
|
|
nsp->prefix, |
76 |
|
|
nsp->delim))); |
77 |
lefcha |
1.8 |
} |
78 |
|
|
|
79 |
lefcha |
1.18 |
|
80 |
lefcha |
1.8 |
/* |
81 |
lefcha |
1.3 |
* Open mailbox in read-write mode. |
82 |
lefcha |
1.1 |
*/ |
83 |
lefcha |
1.25 |
int select_mailbox(int *sock, char *mbox, namesp_t * nsp) |
84 |
lefcha |
1.1 |
{ |
85 |
|
|
int r; |
86 |
lefcha |
1.5 |
|
87 |
lefcha |
1.21 |
if (mailbox_status(sock, mbox, nsp) == -2) |
88 |
lefcha |
1.25 |
return -2; /* No messages exist. No filters need to be |
89 |
|
|
applied. */ |
90 |
lefcha |
1.5 |
|
91 |
lefcha |
1.21 |
r = select_response(sock, imap_select(sock, |
92 |
|
|
apply_namespace(mbox, nsp->prefix, |
93 |
|
|
nsp->delim))); |
94 |
lefcha |
1.5 |
|
95 |
lefcha |
1.4 |
log_info(LOG_MAILBOX, mbox); |
96 |
lefcha |
1.5 |
|
97 |
lefcha |
1.1 |
return r; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* |
102 |
lefcha |
1.3 |
* Get mailbox's status. |
103 |
lefcha |
1.1 |
*/ |
104 |
lefcha |
1.25 |
int mailbox_status(int *sock, char *mbox, namesp_t * nsp) |
105 |
lefcha |
1.1 |
{ |
106 |
lefcha |
1.21 |
return status_response(sock, imap_status(sock, |
107 |
lefcha |
1.25 |
apply_namespace(mbox, nsp->prefix, |
108 |
|
|
nsp->delim), |
109 |
|
|
"MESSAGES RECENT UNSEEN"), mbox); |
110 |
lefcha |
1.16 |
} |
111 |
lefcha |
1.5 |
|
112 |
lefcha |
1.9 |
|
113 |
lefcha |
1.1 |
/* |
114 |
|
|
* Close examined/selected mailbox. |
115 |
|
|
*/ |
116 |
lefcha |
1.21 |
int close_mailbox(int *sock) |
117 |
lefcha |
1.1 |
{ |
118 |
lefcha |
1.21 |
return server_response(sock, imap_close(sock)); |
119 |
lefcha |
1.1 |
} |
120 |
|
|
|
121 |
|
|
|
122 |
|
|
/* |
123 |
|
|
* Logout from server. |
124 |
|
|
*/ |
125 |
lefcha |
1.21 |
int logout(int *sock) |
126 |
lefcha |
1.1 |
{ |
127 |
lefcha |
1.32.2.3 |
return logout_response(sock, imap_logout(sock)); |
128 |
lefcha |
1.1 |
} |
129 |
|
|
|
130 |
|
|
|
131 |
|
|
/* |
132 |
|
|
* Match and apply filters assigned to a mailbox. |
133 |
|
|
*/ |
134 |
lefcha |
1.10 |
int apply_filters(filter_t ** filters) |
135 |
lefcha |
1.1 |
{ |
136 |
lefcha |
1.3 |
int i; |
137 |
lefcha |
1.12 |
char *mesgs; |
138 |
lefcha |
1.1 |
|
139 |
|
|
for (i = 0; filters[i]; i++) { |
140 |
lefcha |
1.16 |
mesgs = NULL; |
141 |
lefcha |
1.1 |
|
142 |
lefcha |
1.12 |
if (match_filter(filters[i], &mesgs)) |
143 |
lefcha |
1.1 |
continue; |
144 |
lefcha |
1.5 |
|
145 |
lefcha |
1.4 |
log_info(LOG_FILTER, filters[i]->key); |
146 |
lefcha |
1.1 |
|
147 |
lefcha |
1.3 |
apply_action(mesgs, &(filters[i]->action.type), |
148 |
lefcha |
1.25 |
filters[i]->action.raccount, filters[i]->action.destmbox, |
149 |
lefcha |
1.26 |
&filters[i]->action.msgflags, filters[i]->action.args); |
150 |
lefcha |
1.12 |
|
151 |
lefcha |
1.19 |
xfree(mesgs); |
152 |
lefcha |
1.1 |
} |
153 |
|
|
|
154 |
|
|
return 0; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
/* |
159 |
|
|
* Generate the search request by the masks of the filter and try to |
160 |
|
|
* match the generated filter. |
161 |
|
|
*/ |
162 |
lefcha |
1.12 |
int match_filter(filter_t * filter, char **mesgs) |
163 |
lefcha |
1.1 |
{ |
164 |
lefcha |
1.3 |
char *search; |
165 |
lefcha |
1.5 |
|
166 |
lefcha |
1.1 |
if (filter->mode == FILTER_MODE_OR) |
167 |
lefcha |
1.5 |
search = generate_filter_or(filter->masks, filter->masknum, |
168 |
|
|
filter->masklen); |
169 |
lefcha |
1.1 |
else |
170 |
lefcha |
1.5 |
search = generate_filter_and(filter->masks, filter->masknum, |
171 |
|
|
filter->masklen); |
172 |
lefcha |
1.1 |
|
173 |
lefcha |
1.21 |
search_response(&sockpri, imap_search(&sockpri, search), mesgs); |
174 |
lefcha |
1.5 |
|
175 |
lefcha |
1.19 |
xfree(search); |
176 |
lefcha |
1.5 |
|
177 |
lefcha |
1.16 |
if (!*mesgs) |
178 |
lefcha |
1.1 |
return 1; |
179 |
lefcha |
1.5 |
|
180 |
lefcha |
1.1 |
return 0; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
/* |
185 |
|
|
* Empty the FIFO inventory. |
186 |
|
|
*/ |
187 |
|
|
void empty_fifo(mask_t ** mfifo) |
188 |
|
|
{ |
189 |
|
|
mfifo[0] = NULL; |
190 |
|
|
|
191 |
lefcha |
1.3 |
queue_fifo(NULL, NULL); |
192 |
|
|
dequeue_fifo(NULL); |
193 |
lefcha |
1.1 |
} |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/* |
197 |
|
|
* Add item to FIFO inventory. |
198 |
|
|
*/ |
199 |
lefcha |
1.3 |
void queue_fifo(mask_t ** mfifo, mask_t * mask) |
200 |
lefcha |
1.1 |
{ |
201 |
lefcha |
1.15 |
static unsigned int i; |
202 |
lefcha |
1.1 |
|
203 |
|
|
if (!mfifo) { |
204 |
|
|
i = 0; |
205 |
|
|
return; |
206 |
|
|
} |
207 |
|
|
mfifo[i++] = mask; |
208 |
|
|
mfifo[i] = NULL; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
/* |
213 |
|
|
* Get next item from FIFO inventory. |
214 |
|
|
*/ |
215 |
lefcha |
1.3 |
mask_t *dequeue_fifo(mask_t ** mfifo) |
216 |
lefcha |
1.1 |
{ |
217 |
lefcha |
1.15 |
static unsigned int j; |
218 |
lefcha |
1.1 |
|
219 |
|
|
if (!mfifo) { |
220 |
|
|
j = 0; |
221 |
|
|
return NULL; |
222 |
|
|
} |
223 |
|
|
return mfifo[j++]; |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
|
227 |
|
|
/* |
228 |
|
|
* Generate the filter search command from the masks, assuming that |
229 |
|
|
* masks are AND-ed. |
230 |
|
|
*/ |
231 |
lefcha |
1.9 |
char *generate_filter_and(mask_t * mask, unsigned int masknum, |
232 |
lefcha |
1.10 |
unsigned int masklen) |
233 |
lefcha |
1.1 |
{ |
234 |
lefcha |
1.3 |
const unsigned int searchbuf = masklen + masknum * 6 + 8; |
235 |
lefcha |
1.15 |
unsigned int len = 0; |
236 |
lefcha |
1.3 |
char *search; |
237 |
lefcha |
1.9 |
mask_t *tmp; |
238 |
lefcha |
1.5 |
|
239 |
lefcha |
1.25 |
search = (char *)xmalloc(sizeof(char) * searchbuf); |
240 |
lefcha |
1.5 |
|
241 |
lefcha |
1.3 |
search[0] = 0; |
242 |
lefcha |
1.1 |
|
243 |
lefcha |
1.9 |
tmp = mask; |
244 |
lefcha |
1.10 |
if (!tmp) { |
245 |
|
|
strncat(search, "ALL ", searchbuf - len - 1); |
246 |
|
|
len += 4; |
247 |
|
|
} else |
248 |
|
|
while ((tmp = tmp->next)) { |
249 |
lefcha |
1.12 |
if (tmp->type != MASK_TYPE_OR) { |
250 |
|
|
strncat(search, "ALL ", searchbuf - len - 1); |
251 |
|
|
len += 4; |
252 |
|
|
break; |
253 |
|
|
} |
254 |
lefcha |
1.9 |
} |
255 |
lefcha |
1.1 |
|
256 |
lefcha |
1.9 |
tmp = NULL; |
257 |
lefcha |
1.1 |
while (mask) { |
258 |
lefcha |
1.9 |
tmp = mask; |
259 |
|
|
mask = mask->next; |
260 |
lefcha |
1.1 |
|
261 |
lefcha |
1.9 |
if (mask && mask->type == MASK_TYPE_OR) { |
262 |
lefcha |
1.3 |
strncat(search, "OR (", searchbuf - len - 1); |
263 |
lefcha |
1.1 |
len += 4; |
264 |
|
|
|
265 |
lefcha |
1.9 |
strncat(search, tmp->body, searchbuf - len - 1); |
266 |
|
|
len = strlen(search); |
267 |
|
|
search[len] = ' '; |
268 |
|
|
search[++len] = 0; |
269 |
|
|
|
270 |
|
|
search[len - 1] = ')'; |
271 |
|
|
search[len] = ' '; |
272 |
|
|
search[++len] = 0; |
273 |
|
|
|
274 |
|
|
if (!mask->next || mask->next->type != MASK_TYPE_OR) { |
275 |
|
|
search[len] = '('; |
276 |
|
|
search[++len] = 0; |
277 |
|
|
strncat(search, mask->body, searchbuf - len - 1); |
278 |
lefcha |
1.1 |
len = strlen(search); |
279 |
lefcha |
1.9 |
search[len] = ')'; |
280 |
|
|
search[++len] = ' '; |
281 |
lefcha |
1.3 |
search[++len] = 0; |
282 |
lefcha |
1.9 |
mask = mask->next; |
283 |
lefcha |
1.1 |
} |
284 |
lefcha |
1.9 |
} else { |
285 |
|
|
strncat(search, tmp->body, searchbuf - len - 1); |
286 |
|
|
len = strlen(search); |
287 |
lefcha |
1.3 |
search[len] = ' '; |
288 |
|
|
search[++len] = 0; |
289 |
lefcha |
1.1 |
} |
290 |
|
|
} |
291 |
lefcha |
1.10 |
|
292 |
lefcha |
1.9 |
search[len - 1] = 0; |
293 |
lefcha |
1.5 |
|
294 |
lefcha |
1.3 |
return search; |
295 |
lefcha |
1.1 |
} |
296 |
|
|
|
297 |
|
|
|
298 |
|
|
/* |
299 |
|
|
* Generate the filter search command from the masks, assuming that |
300 |
lefcha |
1.22 |
* masks are OR-ed. |
301 |
lefcha |
1.1 |
*/ |
302 |
lefcha |
1.9 |
char *generate_filter_or(mask_t * mask, unsigned int masknum, |
303 |
lefcha |
1.10 |
unsigned int masklen) |
304 |
lefcha |
1.1 |
{ |
305 |
lefcha |
1.3 |
const unsigned int searchbuf = masklen + masknum * 6 + 8; |
306 |
lefcha |
1.15 |
unsigned int len = 0; |
307 |
lefcha |
1.3 |
char *search; |
308 |
|
|
mask_t **mfifo; /* Mailbox FIFO queue. */ |
309 |
lefcha |
1.1 |
mask_t *mf; /* Mask returned from FIFO. */ |
310 |
lefcha |
1.5 |
|
311 |
lefcha |
1.25 |
search = (char *)xmalloc(sizeof(char) * searchbuf); |
312 |
lefcha |
1.5 |
mfifo = (mask_t **) xmalloc(sizeof(mask_t *) * (masknum + 1)); |
313 |
lefcha |
1.1 |
|
314 |
lefcha |
1.3 |
search[0] = 0; |
315 |
lefcha |
1.1 |
empty_fifo(mfifo); |
316 |
|
|
|
317 |
lefcha |
1.3 |
strncat(search, "ALL ", searchbuf - len - 1); |
318 |
lefcha |
1.1 |
len += 4; |
319 |
|
|
|
320 |
|
|
while (mask) { |
321 |
lefcha |
1.3 |
queue_fifo(mfifo, mask); |
322 |
lefcha |
1.1 |
mask = mask->next; |
323 |
|
|
|
324 |
|
|
while (mask && mask->type == MASK_TYPE_AND) { |
325 |
lefcha |
1.3 |
queue_fifo(mfifo, mask); |
326 |
lefcha |
1.1 |
mask = mask->next; |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
if (mask) { |
330 |
lefcha |
1.3 |
if (len == 4 && search[0] == 'A') |
331 |
|
|
search[0] = len = 0; |
332 |
lefcha |
1.5 |
|
333 |
lefcha |
1.3 |
strncat(search, "OR ", searchbuf - len - 1); |
334 |
lefcha |
1.1 |
len += 3; |
335 |
|
|
} |
336 |
lefcha |
1.3 |
if (search[0] != 'A') { |
337 |
|
|
search[len] = '('; |
338 |
|
|
search[++len] = 0; |
339 |
|
|
} |
340 |
|
|
while ((mf = dequeue_fifo(mfifo))) { |
341 |
|
|
strncat(search, mf->body, searchbuf - len - 1); |
342 |
lefcha |
1.1 |
len = strlen(search); |
343 |
lefcha |
1.3 |
search[len] = ' '; |
344 |
|
|
search[++len] = 0; |
345 |
lefcha |
1.1 |
} |
346 |
|
|
|
347 |
lefcha |
1.3 |
if (strchr(search, '(')) { |
348 |
|
|
search[len - 1] = ')'; |
349 |
|
|
search[len] = ' '; |
350 |
|
|
search[++len] = 0; |
351 |
|
|
} |
352 |
lefcha |
1.1 |
empty_fifo(mfifo); |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
search[len - 1] = 0; |
356 |
lefcha |
1.10 |
|
357 |
lefcha |
1.19 |
xfree(mfifo); |
358 |
lefcha |
1.3 |
|
359 |
|
|
return search; |
360 |
lefcha |
1.1 |
} |
361 |
|
|
|
362 |
|
|
|
363 |
|
|
/* |
364 |
|
|
* Apply the appropriate action. |
365 |
|
|
*/ |
366 |
lefcha |
1.25 |
int apply_action(char *mesgs, unsigned int *type, account_t * raccount, |
367 |
lefcha |
1.26 |
char *destmbox, unsigned int *msgflags, char *args) |
368 |
lefcha |
1.1 |
{ |
369 |
lefcha |
1.3 |
unsigned int cnt; |
370 |
lefcha |
1.5 |
|
371 |
lefcha |
1.3 |
if (!*mesgs) |
372 |
lefcha |
1.1 |
return 0; |
373 |
lefcha |
1.25 |
|
374 |
lefcha |
1.4 |
log_info(LOG_ACTION, type); |
375 |
|
|
log_info(LOG_DESTINATION_MAILBOX, destmbox); |
376 |
lefcha |
1.25 |
|
377 |
lefcha |
1.21 |
cnt = count_messages(mesgs); |
378 |
lefcha |
1.25 |
|
379 |
lefcha |
1.3 |
switch (*type) { |
380 |
lefcha |
1.1 |
case FILTER_ACTION_DELETE: |
381 |
lefcha |
1.8 |
info("%d message%s deleted.\n", cnt, plural(cnt)); |
382 |
lefcha |
1.1 |
action_delete(mesgs, args); |
383 |
|
|
break; |
384 |
|
|
case FILTER_ACTION_COPY: |
385 |
lefcha |
1.20 |
info("%d message%s copied to mailbox \"%s\".\n", cnt, plural(cnt), |
386 |
lefcha |
1.8 |
destmbox); |
387 |
lefcha |
1.21 |
action_copy(mesgs, apply_namespace(destmbox, nsppri.prefix, |
388 |
|
|
nsppri.delim), args); |
389 |
lefcha |
1.1 |
break; |
390 |
|
|
case FILTER_ACTION_MOVE: |
391 |
lefcha |
1.20 |
info("%d message%s moved to mailbox \"%s\".\n", cnt, plural(cnt), |
392 |
lefcha |
1.8 |
destmbox); |
393 |
lefcha |
1.21 |
action_move(mesgs, apply_namespace(destmbox, nsppri.prefix, |
394 |
|
|
nsppri.delim), args); |
395 |
|
|
break; |
396 |
|
|
case FILTER_ACTION_RCOPY: |
397 |
lefcha |
1.29 |
info("%d message%s copied to mailbox \"%s\" at account %s.\n", cnt, |
398 |
lefcha |
1.21 |
plural(cnt), destmbox, raccount->key); |
399 |
|
|
action_rcopy(mesgs, raccount, destmbox, args); |
400 |
|
|
break; |
401 |
|
|
case FILTER_ACTION_RMOVE: |
402 |
lefcha |
1.32 |
info("%d message%s moved to mailbox \"%s\" at account %s.\n", cnt, |
403 |
lefcha |
1.21 |
plural(cnt), destmbox, raccount->key); |
404 |
|
|
action_rmove(mesgs, raccount, destmbox, args); |
405 |
lefcha |
1.1 |
break; |
406 |
lefcha |
1.26 |
case FILTER_ACTION_FLAG_REPLACE: |
407 |
|
|
case FILTER_ACTION_FLAG_ADD: |
408 |
|
|
case FILTER_ACTION_FLAG_REMOVE: |
409 |
|
|
info("%d message%s flagged.\n", cnt, plural(cnt)); |
410 |
|
|
action_flag(mesgs, type, msgflags, args); |
411 |
|
|
break; |
412 |
lefcha |
1.1 |
case FILTER_ACTION_LIST: |
413 |
lefcha |
1.8 |
info("%d message%s listed.\n", cnt, plural(cnt)); |
414 |
lefcha |
1.1 |
action_list(mesgs, args); |
415 |
|
|
break; |
416 |
|
|
} |
417 |
lefcha |
1.5 |
|
418 |
|
|
if (!*args) |
419 |
lefcha |
1.4 |
log_info(LOG_WRITE, NULL); |
420 |
lefcha |
1.5 |
|
421 |
lefcha |
1.1 |
return 0; |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
|
425 |
|
|
/* |
426 |
|
|
* Delete messages and optionally list some of their headers. |
427 |
|
|
*/ |
428 |
|
|
int action_delete(char *mesgs, char *args) |
429 |
|
|
{ |
430 |
lefcha |
1.26 |
char *tok, *m, *mcp; |
431 |
lefcha |
1.23 |
|
432 |
|
|
action_list(mesgs, args); |
433 |
lefcha |
1.25 |
|
434 |
lefcha |
1.26 |
m = mcp = convert_messages(mesgs); |
435 |
lefcha |
1.25 |
|
436 |
lefcha |
1.32.2.2 |
tok = strtok_r(m, " ", &m); |
437 |
|
|
while (tok) { |
438 |
lefcha |
1.26 |
server_response(&sockpri, imap_store(&sockpri, tok, |
439 |
|
|
STORE_FLAG_ADD, "\\Deleted")); |
440 |
lefcha |
1.5 |
|
441 |
lefcha |
1.32.2.2 |
tok = strtok_r(NULL, " ", &m); |
442 |
|
|
} |
443 |
|
|
|
444 |
lefcha |
1.25 |
if (options & OPTION_EXPUNGE) |
445 |
|
|
server_response(&sockpri, imap_expunge(&sockpri)); |
446 |
|
|
|
447 |
lefcha |
1.19 |
xfree(mcp); |
448 |
lefcha |
1.10 |
|
449 |
lefcha |
1.1 |
return 0; |
450 |
|
|
} |
451 |
|
|
|
452 |
|
|
|
453 |
|
|
/* |
454 |
|
|
* Copy messages to specified mailbox. |
455 |
|
|
*/ |
456 |
lefcha |
1.3 |
int action_copy(char *mesgs, char *destmbox, char *args) |
457 |
lefcha |
1.1 |
{ |
458 |
lefcha |
1.20 |
int r = 0; |
459 |
lefcha |
1.26 |
char *tok = NULL, *mcp, *m; |
460 |
lefcha |
1.25 |
|
461 |
lefcha |
1.21 |
action_list(mesgs, args); |
462 |
lefcha |
1.25 |
|
463 |
lefcha |
1.26 |
m = mcp = convert_messages(mesgs); |
464 |
lefcha |
1.23 |
|
465 |
lefcha |
1.32.2.2 |
tok = strtok_r(m, " ", &m); |
466 |
|
|
while (tok) { |
467 |
lefcha |
1.22 |
if ((r = copy_response(&sockpri, |
468 |
|
|
imap_copy(&sockpri, tok, destmbox))) == |
469 |
|
|
RESPONSE_TRYCREATE) |
470 |
|
|
if (!server_response(&sockpri, imap_create(&sockpri, destmbox))) { |
471 |
lefcha |
1.27 |
if ((options & OPTION_SUBSCRIBE)) |
472 |
|
|
server_response(&sockpri, |
473 |
|
|
imap_subscribe(&sockpri, destmbox)); |
474 |
|
|
r = copy_response(&sockpri, |
475 |
|
|
imap_copy(&sockpri, tok, destmbox)); |
476 |
lefcha |
1.22 |
} |
477 |
lefcha |
1.32.2.2 |
tok = strtok_r(NULL, " ", &m); |
478 |
lefcha |
1.22 |
} |
479 |
lefcha |
1.23 |
|
480 |
lefcha |
1.19 |
xfree(mcp); |
481 |
lefcha |
1.8 |
|
482 |
lefcha |
1.20 |
return r; |
483 |
lefcha |
1.1 |
} |
484 |
|
|
|
485 |
|
|
|
486 |
|
|
/* |
487 |
|
|
* Move messages to specified mailbox. |
488 |
|
|
*/ |
489 |
lefcha |
1.3 |
int action_move(char *mesgs, char *destmbox, char *args) |
490 |
lefcha |
1.1 |
{ |
491 |
lefcha |
1.20 |
if (!action_copy(mesgs, destmbox, args)) |
492 |
|
|
action_delete(mesgs, "\0"); |
493 |
lefcha |
1.16 |
|
494 |
lefcha |
1.1 |
return 0; |
495 |
|
|
} |
496 |
|
|
|
497 |
|
|
|
498 |
|
|
/* |
499 |
lefcha |
1.21 |
* Copy messages to the specified mailbox of another mail server. |
500 |
|
|
*/ |
501 |
lefcha |
1.25 |
int action_rcopy(char *mesgs, account_t * destacc, char *destmbox, char *args) |
502 |
lefcha |
1.21 |
{ |
503 |
lefcha |
1.32.2.5 |
int r, ta, tf; |
504 |
lefcha |
1.21 |
char *tok, *m, *mcp, *ndm; |
505 |
lefcha |
1.30 |
unsigned int n; |
506 |
lefcha |
1.32.2.5 |
char buf[RESPONSE_BUF * 2]; |
507 |
lefcha |
1.25 |
|
508 |
lefcha |
1.21 |
if (init_connection(&sockaux, destacc->server, destacc->port, |
509 |
|
|
destacc->ssl)) |
510 |
|
|
return ERROR_NETWORK; |
511 |
lefcha |
1.25 |
|
512 |
lefcha |
1.21 |
r = greeting_response(&sockaux); |
513 |
lefcha |
1.25 |
|
514 |
lefcha |
1.21 |
if (r == RESPONSE_BYE || check_capabilities(&sockaux)) |
515 |
|
|
return ERROR_NETWORK; |
516 |
lefcha |
1.25 |
|
517 |
lefcha |
1.21 |
#ifdef DEBUG |
518 |
|
|
test(&sockaux); |
519 |
|
|
#endif |
520 |
lefcha |
1.25 |
|
521 |
lefcha |
1.21 |
if (r != RESPONSE_PREAUTH) { |
522 |
|
|
if (destacc->passwdattr == PASSWORD_NONE) { |
523 |
|
|
printf("Enter password for %s@%s: ", destacc->username, |
524 |
|
|
destacc->server); |
525 |
|
|
get_password(destacc->password, PASSWORD_LEN); |
526 |
|
|
destacc->passwdattr = PASSWORD_PLAIN; |
527 |
|
|
} |
528 |
|
|
if (login(&sockaux, destacc->username, |
529 |
|
|
destacc->password) == RESPONSE_NO) { |
530 |
|
|
error("imapfilter: username %s or password rejected at %s\n", |
531 |
|
|
destacc->username, destacc->server); |
532 |
|
|
return ERROR_NETWORK; |
533 |
|
|
} |
534 |
|
|
} |
535 |
|
|
check_namespace(&sockaux, &nspaux); |
536 |
|
|
|
537 |
|
|
/* apply_namespace() returns a pointer to a static buffer. */ |
538 |
|
|
ndm = apply_namespace(destmbox, nspaux.prefix, nspaux.delim); |
539 |
lefcha |
1.25 |
|
540 |
lefcha |
1.21 |
r = check_mailbox(&sockaux, ndm, &nspaux); |
541 |
|
|
|
542 |
|
|
if (r == RESPONSE_OK) |
543 |
|
|
close_mailbox(&sockaux); |
544 |
|
|
else if (r == RESPONSE_NO) { |
545 |
|
|
server_response(&sockaux, imap_create(&sockaux, ndm)); |
546 |
lefcha |
1.27 |
if ((options & OPTION_SUBSCRIBE)) |
547 |
|
|
server_response(&sockaux, imap_subscribe(&sockaux, ndm)); |
548 |
lefcha |
1.21 |
} |
549 |
|
|
m = mcp = xstrdup(mesgs); |
550 |
|
|
|
551 |
lefcha |
1.32.2.2 |
tok = strtok_r(m, " ", &m); |
552 |
|
|
while (tok) { |
553 |
lefcha |
1.21 |
fetchsize_response(&sockpri, &n, |
554 |
lefcha |
1.25 |
imap_fetch(&sockpri, tok, "RFC822.SIZE")); |
555 |
|
|
|
556 |
lefcha |
1.32.2.5 |
ta = imap_append(&sockaux, ndm, n); |
557 |
lefcha |
1.21 |
|
558 |
lefcha |
1.32.2.5 |
fetch_response(&sockpri, 0, 1, NULL); |
559 |
|
|
tf = imap_fetch(&sockpri, tok, "RFC822.HEADER"); |
560 |
lefcha |
1.21 |
do { |
561 |
lefcha |
1.32.2.5 |
r = fetch_response(&sockpri, tf, 0, buf); |
562 |
lefcha |
1.21 |
socket_write(&sockaux, buf); |
563 |
|
|
} while (r == RESPONSE_NONE); |
564 |
lefcha |
1.25 |
|
565 |
lefcha |
1.21 |
socket_write(&sockaux, "\r\n"); |
566 |
|
|
|
567 |
lefcha |
1.32.2.5 |
fetch_response(&sockpri, 0, 1, NULL); |
568 |
|
|
tf = imap_fetch(&sockpri, tok, "BODY[TEXT]"); |
569 |
lefcha |
1.21 |
do { |
570 |
lefcha |
1.32.2.5 |
r = fetch_response(&sockpri, tf, 0, buf); |
571 |
lefcha |
1.21 |
socket_write(&sockaux, buf); |
572 |
|
|
} while (r == RESPONSE_NONE); |
573 |
lefcha |
1.25 |
|
574 |
lefcha |
1.24 |
socket_write(&sockaux, "\r\n\r\n"); |
575 |
lefcha |
1.25 |
|
576 |
lefcha |
1.32.2.5 |
append_response(&sockaux, ta); |
577 |
lefcha |
1.32.2.2 |
|
578 |
|
|
tok = strtok_r(NULL, " ", &m); |
579 |
lefcha |
1.21 |
} |
580 |
|
|
|
581 |
|
|
logout(&sockaux); |
582 |
lefcha |
1.25 |
|
583 |
lefcha |
1.21 |
action_list(mesgs, args); |
584 |
lefcha |
1.25 |
|
585 |
lefcha |
1.21 |
xfree(mcp); |
586 |
|
|
|
587 |
|
|
return 0; |
588 |
|
|
} |
589 |
|
|
|
590 |
|
|
|
591 |
|
|
/* |
592 |
|
|
* Move messages to the specified mailbox of another mail server. |
593 |
|
|
*/ |
594 |
lefcha |
1.25 |
int action_rmove(char *mesgs, account_t * destacc, char *destmbox, char *args) |
595 |
lefcha |
1.21 |
{ |
596 |
|
|
if (!action_rcopy(mesgs, destacc, destmbox, args)) |
597 |
|
|
action_delete(mesgs, "\0"); |
598 |
lefcha |
1.25 |
|
599 |
lefcha |
1.21 |
return 0; |
600 |
|
|
} |
601 |
|
|
|
602 |
lefcha |
1.26 |
|
603 |
|
|
/* |
604 |
lefcha |
1.27 |
* Flag messages by replacing, adding or removing specified flags. |
605 |
lefcha |
1.26 |
*/ |
606 |
|
|
int action_flag(char *mesgs, unsigned int *type, unsigned int *msgflags, |
607 |
|
|
char *args) |
608 |
|
|
{ |
609 |
|
|
unsigned int t; |
610 |
|
|
char s[STORE_FLAGS_BUF]; |
611 |
|
|
char *tok, *m, *mcp; |
612 |
|
|
|
613 |
|
|
*s = 0; |
614 |
|
|
|
615 |
|
|
switch (*type) { |
616 |
|
|
case FILTER_ACTION_FLAG_ADD: |
617 |
|
|
t = STORE_FLAG_ADD; |
618 |
|
|
break; |
619 |
|
|
case FILTER_ACTION_FLAG_REMOVE: |
620 |
|
|
t = STORE_FLAG_REMOVE; |
621 |
|
|
break; |
622 |
lefcha |
1.28 |
default: |
623 |
|
|
t = STORE_FLAG_REPLACE; |
624 |
lefcha |
1.26 |
} |
625 |
|
|
|
626 |
|
|
if ((*msgflags != MESSAGE_FLAG_NONE)) { |
627 |
|
|
if ((*msgflags & MESSAGE_FLAG_SEEN)) |
628 |
|
|
strncat(s, "\\Seen ", STORE_FLAGS_BUF - strlen(s) - 1); |
629 |
|
|
if ((*msgflags & MESSAGE_FLAG_ANSWERED)) |
630 |
|
|
strncat(s, "\\Answered ", STORE_FLAGS_BUF - strlen(s) - 1); |
631 |
|
|
if ((*msgflags & MESSAGE_FLAG_FLAGGED)) |
632 |
|
|
strncat(s, "\\Flagged ", STORE_FLAGS_BUF - strlen(s) - 1); |
633 |
|
|
if ((*msgflags & MESSAGE_FLAG_DELETED)) |
634 |
|
|
strncat(s, "\\Deleted ", STORE_FLAGS_BUF - strlen(s) - 1); |
635 |
|
|
if ((*msgflags & MESSAGE_FLAG_DRAFT)) |
636 |
|
|
strncat(s, "\\Draft", STORE_FLAGS_BUF - strlen(s) - 1); |
637 |
|
|
if ((s[strlen(s) - 1] == ' ')) |
638 |
|
|
s[strlen(s) - 1] = 0; |
639 |
|
|
} |
640 |
|
|
action_list(mesgs, args); |
641 |
|
|
|
642 |
|
|
m = mcp = convert_messages(mesgs); |
643 |
|
|
|
644 |
lefcha |
1.32.2.2 |
tok = strtok_r(m, " ", &m); |
645 |
|
|
while (tok) { |
646 |
lefcha |
1.26 |
server_response(&sockpri, imap_store(&sockpri, tok, t, s)); |
647 |
|
|
|
648 |
lefcha |
1.32.2.2 |
tok = strtok_r(NULL, " ", &m); |
649 |
|
|
} |
650 |
|
|
|
651 |
lefcha |
1.26 |
if (options & OPTION_EXPUNGE) |
652 |
|
|
server_response(&sockpri, imap_expunge(&sockpri)); |
653 |
|
|
|
654 |
|
|
xfree(mcp); |
655 |
|
|
|
656 |
|
|
return 0; |
657 |
|
|
} |
658 |
lefcha |
1.21 |
|
659 |
|
|
/* |
660 |
lefcha |
1.1 |
* List user selected headers of messages. |
661 |
|
|
*/ |
662 |
|
|
int action_list(char *mesgs, char *args) |
663 |
|
|
{ |
664 |
lefcha |
1.32.2.5 |
int r, t; |
665 |
lefcha |
1.21 |
char *tok, *mcp, *m; |
666 |
|
|
char s[ARGS_LEN + 27]; |
667 |
|
|
char hdrs[RESPONSE_BUF]; |
668 |
lefcha |
1.5 |
|
669 |
lefcha |
1.2 |
if (!*args) |
670 |
|
|
return 0; |
671 |
lefcha |
1.1 |
|
672 |
lefcha |
1.5 |
m = mcp = xstrdup(mesgs); |
673 |
lefcha |
1.6 |
|
674 |
lefcha |
1.21 |
snprintf(s, ARGS_LEN + 27 - 1, "BODY.PEEK[HEADER.FIELDS (%s)]", args); |
675 |
lefcha |
1.1 |
|
676 |
lefcha |
1.32.2.2 |
tok = strtok_r(m, " ", &m); |
677 |
|
|
while (tok) { |
678 |
lefcha |
1.21 |
/* Reset internal fetch counter. */ |
679 |
lefcha |
1.32.2.5 |
fetch_response(&sockpri, 0, 1, NULL); |
680 |
|
|
t = imap_fetch(&sockpri, tok, s); |
681 |
lefcha |
1.32.2.1 |
do |
682 |
lefcha |
1.32.2.5 |
r = fetch_response(&sockpri, t, 0, hdrs); |
683 |
lefcha |
1.32.2.1 |
while (r == RESPONSE_NONE); |
684 |
lefcha |
1.25 |
|
685 |
lefcha |
1.32.2.1 |
if (*hdrs) { |
686 |
|
|
if (options & OPTION_HEADERS) |
687 |
|
|
info("%s\n", hdrs); |
688 |
|
|
log_info(LOG_WRITE, hdrs); |
689 |
|
|
} else { |
690 |
|
|
log_info(LOG_WRITE, NULL); |
691 |
|
|
} |
692 |
lefcha |
1.32.2.2 |
|
693 |
|
|
tok = strtok_r(NULL, " ", &m); |
694 |
lefcha |
1.21 |
} |
695 |
lefcha |
1.1 |
|
696 |
lefcha |
1.19 |
xfree(mcp); |
697 |
lefcha |
1.1 |
|
698 |
|
|
return 0; |
699 |
|
|
} |
700 |
|
|
|
701 |
|
|
|
702 |
|
|
/* |
703 |
lefcha |
1.21 |
* Count how many messages matched the filter. |
704 |
|
|
*/ |
705 |
|
|
unsigned int count_messages(char *mesgs) |
706 |
|
|
{ |
707 |
|
|
unsigned int cnt = 0; |
708 |
|
|
char *c = mesgs; |
709 |
lefcha |
1.25 |
|
710 |
lefcha |
1.21 |
while ((c = strchr(c, ' '))) { |
711 |
|
|
cnt++; |
712 |
|
|
c++; |
713 |
|
|
} |
714 |
|
|
|
715 |
|
|
return ++cnt; |
716 |
|
|
} |
717 |
|
|
|
718 |
|
|
|
719 |
|
|
/* |
720 |
lefcha |
1.11 |
* Convert messages with contiguous sequence number to the corresponding |
721 |
lefcha |
1.27 |
* number range, eg. 1 2 3 5 7 8 --> 1:3 5 7:8 and return a newly allocated |
722 |
|
|
* buffer with the results. |
723 |
lefcha |
1.1 |
*/ |
724 |
lefcha |
1.23 |
char *convert_messages(char *mesgs) |
725 |
lefcha |
1.1 |
{ |
726 |
lefcha |
1.32.2.4 |
int maxlen; |
727 |
lefcha |
1.1 |
unsigned int start, end, tmp; |
728 |
lefcha |
1.23 |
char *c, *cp, *tail; |
729 |
lefcha |
1.25 |
|
730 |
lefcha |
1.21 |
start = end = tmp = 0; |
731 |
lefcha |
1.13 |
maxlen = strlen(mesgs) + 1; |
732 |
|
|
tail = NULL; |
733 |
lefcha |
1.1 |
|
734 |
lefcha |
1.23 |
c = cp = xstrdup(mesgs); |
735 |
lefcha |
1.1 |
|
736 |
lefcha |
1.25 |
start = (unsigned int)strtoul(mesgs, &tail, 10); |
737 |
lefcha |
1.1 |
end = start; |
738 |
|
|
|
739 |
|
|
do { |
740 |
|
|
if (tail) { |
741 |
lefcha |
1.25 |
tmp = (unsigned int)strtoul(tail, &tail, 10); |
742 |
lefcha |
1.21 |
if (!tmp) |
743 |
lefcha |
1.15 |
tail = NULL; |
744 |
lefcha |
1.1 |
} |
745 |
|
|
if (tmp == end + 1) |
746 |
|
|
end++; |
747 |
|
|
else { |
748 |
lefcha |
1.13 |
if (start == end) { |
749 |
lefcha |
1.23 |
xstrncpy(c, ultostr(start, 10), maxlen); |
750 |
|
|
c += strlen(c); |
751 |
lefcha |
1.13 |
} else { |
752 |
lefcha |
1.23 |
xstrncpy(c, ultostr(start, 10), maxlen - 1); |
753 |
|
|
c += strlen(c); |
754 |
|
|
*c = ':'; |
755 |
|
|
*++c = 0; |
756 |
|
|
xstrncpy(c, ultostr(end, 10), maxlen); |
757 |
|
|
c += strlen(c); |
758 |
lefcha |
1.13 |
} |
759 |
lefcha |
1.1 |
|
760 |
lefcha |
1.23 |
if (tail && c - cp < maxlen) { |
761 |
|
|
*c = ' '; |
762 |
|
|
*++c = 0; |
763 |
lefcha |
1.13 |
} |
764 |
lefcha |
1.1 |
start = end = tmp; |
765 |
|
|
} |
766 |
|
|
} while (tmp); |
767 |
|
|
|
768 |
lefcha |
1.23 |
return cp; |
769 |
lefcha |
1.1 |
} |