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

Contents of /imapfilter/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (show annotations)
Sat Mar 15 16:05:03 2003 UTC (21 years ago) by lefcha
Branch: MAIN
Changes since 1.46: +65 -16 lines
File MIME type: text/plain
Added default variables support for the destination mailbox.

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <time.h>
6
7 #include "config.h"
8 #include "imapfilter.h"
9 #include "data.h"
10
11
12 extern int sockpri, sockaux;
13 extern unsigned int options;
14 extern char charset[CHARSET_LEN];
15 extern unsigned int capabilities;
16
17 namesp_t nsppri, nspaux; /* Primary and auxiliary namespace. */
18
19
20 #ifdef DEBUG
21 /*
22 * Test/ping server.
23 */
24 int
25 test(int *sock)
26 {
27 return server_response(sock, imap_noop(sock));
28 }
29
30 #endif
31
32
33 /*
34 * Check server's capabilities.
35 */
36 int
37 check_capabilities(int *sock)
38 {
39 capabilities = CAPABILITY_NONE;
40
41 return capability_response(sock, imap_capability(sock));
42 }
43
44
45 /*
46 * Get namespace of mail server's mailboxes.
47 */
48 int
49 check_namespace(int *sock, namesp_t * nsp)
50 {
51 nsp->prefix[0] = nsp->delim = '\0';
52
53 if (!(options & OPTION_NAMESPACE) ||
54 !(capabilities & CAPABILITY_NAMESPACE))
55 return 0;
56 else
57 return namespace_response(sock, imap_namespace(sock), nsp);
58 }
59
60
61 /*
62 * Login to server.
63 */
64 int
65 login(int *sock, char *user, char *pass)
66 {
67 return server_response(sock, imap_login(sock, user, pass));
68 }
69
70
71
72 /*
73 * Check if a mailbox exists.
74 */
75 int
76 check_mailbox(int *sock, char *mbox)
77 {
78 return server_response(sock, imap_status(sock, mbox, "MESSAGES"));
79 }
80
81
82 /*
83 * Open mailbox in read-write mode.
84 */
85 int
86 select_mailbox(int *sock, char *mbox, namesp_t * nsp)
87 {
88 int r;
89
90 if (mailbox_status(sock, mbox, nsp) == -2)
91 return -2; /* No messages exist. No filters need to be
92 * applied. */
93
94 r = select_response(sock, imap_select(sock,
95 apply_namespace(mbox, nsp->prefix,
96 nsp->delim)));
97
98 log_info(LOG_MAILBOX, mbox);
99
100 return r;
101 }
102
103
104 /*
105 * Get mailbox's status.
106 */
107 int
108 mailbox_status(int *sock, char *mbox, namesp_t * nsp)
109 {
110 return status_response(sock, imap_status(sock,
111 apply_namespace(mbox, nsp->prefix, nsp->delim),
112 "MESSAGES RECENT UNSEEN"), mbox);
113 }
114
115
116 /*
117 * Close examined/selected mailbox.
118 */
119 int
120 close_mailbox(int *sock)
121 {
122 return server_response(sock, imap_close(sock));
123 }
124
125
126 /*
127 * Logout from server.
128 */
129 int
130 logout(int *sock)
131 {
132 return logout_response(sock, imap_logout(sock));
133 }
134
135
136 /*
137 * Match and apply filters assigned to a mailbox.
138 */
139 int
140 apply_filters(char *mbox, filter_t ** filters)
141 {
142 int i;
143 char *mesgs;
144
145 for (i = 0; filters[i] != NULL; i++) {
146 mesgs = NULL;
147
148 if (match_filter(filters[i], &mesgs))
149 continue;
150
151 log_info(LOG_FILTER, filters[i]->key);
152
153 apply_action(mbox, mesgs, &(filters[i]->action.type),
154 filters[i]->action.raccount, filters[i]->action.destmbox,
155 &filters[i]->action.msgflags, filters[i]->action.args);
156
157 xfree(mesgs);
158 }
159
160 return 0;
161 }
162
163
164 /*
165 * Generate the search request by the masks of the filter and try to
166 * match the generated filter.
167 */
168 int
169 match_filter(filter_t * filter, char **mesgs)
170 {
171 char *search;
172
173 if (filter->mode == FILTER_MODE_OR)
174 search = generate_filter_or(filter->masks, filter->masknum,
175 filter->masklen);
176 else
177 search = generate_filter_and(filter->masks, filter->masknum,
178 filter->masklen);
179
180 search_response(&sockpri, imap_search(&sockpri, charset, search),
181 mesgs);
182
183 xfree(search);
184
185 if (*mesgs == '\0')
186 return 1;
187
188 return 0;
189 }
190
191
192 /*
193 * Empty the FIFO inventory.
194 */
195 void
196 empty_fifo(mask_t ** mfifo)
197 {
198 mfifo[0] = NULL;
199
200 queue_fifo(NULL, NULL);
201 dequeue_fifo(NULL);
202 }
203
204
205 /*
206 * Add item to FIFO inventory.
207 */
208 void
209 queue_fifo(mask_t ** mfifo, mask_t * mask)
210 {
211 static unsigned int i;
212
213 if (mfifo == NULL) {
214 i = 0;
215 return;
216 }
217 mfifo[i++] = mask;
218 mfifo[i] = NULL;
219 }
220
221
222 /*
223 * Get next item from FIFO inventory.
224 */
225 mask_t *
226 dequeue_fifo(mask_t ** mfifo)
227 {
228 static unsigned int j;
229
230 if (mfifo == NULL) {
231 j = 0;
232 return NULL;
233 }
234 return mfifo[j++];
235 }
236
237
238 /*
239 * Generate the filter search command from the masks, assuming that
240 * masks are AND-ed.
241 */
242 char *
243 generate_filter_and(mask_t * mask, unsigned int masknum,
244 unsigned int masklen)
245 {
246 const unsigned int searchbuf = masklen + masknum * 6 + 8;
247 unsigned int len;
248 char *search;
249 mask_t *tmp;
250
251 len = 0;
252
253 search = (char *)xmalloc(sizeof(char) * searchbuf);
254 search[0] = '\0';
255
256 tmp = mask;
257 if (tmp == NULL) {
258 strncat(search, "ALL ", searchbuf - len - 1);
259 len += 4;
260 } else
261 while ((tmp = tmp->next) != NULL) {
262 if (tmp->type != MASK_TYPE_OR) {
263 strncat(search, "ALL ", searchbuf - len - 1);
264 len += 4;
265 break;
266 }
267 }
268
269 tmp = NULL;
270 while (mask != NULL) {
271 tmp = mask;
272 mask = mask->next;
273
274 if (mask != NULL && mask->type == MASK_TYPE_OR) {
275 strncat(search, "OR (", searchbuf - len - 1);
276 len += 4;
277
278 strncat(search, tmp->body, searchbuf - len - 1);
279 len = strlen(search);
280 search[len] = ' ';
281 search[++len] = '\0';
282
283 search[len - 1] = ')';
284 search[len] = ' ';
285 search[++len] = '\0';
286
287 if (mask->next == NULL ||
288 mask->next->type != MASK_TYPE_OR) {
289 search[len] = '(';
290 search[++len] = '\0';
291 strncat(search, mask->body,
292 searchbuf - len - 1);
293 len = strlen(search);
294 search[len] = ')';
295 search[++len] = ' ';
296 search[++len] = '\0';
297 mask = mask->next;
298 }
299 } else {
300 strncat(search, tmp->body, searchbuf - len - 1);
301 len = strlen(search);
302 search[len] = ' ';
303 search[++len] = '\0';
304 }
305 }
306
307 search[len - 1] = '\0';
308
309 return search;
310 }
311
312
313 /*
314 * Generate the filter search command from the masks, assuming that
315 * masks are OR-ed.
316 */
317 char *
318 generate_filter_or(mask_t * mask, unsigned int masknum,
319 unsigned int masklen)
320 {
321 const unsigned int searchbuf = masklen + masknum * 6 + 8;
322 unsigned int len;
323 char *search;
324 mask_t **mfifo; /* Mailbox FIFO queue. */
325 mask_t *mf; /* Mask returned from FIFO. */
326
327 len = 0;
328
329 search = (char *)xmalloc(sizeof(char) * searchbuf);
330 mfifo = (mask_t **) xmalloc(sizeof(mask_t *) * (masknum + 1));
331
332 search[0] = '\0';
333 empty_fifo(mfifo);
334
335 strncat(search, "ALL ", searchbuf - len - 1);
336 len += 4;
337
338 while (mask != NULL) {
339 queue_fifo(mfifo, mask);
340 mask = mask->next;
341
342 while (mask != NULL && mask->type == MASK_TYPE_AND) {
343 queue_fifo(mfifo, mask);
344 mask = mask->next;
345 }
346
347 if (mask != NULL) {
348 if (len == 4 && search[0] == 'A') {
349 search[0] = '\0';
350 len = 0;
351 }
352 strncat(search, "OR ", searchbuf - len - 1);
353 len += 3;
354 }
355 if (search[0] != 'A') {
356 search[len] = '(';
357 search[++len] = '\0';
358 }
359 while ((mf = dequeue_fifo(mfifo)) != NULL) {
360 strncat(search, mf->body, searchbuf - len - 1);
361 len = strlen(search);
362 search[len] = ' ';
363 search[++len] = '\0';
364 }
365
366 if (strchr(search, '(')) {
367 search[len - 1] = ')';
368 search[len] = ' ';
369 search[++len] = '\0';
370 }
371 empty_fifo(mfifo);
372 }
373
374 search[len - 1] = '\0';
375
376 xfree(mfifo);
377
378 return search;
379 }
380
381
382 /*
383 * Apply the appropriate action.
384 */
385 int
386 apply_action(char *mbox, char *mesgs, unsigned int *type, account_t * raccount,
387 char *destmbox, unsigned int *msgflags, char *args)
388 {
389 unsigned int cnt;
390
391 if (*mesgs == '\0')
392 return 0;
393
394 log_info(LOG_ACTION, type);
395 log_info(LOG_DESTINATION_ACCOUNT, raccount->key);
396 log_info(LOG_DESTINATION_MAILBOX, destmbox);
397
398 cnt = count_messages(mesgs);
399
400 switch (*type) {
401 case FILTER_ACTION_DELETE:
402 info("%d message%s deleted.\n", cnt, plural(cnt));
403 action_delete(mesgs, args);
404 break;
405 case FILTER_ACTION_COPY:
406 info("%d message%s copied to mailbox \"%s\".\n", cnt,
407 plural(cnt), destmbox);
408 action_copy(mbox, mesgs, apply_namespace(destmbox,
409 nsppri.prefix, nsppri.delim), args);
410 break;
411 case FILTER_ACTION_MOVE:
412 info("%d message%s moved to mailbox \"%s\".\n", cnt,
413 plural(cnt), destmbox);
414 action_move(mbox, mesgs, apply_namespace(destmbox,
415 nsppri.prefix, nsppri.delim), args);
416 break;
417 case FILTER_ACTION_RCOPY:
418 info("%d message%s copied to mailbox \"%s\" at account %s.\n",
419 cnt, plural(cnt), destmbox, raccount->key);
420 action_rcopy(mbox, mesgs, raccount, destmbox, args);
421 break;
422 case FILTER_ACTION_RMOVE:
423 info("%d message%s moved to mailbox \"%s\" at account %s.\n",
424 cnt, plural(cnt), destmbox, raccount->key);
425 action_rmove(mbox, mesgs, raccount, destmbox, args);
426 break;
427 case FILTER_ACTION_FLAG_REPLACE:
428 case FILTER_ACTION_FLAG_ADD:
429 case FILTER_ACTION_FLAG_REMOVE:
430 info("%d message%s flagged.\n", cnt, plural(cnt));
431 action_flag(mesgs, type, msgflags, args);
432 break;
433 case FILTER_ACTION_LIST:
434 info("%d message%s listed.\n", cnt, plural(cnt));
435 action_list(mesgs, args);
436 break;
437 }
438
439 if (*args == '\0')
440 log_info(LOG_WRITE, NULL);
441
442 return 0;
443 }
444
445
446 /*
447 * Delete messages and optionally list some of their headers.
448 */
449 int
450 action_delete(char *mesgs, char *args)
451 {
452 char *tok, *m, *mcp;
453
454 action_list(mesgs, args);
455
456 m = mcp = convert_messages(mesgs);
457
458 tok = strtok_r(m, " ", &m);
459 while (tok) {
460 server_response(&sockpri, imap_store(&sockpri, tok,
461 STORE_FLAG_ADD, "\\Deleted"));
462
463 tok = strtok_r(NULL, " ", &m);
464 }
465
466 if (options & OPTION_EXPUNGE)
467 server_response(&sockpri, imap_expunge(&sockpri));
468
469 xfree(mcp);
470
471 return 0;
472 }
473
474
475 /*
476 * Copy messages to specified mailbox.
477 */
478 int
479 action_copy(char *mbox, char *mesgs, char *destmbox, char *args)
480 {
481 int r;
482 char *tok, *mcp, *m;
483 char dm[2][MBOX_NAME_LEN];
484
485 r = 0;
486 tok = NULL;
487
488 action_list(mesgs, args);
489
490 if (strchr(destmbox, '@'))
491 m = mcp = xstrdup(mesgs);
492 else
493 m = mcp = convert_messages(mesgs);
494
495 xstrncpy(dm[0], destmbox, MBOX_NAME_LEN - 1);
496 default_variables(mbox, dm[0]);
497 current_date(dm[0]);
498 tok = strtok_r(m, " ", &m);
499 while (tok != NULL) {
500 xstrncpy(dm[1], dm[0], MBOX_NAME_LEN - 1);
501 message_date(tok, dm[1]);
502
503 if ((r = copy_response(&sockpri, imap_copy(&sockpri, tok,
504 dm[1]))) == RESPONSE_TRYCREATE)
505 if (!server_response(&sockpri, imap_create(&sockpri,
506 dm[1]))) {
507 if ((options & OPTION_SUBSCRIBE))
508 server_response(&sockpri,
509 imap_subscribe(&sockpri, dm[1]));
510 r = copy_response(&sockpri,
511 imap_copy(&sockpri, tok, dm[1]));
512 }
513 tok = strtok_r(NULL, " ", &m);
514 }
515
516 xfree(mcp);
517
518 return r;
519 }
520
521
522 /*
523 * Move messages to specified mailbox.
524 */
525 int
526 action_move(char *mbox, char *mesgs, char *destmbox, char *args)
527 {
528 if (!action_copy(mbox, mesgs, destmbox, args))
529 action_delete(mesgs, "\0");
530
531 return 0;
532 }
533
534
535 /*
536 * Copy messages to the specified mailbox of another mail server.
537 */
538 int
539 action_rcopy(char *mbox, char *mesgs, account_t * destacc, char *destmbox,
540 char *args)
541 {
542 int r, ta, tf;
543 char *tok, *m, *mcp, *ndm;
544 unsigned int n;
545 char buf[RESPONSE_BUF * 2];
546 char dm[3][MBOX_NAME_LEN];
547
548 *dm[0] = *dm[1] = *dm[2] = '\0';
549
550 if (init_connection(&sockaux, destacc->server, destacc->port,
551 destacc->ssl))
552 return ERROR_NETWORK;
553
554 r = greeting_response(&sockaux);
555
556 if (r == RESPONSE_BYE || check_capabilities(&sockaux))
557 return ERROR_NETWORK;
558
559 #ifdef DEBUG
560 test(&sockaux);
561 #endif
562
563 if (r != RESPONSE_PREAUTH) {
564 if (destacc->passwdattr == PASSWORD_NONE) {
565 printf("Enter password for %s@%s: ", destacc->username,
566 destacc->server);
567 get_password(destacc->password, PASSWORD_LEN);
568 destacc->passwdattr = PASSWORD_PLAIN;
569 }
570 if (login(&sockaux, destacc->username,
571 destacc->password) == RESPONSE_NO) {
572 error("username %s or password rejected at %s\n",
573 destacc->username, destacc->server);
574 return ERROR_NETWORK;
575 }
576 }
577 check_namespace(&sockaux, &nspaux);
578
579 m = mcp = xstrdup(mesgs);
580
581 xstrncpy(dm[1], destmbox, MBOX_NAME_LEN - 1);
582 current_date(dm[1]);
583
584 tok = strtok_r(m, " ", &m);
585 while (tok != NULL) {
586 xstrncpy(dm[2], dm[1], MBOX_NAME_LEN - 1);
587 message_date(tok, dm[2]);
588
589 /* apply_namespace() returns a pointer to a static buffer. */
590 ndm = apply_namespace(dm[2], nspaux.prefix, nspaux.delim);
591
592 /* Check only if mailbox name is different from last one. */
593 if (strncmp(dm[0], dm[2], strlen(dm[2]))) {
594 r = check_mailbox(&sockaux, ndm);
595 if (r == RESPONSE_NO) {
596 server_response(&sockaux,
597 imap_create(&sockaux, ndm));
598 if ((options & OPTION_SUBSCRIBE))
599 server_response(&sockaux,
600 imap_subscribe(&sockaux, ndm));
601 }
602 }
603 xstrncpy(dm[0], dm[2], MBOX_NAME_LEN - 1);
604
605 fetchsize_response(&sockpri, &n,
606 imap_fetch(&sockpri, tok, "RFC822.SIZE"));
607
608 ta = imap_append(&sockaux, ndm, n);
609
610 fetch_response(&sockpri, 0, 1, NULL);
611 tf = imap_fetch(&sockpri, tok, "RFC822.HEADER");
612 do {
613 r = fetch_response(&sockpri, tf, 0, buf);
614 socket_write(&sockaux, buf);
615 } while (r == RESPONSE_NONE);
616
617 socket_write(&sockaux, "\r\n");
618
619 fetch_response(&sockpri, 0, 1, NULL);
620 tf = imap_fetch(&sockpri, tok, "BODY[TEXT]");
621 do {
622 r = fetch_response(&sockpri, tf, 0, buf);
623 if (r != RESPONSE_NULLBODY)
624 socket_write(&sockaux, buf);
625 } while (r == RESPONSE_NONE);
626
627 if (r != RESPONSE_NULLBODY)
628 socket_write(&sockaux, "\r\n\r\n");
629 else
630 socket_write(&sockaux, "\r\n");
631
632 append_response(&sockaux, ta);
633
634 tok = strtok_r(NULL, " ", &m);
635 }
636
637 logout(&sockaux);
638
639 action_list(mesgs, args);
640
641 xfree(mcp);
642
643 return 0;
644 }
645
646
647 /*
648 * Move messages to the specified mailbox of another mail server.
649 */
650 int
651 action_rmove(char *mbox, char *mesgs, account_t * destacc, char *destmbox,
652 char *args)
653 {
654 if (!action_rcopy(mbox, mesgs, destacc, destmbox, args))
655 action_delete(mesgs, "\0");
656
657 return 0;
658 }
659
660
661 /*
662 * Flag messages by replacing, adding or removing specified flags.
663 */
664 int
665 action_flag(char *mesgs, unsigned int *type, unsigned int *msgflags,
666 char *args)
667 {
668 unsigned int t;
669 char s[STORE_FLAGS_BUF];
670 char *tok, *m, *mcp;
671
672 *s = 0;
673
674 switch (*type) {
675 case FILTER_ACTION_FLAG_ADD:
676 t = STORE_FLAG_ADD;
677 break;
678 case FILTER_ACTION_FLAG_REMOVE:
679 t = STORE_FLAG_REMOVE;
680 break;
681 default:
682 t = STORE_FLAG_REPLACE;
683 }
684
685 if ((*msgflags != MESSAGE_FLAG_NONE)) {
686 if ((*msgflags & MESSAGE_FLAG_SEEN))
687 strncat(s, "\\Seen ", STORE_FLAGS_BUF - strlen(s) - 1);
688 if ((*msgflags & MESSAGE_FLAG_ANSWERED))
689 strncat(s, "\\Answered ",
690 STORE_FLAGS_BUF - strlen(s) - 1);
691 if ((*msgflags & MESSAGE_FLAG_FLAGGED))
692 strncat(s, "\\Flagged ",
693 STORE_FLAGS_BUF - strlen(s) - 1);
694 if ((*msgflags & MESSAGE_FLAG_DELETED))
695 strncat(s, "\\Deleted ",
696 STORE_FLAGS_BUF - strlen(s) - 1);
697 if ((*msgflags & MESSAGE_FLAG_DRAFT))
698 strncat(s, "\\Draft", STORE_FLAGS_BUF - strlen(s) - 1);
699 if ((s[strlen(s) - 1] == ' '))
700 s[strlen(s) - 1] = 0;
701 }
702 action_list(mesgs, args);
703
704 m = mcp = convert_messages(mesgs);
705
706 tok = strtok_r(m, " ", &m);
707 while (tok != NULL) {
708 server_response(&sockpri, imap_store(&sockpri, tok, t, s));
709
710 tok = strtok_r(NULL, " ", &m);
711 }
712
713 if (options & OPTION_EXPUNGE)
714 server_response(&sockpri, imap_expunge(&sockpri));
715
716 xfree(mcp);
717
718 return 0;
719 }
720
721 /*
722 * List user selected headers of messages.
723 */
724 int
725 action_list(char *mesgs, char *args)
726 {
727 int r, t;
728 char *tok, *mcp, *m;
729 char s[ARGS_LEN + 27];
730 char hdrs[RESPONSE_BUF];
731
732 if (*args == '\0')
733 return 0;
734
735 m = mcp = xstrdup(mesgs);
736
737 snprintf(s, ARGS_LEN + 27 - 1, "BODY.PEEK[HEADER.FIELDS (%s)]", args);
738
739 tok = strtok_r(m, " ", &m);
740 while (tok != NULL) {
741 /* Reset internal fetch counter. */
742 fetch_response(&sockpri, 0, 1, NULL);
743 t = imap_fetch(&sockpri, tok, s);
744 do {
745 r = fetch_response(&sockpri, t, 0, hdrs);
746 } while (r == RESPONSE_NONE);
747
748 if (*hdrs != '\0') {
749 if (options & OPTION_HEADERS)
750 info("%s\n", hdrs);
751 log_info(LOG_WRITE, hdrs);
752 } else {
753 log_info(LOG_WRITE, NULL);
754 }
755
756 tok = strtok_r(NULL, " ", &m);
757 }
758
759 xfree(mcp);
760
761 return 0;
762 }
763
764
765 /*
766 * Count how many messages matched the filter.
767 */
768 unsigned int
769 count_messages(char *mesgs)
770 {
771 unsigned int cnt;
772 char *c;
773
774 cnt = 0;
775 c = mesgs;
776
777 while ((c = strchr(c, ' '))) {
778 cnt++;
779 c++;
780 }
781
782 return ++cnt;
783 }
784
785
786 /*
787 * Convert messages with contiguous sequence number to the corresponding
788 * number range, eg. 1 2 3 5 7 8 --> 1:3 5 7:8 and return a newly allocated
789 * buffer with the results.
790 */
791 char *
792 convert_messages(char *mesgs)
793 {
794 int maxlen;
795 unsigned int start, end, tmp;
796 char *c, *cp, *tail;
797
798 start = end = tmp = 0;
799 maxlen = strlen(mesgs) + 1;
800 tail = NULL;
801
802 c = cp = xstrdup(mesgs);
803
804 start = (unsigned int)strtoul(mesgs, &tail, 10);
805 end = start;
806
807 do {
808 if (tail != NULL) {
809 tmp = (unsigned int)strtoul(tail, &tail, 10);
810 if (tmp == 0)
811 tail = NULL;
812 }
813 if (tmp == end + 1)
814 end++;
815 else {
816 if (start == end) {
817 xstrncpy(c, ultostr(start, 10), maxlen);
818 c += strlen(c);
819 } else {
820 xstrncpy(c, ultostr(start, 10), maxlen - 1);
821 c += strlen(c);
822 *c = ':';
823 *++c = 0;
824 xstrncpy(c, ultostr(end, 10), maxlen);
825 c += strlen(c);
826 }
827
828 if (tail != NULL && c - cp < maxlen) {
829 *c = ' ';
830 *++c = 0;
831 }
832 start = end = tmp;
833 }
834 } while (tmp != 0);
835
836 return cp;
837 }
838
839
840 /*
841 * Substitute all occurences of the '@' character with the '%' character,
842 * and any double '@' characters with a signle '@' character, returning the
843 * number of replacements.
844 */
845 int
846 substitute_date(char *str)
847 {
848 int n;
849 char *r, *w, *s;
850
851 n = 0;
852
853 s = xstrdup(str);
854
855 for (r = s, w = str; *r != '\0'; r++, w++) {
856 if (*r == '%') {
857 *w++ = '%';
858 *w = '%';
859 } else if (*r == '@') {
860 if (*(r + 1) == '@') {
861 *w = *r++;
862 } else {
863 *w = '%';
864 n++;
865 }
866 } else {
867 *w = *r;
868 }
869 }
870 *w = '\0';
871
872 xfree(s);
873
874 return n;
875 }
876
877
878 /*
879 * Format the destination mailbox according to the current date conversion
880 * specifiers.
881 */
882 void
883 current_date(char *destmbox)
884 {
885 char s[MBOX_NAME_LEN];
886 time_t te;
887 struct tm *tl;
888
889 if (!strchr(destmbox, '%'))
890 return;
891
892 te = time(NULL);
893 tl = localtime(&te);
894
895 if (strftime(s, MBOX_NAME_LEN - 1, destmbox, tl))
896 xstrncpy(destmbox, s, MBOX_NAME_LEN - 1);
897 }
898
899
900 /*
901 * Format the destination mailbox according to the message date conversion
902 * specifiers.
903 */
904 void
905 message_date(char *mesg, char *destmbox)
906 {
907 unsigned int t;
908 char s[MBOX_NAME_LEN];
909 struct tm tl;
910 char dbuf[RESPONSE_BUF];
911
912 if (!strchr(destmbox, '@'))
913 return;
914
915 substitute_date(destmbox);
916
917 fetch_response(&sockpri, 0, 1, NULL);
918 t = imap_fetch(&sockpri, mesg, "BODY.PEEK[HEADER.FIELDS (DATE)]");
919
920 while (fetch_response(&sockpri, t, 0, dbuf) == RESPONSE_NONE);
921
922 if (strptime(dbuf, "Date: %a, %d %b %Y %H:%M:%S", &tl) &&
923 strftime(s, MBOX_NAME_LEN - 1, destmbox, &tl))
924 xstrncpy(destmbox, s, MBOX_NAME_LEN - 1);
925 }
926
927
928 /*
929 * Format the destination mailbox according to "default variables" specifiers.
930 */
931 void
932 default_variables(char *mbox, char *destmbox)
933 {
934 char *m, *r, *w, *s;
935
936 if (!strchr(destmbox, '$'))
937 return;
938
939 s = xstrdup(destmbox);
940
941 for (r = s, w = destmbox; *r != '\0';) {
942 if (w - destmbox >= MBOX_NAME_LEN - 1)
943 break;
944 if (*r == '$') {
945 switch (*(r + 1)) {
946 case '_':
947 if (w + strlen(mbox) - destmbox >
948 MBOX_NAME_LEN - 1) {
949 r += 2;
950 break;
951 }
952 for (m = mbox; *m != '\0'; m++, w++)
953 *w = *m;
954 r += 2;
955 break;
956 case '$':
957 *w++ = '$';
958 r += 2;
959 break;
960 default:
961 *w++ = *r++;
962 break;
963 }
964 } else {
965 *w++ = *r++;
966 }
967 }
968 *w = '\0';
969
970 xfree(s);
971 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26