Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-mime-part-utils.c
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
3  *
4  * Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
5  *          Michael Zucchi <notzed@ximian.com>
6  *          Jeffrey Stedfast <fejj@ximian.com>
7  *
8  * Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
9  *
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.
13  *
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.
18  *
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
22  * USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include <libedataserver/e-iconv.h>
36
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"
52
53 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))
54                #include <stdio.h>*/
55
56 /* simple data wrapper */
57 static void
58 simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw, CamelMimeParser *mp)
59 {
60         char *buf;
61         GByteArray *buffer;
62         CamelStream *mem;
63         size_t len;
64         
65         d(printf ("simple_data_wrapper_construct_from_parser()\n"));
66         
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);
72         }
73         
74         d(printf("message part kept in memory!\n"));
75         
76         mem = camel_stream_mem_new_with_byte_array (buffer);
77         camel_data_wrapper_construct_from_stream (dw, mem);
78         camel_object_unref (mem);
79 }
80
81 /* This replaces the data wrapper repository ... and/or could be replaced by it? */
82 void
83 camel_mime_part_construct_content_from_parser (CamelMimePart *dw, CamelMimeParser *mp)
84 {
85         CamelDataWrapper *content = NULL;
86         CamelContentType *ct;
87         char *encoding;
88
89         if (!dw)
90                 return;
91
92         ct = camel_mime_parser_content_type (mp);
93
94         encoding = camel_content_transfer_encoding_decode (camel_mime_parser_header (mp, "Content-Transfer-Encoding", NULL));
95         
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);
103                 } else {
104                         content = camel_data_wrapper_new ();
105                         simple_data_wrapper_construct_from_parser (content, mp);
106                 }
107                 break;
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);
112                 break;
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 ();
119                 else
120                         content = (CamelDataWrapper *) camel_multipart_new ();
121                 
122                 camel_multipart_construct_from_parser((CamelMultipart *)content, mp);
123                 d(printf("Created multi-part\n"));
124                 break;
125         default:
126                 g_warning("Invalid state encountered???: %u", camel_mime_parser_state (mp));
127         }
128         
129         if (content) {
130                 if (encoding)
131                         content->encoding = camel_transfer_encoding_from_string (encoding);
132
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);
137         }
138
139         g_free (encoding);
140 }