camel-mime-part.c 21.5 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
2 3 4
/* camelMimePart.c : Abstract class for a mime_part */

/* 
5 6
 * Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
 *	    Michael Zucchi <notzed@helixcode.com>
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
7
 *
8
 * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
25

26
#include <config.h>
27
#include <string.h>
28
#include "camel-mime-part.h"
29
#include <stdio.h>
30
#include "string-utils.h"
31
#include "hash-table-utils.h"
32
#include "camel-mime-part-utils.h"
33
#include <ctype.h>
34
#include "camel-mime-parser.h"
35
#include "camel-stream-mem.h"
36 37
#include "camel-stream-filter.h"
#include "camel-mime-filter-basic.h"
38
#include "camel-mime-filter-crlf.h"
39
#include "camel-mime-filter-charset.h"
40
#include "camel-exception.h"
41

42
#define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
43

Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
44 45 46 47 48 49
typedef enum {
	HEADER_UNKNOWN,
	HEADER_DESCRIPTION,
	HEADER_DISPOSITION,
	HEADER_CONTENT_ID,
	HEADER_ENCODING,
bertrand's avatar
bertrand committed
50
	HEADER_CONTENT_MD5,
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
51 52
	HEADER_CONTENT_LANGUAGES,
	HEADER_CONTENT_TYPE
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
53
} CamelHeaderType;
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
54 55


Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
56
static GHashTable *header_name_table;
57
static GHashTable *header_formatted_table;
58

bertrand's avatar
bertrand committed
59
static CamelMediumClass *parent_class=NULL;
60 61

/* Returns the class for a CamelMimePart */
Peter Williams's avatar
Peter Williams committed
62 63 64
#define CMP_CLASS(so) CAMEL_MIME_PART_CLASS (CAMEL_OBJECT_GET_CLASS(so))
#define CDW_CLASS(so) CAMEL_DATA_WRAPPER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
#define CMD_CLASS(so) CAMEL_MEDIUM_CLASS (CAMEL_OBJECT_GET_CLASS(so))
bertrand's avatar
bertrand committed
65 66

/* from CamelDataWrapper */
67
static int             write_to_stream                 (CamelDataWrapper *data_wrapper, CamelStream *stream);
68
static int	       construct_from_stream	       (CamelDataWrapper *dw, CamelStream *s);
bertrand's avatar
bertrand committed
69 70

/* from CamelMedia */ 
71 72
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);
73
static void            remove_header                   (CamelMedium *medium, const char *header_name);
74
static const void     *get_header                      (CamelMedium *medium, const char *header_name);
75

76
static void            set_content_object              (CamelMedium *medium, CamelDataWrapper *content);
77 78

/* from camel mime parser */
79
static int             construct_from_parser           (CamelMimePart *, CamelMimeParser *);
80

81 82
/* forward references */
static void set_disposition (CamelMimePart *mime_part, const gchar *disposition);
bertrand's avatar
bertrand committed
83

84

Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
85 86 87
/* loads in a hash table the set of header names we */
/* recognize and associate them with a unique enum  */
/* identifier (see CamelHeaderType above)           */
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
88
static void
89
init_header_name_table()
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
90
{
91
	header_name_table = g_hash_table_new (g_strcase_hash, g_strcase_equal);
92 93 94 95 96 97
	g_hash_table_insert (header_name_table, "Content-Description", (gpointer)HEADER_DESCRIPTION);
	g_hash_table_insert (header_name_table, "Content-Disposition", (gpointer)HEADER_DISPOSITION);
	g_hash_table_insert (header_name_table, "Content-id", (gpointer)HEADER_CONTENT_ID);
	g_hash_table_insert (header_name_table, "Content-Transfer-Encoding", (gpointer)HEADER_ENCODING);
	g_hash_table_insert (header_name_table, "Content-MD5", (gpointer)HEADER_CONTENT_MD5);
	g_hash_table_insert (header_name_table, "Content-Type", (gpointer)HEADER_CONTENT_TYPE);
98 99 100 101

	header_formatted_table = g_hash_table_new(g_strcase_hash, g_strcase_equal);
	g_hash_table_insert(header_formatted_table, "Content-Type", (void *)1);
	g_hash_table_insert(header_formatted_table, "Content-Disposition", (void *)1);
102 103 104 105
	g_hash_table_insert(header_formatted_table, "To", (void *)1);
	g_hash_table_insert(header_formatted_table, "From", (void *)1);
	g_hash_table_insert(header_formatted_table, "Cc", (void *)1);
	g_hash_table_insert(header_formatted_table, "Bcc", (void *)1);
106
	g_hash_table_insert(header_formatted_table, "Message-ID", (void *)1);
Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
107
}
108

109 110 111
static void
camel_mime_part_class_init (CamelMimePartClass *camel_mime_part_class)
{
bertrand's avatar
bertrand committed
112
	CamelMediumClass *camel_medium_class = CAMEL_MEDIUM_CLASS (camel_mime_part_class);
113
	CamelDataWrapperClass *camel_data_wrapper_class = CAMEL_DATA_WRAPPER_CLASS (camel_mime_part_class);
bertrand's avatar
bertrand committed
114

Peter Williams's avatar
Peter Williams committed
115
	parent_class = CAMEL_MEDIUM_CLASS (camel_type_get_global_classfuncs (camel_medium_get_type ()));
116
	init_header_name_table();
117 118

	camel_mime_part_class->construct_from_parser = construct_from_parser;
119
	
bertrand's avatar
bertrand committed
120
	/* virtual method overload */	
121 122
	camel_medium_class->add_header                = add_header;
	camel_medium_class->set_header                = set_header;
123
	camel_medium_class->get_header                = get_header;
124
	camel_medium_class->remove_header             = remove_header;
125
	camel_medium_class->set_content_object        = set_content_object;
bertrand's avatar
bertrand committed
126

127
	camel_data_wrapper_class->write_to_stream     = write_to_stream;
128
	camel_data_wrapper_class->construct_from_stream= construct_from_stream;
129 130
}

131 132 133 134
static void
camel_mime_part_init (gpointer   object,  gpointer   klass)
{
	CamelMimePart *camel_mime_part = CAMEL_MIME_PART (object);
135
	
136
	camel_mime_part->content_type         = header_content_type_new ("text", "plain");
137 138 139 140 141 142
	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_languages    = NULL;
	camel_mime_part->encoding             = CAMEL_MIME_PART_ENCODING_DEFAULT;
143
}
144 145


bertrand's avatar
bertrand committed
146
static void           
Peter Williams's avatar
Peter Williams committed
147
camel_mime_part_finalize (CamelObject *object)
bertrand's avatar
bertrand committed
148 149 150
{
	CamelMimePart *mime_part = CAMEL_MIME_PART (object);

151 152 153 154
	g_free (mime_part->description);
	g_free (mime_part->content_id);
	g_free (mime_part->content_MD5);
	string_list_free (mime_part->content_languages);
155
	header_disposition_unref(mime_part->disposition);
156
	
157
	if (mime_part->content_type)
158
		header_content_type_unref (mime_part->content_type);
159

160
	header_raw_clear(&mime_part->headers);
Peter Williams's avatar
Peter Williams committed
161 162
}

163

Peter Williams's avatar
Peter Williams committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

CamelType
camel_mime_part_get_type (void)
{
	static CamelType camel_mime_part_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);
	}
	
	return camel_mime_part_type;
bertrand's avatar
bertrand committed
181
}
182

Peter Williams's avatar
Peter Williams committed
183

bertrand's avatar
bertrand committed
184 185
/* **** */

186 187
static gboolean
process_header(CamelMedium *medium, const char *header_name, const char *header_value)
bertrand's avatar
bertrand committed
188
{
bertrand's avatar
bertrand committed
189
	CamelMimePart *mime_part = CAMEL_MIME_PART (medium);
190 191 192
	CamelHeaderType header_type;
	char *text;

Bertrand Guiheneuf's avatar
Bertrand Guiheneuf committed
193 194 195
	/* Try to parse the header pair. If it corresponds to something   */
	/* known, the job is done in the parsing routine. If not,         */
	/* we simply add the header in a raw fashion                      */
196 197 198 199 200 201

	header_type = (CamelHeaderType) g_hash_table_lookup (header_name_table, header_name);
	switch (header_type) {
	case HEADER_DESCRIPTION: /* raw header->utf8 conversion */
		text = header_decode_string(header_value);
		g_free(mime_part->description);
202
		mime_part->description = g_strstrip (text);
203 204
		break;
	case HEADER_DISPOSITION:
205
		set_disposition(mime_part, header_value);
206 207 208 209 210 211 212 213
		break;
	case HEADER_CONTENT_ID:
		text = header_msgid_decode(header_value);
		g_free(mime_part->content_id);
		mime_part->content_id = text;
		break;
	case HEADER_ENCODING:
		text = header_token_decode(header_value);
214
		mime_part->encoding = camel_mime_part_encoding_from_string (text);
215 216 217 218 219 220 221
		g_free(text);
		break;
	case HEADER_CONTENT_MD5:
		g_free(mime_part->content_MD5);
		mime_part->content_MD5 = g_strdup(header_value);
		break;
	case HEADER_CONTENT_TYPE: 
222 223 224
		if (mime_part->content_type)
			header_content_type_unref (mime_part->content_type);
		mime_part->content_type = header_content_type_decode (header_value);
225 226 227 228 229
		break;
	default:
		return FALSE;
	}
	return TRUE;
bertrand's avatar
bertrand committed
230
}
231

232
static void
233
set_header (CamelMedium *medium, const char *header_name, const void *header_value)
234
{
235 236
	CamelMimePart *part = CAMEL_MIME_PART (medium);
	
237 238
	process_header(medium, header_name, header_value);
	header_raw_replace(&part->headers, header_name, header_value, -1);
239
}
240

241
static void
242
add_header (CamelMedium *medium, const char *header_name, const void *header_value)
243
{
244 245
	CamelMimePart *part = CAMEL_MIME_PART (medium);
	
246 247 248
	/* Try to parse the header pair. If it corresponds to something   */
	/* known, the job is done in the parsing routine. If not,         */
	/* we simply add the header in a raw fashion                      */
249

250
	/* If it was one of the headers we handled, it must be unique, set it instead of add */
251 252
	if (process_header(medium, header_name, header_value))
		header_raw_replace(&part->headers, header_name, header_value, -1);
253
	else
254
		header_raw_append(&part->headers, header_name, header_value, -1);
255
}
256 257

static void
258
remove_header (CamelMedium *medium, const char *header_name)
259
{
260
	CamelMimePart *part = (CamelMimePart *)medium;
261
	
262
	process_header(medium, header_name, NULL);
263 264 265 266 267 268 269 270 271
	header_raw_remove(&part->headers, header_name);
}

static const void *
get_header (CamelMedium *medium, const char *header_name)
{
	CamelMimePart *part = (CamelMimePart *)medium;

	return header_raw_find(&part->headers, header_name, NULL);
272 273
}

274 275

/* **** Content-Description */
276
void
277
camel_mime_part_set_description (CamelMimePart *mime_part, const gchar *description)
278
{
279 280
	char *text = header_encode_string (description);
	
281
	camel_medium_set_header (CAMEL_MEDIUM (mime_part),
282 283
				 "Content-Description", text);
	g_free (text);
284 285
}

286
const gchar *
287 288
camel_mime_part_get_description (CamelMimePart *mime_part)
{
289
	return mime_part->description;
290 291
}

292
/* **** Content-Disposition */
bertrand's avatar
bertrand committed
293

294
static void
295
set_disposition (CamelMimePart *mime_part, const gchar *disposition)
296
{
297
	header_disposition_unref(mime_part->disposition);
298 299 300 301
	if (disposition)
		mime_part->disposition = header_disposition_decode(disposition);
	else
		mime_part->disposition = NULL;
302 303 304
}


305
void
306
camel_mime_part_set_disposition (CamelMimePart *mime_part, const gchar *disposition)
307
{
308
	char *text;
bertrand's avatar
bertrand committed
309

310 311 312 313 314 315 316 317 318
	/* we poke in a new disposition (so we dont lose 'filename', etc) */
	if (mime_part->disposition == NULL) {
		set_disposition(mime_part, disposition);
	}
	if (mime_part->disposition != NULL) {
		g_free(mime_part->disposition->disposition);
		mime_part->disposition->disposition = g_strdup(disposition);
	}
	text = header_disposition_format(mime_part->disposition);
bertrand's avatar
bertrand committed
319

320
	camel_medium_set_header (CAMEL_MEDIUM (mime_part),
321
				 "Content-Disposition", text);
322

323
	g_free(text);
324 325
}

326
const gchar *
327 328
camel_mime_part_get_disposition (CamelMimePart *mime_part)
{
329 330 331 332
	if (mime_part->disposition)
		return (mime_part->disposition)->disposition;
	else
		return NULL;
333 334 335
}


336
/* **** Content-Disposition: filename="xxx" */
337

338
void
339
camel_mime_part_set_filename (CamelMimePart *mime_part, const gchar *filename)
340
{
341 342 343
	char *str;
	if (mime_part->disposition == NULL)
		mime_part->disposition = header_disposition_decode("attachment");
344

345 346
	header_set_param(&mime_part->disposition->params, "filename", filename);
	str = header_disposition_format(mime_part->disposition);
347

348 349
	camel_medium_set_header (CAMEL_MEDIUM (mime_part),
				 "Content-Disposition", str);
350
	g_free(str);
351 352
}

353
const gchar *
354 355
camel_mime_part_get_filename (CamelMimePart *mime_part)
{
356 357 358 359 360 361
	if (mime_part->disposition) {
		const gchar *name = header_param (mime_part->disposition->params, "filename");
		if (name)
			return name;
	}

362
	return header_content_type_param (mime_part->content_type, "name");
363 364
}

365

366
/* **** Content-ID: */
bertrand's avatar
bertrand committed
367

368 369
void
camel_mime_part_set_content_id (CamelMimePart *mime_part, const char *contentid)
370
{
371 372
	camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-ID",
				 contentid);
373 374
}

375
const gchar *
376 377
camel_mime_part_get_content_id (CamelMimePart *mime_part)
{
378
	return mime_part->content_id;
379 380
}

381
/* **** Content-MD5: */
382

383 384
void
camel_mime_part_set_content_MD5 (CamelMimePart *mime_part, const char *md5)
385
{
386
	camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-MD5", md5);
387 388
}

389
const gchar *
390 391
camel_mime_part_get_content_MD5 (CamelMimePart *mime_part)
{
392
	return mime_part->content_MD5;
393
}
394

395
/* **** Content-Transfer-Encoding: */
396

397
void
398 399
camel_mime_part_set_encoding (CamelMimePart *mime_part,
			      CamelMimePartEncodingType encoding)
400
{
401
	const char *text;
bertrand's avatar
bertrand committed
402

403
	text = camel_mime_part_encoding_to_string (encoding);
404 405
	camel_medium_set_header (CAMEL_MEDIUM (mime_part),
				 "Content-Transfer-Encoding", text);
406 407
}

408
const CamelMimePartEncodingType
409 410
camel_mime_part_get_encoding (CamelMimePart *mime_part)
{
411
	return mime_part->encoding;
412 413
}

414
/* FIXME: do something with this stuff ... */
415

416 417 418
void
camel_mime_part_set_content_languages (CamelMimePart *mime_part, GList *content_languages)
{
419 420
	if (mime_part->content_languages) string_list_free (mime_part->content_languages);
	mime_part->content_languages = content_languages;
421

422
	/* FIXME: translate to a header and set it */
423 424
}

425
const GList *
426 427
camel_mime_part_get_content_languages (CamelMimePart *mime_part)
{
428
	return mime_part->content_languages;
429 430
}

431

bertrand's avatar
bertrand committed
432 433
/* **** */

434
/* **** Content-Type: */
435 436

void 
437
camel_mime_part_set_content_type (CamelMimePart *mime_part, gchar *content_type)
438
{
439 440
	camel_medium_set_header (CAMEL_MEDIUM (mime_part),
				 "Content-Type", content_type);
441 442
}

443
CamelContentType *
444 445
camel_mime_part_get_content_type (CamelMimePart *mime_part)
{
446
	return mime_part->content_type;
447
}
448

449 450
/*********/

451

452

453
static void
454
set_content_object (CamelMedium *medium, CamelDataWrapper *content)
455
{
bertrand's avatar
bertrand committed
456
	CamelMimePart *mime_part = CAMEL_MIME_PART (medium);
457
	CamelContentType *object_content_type;
458

bertrand's avatar
bertrand committed
459 460
	parent_class->set_content_object (medium, content);

461 462
	object_content_type = camel_data_wrapper_get_mime_type_field (content);
	if (mime_part->content_type != object_content_type) {
463 464
		char *txt;

465
		txt = header_content_type_format (object_content_type);
Not Zed's avatar
Not Zed committed
466 467
		camel_medium_set_header (CAMEL_MEDIUM (mime_part), "Content-Type", txt);
		g_free(txt);
468
	}
469 470
}

471
/**********************************************************************/
472

473
static int
474
write_to_stream(CamelDataWrapper *data_wrapper, CamelStream *stream)
475
{
476 477
	CamelMimePart *mp = CAMEL_MIME_PART(data_wrapper);
	CamelMedium *medium = CAMEL_MEDIUM(data_wrapper);
478
	CamelDataWrapper *content;
479
	int total = 0;
480
	int count;
bertrand's avatar
bertrand committed
481

482
	d(printf("mime_part::write_to_stream\n"));
483

484
	/* FIXME: something needs to be done about this ... */
485
	/* FIXME: need to count these bytes too */
486
#ifndef NO_WARNINGS
487
#warning content-languages should be stored as a header
488
#endif
489 490 491

	if (mp->headers) {
		struct _header_raw *h = mp->headers;
492
		char *val;
493

494 495
		/* 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) */
496
		while (h) {
497 498
			val = h->value;
			if (val == NULL) {
499
				g_warning("h->value is NULL here for %s", h->name);
500
				count = 0;
501
			} else if (g_hash_table_lookup(header_formatted_table, h->name) == NULL) {
502 503 504
				val = header_fold(val, strlen(h->name));
				count = camel_stream_printf(stream, "%s%s%s\n", h->name, isspace(val[0]) ? ":" : ": ", val);
				g_free(val);
505 506
			} else {
				count = camel_stream_printf(stream, "%s%s%s\n", h->name, isspace(val[0]) ? ":" : ": ", val);
507
			}
508
			if (count == -1)
509
				return -1;
510
			total += count;
511
			h = h->next;
512 513
		}
	}
514

515
	count = camel_stream_write(stream, "\n", 1);
516
	if (count == -1)
517
		return -1;
518
	total += count;
519

520
	content = camel_medium_get_content_object(medium);
521
	if (content) {
522 523 524 525 526
		/* 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;
527 528
		CamelMimeFilter *charenc = NULL;
		const char *charset;
529 530 531 532 533 534 535 536 537 538 539

		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;
		default:
			break;
		}
540

541 542
		if (header_content_type_is(mp->content_type, "text", "*")) {
			charset = header_content_type_param(mp->content_type, "charset");
543 544 545 546 547 548
			if (!(charset == NULL || !strcasecmp(charset, "us-ascii") || !strcasecmp(charset, "utf-8"))) {
				charenc = (CamelMimeFilter *)camel_mime_filter_charset_new_convert("utf-8", charset);
			} 
		}

		if (filter || charenc) {
549
			filter_stream = camel_stream_filter_new_with_stream(stream);
550 551 552 553 554 555 556 557

			/* if we have a character encoder, add that always */
			if (charenc) {
				camel_stream_filter_add(filter_stream, charenc);
				camel_object_unref((CamelObject *)charenc);
			}

			/* we only re-do crlf on encoded blocks */
558
			if (filter && header_content_type_is(mp->content_type, "text", "*")) {
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
559 560
				CamelMimeFilter *crlf = camel_mime_filter_crlf_new(CAMEL_MIME_FILTER_CRLF_ENCODE,
										   CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
561

562
				camel_stream_filter_add(filter_stream, crlf);
Peter Williams's avatar
Peter Williams committed
563
				camel_object_unref((CamelObject *)crlf);
564

565
			}
566 567 568 569 570 571

			if (filter) {
				camel_stream_filter_add(filter_stream, filter);
				camel_object_unref((CamelObject *)filter);
			}

572 573 574 575
			stream = (CamelStream *)filter_stream;
		}

#endif
576
		count = camel_data_wrapper_write_to_stream(content, stream);
577 578
		if (filter_stream) {
			camel_stream_flush((CamelStream *)filter_stream);
Peter Williams's avatar
Peter Williams committed
579
			camel_object_unref((CamelObject *)filter_stream);
580
		}
581
		if (count == -1)
582
			return -1;
583
		total += count;
584 585
	} else {
		g_warning("No content for medium, nothing to write");
586
	}
587
	return total;
588 589 590
}

/* mime_part */
591
static int
592
construct_from_parser(CamelMimePart *dw, CamelMimeParser *mp)
593 594 595 596 597
{
	struct _header_raw *headers;
	char *buf;
	int len;

598
	d(printf("mime_part::construct_from_parser()\n"));
599 600 601

	switch (camel_mime_parser_step(mp, &buf, &len)) {
	case HSCAN_MESSAGE:
Not Zed's avatar
Not Zed committed
602
		/* set the default type of a message always */
603 604 605
		if (dw->content_type)
			header_content_type_unref (dw->content_type);
		dw->content_type = header_content_type_decode ("message/rfc822");
Not Zed's avatar
Not Zed committed
606
	case HSCAN_HEADER:
607 608 609 610 611 612 613
	case HSCAN_MULTIPART:
		/* we have the headers, build them into 'us' */
		headers = camel_mime_parser_headers_raw(mp);
		while (headers) {
			camel_medium_add_header((CamelMedium *)dw, headers->name, headers->value);
			headers = headers->next;
		}
614
		camel_mime_part_construct_content_from_parser(dw, mp);
615 616 617 618
		break;
	default:
		g_warning("Invalid state encountered???: %d", camel_mime_parser_state(mp));
	}
619 620

	d(printf("mime_part::construct_from_parser() leaving\n"));
621
#ifndef NO_WARNINGS
622
#warning "Need to work out how to detect a (fatally) bad parse in the parser"
623
#endif
624
	return 0;
625
}
626

627 628 629 630 631 632 633 634 635 636
/**
 * camel_mime_part_construct_from_parser:
 * @mime_part: 
 * @mp: 
 * 
 * 
 * 
 * Return value: 
 **/
int
637
camel_mime_part_construct_from_parser(CamelMimePart *mime_part, CamelMimeParser *mp)
638
{
639
	return CMP_CLASS (mime_part)->construct_from_parser (mime_part, mp);
640 641
}

642
static int
643
construct_from_stream(CamelDataWrapper *dw, CamelStream *s)
644
{
645
	CamelMimeParser *mp;
646
	int ret;
647

648
	d(printf("mime_part::construct_from_stream()\n"));
649

650 651 652
	mp = camel_mime_parser_new();
	if (camel_mime_parser_init_with_stream(mp, s) == -1) {
		g_warning("Cannot create parser for stream");
653
		ret = -1;
654
	} else {
655
		ret = camel_mime_part_construct_from_parser((CamelMimePart *)dw, mp);
656
	}
Peter Williams's avatar
Peter Williams committed
657
	camel_object_unref((CamelObject *)mp);
658
	return ret;
659 660
}

661 662 663 664 665 666 667 668 669 670 671
/* this must be kept in sync with the header */
static const char *encodings[] = {
	"",
	"7bit",
	"8bit",
	"base64",
	"quoted-printable",
	"binary"
};

const char *
672 673
camel_mime_part_encoding_to_string (CamelMimePartEncodingType encoding)
{
674 675
	if (encoding >= sizeof(encodings)/sizeof(encodings[0]))
		encoding = 0;
676

677 678
	return encodings[encoding];
}
679

680 681 682 683
/* FIXME I am not sure this is the correct way to do this.  */
CamelMimePartEncodingType
camel_mime_part_encoding_from_string (const gchar *string)
{
684 685 686 687 688 689 690 691 692
	int i;

	if (string != NULL) {
		for (i=0;i<sizeof(encodings)/sizeof(encodings[0]);i++)
			if (!strcasecmp(string, encodings[i]))
				return i;
	}

	return CAMEL_MIME_PART_ENCODING_DEFAULT;
693 694 695
}


696 697 698
/******************************/
/**  Misc utility functions  **/

699 700 701 702 703 704 705 706
/**
 * camel_mime_part_new:
 *
 * Return value: a new CamelMimePart
 **/
CamelMimePart *
camel_mime_part_new (void)
{
Peter Williams's avatar
Peter Williams committed
707
	return (CamelMimePart *)camel_object_new (CAMEL_MIME_PART_TYPE);
708
}
709 710

/**
711 712 713 714 715
 * camel_mime_part_set_content:
 * @camel_mime_part: Mime part
 * @data: data to put into the part
 * @length: length of @data
 * @type: Content-Type of the data
716 717
 * 
 * Utility function used to set the content of a mime part object to 
718 719 720
 * be the provided data. If @length is 0, this routine can be used as
 * a way to remove old content (in which case @data and @type are
 * ignored and may be %NULL).
721 722
 **/
void 
723
camel_mime_part_set_content (CamelMimePart *camel_mime_part,
724 725
			     const char *data, int length,
			     const char *type) /* why on earth is the type last? */
726
{
bertrand's avatar
bertrand committed
727 728
	CamelMedium *medium = CAMEL_MEDIUM (camel_mime_part);

729 730 731
	if (length) {
		CamelDataWrapper *dw;
		CamelStream *stream;
732

733
		dw = camel_data_wrapper_new ();
734 735 736
		camel_data_wrapper_set_mime_type (dw, type);
		stream = camel_stream_mem_new_with_buffer (data, length);
		camel_data_wrapper_construct_from_stream (dw, stream);
Peter Williams's avatar
Peter Williams committed
737
		camel_object_unref (CAMEL_OBJECT (stream));
738
		camel_medium_set_content_object (medium, dw);
Peter Williams's avatar
Peter Williams committed
739
		camel_object_unref (CAMEL_OBJECT (dw));
740
	} else {
741
		if (medium->content)
Peter Williams's avatar
Peter Williams committed
742
			camel_object_unref (CAMEL_OBJECT (medium->content));
743 744
		medium->content = NULL;
	}
745
}