summaryrefslogtreecommitdiff
path: root/src/camel/camel-mime-part-utils.c
blob: 9bec30373ef87ee403995920c2c24cc4c2e9454d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
/* camel-mime-part-utils : Utility for mime parsing and so on
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
 *          Michael Zucchi <notzed@ximian.com>
 *          Jeffrey Stedfast <fejj@ximian.com>
 */

#include "evolution-data-server-config.h"

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "camel-charset-map.h"
#include "camel-html-parser.h"
#include "camel-iconv.h"
#include "camel-mime-filter-basic.h"
#include "camel-mime-filter-charset.h"
#include "camel-mime-filter-crlf.h"
#include "camel-mime-message.h"
#include "camel-mime-part-utils.h"
#include "camel-multipart-encrypted.h"
#include "camel-multipart-signed.h"
#include "camel-multipart.h"
#include "camel-stream-filter.h"
#include "camel-stream-fs.h"
#include "camel-stream-mem.h"
#include "camel-stream-buffer.h"
#include "camel-utf8.h"

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

/* simple data wrapper */
static gboolean
simple_data_wrapper_construct_from_parser (CamelDataWrapper *dw,
                                           CamelMimeParser *mp,
                                           GCancellable *cancellable,
                                           GError **error)
{
	gchar *buf;
	GByteArray *buffer;
	CamelStream *mem;
	gsize len;
	gboolean success;

	d (printf ("simple_data_wrapper_construct_from_parser()\n"));

	/* read in the entire content */
	buffer = g_byte_array_new ();
	while (camel_mime_parser_step (mp, &buf, &len) != CAMEL_MIME_PARSER_STATE_BODY_END) {
		d (printf ("appending o/p data: %d: %.*s\n", len, len, buf));
		g_byte_array_append (buffer, (guint8 *) buf, len);
	}

	d (printf ("message part kept in memory!\n"));

	mem = camel_stream_mem_new_with_byte_array (buffer);
	success = camel_data_wrapper_construct_from_stream_sync (
		dw, mem, cancellable, error);
	g_object_unref (mem);

	return success;
}

/**
 * camel_mime_part_construct_content_from_parser:
 * @mime_part: a #CamelMimePart
 * @mp: a #CamelMimeParser
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Constructs the contnet of @mime_part from the given mime parser.
 *
 * Returns: whether succeeded
 *
 * Since: 2.24
 **/
gboolean
camel_mime_part_construct_content_from_parser (CamelMimePart *mime_part,
                                               CamelMimeParser *mp,
                                               GCancellable *cancellable,
                                               GError **error)
{
	CamelDataWrapper *content = NULL;
	CamelContentType *ct;
	gchar *encoding;
	gboolean success = TRUE;

	g_return_val_if_fail (CAMEL_IS_MIME_PART (mime_part), FALSE);

	ct = camel_mime_parser_content_type (mp);

	encoding = camel_content_transfer_encoding_decode (camel_mime_parser_header (mp, "Content-Transfer-Encoding", NULL));

	switch (camel_mime_parser_state (mp)) {
	case CAMEL_MIME_PARSER_STATE_HEADER:
		d (printf ("Creating body part\n"));
		/* multipart/signed is some type that we must treat as binary data. */
		if (camel_content_type_is (ct, "multipart", "signed")) {
			content = (CamelDataWrapper *) camel_multipart_signed_new ();
			camel_multipart_construct_from_parser ((CamelMultipart *) content, mp);
		} else {
			content = camel_data_wrapper_new ();
			success = simple_data_wrapper_construct_from_parser (
				content, mp, cancellable, error);
		}
		break;
	case CAMEL_MIME_PARSER_STATE_MESSAGE:
		d (printf ("Creating message part\n"));
		content = (CamelDataWrapper *) camel_mime_message_new ();
		success = camel_mime_part_construct_from_parser_sync (
			(CamelMimePart *) content, mp, cancellable, error);
		break;
	case CAMEL_MIME_PARSER_STATE_MULTIPART:
		d (printf ("Creating multi-part\n"));
		if (camel_content_type_is (ct, "multipart", "encrypted"))
			content = (CamelDataWrapper *) camel_multipart_encrypted_new ();
		else if (camel_content_type_is (ct, "multipart", "signed"))
			content = (CamelDataWrapper *) camel_multipart_signed_new ();
		else
			content = (CamelDataWrapper *) camel_multipart_new ();

		camel_multipart_construct_from_parser ((CamelMultipart *) content, mp);
		d (printf ("Created multi-part\n"));
		break;
	default:
		g_warning ("Invalid state encountered???: %u", camel_mime_parser_state (mp));
	}

	if (content) {
		if (encoding)
			camel_data_wrapper_set_encoding (content, camel_transfer_encoding_from_string (encoding));

		camel_data_wrapper_set_mime_type_field (content, camel_mime_part_get_content_type (mime_part));
		camel_medium_set_content (CAMEL_MEDIUM (mime_part), content);
		g_object_unref (content);
	}

	g_free (encoding);

	return success;
}

G_DEFINE_BOXED_TYPE (CamelMessageContentInfo,
		camel_message_content_info,
		camel_message_content_info_copy,
		camel_message_content_info_free)

/**
 * camel_message_content_info_new:
 *
 * Allocate a new #CamelMessageContentInfo.
 *
 * Returns: (transfer full): a newly allocated #CamelMessageContentInfo
 **/
CamelMessageContentInfo *
camel_message_content_info_new (void)
{
	return g_slice_alloc0 (sizeof (CamelMessageContentInfo));
}

/**
 * camel_message_content_info_copy:
 * @src: (nullable): a source #CamelMessageContentInfo to copy
 *
 * Returns: (nullable): a copy of @src, or %NULL, if @src was %NULL
 *
 * Since: 3.24
 **/
CamelMessageContentInfo *
camel_message_content_info_copy (const CamelMessageContentInfo *src)
{
	CamelMessageContentInfo *res;

	if (!src)
		return NULL;

	res = camel_message_content_info_new ();

	if (src->type) {
		gchar *content_type;

		content_type = camel_content_type_format (src->type);
		res->type = camel_content_type_decode (content_type);

		g_free (content_type);
	}

	if (src->disposition) {
		gchar *disposition;

		disposition = camel_content_disposition_format (src->disposition);
		res->disposition = camel_content_disposition_decode (disposition);

		g_free (disposition);
	}

	res->id = g_strdup (src->id);
	res->description = g_strdup (src->description);
	res->encoding = g_strdup (src->encoding);
	res->size = src->size;

	res->next = camel_message_content_info_copy (src->next);
	res->childs = camel_message_content_info_copy (src->childs);

	if (res->childs) {
		CamelMessageContentInfo *child;

		for (child = res->childs; child; child = child->next) {
			child->parent = res;
		}
	}

	return res;
}

/**
 * camel_message_content_info_free:
 * @ci: a #CamelMessageContentInfo
 *
 * Recursively frees the content info @ci, and all associated memory.
 **/
void
camel_message_content_info_free (CamelMessageContentInfo *ci)
{
	CamelMessageContentInfo *pw, *pn;

	pw = ci->childs;

	camel_content_type_unref (ci->type);
	camel_content_disposition_unref (ci->disposition);
	g_free (ci->id);
	g_free (ci->description);
	g_free (ci->encoding);
	g_slice_free1 (sizeof (CamelMessageContentInfo), ci);

	while (pw) {
		pn = pw->next;
		camel_message_content_info_free (pw);
		pw = pn;
	}
}

CamelMessageContentInfo *
camel_message_content_info_new_from_parser (CamelMimeParser *mp)
{
	CamelMessageContentInfo *ci = NULL;
	CamelNameValueArray *headers = NULL;

	g_return_val_if_fail (CAMEL_IS_MIME_PARSER (mp), NULL);

	switch (camel_mime_parser_state (mp)) {
	case CAMEL_MIME_PARSER_STATE_HEADER:
	case CAMEL_MIME_PARSER_STATE_MESSAGE:
	case CAMEL_MIME_PARSER_STATE_MULTIPART:
		headers = camel_mime_parser_dup_headers (mp);
		ci = camel_message_content_info_new_from_headers (headers);
		camel_name_value_array_free (headers);
		if (ci) {
			if (ci->type)
				camel_content_type_unref (ci->type);
			ci->type = camel_mime_parser_content_type (mp);
			camel_content_type_ref (ci->type);
		}
		break;
	default:
		g_error ("Invalid parser state");
	}

	return ci;
}

CamelMessageContentInfo *
camel_message_content_info_new_from_message (CamelMimePart *mp)
{
	CamelMessageContentInfo *ci = NULL;
	const CamelNameValueArray *headers = NULL;

	g_return_val_if_fail (CAMEL_IS_MIME_PART (mp), NULL);

	headers = camel_medium_get_headers (CAMEL_MEDIUM (mp));
	ci = camel_message_content_info_new_from_headers (headers);

	return ci;
}

CamelMessageContentInfo *
camel_message_content_info_new_from_headers (const CamelNameValueArray *headers)
{
	CamelMessageContentInfo *ci;
	const gchar *charset;

	ci = camel_message_content_info_new ();

	charset = camel_iconv_locale_charset ();
	ci->id = camel_header_msgid_decode (camel_name_value_array_get_named (headers, CAMEL_COMPARE_CASE_INSENSITIVE, "Content-ID"));
	ci->description = camel_header_decode_string (camel_name_value_array_get_named (headers, CAMEL_COMPARE_CASE_INSENSITIVE, "Content-Description"), charset);
	ci->encoding = camel_content_transfer_encoding_decode (camel_name_value_array_get_named (headers, CAMEL_COMPARE_CASE_INSENSITIVE, "Content-Transfer-Encoding"));
	ci->type = camel_content_type_decode (camel_name_value_array_get_named (headers, CAMEL_COMPARE_CASE_INSENSITIVE, "Content-Type"));
	ci->disposition = camel_content_disposition_decode (camel_name_value_array_get_named (headers, CAMEL_COMPARE_CASE_INSENSITIVE, "Content-Disposition"));

	return ci;
}

/**
 * camel_message_content_info_traverse:
 * @ci: a #CamelMessageContentInfo
 * @func: (scope call): a #CamelMessageContentInfoTraverseCallback
 * @user_data: user data passed to @func
 *
 * Calls the @func for each #CamelMessageContentInfo, including the top one.
 * The @func can return %TRUE to continue processing or %FALSE to stop it.
 *
 * Returns: %FALSE on error or when the @func returned %FALSE, otherwise %TRUE
 **/
gboolean
camel_message_content_info_traverse (CamelMessageContentInfo *ci,
				     CamelMessageContentInfoTraverseCallback func,
				     gpointer user_data)
{
	CamelMessageContentInfo *next, *cur;
	gint depth = 0;

	g_return_val_if_fail (ci != NULL, FALSE);
	g_return_val_if_fail (func != NULL, FALSE);

	cur = ci;
	do {
		if (!func (cur, depth, user_data))
			return FALSE;

		next = cur->childs;
		if (next)
			depth++;
		else
			next = cur->next;

		if (!next) {
			next = cur->parent;
			depth--;

			if (depth < 0) {
				next = NULL;
				break;
			}

			while (next) {
				CamelMessageContentInfo *sibl;

				sibl = next->next;
				if (sibl) {
					next = sibl;
					break;
				}

				next = next->parent;
				depth--;

				if (depth < 0) {
					next = NULL;
					break;
				}
			}
		}

		cur = next;
	} while (cur);

	return TRUE;
}

static gboolean
dump_content_into_cb (CamelMessageContentInfo *ci,
		      gint depth,
		      gpointer user_data)
{
	depth = (GPOINTER_TO_INT (user_data) + depth) * 4;

	if (ci->type)
		printf ("%*scontent-type: %s/%s\n", depth, "",
			ci->type->type ? ci->type->type : "(null)",
			ci->type->subtype ? ci->type->subtype : "(null)");
	else
		printf ("%*scontent-type: <unset>\n", depth, "");

	printf ("%*scontent-transfer-encoding: %s\n", depth, "", ci->encoding ? ci->encoding : "(null)");
	printf ("%*scontent-description: %s\n", depth, "", ci->description ? ci->description : "(null)");

	if (ci->disposition) {
		gchar *disposition;

		disposition = camel_content_disposition_format (ci->disposition);
		printf ("%*scontent-disposition: %s\n", depth, "", disposition ? disposition : "(null)");
		g_free (disposition);
	} else {
		printf ("%*scontent-disposition: <unset>\n", depth, "");
	}

	printf ("%*ssize: %" G_GUINT32_FORMAT "\n", depth, "", ci->size);

	return TRUE;
}

void
camel_message_content_info_dump (CamelMessageContentInfo *ci,
				 gint depth)
{
	if (ci == NULL) {
		printf ("%*s<empty>\n", depth * 4, "");
		return;
	}

	camel_message_content_info_traverse (ci, dump_content_into_cb, GINT_TO_POINTER (depth));
}