summaryrefslogtreecommitdiffabout
path: root/libetpan/src/low-level/mime/mailmime_types.c
Unidiff
Diffstat (limited to 'libetpan/src/low-level/mime/mailmime_types.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/low-level/mime/mailmime_types.c753
1 files changed, 753 insertions, 0 deletions
diff --git a/libetpan/src/low-level/mime/mailmime_types.c b/libetpan/src/low-level/mime/mailmime_types.c
new file mode 100644
index 0000000..115d17f
--- a/dev/null
+++ b/libetpan/src/low-level/mime/mailmime_types.c
@@ -0,0 +1,753 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * All rights reserved.
6 *
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.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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.
30 */
31
32/*
33 * $Id$
34 */
35
36#include "mailmime_types.h"
37#include "mmapstring.h"
38
39#include <string.h>
40#include <stdlib.h>
41
42void mailmime_attribute_free(char * attribute)
43{
44 mailmime_token_free(attribute);
45}
46
47
48
49struct mailmime_composite_type *
50mailmime_composite_type_new(int ct_type, char * ct_token)
51{
52 struct mailmime_composite_type * ct;
53
54 ct = malloc(sizeof(* ct));
55 if (ct == NULL)
56 return NULL;
57
58 ct->ct_type = ct_type;
59 ct->ct_token = ct_token;
60
61 return ct;
62}
63
64void mailmime_composite_type_free(struct mailmime_composite_type * ct)
65{
66 if (ct->ct_token != NULL)
67 mailmime_extension_token_free(ct->ct_token);
68 free(ct);
69}
70
71
72struct mailmime_content *
73mailmime_content_new(struct mailmime_type * ct_type,
74 char * ct_subtype,
75 clist * ct_parameters)
76{
77 struct mailmime_content * content;
78
79 content = malloc(sizeof(* content));
80 if (content == NULL)
81 return NULL;
82
83 content->ct_type = ct_type;
84 content->ct_subtype = ct_subtype;
85 content->ct_parameters = ct_parameters;
86
87 return content;
88}
89
90void mailmime_content_free(struct mailmime_content * content)
91{
92 mailmime_type_free(content->ct_type);
93 mailmime_subtype_free(content->ct_subtype);
94 if (content->ct_parameters != NULL) {
95 clist_foreach(content->ct_parameters,
96 (clist_func) mailmime_parameter_free, NULL);
97 clist_free(content->ct_parameters);
98 }
99
100 free(content);
101}
102
103
104void mailmime_description_free(char * description)
105{
106 free(description);
107}
108
109struct mailmime_discrete_type *
110mailmime_discrete_type_new(int dt_type, char * dt_extension)
111{
112 struct mailmime_discrete_type * discrete_type;
113
114 discrete_type = malloc(sizeof(* discrete_type));
115 if (discrete_type == NULL)
116 return NULL;
117
118 discrete_type->dt_type = dt_type;
119 discrete_type->dt_extension = dt_extension;
120
121 return discrete_type;
122}
123
124void mailmime_discrete_type_free(struct mailmime_discrete_type * discrete_type)
125{
126 if (discrete_type->dt_extension != NULL)
127 mailmime_extension_token_free(discrete_type->dt_extension);
128 free(discrete_type);
129}
130
131void mailmime_encoding_free(struct mailmime_mechanism * encoding)
132{
133 mailmime_mechanism_free(encoding);
134}
135
136void mailmime_extension_token_free(char * extension)
137{
138 mailmime_token_free(extension);
139}
140
141void mailmime_id_free(char * id)
142{
143 mailimf_msg_id_free(id);
144}
145
146struct mailmime_mechanism * mailmime_mechanism_new(int enc_type, char * enc_token)
147{
148 struct mailmime_mechanism * mechanism;
149
150 mechanism = malloc(sizeof(* mechanism));
151 if (mechanism == NULL)
152 return NULL;
153
154 mechanism->enc_type = enc_type;
155 mechanism->enc_token = enc_token;
156
157 return mechanism;
158}
159
160void mailmime_mechanism_free(struct mailmime_mechanism * mechanism)
161{
162 if (mechanism->enc_token != NULL)
163 mailmime_token_free(mechanism->enc_token);
164 free(mechanism);
165}
166
167struct mailmime_parameter *
168mailmime_parameter_new(char * pa_name, char * pa_value)
169{
170 struct mailmime_parameter * parameter;
171
172 parameter = malloc(sizeof(* parameter));
173 if (parameter == NULL)
174 return NULL;
175
176 parameter->pa_name = pa_name;
177 parameter->pa_value = pa_value;
178
179 return parameter;
180}
181
182void mailmime_parameter_free(struct mailmime_parameter * parameter)
183{
184 mailmime_attribute_free(parameter->pa_name);
185 mailmime_value_free(parameter->pa_value);
186 free(parameter);
187}
188
189
190void mailmime_subtype_free(char * subtype)
191{
192 mailmime_extension_token_free(subtype);
193}
194
195
196void mailmime_token_free(char * token)
197{
198 free(token);
199}
200
201
202struct mailmime_type *
203mailmime_type_new(int tp_type,
204 struct mailmime_discrete_type * tp_discrete_type,
205 struct mailmime_composite_type * tp_composite_type)
206{
207 struct mailmime_type * mime_type;
208
209 mime_type = malloc(sizeof(* mime_type));
210 if (mime_type == NULL)
211 return NULL;
212
213 mime_type->tp_type = tp_type;
214 switch (tp_type) {
215 case MAILMIME_TYPE_DISCRETE_TYPE:
216 mime_type->tp_data.tp_discrete_type = tp_discrete_type;
217 break;
218 case MAILMIME_TYPE_COMPOSITE_TYPE:
219 mime_type->tp_data.tp_composite_type = tp_composite_type;
220 break;
221 }
222
223 return mime_type;
224}
225
226void mailmime_type_free(struct mailmime_type * type)
227{
228 switch (type->tp_type) {
229 case MAILMIME_TYPE_DISCRETE_TYPE:
230 mailmime_discrete_type_free(type->tp_data.tp_discrete_type);
231 break;
232 case MAILMIME_TYPE_COMPOSITE_TYPE:
233 mailmime_composite_type_free(type->tp_data.tp_composite_type);
234 break;
235 }
236 free(type);
237}
238
239void mailmime_value_free(char * value)
240{
241 free(value);
242}
243
244
245/*
246void mailmime_x_token_free(gchar * x_token)
247{
248 g_free(x_token);
249}
250*/
251
252struct mailmime_field *
253mailmime_field_new(int fld_type,
254 struct mailmime_content * fld_content,
255 struct mailmime_mechanism * fld_encoding,
256 char * fld_id,
257 char * fld_description,
258 uint32_t fld_version,
259 struct mailmime_disposition * fld_disposition,
260 struct mailmime_language * fld_language)
261{
262 struct mailmime_field * field;
263
264 field = malloc(sizeof(* field));
265 if (field == NULL)
266 return NULL;
267 field->fld_type = fld_type;
268
269 switch (fld_type) {
270 case MAILMIME_FIELD_TYPE:
271 field->fld_data.fld_content = fld_content;
272 break;
273 case MAILMIME_FIELD_TRANSFER_ENCODING:
274 field->fld_data.fld_encoding = fld_encoding;
275 break;
276 case MAILMIME_FIELD_ID:
277 field->fld_data.fld_id = fld_id;
278 break;
279 case MAILMIME_FIELD_DESCRIPTION:
280 field->fld_data.fld_description = fld_description;
281 break;
282 case MAILMIME_FIELD_VERSION:
283 field->fld_data.fld_version = fld_version;
284 break;
285 case MAILMIME_FIELD_DISPOSITION:
286 field->fld_data.fld_disposition = fld_disposition;
287 break;
288 case MAILMIME_FIELD_LANGUAGE:
289 field->fld_data.fld_language = fld_language;
290 break;
291 }
292 return field;
293}
294
295void mailmime_field_free(struct mailmime_field * field)
296{
297 switch (field->fld_type) {
298 case MAILMIME_FIELD_TYPE:
299 if (field->fld_data.fld_content != NULL)
300 mailmime_content_free(field->fld_data.fld_content);
301 break;
302 case MAILMIME_FIELD_TRANSFER_ENCODING:
303 if (field->fld_data.fld_encoding != NULL)
304 mailmime_encoding_free(field->fld_data.fld_encoding);
305 break;
306 case MAILMIME_FIELD_ID:
307 if (field->fld_data.fld_id != NULL)
308 mailmime_id_free(field->fld_data.fld_id);
309 break;
310 case MAILMIME_FIELD_DESCRIPTION:
311 if (field->fld_data.fld_description != NULL)
312 mailmime_description_free(field->fld_data.fld_description);
313 break;
314 case MAILMIME_FIELD_DISPOSITION:
315 if (field->fld_data.fld_disposition != NULL)
316 mailmime_disposition_free(field->fld_data.fld_disposition);
317 break;
318 case MAILMIME_FIELD_LANGUAGE:
319 if (field->fld_data.fld_language != NULL)
320 mailmime_language_free(field->fld_data.fld_language);
321 break;
322 }
323
324 free(field);
325}
326
327struct mailmime_fields * mailmime_fields_new(clist * fld_list)
328{
329 struct mailmime_fields * fields;
330
331 fields = malloc(sizeof(* fields));
332 if (fields == NULL)
333 return NULL;
334
335 fields->fld_list = fld_list;
336
337 return fields;
338}
339
340void mailmime_fields_free(struct mailmime_fields * fields)
341{
342 clist_foreach(fields->fld_list, (clist_func) mailmime_field_free, NULL);
343 clist_free(fields->fld_list);
344 free(fields);
345}
346
347
348/*
349struct mailmime_body_part *
350mailmime_body_part_new(gchar * text, guint32 size)
351{
352 struct mailmime_body_part * body_part;
353
354 body_part = g_new(struct mailmime_body_part, 1);
355 if (body_part == NULL)
356 return NULL;
357
358 body_part->text = text;
359 body_part->size = size;
360
361 return body_part;
362}
363
364void mailmime_body_part_free(struct mailmime_body_part * body_part)
365{
366 g_free(body_part);
367}
368*/
369
370struct mailmime_multipart_body *
371mailmime_multipart_body_new(clist * bd_list)
372{
373 struct mailmime_multipart_body * mp_body;
374
375 mp_body = malloc(sizeof(* mp_body));
376 if (mp_body == NULL)
377 return NULL;
378
379 mp_body->bd_list = bd_list;
380
381 return mp_body;
382}
383
384void mailmime_multipart_body_free(struct mailmime_multipart_body * mp_body)
385{
386 clist_foreach(mp_body->bd_list, (clist_func) mailimf_body_free, NULL);
387 clist_free(mp_body->bd_list);
388 free(mp_body);
389}
390
391
392
393
394struct mailmime * mailmime_new(int mm_type,
395 const char * mm_mime_start, size_t mm_length,
396 struct mailmime_fields * mm_mime_fields,
397 struct mailmime_content * mm_content_type,
398 struct mailmime_data * mm_body,
399 struct mailmime_data * mm_preamble,
400 struct mailmime_data * mm_epilogue,
401 clist * mm_mp_list,
402 struct mailimf_fields * mm_fields,
403 struct mailmime * mm_msg_mime)
404{
405 struct mailmime * mime;
406 clistiter * cur;
407
408 mime = malloc(sizeof(* mime));
409 if (mime == NULL)
410 return NULL;
411
412 mime->mm_parent = NULL;
413 mime->mm_parent_type = MAILMIME_NONE;
414 mime->mm_multipart_pos = NULL;
415
416 mime->mm_type = mm_type;
417 mime->mm_mime_start = mm_mime_start;
418 mime->mm_length = mm_length;
419 mime->mm_mime_fields = mm_mime_fields;
420 mime->mm_content_type = mm_content_type;
421
422 mime->mm_body = mm_body;
423
424 switch (mm_type) {
425 case MAILMIME_SINGLE:
426 mime->mm_data.mm_single = mm_body;
427 break;
428
429 case MAILMIME_MULTIPLE:
430 mime->mm_data.mm_multipart.mm_preamble = mm_preamble;
431 mime->mm_data.mm_multipart.mm_epilogue = mm_epilogue;
432 mime->mm_data.mm_multipart.mm_mp_list = mm_mp_list;
433
434 for(cur = clist_begin(mm_mp_list) ; cur != NULL ;
435 cur = clist_next(cur)) {
436 struct mailmime * submime;
437
438 submime = clist_content(cur);
439 submime->mm_parent = mime;
440 submime->mm_parent_type = MAILMIME_MULTIPLE;
441 submime->mm_multipart_pos = cur;
442 }
443 break;
444
445 case MAILMIME_MESSAGE:
446 mime->mm_data.mm_message.mm_fields = mm_fields;
447 mime->mm_data.mm_message.mm_msg_mime = mm_msg_mime;
448 if (mm_msg_mime != NULL) {
449 mm_msg_mime->mm_parent = mime;
450 mm_msg_mime->mm_parent_type = MAILMIME_MESSAGE;
451 }
452 break;
453
454 }
455
456 return mime;
457}
458
459void mailmime_free(struct mailmime * mime)
460{
461 switch (mime->mm_type) {
462 case MAILMIME_SINGLE:
463 if ((mime->mm_body == NULL) && (mime->mm_data.mm_single != NULL))
464 mailmime_data_free(mime->mm_data.mm_single);
465 /* do nothing */
466 break;
467
468 case MAILMIME_MULTIPLE:
469 if (mime->mm_data.mm_multipart.mm_preamble != NULL)
470 mailmime_data_free(mime->mm_data.mm_multipart.mm_preamble);
471 if (mime->mm_data.mm_multipart.mm_epilogue != NULL)
472 mailmime_data_free(mime->mm_data.mm_multipart.mm_epilogue);
473 clist_foreach(mime->mm_data.mm_multipart.mm_mp_list,
474 (clist_func) mailmime_free, NULL);
475 clist_free(mime->mm_data.mm_multipart.mm_mp_list);
476 break;
477
478 case MAILMIME_MESSAGE:
479 if (mime->mm_data.mm_message.mm_fields != NULL)
480 mailimf_fields_free(mime->mm_data.mm_message.mm_fields);
481 if (mime->mm_data.mm_message.mm_msg_mime != NULL)
482 mailmime_free(mime->mm_data.mm_message.mm_msg_mime);
483 break;
484
485 }
486 if (mime->mm_body != NULL)
487 mailmime_data_free(mime->mm_body);
488
489 if (mime->mm_mime_fields != NULL)
490 mailmime_fields_free(mime->mm_mime_fields);
491 if (mime->mm_content_type != NULL)
492 mailmime_content_free(mime->mm_content_type);
493 free(mime);
494}
495
496
497
498struct mailmime_encoded_word *
499mailmime_encoded_word_new(char * wd_charset, char * wd_text)
500{
501 struct mailmime_encoded_word * ew;
502
503 ew = malloc(sizeof(* ew));
504 if (ew == NULL)
505 return NULL;
506 ew->wd_charset = wd_charset;
507 ew->wd_text = wd_text;
508
509 return ew;
510}
511
512void mailmime_charset_free(char * charset)
513{
514 free(charset);
515}
516
517void mailmime_encoded_text_free(char * text)
518{
519 free(text);
520}
521
522void mailmime_encoded_word_free(struct mailmime_encoded_word * ew)
523{
524 mailmime_charset_free(ew->wd_charset);
525 mailmime_encoded_text_free(ew->wd_text);
526 free(ew);
527}
528
529
530
531/* mailmime_disposition */
532
533
534struct mailmime_disposition *
535mailmime_disposition_new(struct mailmime_disposition_type * dsp_type,
536 clist * dsp_parms)
537{
538 struct mailmime_disposition * dsp;
539
540 dsp = malloc(sizeof(* dsp));
541 if (dsp == NULL)
542 return NULL;
543 dsp->dsp_type = dsp_type;
544 dsp->dsp_parms = dsp_parms;
545
546 return dsp;
547}
548
549void mailmime_disposition_free(struct mailmime_disposition * dsp)
550{
551 mailmime_disposition_type_free(dsp->dsp_type);
552 clist_foreach(dsp->dsp_parms,
553 (clist_func) mailmime_disposition_parm_free, NULL);
554 clist_free(dsp->dsp_parms);
555 free(dsp);
556}
557
558
559
560struct mailmime_disposition_type *
561mailmime_disposition_type_new(int dsp_type, char * dsp_extension)
562{
563 struct mailmime_disposition_type * m_dsp_type;
564
565 m_dsp_type = malloc(sizeof(* m_dsp_type));
566 if (m_dsp_type == NULL)
567 return NULL;
568
569 m_dsp_type->dsp_type = dsp_type;
570 m_dsp_type->dsp_extension = dsp_extension;
571
572 return m_dsp_type;
573}
574
575void mailmime_disposition_type_free(struct mailmime_disposition_type * dsp_type)
576{
577 if (dsp_type->dsp_extension != NULL)
578 free(dsp_type->dsp_extension);
579 free(dsp_type);
580}
581
582
583struct mailmime_disposition_parm *
584mailmime_disposition_parm_new(int pa_type,
585 char * pa_filename,
586 char * pa_creation_date,
587 char * pa_modification_date,
588 char * pa_read_date,
589 size_t pa_size,
590 struct mailmime_parameter * pa_parameter)
591{
592 struct mailmime_disposition_parm * dsp_parm;
593
594 dsp_parm = malloc(sizeof(* dsp_parm));
595 if (dsp_parm == NULL)
596 return NULL;
597
598 dsp_parm->pa_type = pa_type;
599 switch (pa_type) {
600 case MAILMIME_DISPOSITION_PARM_FILENAME:
601 dsp_parm->pa_data.pa_filename = pa_filename;
602 break;
603 case MAILMIME_DISPOSITION_PARM_CREATION_DATE:
604 dsp_parm->pa_data.pa_creation_date = pa_creation_date;
605 break;
606 case MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE:
607 dsp_parm->pa_data.pa_modification_date = pa_modification_date;
608 break;
609 case MAILMIME_DISPOSITION_PARM_READ_DATE:
610 dsp_parm->pa_data.pa_read_date = pa_read_date;
611 break;
612 case MAILMIME_DISPOSITION_PARM_SIZE:
613 dsp_parm->pa_data.pa_size = pa_size;
614 break;
615 case MAILMIME_DISPOSITION_PARM_PARAMETER:
616 dsp_parm->pa_data.pa_parameter = pa_parameter;
617 break;
618 }
619
620 return dsp_parm;
621}
622
623void mailmime_disposition_parm_free(struct mailmime_disposition_parm *
624 dsp_parm)
625{
626 switch (dsp_parm->pa_type) {
627 case MAILMIME_DISPOSITION_PARM_FILENAME:
628 mailmime_filename_parm_free(dsp_parm->pa_data.pa_filename);
629 break;
630 case MAILMIME_DISPOSITION_PARM_CREATION_DATE:
631 mailmime_creation_date_parm_free(dsp_parm->pa_data.pa_creation_date);
632 break;
633 case MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE:
634 mailmime_modification_date_parm_free(dsp_parm->pa_data.pa_modification_date);
635 break;
636 case MAILMIME_DISPOSITION_PARM_READ_DATE:
637 mailmime_read_date_parm_free(dsp_parm->pa_data.pa_read_date);
638 break;
639 case MAILMIME_DISPOSITION_PARM_PARAMETER:
640 mailmime_parameter_free(dsp_parm->pa_data.pa_parameter);
641 break;
642 }
643
644 free(dsp_parm);
645}
646
647
648void mailmime_filename_parm_free(char * filename)
649{
650 mailmime_value_free(filename);
651}
652
653void mailmime_creation_date_parm_free(char * date)
654{
655 mailmime_quoted_date_time_free(date);
656}
657
658void mailmime_modification_date_parm_free(char * date)
659{
660 mailmime_quoted_date_time_free(date);
661}
662
663void mailmime_read_date_parm_free(char * date)
664{
665 mailmime_quoted_date_time_free(date);
666}
667
668void mailmime_quoted_date_time_free(char * date)
669{
670 mailimf_quoted_string_free(date);
671}
672
673struct mailmime_section * mailmime_section_new(clist * sec_list)
674{
675 struct mailmime_section * section;
676
677 section = malloc(sizeof(* section));
678 if (section == NULL)
679 return NULL;
680
681 section->sec_list = sec_list;
682
683 return section;
684}
685
686void mailmime_section_free(struct mailmime_section * section)
687{
688 clist_foreach(section->sec_list, (clist_func) free, NULL);
689 clist_free(section->sec_list);
690 free(section);
691}
692
693
694
695struct mailmime_language * mailmime_language_new(clist * lg_list)
696{
697 struct mailmime_language * lang;
698
699 lang = malloc(sizeof(* lang));
700 if (lang == NULL)
701 return NULL;
702
703 lang->lg_list = lg_list;
704
705 return lang;
706}
707
708void mailmime_language_free(struct mailmime_language * lang)
709{
710 clist_foreach(lang->lg_list, (clist_func) mailimf_atom_free, NULL);
711 clist_free(lang->lg_list);
712 free(lang);
713}
714
715void mailmime_decoded_part_free(char * part)
716{
717 mmap_string_unref(part);
718}
719
720struct mailmime_data * mailmime_data_new(int dt_type, int dt_encoding,
721 int dt_encoded, const char * dt_data, size_t dt_length, char * dt_filename)
722{
723 struct mailmime_data * mime_data;
724
725 mime_data = malloc(sizeof(* mime_data));
726 if (mime_data == NULL)
727 return NULL;
728
729 mime_data->dt_type = dt_type;
730 mime_data->dt_encoding = dt_encoding;
731 mime_data->dt_encoded = dt_encoded;
732 switch (dt_type) {
733 case MAILMIME_DATA_TEXT:
734 mime_data->dt_data.dt_text.dt_data = dt_data;
735 mime_data->dt_data.dt_text.dt_length = dt_length;
736 break;
737 case MAILMIME_DATA_FILE:
738 mime_data->dt_data.dt_filename = dt_filename;
739 break;
740 }
741
742 return mime_data;
743}
744
745void mailmime_data_free(struct mailmime_data * mime_data)
746{
747 switch (mime_data->dt_type) {
748 case MAILMIME_DATA_FILE:
749 free(mime_data->dt_data.dt_filename);
750 break;
751 }
752 free(mime_data);
753}