diff options
author | Jeffrey Stedfast <fejj@ximian.com> | 2003-08-12 01:57:45 +0800 |
---|---|---|
committer | Jeffrey Stedfast <fejj@src.gnome.org> | 2003-08-12 01:57:45 +0800 |
commit | b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb (patch) | |
tree | 2c0f3caac1a793197a951958fa7f07cdbf61ac07 /camel | |
parent | 7b1013be730b11384d0e0af340758bdef3f00330 (diff) | |
download | gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar.gz gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar.bz2 gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar.lz gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar.xz gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.tar.zst gsoc2013-evolution-b328a21e7c026aaa9cdd5e332ed7e39e0003d8eb.zip |
New test suite for the mime parser (which is where the below 2 fixes were
* tests/message/test4.c: New test suite for the mime parser (which
is where the below 2 fixes were noticed).
* camel-mime-parser.c (folder_boundary_check): Calculate 'len' by
subtracting the boundary start from inend rather than 'atleast'.
(folder_scan_content): Calculate 'inend' differently depending on
the EOF state.
2003-08-08 Jeffrey Stedfast <fejj@ximian.com>
* camel-mime-filter-tohtml.c (html_convert): Rather than checking
*inptr == '\n', check inptr >= inend - this gets rid of an Invalid
Read report from valgrind.
* camel-mime-part.c (write_to_stream): Don't necessarily re-encode
just because the encodings differ. Need to look into making it so
that message/rfc822 and multipart parts ignore the
Content-Transfer-Encoding header and just keep their 'encoding'
bits set to DEFAULT.
2003-08-05 Jeffrey Stedfast <fejj@ximian.com>
* providers/imap/camel-imap-folder.c (get_content): Updated.
* camel-mime-message.c (camel_mime_message_init): Don't override
the mime_type here.
(process_header): Updated to use CamelDataWrapper's mime_type
field.
(find_best_encoding): Same.
(best_encoding): Here too.
* camel-digest-folder.c (camel_digest_folder_new): Updated for
CamelMimePart::content_type change.
* camel-mime-part.c (camel_mime_part_init): Override our parent
class's default mime_type.
(camel_mime_part_finalize): Don't need to unref the content_type
anymore.
(process_header): Updated to use CamelDataWrapper's mime_type
field.
(camel_mime_part_set_filename): Same.
(camel_mime_part_get_filename): Same.
(camel_mime_part_get_content_type): Same.
(set_content_object): Here too.
(write_to_stream): Updated.
(construct_from_parser): Updated.
* camel-mime-part.h: Remove the content_type field.
2003-07-31 Jeffrey Stedfast <fejj@ximian.com>
* tests/lib/messages.c (test_message_compare_content): If the
chunks differ, perform a hexdump on the data being compared so
that we may analyse it easier.
* camel-multipart-signed.c (write_to_stream): Return ssize_t.
* camel-mime-utils.h: Added the CamelMimePartEncodingType enum
here.
* camel-mime-part.h: Removed the CamelMimePartEncodingType enum
from here.
* camel-mime-part.c (write_to_stream): Updated to return
ssize_t. Also minor changes to only re-encode the content stream
if the charset or encoding changed (this way we write it out in
the original raw form if nothing changed).
* camel-mime-part-utils.c
(simple_data_wrapper_construct_from_parser): Drastically
simplify. We no longer scan html content to try and find the
charset, nor do we care about converting the content to UTF-8 and
handling broken windows charsets.
* camel-mime-message.c (find_best_encoding): Use
decode_to_stream() here. Also updated to not assume the content
charset is UTF-8 since it is very likely not the case anymore
since data-wrappers no longer are converted to UTF-8 at parse
time.
* camel-folder-summary.c (summary_build_content_info_message): Use
decode_to_stream instead here too.
* camel-folder-search.c (match_words_1message): Use
decode_to_stream instead of write_to_stream so we can search the
contents.
* camel-data-wrapper.c (camel_data_wrapper_init): Set the default
encoding to DEFAULT.
(write_to_stream): Updated to return ssize_t
(camel_data_wrapper_decode_to_stream): New virtual function to
decode a data wrapper to a stream (results in nearly identical
behaviour to the old write_to_stream method).
(decode_to_stream): Default implementation of above virtual
method. Decodes base64/qp/etc streams.
* camel-data-wrapper.h: Removed the rawtext bit and added an
encoding member.
svn path=/trunk/; revision=22171
Diffstat (limited to 'camel')
-rw-r--r-- | camel/ChangeLog | 101 | ||||
-rw-r--r-- | camel/Makefile.am | 2 | ||||
-rw-r--r-- | camel/camel-data-wrapper.c | 107 | ||||
-rw-r--r-- | camel/camel-data-wrapper.h | 11 | ||||
-rw-r--r-- | camel/camel-digest-folder.c | 2 | ||||
-rw-r--r-- | camel/camel-folder-search.c | 2 | ||||
-rw-r--r-- | camel/camel-folder-summary.c | 2 | ||||
-rw-r--r-- | camel/camel-mime-filter-tohtml.c | 2 | ||||
-rw-r--r-- | camel/camel-mime-filter-windows.c | 180 | ||||
-rw-r--r-- | camel/camel-mime-filter-windows.h | 65 | ||||
-rw-r--r-- | camel/camel-mime-message.c | 74 | ||||
-rw-r--r-- | camel/camel-mime-parser.c | 14 | ||||
-rw-r--r-- | camel/camel-mime-part-utils.c | 360 | ||||
-rw-r--r-- | camel/camel-mime-part.c | 244 | ||||
-rw-r--r-- | camel/camel-mime-part.h | 46 | ||||
-rw-r--r-- | camel/camel-mime-utils.h | 12 | ||||
-rw-r--r-- | camel/camel-multipart.c | 4 | ||||
-rw-r--r-- | camel/providers/imap/camel-imap-folder.c | 4 | ||||
-rw-r--r-- | camel/tests/lib/messages.c | 86 | ||||
-rw-r--r-- | camel/tests/lib/messages.h | 1 | ||||
-rw-r--r-- | camel/tests/message/Makefile.am | 5 | ||||
-rw-r--r-- | camel/tests/message/test4.c | 126 |
22 files changed, 884 insertions, 566 deletions
diff --git a/camel/ChangeLog b/camel/ChangeLog index 2c2516c433..84f649fbde 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,3 +1,104 @@ +2003-08-08 Jeffrey Stedfast <fejj@ximian.com> + + * tests/message/test4.c: New test suite for the mime parser (which + is where the below 2 fixes were noticed). + + * camel-mime-parser.c (folder_boundary_check): Calculate 'len' by + subtracting the boundary start from inend rather than 'atleast'. + (folder_scan_content): Calculate 'inend' differently depending on + the EOF state. + +2003-08-08 Jeffrey Stedfast <fejj@ximian.com> + + * camel-mime-filter-tohtml.c (html_convert): Rather than checking + *inptr == '\n', check inptr >= inend - this gets rid of an Invalid + Read report from valgrind. + + * camel-mime-part.c (write_to_stream): Don't necessarily re-encode + just because the encodings differ. Need to look into making it so + that message/rfc822 and multipart parts ignore the + Content-Transfer-Encoding header and just keep their 'encoding' + bits set to DEFAULT. + +2003-08-05 Jeffrey Stedfast <fejj@ximian.com> + + * providers/imap/camel-imap-folder.c (get_content): Updated. + + * camel-mime-message.c (camel_mime_message_init): Don't override + the mime_type here. + (process_header): Updated to use CamelDataWrapper's mime_type + field. + (find_best_encoding): Same. + (best_encoding): Here too. + + * camel-digest-folder.c (camel_digest_folder_new): Updated for + CamelMimePart::content_type change. + + * camel-mime-part.c (camel_mime_part_init): Override our parent + class's default mime_type. + (camel_mime_part_finalize): Don't need to unref the content_type + anymore. + (process_header): Updated to use CamelDataWrapper's mime_type + field. + (camel_mime_part_set_filename): Same. + (camel_mime_part_get_filename): Same. + (camel_mime_part_get_content_type): Same. + (set_content_object): Here too. + (write_to_stream): Updated. + (construct_from_parser): Updated. + + * camel-mime-part.h: Remove the content_type field. + +2003-07-31 Jeffrey Stedfast <fejj@ximian.com> + + * tests/lib/messages.c (test_message_compare_content): If the + chunks differ, perform a hexdump on the data being compared so + that we may analyse it easier. + + * camel-multipart-signed.c (write_to_stream): Return ssize_t. + + * camel-mime-utils.h: Added the CamelMimePartEncodingType enum + here. + + * camel-mime-part.h: Removed the CamelMimePartEncodingType enum + from here. + + * camel-mime-part.c (write_to_stream): Updated to return + ssize_t. Also minor changes to only re-encode the content stream + if the charset or encoding changed (this way we write it out in + the original raw form if nothing changed). + + * camel-mime-part-utils.c + (simple_data_wrapper_construct_from_parser): Drastically + simplify. We no longer scan html content to try and find the + charset, nor do we care about converting the content to UTF-8 and + handling broken windows charsets. + + * camel-mime-message.c (find_best_encoding): Use + decode_to_stream() here. Also updated to not assume the content + charset is UTF-8 since it is very likely not the case anymore + since data-wrappers no longer are converted to UTF-8 at parse + time. + + * camel-folder-summary.c (summary_build_content_info_message): Use + decode_to_stream instead here too. + + * camel-folder-search.c (match_words_1message): Use + decode_to_stream instead of write_to_stream so we can search the + contents. + + * camel-data-wrapper.c (camel_data_wrapper_init): Set the default + encoding to DEFAULT. + (write_to_stream): Updated to return ssize_t + (camel_data_wrapper_decode_to_stream): New virtual function to + decode a data wrapper to a stream (results in nearly identical + behaviour to the old write_to_stream method). + (decode_to_stream): Default implementation of above virtual + method. Decodes base64/qp/etc streams. + + * camel-data-wrapper.h: Removed the rawtext bit and added an + encoding member. + 2003-08-01 Jeffrey Stedfast <fejj@ximian.com> * tests/smime/pgp-mime.c: Same. diff --git a/camel/Makefile.am b/camel/Makefile.am index b07af12db1..e1450ea40f 100644 --- a/camel/Makefile.am +++ b/camel/Makefile.am @@ -63,6 +63,7 @@ libcamel_la_SOURCES = \ camel-mime-filter-index.c \ camel-mime-filter-linewrap.c \ camel-mime-filter-save.c \ + camel-mime-filter-windows.c \ camel-mime-filter.c \ camel-mime-message.c \ camel-mime-parser.c \ @@ -161,6 +162,7 @@ libcamelinclude_HEADERS = \ camel-mime-filter-index.h \ camel-mime-filter-linewrap.h \ camel-mime-filter-save.h \ + camel-mime-filter-windows.h \ camel-mime-filter.h \ camel-mime-message.h \ camel-mime-parser.h \ diff --git a/camel/camel-data-wrapper.c b/camel/camel-data-wrapper.c index 1f2066aa7a..f950c5d919 100644 --- a/camel/camel-data-wrapper.c +++ b/camel/camel-data-wrapper.c @@ -28,8 +28,10 @@ #include <errno.h> #include "camel-data-wrapper.h" -#include "camel-mime-utils.h" #include "camel-stream.h" +#include "camel-stream-filter.h" +#include "camel-mime-filter-basic.h" +#include "camel-mime-filter-crlf.h" #include "camel-exception.h" #include "camel-private.h" @@ -40,8 +42,9 @@ static CamelObjectClass *parent_class = NULL; /* Returns the class for a CamelDataWrapper */ #define CDW_CLASS(so) CAMEL_DATA_WRAPPER_CLASS (CAMEL_OBJECT_GET_CLASS(so)) -static int construct_from_stream (CamelDataWrapper *, CamelStream *); +static int construct_from_stream(CamelDataWrapper *, CamelStream *); static ssize_t write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream); +static ssize_t decode_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream); static void set_mime_type (CamelDataWrapper *data_wrapper, const char *mime_type); static gchar *get_mime_type (CamelDataWrapper *data_wrapper); static CamelContentType *get_mime_type_field (CamelDataWrapper *data_wrapper); @@ -55,6 +58,7 @@ camel_data_wrapper_class_init (CamelDataWrapperClass *camel_data_wrapper_class) /* virtual method definition */ camel_data_wrapper_class->write_to_stream = write_to_stream; + camel_data_wrapper_class->decode_to_stream = decode_to_stream; camel_data_wrapper_class->set_mime_type = set_mime_type; camel_data_wrapper_class->get_mime_type = get_mime_type; camel_data_wrapper_class->get_mime_type_field = get_mime_type_field; @@ -72,8 +76,8 @@ camel_data_wrapper_init (gpointer object, gpointer klass) pthread_mutex_init (&camel_data_wrapper->priv->stream_lock, NULL); camel_data_wrapper->mime_type = header_content_type_new ("application", "octet-stream"); + camel_data_wrapper->encoding = CAMEL_MIME_PART_ENCODING_DEFAULT; camel_data_wrapper->offline = FALSE; - camel_data_wrapper->rawtext = TRUE; } static void @@ -111,6 +115,13 @@ camel_data_wrapper_get_type (void) return type; } + +CamelDataWrapper * +camel_data_wrapper_new (void) +{ + return (CamelDataWrapper *) camel_object_new (CAMEL_DATA_WRAPPER_TYPE); +} + static ssize_t write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream) { @@ -133,12 +144,6 @@ write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream) return ret; } -CamelDataWrapper * -camel_data_wrapper_new(void) -{ - return (CamelDataWrapper *)camel_object_new(camel_data_wrapper_get_type()); -} - /** * camel_data_wrapper_write_to_stream: * @data_wrapper: a data wrapper @@ -148,11 +153,12 @@ camel_data_wrapper_new(void) * Writes the data content to @stream in a machine-independent format * appropriate for the data. It should be possible to construct an * equivalent data wrapper object later by passing this stream to - * camel_data_construct_from_stream(). + * camel_data_wrapper_construct_from_stream(). * - * Return value: the number of bytes written, or -1 if an error occurs. + * Return value: the number of bytes written, or -1 if an error + * occurs. **/ -int +ssize_t camel_data_wrapper_write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream) { @@ -162,6 +168,83 @@ camel_data_wrapper_write_to_stream (CamelDataWrapper *data_wrapper, return CDW_CLASS (data_wrapper)->write_to_stream (data_wrapper, stream); } + +static ssize_t +decode_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream) +{ + CamelMimeFilter *filter; + CamelStream *fstream; + ssize_t ret; + + if (data_wrapper->stream == NULL) { + return -1; + } + + CAMEL_DATA_WRAPPER_LOCK (data_wrapper, stream_lock); + if (camel_stream_reset (data_wrapper->stream) == -1) { + CAMEL_DATA_WRAPPER_UNLOCK (data_wrapper, stream_lock); + return -1; + } + + fstream = (CamelStream *) camel_stream_filter_new_with_stream (data_wrapper->stream); + + switch (data_wrapper->encoding) { + case CAMEL_MIME_PART_ENCODING_BASE64: + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_BASE64_DEC); + camel_stream_filter_add (CAMEL_STREAM_FILTER (fstream), filter); + camel_object_unref (filter); + break; + case CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE: + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_QP_DEC); + camel_stream_filter_add (CAMEL_STREAM_FILTER (fstream), filter); + camel_object_unref (filter); + break; + case CAMEL_MIME_PART_ENCODING_UUENCODE: + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_UU_DEC); + camel_stream_filter_add (CAMEL_STREAM_FILTER (fstream), filter); + camel_object_unref (filter); + break; + default: + break; + } + + if (header_content_type_is (data_wrapper->mime_type, "text", "*")) { + filter = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE, + CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY); + camel_stream_filter_add (CAMEL_STREAM_FILTER (fstream), filter); + camel_object_unref (filter); + } + + ret = camel_stream_write_to_stream (fstream, stream); + camel_object_unref (fstream); + + CAMEL_DATA_WRAPPER_UNLOCK (data_wrapper, stream_lock); + + return ret; +} + +/** + * camel_data_wrapper_decode_to_stream: + * @data_wrapper: a data wrapper + * @stream: stream for decoded data to be written to + * @ex: a CamelException + * + * Writes the decoded data content to @stream. + * + * Return value: the number of bytes written, or -1 if an error + * occurs. + **/ +ssize_t +camel_data_wrapper_decode_to_stream (CamelDataWrapper *data_wrapper, + CamelStream *stream) +{ + g_return_val_if_fail (CAMEL_IS_DATA_WRAPPER (data_wrapper), -1); + g_return_val_if_fail (CAMEL_IS_STREAM (stream), -1); + + return CDW_CLASS (data_wrapper)->decode_to_stream (data_wrapper, stream); +} + + static int construct_from_stream (CamelDataWrapper *data_wrapper, CamelStream *stream) { diff --git a/camel/camel-data-wrapper.h b/camel/camel-data-wrapper.h index 1ca3754207..cfe7a8cf53 100644 --- a/camel/camel-data-wrapper.h +++ b/camel/camel-data-wrapper.h @@ -32,7 +32,9 @@ extern "C" { #endif /* __cplusplus */ #include <glib.h> +#include <sys/types.h> #include <camel/camel-object.h> +#include <camel/camel-mime-utils.h> #define CAMEL_DATA_WRAPPER_TYPE (camel_data_wrapper_get_type ()) #define CAMEL_DATA_WRAPPER(obj) (CAMEL_CHECK_CAST((obj), CAMEL_DATA_WRAPPER_TYPE, CamelDataWrapper)) @@ -43,11 +45,12 @@ struct _CamelDataWrapper { CamelObject parent_object; struct _CamelDataWrapperPrivate *priv; + CamelMimePartEncodingType encoding; + CamelContentType *mime_type; CamelStream *stream; unsigned int offline:1; - unsigned int rawtext:1; }; typedef struct { @@ -64,6 +67,9 @@ typedef struct { ssize_t (*write_to_stream) (CamelDataWrapper *data_wrapper, CamelStream *stream); + ssize_t (*decode_to_stream) (CamelDataWrapper *data_wrapper, + CamelStream *stream); + int (*construct_from_stream) (CamelDataWrapper *data_wrapper, CamelStream *); @@ -77,6 +83,9 @@ CamelType camel_data_wrapper_get_type (void); CamelDataWrapper *camel_data_wrapper_new(void); ssize_t camel_data_wrapper_write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream); +ssize_t camel_data_wrapper_decode_to_stream (CamelDataWrapper *data_wrapper, + CamelStream *stream); + void camel_data_wrapper_set_mime_type (CamelDataWrapper *data_wrapper, const char *mime_type); char *camel_data_wrapper_get_mime_type (CamelDataWrapper *data_wrapper); diff --git a/camel/camel-digest-folder.c b/camel/camel-digest-folder.c index 21a4071036..2f4ae06740 100644 --- a/camel/camel-digest-folder.c +++ b/camel/camel-digest-folder.c @@ -221,7 +221,7 @@ camel_digest_folder_new (CamelStore *parent_store, CamelMimeMessage *message) return NULL; /* Make sure we have a multipart/digest subpart or at least some message/rfc822 attachments... */ - if (!header_content_type_is (CAMEL_MIME_PART (message)->content_type, "multipart", "digest")) { + if (!header_content_type_is (CAMEL_DATA_WRAPPER (message)->mime_type, "multipart", "digest")) { if (!multipart_contains_message_parts (CAMEL_MULTIPART (wrapper))) return NULL; } diff --git a/camel/camel-folder-search.c b/camel/camel-folder-search.c index fee38ab9c3..f19ec19aa3 100644 --- a/camel/camel-folder-search.c +++ b/camel/camel-folder-search.c @@ -860,7 +860,7 @@ match_words_1message (CamelDataWrapper *object, struct _camel_search_words *word CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new (); /* FIXME: The match should be part of a stream op */ - camel_data_wrapper_write_to_stream (containee, CAMEL_STREAM (mem)); + camel_data_wrapper_decode_to_stream (containee, CAMEL_STREAM (mem)); camel_stream_write (CAMEL_STREAM (mem), "", 1); for (i=0;i<words->len;i++) { /* FIXME: This is horridly slow, and should use a real search algorithm */ diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c index 6f1fcabaa9..6306280f63 100644 --- a/camel/camel-folder-summary.c +++ b/camel/camel-folder-summary.c @@ -2193,7 +2193,7 @@ summary_build_content_info_message(CamelFolderSummary *s, CamelMessageInfo *msgi } idx_id = camel_stream_filter_add(p->filter_stream, (CamelMimeFilter *)p->filter_index); - camel_data_wrapper_write_to_stream(containee, (CamelStream *)p->filter_stream); + camel_data_wrapper_decode_to_stream(containee, (CamelStream *)p->filter_stream); camel_stream_flush((CamelStream *)p->filter_stream); camel_stream_filter_remove(p->filter_stream, idx_id); diff --git a/camel/camel-mime-filter-tohtml.c b/camel/camel-mime-filter-tohtml.c index 45112731a0..678d8005f4 100644 --- a/camel/camel-mime-filter-tohtml.c +++ b/camel/camel-mime-filter-tohtml.c @@ -254,7 +254,7 @@ html_convert (CamelMimeFilter *filter, char *in, size_t inlen, size_t prespace, while (inptr < inend && *inptr != '\n') inptr++; - if (*inptr != '\n' && !flush) + if (inptr >= inend && !flush) break; html->column = 0; diff --git a/camel/camel-mime-filter-windows.c b/camel/camel-mime-filter-windows.c new file mode 100644 index 0000000000..0b6e00dea0 --- /dev/null +++ b/camel/camel-mime-filter-windows.c @@ -0,0 +1,180 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2002 Ximian, Inc. (www.ximian.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <string.h> +#include <ctype.h> + +#include "camel-mime-filter-windows.h" + +#include "camel-charset-map.h" + +#define d(x) + +static void camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass); +static void camel_mime_filter_windows_init (CamelObject *o); +static void camel_mime_filter_windows_finalize (CamelObject *o); + + +static CamelMimeFilterClass *parent_class = NULL; + + +CamelType +camel_mime_filter_windows_get_type (void) +{ + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register (camel_mime_filter_get_type (), + "CamelMimeFilterWindows", + sizeof (CamelMimeFilterWindows), + sizeof (CamelMimeFilterWindowsClass), + (CamelObjectClassInitFunc) camel_mime_filter_windows_class_init, + NULL, + (CamelObjectInitFunc) camel_mime_filter_windows_init, + (CamelObjectFinalizeFunc) camel_mime_filter_windows_finalize); + } + + return type; +} + +static void +camel_mime_filter_windows_finalize (CamelObject *o) +{ + CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o; + + g_free (windows->claimed_charset); +} + +static void +camel_mime_filter_windows_init (CamelObject *o) +{ + CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o; + + windows->is_windows = FALSE; + windows->claimed_charset = NULL; +} + +static void +filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace, + char **out, size_t *outlen, size_t *outprespace) +{ + CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter; + register unsigned char *inptr; + unsigned char *inend; + + if (!windows->is_windows) { + inptr = (unsigned char *) in; + inend = inptr + len; + + while (inptr < inend) { + register unsigned char c = *inptr++; + + if (c >= 128 && c <= 159) { + g_warning ("Encountered Windows charset parading as %s", + windows->claimed_charset); + windows->is_windows = TRUE; + break; + } + } + } + + *out = in; + *outlen = len; + *outprespace = prespace; +} + +static void +filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace, + char **out, size_t *outlen, size_t *outprespace) +{ + filter_filter (filter, in, len, prespace, out, outlen, outprespace); +} + +static void +filter_reset (CamelMimeFilter *filter) +{ + CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter; + + windows->is_windows = FALSE; +} + +static void +camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass) +{ + CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass; + + parent_class = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ())); + + filter_class->reset = filter_reset; + filter_class->filter = filter_filter; + filter_class->complete = filter_complete; +} + + +/** + * camel_mime_filter_windows_new: + * @claimed_charset: + * + * Creates a new CamelMimeFilterWindows object. + * + * Returns a new CamelMimeFilter object. + **/ +CamelMimeFilter * +camel_mime_filter_windows_new (const char *claimed_charset) +{ + CamelMimeFilterWindows *new; + + g_return_val_if_fail (claimed_charset != NULL, NULL); + + new = CAMEL_MIME_FILTER_WINDOWS (camel_object_new (camel_mime_filter_windows_get_type ())); + + new->claimed_charset = g_strdup (claimed_charset); + + return CAMEL_MIME_FILTER (new); +} + + +gboolean +camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter) +{ + g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE); + + return filter->is_windows; +} + + +const char * +camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter) +{ + g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL); + + if (filter->is_windows) + return camel_charset_iso_to_windows (filter->claimed_charset); + else + return filter->claimed_charset; +} diff --git a/camel/camel-mime-filter-windows.h b/camel/camel-mime-filter-windows.h new file mode 100644 index 0000000000..d2dac4fe92 --- /dev/null +++ b/camel/camel-mime-filter-windows.h @@ -0,0 +1,65 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2002 Ximian, Inc. (www.ximian.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef __CAMEL_MIME_FILTER_WINDOWS_H__ +#define __CAMEL_MIME_FILTER_WINDOWS_H__ + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +#include <camel/camel-mime-filter.h> + +#define CAMEL_MIME_FILTER_WINDOWS(obj) CAMEL_CHECK_CAST (obj, camel_mime_filter_windows_get_type (), CamelMimeFilterWindows) +#define CAMEL_MIME_FILTER_WINDOWS_CLASS(klass) CAMEL_CHECK_CLASS_CAST (klass, camel_mime_filter_windows_get_type (), CamelMimeFilterWindowsClass) +#define CAMEL_IS_MIME_FILTER_WINDOWS(obj) CAMEL_CHECK_TYPE (obj, camel_mime_filter_windows_get_type ()) + +typedef struct _CamelMimeFilterWindowsClass CamelMimeFilterWindowsClass; +typedef struct _CamelMimeFilterWindows CamelMimeFilterWindows; + +struct _CamelMimeFilterWindows { + CamelMimeFilter parent; + + gboolean is_windows; + char *claimed_charset; +}; + +struct _CamelMimeFilterWindowsClass { + CamelMimeFilterClass parent_class; + +}; + + +CamelType camel_mime_filter_windows_get_type (void); + +CamelMimeFilter *camel_mime_filter_windows_new (const char *claimed_charset); + +gboolean camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter); +const char *camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __CAMEL_MIME_FILTER_WINDOWS_H__ */ diff --git a/camel/camel-mime-message.c b/camel/camel-mime-message.c index f9e9373a8f..ee5b7cac0e 100644 --- a/camel/camel-mime-message.c +++ b/camel/camel-mime-message.c @@ -122,8 +122,6 @@ camel_mime_message_init (gpointer object, gpointer klass) CamelMimeMessage *mime_message = (CamelMimeMessage *)object; int i; - camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (object), "message/rfc822"); - mime_message->recipients = g_hash_table_new (camel_strcase_hash, camel_strcase_equal); for (i=0;recipient_names[i];i++) { g_hash_table_insert(mime_message->recipients, recipient_names[i], camel_internet_address_new()); @@ -540,8 +538,8 @@ process_header (CamelMedium *medium, const char *header_name, const char *header break; case HEADER_SUBJECT: g_free (message->subject); - if (((CamelMimePart *) message)->content_type) { - charset = header_content_type_param (((CamelMimePart *) message)->content_type, "charset"); + if (((CamelDataWrapper *) message)->mime_type) { + charset = header_content_type_param (((CamelDataWrapper *) message)->mime_type, "charset"); charset = e_iconv_charset_name (charset); } else charset = NULL; @@ -673,17 +671,17 @@ camel_mime_message_has_8bit_parts (CamelMimeMessage *msg) static CamelMimePartEncodingType find_best_encoding (CamelMimePart *part, CamelBestencRequired required, CamelBestencEncoding enctype, char **charsetp) { - const char *charsetin = NULL; - char *charset = NULL; - CamelStream *null; - CamelStreamFilter *filter; CamelMimeFilterCharset *charenc = NULL; + CamelMimePartEncodingType encoding; CamelMimeFilterBestenc *bestenc; - int idb, idc = -1; - gboolean istext; unsigned int flags, callerflags; - CamelMimePartEncodingType encoding; CamelDataWrapper *content; + CamelStreamFilter *filter; + const char *charsetin = NULL; + char *charset = NULL; + CamelStream *null; + int idb, idc = -1; + gboolean istext; /* we use all these weird stream things so we can do it with streams, and not have to read the whole lot into memory - although i have a feeling @@ -699,7 +697,7 @@ find_best_encoding (CamelMimePart *part, CamelBestencRequired required, CamelBes return CAMEL_MIME_PART_ENCODING_DEFAULT; } - istext = header_content_type_is (part->content_type, "text", "*"); + istext = header_content_type_is (((CamelDataWrapper *) part)->mime_type, "text", "*"); if (istext) { flags = CAMEL_BESTENC_GET_CHARSET | CAMEL_BESTENC_GET_ENCODING; enctype |= CAMEL_BESTENC_TEXT; @@ -717,12 +715,10 @@ find_best_encoding (CamelMimePart *part, CamelBestencRequired required, CamelBes null = (CamelStream *)camel_stream_null_new (); filter = camel_stream_filter_new_with_stream (null); - /* if we're not looking for the best charset, then use the one we have */ - if (istext && (required & CAMEL_BESTENC_GET_CHARSET) == 0 - && (charsetin = header_content_type_param (part->content_type, "charset"))) { - /* if libunicode doesn't support it, we dont really have utf8 anyway, so - we dont need a converter */ - charenc = camel_mime_filter_charset_new_convert ("UTF-8", charsetin); + /* if we're looking for the best charset, then we need to convert to UTF-8 */ + if (istext && (required & CAMEL_BESTENC_GET_CHARSET) != 0 + && (charsetin = header_content_type_param (content->mime_type, "charset"))) { + charenc = camel_mime_filter_charset_new_convert (charsetin, "UTF-8"); if (charenc != NULL) idc = camel_stream_filter_add (filter, (CamelMimeFilter *)charenc); charsetin = NULL; @@ -731,35 +727,33 @@ find_best_encoding (CamelMimePart *part, CamelBestencRequired required, CamelBes bestenc = camel_mime_filter_bestenc_new (flags); idb = camel_stream_filter_add (filter, (CamelMimeFilter *)bestenc); d(printf("writing to checking stream\n")); - camel_data_wrapper_write_to_stream (content, (CamelStream *)filter); + camel_data_wrapper_decode_to_stream (content, (CamelStream *)filter); camel_stream_filter_remove (filter, idb); if (idc != -1) { camel_stream_filter_remove (filter, idc); - camel_object_unref ((CamelObject *)charenc); + camel_object_unref (charenc); charenc = NULL; } - if (istext) + if (istext && (required & CAMEL_BESTENC_GET_CHARSET) != 0) { charsetin = camel_mime_filter_bestenc_get_best_charset (bestenc); - - d(printf("charsetin = %s\n", charsetin ? charsetin : "(null)")); - - /* if we have US-ASCII, or we're not doing text, we dont need to bother with the rest */ - if (charsetin != NULL && (required & CAMEL_BESTENC_GET_CHARSET) != 0) { + d(printf("best charset = %s\n", charsetin ? charsetin : "(null)")); charset = g_strdup (charsetin); + charsetin = header_content_type_param (content->mime_type, "charset"); + } else { + charset = NULL; + } + + /* if we have US-ASCII, or we're not doing text, we dont need to bother with the rest */ + if (istext && charsetin && charset && (required & CAMEL_BESTENC_GET_CHARSET) != 0) { d(printf("have charset, trying conversion/etc\n")); - /* now the 'bestenc' can has told us what the best encoding is, we can use that to create + /* now that 'bestenc' has told us what the best encoding is, we can use that to create a charset conversion filter as well, and then re-add the bestenc to filter the result to find the best encoding to use as well */ - charenc = camel_mime_filter_charset_new_convert ("UTF-8", charset); - - /* eek, libunicode doesn't undertand this charset anyway, then the 'utf8' we - thought we had is really the native format, in which case, we just treat - it as binary data (and take the result we have so far) */ - + charenc = camel_mime_filter_charset_new_convert (charsetin, charset); if (charenc != NULL) { /* otherwise, try another pass, converting to the real charset */ @@ -773,15 +767,15 @@ find_best_encoding (CamelMimePart *part, CamelBestencRequired required, CamelBes /* and write it to the new stream */ camel_data_wrapper_write_to_stream (content, (CamelStream *)filter); - camel_object_unref ((CamelObject *)charenc); + camel_object_unref (charenc); } } encoding = camel_mime_filter_bestenc_get_best_encoding (bestenc, enctype); - camel_object_unref ((CamelObject *)filter); - camel_object_unref ((CamelObject *)bestenc); - camel_object_unref ((CamelObject *)null); + camel_object_unref (filter); + camel_object_unref (bestenc); + camel_object_unref (null); d(printf("done, best encoding = %d\n", encoding)); @@ -818,13 +812,13 @@ best_encoding (CamelMimeMessage *msg, CamelMimePart *part, void *datap) camel_mime_part_set_encoding (part, encoding); if ((data->required & CAMEL_BESTENC_GET_CHARSET) != 0) { - if (header_content_type_is (part->content_type, "text", "*")) { + if (header_content_type_is (((CamelDataWrapper *) part)->mime_type, "text", "*")) { char *newct; /* FIXME: ick, the part content_type interface needs fixing bigtime */ - header_content_type_set_param (part->content_type, "charset", + header_content_type_set_param (((CamelDataWrapper *) part)->mime_type, "charset", charset ? charset : "us-ascii"); - newct = header_content_type_format (part->content_type); + newct = header_content_type_format (((CamelDataWrapper *) part)->mime_type); if (newct) { d(printf("Setting content-type to %s\n", newct)); diff --git a/camel/camel-mime-parser.c b/camel/camel-mime-parser.c index 511aa121ca..decac00db7 100644 --- a/camel/camel-mime-parser.c +++ b/camel/camel-mime-parser.c @@ -1,5 +1,6 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2000 Ximian Inc. + * Copyright (C) 2000-2003 Ximian Inc. * * Authors: Michael Zucchi <notzed@ximian.com> * @@ -1128,13 +1129,13 @@ static struct _header_scan_stack * folder_boundary_check(struct _header_scan_state *s, const char *boundary, int *lastone) { struct _header_scan_stack *part; - int len = s->atleast; /* make sure we dont access past the buffer */ - + int len = s->inend - boundary; /* make sure we dont access past the buffer */ + h(printf("checking boundary marker upto %d bytes\n", len)); part = s->parts; while (part) { h(printf(" boundary: %s\n", part->boundary)); - h(printf(" against: '%.*s'\n", s->atleast, boundary)); + h(printf(" against: '%.*s'\n", part->boundarylen, boundary)); if (part->boundary && part->boundarylen <= len && memcmp(boundary, part->boundary, part->boundarylen)==0) { @@ -1384,7 +1385,10 @@ folder_scan_content(struct _header_scan_state *s, int *lastone, char **data, siz while ((len = folder_read(s))>0 && len >= s->atleast) { /* ensure we have at least enough room here */ inptr = s->inptr; - inend = s->inend-s->atleast+1; + if (s->eof) + inend = s->inend; + else + inend = s->inend-s->atleast+1; start = inptr; c(printf("inptr = %p, inend = %p\n", inptr, inend)); diff --git a/camel/camel-mime-part-utils.c b/camel/camel-mime-part-utils.c index 800f23372e..92769b3083 100644 --- a/camel/camel-mime-part-utils.c +++ b/camel/camel-mime-part-utils.c @@ -5,7 +5,7 @@ * Michael Zucchi <notzed@ximian.com> * Jeffrey Stedfast <fejj@ximian.com> * - * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com) + * Copyright 1999-2003 Ximian, Inc. (www.ximian.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -34,7 +34,6 @@ #include <gal/util/e-iconv.h> -#include "camel-string-utils.h" #include "camel-charset-map.h" #include "camel-mime-part-utils.h" #include "camel-mime-message.h" @@ -54,278 +53,19 @@ #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x)) #include <stdio.h>*/ -/* example: <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> */ - -static const char * -check_html_charset(char *buffer, int length) -{ - CamelHTMLParser *hp; - const char *charset = NULL; - camel_html_parser_t state; - struct _header_content_type *ct; - - /* if we need to first base64/qp decode, do this here, sigh */ - hp = camel_html_parser_new(); - camel_html_parser_set_data(hp, buffer, length, TRUE); - - do { - const char *data; - int len; - const char *val; - - state = camel_html_parser_step(hp, &data, &len); - - /* example: <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> */ - - switch(state) { - case CAMEL_HTML_PARSER_ELEMENT: - val = camel_html_parser_tag(hp); - d(printf("Got tag: %s\n", val)); - if (strcasecmp(val, "meta") == 0 - && (val = camel_html_parser_attr(hp, "http-equiv")) - && strcasecmp(val, "content-type") == 0 - && (val = camel_html_parser_attr(hp, "content")) - && (ct = header_content_type_decode(val))) { - charset = header_content_type_param(ct, "charset"); - charset = e_iconv_charset_name (charset); - header_content_type_unref(ct); - } - break; - default: - /* ignore everything else */ - break; - } - } while (charset == NULL && state != CAMEL_HTML_PARSER_EOF); - - camel_object_unref (hp); - - return charset; -} - -static GByteArray * -convert_buffer (GByteArray *in, const char *to, const char *from) -{ - size_t inleft, outleft, outlen, converted = 0; - GByteArray *out = NULL; - const char *inbuf; - char *outbuf; - iconv_t cd; - - if (in->len == 0) - return g_byte_array_new(); - - d(printf("converting buffer from %s to %s:\n", from, to)); - d(fwrite(in->data, 1, (int)in->len, stdout)); - d(printf("\n")); - - cd = e_iconv_open(to, from); - if (cd == (iconv_t) -1) { - g_warning ("Cannot convert from '%s' to '%s': %s", from, to, strerror (errno)); - return NULL; - } - - outlen = in->len * 2 + 16; - out = g_byte_array_new (); - g_byte_array_set_size (out, outlen); - - inbuf = in->data; - inleft = in->len; - - do { - outbuf = out->data + converted; - outleft = outlen - converted; - - converted = e_iconv (cd, &inbuf, &inleft, &outbuf, &outleft); - if (converted == (size_t) -1) { - if (errno != E2BIG && errno != EINVAL) - goto fail; - } - - /* - * E2BIG There is not sufficient room at *outbuf. - * - * We just need to grow our outbuffer and try again. - */ - - converted = outbuf - (char *)out->data; - if (errno == E2BIG) { - outlen += inleft * 2 + 16; - out = g_byte_array_set_size (out, outlen); - outbuf = out->data + converted; - } - - } while (errno == E2BIG && inleft > 0); - - /* - * EINVAL An incomplete multibyte sequence has been encoun - * tered in the input. - * - * We'll just have to ignore it... - */ - - /* flush the iconv conversion */ - e_iconv (cd, NULL, NULL, &outbuf, &outleft); - - /* now set the true length on the GByteArray */ - converted = outbuf - (char *)out->data; - g_byte_array_set_size (out, converted); - - d(printf("converted data:\n")); - d(fwrite(out->data, 1, (int)out->len, stdout)); - d(printf("\n")); - - e_iconv_close (cd); - - return out; - - fail: - g_warning ("Cannot convert from '%s' to '%s': %s", from, to, strerror (errno)); - - g_byte_array_free (out, TRUE); - - e_iconv_close (cd); - - return NULL; -} - -/* We don't really use the charset argument except for debugging... */ -static gboolean -broken_windows_charset (GByteArray *buffer, const char *charset) -{ - register unsigned char *inptr; - unsigned char *inend; - - inptr = buffer->data; - inend = inptr + buffer->len; - - while (inptr < inend) { - register unsigned char c = *inptr++; - - if (c >= 128 && c <= 159) { - g_warning ("Encountered Windows charset parading as %s", charset); - return TRUE; - } - } - - return FALSE; -} - -static gboolean -is_7bit (GByteArray *buffer) -{ - register unsigned int i; - - for (i = 0; i < buffer->len; i++) - if (buffer->data[i] > 127) - return FALSE; - - return TRUE; -} - -static const char *iso_charsets[] = { - "us-ascii", - "iso-8859-1", - "iso-8859-2", - "iso-8859-3", - "iso-8859-4", - "iso-8859-5", - "iso-8859-6", - "iso-8859-7", - "iso-8859-8", - "iso-8859-9", - "iso-8859-10", - "iso-8859-11", - "iso-8859-12", - "iso-8859-13", - "iso-8859-14", - "iso-8859-15", - "iso-8859-16" -}; - -#define NUM_ISO_CHARSETS (sizeof (iso_charsets) / sizeof (iso_charsets[0])) - -static const char * -canon_charset_name (const char *charset) -{ - const char *ptr; - char *endptr; - int iso; - - if (strncasecmp (charset, "iso", 3) != 0) - return charset; - - ptr = charset + 3; - if (*ptr == '-' || *ptr == '_') - ptr++; - - /* if it's not an iso-8859-# charset, we don't care about it */ - if (strncmp (ptr, "8859", 4) != 0) - return charset; - - ptr += 4; - if (*ptr == '-' || *ptr == '_') - ptr++; - - iso = strtoul (ptr, &endptr, 10); - if (endptr == ptr || *endptr != '\0') - return charset; - - if (iso >= NUM_ISO_CHARSETS) - return charset; - - return iso_charsets[iso]; -} - /* simple data wrapper */ static void simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw, CamelMimeParser *mp) { - CamelMimeFilter *fdec = NULL, *fcrlf = NULL; - CamelMimeFilterBasicType enctype = 0; - size_t len; - int decid = -1, crlfid = -1; - struct _header_content_type *ct; - const char *charset = NULL; char *encoding, *buf; GByteArray *buffer; CamelStream *mem; + size_t len; d(printf ("simple_data_wrapper_construct_from_parser()\n")); /* first, work out conversion, if any, required, we dont care about what we dont know about */ encoding = header_content_encoding_decode (camel_mime_parser_header (mp, "Content-Transfer-Encoding", NULL)); - if (encoding) { - if (!strcasecmp (encoding, "base64")) { - d(printf("Adding base64 decoder ...\n")); - enctype = CAMEL_MIME_FILTER_BASIC_BASE64_DEC; - } else if (!strcasecmp (encoding, "quoted-printable")) { - d(printf("Adding quoted-printable decoder ...\n")); - enctype = CAMEL_MIME_FILTER_BASIC_QP_DEC; - } else if (!strcasecmp (encoding, "x-uuencode")) { - d(printf("Adding uudecoder ...\n")); - enctype = CAMEL_MIME_FILTER_BASIC_UU_DEC; - } - g_free (encoding); - - if (enctype != 0) { - fdec = (CamelMimeFilter *)camel_mime_filter_basic_new_type(enctype); - decid = camel_mime_parser_filter_add (mp, fdec); - } - } - - /* If we're doing text, we also need to do CRLF->LF and may have to convert it to UTF8 as well. */ - ct = camel_mime_parser_content_type (mp); - if (header_content_type_is (ct, "text", "*")) { - charset = header_content_type_param (ct, "charset"); - charset = e_iconv_charset_name (charset); - - if (fdec) { - d(printf ("Adding CRLF conversion filter\n")); - fcrlf = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE, - CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY); - crlfid = camel_mime_parser_filter_add (mp, fcrlf); - } - } /* read in the entire content */ buffer = g_byte_array_new (); @@ -334,86 +74,16 @@ simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw, CamelMimeParser g_byte_array_append (buffer, buf, len); } - /* check for broken Outlook/Web mailers that like to send html marked as text/plain */ - if (header_content_type_is (ct, "text", "plain")) { - register const unsigned char *inptr; - const unsigned char *inend; - - inptr = buffer->data; - inend = inptr + buffer->len; - - while (inptr < inend && isspace ((int) *inptr)) - inptr++; - - if (((inend-inptr) > 5 && g_ascii_strncasecmp(inptr, "<html", 5) == 0) - || ((inend-inptr) > 9 && g_ascii_strncasecmp(inptr, "<!doctype", 9) == 0)) { - /* re-tag as text/html */ - g_free (ct->subtype); - ct->subtype = g_strdup ("html"); - } - } - - /* Possible Lame Mailer Alert... check the META tags for a charset */ - if (!charset && header_content_type_is (ct, "text", "html")) { - if ((charset = check_html_charset (buffer->data, buffer->len))) - header_content_type_set_param (ct, "charset", charset); - } - - /* if we need to do charset conversion, see if we can/it works/etc */ - if (charset && !(strcasecmp (charset, "us-ascii") == 0 - || strcasecmp (charset, "utf-8") == 0 - || strncasecmp (charset, "x-", 2) == 0)) { - GByteArray *out; - - /* You often see Microsoft Windows users announcing their texts - * as being in ISO-8859-1 even when in fact they contain funny - * characters from the Windows-CP1252 superset. - */ - charset = canon_charset_name (charset); - if (!strncasecmp (charset, "iso-8859", 8)) { - /* check for Windows-specific chars... */ - if (broken_windows_charset (buffer, charset)) - charset = camel_charset_iso_to_windows (charset); - } - - out = convert_buffer (buffer, "UTF-8", charset); - if (out) { - /* converted ok, use this data instead */ - g_byte_array_free(buffer, TRUE); - dw->rawtext = FALSE; - buffer = out; - } else { - /* else failed to convert, leave as raw? */ - g_warning("Storing text as raw, unknown charset '%s' or invalid format", charset); - dw->rawtext = TRUE; - } - } else if (header_content_type_is (ct, "text", "*")) { - if (charset == NULL || !strcasecmp (charset, "us-ascii")) { - /* check that it's 7bit */ - dw->rawtext = !is_7bit (buffer); - } else if (!strncasecmp (charset, "x-", 2)) { - /* we're not even going to bother trying to convert, so set the - rawtext bit to TRUE and let the mailer deal with it. */ - dw->rawtext = TRUE; - } else if (!strcasecmp (charset, "utf-8") && buffer->len) { - /* check that it is valid utf8 */ - dw->rawtext = !g_utf8_validate (buffer->data, buffer->len, NULL); - } - } - d(printf("message part kept in memory!\n")); - mem = camel_stream_mem_new_with_byte_array(buffer); - camel_data_wrapper_construct_from_stream(dw, mem); - camel_object_unref((CamelObject *)mem); - - camel_mime_parser_filter_remove(mp, decid); - camel_mime_parser_filter_remove(mp, crlfid); + mem = camel_stream_mem_new_with_byte_array (buffer); + camel_data_wrapper_construct_from_stream (dw, mem); + camel_object_unref (mem); - if (fdec) - camel_object_unref((CamelObject *)fdec); - if (fcrlf) - camel_object_unref((CamelObject *)fcrlf); + if (encoding) { + dw->encoding = camel_mime_part_encoding_from_string (encoding); + g_free (encoding); + } } /* This replaces the data wrapper repository ... and/or could be replaced by it? */ @@ -424,7 +94,7 @@ camel_mime_part_construct_content_from_parser (CamelMimePart *dw, CamelMimeParse CamelContentType *ct; ct = camel_mime_parser_content_type (mp); - + switch (camel_mime_parser_state (mp)) { case HSCAN_HEADER: d(printf("Creating body part\n")); @@ -457,19 +127,11 @@ camel_mime_part_construct_content_from_parser (CamelMimePart *dw, CamelMimeParse default: g_warning("Invalid state encountered???: %d", camel_mime_parser_state (mp)); } + if (content) { /* would you believe you have to set this BEFORE you set the content object??? oh my god !!!! */ camel_data_wrapper_set_mime_type_field (content, camel_mime_part_get_content_type (dw)); camel_medium_set_content_object ((CamelMedium *)dw, content); - - /* Note: we don't set ct as the content-object's mime-type above because - * camel_medium_set_content_object() may re-write the Content-Type header - * (see CamelMimePart::set_content_object) if we did that (which is a Bad Thing). - * However, if we set it *afterward*, we can still use any special auto-detections - * that we found in simple_data_wrapper_construct_from_parser(). This is important - * later when we go to render the MIME parts in mail-format.c */ - camel_data_wrapper_set_mime_type_field (content, ct); - camel_object_unref (content); } } diff --git a/camel/camel-mime-part.c b/camel/camel-mime-part.c index 1c85efb3e4..a31c179d99 100644 --- a/camel/camel-mime-part.c +++ b/camel/camel-mime-part.c @@ -72,10 +72,10 @@ static CamelMediumClass *parent_class=NULL; #define CMD_CLASS(so) CAMEL_MEDIUM_CLASS (CAMEL_OBJECT_GET_CLASS(so)) /* from CamelDataWrapper */ -static ssize_t write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream); -static int construct_from_stream (CamelDataWrapper *dw, CamelStream *s); +static ssize_t write_to_stream (CamelDataWrapper *dw, CamelStream *stream); +static int construct_from_stream (CamelDataWrapper *dw, CamelStream *stream); -/* from CamelMedia */ +/* from CamelMedium */ static void add_header (CamelMedium *medium, const char *header_name, const void *header_value); static void set_header (CamelMedium *medium, const char *header_name, const void *header_value); static void remove_header (CamelMedium *medium, const char *header_name); @@ -86,10 +86,10 @@ static void free_headers (CamelMedium *medium, GAr static void set_content_object (CamelMedium *medium, CamelDataWrapper *content); /* from camel mime parser */ -static int construct_from_parser (CamelMimePart *, CamelMimeParser *); +static int construct_from_parser (CamelMimePart *mime_part, CamelMimeParser *mp); /* forward references */ -static void set_disposition (CamelMimePart *mime_part, const gchar *disposition); +static void set_disposition (CamelMimePart *mime_part, const char *disposition); /* format output of headers */ static int write_references(CamelStream *stream, struct _header_raw *h); @@ -144,24 +144,27 @@ camel_mime_part_class_init (CamelMimePartClass *camel_mime_part_class) camel_medium_class->get_headers = get_headers; camel_medium_class->free_headers = free_headers; camel_medium_class->set_content_object = set_content_object; - + camel_data_wrapper_class->write_to_stream = write_to_stream; camel_data_wrapper_class->construct_from_stream= construct_from_stream; } static void -camel_mime_part_init (gpointer object, gpointer klass) +camel_mime_part_init (gpointer object, gpointer klass) { - CamelMimePart *camel_mime_part = CAMEL_MIME_PART (object); + CamelMimePart *mime_part = CAMEL_MIME_PART (object); - camel_mime_part->content_type = header_content_type_new ("text", "plain"); - camel_mime_part->description = NULL; - camel_mime_part->disposition = NULL; - camel_mime_part->content_id = NULL; - camel_mime_part->content_MD5 = NULL; - camel_mime_part->content_location = NULL; - camel_mime_part->content_languages = NULL; - camel_mime_part->encoding = CAMEL_MIME_PART_ENCODING_DEFAULT; + if (((CamelDataWrapper *) mime_part)->mime_type) + header_content_type_unref (((CamelDataWrapper *) mime_part)->mime_type); + ((CamelDataWrapper *) mime_part)->mime_type = header_content_type_new ("text", "plain"); + + mime_part->description = NULL; + mime_part->disposition = NULL; + mime_part->content_id = NULL; + mime_part->content_MD5 = NULL; + mime_part->content_location = NULL; + mime_part->content_languages = NULL; + mime_part->encoding = CAMEL_MIME_PART_ENCODING_DEFAULT; } @@ -177,9 +180,6 @@ camel_mime_part_finalize (CamelObject *object) camel_string_list_free (mime_part->content_languages); header_disposition_unref(mime_part->disposition); - if (mime_part->content_type) - header_content_type_unref (mime_part->content_type); - header_raw_clear(&mime_part->headers); } @@ -188,19 +188,20 @@ camel_mime_part_finalize (CamelObject *object) CamelType camel_mime_part_get_type (void) { - static CamelType camel_mime_part_type = CAMEL_INVALID_TYPE; + static CamelType type = CAMEL_INVALID_TYPE; - if (camel_mime_part_type == CAMEL_INVALID_TYPE) { - camel_mime_part_type = camel_type_register (CAMEL_MEDIUM_TYPE, "CamelMimePart", - sizeof (CamelMimePart), - sizeof (CamelMimePartClass), - (CamelObjectClassInitFunc) camel_mime_part_class_init, - NULL, - (CamelObjectInitFunc) camel_mime_part_init, - (CamelObjectFinalizeFunc) camel_mime_part_finalize); + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register (CAMEL_MEDIUM_TYPE, + "CamelMimePart", + sizeof (CamelMimePart), + sizeof (CamelMimePartClass), + (CamelObjectClassInitFunc) camel_mime_part_class_init, + NULL, + (CamelObjectInitFunc) camel_mime_part_init, + (CamelObjectFinalizeFunc) camel_mime_part_finalize); } - return camel_mime_part_type; + return type; } @@ -222,8 +223,8 @@ process_header(CamelMedium *medium, const char *header_name, const char *header_ switch (header_type) { case HEADER_DESCRIPTION: /* raw header->utf8 conversion */ g_free (mime_part->description); - if (mime_part->content_type) { - charset = header_content_type_param (mime_part->content_type, "charset"); + if (((CamelDataWrapper *) mime_part)->mime_type) { + charset = header_content_type_param (((CamelDataWrapper *) mime_part)->mime_type, "charset"); charset = e_iconv_charset_name (charset); } else charset = NULL; @@ -237,6 +238,7 @@ process_header(CamelMedium *medium, const char *header_name, const char *header_ mime_part->content_id = header_contentid_decode (header_value); break; case HEADER_ENCODING: + /* FIXME: ignore this if we are a multipart or a message/rfc822 part */ text = header_token_decode (header_value); mime_part->encoding = camel_mime_part_encoding_from_string (text); g_free (text); @@ -250,9 +252,9 @@ process_header(CamelMedium *medium, const char *header_name, const char *header_ mime_part->content_location = header_location_decode (header_value); break; case HEADER_CONTENT_TYPE: - if (mime_part->content_type) - header_content_type_unref (mime_part->content_type); - mime_part->content_type = header_content_type_decode (header_value); + if (((CamelDataWrapper *) mime_part)->mime_type) + header_content_type_unref (((CamelDataWrapper *) mime_part)->mime_type); + ((CamelDataWrapper *) mime_part)->mime_type = header_content_type_decode (header_value); break; default: return FALSE; @@ -328,7 +330,7 @@ free_headers (CamelMedium *medium, GArray *gheaders) /* **** Content-Description */ void -camel_mime_part_set_description (CamelMimePart *mime_part, const gchar *description) +camel_mime_part_set_description (CamelMimePart *mime_part, const char *description) { char *text = header_encode_string (description); @@ -337,7 +339,7 @@ camel_mime_part_set_description (CamelMimePart *mime_part, const gchar *descript g_free (text); } -const gchar * +const char * camel_mime_part_get_description (CamelMimePart *mime_part) { return mime_part->description; @@ -346,7 +348,7 @@ camel_mime_part_get_description (CamelMimePart *mime_part) /* **** Content-Disposition */ static void -set_disposition (CamelMimePart *mime_part, const gchar *disposition) +set_disposition (CamelMimePart *mime_part, const char *disposition) { header_disposition_unref(mime_part->disposition); if (disposition) @@ -357,7 +359,7 @@ set_disposition (CamelMimePart *mime_part, const gchar *disposition) void -camel_mime_part_set_disposition (CamelMimePart *mime_part, const gchar *disposition) +camel_mime_part_set_disposition (CamelMimePart *mime_part, const char *disposition) { char *text; @@ -377,7 +379,7 @@ camel_mime_part_set_disposition (CamelMimePart *mime_part, const gchar *disposit g_free(text); } -const gchar * +const char * camel_mime_part_get_disposition (CamelMimePart *mime_part) { if (mime_part->disposition) @@ -390,7 +392,7 @@ camel_mime_part_get_disposition (CamelMimePart *mime_part) /* **** Content-Disposition: filename="xxx" */ void -camel_mime_part_set_filename (CamelMimePart *mime_part, const gchar *filename) +camel_mime_part_set_filename (CamelMimePart *mime_part, const char *filename) { char *str; @@ -404,22 +406,22 @@ camel_mime_part_set_filename (CamelMimePart *mime_part, const gchar *filename) "Content-Disposition", str); g_free(str); - header_content_type_set_param (mime_part->content_type, "name", filename); - str = header_content_type_format (mime_part->content_type); + header_content_type_set_param (((CamelDataWrapper *) mime_part)->mime_type, "name", filename); + str = header_content_type_format (((CamelDataWrapper *) mime_part)->mime_type); camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-Type", str); g_free (str); } -const gchar * +const char * camel_mime_part_get_filename (CamelMimePart *mime_part) { if (mime_part->disposition) { - const gchar *name = header_param (mime_part->disposition->params, "filename"); + const char *name = header_param (mime_part->disposition->params, "filename"); if (name) return name; } - - return header_content_type_param (mime_part->content_type, "name"); + + return header_content_type_param (((CamelDataWrapper *) mime_part)->mime_type, "name"); } @@ -441,7 +443,7 @@ camel_mime_part_set_content_id (CamelMimePart *mime_part, const char *contentid) g_free (cid); } -const gchar * +const char * camel_mime_part_get_content_id (CamelMimePart *mime_part) { return mime_part->content_id; @@ -455,7 +457,7 @@ camel_mime_part_set_content_MD5 (CamelMimePart *mime_part, const char *md5) camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-MD5", md5); } -const gchar * +const char * camel_mime_part_get_content_MD5 (CamelMimePart *mime_part) { return mime_part->content_MD5; @@ -469,7 +471,7 @@ camel_mime_part_set_content_location (CamelMimePart *mime_part, const char *loca camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-Location", location); } -const gchar * +const char * camel_mime_part_get_content_location (CamelMimePart *mime_part) { return mime_part->content_location; @@ -519,7 +521,7 @@ camel_mime_part_get_content_languages (CamelMimePart *mime_part) /* **** Content-Type: */ void -camel_mime_part_set_content_type (CamelMimePart *mime_part, const gchar *content_type) +camel_mime_part_set_content_type (CamelMimePart *mime_part, const char *content_type) { camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-Type", content_type); @@ -528,39 +530,36 @@ camel_mime_part_set_content_type (CamelMimePart *mime_part, const gchar *content CamelContentType * camel_mime_part_get_content_type (CamelMimePart *mime_part) { - return mime_part->content_type; + return ((CamelDataWrapper *) mime_part)->mime_type; } -/*********/ - - static void set_content_object (CamelMedium *medium, CamelDataWrapper *content) { - CamelMimePart *mime_part = CAMEL_MIME_PART (medium); - CamelContentType *object_content_type; - + CamelDataWrapper *mime_part = CAMEL_DATA_WRAPPER (medium); + CamelContentType *content_type; + parent_class->set_content_object (medium, content); - - object_content_type = camel_data_wrapper_get_mime_type_field (content); - if (mime_part->content_type != object_content_type) { + + content_type = camel_data_wrapper_get_mime_type_field (content); + if (mime_part->mime_type != content_type) { char *txt; - - txt = header_content_type_format (object_content_type); + + txt = header_content_type_format (content_type); camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-Type", txt); - g_free(txt); + g_free (txt); } } /**********************************************************************/ -static int +static ssize_t write_references(CamelStream *stream, struct _header_raw *h) { - int len, out, total; + ssize_t len, out, total; char *v, *ids, *ide; - + /* this is only approximate, based on the next >, this way it retains any content from the original which may not be properly formatted, etc. It also doesn't handle the case where an individual messageid is too long, however thats a bad mail to @@ -599,7 +598,7 @@ write_references(CamelStream *stream, struct _header_raw *h) #if 0 /* not needed - yet - handled by default case */ -static int +static ssize_t write_fold(CamelStream *stream, struct _header_raw *h) { char *val; @@ -613,7 +612,7 @@ write_fold(CamelStream *stream, struct _header_raw *h) } #endif -static int +static ssize_t write_raw(CamelStream *stream, struct _header_raw *h) { char *val = h->value; @@ -622,10 +621,10 @@ write_raw(CamelStream *stream, struct _header_raw *h) } static ssize_t -write_to_stream(CamelDataWrapper *data_wrapper, CamelStream *stream) +write_to_stream (CamelDataWrapper *dw, CamelStream *stream) { - CamelMimePart *mp = CAMEL_MIME_PART(data_wrapper); - CamelMedium *medium = CAMEL_MEDIUM(data_wrapper); + CamelMimePart *mp = CAMEL_MIME_PART (dw); + CamelMedium *medium = CAMEL_MEDIUM (dw); CamelStream *ostream = stream; CamelDataWrapper *content; ssize_t total = 0; @@ -639,7 +638,7 @@ write_to_stream(CamelDataWrapper *data_wrapper, CamelStream *stream) if (mp->headers) { struct _header_raw *h = mp->headers; char *val; - int (*writefn)(CamelStream *stream, struct _header_raw *); + ssize_t (*writefn)(CamelStream *stream, struct _header_raw *); /* fold/write the headers. But dont fold headers that are already formatted (e.g. ones with parameter-lists, that we know about, and have created) */ @@ -669,80 +668,92 @@ write_to_stream(CamelDataWrapper *data_wrapper, CamelStream *stream) content = camel_medium_get_content_object(medium); if (content) { - /* I dont really like this here, but i dont know where else it might go ... */ -#define CAN_THIS_GO_ELSEWHERE -#ifdef CAN_THIS_GO_ELSEWHERE CamelMimeFilter *filter = NULL; CamelStreamFilter *filter_stream = NULL; CamelMimeFilter *charenc = NULL; + const char *content_charset = NULL; + const char *part_charset = NULL; + gboolean reencode = FALSE; const char *filename; - const char *charset; - switch (mp->encoding) { - case CAMEL_MIME_PART_ENCODING_BASE64: - filter = (CamelMimeFilter *)camel_mime_filter_basic_new_type(CAMEL_MIME_FILTER_BASIC_BASE64_ENC); - break; - case CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE: - filter = (CamelMimeFilter *)camel_mime_filter_basic_new_type(CAMEL_MIME_FILTER_BASIC_QP_ENC); - break; - case CAMEL_MIME_PART_ENCODING_UUENCODE: - filename = camel_mime_part_get_filename (mp); - count = camel_stream_printf (ostream, "begin 644 %s\n", filename ? filename : "untitled"); - if (count == -1) - return -1; - total += count; - filter = (CamelMimeFilter *)camel_mime_filter_basic_new_type(CAMEL_MIME_FILTER_BASIC_UU_ENC); - break; - default: - break; + if (header_content_type_is (dw->mime_type, "text", "*")) { + content_charset = header_content_type_param (content->mime_type, "charset"); + part_charset = header_content_type_param (dw->mime_type, "charset"); + + if (content_charset && part_charset) { + content_charset = e_iconv_charset_name (content_charset); + part_charset = e_iconv_charset_name (part_charset); + } } - if (!content->rawtext && header_content_type_is(mp->content_type, "text", "*")) { - charset = header_content_type_param(mp->content_type, "charset"); - if (charset && !(!strcasecmp(charset, "us-ascii") || !strcasecmp(charset, "utf-8"))) { - charenc = (CamelMimeFilter *)camel_mime_filter_charset_new_convert("UTF-8", charset); - } + if (mp->encoding != content->encoding) { + switch (mp->encoding) { + case CAMEL_MIME_PART_ENCODING_BASE64: + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_BASE64_ENC); + break; + case CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE: + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_QP_ENC); + break; + case CAMEL_MIME_PART_ENCODING_UUENCODE: + filename = camel_mime_part_get_filename (mp); + count = camel_stream_printf (ostream, "begin 644 %s\n", filename ? filename : "untitled"); + if (count == -1) + return -1; + total += count; + filter = (CamelMimeFilter *) camel_mime_filter_basic_new_type (CAMEL_MIME_FILTER_BASIC_UU_ENC); + break; + default: + break; + } } + if (content_charset && part_charset && part_charset != content_charset) + charenc = (CamelMimeFilter *) camel_mime_filter_charset_new_convert (content_charset, part_charset); + if (filter || charenc) { filter_stream = camel_stream_filter_new_with_stream(stream); /* if we have a character encoder, add that always */ if (charenc) { camel_stream_filter_add(filter_stream, charenc); - camel_object_unref((CamelObject *)charenc); + camel_object_unref (charenc); } /* we only re-do crlf on encoded blocks */ - if (filter && header_content_type_is(mp->content_type, "text", "*")) { + if (filter && header_content_type_is (dw->mime_type, "text", "*")) { CamelMimeFilter *crlf = camel_mime_filter_crlf_new(CAMEL_MIME_FILTER_CRLF_ENCODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY); camel_stream_filter_add(filter_stream, crlf); - camel_object_unref((CamelObject *)crlf); + camel_object_unref (crlf); } if (filter) { camel_stream_filter_add(filter_stream, filter); - camel_object_unref((CamelObject *)filter); + camel_object_unref (filter); } stream = (CamelStream *)filter_stream; + + reencode = TRUE; } - -#endif - count = camel_data_wrapper_write_to_stream(content, stream); + if (reencode) + count = camel_data_wrapper_decode_to_stream (content, stream); + else + count = camel_data_wrapper_write_to_stream (content, stream); if (filter_stream) { - camel_stream_flush((CamelStream *)filter_stream); - camel_object_unref((CamelObject *)filter_stream); + camel_stream_flush (stream); + camel_object_unref (filter_stream); } + if (count == -1) return -1; + total += count; - if (mp->encoding == CAMEL_MIME_PART_ENCODING_UUENCODE) { + if (reencode && mp->encoding == CAMEL_MIME_PART_ENCODING_UUENCODE) { count = camel_stream_write (ostream, "end\n", 4); if (count == -1) return -1; @@ -757,22 +768,23 @@ write_to_stream(CamelDataWrapper *data_wrapper, CamelStream *stream) /* mime_part */ static int -construct_from_parser(CamelMimePart *dw, CamelMimeParser *mp) +construct_from_parser (CamelMimePart *mime_part, CamelMimeParser *mp) { + CamelDataWrapper *dw = (CamelDataWrapper *) mime_part; struct _header_raw *headers; const char *content; char *buf; size_t len; int err; - + d(printf("mime_part::construct_from_parser()\n")); - + switch (camel_mime_parser_step(mp, &buf, &len)) { case HSCAN_MESSAGE: /* set the default type of a message always */ - if (dw->content_type) - header_content_type_unref (dw->content_type); - dw->content_type = header_content_type_decode ("message/rfc822"); + if (dw->mime_type) + header_content_type_unref (dw->mime_type); + dw->mime_type = header_content_type_decode ("message/rfc822"); case HSCAN_HEADER: case HSCAN_MULTIPART: /* we have the headers, build them into 'us' */ @@ -792,7 +804,7 @@ construct_from_parser(CamelMimePart *dw, CamelMimeParser *mp) headers = headers->next; } - camel_mime_part_construct_content_from_parser(dw, mp); + camel_mime_part_construct_content_from_parser (mime_part, mp); break; default: g_warning("Invalid state encountered???: %d", camel_mime_parser_state(mp)); @@ -864,7 +876,7 @@ camel_mime_part_encoding_to_string (CamelMimePartEncodingType encoding) /* FIXME I am not sure this is the correct way to do this. */ CamelMimePartEncodingType -camel_mime_part_encoding_from_string (const gchar *string) +camel_mime_part_encoding_from_string (const char *string) { int i; diff --git a/camel/camel-mime-part.h b/camel/camel-mime-part.h index c925643f24..ac7fb403f4 100644 --- a/camel/camel-mime-part.h +++ b/camel/camel-mime-part.h @@ -31,7 +31,7 @@ #ifdef __cplusplus extern "C" { #pragma } -#endif /* __cplusplus }*/ +#endif /* __cplusplus */ #include <camel/camel-medium.h> #include <camel/camel-mime-utils.h> @@ -42,26 +42,10 @@ extern "C" { #define CAMEL_MIME_PART_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_MIME_PART_TYPE, CamelMimePartClass)) #define CAMEL_IS_MIME_PART(o) (CAMEL_CHECK_TYPE((o), CAMEL_MIME_PART_TYPE)) -/* note, if you change this, make sure you change the 'encodings' array in camel-mime-part.c */ -enum _CamelMimePartEncodingType { - CAMEL_MIME_PART_ENCODING_DEFAULT, - CAMEL_MIME_PART_ENCODING_7BIT, - CAMEL_MIME_PART_ENCODING_8BIT, - CAMEL_MIME_PART_ENCODING_BASE64, - CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE, - CAMEL_MIME_PART_ENCODING_BINARY, - CAMEL_MIME_PART_ENCODING_UUENCODE, - CAMEL_MIME_PART_NUM_ENCODINGS -}; -typedef enum _CamelMimePartEncodingType CamelMimePartEncodingType; - - /* Do not change these values directly, you would regret it one day */ -struct _CamelMimePart -{ +struct _CamelMimePart { CamelMedium parent_object; - - CamelContentType *content_type; + struct _header_raw *headers; /* mime headers */ /* All fields here are -** PRIVATE **- */ @@ -88,23 +72,23 @@ CamelType camel_mime_part_get_type (void); /* public methods */ CamelMimePart * camel_mime_part_new (void); -void camel_mime_part_set_description (CamelMimePart *mime_part, const gchar *description); -const gchar *camel_mime_part_get_description (CamelMimePart *mime_part); +void camel_mime_part_set_description (CamelMimePart *mime_part, const char *description); +const char *camel_mime_part_get_description (CamelMimePart *mime_part); -void camel_mime_part_set_disposition (CamelMimePart *mime_part, const gchar *disposition); -const gchar *camel_mime_part_get_disposition (CamelMimePart *mime_part); +void camel_mime_part_set_disposition (CamelMimePart *mime_part, const char *disposition); +const char *camel_mime_part_get_disposition (CamelMimePart *mime_part); -void camel_mime_part_set_filename (CamelMimePart *mime_part, const gchar *filename); -const gchar *camel_mime_part_get_filename (CamelMimePart *mime_part); +void camel_mime_part_set_filename (CamelMimePart *mime_part, const char *filename); +const char *camel_mime_part_get_filename (CamelMimePart *mime_part); void camel_mime_part_set_content_id (CamelMimePart *mime_part, const char *contentid); -const gchar *camel_mime_part_get_content_id (CamelMimePart *mime_part); +const char *camel_mime_part_get_content_id (CamelMimePart *mime_part); void camel_mime_part_set_content_MD5 (CamelMimePart *mime_part, const char *); -const gchar *camel_mime_part_get_content_MD5 (CamelMimePart *mime_part); +const char *camel_mime_part_get_content_MD5 (CamelMimePart *mime_part); void camel_mime_part_set_content_location (CamelMimePart *mime_part, const char *); -const gchar *camel_mime_part_get_content_location (CamelMimePart *mime_part); +const char *camel_mime_part_get_content_location (CamelMimePart *mime_part); void camel_mime_part_set_encoding (CamelMimePart *mime_part, CamelMimePartEncodingType type); CamelMimePartEncodingType camel_mime_part_get_encoding (CamelMimePart *mime_part); @@ -113,11 +97,11 @@ void camel_mime_part_set_content_languages (CamelMimePart *mime_part, GList * const GList *camel_mime_part_get_content_languages (CamelMimePart *mime_part); /* FIXME: what about content-type parameters? what about major/minor parts? */ -void camel_mime_part_set_content_type (CamelMimePart *mime_part, const gchar *content_type); +void camel_mime_part_set_content_type (CamelMimePart *mime_part, const char *content_type); CamelContentType *camel_mime_part_get_content_type (CamelMimePart *mime_part); -const gchar * camel_mime_part_encoding_to_string (CamelMimePartEncodingType encoding); -CamelMimePartEncodingType camel_mime_part_encoding_from_string (const gchar *string); +const char * camel_mime_part_encoding_to_string (CamelMimePartEncodingType encoding); +CamelMimePartEncodingType camel_mime_part_encoding_from_string (const char *string); /* construction */ int camel_mime_part_construct_from_parser (CamelMimePart *, CamelMimeParser *); diff --git a/camel/camel-mime-utils.h b/camel/camel-mime-utils.h index b949ffcf98..b58bcbea72 100644 --- a/camel/camel-mime-utils.h +++ b/camel/camel-mime-utils.h @@ -42,6 +42,18 @@ extern "C" { #define CAMEL_UUDECODE_STATE_END (1 << 17) #define CAMEL_UUDECODE_STATE_MASK (CAMEL_UUDECODE_STATE_BEGIN | CAMEL_UUDECODE_STATE_END) +/* note, if you change this, make sure you change the 'encodings' array in camel-mime-part.c */ +typedef enum _CamelMimePartEncodingType { + CAMEL_MIME_PART_ENCODING_DEFAULT, + CAMEL_MIME_PART_ENCODING_7BIT, + CAMEL_MIME_PART_ENCODING_8BIT, + CAMEL_MIME_PART_ENCODING_BASE64, + CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE, + CAMEL_MIME_PART_ENCODING_BINARY, + CAMEL_MIME_PART_ENCODING_UUENCODE, + CAMEL_MIME_PART_NUM_ENCODINGS +} CamelMimePartEncodingType; + /* a list of references for this message */ struct _header_references { struct _header_references *next; diff --git a/camel/camel-multipart.c b/camel/camel-multipart.c index 9c41050677..fcb500df93 100644 --- a/camel/camel-multipart.c +++ b/camel/camel-multipart.c @@ -549,9 +549,9 @@ construct_from_parser(CamelMultipart *multipart, struct _CamelMimeParser *mp) CamelMimePart *bodypart; char *buf; size_t len; - + g_assert(camel_mime_parser_state(mp) == HSCAN_MULTIPART); - + /* FIXME: we should use a came-mime-mutlipart, not jsut a camel-multipart, but who cares */ d(printf("Creating multi-part\n")); diff --git a/camel/providers/imap/camel-imap-folder.c b/camel/providers/imap/camel-imap-folder.c index 4a4fb85ecb..be3e29f16b 100644 --- a/camel/providers/imap/camel-imap-folder.c +++ b/camel/providers/imap/camel-imap-folder.c @@ -1732,7 +1732,7 @@ get_content (CamelImapFolder *imap_folder, const char *uid, body_mp = camel_multipart_signed_new (); /* need to set this so it grabs the boundary and other info about the signed type */ /* we assume that part->content_type is more accurate/full than ci->type */ - camel_data_wrapper_set_mime_type_field (CAMEL_DATA_WRAPPER (body_mp), part->content_type); + camel_data_wrapper_set_mime_type_field (CAMEL_DATA_WRAPPER (body_mp), CAMEL_DATA_WRAPPER (part)->mime_type); spec = g_alloca (strlen (part_spec) + 6); sprintf (spec, part_spec[0] ? "%s.TEXT" : "TEXT", part_spec); @@ -1761,7 +1761,7 @@ get_content (CamelImapFolder *imap_folder, const char *uid, /* need to set this so it grabs the boundary and other info about the multipart */ /* we assume that part->content_type is more accurate/full than ci->type */ - camel_data_wrapper_set_mime_type_field (CAMEL_DATA_WRAPPER (body_mp), part->content_type); + camel_data_wrapper_set_mime_type_field (CAMEL_DATA_WRAPPER (body_mp), CAMEL_DATA_WRAPPER (part)->mime_type); speclen = strlen (part_spec); child_spec = g_malloc (speclen + 17); /* dot + 10 + dot + MIME + nul */ diff --git a/camel/tests/lib/messages.c b/camel/tests/lib/messages.c index 21d77582ad..3cf1b5fd51 100644 --- a/camel/tests/lib/messages.c +++ b/camel/tests/lib/messages.c @@ -1,8 +1,11 @@ +#include <stdio.h> #include <string.h> +#include <ctype.h> #include "messages.h" #include "camel-test.h" +#include <camel/camel-multipart.h> #include <camel/camel-mime-message.h> #include <camel/camel-stream-fs.h> #include <camel/camel-stream-mem.h> @@ -120,6 +123,36 @@ test_message_read_file(const char *name) return msg2; } +static void +hexdump (const unsigned char *in, int inlen) +{ + const unsigned char *inptr = in, *start = inptr; + const unsigned char *inend = in + inlen; + int octets; + + while (inptr < inend) { + octets = 0; + while (inptr < inend && octets < 16) { + printf ("%.2X ", *inptr++); + octets++; + } + + while (octets < 16) { + printf (" "); + octets++; + } + + printf (" "); + + while (start < inptr) { + fputc (isprint ((int) *start) ? *start : '.', stdout); + start++; + } + + fputc ('\n', stdout); + } +} + int test_message_compare_content(CamelDataWrapper *dw, const char *text, int len) { @@ -131,8 +164,16 @@ test_message_compare_content(CamelDataWrapper *dw, const char *text, int len) return 0; content = (CamelStreamMem *)camel_stream_mem_new(); - camel_data_wrapper_write_to_stream(dw, (CamelStream *)content); - + camel_data_wrapper_decode_to_stream(dw, (CamelStream *)content); + + if (content->buffer->len != len) { + printf ("original text:\n"); + hexdump (text, len); + + printf ("new text:\n"); + hexdump (content->buffer->data, content->buffer->len); + } + check_msg(content->buffer->len == len, "buffer->len = %d, len = %d", content->buffer->len, len); check_msg(memcmp(content->buffer->data, text, content->buffer->len) == 0, "len = %d", len); @@ -142,6 +183,47 @@ test_message_compare_content(CamelDataWrapper *dw, const char *text, int len) } int +test_message_compare (CamelMimeMessage *msg) +{ + CamelMimeMessage *msg2; + CamelStreamMem *mem1, *mem2; + + mem1 = (CamelStreamMem *) camel_stream_mem_new (); + camel_data_wrapper_write_to_stream ((CamelDataWrapper *) msg, (CamelStream *) mem1); + camel_stream_reset ((CamelStream *) mem1); + + msg2 = camel_mime_message_new (); + camel_data_wrapper_construct_from_stream ((CamelDataWrapper *) msg2, (CamelStream *) mem1); + camel_stream_reset ((CamelStream *) mem1); + + mem2 = (CamelStreamMem *) camel_stream_mem_new (); + camel_data_wrapper_write_to_stream ((CamelDataWrapper *) msg2, (CamelStream *) mem2); + camel_stream_reset ((CamelStream *) mem2); + + camel_object_unref (msg2); + + if (mem1->buffer->len != mem2->buffer->len) { + CamelDataWrapper *content; + + printf ("mem1 stream:\n%.*s\n", mem1->buffer->len, mem1->buffer->data); + printf ("mem2 stream:\n%.*s\n\n", mem2->buffer->len, mem2->buffer->data); + + content = camel_medium_get_content_object ((CamelMedium *) msg); + } + + check_msg (mem1->buffer->len == mem2->buffer->len, + "mem1->buffer->len = %d, mem2->buffer->len = %d", + mem1->buffer->len, mem2->buffer->len); + + check_msg (memcmp (mem1->buffer->data, mem2->buffer->data, mem1->buffer->len) == 0, "msg/stream compare"); + + camel_object_unref (mem1); + camel_object_unref (mem2); + + return 0; +} + +int test_message_compare_header(CamelMimeMessage *m1, CamelMimeMessage *m2) { return 0; diff --git a/camel/tests/lib/messages.h b/camel/tests/lib/messages.h index 9cb5758826..cdac98f30a 100644 --- a/camel/tests/lib/messages.h +++ b/camel/tests/lib/messages.h @@ -10,3 +10,4 @@ void test_message_set_content_simple(CamelMimePart *part, int how, const char *t int test_message_write_file(CamelMimeMessage *msg, const char *name); CamelMimeMessage *test_message_read_file(const char *name); int test_message_compare_content(CamelDataWrapper *dw, const char *text, int len); +int test_message_compare (CamelMimeMessage *msg); diff --git a/camel/tests/message/Makefile.am b/camel/tests/message/Makefile.am index 47f0024901..32b4294aa2 100644 --- a/camel/tests/message/Makefile.am +++ b/camel/tests/message/Makefile.am @@ -19,6 +19,7 @@ LDADD = \ check_PROGRAMS = \ test1 \ test2 \ - test3 + test3 \ + test4 -TESTS = test1 test2 test3 +TESTS = test1 test2 test3 test4 diff --git a/camel/tests/message/test4.c b/camel/tests/message/test4.c new file mode 100644 index 0000000000..bc728a928f --- /dev/null +++ b/camel/tests/message/test4.c @@ -0,0 +1,126 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2003 Ximian, Inc. (www.ximian.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> + +#include "camel-test.h" +#include "messages.h" + +#include <camel/camel-multipart.h> +#include <camel/camel-mime-message.h> +#include <camel/camel-stream-fs.h> +#include <camel/camel-stream-mem.h> + + +#if 0 +static void +dump_mime_struct (CamelMimePart *mime_part, int depth) +{ + CamelDataWrapper *content; + char *mime_type; + int i = 0; + + while (i < depth) { + printf (" "); + i++; + } + + content = camel_medium_get_content_object ((CamelMedium *) mime_part); + + mime_type = camel_data_wrapper_get_mime_type (content); + printf ("Content-Type: %s\n", mime_type); + g_free (mime_type); + + if (CAMEL_IS_MULTIPART (content)) { + guint num, index = 0; + + num = camel_multipart_get_number ((CamelMultipart *) content); + while (index < num) { + mime_part = camel_multipart_get_part ((CamelMultipart *) content, index); + dump_mime_struct (mime_part, depth + 1); + index++; + } + } else if (CAMEL_IS_MIME_MESSAGE (content)) { + dump_mime_struct ((CamelMimePart *) content, depth + 1); + } +} +#endif + +int main (int argc, char **argv) +{ + struct dirent *dent; + DIR *dir; + int fd; + + camel_test_init (argc, argv); + + camel_test_start ("Message Test Suite"); + + if (!(dir = opendir ("../data/messages"))) + return 77; + + while ((dent = readdir (dir)) != NULL) { + CamelMimeMessage *message; + CamelStream *stream; + char *filename; + struct stat st; + + filename = g_strdup_printf ("../data/messages/%s", dent->d_name); + if (stat (filename, &st) == -1 || !S_ISREG (st.st_mode)) { + g_free (filename); + continue; + } + + if ((fd = open (filename, O_RDONLY)) == -1) { + g_free (filename); + continue; + } + + push ("testing message `%s`", filename); + g_free (filename); + + stream = camel_stream_fs_new_with_fd (fd); + message = camel_mime_message_new (); + camel_data_wrapper_construct_from_stream ((CamelDataWrapper *) message, stream); + camel_stream_reset (stream); + + /*dump_mime_struct ((CamelMimePart *) message, 0);*/ + test_message_compare (message); + + camel_object_unref (message); + camel_object_unref (stream); + + pull (); + } + + closedir (dir); + + camel_test_end (); + + return 0; +} |