1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
2 /* camel-mime-part-utils : Utility for mime parsing and so on
4 * Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
5 * Michael Zucchi <notzed@ximian.com>
6 * Jeffrey Stedfast <fejj@ximian.com>
8 * Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU Lesser General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
35 #include <libedataserver/e-iconv.h>
37 #include "camel-charset-map.h"
38 #include "camel-html-parser.h"
39 #include "camel-mime-filter-basic.h"
40 #include "camel-mime-filter-charset.h"
41 #include "camel-mime-filter-crlf.h"
42 #include "camel-mime-filter-save.h"
43 #include "camel-mime-message.h"
44 #include "camel-mime-part-utils.h"
45 #include "camel-multipart-encrypted.h"
46 #include "camel-multipart-signed.h"
47 #include "camel-multipart.h"
48 #include "camel-seekable-substream.h"
49 #include "camel-stream-filter.h"
50 #include "camel-stream-fs.h"
51 #include "camel-stream-mem.h"
53 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))
56 /* simple data wrapper */
58 simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw, CamelMimeParser *mp)
65 d(printf ("simple_data_wrapper_construct_from_parser()\n"));
67 /* read in the entire content */
68 buffer = g_byte_array_new ();
69 while (camel_mime_parser_step (mp, &buf, &len) != CAMEL_MIME_PARSER_STATE_BODY_END) {
70 d(printf("appending o/p data: %d: %.*s\n", len, len, buf));
71 g_byte_array_append (buffer, buf, len);
74 d(printf("message part kept in memory!\n"));
76 mem = camel_stream_mem_new_with_byte_array (buffer);
77 camel_data_wrapper_construct_from_stream (dw, mem);
78 camel_object_unref (mem);
81 /* This replaces the data wrapper repository ... and/or could be replaced by it? */
83 camel_mime_part_construct_content_from_parser (CamelMimePart *dw, CamelMimeParser *mp)
85 CamelDataWrapper *content = NULL;
92 ct = camel_mime_parser_content_type (mp);
94 encoding = camel_content_transfer_encoding_decode (camel_mime_parser_header (mp, "Content-Transfer-Encoding", NULL));
96 switch (camel_mime_parser_state (mp)) {
97 case CAMEL_MIME_PARSER_STATE_HEADER:
98 d(printf("Creating body part\n"));
99 /* multipart/signed is some fucked up type that we must treat as binary data, fun huh, idiots. */
100 if (camel_content_type_is (ct, "multipart", "signed")) {
101 content = (CamelDataWrapper *) camel_multipart_signed_new ();
102 camel_multipart_construct_from_parser ((CamelMultipart *) content, mp);
104 content = camel_data_wrapper_new ();
105 simple_data_wrapper_construct_from_parser (content, mp);
108 case CAMEL_MIME_PARSER_STATE_MESSAGE:
109 d(printf("Creating message part\n"));
110 content = (CamelDataWrapper *) camel_mime_message_new ();
111 camel_mime_part_construct_from_parser ((CamelMimePart *)content, mp);
113 case CAMEL_MIME_PARSER_STATE_MULTIPART:
114 d(printf("Creating multi-part\n"));
115 if (camel_content_type_is (ct, "multipart", "encrypted"))
116 content = (CamelDataWrapper *) camel_multipart_encrypted_new ();
117 else if (camel_content_type_is (ct, "multipart", "signed"))
118 content = (CamelDataWrapper *) camel_multipart_signed_new ();
120 content = (CamelDataWrapper *) camel_multipart_new ();
122 camel_multipart_construct_from_parser((CamelMultipart *)content, mp);
123 d(printf("Created multi-part\n"));
126 g_warning("Invalid state encountered???: %u", camel_mime_parser_state (mp));
131 content->encoding = camel_transfer_encoding_from_string (encoding);
133 /* would you believe you have to set this BEFORE you set the content object??? oh my god !!!! */
134 camel_data_wrapper_set_mime_type_field (content, camel_mime_part_get_content_type (dw));
135 camel_medium_set_content_object ((CamelMedium *)dw, content);
136 camel_object_unref (content);