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, 2000 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 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include <gal/util/e-iconv.h>
37 #include "camel-charset-map.h"
38 #include "camel-mime-part-utils.h"
39 #include "camel-mime-message.h"
40 #include "camel-multipart.h"
41 #include "camel-multipart-signed.h"
42 #include "camel-multipart-encrypted.h"
43 #include "camel-seekable-substream.h"
44 #include "camel-stream-fs.h"
45 #include "camel-stream-filter.h"
46 #include "camel-stream-mem.h"
47 #include "camel-mime-filter-basic.h"
48 #include "camel-mime-filter-charset.h"
49 #include "camel-mime-filter-crlf.h"
50 #include "camel-mime-filter-save.h"
51 #include "camel-html-parser.h"
53 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))
56 /* example: <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> */
59 check_html_charset(char *buffer, int length)
62 const char *charset = NULL;
63 camel_html_parser_t state;
64 struct _header_content_type *ct;
66 /* if we need to first base64/qp decode, do this here, sigh */
67 hp = camel_html_parser_new();
68 camel_html_parser_set_data(hp, buffer, length, TRUE);
75 state = camel_html_parser_step(hp, &data, &len);
77 /* example: <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> */
80 case CAMEL_HTML_PARSER_ELEMENT:
81 val = camel_html_parser_tag(hp);
82 d(printf("Got tag: %s\n", val));
83 if (strcasecmp(val, "meta") == 0
84 && (val = camel_html_parser_attr(hp, "http-equiv"))
85 && strcasecmp(val, "content-type") == 0
86 && (val = camel_html_parser_attr(hp, "content"))
87 && (ct = header_content_type_decode(val))) {
88 charset = header_content_type_param(ct, "charset");
89 charset = e_iconv_charset_name (charset);
90 header_content_type_unref(ct);
94 /* ignore everything else */
97 } while (charset == NULL && state != CAMEL_HTML_PARSER_EOF);
99 camel_object_unref (hp);
105 convert_buffer (GByteArray *in, const char *to, const char *from)
107 size_t inleft, outleft, outlen, converted = 0;
108 GByteArray *out = NULL;
114 return g_byte_array_new();
116 d(printf("converting buffer from %s to %s:\n", from, to));
117 d(fwrite(in->data, 1, (int)in->len, stdout));
120 cd = e_iconv_open(to, from);
121 if (cd == (iconv_t) -1) {
122 g_warning ("Cannot convert from '%s' to '%s': %s", from, to, strerror (errno));
126 outlen = in->len * 2 + 16;
127 out = g_byte_array_new ();
128 g_byte_array_set_size (out, outlen);
134 outbuf = out->data + converted;
135 outleft = outlen - converted;
137 converted = e_iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
138 if (converted == (size_t) -1) {
139 if (errno != E2BIG && errno != EINVAL)
144 * E2BIG There is not sufficient room at *outbuf.
146 * We just need to grow our outbuffer and try again.
149 converted = outbuf - (char *)out->data;
150 if (errno == E2BIG) {
151 outlen += inleft * 2 + 16;
152 out = g_byte_array_set_size (out, outlen);
153 outbuf = out->data + converted;
156 } while (errno == E2BIG && inleft > 0);
159 * EINVAL An incomplete multibyte sequence has been encounĀ
160 * tered in the input.
162 * We'll just have to ignore it...
165 /* flush the iconv conversion */
166 e_iconv (cd, NULL, NULL, &outbuf, &outleft);
168 /* now set the true length on the GByteArray */
169 converted = outbuf - (char *)out->data;
170 g_byte_array_set_size (out, converted);
172 d(printf("converted data:\n"));
173 d(fwrite(out->data, 1, (int)out->len, stdout));
181 g_warning ("Cannot convert from '%s' to '%s': %s", from, to, strerror (errno));
183 g_byte_array_free (out, TRUE);
190 /* We don't really use the charset argument except for debugging... */
192 broken_windows_charset (GByteArray *buffer, const char *charset)
194 register unsigned char *inptr;
195 unsigned char *inend;
197 inptr = buffer->data;
198 inend = inptr + buffer->len;
200 while (inptr < inend) {
201 register unsigned char c = *inptr++;
203 if (c >= 128 && c <= 159) {
204 g_warning ("Encountered Windows charset parading as %s", charset);
213 is_7bit (GByteArray *buffer)
215 register unsigned int i;
217 for (i = 0; i < buffer->len; i++)
218 if (buffer->data[i] > 127)
224 static const char *iso_charsets[] = {
244 #define NUM_ISO_CHARSETS (sizeof (iso_charsets) / sizeof (iso_charsets[0]))
247 canon_charset_name (const char *charset)
253 if (strncasecmp (charset, "iso", 3) != 0)
257 if (*ptr == '-' || *ptr == '_')
260 /* if it's not an iso-8859-# charset, we don't care about it */
261 if (strncmp (ptr, "8859", 4) != 0)
265 if (*ptr == '-' || *ptr == '_')
268 iso = strtoul (ptr, &endptr, 10);
269 if (endptr == ptr || *endptr != '\0')
272 if (iso >= NUM_ISO_CHARSETS)
275 return iso_charsets[iso];
278 /* simple data wrapper */
280 simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw, CamelMimeParser *mp)
282 CamelMimeFilter *fdec = NULL, *fcrlf = NULL;
283 CamelMimeFilterBasicType enctype = 0;
285 int decid = -1, crlfid = -1;
286 struct _header_content_type *ct;
287 const char *charset = NULL;
288 char *encoding, *buf;
292 d(printf ("simple_data_wrapper_construct_from_parser()\n"));
294 /* first, work out conversion, if any, required, we dont care about what we dont know about */
295 encoding = header_content_encoding_decode (camel_mime_parser_header (mp, "Content-Transfer-Encoding", NULL));
297 if (!strcasecmp (encoding, "base64")) {
298 d(printf("Adding base64 decoder ...\n"));
299 enctype = CAMEL_MIME_FILTER_BASIC_BASE64_DEC;
300 } else if (!strcasecmp (encoding, "quoted-printable")) {
301 d(printf("Adding quoted-printable decoder ...\n"));
302 enctype = CAMEL_MIME_FILTER_BASIC_QP_DEC;
303 } else if (!strcasecmp (encoding, "x-uuencode")) {
304 d(printf("Adding uudecoder ...\n"));
305 enctype = CAMEL_MIME_FILTER_BASIC_UU_DEC;
310 fdec = (CamelMimeFilter *)camel_mime_filter_basic_new_type(enctype);
311 decid = camel_mime_parser_filter_add (mp, fdec);
315 /* If we're doing text, we also need to do CRLF->LF and may have to convert it to UTF8 as well. */
316 ct = camel_mime_parser_content_type (mp);
317 if (header_content_type_is (ct, "text", "*")) {
318 charset = header_content_type_param (ct, "charset");
319 charset = e_iconv_charset_name (charset);
322 d(printf ("Adding CRLF conversion filter\n"));
323 fcrlf = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE,
324 CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
325 crlfid = camel_mime_parser_filter_add (mp, fcrlf);
329 /* read in the entire content */
330 buffer = g_byte_array_new ();
331 while (camel_mime_parser_step (mp, &buf, &len) != HSCAN_BODY_END) {
332 d(printf("appending o/p data: %d: %.*s\n", len, len, buf));
333 g_byte_array_append (buffer, buf, len);
336 /* check for broken Outlook/Web mailers that like to send html marked as text/plain */
337 if (header_content_type_is (ct, "text", "plain")) {
338 register const unsigned char *inptr;
339 const unsigned char *inend;
341 inptr = buffer->data;
342 inend = inptr + buffer->len;
344 while (inptr < inend && isspace ((int) *inptr))
347 if (((inend-inptr) > 5 && g_ascii_strncasecmp(inptr, "<html", 5) == 0)
348 || ((inend-inptr) > 9 && g_ascii_strncasecmp(inptr, "<!doctype", 9) == 0)) {
349 /* re-tag as text/html */
350 g_free (ct->subtype);
351 ct->subtype = g_strdup ("html");
355 /* Possible Lame Mailer Alert... check the META tags for a charset */
356 if (!charset && header_content_type_is (ct, "text", "html")) {
357 if ((charset = check_html_charset (buffer->data, buffer->len)))
358 header_content_type_set_param (ct, "charset", charset);
361 /* if we need to do charset conversion, see if we can/it works/etc */
362 if (charset && !(strcasecmp (charset, "us-ascii") == 0
363 || strcasecmp (charset, "utf-8") == 0
364 || strncasecmp (charset, "x-", 2) == 0)) {
367 /* You often see Microsoft Windows users announcing their texts
368 * as being in ISO-8859-1 even when in fact they contain funny
369 * characters from the Windows-CP1252 superset.
371 charset = canon_charset_name (charset);
372 if (!strncasecmp (charset, "iso-8859", 8)) {
373 /* check for Windows-specific chars... */
374 if (broken_windows_charset (buffer, charset))
375 charset = camel_charset_iso_to_windows (charset);
378 out = convert_buffer (buffer, "UTF-8", charset);
380 /* converted ok, use this data instead */
381 g_byte_array_free(buffer, TRUE);
385 /* else failed to convert, leave as raw? */
386 g_warning("Storing text as raw, unknown charset '%s' or invalid format", charset);
389 } else if (header_content_type_is (ct, "text", "*")) {
390 if (charset == NULL || !strcasecmp (charset, "us-ascii")) {
391 /* check that it's 7bit */
392 dw->rawtext = !is_7bit (buffer);
393 } else if (!strncasecmp (charset, "x-", 2)) {
394 /* we're not even going to bother trying to convert, so set the
395 rawtext bit to TRUE and let the mailer deal with it. */
397 } else if (!strcasecmp (charset, "utf-8") && buffer->len) {
398 /* check that it is valid utf8 */
399 dw->rawtext = !g_utf8_validate (buffer->data, buffer->len, NULL);
403 d(printf("message part kept in memory!\n"));
405 mem = camel_stream_mem_new_with_byte_array(buffer);
406 camel_data_wrapper_construct_from_stream(dw, mem);
407 camel_object_unref((CamelObject *)mem);
409 camel_mime_parser_filter_remove(mp, decid);
410 camel_mime_parser_filter_remove(mp, crlfid);
413 camel_object_unref((CamelObject *)fdec);
415 camel_object_unref((CamelObject *)fcrlf);
418 /* This replaces the data wrapper repository ... and/or could be replaced by it? */
420 camel_mime_part_construct_content_from_parser (CamelMimePart *dw, CamelMimeParser *mp)
422 CamelDataWrapper *content = NULL;
423 CamelContentType *ct;
425 ct = camel_mime_parser_content_type (mp);
427 switch (camel_mime_parser_state (mp)) {
429 d(printf("Creating body part\n"));
430 /* multipart/signed is some fucked up type that we must treat as binary data, fun huh, idiots. */
431 if (header_content_type_is (ct, "multipart", "signed")) {
432 content = (CamelDataWrapper *) camel_multipart_signed_new ();
433 camel_multipart_construct_from_parser ((CamelMultipart *) content, mp);
435 content = camel_data_wrapper_new ();
436 simple_data_wrapper_construct_from_parser (content, mp);
440 d(printf("Creating message part\n"));
441 content = (CamelDataWrapper *) camel_mime_message_new ();
442 camel_mime_part_construct_from_parser ((CamelMimePart *)content, mp);
444 case HSCAN_MULTIPART:
445 d(printf("Creating multi-part\n"));
446 if (header_content_type_is (ct, "multipart", "encrypted"))
447 content = (CamelDataWrapper *) camel_multipart_encrypted_new ();
448 else if (header_content_type_is (ct, "multipart", "signed"))
449 content = (CamelDataWrapper *) camel_multipart_signed_new ();
451 content = (CamelDataWrapper *) camel_multipart_new ();
453 camel_multipart_construct_from_parser((CamelMultipart *)content, mp);
454 d(printf("Created multi-part\n"));
457 g_warning("Invalid state encountered???: %d", camel_mime_parser_state (mp));
460 /* would you believe you have to set this BEFORE you set the content object??? oh my god !!!! */
461 camel_data_wrapper_set_mime_type_field (content, camel_mime_part_get_content_type (dw));
462 camel_medium_set_content_object ((CamelMedium *)dw, content);
464 /* Note: we don't set ct as the content-object's mime-type above because
465 * camel_medium_set_content_object() may re-write the Content-Type header
466 * (see CamelMimePart::set_content_object) if we did that (which is a Bad Thing).
467 * However, if we set it *afterward*, we can still use any special auto-detections
468 * that we found in simple_data_wrapper_construct_from_parser(). This is important
469 * later when we go to render the MIME parts in mail-format.c */
470 camel_data_wrapper_set_mime_type_field (content, ct);
472 camel_object_unref (content);