/[imapfilter]/imapfilter/action.c
ViewVC logotype

Annotation of /imapfilter/action.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2.2.1 - (hide annotations)
Fri Aug 8 00:28:03 2003 UTC (20 years, 7 months ago) by lefcha
Branch: release-0_9-patches
Changes since 1.2: +1 -0 lines
File MIME type: text/plain
Corrected headers includes.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.2 #include <stdlib.h>
3 lefcha 1.1 #include <string.h>
4 lefcha 1.2.2.1 #include <sys/types.h>
5 lefcha 1.1 #include <time.h>
6    
7     #include "config.h"
8     #include "imapfilter.h"
9    
10    
11     extern conn_t connpri, connaux;
12     extern unsigned int options;
13    
14    
15     int action_delete(char *mesgs, char *args);
16     int action_copy(char *mbox, char *mesgs, char *destmbox, char *args);
17     int action_move(char *mbox, char *mesgs, char *destmbox, char *args);
18     int action_rcopy(char *mbox, char *mesgs, account_t * destacc, char *destmbox, char *args);
19     int action_rmove(char *mbox, char *mesgs, account_t * destacc, char *destmbox, char *args);
20     int action_flag(char *mesgs, unsigned int *type, unsigned int *msgflags, char *args);
21     int action_list(char *mesgs, char *args);
22    
23     unsigned int count_messages(char *mesgs);
24     char *convert_messages(char *mesgs);
25     int substitute_date(char *str);
26     void current_date(char *destmbox);
27     void message_date(char *mesg, char *destmbox);
28     void default_variables(char *mbox, char *destmbox);
29    
30    
31     /*
32     * Apply the appropriate action.
33     */
34     int
35     apply_action(char *mbox, char *mesgs, unsigned int *type, account_t * raccount,
36     char *destmbox, unsigned int *msgflags, char *args)
37     {
38     unsigned int cnt;
39    
40     if (*mesgs == '\0')
41     return 0;
42    
43     log_info(LOG_ACTION, type);
44     log_info(LOG_DESTINATION_ACCOUNT, raccount->key);
45     log_info(LOG_DESTINATION_MAILBOX, destmbox);
46    
47     cnt = count_messages(mesgs);
48    
49     switch (*type) {
50     case FILTER_ACTION_DELETE:
51     info("%d message%s deleted.\n", cnt, plural(cnt));
52     action_delete(mesgs, args);
53     break;
54     case FILTER_ACTION_COPY:
55     info("%d message%s copied from \"%s\" to mailbox \"%s\".\n",
56     cnt, plural(cnt), mbox, destmbox);
57     action_copy(mbox, mesgs, apply_namespace(destmbox,
58     connpri.nsp.prefix, connpri.nsp.delim), args);
59     break;
60     case FILTER_ACTION_MOVE:
61     info("%d message%s moved from \"%s\" to mailbox \"%s\".\n",
62     cnt, plural(cnt), mbox, destmbox);
63     action_move(mbox, mesgs, apply_namespace(destmbox,
64     connpri.nsp.prefix, connpri.nsp.delim), args);
65     break;
66     case FILTER_ACTION_RCOPY:
67     info("%d message%s copied from \"%s\" to mailbox "
68     "\"%s\" at account %s.\n", cnt, plural(cnt),
69     mbox, destmbox, raccount->key);
70     action_rcopy(mbox, mesgs, raccount, destmbox, args);
71     break;
72     case FILTER_ACTION_RMOVE:
73     info("%d message%s moved from \"%s\" to mailbox "
74     "\"%s\" at account %s.\n", cnt, plural(cnt),
75     mbox, destmbox, raccount->key);
76     action_rmove(mbox, mesgs, raccount, destmbox, args);
77     break;
78     case FILTER_ACTION_FLAG_REPLACE:
79     case FILTER_ACTION_FLAG_ADD:
80     case FILTER_ACTION_FLAG_REMOVE:
81     info("%d message%s flagged.\n", cnt, plural(cnt));
82     action_flag(mesgs, type, msgflags, args);
83     break;
84     case FILTER_ACTION_LIST:
85     info("%d message%s listed.\n", cnt, plural(cnt));
86     action_list(mesgs, args);
87     break;
88     }
89    
90     if (*args == '\0')
91     log_info(LOG_PREAMBLE, NULL);
92    
93     return 0;
94     }
95    
96    
97     /*
98     * Delete messages and optionally list some of their headers.
99     */
100     int
101     action_delete(char *mesgs, char *args)
102     {
103     char *tok, *m, *mcp;
104    
105     action_list(mesgs, args);
106    
107     m = mcp = convert_messages(mesgs);
108    
109     tok = strtok_r(m, " ", &m);
110     while (tok) {
111     server_response(&connpri, imap_store(&connpri, tok,
112     STORE_FLAG_ADD, "\\Deleted"));
113    
114     tok = strtok_r(NULL, " ", &m);
115     }
116    
117     if (options & OPTION_EXPUNGE)
118     server_response(&connpri, imap_expunge(&connpri));
119    
120     xfree(mcp);
121    
122     return 0;
123     }
124    
125    
126     /*
127     * Copy messages to specified mailbox.
128     */
129     int
130     action_copy(char *mbox, char *mesgs, char *destmbox, char *args)
131     {
132     int r;
133     char *tok, *mcp, *m;
134     char dm[2][MBOX_NAME_LEN];
135    
136     r = 0;
137     tok = NULL;
138    
139     action_list(mesgs, args);
140    
141     if (strchr(destmbox, '@'))
142     m = mcp = xstrdup(mesgs);
143     else
144     m = mcp = convert_messages(mesgs);
145    
146     xstrncpy(dm[0], destmbox, MBOX_NAME_LEN - 1);
147     default_variables(mbox, dm[0]);
148     current_date(dm[0]);
149     tok = strtok_r(m, " ", &m);
150     while (tok != NULL) {
151     xstrncpy(dm[1], dm[0], MBOX_NAME_LEN - 1);
152     message_date(tok, dm[1]);
153    
154     if ((r = copy_response(&connpri, imap_copy(&connpri, tok,
155     dm[1]))) == RESPONSE_TRYCREATE)
156     if (!server_response(&connpri, imap_create(&connpri,
157     dm[1]))) {
158     if ((options & OPTION_SUBSCRIBE))
159     server_response(&connpri,
160     imap_subscribe(&connpri, dm[1]));
161     r = copy_response(&connpri,
162     imap_copy(&connpri, tok, dm[1]));
163     }
164     tok = strtok_r(NULL, " ", &m);
165     }
166    
167     xfree(mcp);
168    
169     return r;
170     }
171    
172    
173     /*
174     * Move messages to specified mailbox.
175     */
176     int
177     action_move(char *mbox, char *mesgs, char *destmbox, char *args)
178     {
179     if (!action_copy(mbox, mesgs, destmbox, args))
180     action_delete(mesgs, "\0");
181    
182     return 0;
183     }
184    
185    
186     /*
187     * Copy messages to the specified mailbox of another mail server.
188     */
189     int
190     action_rcopy(char *mbox, char *mesgs, account_t * destacc, char *destmbox,
191     char *args)
192     {
193     int r, ta, tf;
194     char *tok, *m, *mcp, *ndm;
195     unsigned int n;
196     char buf[RESPONSE_BUF * 2 + 1];
197     char dm[3][MBOX_NAME_LEN];
198    
199     *dm[0] = *dm[1] = *dm[2] = '\0';
200    
201     if (init_connection(&connaux, destacc->server, destacc->port,
202     destacc->ssl))
203     return ERROR_NETWORK;
204    
205     r = greeting_response(&connaux);
206    
207     #ifdef DEBUG
208     test(&connaux);
209     #endif
210    
211     if (r == RESPONSE_BYE || check_capabilities(&connaux))
212     return ERROR_NETWORK;
213    
214     #ifdef SSL_TLS
215     if (destacc->ssl == SSL_DISABLED && connaux.caps & CAPABILITY_STARTTLS)
216     if (negotiate_tls(&connaux) == RESPONSE_OK)
217     check_capabilities(&connaux);
218     #endif
219    
220     if (r != RESPONSE_PREAUTH) {
221     if (destacc->passwdattr == PASSWORD_NONE) {
222     printf("Enter password for %s@%s: ", destacc->username,
223     destacc->server);
224     get_password(destacc->password, PASSWORD_LEN);
225     destacc->passwdattr = PASSWORD_PLAIN;
226     }
227     #ifdef CRAM_MD5
228     if (connaux.caps & CAPABILITY_AUTH_CRAM_MD5)
229     r = auth_cram_md5(&connaux, destacc->username,
230     destacc->password);
231     else
232     #endif
233     r = login(&connaux, destacc->username,
234     destacc->password);
235    
236     if (r == RESPONSE_NO) {
237     error("username %s or password rejected at %s\n",
238     destacc->username, destacc->server);
239     return ERROR_NETWORK;
240     }
241     }
242     check_namespace(&connaux);
243    
244     m = mcp = xstrdup(mesgs);
245    
246     xstrncpy(dm[1], destmbox, MBOX_NAME_LEN - 1);
247     current_date(dm[1]);
248    
249     tok = strtok_r(m, " ", &m);
250     while (tok != NULL) {
251     xstrncpy(dm[2], dm[1], MBOX_NAME_LEN - 1);
252     message_date(tok, dm[2]);
253    
254     /* apply_namespace() returns a pointer to a static buffer. */
255     ndm = apply_namespace(dm[2], connaux.nsp.prefix,
256     connaux.nsp.delim);
257    
258     /* Check only if mailbox name is different from last one. */
259     if (strncmp(dm[0], dm[2], strlen(dm[2]))) {
260     r = check_mailbox(&connaux, ndm);
261     if (r == RESPONSE_NO) {
262     server_response(&connaux,
263     imap_create(&connaux, ndm));
264     if ((options & OPTION_SUBSCRIBE))
265     server_response(&connaux,
266     imap_subscribe(&connaux, ndm));
267     }
268     }
269     xstrncpy(dm[0], dm[2], MBOX_NAME_LEN - 1);
270    
271     fetchsize_response(&connpri, &n,
272     imap_fetch(&connpri, tok, "RFC822.SIZE"));
273    
274     ta = imap_append(&connaux, ndm, n);
275    
276     fetch_response(&connpri, 0, 1, NULL);
277     tf = imap_fetch(&connpri, tok, "RFC822.HEADER");
278     do {
279     r = fetch_response(&connpri, tf, 0, buf);
280     socket_write(&connaux, buf);
281     } while (r == RESPONSE_NONE);
282    
283     socket_write(&connaux, "\r\n");
284    
285     fetch_response(&connpri, 0, 1, NULL);
286     tf = imap_fetch(&connpri, tok, "BODY[TEXT]");
287     do {
288     r = fetch_response(&connpri, tf, 0, buf);
289     if (r != RESPONSE_NULLBODY)
290     socket_write(&connaux, buf);
291     } while (r == RESPONSE_NONE);
292    
293     if (r != RESPONSE_NULLBODY)
294     socket_write(&connaux, "\r\n\r\n");
295     else
296     socket_write(&connaux, "\r\n");
297    
298     append_response(&connaux, ta);
299    
300     tok = strtok_r(NULL, " ", &m);
301     }
302    
303     logout(&connaux);
304    
305     action_list(mesgs, args);
306    
307     xfree(mcp);
308    
309     return 0;
310     }
311    
312    
313     /*
314     * Move messages to the specified mailbox of another mail server.
315     */
316     int
317     action_rmove(char *mbox, char *mesgs, account_t * destacc, char *destmbox,
318     char *args)
319     {
320     if (!action_rcopy(mbox, mesgs, destacc, destmbox, args))
321     action_delete(mesgs, "\0");
322    
323     return 0;
324     }
325    
326    
327     /*
328     * Flag messages by replacing, adding or removing specified flags.
329     */
330     int
331     action_flag(char *mesgs, unsigned int *type, unsigned int *msgflags,
332     char *args)
333     {
334     unsigned int t;
335     char s[STORE_FLAGS_BUF];
336     char *tok, *m, *mcp;
337    
338     *s = 0;
339    
340     switch (*type) {
341     case FILTER_ACTION_FLAG_ADD:
342     t = STORE_FLAG_ADD;
343     break;
344     case FILTER_ACTION_FLAG_REMOVE:
345     t = STORE_FLAG_REMOVE;
346     break;
347     default:
348     t = STORE_FLAG_REPLACE;
349     }
350    
351     if ((*msgflags != MESSAGE_FLAG_NONE)) {
352     if ((*msgflags & MESSAGE_FLAG_SEEN))
353     strncat(s, "\\Seen ", STORE_FLAGS_BUF - strlen(s) - 1);
354     if ((*msgflags & MESSAGE_FLAG_ANSWERED))
355     strncat(s, "\\Answered ",
356     STORE_FLAGS_BUF - strlen(s) - 1);
357     if ((*msgflags & MESSAGE_FLAG_FLAGGED))
358     strncat(s, "\\Flagged ",
359     STORE_FLAGS_BUF - strlen(s) - 1);
360     if ((*msgflags & MESSAGE_FLAG_DELETED))
361     strncat(s, "\\Deleted ",
362     STORE_FLAGS_BUF - strlen(s) - 1);
363     if ((*msgflags & MESSAGE_FLAG_DRAFT))
364     strncat(s, "\\Draft", STORE_FLAGS_BUF - strlen(s) - 1);
365     if ((s[strlen(s) - 1] == ' '))
366     s[strlen(s) - 1] = 0;
367     }
368     action_list(mesgs, args);
369    
370     m = mcp = convert_messages(mesgs);
371    
372     tok = strtok_r(m, " ", &m);
373     while (tok != NULL) {
374     server_response(&connpri, imap_store(&connpri, tok, t, s));
375    
376     tok = strtok_r(NULL, " ", &m);
377     }
378    
379     if (options & OPTION_EXPUNGE)
380     server_response(&connpri, imap_expunge(&connpri));
381    
382     xfree(mcp);
383    
384     return 0;
385     }
386    
387     /*
388     * List user selected headers of messages.
389     */
390     int
391     action_list(char *mesgs, char *args)
392     {
393     int r, t;
394     char *tok, *mcp, *m;
395     char s[ARGS_LEN + 27];
396     char hdrs[RESPONSE_BUF * 2 + 1];
397    
398     if (*args == '\0')
399     return 0;
400    
401     m = mcp = xstrdup(mesgs);
402    
403     snprintf(s, ARGS_LEN + 27 - 1, "BODY.PEEK[HEADER.FIELDS (%s)]", args);
404    
405     tok = strtok_r(m, " ", &m);
406     while (tok != NULL) {
407     /* Reset internal fetch counter. */
408     fetch_response(&connpri, 0, 1, NULL);
409     t = imap_fetch(&connpri, tok, s);
410    
411     log_info(LOG_PREAMBLE, NULL);
412     do {
413     r = fetch_response(&connpri, t, 0, hdrs);
414    
415     if (*hdrs != '\0') {
416     if (options & OPTION_HEADERS)
417     info("%s\n", hdrs);
418     log_info(LOG_HEADER, hdrs);
419     }
420     } while (r == RESPONSE_NONE);
421    
422     tok = strtok_r(NULL, " ", &m);
423     }
424    
425     xfree(mcp);
426    
427     return 0;
428     }
429    
430    
431     /*
432     * Count how many messages matched the filter.
433     */
434     unsigned int
435     count_messages(char *mesgs)
436     {
437     unsigned int cnt;
438     char *c;
439    
440     cnt = 0;
441     c = mesgs;
442    
443     while ((c = strchr(c, ' '))) {
444     cnt++;
445     c++;
446     }
447    
448     return ++cnt;
449     }
450    
451    
452     /*
453     * Convert messages with contiguous sequence number to the corresponding
454     * number range, eg. 1 2 3 5 7 8 --> 1:3 5 7:8 and return a newly allocated
455     * buffer with the results.
456     */
457     char *
458     convert_messages(char *mesgs)
459     {
460     int maxlen;
461     unsigned int start, end, tmp;
462     char *c, *cp, *tail;
463    
464     start = end = tmp = 0;
465     maxlen = strlen(mesgs) + 1;
466     tail = NULL;
467    
468     c = cp = xstrdup(mesgs);
469    
470     start = (unsigned int)strtoul(mesgs, &tail, 10);
471     end = start;
472    
473     do {
474     if (tail != NULL) {
475     tmp = (unsigned int)strtoul(tail, &tail, 10);
476     if (tmp == 0)
477     tail = NULL;
478     }
479     if (tmp == end + 1)
480     end++;
481     else {
482     if (start == end) {
483     xstrncpy(c, ultostr(start, 10), maxlen);
484     c += strlen(c);
485     } else {
486     xstrncpy(c, ultostr(start, 10), maxlen - 1);
487     c += strlen(c);
488     *c = ':';
489     *++c = 0;
490     xstrncpy(c, ultostr(end, 10), maxlen);
491     c += strlen(c);
492     }
493    
494     if (tail != NULL && c - cp < maxlen) {
495     *c = ' ';
496     *++c = 0;
497     }
498     start = end = tmp;
499     }
500     } while (tmp != 0);
501    
502     return cp;
503     }
504    
505    
506     /*
507     * Substitute all occurences of the '@' character with the '%' character,
508     * and any double '@' characters with a signle '@' character, returning the
509     * number of replacements.
510     */
511     int
512     substitute_date(char *str)
513     {
514     int n;
515     char *r, *w, *s;
516    
517     n = 0;
518    
519     s = xstrdup(str);
520    
521     for (r = s, w = str; *r != '\0'; r++, w++) {
522     if (*r == '%') {
523     *w++ = '%';
524     *w = '%';
525     } else if (*r == '@') {
526     if (*(r + 1) == '@') {
527     *w = *r++;
528     } else {
529     *w = '%';
530     n++;
531     }
532     } else {
533     *w = *r;
534     }
535     }
536     *w = '\0';
537    
538     xfree(s);
539    
540     return n;
541     }
542    
543    
544     /*
545     * Format the destination mailbox according to the current date conversion
546     * specifiers.
547     */
548     void
549     current_date(char *destmbox)
550     {
551     char s[MBOX_NAME_LEN];
552     time_t te;
553     struct tm *tl;
554    
555     if (!strchr(destmbox, '%'))
556     return;
557    
558     te = time(NULL);
559     tl = localtime(&te);
560    
561     if (strftime(s, MBOX_NAME_LEN - 1, destmbox, tl))
562     xstrncpy(destmbox, s, MBOX_NAME_LEN - 1);
563     }
564    
565    
566     /*
567     * Format the destination mailbox according to the message date conversion
568     * specifiers.
569     */
570     void
571     message_date(char *mesg, char *destmbox)
572     {
573     unsigned int t;
574     char s[MBOX_NAME_LEN];
575     struct tm tl;
576     char dbuf[RESPONSE_BUF + 1];
577    
578     if (!strchr(destmbox, '@'))
579     return;
580    
581     substitute_date(destmbox);
582    
583     fetch_response(&connpri, 0, 1, NULL);
584     t = imap_fetch(&connpri, mesg, "BODY.PEEK[HEADER.FIELDS (DATE)]");
585    
586     while (fetch_response(&connpri, t, 0, dbuf) == RESPONSE_NONE);
587    
588     if (strptime(dbuf, "Date: %a, %d %b %Y %H:%M:%S", &tl) &&
589     strftime(s, MBOX_NAME_LEN - 1, destmbox, &tl))
590     xstrncpy(destmbox, s, MBOX_NAME_LEN - 1);
591     }
592    
593    
594     /*
595     * Format the destination mailbox according to "default variables" specifiers.
596     */
597     void
598     default_variables(char *mbox, char *destmbox)
599     {
600     char *m, *r, *w, *s;
601    
602     if (!strchr(destmbox, '$'))
603     return;
604    
605     s = xstrdup(destmbox);
606    
607     for (r = s, w = destmbox; *r != '\0';) {
608     if (w - destmbox >= MBOX_NAME_LEN - 1)
609     break;
610     if (*r == '$') {
611     switch (*(r + 1)) {
612     case '_':
613     if (w + strlen(mbox) - destmbox >
614     MBOX_NAME_LEN - 1) {
615     r += 2;
616     break;
617     }
618     for (m = mbox; *m != '\0'; m++, w++)
619     *w = *m;
620     r += 2;
621     break;
622     case '$':
623     *w++ = '$';
624     r += 2;
625     break;
626     default:
627     *w++ = *r++;
628     break;
629     }
630     } else {
631     *w++ = *r++;
632     }
633     }
634     *w = '\0';
635    
636     xfree(s);
637     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26