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
38 #include "mailimf_types.h"
39 #include "mmapstring.h"
42 void mailimf_atom_free(char * atom
)
47 void mailimf_dot_atom_free(char * dot_atom
)
52 void mailimf_dot_atom_text_free(char * dot_atom
)
57 void mailimf_quoted_string_free(char * quoted_string
)
62 void mailimf_word_free(char * word
)
67 void mailimf_phrase_free(char * phrase
)
72 void mailimf_unstructured_free(char * unstructured
)
78 struct mailimf_date_time
*
79 mailimf_date_time_new(int dt_day
, int dt_month
, int dt_year
,
80 int dt_hour
, int dt_min
, int dt_sec
, int dt_zone
)
82 struct mailimf_date_time
* date_time
;
84 date_time
= malloc(sizeof(* date_time
));
85 if (date_time
== NULL
)
88 date_time
->dt_day
= dt_day
;
89 date_time
->dt_month
= dt_month
;
90 date_time
->dt_year
= dt_year
;
91 date_time
->dt_hour
= dt_hour
;
92 date_time
->dt_min
= dt_min
;
93 date_time
->dt_sec
= dt_sec
;
94 date_time
->dt_zone
= dt_zone
;
100 void mailimf_date_time_free(struct mailimf_date_time
* date_time
)
108 struct mailimf_address
*
109 mailimf_address_new(int ad_type
, struct mailimf_mailbox
* ad_mailbox
,
110 struct mailimf_group
* ad_group
)
112 struct mailimf_address
* address
;
114 address
= malloc(sizeof(* address
));
118 address
->ad_type
= ad_type
;
120 case MAILIMF_ADDRESS_MAILBOX
:
121 address
->ad_data
.ad_mailbox
= ad_mailbox
;
123 case MAILIMF_ADDRESS_GROUP
:
124 address
->ad_data
.ad_group
= ad_group
;
131 void mailimf_address_free(struct mailimf_address
* address
)
133 switch (address
->ad_type
) {
134 case MAILIMF_ADDRESS_MAILBOX
:
135 mailimf_mailbox_free(address
->ad_data
.ad_mailbox
);
137 case MAILIMF_ADDRESS_GROUP
:
138 mailimf_group_free(address
->ad_data
.ad_group
);
143 struct mailimf_mailbox
*
144 mailimf_mailbox_new(char * mb_display_name
, char * mb_addr_spec
)
146 struct mailimf_mailbox
* mb
;
148 mb
= malloc(sizeof(* mb
));
152 mb
->mb_display_name
= mb_display_name
;
153 mb
->mb_addr_spec
= mb_addr_spec
;
158 void mailimf_mailbox_free(struct mailimf_mailbox
* mailbox
)
160 if (mailbox
->mb_display_name
!= NULL
)
161 mailimf_display_name_free(mailbox
->mb_display_name
);
162 mailimf_addr_spec_free(mailbox
->mb_addr_spec
);
167 void mailimf_angle_addr_free(char * angle_addr
)
173 struct mailimf_group
*
174 mailimf_group_new(char * grp_display_name
,
175 struct mailimf_mailbox_list
* grp_mb_list
)
177 struct mailimf_group
* group
;
179 group
= malloc(sizeof(* group
));
183 group
->grp_display_name
= grp_display_name
;
184 group
->grp_mb_list
= grp_mb_list
;
189 void mailimf_group_free(struct mailimf_group
* group
)
191 if (group
->grp_mb_list
)
192 mailimf_mailbox_list_free(group
->grp_mb_list
);
193 mailimf_display_name_free(group
->grp_display_name
);
197 void mailimf_display_name_free(char * display_name
)
199 mailimf_phrase_free(display_name
);
203 struct mailimf_mailbox_list
*
204 mailimf_mailbox_list_new(clist
* mb_list
)
206 struct mailimf_mailbox_list
* mbl
;
208 mbl
= malloc(sizeof(* mbl
));
212 mbl
->mb_list
= mb_list
;
217 void mailimf_mailbox_list_free(struct mailimf_mailbox_list
* mb_list
)
219 clist_foreach(mb_list
->mb_list
, (clist_func
) mailimf_mailbox_free
, NULL
);
220 clist_free(mb_list
->mb_list
);
226 struct mailimf_address_list
*
227 mailimf_address_list_new(clist
* ad_list
)
229 struct mailimf_address_list
* addr_list
;
231 addr_list
= malloc(sizeof(* addr_list
));
232 if (addr_list
== NULL
)
235 addr_list
->ad_list
= ad_list
;
240 void mailimf_address_list_free(struct mailimf_address_list
* addr_list
)
242 clist_foreach(addr_list
->ad_list
, (clist_func
) mailimf_address_free
, NULL
);
243 clist_free(addr_list
->ad_list
);
248 void mailimf_addr_spec_free(char * addr_spec
)
253 void mailimf_local_part_free(char * local_part
)
258 void mailimf_domain_free(char * domain
)
263 void mailimf_domain_literal_free(char * domain_literal
)
265 free(domain_literal
);
270 struct mailimf_message
*
271 mailimf_message_new(struct mailimf_fields
* msg_fields
,
272 struct mailimf_body
* msg_body
)
274 struct mailimf_message
* message
;
276 message
= malloc(sizeof(* message
));
280 message
->msg_fields
= msg_fields
;
281 message
->msg_body
= msg_body
;
286 void mailimf_message_free(struct mailimf_message
* message
)
288 mailimf_body_free(message
->msg_body
);
289 mailimf_fields_free(message
->msg_fields
);
294 struct mailimf_body
* mailimf_body_new(const char * bd_text
, size_t bd_size
)
296 struct mailimf_body
* body
;
298 body
= malloc(sizeof(* body
));
301 body
->bd_text
= bd_text
;
302 body
->bd_size
= bd_size
;
307 void mailimf_body_free(struct mailimf_body
* body
)
314 struct mailimf_field
*
315 mailimf_field_new(int fld_type
,
316 struct mailimf_return
* fld_return_path
,
317 struct mailimf_orig_date
* fld_resent_date
,
318 struct mailimf_from
* fld_resent_from
,
319 struct mailimf_sender
* fld_resent_sender
,
320 struct mailimf_to
* fld_resent_to
,
321 struct mailimf_cc
* fld_resent_cc
,
322 struct mailimf_bcc
* fld_resent_bcc
,
323 struct mailimf_message_id
* fld_resent_msg_id
,
324 struct mailimf_orig_date
* fld_orig_date
,
325 struct mailimf_from
* fld_from
,
326 struct mailimf_sender
* fld_sender
,
327 struct mailimf_reply_to
* fld_reply_to
,
328 struct mailimf_to
* fld_to
,
329 struct mailimf_cc
* fld_cc
,
330 struct mailimf_bcc
* fld_bcc
,
331 struct mailimf_message_id
* fld_message_id
,
332 struct mailimf_in_reply_to
* fld_in_reply_to
,
333 struct mailimf_references
* fld_references
,
334 struct mailimf_subject
* fld_subject
,
335 struct mailimf_comments
* fld_comments
,
336 struct mailimf_keywords
* fld_keywords
,
337 struct mailimf_optional_field
* fld_optional_field
)
339 struct mailimf_field
* field
;
341 field
= malloc(sizeof(* field
));
345 field
->fld_type
= fld_type
;
347 case MAILIMF_FIELD_RETURN_PATH
:
348 field
->fld_data
.fld_return_path
= fld_return_path
;
350 case MAILIMF_FIELD_RESENT_DATE
:
351 field
->fld_data
.fld_resent_date
= fld_resent_date
;
353 case MAILIMF_FIELD_RESENT_FROM
:
354 field
->fld_data
.fld_resent_from
= fld_resent_from
;
356 case MAILIMF_FIELD_RESENT_SENDER
:
357 field
->fld_data
.fld_resent_sender
= fld_resent_sender
;
359 case MAILIMF_FIELD_RESENT_TO
:
360 field
->fld_data
.fld_resent_to
= fld_resent_to
;
362 case MAILIMF_FIELD_RESENT_CC
:
363 field
->fld_data
.fld_resent_cc
= fld_resent_cc
;
365 case MAILIMF_FIELD_RESENT_BCC
:
366 field
->fld_data
.fld_resent_bcc
= fld_resent_bcc
;
368 case MAILIMF_FIELD_RESENT_MSG_ID
:
369 field
->fld_data
.fld_resent_msg_id
= fld_resent_msg_id
;
371 case MAILIMF_FIELD_ORIG_DATE
:
372 field
->fld_data
.fld_orig_date
= fld_orig_date
;
374 case MAILIMF_FIELD_FROM
:
375 field
->fld_data
.fld_from
= fld_from
;
377 case MAILIMF_FIELD_SENDER
:
378 field
->fld_data
.fld_sender
= fld_sender
;
380 case MAILIMF_FIELD_REPLY_TO
:
381 field
->fld_data
.fld_reply_to
= fld_reply_to
;
383 case MAILIMF_FIELD_TO
:
384 field
->fld_data
.fld_to
= fld_to
;
386 case MAILIMF_FIELD_CC
:
387 field
->fld_data
.fld_cc
= fld_cc
;
389 case MAILIMF_FIELD_BCC
:
390 field
->fld_data
.fld_bcc
= fld_bcc
;
392 case MAILIMF_FIELD_MESSAGE_ID
:
393 field
->fld_data
.fld_message_id
= fld_message_id
;
395 case MAILIMF_FIELD_IN_REPLY_TO
:
396 field
->fld_data
.fld_in_reply_to
= fld_in_reply_to
;
398 case MAILIMF_FIELD_REFERENCES
:
399 field
->fld_data
.fld_references
= fld_references
;
401 case MAILIMF_FIELD_SUBJECT
:
402 field
->fld_data
.fld_subject
= fld_subject
;
404 case MAILIMF_FIELD_COMMENTS
:
405 field
->fld_data
.fld_comments
= fld_comments
;
407 case MAILIMF_FIELD_KEYWORDS
:
408 field
->fld_data
.fld_keywords
= fld_keywords
;
410 case MAILIMF_FIELD_OPTIONAL_FIELD
:
411 field
->fld_data
.fld_optional_field
= fld_optional_field
;
418 void mailimf_field_free(struct mailimf_field
* field
)
420 switch (field
->fld_type
) {
421 case MAILIMF_FIELD_RETURN_PATH
:
422 mailimf_return_free(field
->fld_data
.fld_return_path
);
424 case MAILIMF_FIELD_RESENT_DATE
:
425 mailimf_orig_date_free(field
->fld_data
.fld_resent_date
);
427 case MAILIMF_FIELD_RESENT_FROM
:
428 mailimf_from_free(field
->fld_data
.fld_resent_from
);
430 case MAILIMF_FIELD_RESENT_SENDER
:
431 mailimf_sender_free(field
->fld_data
.fld_resent_sender
);
433 case MAILIMF_FIELD_RESENT_TO
:
434 mailimf_to_free(field
->fld_data
.fld_resent_to
);
436 case MAILIMF_FIELD_RESENT_CC
:
437 mailimf_cc_free(field
->fld_data
.fld_resent_cc
);
439 case MAILIMF_FIELD_RESENT_BCC
:
440 mailimf_bcc_free(field
->fld_data
.fld_resent_bcc
);
442 case MAILIMF_FIELD_RESENT_MSG_ID
:
443 mailimf_message_id_free(field
->fld_data
.fld_resent_msg_id
);
445 case MAILIMF_FIELD_ORIG_DATE
:
446 mailimf_orig_date_free(field
->fld_data
.fld_orig_date
);
448 case MAILIMF_FIELD_FROM
:
449 mailimf_from_free(field
->fld_data
.fld_from
);
451 case MAILIMF_FIELD_SENDER
:
452 mailimf_sender_free(field
->fld_data
.fld_sender
);
454 case MAILIMF_FIELD_REPLY_TO
:
455 mailimf_reply_to_free(field
->fld_data
.fld_reply_to
);
457 case MAILIMF_FIELD_TO
:
458 mailimf_to_free(field
->fld_data
.fld_to
);
460 case MAILIMF_FIELD_CC
:
461 mailimf_cc_free(field
->fld_data
.fld_cc
);
463 case MAILIMF_FIELD_BCC
:
464 mailimf_bcc_free(field
->fld_data
.fld_bcc
);
466 case MAILIMF_FIELD_MESSAGE_ID
:
467 mailimf_message_id_free(field
->fld_data
.fld_message_id
);
469 case MAILIMF_FIELD_IN_REPLY_TO
:
470 mailimf_in_reply_to_free(field
->fld_data
.fld_in_reply_to
);
472 case MAILIMF_FIELD_REFERENCES
:
473 mailimf_references_free(field
->fld_data
.fld_references
);
475 case MAILIMF_FIELD_SUBJECT
:
476 mailimf_subject_free(field
->fld_data
.fld_subject
);
478 case MAILIMF_FIELD_COMMENTS
:
479 mailimf_comments_free(field
->fld_data
.fld_comments
);
481 case MAILIMF_FIELD_KEYWORDS
:
482 mailimf_keywords_free(field
->fld_data
.fld_keywords
);
484 case MAILIMF_FIELD_OPTIONAL_FIELD
:
485 mailimf_optional_field_free(field
->fld_data
.fld_optional_field
);
492 struct mailimf_fields
* mailimf_fields_new(clist
* fld_list
)
494 struct mailimf_fields
* fields
;
496 fields
= malloc(sizeof(* fields
));
500 fields
->fld_list
= fld_list
;
505 void mailimf_fields_free(struct mailimf_fields
* fields
)
507 if (fields
->fld_list
!= NULL
) {
508 clist_foreach(fields
->fld_list
, (clist_func
) mailimf_field_free
, NULL
);
509 clist_free(fields
->fld_list
);
515 struct mailimf_orig_date
* mailimf_orig_date_new(struct mailimf_date_time
*
518 struct mailimf_orig_date
* orig_date
;
520 orig_date
= malloc(sizeof(* orig_date
));
521 if (orig_date
== NULL
)
524 orig_date
->dt_date_time
= dt_date_time
;
529 void mailimf_orig_date_free(struct mailimf_orig_date
* orig_date
)
531 if (orig_date
->dt_date_time
!= NULL
)
532 mailimf_date_time_free(orig_date
->dt_date_time
);
536 struct mailimf_from
*
537 mailimf_from_new(struct mailimf_mailbox_list
* frm_mb_list
)
539 struct mailimf_from
* from
;
541 from
= malloc(sizeof(* from
));
545 from
->frm_mb_list
= frm_mb_list
;
550 void mailimf_from_free(struct mailimf_from
* from
)
552 if (from
->frm_mb_list
!= NULL
)
553 mailimf_mailbox_list_free(from
->frm_mb_list
);
557 struct mailimf_sender
* mailimf_sender_new(struct mailimf_mailbox
* snd_mb
)
559 struct mailimf_sender
* sender
;
561 sender
= malloc(sizeof(* sender
));
565 sender
->snd_mb
= snd_mb
;
570 void mailimf_sender_free(struct mailimf_sender
* sender
)
572 if (sender
->snd_mb
!= NULL
)
573 mailimf_mailbox_free(sender
->snd_mb
);
577 struct mailimf_reply_to
*
578 mailimf_reply_to_new(struct mailimf_address_list
* rt_addr_list
)
580 struct mailimf_reply_to
* reply_to
;
582 reply_to
= malloc(sizeof(* reply_to
));
583 if (reply_to
== NULL
)
586 reply_to
->rt_addr_list
= rt_addr_list
;
591 void mailimf_reply_to_free(struct mailimf_reply_to
* reply_to
)
593 if (reply_to
->rt_addr_list
!= NULL
)
594 mailimf_address_list_free(reply_to
->rt_addr_list
);
598 struct mailimf_to
* mailimf_to_new(struct mailimf_address_list
* to_addr_list
)
600 struct mailimf_to
* to
;
602 to
= malloc(sizeof(* to
));
606 to
->to_addr_list
= to_addr_list
;
611 void mailimf_to_free(struct mailimf_to
* to
)
613 if (to
->to_addr_list
!= NULL
)
614 mailimf_address_list_free(to
->to_addr_list
);
618 struct mailimf_cc
* mailimf_cc_new(struct mailimf_address_list
* cc_addr_list
)
620 struct mailimf_cc
* cc
;
622 cc
= malloc(sizeof(* cc
));
626 cc
->cc_addr_list
= cc_addr_list
;
631 void mailimf_cc_free(struct mailimf_cc
* cc
)
633 if (cc
->cc_addr_list
!= NULL
)
634 mailimf_address_list_free(cc
->cc_addr_list
);
639 mailimf_bcc_new(struct mailimf_address_list
* bcc_addr_list
)
641 struct mailimf_bcc
* bcc
;
643 bcc
= malloc(sizeof(* bcc
));
647 bcc
->bcc_addr_list
= bcc_addr_list
;
652 void mailimf_bcc_free(struct mailimf_bcc
* bcc
)
654 if (bcc
->bcc_addr_list
!= NULL
)
655 mailimf_address_list_free(bcc
->bcc_addr_list
);
659 struct mailimf_message_id
* mailimf_message_id_new(char * mid_value
)
661 struct mailimf_message_id
* message_id
;
663 message_id
= malloc(sizeof(* message_id
));
664 if (message_id
== NULL
)
667 message_id
->mid_value
= mid_value
;
672 void mailimf_message_id_free(struct mailimf_message_id
* message_id
)
674 if (message_id
->mid_value
!= NULL
)
675 mailimf_msg_id_free(message_id
->mid_value
);
679 struct mailimf_in_reply_to
* mailimf_in_reply_to_new(clist
* mid_list
)
681 struct mailimf_in_reply_to
* in_reply_to
;
683 in_reply_to
= malloc(sizeof(* in_reply_to
));
684 if (in_reply_to
== NULL
)
687 in_reply_to
->mid_list
= mid_list
;
692 void mailimf_in_reply_to_free(struct mailimf_in_reply_to
* in_reply_to
)
694 clist_foreach(in_reply_to
->mid_list
,
695 (clist_func
) mailimf_msg_id_free
, NULL
);
696 clist_free(in_reply_to
->mid_list
);
700 struct mailimf_references
* mailimf_references_new(clist
* mid_list
)
702 struct mailimf_references
* ref
;
704 ref
= malloc(sizeof(* ref
));
708 ref
->mid_list
= mid_list
;
713 void mailimf_references_free(struct mailimf_references
* references
)
715 clist_foreach(references
->mid_list
,
716 (clist_func
) mailimf_msg_id_free
, NULL
);
717 clist_free(references
->mid_list
);
721 void mailimf_msg_id_free(char * msg_id
)
726 void mailimf_id_left_free(char * id_left
)
731 void mailimf_id_right_free(char * id_right
)
736 void mailimf_no_fold_quote_free(char * nfq
)
741 void mailimf_no_fold_literal_free(char * nfl
)
746 struct mailimf_subject
* mailimf_subject_new(char * sbj_value
)
748 struct mailimf_subject
* subject
;
750 subject
= malloc(sizeof(* subject
));
754 subject
->sbj_value
= sbj_value
;
759 void mailimf_subject_free(struct mailimf_subject
* subject
)
761 mailimf_unstructured_free(subject
->sbj_value
);
765 struct mailimf_comments
* mailimf_comments_new(char * cm_value
)
767 struct mailimf_comments
* comments
;
769 comments
= malloc(sizeof(* comments
));
770 if (comments
== NULL
)
773 comments
->cm_value
= cm_value
;
778 void mailimf_comments_free(struct mailimf_comments
* comments
)
780 mailimf_unstructured_free(comments
->cm_value
);
784 struct mailimf_keywords
* mailimf_keywords_new(clist
* kw_list
)
786 struct mailimf_keywords
* keywords
;
788 keywords
= malloc(sizeof(* keywords
));
789 if (keywords
== NULL
)
792 keywords
->kw_list
= kw_list
;
797 void mailimf_keywords_free(struct mailimf_keywords
* keywords
)
799 clist_foreach(keywords
->kw_list
, (clist_func
) mailimf_phrase_free
, NULL
);
800 clist_free(keywords
->kw_list
);
804 struct mailimf_return
*
805 mailimf_return_new(struct mailimf_path
* ret_path
)
807 struct mailimf_return
* return_path
;
809 return_path
= malloc(sizeof(* return_path
));
810 if (return_path
== NULL
)
813 return_path
->ret_path
= ret_path
;
818 void mailimf_return_free(struct mailimf_return
* return_path
)
820 mailimf_path_free(return_path
->ret_path
);
825 struct mailimf_path
* mailimf_path_new(char * pt_addr_spec
)
827 struct mailimf_path
* path
;
829 path
= malloc(sizeof(* path
));
833 path
->pt_addr_spec
= pt_addr_spec
;
838 void mailimf_path_free(struct mailimf_path
* path
)
840 if (path
->pt_addr_spec
!= NULL
)
841 mailimf_addr_spec_free(path
->pt_addr_spec
);
845 struct mailimf_optional_field
*
846 mailimf_optional_field_new(char * fld_name
, char * fld_value
)
848 struct mailimf_optional_field
* opt_field
;
850 opt_field
= malloc(sizeof(* opt_field
));
851 if (opt_field
== NULL
)
854 opt_field
->fld_name
= fld_name
;
855 opt_field
->fld_value
= fld_value
;
860 void mailimf_optional_field_free(struct mailimf_optional_field
* opt_field
)
862 mailimf_field_name_free(opt_field
->fld_name
);
863 mailimf_unstructured_free(opt_field
->fld_value
);
867 void mailimf_field_name_free(char * field_name
)