2 * libEtPan! -- a mail stuff library
4 * Copyright (C) 2001, 2002 - DINH Viet Hoa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
32 #include "mailimf_types_helper.h"
41 struct mailimf_mailbox_list *
42 mailimf_mailbox_list_new_empty()
45 struct mailimf_mailbox_list * mb_list;
51 mb_list = mailimf_mailbox_list_new(list);
58 int mailimf_mailbox_list_add(struct mailimf_mailbox_list * mailbox_list,
59 struct mailimf_mailbox * mb)
63 r = clist_append(mailbox_list->mb_list, mb);
65 return MAILIMF_ERROR_MEMORY;
67 return MAILIMF_NO_ERROR;
70 int mailimf_mailbox_list_add_parse(struct mailimf_mailbox_list * mailbox_list,
75 struct mailimf_mailbox * mb;
79 r = mailimf_mailbox_parse(mb_str, strlen(mb_str), &cur_token, &mb);
80 if (r != MAILIMF_NO_ERROR) {
85 r = mailimf_mailbox_list_add(mailbox_list, mb);
86 if (r != MAILIMF_NO_ERROR) {
91 return MAILIMF_NO_ERROR;
94 mailimf_mailbox_free(mb);
99 int mailimf_mailbox_list_add_mb(struct mailimf_mailbox_list * mailbox_list,
100 char * display_name, char * address)
103 struct mailimf_mailbox * mb;
106 mb = mailimf_mailbox_new(display_name, address);
108 res = MAILIMF_ERROR_MEMORY;
112 r = mailimf_mailbox_list_add(mailbox_list, mb);
113 if (r != MAILIMF_NO_ERROR) {
118 return MAILIMF_NO_ERROR;
121 mailimf_mailbox_free(mb);
128 struct mailimf_address_list *
129 mailimf_address_list_new_empty()
132 struct mailimf_address_list * addr_list;
138 addr_list = mailimf_address_list_new(list);
139 if (addr_list == NULL)
145 int mailimf_address_list_add(struct mailimf_address_list * address_list,
146 struct mailimf_address * addr)
150 r = clist_append(address_list->ad_list, addr);
152 return MAILIMF_ERROR_MEMORY;
154 return MAILIMF_NO_ERROR;
157 int mailimf_address_list_add_parse(struct mailimf_address_list * address_list,
162 struct mailimf_address * addr;
166 r = mailimf_address_parse(addr_str, strlen(addr_str), &cur_token, &addr);
167 if (r != MAILIMF_NO_ERROR) {
172 r = mailimf_address_list_add(address_list, addr);
173 if (r != MAILIMF_NO_ERROR) {
178 return MAILIMF_NO_ERROR;
181 mailimf_address_free(addr);
186 int mailimf_address_list_add_mb(struct mailimf_address_list * address_list,
187 char * display_name, char * address)
190 struct mailimf_mailbox * mb;
191 struct mailimf_address * addr;
194 mb = mailimf_mailbox_new(display_name, address);
196 res = MAILIMF_ERROR_MEMORY;
200 addr = mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mb, NULL);
202 res = MAILIMF_ERROR_MEMORY;
206 r = mailimf_address_list_add(address_list, addr);
207 if (r != MAILIMF_NO_ERROR) {
212 return MAILIMF_NO_ERROR;
215 mailimf_address_free(addr);
217 mailimf_mailbox_free(mb);
224 struct mailimf_resent_fields_list *
225 mailimf_resent_fields_list_new_empty()
228 struct mailimf_resent_fields_list * rf_list;
234 rf_list = mailimf_resent_fields_list_new(list);
241 int mailimf_resent_fields_add(struct mailimf_resent_fields_list * fields,
242 struct mailimf_resent_field * field)
246 r = clist_append(fields->list, field);
248 return MAILIMF_ERROR_MEMORY;
250 return MAILIMF_NO_ERROR;
255 static void detach_free_common_fields(struct mailimf_orig_date * imf_date,
256 struct mailimf_from * imf_from,
257 struct mailimf_sender * imf_sender,
258 struct mailimf_to * imf_to,
259 struct mailimf_cc * imf_cc,
260 struct mailimf_bcc * imf_bcc,
261 struct mailimf_message_id * imf_msg_id)
263 if (imf_date != NULL) {
264 imf_date->dt_date_time = NULL;
265 mailimf_orig_date_free(imf_date);
267 if (imf_from != NULL) {
268 imf_from->frm_mb_list = NULL;
269 mailimf_from_free(imf_from);
271 if (imf_sender != NULL) {
272 imf_sender->snd_mb = NULL;
273 mailimf_sender_free(imf_sender);
275 if (imf_to != NULL) {
276 imf_to->to_addr_list = NULL;
277 mailimf_to_free(imf_to);
279 if (imf_cc != NULL) {
280 imf_cc->cc_addr_list = NULL;
281 mailimf_to_free(imf_cc);
283 if (imf_bcc != NULL) {
284 imf_bcc->bcc_addr_list = NULL;
285 mailimf_bcc_free(imf_bcc);
287 if (imf_msg_id != NULL) {
288 imf_msg_id->mid_value = NULL;
289 mailimf_message_id_free(imf_msg_id);
293 static void detach_resent_field(struct mailimf_field * field)
295 field->fld_type = MAILIMF_FIELD_NONE;
296 mailimf_field_free(field);
300 mailimf_resent_fields_add_data(struct mailimf_fields * fields,
301 struct mailimf_date_time * resent_date,
302 struct mailimf_mailbox_list * resent_from,
303 struct mailimf_mailbox * resent_sender,
304 struct mailimf_address_list * resent_to,
305 struct mailimf_address_list * resent_cc,
306 struct mailimf_address_list * resent_bcc,
307 char * resent_msg_id)
309 struct mailimf_orig_date * imf_resent_date;
310 struct mailimf_from * imf_resent_from;
311 struct mailimf_sender * imf_resent_sender;
312 struct mailimf_to * imf_resent_to;
313 struct mailimf_cc * imf_resent_cc;
314 struct mailimf_bcc * imf_resent_bcc;
315 struct mailimf_message_id * imf_resent_msg_id;
316 struct mailimf_field * field;
319 imf_resent_date = NULL;
320 imf_resent_from = NULL;
321 imf_resent_sender = NULL;
322 imf_resent_to = NULL;
323 imf_resent_cc = NULL;
324 imf_resent_bcc = NULL;
325 imf_resent_msg_id = NULL;
328 if (resent_date != NULL) {
329 imf_resent_date = mailimf_orig_date_new(resent_date);
330 if (imf_resent_date == NULL)
332 field = mailimf_field_new(MAILIMF_FIELD_RESENT_DATE,
333 NULL /* return-path */,
334 imf_resent_date /* resent date */,
335 NULL /* resent from */,
336 NULL /* resent sender */,
337 NULL /* resent to */,
338 NULL /* resent cc */,
339 NULL /* resent bcc */,
340 NULL /* resent msg id */,
348 NULL /* message id */,
349 NULL /* in reply to */,
350 NULL /* references */,
354 NULL /* optional field */);
357 r = mailimf_fields_add(fields, field);
358 if (r != MAILIMF_NO_ERROR)
362 if (resent_from != NULL) {
363 imf_resent_from = mailimf_from_new(resent_from);
364 if (imf_resent_from == NULL)
366 field = mailimf_field_new(MAILIMF_FIELD_RESENT_FROM,
367 NULL /* return-path */,
368 NULL /* resent date */,
369 imf_resent_from /* resent from */,
370 NULL /* resent sender */,
371 NULL /* resent to */,
372 NULL /* resent cc */,
373 NULL /* resent bcc */,
374 NULL /* resent msg id */,
382 NULL /* message id */,
383 NULL /* in reply to */,
384 NULL /* references */,
388 NULL /* optional field */);
391 r = mailimf_fields_add(fields, field);
392 if (r != MAILIMF_NO_ERROR)
396 if (resent_sender != NULL) {
397 imf_resent_sender = mailimf_sender_new(resent_sender);
398 if (imf_resent_sender == NULL)
400 field = mailimf_field_new(MAILIMF_FIELD_RESENT_SENDER,
401 NULL /* return-path */,
402 NULL /* resent date */,
403 NULL /* resent from */,
404 imf_resent_sender /* resent sender */,
405 NULL /* resent to */,
406 NULL /* resent cc */,
407 NULL /* resent bcc */,
408 NULL /* resent msg id */,
416 NULL /* message id */,
417 NULL /* in reply to */,
418 NULL /* references */,
422 NULL /* optional field */);
425 r = mailimf_fields_add(fields, field);
426 if (r != MAILIMF_NO_ERROR)
430 if (resent_to != NULL) {
431 imf_resent_to = mailimf_to_new(resent_to);
432 if (imf_resent_to == NULL)
434 field = mailimf_field_new(MAILIMF_FIELD_RESENT_TO,
435 NULL /* return-path */,
436 NULL /* resent date */,
437 NULL /* resent from */,
438 NULL /* resent sender */,
439 imf_resent_to /* resent to */,
440 NULL /* resent cc */,
441 NULL /* resent bcc */,
442 NULL /* resent msg id */,
450 NULL /* message id */,
451 NULL /* in reply to */,
452 NULL /* references */,
456 NULL /* optional field */);
459 r = mailimf_fields_add(fields, field);
460 if (r != MAILIMF_NO_ERROR)
464 if (resent_cc != NULL) {
465 imf_resent_cc = mailimf_cc_new(resent_cc);
466 if (imf_resent_cc == NULL)
468 field = mailimf_field_new(MAILIMF_FIELD_RESENT_CC,
469 NULL /* return-path */,
470 NULL /* resent date */,
471 NULL /* resent from */,
472 NULL /* resent sender */,
473 NULL /* resent to */,
474 imf_resent_cc /* resent cc */,
475 NULL /* resent bcc */,
476 NULL /* resent msg id */,
484 NULL /* message id */,
485 NULL /* in reply to */,
486 NULL /* references */,
490 NULL /* optional field */);
493 r = mailimf_fields_add(fields, field);
494 if (r != MAILIMF_NO_ERROR)
498 if (resent_bcc != NULL) {
499 imf_resent_bcc = mailimf_bcc_new(resent_bcc);
500 if (imf_resent_bcc == NULL)
502 field = mailimf_field_new(MAILIMF_FIELD_RESENT_BCC,
503 NULL /* return-path */,
504 NULL /* resent date */,
505 NULL /* resent from */,
506 NULL /* resent sender */,
507 NULL /* resent to */,
508 NULL /* resent cc */,
509 imf_resent_bcc /* resent bcc */,
510 NULL /* resent msg id */,
518 NULL /* message id */,
519 NULL /* in reply to */,
520 NULL /* references */,
524 NULL /* optional field */);
527 r = mailimf_fields_add(fields, field);
528 if (r != MAILIMF_NO_ERROR)
532 if (resent_msg_id != NULL) {
533 imf_resent_msg_id = mailimf_message_id_new(resent_msg_id);
534 if (imf_resent_msg_id == NULL)
536 field = mailimf_field_new(MAILIMF_FIELD_RESENT_MSG_ID,
537 NULL /* return-path */,
538 NULL /* resent date */,
539 NULL /* resent from */,
540 NULL /* resent sender */,
541 NULL /* resent to */,
542 NULL /* resent cc */,
543 NULL /* resent bcc */,
544 imf_resent_msg_id /* resent msg id */,
552 NULL /* message id */,
553 NULL /* in reply to */,
554 NULL /* references */,
558 NULL /* optional field */);
561 r = mailimf_fields_add(fields, field);
562 if (r != MAILIMF_NO_ERROR)
566 return MAILIMF_NO_ERROR;
570 detach_resent_field(field);
573 detach_free_common_fields(imf_resent_date,
580 return MAILIMF_ERROR_MEMORY;
583 struct mailimf_fields *
584 mailimf_resent_fields_new_with_data_all(struct mailimf_date_time *
586 struct mailimf_mailbox_list *
588 struct mailimf_mailbox *
590 struct mailimf_address_list *
592 struct mailimf_address_list *
594 struct mailimf_address_list *
596 char * resent_msg_id)
598 struct mailimf_fields * resent_fields;
601 resent_fields = mailimf_fields_new_empty();
602 if (resent_fields == NULL)
605 r = mailimf_resent_fields_add_data(resent_fields,
606 resent_date, resent_from,
607 resent_sender, resent_to,
608 resent_cc, resent_bcc,
610 if (r != MAILIMF_NO_ERROR)
613 return resent_fields;
616 mailimf_fields_free(resent_fields);
622 struct mailimf_fields *
623 mailimf_resent_fields_new_with_data(struct mailimf_mailbox_list * from,
624 struct mailimf_mailbox * sender,
625 struct mailimf_address_list * to,
626 struct mailimf_address_list * cc,
627 struct mailimf_address_list * bcc)
629 struct mailimf_date_time * date;
631 struct mailimf_fields * fields;
633 date = mailimf_get_current_date();
637 msg_id = mailimf_get_message_id();
641 fields = mailimf_resent_fields_new_with_data_all(date,
642 from, sender, to, cc, bcc, msg_id);
651 mailimf_date_time_free(date);
657 struct mailimf_fields *
658 mailimf_fields_new_empty(void)
661 struct mailimf_fields * fields_list;
667 fields_list = mailimf_fields_new(list);
668 if (fields_list == NULL)
674 int mailimf_fields_add(struct mailimf_fields * fields,
675 struct mailimf_field * field)
679 r = clist_append(fields->fld_list, field);
681 return MAILIMF_ERROR_MEMORY;
683 return MAILIMF_NO_ERROR;
686 static void detach_free_fields(struct mailimf_orig_date * date,
687 struct mailimf_from * from,
688 struct mailimf_sender * sender,
689 struct mailimf_reply_to * reply_to,
690 struct mailimf_to * to,
691 struct mailimf_cc * cc,
692 struct mailimf_bcc * bcc,
693 struct mailimf_message_id * msg_id,
694 struct mailimf_in_reply_to * in_reply_to,
695 struct mailimf_references * references,
696 struct mailimf_subject * subject)
698 detach_free_common_fields(date,
706 if (reply_to != NULL) {
707 reply_to->rt_addr_list = NULL;
708 mailimf_reply_to_free(reply_to);
711 if (in_reply_to != NULL) {
712 in_reply_to->mid_list = NULL;
713 mailimf_in_reply_to_free(in_reply_to);
716 if (references != NULL) {
717 references->mid_list = NULL;
718 mailimf_references_free(references);
721 if (subject != NULL) {
722 subject->sbj_value = NULL;
723 mailimf_subject_free(subject);
728 static void detach_field(struct mailimf_field * field)
730 field->fld_type = MAILIMF_FIELD_NONE;
731 mailimf_field_free(field);
734 int mailimf_fields_add_data(struct mailimf_fields * fields,
735 struct mailimf_date_time * date,
736 struct mailimf_mailbox_list * from,
737 struct mailimf_mailbox * sender,
738 struct mailimf_address_list * reply_to,
739 struct mailimf_address_list * to,
740 struct mailimf_address_list * cc,
741 struct mailimf_address_list * bcc,
747 struct mailimf_orig_date * imf_date;
748 struct mailimf_from * imf_from;
749 struct mailimf_sender * imf_sender;
750 struct mailimf_reply_to * imf_reply_to;
751 struct mailimf_to * imf_to;
752 struct mailimf_cc * imf_cc;
753 struct mailimf_bcc * imf_bcc;
754 struct mailimf_message_id * imf_msg_id;
755 struct mailimf_references * imf_references;
756 struct mailimf_in_reply_to * imf_in_reply_to;
757 struct mailimf_subject * imf_subject;
758 struct mailimf_field * field;
769 imf_references = NULL;
770 imf_in_reply_to = NULL;
775 imf_date = mailimf_orig_date_new(date);
776 if (imf_date == NULL)
778 field = mailimf_field_new(MAILIMF_FIELD_ORIG_DATE,
779 NULL /* return-path */,
780 NULL /* resent date */,
781 NULL /* resent from */,
782 NULL /* resent sender */,
783 NULL /* resent to */,
784 NULL /* resent cc */,
785 NULL /* resent bcc */,
786 NULL /* resent msg id */,
794 NULL /* message id */,
795 NULL /* in reply to */,
796 NULL /* references */,
800 NULL /* optional field */);
803 r = mailimf_fields_add(fields, field);
804 if (r != MAILIMF_NO_ERROR)
809 imf_from = mailimf_from_new(from);
810 if (imf_from == NULL)
812 field = mailimf_field_new(MAILIMF_FIELD_FROM,
813 NULL /* return-path */,
814 NULL /* resent date */,
815 NULL /* resent from */,
816 NULL /* resent sender */,
817 NULL /* resent to */,
818 NULL /* resent cc */,
819 NULL /* resent bcc */,
820 NULL /* resent msg id */,
828 NULL /* message id */,
829 NULL /* in reply to */,
830 NULL /* references */,
834 NULL /* optional field */);
837 r = mailimf_fields_add(fields, field);
838 if (r != MAILIMF_NO_ERROR)
842 if (sender != NULL) {
843 imf_sender = mailimf_sender_new(sender);
844 if (imf_sender == NULL)
846 field = mailimf_field_new(MAILIMF_FIELD_SENDER,
847 NULL /* return-path */,
848 NULL /* resent date */,
849 NULL /* resent from */,
850 NULL /* resent sender */,
851 NULL /* resent to */,
852 NULL /* resent cc */,
853 NULL /* resent bcc */,
854 NULL /* resent msg id */,
857 imf_sender /* sender */,
862 NULL /* message id */,
863 NULL /* in reply to */,
864 NULL /* references */,
868 NULL /* optional field */);
871 r = mailimf_fields_add(fields, field);
872 if (r != MAILIMF_NO_ERROR)
876 if (reply_to != NULL) {
877 imf_reply_to = mailimf_reply_to_new(reply_to);
878 if (imf_reply_to == NULL)
880 field = mailimf_field_new(MAILIMF_FIELD_REPLY_TO,
881 NULL /* return-path */,
882 NULL /* resent date */,
883 NULL /* resent from */,
884 NULL /* resent sender */,
885 NULL /* resent to */,
886 NULL /* resent cc */,
887 NULL /* resent bcc */,
888 NULL /* resent msg id */,
892 imf_reply_to /* reply-to */,
896 NULL /* message id */,
897 NULL /* in reply to */,
898 NULL /* references */,
902 NULL /* optional field */);
905 r = mailimf_fields_add(fields, field);
906 if (r != MAILIMF_NO_ERROR)
911 imf_to = mailimf_to_new(to);
914 field = mailimf_field_new(MAILIMF_FIELD_TO,
915 NULL /* return-path */,
916 NULL /* resent date */,
917 NULL /* resent from */,
918 NULL /* resent sender */,
919 NULL /* resent to */,
920 NULL /* resent cc */,
921 NULL /* resent bcc */,
922 NULL /* resent msg id */,
930 NULL /* message id */,
931 NULL /* in reply to */,
932 NULL /* references */,
936 NULL /* optional field */);
939 r = mailimf_fields_add(fields, field);
940 if (r != MAILIMF_NO_ERROR)
945 imf_cc = mailimf_cc_new(cc);
948 field = mailimf_field_new(MAILIMF_FIELD_CC,
949 NULL /* return-path */,
950 NULL /* resent date */,
951 NULL /* resent from */,
952 NULL /* resent sender */,
953 NULL /* resent to */,
954 NULL /* resent cc */,
955 NULL /* resent bcc */,
956 NULL /* resent msg id */,
964 NULL /* message id */,
965 NULL /* in reply to */,
966 NULL /* references */,
970 NULL /* optional field */);
973 r = mailimf_fields_add(fields, field);
974 if (r != MAILIMF_NO_ERROR)
979 imf_bcc = mailimf_bcc_new(bcc);
982 field = mailimf_field_new(MAILIMF_FIELD_BCC,
983 NULL /* return-path */,
984 NULL /* resent date */,
985 NULL /* resent from */,
986 NULL /* resent sender */,
987 NULL /* resent to */,
988 NULL /* resent cc */,
989 NULL /* resent bcc */,
990 NULL /* resent msg id */,
998 NULL /* message id */,
999 NULL /* in reply to */,
1000 NULL /* references */,
1002 NULL /* comments */,
1003 NULL /* keywords */,
1004 NULL /* optional field */);
1007 r = mailimf_fields_add(fields, field);
1008 if (r != MAILIMF_NO_ERROR)
1012 if (msg_id != NULL) {
1013 imf_msg_id = mailimf_message_id_new(msg_id);
1014 if (imf_msg_id == NULL)
1016 field = mailimf_field_new(MAILIMF_FIELD_MESSAGE_ID,
1017 NULL /* return-path */,
1018 NULL /* resent date */,
1019 NULL /* resent from */,
1020 NULL /* resent sender */,
1021 NULL /* resent to */,
1022 NULL /* resent cc */,
1023 NULL /* resent bcc */,
1024 NULL /* resent msg id */,
1028 NULL /* reply-to */,
1032 imf_msg_id /* message id */,
1033 NULL /* in reply to */,
1034 NULL /* references */,
1036 NULL /* comments */,
1037 NULL /* keywords */,
1038 NULL /* optional field */);
1041 r = mailimf_fields_add(fields, field);
1042 if (r != MAILIMF_NO_ERROR)
1046 if (in_reply_to != NULL) {
1047 imf_in_reply_to = mailimf_in_reply_to_new(in_reply_to);
1048 if (imf_in_reply_to == NULL)
1050 field = mailimf_field_new(MAILIMF_FIELD_IN_REPLY_TO,
1051 NULL /* return-path */,
1052 NULL /* resent date */,
1053 NULL /* resent from */,
1054 NULL /* resent sender */,
1055 NULL /* resent to */,
1056 NULL /* resent cc */,
1057 NULL /* resent bcc */,
1058 NULL /* resent msg id */,
1062 NULL /* reply-to */,
1066 NULL /* message id */,
1067 imf_in_reply_to /* in reply to */,
1068 NULL /* references */,
1070 NULL /* comments */,
1071 NULL /* keywords */,
1072 NULL /* optional field */);
1075 r = mailimf_fields_add(fields, field);
1076 if (r != MAILIMF_NO_ERROR)
1080 if (references != NULL) {
1081 imf_references = mailimf_references_new(references);
1082 if (imf_references == NULL)
1084 field = mailimf_field_new(MAILIMF_FIELD_REFERENCES,
1085 NULL /* return-path */,
1086 NULL /* resent date */,
1087 NULL /* resent from */,
1088 NULL /* resent sender */,
1089 NULL /* resent to */,
1090 NULL /* resent cc */,
1091 NULL /* resent bcc */,
1092 NULL /* resent msg id */,
1096 NULL /* reply-to */,
1100 NULL /* message id */,
1101 NULL /* in reply to */,
1102 imf_references /* references */,
1104 NULL /* comments */,
1105 NULL /* keywords */,
1106 NULL /* optional field */);
1109 r = mailimf_fields_add(fields, field);
1110 if (r != MAILIMF_NO_ERROR)
1114 if (subject != NULL) {
1115 imf_subject = mailimf_subject_new(subject);
1116 if (imf_subject == NULL)
1118 field = mailimf_field_new(MAILIMF_FIELD_SUBJECT,
1119 NULL /* return-path */,
1120 NULL /* resent date */,
1121 NULL /* resent from */,
1122 NULL /* resent sender */,
1123 NULL /* resent to */,
1124 NULL /* resent cc */,
1125 NULL /* resent bcc */,
1126 NULL /* resent msg id */,
1130 NULL /* reply-to */,
1134 NULL /* message id */,
1135 NULL /* in reply to */,
1136 NULL /* references */,
1137 imf_subject /* subject */,
1138 NULL /* comments */,
1139 NULL /* keywords */,
1140 NULL /* optional field */);
1143 r = mailimf_fields_add(fields, field);
1144 if (r != MAILIMF_NO_ERROR)
1148 return MAILIMF_NO_ERROR;
1151 if (field != NULL) {
1152 detach_field(field);
1155 detach_free_fields(imf_date,
1167 return MAILIMF_ERROR_MEMORY;
1170 struct mailimf_fields *
1171 mailimf_fields_new_with_data_all(struct mailimf_date_time * date,
1172 struct mailimf_mailbox_list * from,
1173 struct mailimf_mailbox * sender,
1174 struct mailimf_address_list * reply_to,
1175 struct mailimf_address_list * to,
1176 struct mailimf_address_list * cc,
1177 struct mailimf_address_list * bcc,
1179 clist * in_reply_to,
1183 struct mailimf_fields * fields;
1186 fields = mailimf_fields_new_empty();
1190 r = mailimf_fields_add_data(fields,
1202 if (r != MAILIMF_NO_ERROR)
1208 mailimf_fields_free(fields);
1213 struct mailimf_fields *
1214 mailimf_fields_new_with_data(struct mailimf_mailbox_list * from,
1215 struct mailimf_mailbox * sender,
1216 struct mailimf_address_list * reply_to,
1217 struct mailimf_address_list * to,
1218 struct mailimf_address_list * cc,
1219 struct mailimf_address_list * bcc,
1220 clist * in_reply_to,
1224 struct mailimf_date_time * date;
1226 struct mailimf_fields * fields;
1228 date = mailimf_get_current_date();
1232 msg_id = mailimf_get_message_id();
1236 fields = mailimf_fields_new_with_data_all(date,
1237 from, sender, reply_to,
1240 in_reply_to, references,
1250 mailimf_date_time_free(date);
1257 #define MAX_MESSAGE_ID 512
1259 char * mailimf_get_message_id(void)
1261 char id[MAX_MESSAGE_ID];
1263 char name[MAX_MESSAGE_ID];
1269 gethostname(name, MAX_MESSAGE_ID);
1270 snprintf(id, MAX_MESSAGE_ID, "etPan.%lx.%lx.%x@%s",
1271 now, value, getpid(), name);
1278 static time_t mkgmtime(struct tm * tmp);
1281 struct mailimf_date_time * mailimf_get_current_date(void)
1287 struct mailimf_date_time * date_time;
1291 if (gmtime_r(&now, &gmt) == NULL)
1294 if (localtime_r(&now, <) == NULL)
1297 off = (mkgmtime(<) - mkgmtime(&gmt)) / (60 * 60) * 100;
1299 date_time = mailimf_date_time_new(lt.tm_mday, lt.tm_mon + 1, lt.tm_year + 1900,
1300 lt.tm_hour, lt.tm_min, lt.tm_sec,
1308 /* mkgmtime.c - make time corresponding to a GMT timeval struct
1311 * Copyright (c) 1998-2000 Carnegie Mellon University. All rights reserved.
1313 * Redistribution and use in source and binary forms, with or without
1314 * modification, are permitted provided that the following conditions
1317 * 1. Redistributions of source code must retain the above copyright
1318 * notice, this list of conditions and the following disclaimer.
1320 * 2. Redistributions in binary form must reproduce the above copyright
1321 * notice, this list of conditions and the following disclaimer in
1322 * the documentation and/or other materials provided with the
1325 * 3. The name "Carnegie Mellon University" must not be used to
1326 * endorse or promote products derived from this software without
1327 * prior written permission. For permission or any other legal
1328 * details, please contact
1329 * Office of Technology Transfer
1330 * Carnegie Mellon University
1331 * 5000 Forbes Avenue
1332 * Pittsburgh, PA 15213-3890
1333 * (412) 268-4387, fax: (412) 268-7395
1334 * tech-transfer@andrew.cmu.edu
1336 * 4. Redistributions of any form whatsoever must retain the following
1338 * "This product includes software developed by Computing Services
1339 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
1341 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
1342 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1343 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
1344 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1345 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
1346 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1347 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1352 * Copyright (c) 1987, 1989, 1993
1353 * The Regents of the University of California. All rights reserved.
1355 * This code is derived from software contributed to Berkeley by
1356 * Arthur David Olson of the National Cancer Institute.
1358 * Redistribution and use in source and binary forms, with or without
1359 * modification, are permitted provided that the following conditions
1361 * 1. Redistributions of source code must retain the above copyright
1362 * notice, this list of conditions and the following disclaimer.
1363 * 2. Redistributions in binary form must reproduce the above copyright
1364 * notice, this list of conditions and the following disclaimer in the
1365 * documentation and/or other materials provided with the distribution.
1366 * 3. All advertising materials mentioning features or use of this software
1367 * must display the following acknowledgement:
1368 * This product includes software developed by the University of
1369 * California, Berkeley and its contributors.
1370 * 4. Neither the name of the University nor the names of its contributors
1371 * may be used to endorse or promote products derived from this software
1372 * without specific prior written permission.
1374 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1375 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1376 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1377 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1378 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1379 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1380 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1381 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1382 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1383 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1388 ** Adapted from code provided by Robert Elz, who writes:
1389 ** The "best" way to do mktime I think is based on an idea of Bob
1390 ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
1391 ** It does a binary search of the time_t space. Since time_t's are
1392 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1393 ** would still be very reasonable).
1397 adapted for libEtPan! by DINH V. Hoa
1402 #endif /* !defined WRONG */
1404 static int tmcomp(struct tm * atmp, struct tm * btmp)
1406 register int result;
1408 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1409 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1410 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1411 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1412 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1413 result = atmp->tm_sec - btmp->tm_sec;
1417 static time_t mkgmtime(struct tm * tmp)
1421 register int saved_seconds;
1423 struct tm yourtm, *mytm, buft;
1426 saved_seconds = yourtm.tm_sec;
1429 ** Calculate the number of magnitude bits in a time_t
1430 ** (this works regardless of whether time_t is
1431 ** signed or unsigned, though lint complains if unsigned).
1433 for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
1436 ** If time_t is signed, then 0 is the median value,
1437 ** if time_t is unsigned, then 1 << bits is median.
1439 t = (t < 0) ? 0 : ((time_t) 1 << bits);
1441 mytm = gmtime_r(&t, &buft);
1442 dir = tmcomp(mytm, &yourtm);
1449 t -= (time_t) 1 << bits;
1450 else t += (time_t) 1 << bits;
1465 void mailimf_single_fields_init(struct mailimf_single_fields * single_fields,
1466 struct mailimf_fields * fields)
1470 memset(single_fields, 0, sizeof(struct mailimf_single_fields));
1472 cur = clist_begin(fields->fld_list);
1473 while (cur != NULL) {
1474 struct mailimf_field * field;
1476 field = clist_content(cur);
1478 switch (field->fld_type) {
1479 case MAILIMF_FIELD_ORIG_DATE:
1480 if (single_fields->fld_orig_date == NULL)
1481 single_fields->fld_orig_date = field->fld_data.fld_orig_date;
1482 cur = clist_next(cur);
1484 case MAILIMF_FIELD_FROM:
1485 if (single_fields->fld_from == NULL) {
1486 single_fields->fld_from = field->fld_data.fld_from;
1487 cur = clist_next(cur);
1490 clist_concat(single_fields->fld_from->frm_mb_list->mb_list,
1491 field->fld_data.fld_from->frm_mb_list->mb_list);
1492 mailimf_field_free(field);
1493 cur = clist_delete(fields->fld_list, cur);
1496 case MAILIMF_FIELD_SENDER:
1497 if (single_fields->fld_sender == NULL)
1498 single_fields->fld_sender = field->fld_data.fld_sender;
1499 cur = clist_next(cur);
1501 case MAILIMF_FIELD_REPLY_TO:
1502 if (single_fields->fld_reply_to == NULL) {
1503 single_fields->fld_reply_to = field->fld_data.fld_reply_to;
1504 cur = clist_next(cur);
1507 clist_concat(single_fields->fld_reply_to->rt_addr_list->ad_list,
1508 field->fld_data.fld_reply_to->rt_addr_list->ad_list);
1509 mailimf_field_free(field);
1510 cur = clist_delete(fields->fld_list, cur);
1513 case MAILIMF_FIELD_TO:
1514 if (single_fields->fld_to == NULL) {
1515 single_fields->fld_to = field->fld_data.fld_to;
1516 cur = clist_next(cur);
1519 clist_concat(single_fields->fld_to->to_addr_list->ad_list,
1520 field->fld_data.fld_to->to_addr_list->ad_list);
1521 mailimf_field_free(field);
1522 cur = clist_delete(fields->fld_list, cur);
1525 case MAILIMF_FIELD_CC:
1526 if (single_fields->fld_cc == NULL) {
1527 single_fields->fld_cc = field->fld_data.fld_cc;
1528 cur = clist_next(cur);
1531 clist_concat(single_fields->fld_cc->cc_addr_list->ad_list,
1532 field->fld_data.fld_cc->cc_addr_list->ad_list);
1533 mailimf_field_free(field);
1534 cur = clist_delete(fields->fld_list, cur);
1537 case MAILIMF_FIELD_BCC:
1538 if (single_fields->fld_bcc == NULL) {
1539 single_fields->fld_bcc = field->fld_data.fld_bcc;
1540 cur = clist_next(cur);
1543 clist_concat(single_fields->fld_bcc->bcc_addr_list->ad_list,
1544 field->fld_data.fld_bcc->bcc_addr_list->ad_list);
1545 mailimf_field_free(field);
1546 cur = clist_delete(fields->fld_list, cur);
1549 case MAILIMF_FIELD_MESSAGE_ID:
1550 if (single_fields->fld_message_id == NULL)
1551 single_fields->fld_message_id = field->fld_data.fld_message_id;
1552 cur = clist_next(cur);
1554 case MAILIMF_FIELD_IN_REPLY_TO:
1555 if (single_fields->fld_in_reply_to == NULL)
1556 single_fields->fld_in_reply_to = field->fld_data.fld_in_reply_to;
1557 cur = clist_next(cur);
1559 case MAILIMF_FIELD_REFERENCES:
1560 if (single_fields->fld_references == NULL)
1561 single_fields->fld_references = field->fld_data.fld_references;
1562 cur = clist_next(cur);
1564 case MAILIMF_FIELD_SUBJECT:
1565 if (single_fields->fld_subject == NULL)
1566 single_fields->fld_subject = field->fld_data.fld_subject;
1567 cur = clist_next(cur);
1569 case MAILIMF_FIELD_COMMENTS:
1570 if (single_fields->fld_comments == NULL)
1571 single_fields->fld_comments = field->fld_data.fld_comments;
1572 cur = clist_next(cur);
1574 case MAILIMF_FIELD_KEYWORDS:
1575 if (single_fields->fld_keywords == NULL)
1576 single_fields->fld_keywords = field->fld_data.fld_keywords;
1577 cur = clist_next(cur);
1580 cur = clist_next(cur);
1587 struct mailimf_single_fields *
1588 mailimf_single_fields_new(struct mailimf_fields * fields)
1590 struct mailimf_single_fields * single_fields;
1592 single_fields = malloc(sizeof(struct mailimf_single_fields));
1593 if (single_fields == NULL)
1596 mailimf_single_fields_init(single_fields, fields);
1598 return single_fields;
1604 void mailimf_single_fields_free(struct mailimf_single_fields *
1607 free(single_fields);
1610 struct mailimf_field * mailimf_field_new_custom(char * name, char * value)
1612 struct mailimf_optional_field * opt_field;
1613 struct mailimf_field * field;
1615 opt_field = mailimf_optional_field_new(name, value);
1616 if (opt_field == NULL)
1619 field = mailimf_field_new(MAILIMF_FIELD_OPTIONAL_FIELD,
1620 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1621 NULL, NULL, NULL, NULL,
1622 NULL, NULL, NULL, NULL,
1623 NULL, NULL, NULL, NULL,
1624 NULL, NULL, opt_field);
1626 goto free_opt_field;
1631 mailimf_optional_field_free(opt_field);