Fix error creation and warning
[claws.git] / src / plugins / mailmbox / mailimf_write.c
blob816a972d6c2a64fb7c3d701254e0da716bab8250
1 /*
2 * libEtPan! -- a mail stuff library
4 * Copyright (C) 2001, 2002 - DINH Viet Hoa
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * $Id$
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #include "claws-features.h"
39 #endif
41 #include "mailimf_write.h"
43 #include <time.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include "file-utils.h"
48 #define MAX_MAIL_COL 72
50 #ifndef TRUE
51 #define TRUE 1
52 #endif
54 #ifndef FALSE
55 #define FALSE 0
56 #endif
58 #define MAX_VALID_IMF_LINE 998
60 static int mailimf_orig_date_write(FILE * f, int * col,
61 struct mailimf_orig_date * date);
62 static int mailimf_date_time_write(FILE * f, int * col,
63 struct mailimf_date_time * date_time);
64 static int mailimf_from_write(FILE * f, int * col,
65 struct mailimf_from * from);
66 static int mailimf_sender_write(FILE * f, int * col,
67 struct mailimf_sender * sender);
68 static int mailimf_reply_to_write(FILE * f, int * col,
69 struct mailimf_reply_to * reply_to);
70 static int mailimf_to_write(FILE * f, int * col,
71 struct mailimf_to * to);
72 static int mailimf_cc_write(FILE * f, int * col,
73 struct mailimf_cc * to);
74 static int mailimf_bcc_write(FILE * f, int * col,
75 struct mailimf_bcc * to);
76 static int mailimf_message_id_write(FILE * f, int * col,
77 struct mailimf_message_id * message_id);
78 static int mailimf_msg_id_list_write(FILE * f, int * col,
79 clist * list);
80 static int mailimf_in_reply_to_write(FILE * f, int * col,
81 struct mailimf_in_reply_to *
82 in_reply_to);
83 static int mailimf_references_write(FILE * f, int * col,
84 struct mailimf_references * references);
85 static int mailimf_subject_write(FILE * f, int * col,
86 struct mailimf_subject * subject);
88 static int mailimf_address_write(FILE * f, int * col,
89 struct mailimf_address * addr);
90 static int mailimf_group_write(FILE * f, int * col,
91 struct mailimf_group * group);
93 static int mailimf_mailbox_write(FILE * f, int * col,
94 struct mailimf_mailbox * mb);
96 static int mailimf_comments_write(FILE * f, int * col,
97 struct mailimf_comments * comments);
99 static int mailimf_optional_field_write(FILE * f, int * col,
100 struct mailimf_optional_field * field);
102 static int mailimf_keywords_write(FILE * f, int * col,
103 struct mailimf_keywords * keywords);
105 static int mailimf_return_write(FILE * f, int * col,
106 struct mailimf_return * return_path);
108 static int mailimf_path_write(FILE * f, int * col,
109 struct mailimf_path * path);
111 static int mailimf_resent_date_write(FILE * f, int * col,
112 struct mailimf_orig_date * date);
114 static int mailimf_resent_from_write(FILE * f, int * col,
115 struct mailimf_from * from);
117 static int mailimf_resent_sender_write(FILE * f, int * col,
118 struct mailimf_sender * sender);
120 static int mailimf_resent_to_write(FILE * f, int * col,
121 struct mailimf_to * to);
123 static int mailimf_resent_cc_write(FILE * f, int * col,
124 struct mailimf_cc * cc);
126 static int mailimf_resent_bcc_write(FILE * f, int * col,
127 struct mailimf_bcc * bcc);
129 static int
130 mailimf_resent_msg_id_write(FILE * f, int * col,
131 struct mailimf_message_id * message_id);
135 /* ************************ */
137 #define CRLF "\r\n"
138 #define HEADER_FOLD "\r\n "
140 static inline int flush_buf(FILE * f, const char * str, size_t length)
142 if (length != 0) {
143 int r;
145 r = claws_fwrite(str, 1, length, f);
146 if (r == 0)
147 return MAILIMF_ERROR_FILE;
149 return MAILIMF_NO_ERROR;
152 #define CUT_AT_MAX_VALID_IMF_LINE
154 int mailimf_string_write(FILE * f, int * col,
155 const char * str, size_t length)
157 int r;
158 size_t count;
159 const char * block_begin;
160 const char * p;
161 int done;
163 p = str;
164 block_begin = str;
165 count = 0;
167 while (length > 0) {
168 #ifdef CUT_AT_MAX_VALID_IMF_LINE
169 if (count >= 998) {
171 cut lines at maximum valid length for internet message
172 format standard (currently RFC 2822)
174 This should not happen.
175 In case there are some lines larger than 998 in body,
176 the encoding must be changed into base64 or quoted-printable
177 so that wrapping to 72 columns is done.
180 r = flush_buf(f, block_begin, count);
181 if (r != MAILIMF_NO_ERROR)
182 return r;
184 r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
185 if (r == 0)
186 return MAILIMF_ERROR_FILE;
188 count = 0;
189 block_begin = p;
191 * col = 0;
193 #endif
194 switch (* p) {
195 case '\n':
196 r = flush_buf(f, block_begin, count);
197 if (r != MAILIMF_NO_ERROR)
198 return r;
200 r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
201 if (r == 0)
202 return MAILIMF_ERROR_FILE;
204 p ++;
205 length --;
206 count = 0;
207 block_begin = p;
209 * col = 0;
210 break;
212 case '\r':
213 done = 0;
214 if (length >= 2) {
215 if (* (p + 1) == '\n') {
216 r = flush_buf(f, block_begin, count);
217 if (r != MAILIMF_NO_ERROR)
218 return r;
220 r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
221 if (r == 0)
222 return MAILIMF_ERROR_FILE;
224 p += 2;
225 length -= 2;
226 count = 0;
227 block_begin = p;
229 * col = 0;
231 done = 1;
234 if (!done) {
235 r = flush_buf(f, block_begin, count);
236 if (r != MAILIMF_NO_ERROR)
237 return r;
239 r = claws_fwrite(CRLF, 1, sizeof(CRLF) - 1, f);
240 if (r == 0)
241 return MAILIMF_ERROR_FILE;
243 p ++;
244 length --;
245 count = 0;
246 block_begin = p;
248 * col = 0;
250 break;
252 default:
253 p ++;
254 count ++;
255 length --;
256 break;
260 r = flush_buf(f, block_begin, count);
261 if (r != MAILIMF_NO_ERROR)
262 return r;
263 * col += count;
265 return MAILIMF_NO_ERROR;
268 #if 0
269 int mailimf_header_string_write(FILE * f, int * col,
270 char * str, size_t length)
272 char * p;
273 char * block_begin;
274 int current_col;
275 char * last_cut;
276 int r;
277 int first;
279 if (* col + length < MAX_MAIL_COL)
280 return mailimf_string_write(f, col, str, length);
282 first = 1;
283 p = str;
284 block_begin = p;
285 last_cut = block_begin;
286 current_col = * col;
288 while (1) {
289 if (current_col >= MAX_MAIL_COL) {
290 /* if we reach the maximum recommanded size of line */
291 if (last_cut == block_begin) {
292 /* if we could not find any place to cut */
293 if (first) {
294 /* fold the header */
295 r = mailimf_string_write(f, col, HEADER_FOLD,
296 sizeof(HEADER_FOLD) - 1);
297 if (r != MAILIMF_NO_ERROR)
298 return r;
299 current_col = * col + p - block_begin;
300 first = 0;
302 else {
303 /* cut the header */
304 r = mailimf_string_write(f, col, block_begin, p - block_begin);
305 if (r != MAILIMF_NO_ERROR)
306 return r;
307 r = mailimf_string_write(f, col, HEADER_FOLD,
308 sizeof(HEADER_FOLD) - 1);
309 if (r != MAILIMF_NO_ERROR)
310 return r;
311 first = 0;
312 block_begin = p;
313 last_cut = block_begin;
314 current_col = * col + p - block_begin;
317 else {
318 /* if we found a place to cut */
319 r = mailimf_string_write(f, col, block_begin, last_cut - block_begin);
320 if (r != MAILIMF_NO_ERROR)
321 return r;
322 r = mailimf_string_write(f, col, HEADER_FOLD,
323 sizeof(HEADER_FOLD) - 1);
324 if (r != MAILIMF_NO_ERROR)
325 return r;
326 first = 0;
327 block_begin = last_cut;
328 last_cut = block_begin;
329 current_col = * col + p - block_begin;
330 continue;
333 else {
334 if (length == 0)
335 break;
337 switch (* p) {
338 case ' ':
339 case '\t':
340 last_cut = p;
341 current_col ++;
342 break;
344 case '\r':
345 case '\n':
346 current_col = 0;
347 break;
349 default:
350 current_col ++;
351 break;
354 p ++;
355 length --;
359 return mailimf_string_write(f, col, block_begin, p - block_begin);
361 #endif
363 #if 0
364 enum {
365 STATE_LOWER_72,
366 STATE_LOWER_72_CUT,
367 STATE_EQUAL_72,
368 STATE_LOWER_998,
369 STATE_EQUAL_998,
372 int mailimf_header_string_write(FILE * f, int * col,
373 const char * str, size_t length)
375 int state;
376 const char * p;
377 const char * block_begin;
378 size_t size;
379 const char * cut;
380 int r;
382 if (* col < MAX_MAIL_COL)
383 state = STATE_LOWER_72_CUT;
384 else if (* col == MAX_MAIL_COL)
385 state = STATE_EQUAL_72;
386 else if (* col < MAX_VALID_IMF_LINE)
387 state = STATE_LOWER_998;
388 else
389 state = STATE_EQUAL_998;
391 p = str;
392 block_begin = p;
393 size = * col;
394 cut = p;
396 while (length > 0) {
397 switch (state) {
398 case STATE_LOWER_72:
399 switch (* p) {
400 case '\r':
401 case '\n':
402 p ++;
403 length --;
404 size = 0;
405 break;
407 case ' ':
408 case '\t':
409 cut = p;
410 p ++;
411 length --;
412 size ++;
413 state = STATE_LOWER_72_CUT;
414 break;
416 default:
417 if (size < MAX_MAIL_COL - 1) {
418 p ++;
419 length --;
420 size ++;
422 else {
423 state = STATE_EQUAL_72;
424 p ++;
425 length --;
426 size ++;
428 break;
430 break; /* end of STATE_LOWER_72 */
432 case STATE_LOWER_72_CUT:
433 switch (* p) {
434 case '\r':
435 case '\n':
436 p ++;
437 length --;
438 size = 0;
439 state = STATE_LOWER_72;
440 break;
442 case ' ':
443 case '\t':
444 cut = p;
445 p ++;
446 length --;
447 size ++;
448 break;
450 default:
451 if (size < MAX_MAIL_COL) {
452 p ++;
453 length --;
454 size ++;
456 else {
457 r = mailimf_string_write(f, col, block_begin, cut - block_begin);
458 if (r != MAILIMF_NO_ERROR)
459 return r;
460 r = mailimf_string_write(f, col, HEADER_FOLD,
461 sizeof(HEADER_FOLD) - 1);
462 if (r != MAILIMF_NO_ERROR)
463 return r;
464 p ++;
465 length --;
466 block_begin = cut;
467 if ((* block_begin == ' ') || (* block_begin == '\t'))
468 block_begin ++;
469 size = p - block_begin + * col;
470 state = STATE_LOWER_72;
472 break;
474 break; /* end of STATE_LOWER_72_CUT */
476 case STATE_EQUAL_72:
477 switch (* p) {
478 case '\r':
479 case '\n':
480 p ++;
481 length --;
482 size = 0;
483 state = STATE_LOWER_72;
484 break;
486 case ' ':
487 case '\t':
488 r = mailimf_string_write(f, col, block_begin, p - block_begin);
489 if (r != MAILIMF_NO_ERROR)
490 return r;
491 r = mailimf_string_write(f, col, HEADER_FOLD,
492 sizeof(HEADER_FOLD) - 1);
493 if (r != MAILIMF_NO_ERROR)
494 return r;
495 p ++;
496 length --;
497 block_begin = p;
498 size = p - block_begin + * col;
499 state = STATE_LOWER_72;
500 break;
502 default:
503 p ++;
504 length --;
505 size ++;
506 state = STATE_LOWER_998;
507 break;
509 break; /* end of STATE_EQUAL_72 */
511 case STATE_LOWER_998:
512 switch (* p) {
513 case '\r':
514 case '\n':
515 p ++;
516 length --;
517 size = 0;
518 state = STATE_LOWER_72;
519 break;
521 case ' ':
522 case '\t':
523 r = mailimf_string_write(f, col, block_begin, p - block_begin);
524 if (r != MAILIMF_NO_ERROR)
525 return r;
526 r = mailimf_string_write(f, col, HEADER_FOLD,
527 sizeof(HEADER_FOLD) - 1);
528 if (r != MAILIMF_NO_ERROR)
529 return r;
530 p ++;
531 length --;
532 block_begin = p;
533 size = p - block_begin + * col;
534 state = STATE_LOWER_72;
535 break;
537 default:
538 if (size < MAX_VALID_IMF_LINE - 1) {
539 p ++;
540 length --;
541 size ++;
543 else {
544 p ++;
545 length --;
546 size = 0;
547 state = STATE_EQUAL_998;
549 break;
551 break; /* end of STATE_LOWER_998 */
553 case STATE_EQUAL_998:
554 switch (* p) {
555 case '\r':
556 case '\n':
557 p ++;
558 length --;
559 size = 0;
560 state = STATE_LOWER_72;
561 break;
563 case ' ':
564 case '\t':
565 r = mailimf_string_write(f, col, block_begin, p - block_begin);
566 if (r != MAILIMF_NO_ERROR)
567 return r;
568 r = mailimf_string_write(f, col, HEADER_FOLD,
569 sizeof(HEADER_FOLD) - 1);
570 if (r != MAILIMF_NO_ERROR)
571 return r;
572 p ++;
573 length --;
574 block_begin = p;
575 size = p - block_begin + * col;
576 state = STATE_LOWER_72;
577 break;
579 default:
580 #ifdef CUT_AT_MAX_VALID_IMF_LINE
581 r = mailimf_string_write(f, col, block_begin, p - block_begin);
582 if (r != MAILIMF_NO_ERROR)
583 return r;
584 r = mailimf_string_write(f, col, HEADER_FOLD,
585 sizeof(HEADER_FOLD) - 1);
586 if (r != MAILIMF_NO_ERROR)
587 return r;
588 p ++;
589 length --;
590 block_begin = p;
591 size = p - block_begin + * col;
592 state = STATE_LOWER_72;
593 #else
594 p ++;
595 length --;
596 size ++;
597 #endif
598 break;
600 break; /* end of STATE_EQUAL_998 */
604 r = mailimf_string_write(f, col, block_begin, p - block_begin);
605 if (r != MAILIMF_NO_ERROR)
606 return r;
608 return MAILIMF_NO_ERROR;
610 #endif
612 enum {
613 STATE_BEGIN,
614 STATE_WORD,
615 STATE_SPACE,
618 int mailimf_header_string_write(FILE * f, int * col,
619 const char * str, size_t length)
621 int state;
622 const char * p;
623 const char * word_begin;
624 int first;
626 state = STATE_BEGIN;
628 p = str;
629 word_begin = p;
630 first = 1;
632 while (length > 0) {
633 switch (state) {
634 case STATE_BEGIN:
635 switch (* p) {
636 case '\r':
637 case '\n':
638 case ' ':
639 case '\t':
640 p ++;
641 length --;
642 break;
644 default:
645 word_begin = p;
646 state = STATE_WORD;
647 break;
649 break;
651 case STATE_SPACE:
652 switch (* p) {
653 case '\r':
654 case '\n':
655 case ' ':
656 case '\t':
657 p ++;
658 length --;
659 break;
661 default:
662 word_begin = p;
663 state = STATE_WORD;
664 break;
666 break;
668 case STATE_WORD:
669 switch (* p) {
670 case '\r':
671 case '\n':
672 case ' ':
673 case '\t':
674 if (p - word_begin + (* col) + 1 > MAX_MAIL_COL)
675 mailimf_string_write(f, col, HEADER_FOLD,
676 sizeof(HEADER_FOLD) - 1);
677 else {
678 if (!first)
679 mailimf_string_write(f, col, " ", 1);
681 first = 0;
682 mailimf_string_write(f, col, word_begin, p - word_begin);
683 state = STATE_SPACE;
684 break;
686 default:
687 if (p - word_begin + (* col) >= MAX_VALID_IMF_LINE) {
688 mailimf_string_write(f, col, word_begin, p - word_begin);
689 mailimf_string_write(f, col, HEADER_FOLD,
690 sizeof(HEADER_FOLD) - 1);
691 word_begin = p;
693 p ++;
694 length --;
695 break;
697 break;
701 if (state == STATE_WORD) {
702 if (p - word_begin + (* col) >= MAX_MAIL_COL)
703 mailimf_string_write(f, col, HEADER_FOLD,
704 sizeof(HEADER_FOLD) - 1);
705 else {
706 if (!first)
707 mailimf_string_write(f, col, " ", 1);
709 first = 0;
710 mailimf_string_write(f, col, word_begin, p - word_begin);
713 return MAILIMF_NO_ERROR;
716 int mailimf_envelope_fields_write(FILE * f, int * col,
717 struct mailimf_fields * fields)
719 clistiter * cur;
721 for(cur = clist_begin(fields->fld_list) ; cur != NULL ;
722 cur = clist_next(cur)) {
723 int r;
724 struct mailimf_field * field;
726 field = clist_content(cur);
727 if (field->fld_type != MAILIMF_FIELD_OPTIONAL_FIELD) {
728 r = mailimf_field_write(f, col, field);
729 if (r != MAILIMF_NO_ERROR)
730 return r;
734 return MAILIMF_NO_ERROR;
737 int mailimf_fields_write(FILE * f, int * col,
738 struct mailimf_fields * fields)
740 clistiter * cur;
742 for(cur = clist_begin(fields->fld_list) ; cur != NULL ;
743 cur = clist_next(cur)) {
744 int r;
746 r = mailimf_field_write(f, col, clist_content(cur));
747 if (r != MAILIMF_NO_ERROR)
748 return r;
751 return MAILIMF_NO_ERROR;
754 #if 0
755 int mailimf_unparsed_fields_write(FILE * f, int * col,
756 struct mailimf_unparsed_fields * fields)
758 clistiter * cur;
760 for(cur = clist_begin(fields->list) ; cur != NULL ; cur = cur->next) {
761 int r;
763 r = mailimf_optional_field_write(f, col, cur->data);
764 if (r != MAILIMF_NO_ERROR)
765 return r;
768 return MAILIMF_NO_ERROR;
770 #endif
772 int mailimf_field_write(FILE * f, int * col,
773 struct mailimf_field * field)
775 int r;
777 switch (field->fld_type) {
778 case MAILIMF_FIELD_RETURN_PATH:
779 r = mailimf_return_write(f, col, field->fld_data.fld_return_path);
780 break;
781 case MAILIMF_FIELD_RESENT_DATE:
782 r = mailimf_resent_date_write(f, col, field->fld_data.fld_resent_date);
783 break;
784 case MAILIMF_FIELD_RESENT_FROM:
785 r = mailimf_resent_from_write(f, col, field->fld_data.fld_resent_from);
786 break;
787 case MAILIMF_FIELD_RESENT_SENDER:
788 r = mailimf_resent_sender_write(f, col, field->fld_data.fld_resent_sender);
789 break;
790 case MAILIMF_FIELD_RESENT_TO:
791 r = mailimf_resent_to_write(f, col, field->fld_data.fld_resent_to);
792 break;
793 case MAILIMF_FIELD_RESENT_CC:
794 r = mailimf_resent_cc_write(f, col, field->fld_data.fld_resent_cc);
795 break;
796 case MAILIMF_FIELD_RESENT_BCC:
797 r = mailimf_resent_bcc_write(f, col, field->fld_data.fld_resent_bcc);
798 break;
799 case MAILIMF_FIELD_RESENT_MSG_ID:
800 r = mailimf_resent_msg_id_write(f, col, field->fld_data.fld_resent_msg_id);
801 break;
802 case MAILIMF_FIELD_ORIG_DATE:
803 r = mailimf_orig_date_write(f, col, field->fld_data.fld_orig_date);
804 break;
805 case MAILIMF_FIELD_FROM:
806 r = mailimf_from_write(f, col, field->fld_data.fld_from);
807 break;
808 case MAILIMF_FIELD_SENDER:
809 r = mailimf_sender_write(f, col, field->fld_data.fld_sender);
810 break;
811 case MAILIMF_FIELD_REPLY_TO:
812 r = mailimf_reply_to_write(f, col, field->fld_data.fld_reply_to);
813 break;
814 case MAILIMF_FIELD_TO:
815 r = mailimf_to_write(f, col, field->fld_data.fld_to);
816 break;
817 case MAILIMF_FIELD_CC:
818 r = mailimf_cc_write(f, col, field->fld_data.fld_cc);
819 break;
820 case MAILIMF_FIELD_BCC:
821 r = mailimf_bcc_write(f, col, field->fld_data.fld_bcc);
822 break;
823 case MAILIMF_FIELD_MESSAGE_ID:
824 r = mailimf_message_id_write(f, col, field->fld_data.fld_message_id);
825 break;
826 case MAILIMF_FIELD_IN_REPLY_TO:
827 r = mailimf_in_reply_to_write(f, col, field->fld_data.fld_in_reply_to);
828 break;
829 case MAILIMF_FIELD_REFERENCES:
830 r = mailimf_references_write(f, col, field->fld_data.fld_references);
831 break;
832 case MAILIMF_FIELD_SUBJECT:
833 r = mailimf_subject_write(f, col, field->fld_data.fld_subject);
834 break;
835 case MAILIMF_FIELD_COMMENTS:
836 r = mailimf_comments_write(f, col, field->fld_data.fld_comments);
837 break;
838 case MAILIMF_FIELD_KEYWORDS:
839 r = mailimf_keywords_write(f, col, field->fld_data.fld_keywords);
840 break;
841 case MAILIMF_FIELD_OPTIONAL_FIELD:
842 r = mailimf_optional_field_write(f, col, field->fld_data.fld_optional_field);
843 break;
844 default:
845 r = MAILIMF_ERROR_INVAL;
846 break;
849 if (r != MAILIMF_NO_ERROR)
850 return r;
852 return MAILIMF_NO_ERROR;
856 static int mailimf_orig_date_write(FILE * f, int * col,
857 struct mailimf_orig_date * date)
859 int r;
861 r = mailimf_string_write(f, col, "Date: ", 6);
862 if (r != MAILIMF_NO_ERROR)
863 return r;
865 r = mailimf_date_time_write(f, col, date->dt_date_time);
866 if (r != MAILIMF_NO_ERROR)
867 return r;
869 r = mailimf_string_write(f, col, "\r\n", 2);
870 if (r != MAILIMF_NO_ERROR)
871 return r;
872 #if 0
873 * col = 0;
874 #endif
876 return MAILIMF_NO_ERROR;
879 #define MAX_DATE_STR 256
881 /* 0 = Sunday */
882 /* y > 1752 */
884 static int dayofweek(int year, int month, int day)
886 static int offset[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
888 year -= month < 3;
890 return (year + year/4 - year/100 + year/400 + offset[month-1] + day) % 7;
893 static const char * week_of_day_str[] = { "Sun", "Mon", "Tue", "Wed", "Thu",
894 "Fri", "Sat"};
895 static const char * month_str[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
896 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
898 static int mailimf_date_time_write(FILE * f, int * col,
899 struct mailimf_date_time * date_time)
901 int r;
902 char date_str[MAX_DATE_STR];
903 #if 0
904 struct tm tmval;
905 time_t timeval;
906 #endif
907 int wday;
909 #if 0
910 tmval.tm_sec = date_time->sec;
911 tmval.tm_min = date_time->min;
912 tmval.tm_hour = date_time->hour;
913 tmval.tm_sec = date_time->sec;
914 tmval.tm_mday = date_time->day;
915 tmval.tm_mon = date_time->month - 1;
916 tmval.tm_year = date_time->year - 1900;
917 tmval.tm_isdst = 1;
919 timeval = mktime(&tmval);
921 localtime_r(&timeval, &tmval);
922 #endif
924 wday = dayofweek(date_time->dt_year, date_time->dt_month, date_time->dt_day);
926 snprintf(date_str, MAX_DATE_STR, "%s, %i %s %i %02i:%02i:%02i %+05i",
927 week_of_day_str[wday], date_time->dt_day,
928 month_str[date_time->dt_month - 1],
929 date_time->dt_year, date_time->dt_hour,
930 date_time->dt_min, date_time->dt_sec,
931 date_time->dt_zone);
933 r = mailimf_string_write(f, col, date_str, strlen(date_str));
935 if (r != MAILIMF_NO_ERROR)
936 return r;
938 return MAILIMF_NO_ERROR;
941 static int mailimf_from_write(FILE * f, int * col,
942 struct mailimf_from * from)
944 int r;
946 r = mailimf_string_write(f, col, "From: ", 6);
947 if (r != MAILIMF_NO_ERROR)
948 return r;
950 r = mailimf_mailbox_list_write(f, col, from->frm_mb_list);
951 if (r != MAILIMF_NO_ERROR)
952 return r;
954 r = mailimf_string_write(f, col, "\r\n", 2);
955 if (r != MAILIMF_NO_ERROR)
956 return r;
957 #if 0
958 * col = 0;
959 #endif
961 return MAILIMF_NO_ERROR;
964 static int mailimf_sender_write(FILE * f, int * col,
965 struct mailimf_sender * sender)
967 int r;
969 r = mailimf_string_write(f, col, "Sender: ", 8);
970 if (r != MAILIMF_NO_ERROR)
971 return r;
973 r = mailimf_mailbox_write(f, col, sender->snd_mb);
974 if (r != MAILIMF_NO_ERROR)
975 return r;
977 r = mailimf_string_write(f, col, "\r\n", 2);
978 if (r != MAILIMF_NO_ERROR)
979 return r;
980 #if 0
981 * col = 0;
982 #endif
984 return MAILIMF_NO_ERROR;
987 static int mailimf_reply_to_write(FILE * f, int * col,
988 struct mailimf_reply_to * reply_to)
990 int r;
992 r = mailimf_string_write(f, col, "Reply-To: ", 10);
993 if (r != MAILIMF_NO_ERROR)
994 return r;
996 r = mailimf_address_list_write(f, col, reply_to->rt_addr_list);
997 if (r != MAILIMF_NO_ERROR)
998 return r;
1000 r = mailimf_string_write(f, col, "\r\n", 2);
1001 if (r != MAILIMF_NO_ERROR)
1002 return r;
1003 #if 0
1004 * col = 0;
1005 #endif
1007 return MAILIMF_NO_ERROR;
1011 static int mailimf_to_write(FILE * f, int * col,
1012 struct mailimf_to * to)
1014 int r;
1016 r = mailimf_string_write(f, col, "To: ", 4);
1017 if (r != MAILIMF_NO_ERROR)
1018 return r;
1020 r = mailimf_address_list_write(f, col, to->to_addr_list);
1021 if (r != MAILIMF_NO_ERROR)
1022 return r;
1024 r = mailimf_string_write(f, col, "\r\n", 2);
1025 if (r != MAILIMF_NO_ERROR)
1026 return r;
1027 #if 0
1028 * col = 0;
1029 #endif
1031 return MAILIMF_NO_ERROR;
1035 static int mailimf_cc_write(FILE * f, int * col,
1036 struct mailimf_cc * cc)
1038 int r;
1040 r = mailimf_string_write(f, col, "Cc: ", 4);
1041 if (r != MAILIMF_NO_ERROR)
1042 return r;
1044 r = mailimf_address_list_write(f, col, cc->cc_addr_list);
1045 if (r != MAILIMF_NO_ERROR)
1046 return r;
1048 r = mailimf_string_write(f, col, "\r\n", 2);
1049 if (r != MAILIMF_NO_ERROR)
1050 return r;
1051 #if 0
1052 * col = 0;
1053 #endif
1055 return MAILIMF_NO_ERROR;
1059 static int mailimf_bcc_write(FILE * f, int * col,
1060 struct mailimf_bcc * bcc)
1062 int r;
1064 r = mailimf_string_write(f, col, "Bcc: ", 5);
1065 if (r != MAILIMF_NO_ERROR)
1066 return r;
1068 if (bcc->bcc_addr_list != NULL) {
1069 r = mailimf_address_list_write(f, col, bcc->bcc_addr_list);
1070 if (r != MAILIMF_NO_ERROR)
1071 return r;
1074 r = mailimf_string_write(f, col, "\r\n", 2);
1075 if (r != MAILIMF_NO_ERROR)
1076 return r;
1077 #if 0
1078 * col = 0;
1079 #endif
1081 return MAILIMF_NO_ERROR;
1085 static int mailimf_message_id_write(FILE * f, int * col,
1086 struct mailimf_message_id * message_id)
1088 int r;
1090 r = mailimf_string_write(f, col, "Message-ID: ", 12);
1091 if (r != MAILIMF_NO_ERROR)
1092 return r;
1094 r = mailimf_string_write(f, col, "<", 1);
1095 if (r != MAILIMF_NO_ERROR)
1096 return r;
1098 r = mailimf_string_write(f, col,
1099 message_id->mid_value,
1100 strlen(message_id->mid_value));
1101 if (r != MAILIMF_NO_ERROR)
1102 return r;
1104 r = mailimf_string_write(f, col, ">", 1);
1105 if (r != MAILIMF_NO_ERROR)
1106 return r;
1108 r = mailimf_string_write(f, col, "\r\n", 2);
1109 if (r != MAILIMF_NO_ERROR)
1110 return r;
1111 #if 0
1112 * col = 0;
1113 #endif
1115 return MAILIMF_NO_ERROR;
1119 static int mailimf_msg_id_list_write(FILE * f, int * col, clist * mid_list)
1121 clistiter * cur;
1122 int r;
1123 int first;
1125 first = TRUE;
1127 for(cur = clist_begin(mid_list) ; cur != NULL ; cur = clist_next(cur)) {
1128 char * msgid;
1129 size_t len;
1131 msgid = clist_content(cur);
1132 len = strlen(msgid);
1135 XXX - if this is the first message ID, don't fold.
1136 This is a workaround for a bug of old versions of INN.
1138 if (!first) {
1139 if (* col > 1) {
1141 if (* col + len >= MAX_MAIL_COL) {
1142 r = mailimf_string_write(f, col, "\r\n ", 3);
1143 if (r != MAILIMF_NO_ERROR)
1144 return r;
1145 #if 0
1146 * col = 1;
1147 #endif
1148 first = TRUE;
1153 if (!first) {
1154 r = mailimf_string_write(f, col, " ", 1);
1155 if (r != MAILIMF_NO_ERROR)
1156 return r;
1158 else {
1159 first = FALSE;
1162 r = mailimf_string_write(f, col, "<", 1);
1163 if (r != MAILIMF_NO_ERROR)
1164 return r;
1166 r = mailimf_string_write(f, col, msgid, len);
1167 if (r != MAILIMF_NO_ERROR)
1168 return r;
1170 r = mailimf_string_write(f, col, ">", 1);
1171 if (r != MAILIMF_NO_ERROR)
1172 return r;
1175 return MAILIMF_NO_ERROR;
1179 static int mailimf_in_reply_to_write(FILE * f, int * col,
1180 struct mailimf_in_reply_to * in_reply_to)
1182 int r;
1184 r = mailimf_string_write(f, col, "In-Reply-To: ", 13);
1185 if (r != MAILIMF_NO_ERROR)
1186 return r;
1188 r = mailimf_msg_id_list_write(f, col, in_reply_to->mid_list);
1189 if (r != MAILIMF_NO_ERROR)
1190 return r;
1192 r = mailimf_string_write(f, col, "\r\n", 2);
1193 if (r != MAILIMF_NO_ERROR)
1194 return r;
1195 #if 0
1196 * col = 0;
1197 #endif
1199 return MAILIMF_NO_ERROR;
1203 static int mailimf_references_write(FILE * f, int * col,
1204 struct mailimf_references * references)
1206 int r;
1208 r = mailimf_string_write(f, col, "References: ", 12);
1209 if (r != MAILIMF_NO_ERROR)
1210 return r;
1212 r = mailimf_msg_id_list_write(f, col, references->mid_list);
1213 if (r != MAILIMF_NO_ERROR)
1214 return r;
1216 r = mailimf_string_write(f, col, "\r\n", 2);
1217 if (r != MAILIMF_NO_ERROR)
1218 return r;
1219 #if 0
1220 * col = 0;
1221 #endif
1223 return MAILIMF_NO_ERROR;
1228 static int mailimf_subject_write(FILE * f, int * col,
1229 struct mailimf_subject * subject)
1231 int r;
1233 r = mailimf_string_write(f, col, "Subject: ", 9);
1234 if (r != MAILIMF_NO_ERROR)
1235 return r;
1237 r = mailimf_header_string_write(f, col,
1238 subject->sbj_value, strlen(subject->sbj_value));
1239 if (r != MAILIMF_NO_ERROR)
1240 return r;
1242 r = mailimf_string_write(f, col, "\r\n", 2);
1243 if (r != MAILIMF_NO_ERROR)
1244 return r;
1245 #if 0
1246 * col = 0;
1247 #endif
1249 return MAILIMF_NO_ERROR;
1252 int mailimf_address_list_write(FILE * f, int * col,
1253 struct mailimf_address_list * addr_list)
1255 clistiter * cur;
1256 int r;
1257 int first;
1259 first = TRUE;
1261 for(cur = clist_begin(addr_list->ad_list) ; cur != NULL ;
1262 cur = clist_next(cur)) {
1263 struct mailimf_address * addr;
1265 addr = clist_content(cur);
1267 if (!first) {
1268 r = mailimf_string_write(f, col, ", ", 2);
1269 if (r != MAILIMF_NO_ERROR)
1270 return r;
1272 else {
1273 first = FALSE;
1276 r = mailimf_address_write(f, col, addr);
1277 if (r != MAILIMF_NO_ERROR)
1278 return r;
1281 return MAILIMF_NO_ERROR;
1285 static int mailimf_address_write(FILE * f, int * col,
1286 struct mailimf_address * addr)
1288 int r;
1290 switch(addr->ad_type) {
1291 case MAILIMF_ADDRESS_MAILBOX:
1292 r = mailimf_mailbox_write(f, col, addr->ad_data.ad_mailbox);
1293 if (r != MAILIMF_NO_ERROR)
1294 return r;
1296 break;
1298 case MAILIMF_ADDRESS_GROUP:
1299 r = mailimf_group_write(f, col, addr->ad_data.ad_group);
1300 if (r != MAILIMF_NO_ERROR)
1301 return r;
1303 break;
1306 return MAILIMF_NO_ERROR;
1310 static int mailimf_group_write(FILE * f, int * col,
1311 struct mailimf_group * group)
1313 int r;
1315 r = mailimf_header_string_write(f, col, group->grp_display_name,
1316 strlen(group->grp_display_name));
1317 if (r != MAILIMF_NO_ERROR)
1318 return r;
1320 r = mailimf_string_write(f, col, ": ", 2);
1321 if (r != MAILIMF_NO_ERROR)
1322 return r;
1324 if (group->grp_mb_list != NULL) {
1325 r = mailimf_mailbox_list_write(f, col, group->grp_mb_list);
1326 if (r != MAILIMF_NO_ERROR)
1327 return r;
1330 r = mailimf_string_write(f, col, ";", 1);
1331 if (r != MAILIMF_NO_ERROR)
1332 return r;
1334 return MAILIMF_NO_ERROR;
1338 int mailimf_mailbox_list_write(FILE * f, int * col,
1339 struct mailimf_mailbox_list * mb_list)
1341 clistiter * cur;
1342 int r;
1343 int first;
1345 first = TRUE;
1347 for(cur = clist_begin(mb_list->mb_list) ; cur != NULL ;
1348 cur = clist_next(cur)) {
1349 struct mailimf_mailbox * mb;
1351 mb = clist_content(cur);
1353 if (!first) {
1354 r = mailimf_string_write(f, col, ", ", 2);
1355 if (r != MAILIMF_NO_ERROR)
1356 return r;
1358 else {
1359 first = FALSE;
1362 r = mailimf_mailbox_write(f, col, mb);
1363 if (r != MAILIMF_NO_ERROR)
1364 return r;
1367 return MAILIMF_NO_ERROR;
1371 int mailimf_quoted_string_write(FILE * f, int * col,
1372 const char * string, size_t len)
1374 int r;
1375 size_t i;
1377 if (claws_fputc('\"', f) < 0)
1378 return MAILIMF_ERROR_FILE;
1379 for(i = 0 ; i < len ; i ++) {
1380 switch (string[i]) {
1381 case '\\':
1382 case '\"':
1383 r = claws_fputc('\\', f);
1384 if (r < 0)
1385 return MAILIMF_ERROR_FILE;
1386 r = claws_fputc(string[i], f);
1387 if (r < 0)
1388 return MAILIMF_ERROR_FILE;
1389 (* col) += 2;
1390 break;
1392 default:
1393 r = claws_fputc(string[i], f);
1394 if (r < 0)
1395 return MAILIMF_ERROR_FILE;
1396 (* col) ++;
1397 break;
1400 if (claws_fputc('\"', f) < 0)
1401 return MAILIMF_ERROR_FILE;
1403 return MAILIMF_NO_ERROR;
1408 static int
1409 atext = ALPHA / DIGIT / ; Any character except controls,
1410 "!" / "#" / ; SP, and specials.
1411 "$" / "%" / ; Used for atoms
1412 "&" / "'" /
1413 "*" / "+" /
1414 "-" / "/" /
1415 "=" / "?" /
1416 "^" / "_" /
1417 "`" / "{" /
1418 "|" / "}" /
1422 static int is_atext(const char * s)
1424 const char * p;
1426 for(p = s ; * p != 0 ; p ++) {
1427 if (isalpha((unsigned char) * p))
1428 continue;
1429 if (isdigit((unsigned char) * p))
1430 continue;
1431 switch (*p) {
1432 case ' ':
1433 case '\t':
1434 case '!':
1435 case '#':
1436 case '$':
1437 case '%':
1438 case '&':
1439 case '\'':
1440 case '*':
1441 case '+':
1442 case '-':
1443 case '/':
1444 case '=':
1445 case '?':
1446 case '^':
1447 case '_':
1448 case '`':
1449 case '{':
1450 case '|':
1451 case '}':
1452 case '~':
1453 break;
1454 default:
1455 return 0;
1459 return 1;
1462 static int mailimf_mailbox_write(FILE * f, int * col,
1463 struct mailimf_mailbox * mb)
1465 int r;
1466 int do_fold;
1468 #if 0
1469 if (* col > 1) {
1471 if (mb->mb_display_name != NULL) {
1472 if (* col + strlen(mb->mb_display_name) >= MAX_MAIL_COL) {
1473 r = mailimf_string_write(f, col, "\r\n ", 3);
1474 if (r != MAILIMF_NO_ERROR)
1475 return r;
1476 #if 0
1477 * col = 1;
1478 #endif
1482 #endif
1484 if (mb->mb_display_name) {
1486 if (is_atext(mb->mb_display_name)) {
1487 r = mailimf_header_string_write(f, col, mb->mb_display_name,
1488 strlen(mb->mb_display_name));
1489 if (r != MAILIMF_NO_ERROR)
1490 return r;
1492 else {
1493 if (mb->mb_display_name != NULL) {
1494 if (* col + strlen(mb->mb_display_name) >= MAX_MAIL_COL) {
1495 r = mailimf_string_write(f, col, "\r\n ", 3);
1496 if (r != MAILIMF_NO_ERROR)
1497 return r;
1501 if (strlen(mb->mb_display_name) > MAX_VALID_IMF_LINE / 2)
1502 return MAILIMF_ERROR_INVAL;
1504 r = mailimf_quoted_string_write(f, col, mb->mb_display_name,
1505 strlen(mb->mb_display_name));
1506 if (r != MAILIMF_NO_ERROR)
1507 return r;
1510 do_fold = 0;
1511 if (* col > 1) {
1513 if (* col + strlen(mb->mb_addr_spec) + 3 >= MAX_MAIL_COL) {
1514 r = mailimf_string_write(f, col, "\r\n ", 3);
1515 if (r != MAILIMF_NO_ERROR)
1516 return r;
1517 #if 0
1518 * col = 1;
1519 #endif
1520 do_fold = 1;
1524 if (do_fold)
1525 r = mailimf_string_write(f, col, "<", 1);
1526 else
1527 r = mailimf_string_write(f, col, " <", 2);
1528 if (r != MAILIMF_NO_ERROR)
1529 return r;
1531 r = mailimf_string_write(f, col, mb->mb_addr_spec,
1532 strlen(mb->mb_addr_spec));
1533 if (r != MAILIMF_NO_ERROR)
1534 return r;
1536 r = mailimf_string_write(f, col, ">", 1);
1537 if (r != MAILIMF_NO_ERROR)
1538 return r;
1540 else {
1541 if (* col + strlen(mb->mb_addr_spec) >= MAX_MAIL_COL) {
1542 r = mailimf_string_write(f, col, "\r\n ", 3);
1543 if (r != MAILIMF_NO_ERROR)
1544 return r;
1547 r = mailimf_string_write(f, col,
1548 mb->mb_addr_spec, strlen(mb->mb_addr_spec));
1549 if (r != MAILIMF_NO_ERROR)
1550 return r;
1554 return MAILIMF_NO_ERROR;
1557 static int mailimf_comments_write(FILE * f, int * col,
1558 struct mailimf_comments * comments)
1560 int r;
1562 r = mailimf_string_write(f, col, "Comments: ", 10);
1563 if (r != MAILIMF_NO_ERROR)
1564 return r;
1566 r = mailimf_header_string_write(f, col,
1567 comments->cm_value, strlen(comments->cm_value));
1568 if (r != MAILIMF_NO_ERROR)
1569 return r;
1571 r = mailimf_string_write(f, col, "\r\n", 2);
1572 if (r != MAILIMF_NO_ERROR)
1573 return r;
1574 #if 0
1575 * col = 0;
1576 #endif
1578 return MAILIMF_NO_ERROR;
1581 static int mailimf_optional_field_write(FILE * f, int * col,
1582 struct mailimf_optional_field * field)
1584 int r;
1586 if (strlen(field->fld_name) + 2 > MAX_VALID_IMF_LINE)
1587 return MAILIMF_ERROR_INVAL;
1589 r = mailimf_string_write(f, col, field->fld_name, strlen(field->fld_name));
1590 if (r != MAILIMF_NO_ERROR)
1591 return r;
1593 r = mailimf_string_write(f, col, ": ", 2);
1594 if (r != MAILIMF_NO_ERROR)
1595 return r;
1597 r = mailimf_header_string_write(f, col, field->fld_value,
1598 strlen(field->fld_value));
1599 if (r != MAILIMF_NO_ERROR)
1600 return r;
1602 #if 0
1603 /* XXX parsing debug */
1604 mailimf_string_write(f, col, " (X)", 4);
1605 #endif
1607 r = mailimf_string_write(f, col, "\r\n", 2);
1608 if (r != MAILIMF_NO_ERROR)
1609 return r;
1610 #if 0
1611 * col = 0;
1612 #endif
1614 return MAILIMF_NO_ERROR;
1617 static int mailimf_keywords_write(FILE * f, int * col,
1618 struct mailimf_keywords * keywords)
1620 int r;
1621 clistiter * cur;
1622 int first;
1624 r = mailimf_string_write(f, col, "Keywords: ", 10);
1625 if (r != MAILIMF_NO_ERROR)
1626 return r;
1628 first = TRUE;
1630 for(cur = clist_begin(keywords->kw_list) ; cur != NULL ;
1631 cur = clist_next(cur)) {
1632 char * keyword;
1633 size_t len;
1635 keyword = clist_content(cur);
1636 len = strlen(keyword);
1638 if (!first) {
1639 r = mailimf_string_write(f, col, ", ", 2);
1640 if (r != MAILIMF_NO_ERROR)
1641 return r;
1643 else {
1644 first = FALSE;
1647 #if 0
1648 if (* col > 1) {
1650 if (* col + len >= MAX_MAIL_COL) {
1651 r = mailimf_string_write(f, col, "\r\n ", 3);
1652 if (r != MAILIMF_NO_ERROR)
1653 return r;
1654 #if 0
1655 * col = 1;
1656 #endif
1659 #endif
1661 r = mailimf_header_string_write(f, col, keyword, len);
1662 if (r != MAILIMF_NO_ERROR)
1663 return r;
1666 r = mailimf_string_write(f, col, "\r\n", 2);
1667 if (r != MAILIMF_NO_ERROR)
1668 return r;
1669 #if 0
1670 * col = 0;
1671 #endif
1673 return MAILIMF_NO_ERROR;
1676 #if 0
1677 static int mailimf_delivering_info_write(FILE * f, int * col,
1678 struct mailimf_delivering_info * info)
1680 clistiter * cur;
1681 int r;
1683 for(cur = clist_begin(info->received_fields) ;
1684 cur != NULL ; cur = cur->next) {
1685 struct mailimf_trace_resent_fields * field;
1687 field = cur->data;
1689 r = mailimf_trace_resent_fields_write(f, col, field);
1690 if (r != MAILIMF_NO_ERROR)
1691 return r;
1694 return MAILIMF_NO_ERROR;
1698 static int
1699 mailimf_trace_resent_fields_write(FILE * f, int * col,
1700 struct mailimf_trace_resent_fields * field)
1702 int r;
1704 if (field->return_path != NULL) {
1705 r = mailimf_return_write(f, col, field->return_path);
1706 if (r != MAILIMF_NO_ERROR)
1707 return r;
1710 if (field->resent_fields != NULL) {
1711 r = mailimf_resent_fields_write(f, col, field->resent_fields);
1712 if (r != MAILIMF_NO_ERROR)
1713 return r;
1716 return MAILIMF_NO_ERROR;
1718 #endif
1720 static int mailimf_return_write(FILE * f, int * col,
1721 struct mailimf_return * return_path)
1723 int r;
1725 r = mailimf_string_write(f, col, "Return-Path: ", 13);
1726 if (r != MAILIMF_NO_ERROR)
1727 return r;
1729 r = mailimf_path_write(f, col, return_path->ret_path);
1730 if (r != MAILIMF_NO_ERROR)
1731 return r;
1733 r = mailimf_string_write(f, col, "\r\n", 2);
1734 if (r != MAILIMF_NO_ERROR)
1735 return r;
1736 #if 0
1737 * col = 0;
1738 #endif
1740 return MAILIMF_NO_ERROR;
1743 static int mailimf_path_write(FILE * f, int * col,
1744 struct mailimf_path * path)
1746 int r;
1748 r = mailimf_string_write(f, col, "<", 1);
1749 if (r != MAILIMF_NO_ERROR)
1750 return r;
1752 r = mailimf_string_write(f, col, path->pt_addr_spec,
1753 strlen(path->pt_addr_spec));
1754 if (r != MAILIMF_NO_ERROR)
1755 return r;
1757 r = mailimf_string_write(f, col, ">", 1);
1758 if (r != MAILIMF_NO_ERROR)
1759 return r;
1761 return MAILIMF_NO_ERROR;
1764 #if 0
1765 static int mailimf_resent_fields_write(FILE * f, int * col,
1766 struct mailimf_resent_fields_list *
1767 resent_fields)
1769 clistiter * cur;
1770 int r;
1772 for(cur = clist_begin(resent_fields->list) ; cur != NULL ; cur = cur->next) {
1773 struct mailimf_resent_field * field;
1775 field = cur->data;
1777 r = mailimf_resent_field_write(f, col, field);
1778 if (r != MAILIMF_NO_ERROR)
1779 return r;
1782 return MAILIMF_NO_ERROR;
1787 static int mailimf_resent_field_write(FILE * f, int * col,
1788 struct mailimf_resent_field *
1789 resent_field)
1791 int r;
1793 switch (resent_field->type) {
1794 case MAILIMF_RESENT_FIELD_DATE:
1795 r = mailimf_resent_date_write(f, col, resent_field->resent_date);
1796 break;
1798 case MAILIMF_RESENT_FIELD_FROM:
1799 r = mailimf_resent_from_write(f, col, resent_field->resent_from);
1800 break;
1802 case MAILIMF_RESENT_FIELD_SENDER:
1803 r = mailimf_resent_sender_write(f, col, resent_field->resent_sender);
1804 break;
1806 case MAILIMF_RESENT_FIELD_TO:
1807 r = mailimf_resent_to_write(f, col, resent_field->resent_to);
1808 break;
1810 case MAILIMF_RESENT_FIELD_CC:
1811 r = mailimf_resent_cc_write(f, col, resent_field->resent_cc);
1812 break;
1814 case MAILIMF_RESENT_FIELD_BCC:
1815 r = mailimf_resent_bcc_write(f, col, resent_field->resent_bcc);
1816 break;
1818 case MAILIMF_RESENT_FIELD_MSG_ID:
1819 r = mailimf_resent_msg_id_write(f, col, resent_field->resent_msg_id);
1820 break;
1821 default:
1822 r = MAILIMF_ERROR_INVAL;
1823 break;
1827 if (r != MAILIMF_NO_ERROR)
1828 return r;
1830 return MAILIMF_NO_ERROR;
1832 #endif
1834 static int mailimf_resent_date_write(FILE * f, int * col,
1835 struct mailimf_orig_date * date)
1837 int r;
1839 r = mailimf_string_write(f, col, "Resent-Date: ", 13);
1840 if (r != MAILIMF_NO_ERROR)
1841 return r;
1843 r = mailimf_date_time_write(f, col, date->dt_date_time);
1844 if (r != MAILIMF_NO_ERROR)
1845 return r;
1847 r = mailimf_string_write(f, col, "\r\n", 2);
1848 if (r != MAILIMF_NO_ERROR)
1849 return r;
1850 #if 0
1851 * col = 0;
1852 #endif
1854 return MAILIMF_NO_ERROR;
1857 static int mailimf_resent_from_write(FILE * f, int * col,
1858 struct mailimf_from * from)
1860 int r;
1862 r = mailimf_string_write(f, col, "Resent-From: ", 13);
1863 if (r != MAILIMF_NO_ERROR)
1864 return r;
1866 r = mailimf_mailbox_list_write(f, col, from->frm_mb_list);
1867 if (r != MAILIMF_NO_ERROR)
1868 return r;
1870 r = mailimf_string_write(f, col, "\r\n", 2);
1871 if (r != MAILIMF_NO_ERROR)
1872 return r;
1873 #if 0
1874 * col = 0;
1875 #endif
1877 return MAILIMF_NO_ERROR;
1880 static int mailimf_resent_sender_write(FILE * f, int * col,
1881 struct mailimf_sender * sender)
1883 int r;
1885 r = mailimf_string_write(f, col, "Resent-Sender: ", 15);
1886 if (r != MAILIMF_NO_ERROR)
1887 return r;
1889 r = mailimf_mailbox_write(f, col, sender->snd_mb);
1890 if (r != MAILIMF_NO_ERROR)
1891 return r;
1893 r = mailimf_string_write(f, col, "\r\n", 2);
1894 if (r != MAILIMF_NO_ERROR)
1895 return r;
1896 #if 0
1897 * col = 0;
1898 #endif
1900 return MAILIMF_NO_ERROR;
1903 static int mailimf_resent_to_write(FILE * f, int * col,
1904 struct mailimf_to * to)
1906 int r;
1908 r = mailimf_string_write(f, col, "Resent-To: ", 11);
1909 if (r != MAILIMF_NO_ERROR)
1910 return r;
1912 r = mailimf_address_list_write(f, col, to->to_addr_list);
1913 if (r != MAILIMF_NO_ERROR)
1914 return r;
1916 r = mailimf_string_write(f, col, "\r\n", 2);
1917 if (r != MAILIMF_NO_ERROR)
1918 return r;
1919 #if 0
1920 * col = 0;
1921 #endif
1923 return MAILIMF_NO_ERROR;
1927 static int mailimf_resent_cc_write(FILE * f, int * col,
1928 struct mailimf_cc * cc)
1930 int r;
1932 r = mailimf_string_write(f, col, "Resent-Cc: ", 11);
1933 if (r != MAILIMF_NO_ERROR)
1934 return r;
1936 r = mailimf_address_list_write(f, col, cc->cc_addr_list);
1937 if (r != MAILIMF_NO_ERROR)
1938 return r;
1940 r = mailimf_string_write(f, col, "\r\n", 2);
1941 if (r != MAILIMF_NO_ERROR)
1942 return r;
1943 #if 0
1944 * col = 0;
1945 #endif
1947 return MAILIMF_NO_ERROR;
1951 static int mailimf_resent_bcc_write(FILE * f, int * col,
1952 struct mailimf_bcc * bcc)
1954 int r;
1956 r = mailimf_string_write(f, col, "Resent-Bcc: ", 12);
1957 if (r != MAILIMF_NO_ERROR)
1958 return r;
1960 if (bcc->bcc_addr_list != NULL) {
1961 r = mailimf_address_list_write(f, col, bcc->bcc_addr_list);
1962 if (r != MAILIMF_NO_ERROR)
1963 return r;
1966 r = mailimf_string_write(f, col, "\r\n", 2);
1967 if (r != MAILIMF_NO_ERROR)
1968 return r;
1969 #if 0
1970 * col = 0;
1971 #endif
1973 return MAILIMF_NO_ERROR;
1977 static int
1978 mailimf_resent_msg_id_write(FILE * f, int * col,
1979 struct mailimf_message_id * message_id)
1981 int r;
1983 r = mailimf_string_write(f, col, "Resent-Message-ID: ", 19);
1984 if (r != MAILIMF_NO_ERROR)
1985 return r;
1987 r = mailimf_string_write(f, col, "<", 1);
1988 if (r != MAILIMF_NO_ERROR)
1989 return r;
1991 r = mailimf_string_write(f, col,
1992 message_id->mid_value, strlen(message_id->mid_value));
1993 if (r != MAILIMF_NO_ERROR)
1994 return r;
1996 r = mailimf_string_write(f, col, ">", 1);
1997 if (r != MAILIMF_NO_ERROR)
1998 return r;
2000 r = mailimf_string_write(f, col, "\r\n", 2);
2001 if (r != MAILIMF_NO_ERROR)
2002 return r;
2003 #if 0
2004 * col = 0;
2005 #endif
2007 return MAILIMF_NO_ERROR;