/* gcc `pkg-config --cflags --libs camel-1.2` a.c -o a -g -O2 -Wall -fPIC -fno-strict-aliasing -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 */

#include <stdio.h>
#include <string.h>
#include <camel/camel.h>

const char *message = 
	"From zyx@no.where Wed Jul  7 15:16:03 2010\n"
	"Date: Wed, 07 Jul 2010 15:16:47 +0200 (CEST)\n"
	"To: zyx@no.where\n"
	"From: zyx@no-where\n"
	"Subject: Message\n"
	"Mime-Version: 1.0\n"
	"Message-Id: <50541.16533.40621-8971-2119775184-1278508607@no.where>\n"
	"Content-Type: text/plain; charset=\"iso-8859-2\"\n"
	"Content-Transfer-Encoding: 8bit\n"
	"\n"
	"123456789012345678901234567890123456789012345678901234567890\n"
	"123456789012345678901234567890123456789012345678901234567890\n"
	"123456789012345678901234567890123456789012345678901234567890\n"
	"123456789012345678901234567890123456789012345678901234567890\n"
	"123456789012345678901234567890123456789012345678901234567890\n"
	"\n";

int main(void)
{
	CamelMimeMessage *msg;
	CamelDataWrapper *content;
	CamelStream *mem;
	gchar *data;

	g_type_init ();
	g_thread_init (NULL);
	camel_object_get_type ();

	msg = camel_mime_message_new ();
	mem = camel_stream_mem_new_with_buffer (message, strlen (message));
	camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg), mem);
	camel_object_unref (mem);

	content = camel_medium_get_content_object (CAMEL_MEDIUM (msg));
	mem = camel_stream_mem_new ();
	camel_data_wrapper_decode_to_stream (content, mem);

	if (((CamelStreamMem *)mem)->buffer->len == 0)
 		data = NULL;
 	else
		data = g_strndup ((gchar *)((CamelStreamMem *)mem)->buffer->data, ((CamelStreamMem *)mem)->buffer->len);

 	camel_object_unref (mem);
 	camel_object_unref (msg);

	if (data) {
		printf ("Received decoded: '%s'\n", data);
		g_free (data);
	} else {
		printf ("Empty stream after decode...\n");
	}

	return 0;
}
