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"
43 struct mailimf_mailbox_list *
44 mailimf_mailbox_list_new_empty()
47 struct mailimf_mailbox_list * mb_list;
53 mb_list = mailimf_mailbox_list_new(list);
60 int mailimf_mailbox_list_add(struct mailimf_mailbox_list * mailbox_list,
61 struct mailimf_mailbox * mb)
65 r = clist_append(mailbox_list->mb_list, mb);
67 return MAILIMF_ERROR_MEMORY;
69 return MAILIMF_NO_ERROR;
72 int mailimf_mailbox_list_add_parse(struct mailimf_mailbox_list * mailbox_list,
77 struct mailimf_mailbox * mb;
81 r = mailimf_mailbox_parse(mb_str, strlen(mb_str), &cur_token, &mb);
82 if (r != MAILIMF_NO_ERROR) {
87 r = mailimf_mailbox_list_add(mailbox_list, mb);
88 if (r != MAILIMF_NO_ERROR) {
93 return MAILIMF_NO_ERROR;
96 mailimf_mailbox_free(mb);
101 int mailimf_mailbox_list_add_mb(struct mailimf_mailbox_list * mailbox_list,
102 char * display_name, char * address)
105 struct mailimf_mailbox * mb;
108 mb = mailimf_mailbox_new(display_name, address);
110 res = MAILIMF_ERROR_MEMORY;
114 r = mailimf_mailbox_list_add(mailbox_list, mb);
115 if (r != MAILIMF_NO_ERROR) {
120 return MAILIMF_NO_ERROR;
123 mailimf_mailbox_free(mb);
130 struct mailimf_address_list *
131 mailimf_address_list_new_empty()
134 struct mailimf_address_list * addr_list;
140 addr_list = mailimf_address_list_new(list);
141 if (addr_list == NULL)
147 int mailimf_address_list_add(struct mailimf_address_list * address_list,
148 struct mailimf_address * addr)
152 r = clist_append(address_list->ad_list, addr);
154 return MAILIMF_ERROR_MEMORY;
156 return MAILIMF_NO_ERROR;
159 int mailimf_address_list_add_parse(struct mailimf_address_list * address_list,
164 struct mailimf_address * addr;
168 r = mailimf_address_parse(addr_str, strlen(addr_str), &cur_token, &addr);
169 if (r != MAILIMF_NO_ERROR) {
174 r = mailimf_address_list_add(address_list, addr);
175 if (r != MAILIMF_NO_ERROR) {
180 return MAILIMF_NO_ERROR;
183 mailimf_address_free(addr);
188 int mailimf_address_list_add_mb(struct mailimf_address_list * address_list,
189 char * display_name, char * address)
192 struct mailimf_mailbox * mb;
193 struct mailimf_address * addr;
196 mb = mailimf_mailbox_new(display_name, address);
198 res = MAILIMF_ERROR_MEMORY;
202 addr = mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mb, NULL);
204 res = MAILIMF_ERROR_MEMORY;
208 r = mailimf_address_list_add(address_list, addr);
209 if (r != MAILIMF_NO_ERROR) {
214 return MAILIMF_NO_ERROR;
217 mailimf_address_free(addr);
219 mailimf_mailbox_free(mb);
226 struct mailimf_resent_fields_list *
227 mailimf_resent_fields_list_new_empty()
230 struct mailimf_resent_fields_list * rf_list;
236 rf_list = mailimf_resent_fields_list_new(list);
243 int mailimf_resent_fields_add(struct mailimf_resent_fields_list * fields,
244 struct mailimf_resent_field * field)
248 r = clist_append(fields->list, field);
250 return MAILIMF_ERROR_MEMORY;
252 return MAILIMF_NO_ERROR;
257 static void detach_free_common_fields(struct mailimf_orig_date * imf_date,
258 struct mailimf_from * imf_from,
259 struct mailimf_sender * imf_sender,
260 struct mailimf_to * imf_to,
261 struct mailimf_cc * imf_cc,
262 struct mailimf_bcc * imf_bcc,
263 struct mailimf_message_id * imf_msg_id)
265 if (imf_date != NULL) {
266 imf_date->dt_date_time = NULL;
267 mailimf_orig_date_free(imf_date);
269 if (imf_from != NULL) {
270 imf_from->frm_mb_list = NULL;
271 mailimf_from_free(imf_from);
273 if (imf_sender != NULL) {
274 imf_sender->snd_mb = NULL;
275 mailimf_sender_free(imf_sender);
277 if (imf_to != NULL) {
278 imf_to->to_addr_list = NULL;
279 mailimf_to_free(imf_to);
281 if (imf_cc != NULL) {
282 imf_cc->cc_addr_list = NULL;
283 mailimf_cc_free(imf_cc);
285 if (imf_bcc != NULL) {
286 imf_bcc->bcc_addr_list = NULL;
287 mailimf_bcc_free(imf_bcc);
289 if (imf_msg_id != NULL) {
290 imf_msg_id->mid_value = NULL;
291 mailimf_message_id_free(imf_msg_id);
295 static void detach_resent_field(struct mailimf_field * field)
297 field->fld_type = MAILIMF_FIELD_NONE;
298 mailimf_field_free(field);
302 mailimf_resent_fields_add_data(struct mailimf_fields * fields,
303 struct mailimf_date_time * resent_date,
304 struct mailimf_mailbox_list * resent_from,
305 struct mailimf_mailbox * resent_sender,
306 struct mailimf_address_list * resent_to,
307 struct mailimf_address_list * resent_cc,
308 struct mailimf_address_list * resent_bcc,
309 char * resent_msg_id)
311 struct mailimf_orig_date * imf_resent_date;
312 struct mailimf_from * imf_resent_from;
313 struct mailimf_sender * imf_resent_sender;
314 struct mailimf_to * imf_resent_to;
315 struct mailimf_cc * imf_resent_cc;
316 struct mailimf_bcc * imf_resent_bcc;
317 struct mailimf_message_id * imf_resent_msg_id;
318 struct mailimf_field * field;
321 imf_resent_date = NULL;
322 imf_resent_from = NULL;
323 imf_resent_sender = NULL;
324 imf_resent_to = NULL;
325 imf_resent_cc = NULL;
326 imf_resent_bcc = NULL;
327 imf_resent_msg_id = NULL;
330 if (resent_date != NULL) {
331 imf_resent_date = mailimf_orig_date_new(resent_date);
332 if (imf_resent_date == NULL)
334 field = mailimf_field_new(MAILIMF_FIELD_RESENT_DATE,
335 NULL /* return-path */,
336 imf_resent_date /* resent date */,
337 NULL /* resent from */,
338 NULL /* resent sender */,
339 NULL /* resent to */,
340 NULL /* resent cc */,
341 NULL /* resent bcc */,
342 NULL /* resent msg id */,
350 NULL /* message id */,
351 NULL /* in reply to */,
352 NULL /* references */,
356 NULL /* optional field */);
359 r = mailimf_fields_add(fields, field);
360 if (r != MAILIMF_NO_ERROR)
364 if (resent_from != NULL) {
365 imf_resent_from = mailimf_from_new(resent_from);
366 if (imf_resent_from == NULL)
368 field = mailimf_field_new(MAILIMF_FIELD_RESENT_FROM,
369 NULL /* return-path */,
370 NULL /* resent date */,
371 imf_resent_from /* resent from */,
372 NULL /* resent sender */,
373 NULL /* resent to */,
374 NULL /* resent cc */,
375 NULL /* resent bcc */,
376 NULL /* resent msg id */,
384 NULL /* message id */,
385 NULL /* in reply to */,
386 NULL /* references */,
390 NULL /* optional field */);
393 r = mailimf_fields_add(fields, field);
394 if (r != MAILIMF_NO_ERROR)
398 if (resent_sender != NULL) {
399 imf_resent_sender = mailimf_sender_new(resent_sender);
400 if (imf_resent_sender == NULL)
402 field = mailimf_field_new(MAILIMF_FIELD_RESENT_SENDER,
403 NULL /* return-path */,
404 NULL /* resent date */,
405 NULL /* resent from */,
406 imf_resent_sender /* resent sender */,
407 NULL /* resent to */,
408 NULL /* resent cc */,
409 NULL /* resent bcc */,
410 NULL /* resent msg id */,
418 NULL /* message id */,
419 NULL /* in reply to */,
420 NULL /* references */,
424 NULL /* optional field */);
427 r = mailimf_fields_add(fields, field);
428 if (r != MAILIMF_NO_ERROR)
432 if (resent_to != NULL) {
433 imf_resent_to = mailimf_to_new(resent_to);
434 if (imf_resent_to == NULL)
436 field = mailimf_field_new(MAILIMF_FIELD_RESENT_TO,
437 NULL /* return-path */,
438 NULL /* resent date */,
439 NULL /* resent from */,
440 NULL /* resent sender */,
441 imf_resent_to /* resent to */,
442 NULL /* resent cc */,
443 NULL /* resent bcc */,
444 NULL /* resent msg id */,
452 NULL /* message id */,
453 NULL /* in reply to */,
454 NULL /* references */,
458 NULL /* optional field */);
461 r = mailimf_fields_add(fields, field);
462 if (r != MAILIMF_NO_ERROR)
466 if (resent_cc != NULL) {
467 imf_resent_cc = mailimf_cc_new(resent_cc);
468 if (imf_resent_cc == NULL)
470 field = mailimf_field_new(MAILIMF_FIELD_RESENT_CC,
471 NULL /* return-path */,
472 NULL /* resent date */,
473 NULL /* resent from */,
474 NULL /* resent sender */,
475 NULL /* resent to */,
476 imf_resent_cc /* resent cc */,
477 NULL /* resent bcc */,
478 NULL /* resent msg id */,
486 NULL /* message id */,
487 NULL /* in reply to */,
488 NULL /* references */,
492 NULL /* optional field */);
495 r = mailimf_fields_add(fields, field);
496 if (r != MAILIMF_NO_ERROR)
500 if (resent_bcc != NULL) {
501 imf_resent_bcc = mailimf_bcc_new(resent_bcc);
502 if (imf_resent_bcc == NULL)
504 field = mailimf_field_new(MAILIMF_FIELD_RESENT_BCC,
505 NULL /* return-path */,
506 NULL /* resent date */,
507 NULL /* resent from */,
508 NULL /* resent sender */,
509 NULL /* resent to */,
510 NULL /* resent cc */,
511 imf_resent_bcc /* resent bcc */,
512 NULL /* resent msg id */,
520 NULL /* message id */,
521 NULL /* in reply to */,
522 NULL /* references */,
526 NULL /* optional field */);
529 r = mailimf_fields_add(fields, field);
530 if (r != MAILIMF_NO_ERROR)
534 if (resent_msg_id != NULL) {
535 imf_resent_msg_id = mailimf_message_id_new(resent_msg_id);
536 if (imf_resent_msg_id == NULL)
538 field = mailimf_field_new(MAILIMF_FIELD_RESENT_MSG_ID,
539 NULL /* return-path */,
540 NULL /* resent date */,
541 NULL /* resent from */,
542 NULL /* resent sender */,
543 NULL /* resent to */,
544 NULL /* resent cc */,
545 NULL /* resent bcc */,
546 imf_resent_msg_id /* resent msg id */,
554 NULL /* message id */,
555 NULL /* in reply to */,
556 NULL /* references */,
560 NULL /* optional field */);
563 r = mailimf_fields_add(fields, field);
564 if (r != MAILIMF_NO_ERROR)
568 return MAILIMF_NO_ERROR;
572 detach_resent_field(field);
575 detach_free_common_fields(imf_resent_date,
582 return MAILIMF_ERROR_MEMORY;
585 struct mailimf_fields *
586 mailimf_resent_fields_new_with_data_all(struct mailimf_date_time *
588 struct mailimf_mailbox_list *
590 struct mailimf_mailbox *
592 struct mailimf_address_list *
594 struct mailimf_address_list *
596 struct mailimf_address_list *
598 char * resent_msg_id)
600 struct mailimf_fields * resent_fields;
603 resent_fields = mailimf_fields_new_empty();
604 if (resent_fields == NULL)
607 r = mailimf_resent_fields_add_data(resent_fields,
608 resent_date, resent_from,
609 resent_sender, resent_to,
610 resent_cc, resent_bcc,
612 if (r != MAILIMF_NO_ERROR)
615 return resent_fields;
618 mailimf_fields_free(resent_fields);
624 struct mailimf_fields *
625 mailimf_resent_fields_new_with_data(struct mailimf_mailbox_list * from,
626 struct mailimf_mailbox * sender,
627 struct mailimf_address_list * to,
628 struct mailimf_address_list * cc,
629 struct mailimf_address_list * bcc)
631 struct mailimf_date_time * date;
633 struct mailimf_fields * fields;
635 date = mailimf_get_current_date();
639 msg_id = mailimf_get_message_id();
643 fields = mailimf_resent_fields_new_with_data_all(date,
644 from, sender, to, cc, bcc, msg_id);
653 mailimf_date_time_free(date);
659 struct mailimf_fields *
660 mailimf_fields_new_empty(void)
663 struct mailimf_fields * fields_list;
669 fields_list = mailimf_fields_new(list);
670 if (fields_list == NULL)
676 int mailimf_fields_add(struct mailimf_fields * fields,
677 struct mailimf_field * field)
681 r = clist_append(fields->fld_list, field);
683 return MAILIMF_ERROR_MEMORY;
685 return MAILIMF_NO_ERROR;
688 static void detach_free_fields(struct mailimf_orig_date * date,
689 struct mailimf_from * from,
690 struct mailimf_sender * sender,
691 struct mailimf_reply_to * reply_to,
692 struct mailimf_to * to,
693 struct mailimf_cc * cc,
694 struct mailimf_bcc * bcc,
695 struct mailimf_message_id * msg_id,
696 struct mailimf_in_reply_to * in_reply_to,
697 struct mailimf_references * references,
698 struct mailimf_subject * subject)
700 detach_free_common_fields(date,
708 if (reply_to != NULL) {
709 reply_to->rt_addr_list = NULL;
710 mailimf_reply_to_free(reply_to);
713 if (in_reply_to != NULL) {
714 in_reply_to->mid_list = NULL;
715 mailimf_in_reply_to_free(in_reply_to);
718 if (references != NULL) {
719 references->mid_list = NULL;
720 mailimf_references_free(references);
723 if (subject != NULL) {
724 subject->sbj_value = NULL;
725 mailimf_subject_free(subject);
730 static void detach_field(struct mailimf_field * field)
732 field->fld_type = MAILIMF_FIELD_NONE;
733 mailimf_field_free(field);
736 int mailimf_fields_add_data(struct mailimf_fields * fields,
737 struct mailimf_date_time * date,
738 struct mailimf_mailbox_list * from,
739 struct mailimf_mailbox * sender,
740 struct mailimf_address_list * reply_to,
741 struct mailimf_address_list * to,
742 struct mailimf_address_list * cc,
743 struct mailimf_address_list * bcc,
749 struct mailimf_orig_date * imf_date;
750 struct mailimf_from * imf_from;
751 struct mailimf_sender * imf_sender;
752 struct mailimf_reply_to * imf_reply_to;
753 struct mailimf_to * imf_to;
754 struct mailimf_cc * imf_cc;
755 struct mailimf_bcc * imf_bcc;
756 struct mailimf_message_id * imf_msg_id;
757 struct mailimf_references * imf_references;
758 struct mailimf_in_reply_to * imf_in_reply_to;
759 struct mailimf_subject * imf_subject;
760 struct mailimf_field * field;
771 imf_references = NULL;
772 imf_in_reply_to = NULL;
777 imf_date = mailimf_orig_date_new(date);
778 if (imf_date == NULL)
780 field = mailimf_field_new(MAILIMF_FIELD_ORIG_DATE,
781 NULL /* return-path */,
782 NULL /* resent date */,
783 NULL /* resent from */,
784 NULL /* resent sender */,
785 NULL /* resent to */,
786 NULL /* resent cc */,
787 NULL /* resent bcc */,
788 NULL /* resent msg id */,
796 NULL /* message id */,
797 NULL /* in reply to */,
798 NULL /* references */,
802 NULL /* optional field */);
805 r = mailimf_fields_add(fields, field);
806 if (r != MAILIMF_NO_ERROR)
811 imf_from = mailimf_from_new(from);
812 if (imf_from == NULL)
814 field = mailimf_field_new(MAILIMF_FIELD_FROM,
815 NULL /* return-path */,
816 NULL /* resent date */,
817 NULL /* resent from */,
818 NULL /* resent sender */,
819 NULL /* resent to */,
820 NULL /* resent cc */,
821 NULL /* resent bcc */,
822 NULL /* resent msg id */,
830 NULL /* message id */,
831 NULL /* in reply to */,
832 NULL /* references */,
836 NULL /* optional field */);
839 r = mailimf_fields_add(fields, field);
840 if (r != MAILIMF_NO_ERROR)
844 if (sender != NULL) {
845 imf_sender = mailimf_sender_new(sender);
846 if (imf_sender == NULL)
848 field = mailimf_field_new(MAILIMF_FIELD_SENDER,
849 NULL /* return-path */,
850 NULL /* resent date */,
851 NULL /* resent from */,
852 NULL /* resent sender */,
853 NULL /* resent to */,
854 NULL /* resent cc */,
855 NULL /* resent bcc */,
856 NULL /* resent msg id */,
859 imf_sender /* sender */,
864 NULL /* message id */,
865 NULL /* in reply to */,
866 NULL /* references */,
870 NULL /* optional field */);
873 r = mailimf_fields_add(fields, field);
874 if (r != MAILIMF_NO_ERROR)
878 if (reply_to != NULL) {
879 imf_reply_to = mailimf_reply_to_new(reply_to);
880 if (imf_reply_to == NULL)
882 field = mailimf_field_new(MAILIMF_FIELD_REPLY_TO,
883 NULL /* return-path */,
884 NULL /* resent date */,
885 NULL /* resent from */,
886 NULL /* resent sender */,
887 NULL /* resent to */,
888 NULL /* resent cc */,
889 NULL /* resent bcc */,
890 NULL /* resent msg id */,
894 imf_reply_to /* reply-to */,
898 NULL /* message id */,
899 NULL /* in reply to */,
900 NULL /* references */,
904 NULL /* optional field */);
907 r = mailimf_fields_add(fields, field);
908 if (r != MAILIMF_NO_ERROR)
913 imf_to = mailimf_to_new(to);
916 field = mailimf_field_new(MAILIMF_FIELD_TO,
917 NULL /* return-path */,
918 NULL /* resent date */,
919 NULL /* resent from */,
920 NULL /* resent sender */,
921 NULL /* resent to */,
922 NULL /* resent cc */,
923 NULL /* resent bcc */,
924 NULL /* resent msg id */,
932 NULL /* message id */,
933 NULL /* in reply to */,
934 NULL /* references */,
938 NULL /* optional field */);
941 r = mailimf_fields_add(fields, field);
942 if (r != MAILIMF_NO_ERROR)
947 imf_cc = mailimf_cc_new(cc);
950 field = mailimf_field_new(MAILIMF_FIELD_CC,
951 NULL /* return-path */,
952 NULL /* resent date */,
953 NULL /* resent from */,
954 NULL /* resent sender */,
955 NULL /* resent to */,
956 NULL /* resent cc */,
957 NULL /* resent bcc */,
958 NULL /* resent msg id */,
966 NULL /* message id */,
967 NULL /* in reply to */,
968 NULL /* references */,
972 NULL /* optional field */);
975 r = mailimf_fields_add(fields, field);
976 if (r != MAILIMF_NO_ERROR)
981 imf_bcc = mailimf_bcc_new(bcc);
984 field = mailimf_field_new(MAILIMF_FIELD_BCC,
985 NULL /* return-path */,
986 NULL /* resent date */,
987 NULL /* resent from */,
988 NULL /* resent sender */,
989 NULL /* resent to */,
990 NULL /* resent cc */,
991 NULL /* resent bcc */,
992 NULL /* resent msg id */,
1000 NULL /* message id */,
1001 NULL /* in reply to */,
1002 NULL /* references */,
1004 NULL /* comments */,
1005 NULL /* keywords */,
1006 NULL /* optional field */);
1009 r = mailimf_fields_add(fields, field);
1010 if (r != MAILIMF_NO_ERROR)
1014 if (msg_id != NULL) {
1015 imf_msg_id = mailimf_message_id_new(msg_id);
1016 if (imf_msg_id == NULL)
1018 field = mailimf_field_new(MAILIMF_FIELD_MESSAGE_ID,
1019 NULL /* return-path */,
1020 NULL /* resent date */,
1021 NULL /* resent from */,
1022 NULL /* resent sender */,
1023 NULL /* resent to */,
1024 NULL /* resent cc */,
1025 NULL /* resent bcc */,
1026 NULL /* resent msg id */,
1030 NULL /* reply-to */,
1034 imf_msg_id /* message id */,
1035 NULL /* in reply to */,
1036 NULL /* references */,
1038 NULL /* comments */,
1039 NULL /* keywords */,
1040 NULL /* optional field */);
1043 r = mailimf_fields_add(fields, field);
1044 if (r != MAILIMF_NO_ERROR)
1048 if (in_reply_to != NULL) {
1049 imf_in_reply_to = mailimf_in_reply_to_new(in_reply_to);
1050 if (imf_in_reply_to == NULL)
1052 field = mailimf_field_new(MAILIMF_FIELD_IN_REPLY_TO,
1053 NULL /* return-path */,
1054 NULL /* resent date */,
1055 NULL /* resent from */,
1056 NULL /* resent sender */,
1057 NULL /* resent to */,
1058 NULL /* resent cc */,
1059 NULL /* resent bcc */,
1060 NULL /* resent msg id */,
1064 NULL /* reply-to */,
1068 NULL /* message id */,
1069 imf_in_reply_to /* in reply to */,
1070 NULL /* references */,
1072 NULL /* comments */,
1073 NULL /* keywords */,
1074 NULL /* optional field */);
1077 r = mailimf_fields_add(fields, field);
1078 if (r != MAILIMF_NO_ERROR)
1082 if (references != NULL) {
1083 imf_references = mailimf_references_new(references);
1084 if (imf_references == NULL)
1086 field = mailimf_field_new(MAILIMF_FIELD_REFERENCES,
1087 NULL /* return-path */,
1088 NULL /* resent date */,
1089 NULL /* resent from */,
1090 NULL /* resent sender */,
1091 NULL /* resent to */,
1092 NULL /* resent cc */,
1093 NULL /* resent bcc */,
1094 NULL /* resent msg id */,
1098 NULL /* reply-to */,
1102 NULL /* message id */,
1103 NULL /* in reply to */,
1104 imf_references /* references */,
1106 NULL /* comments */,
1107 NULL /* keywords */,
1108 NULL /* optional field */);
1111 r = mailimf_fields_add(fields, field);
1112 if (r != MAILIMF_NO_ERROR)
1116 if (subject != NULL) {
1117 imf_subject = mailimf_subject_new(subject);
1118 if (imf_subject == NULL)
1120 field = mailimf_field_new(MAILIMF_FIELD_SUBJECT,
1121 NULL /* return-path */,
1122 NULL /* resent date */,
1123 NULL /* resent from */,
1124 NULL /* resent sender */,
1125 NULL /* resent to */,
1126 NULL /* resent cc */,
1127 NULL /* resent bcc */,
1128 NULL /* resent msg id */,
1132 NULL /* reply-to */,
1136 NULL /* message id */,
1137 NULL /* in reply to */,
1138 NULL /* references */,
1139 imf_subject /* subject */,
1140 NULL /* comments */,
1141 NULL /* keywords */,
1142 NULL /* optional field */);
1145 r = mailimf_fields_add(fields, field);
1146 if (r != MAILIMF_NO_ERROR)
1150 return MAILIMF_NO_ERROR;
1153 if (field != NULL) {
1154 detach_field(field);
1157 detach_free_fields(imf_date,
1169 return MAILIMF_ERROR_MEMORY;
1172 struct mailimf_fields *
1173 mailimf_fields_new_with_data_all(struct mailimf_date_time * date,
1174 struct mailimf_mailbox_list * from,
1175 struct mailimf_mailbox * sender,
1176 struct mailimf_address_list * reply_to,
1177 struct mailimf_address_list * to,
1178 struct mailimf_address_list * cc,
1179 struct mailimf_address_list * bcc,
1181 clist * in_reply_to,
1185 struct mailimf_fields * fields;
1188 fields = mailimf_fields_new_empty();
1192 r = mailimf_fields_add_data(fields,
1204 if (r != MAILIMF_NO_ERROR)
1210 mailimf_fields_free(fields);
1215 struct mailimf_fields *
1216 mailimf_fields_new_with_data(struct mailimf_mailbox_list * from,
1217 struct mailimf_mailbox * sender,
1218 struct mailimf_address_list * reply_to,
1219 struct mailimf_address_list * to,
1220 struct mailimf_address_list * cc,
1221 struct mailimf_address_list * bcc,
1222 clist * in_reply_to,
1226 struct mailimf_date_time * date;
1228 struct mailimf_fields * fields;
1230 date = mailimf_get_current_date();
1234 msg_id = mailimf_get_message_id();
1238 fields = mailimf_fields_new_with_data_all(date,
1239 from, sender, reply_to,
1242 in_reply_to, references,
1252 mailimf_date_time_free(date);
1259 #define MAX_MESSAGE_ID 512
1261 char * mailimf_get_message_id(void)
1263 char id[MAX_MESSAGE_ID];
1265 char name[HOST_NAME_MAX];
1272 /* It's unlikely that HOST_NAME_MAX goes above 64, but let's
1273 * leave a generous reserve for the hostname in the message
1275 if (HOST_NAME_MAX > MAX_MESSAGE_ID - 64 ||
1276 (ret = gethostname(name, HOST_NAME_MAX)) != 0) {
1278 perror("gethostname");
1279 strncpy(name, "unknown", HOST_NAME_MAX);
1282 snprintf(id, MAX_MESSAGE_ID, "etPan.%llx.%lx.%x@%s",
1283 (long long)now, value, getpid(), name);
1290 static time_t mkgmtime(struct tm * tmp);
1293 struct mailimf_date_time * mailimf_get_current_date(void)
1299 struct mailimf_date_time * date_time;
1303 if (gmtime_r(&now, &gmt) == NULL)
1306 if (localtime_r(&now, <) == NULL)
1309 off = (mkgmtime(<) - mkgmtime(&gmt)) / (60 * 60) * 100;
1311 date_time = mailimf_date_time_new(lt.tm_mday, lt.tm_mon + 1, lt.tm_year + 1900,
1312 lt.tm_hour, lt.tm_min, lt.tm_sec,
1320 /* mkgmtime.c - make time corresponding to a GMT timeval struct
1323 * Copyright (c) 1998-2000 Carnegie Mellon University. All rights reserved.
1325 * Redistribution and use in source and binary forms, with or without
1326 * modification, are permitted provided that the following conditions
1329 * 1. Redistributions of source code must retain the above copyright
1330 * notice, this list of conditions and the following disclaimer.
1332 * 2. Redistributions in binary form must reproduce the above copyright
1333 * notice, this list of conditions and the following disclaimer in
1334 * the documentation and/or other materials provided with the
1337 * 3. The name "Carnegie Mellon University" must not be used to
1338 * endorse or promote products derived from this software without
1339 * prior written permission. For permission or any other legal
1340 * details, please contact
1341 * Office of Technology Transfer
1342 * Carnegie Mellon University
1343 * 5000 Forbes Avenue
1344 * Pittsburgh, PA 15213-3890
1345 * (412) 268-4387, fax: (412) 268-7395
1346 * tech-transfer@andrew.cmu.edu
1348 * 4. Redistributions of any form whatsoever must retain the following
1350 * "This product includes software developed by Computing Services
1351 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
1353 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
1354 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1355 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
1356 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1357 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
1358 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1359 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1364 * Copyright (c) 1987, 1989, 1993
1365 * The Regents of the University of California. All rights reserved.
1367 * This code is derived from software contributed to Berkeley by
1368 * Arthur David Olson of the National Cancer Institute.
1370 * Redistribution and use in source and binary forms, with or without
1371 * modification, are permitted provided that the following conditions
1373 * 1. Redistributions of source code must retain the above copyright
1374 * notice, this list of conditions and the following disclaimer.
1375 * 2. Redistributions in binary form must reproduce the above copyright
1376 * notice, this list of conditions and the following disclaimer in the
1377 * documentation and/or other materials provided with the distribution.
1378 * 3. All advertising materials mentioning features or use of this software
1379 * must display the following acknowledgement:
1380 * This product includes software developed by the University of
1381 * California, Berkeley and its contributors.
1382 * 4. Neither the name of the University nor the names of its contributors
1383 * may be used to endorse or promote products derived from this software
1384 * without specific prior written permission.
1386 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1387 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1388 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1389 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1390 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1391 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1392 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1393 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1394 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1395 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1400 ** Adapted from code provided by Robert Elz, who writes:
1401 ** The "best" way to do mktime I think is based on an idea of Bob
1402 ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
1403 ** It does a binary search of the time_t space. Since time_t's are
1404 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1405 ** would still be very reasonable).
1409 adapted for libEtPan! by DINH V. Hoa
1414 #endif /* !defined WRONG */
1416 static int tmcomp(struct tm * atmp, struct tm * btmp)
1418 register int result;
1420 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1421 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1422 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1423 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1424 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1425 result = atmp->tm_sec - btmp->tm_sec;
1429 static time_t mkgmtime(struct tm * tmp)
1433 register int saved_seconds;
1435 struct tm yourtm, *mytm, buft;
1438 saved_seconds = yourtm.tm_sec;
1441 ** Calculate the number of magnitude bits in a time_t
1442 ** (this works regardless of whether time_t is
1443 ** signed or unsigned, though lint complains if unsigned).
1445 for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
1448 ** If time_t is signed, then 0 is the median value,
1449 ** if time_t is unsigned, then 1 << bits is median.
1451 t = (t < 0) ? 0 : ((time_t) 1 << bits);
1453 mytm = gmtime_r(&t, &buft);
1454 dir = tmcomp(mytm, &yourtm);
1461 t -= (time_t) 1 << bits;
1462 else t += (time_t) 1 << bits;
1477 void mailimf_single_fields_init(struct mailimf_single_fields * single_fields,
1478 struct mailimf_fields * fields)
1482 memset(single_fields, 0, sizeof(struct mailimf_single_fields));
1484 cur = clist_begin(fields->fld_list);
1485 while (cur != NULL) {
1486 struct mailimf_field * field;
1488 field = clist_content(cur);
1490 switch (field->fld_type) {
1491 case MAILIMF_FIELD_ORIG_DATE:
1492 if (single_fields->fld_orig_date == NULL)
1493 single_fields->fld_orig_date = field->fld_data.fld_orig_date;
1494 cur = clist_next(cur);
1496 case MAILIMF_FIELD_FROM:
1497 if (single_fields->fld_from == NULL) {
1498 single_fields->fld_from = field->fld_data.fld_from;
1499 cur = clist_next(cur);
1502 clist_concat(single_fields->fld_from->frm_mb_list->mb_list,
1503 field->fld_data.fld_from->frm_mb_list->mb_list);
1504 mailimf_field_free(field);
1505 cur = clist_delete(fields->fld_list, cur);
1508 case MAILIMF_FIELD_SENDER:
1509 if (single_fields->fld_sender == NULL)
1510 single_fields->fld_sender = field->fld_data.fld_sender;
1511 cur = clist_next(cur);
1513 case MAILIMF_FIELD_REPLY_TO:
1514 if (single_fields->fld_reply_to == NULL) {
1515 single_fields->fld_reply_to = field->fld_data.fld_reply_to;
1516 cur = clist_next(cur);
1519 clist_concat(single_fields->fld_reply_to->rt_addr_list->ad_list,
1520 field->fld_data.fld_reply_to->rt_addr_list->ad_list);
1521 mailimf_field_free(field);
1522 cur = clist_delete(fields->fld_list, cur);
1525 case MAILIMF_FIELD_TO:
1526 if (single_fields->fld_to == NULL) {
1527 single_fields->fld_to = field->fld_data.fld_to;
1528 cur = clist_next(cur);
1531 clist_concat(single_fields->fld_to->to_addr_list->ad_list,
1532 field->fld_data.fld_to->to_addr_list->ad_list);
1533 mailimf_field_free(field);
1534 cur = clist_delete(fields->fld_list, cur);
1537 case MAILIMF_FIELD_CC:
1538 if (single_fields->fld_cc == NULL) {
1539 single_fields->fld_cc = field->fld_data.fld_cc;
1540 cur = clist_next(cur);
1543 clist_concat(single_fields->fld_cc->cc_addr_list->ad_list,
1544 field->fld_data.fld_cc->cc_addr_list->ad_list);
1545 mailimf_field_free(field);
1546 cur = clist_delete(fields->fld_list, cur);
1549 case MAILIMF_FIELD_BCC:
1550 if (single_fields->fld_bcc == NULL) {
1551 single_fields->fld_bcc = field->fld_data.fld_bcc;
1552 cur = clist_next(cur);
1555 clist_concat(single_fields->fld_bcc->bcc_addr_list->ad_list,
1556 field->fld_data.fld_bcc->bcc_addr_list->ad_list);
1557 mailimf_field_free(field);
1558 cur = clist_delete(fields->fld_list, cur);
1561 case MAILIMF_FIELD_MESSAGE_ID:
1562 if (single_fields->fld_message_id == NULL)
1563 single_fields->fld_message_id = field->fld_data.fld_message_id;
1564 cur = clist_next(cur);
1566 case MAILIMF_FIELD_IN_REPLY_TO:
1567 if (single_fields->fld_in_reply_to == NULL)
1568 single_fields->fld_in_reply_to = field->fld_data.fld_in_reply_to;
1569 cur = clist_next(cur);
1571 case MAILIMF_FIELD_REFERENCES:
1572 if (single_fields->fld_references == NULL)
1573 single_fields->fld_references = field->fld_data.fld_references;
1574 cur = clist_next(cur);
1576 case MAILIMF_FIELD_SUBJECT:
1577 if (single_fields->fld_subject == NULL)
1578 single_fields->fld_subject = field->fld_data.fld_subject;
1579 cur = clist_next(cur);
1581 case MAILIMF_FIELD_COMMENTS:
1582 if (single_fields->fld_comments == NULL)
1583 single_fields->fld_comments = field->fld_data.fld_comments;
1584 cur = clist_next(cur);
1586 case MAILIMF_FIELD_KEYWORDS:
1587 if (single_fields->fld_keywords == NULL)
1588 single_fields->fld_keywords = field->fld_data.fld_keywords;
1589 cur = clist_next(cur);
1592 cur = clist_next(cur);
1599 struct mailimf_single_fields *
1600 mailimf_single_fields_new(struct mailimf_fields * fields)
1602 struct mailimf_single_fields * single_fields;
1604 single_fields = malloc(sizeof(struct mailimf_single_fields));
1605 if (single_fields == NULL)
1608 mailimf_single_fields_init(single_fields, fields);
1610 return single_fields;
1616 void mailimf_single_fields_free(struct mailimf_single_fields *
1619 free(single_fields);
1622 struct mailimf_field * mailimf_field_new_custom(char * name, char * value)
1624 struct mailimf_optional_field * opt_field;
1625 struct mailimf_field * field;
1627 opt_field = mailimf_optional_field_new(name, value);
1628 if (opt_field == NULL)
1631 field = mailimf_field_new(MAILIMF_FIELD_OPTIONAL_FIELD,
1632 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1633 NULL, NULL, NULL, NULL,
1634 NULL, NULL, NULL, NULL,
1635 NULL, NULL, NULL, NULL,
1636 NULL, NULL, opt_field);
1638 goto free_opt_field;
1643 mailimf_optional_field_free(opt_field);