File: gmime-message-partial.c

package info (click to toggle)
gmime2 2.0.14-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,092 kB
  • ctags: 3,177
  • sloc: ansic: 29,973; sh: 8,234; makefile: 413; perl: 6
file content (456 lines) | stat: -rw-r--r-- 12,343 bytes parent folder | download
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2002-2004 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 <stdlib.h>
#include <string.h>

#include "gmime-message-partial.h"
#include "gmime-stream-cat.h"
#include "gmime-stream-mem.h"
#include "gmime-parser.h"

/* GObject class methods */
static void g_mime_message_partial_class_init (GMimeMessagePartialClass *klass);
static void g_mime_message_partial_init (GMimeMessagePartial *catpart, GMimeMessagePartialClass *klass);
static void g_mime_message_partial_finalize (GObject *object);

/* GMimeObject class methods */
static void message_partial_init (GMimeObject *object);
static void message_partial_add_header (GMimeObject *object, const char *header, const char *value);
static void message_partial_set_header (GMimeObject *object, const char *header, const char *value);
static void message_partial_set_content_type (GMimeObject *object, GMimeContentType *content_type);


static GMimePartClass *parent_class = NULL;


GType
g_mime_message_partial_get_type (void)
{
	static GType type = 0;
	
	if (!type) {
		static const GTypeInfo info = {
			sizeof (GMimeMessagePartialClass),
			NULL, /* base_class_init */
			NULL, /* base_class_finalize */
			(GClassInitFunc) g_mime_message_partial_class_init,
			NULL, /* class_finalize */
			NULL, /* class_data */
			sizeof (GMimeMessagePartial),
			0,    /* n_preallocs */
			(GInstanceInitFunc) g_mime_message_partial_init,
		};
		
		type = g_type_register_static (GMIME_TYPE_PART, "GMimeMessagePartial", &info, 0);
	}
	
	return type;
}


static void
g_mime_message_partial_class_init (GMimeMessagePartialClass *klass)
{
	GMimeObjectClass *object_class = GMIME_OBJECT_CLASS (klass);
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	
	parent_class = g_type_class_ref (GMIME_TYPE_PART);
	
	gobject_class->finalize = g_mime_message_partial_finalize;
	
	object_class->init = message_partial_init;
	object_class->add_header = message_partial_add_header;
	object_class->set_header = message_partial_set_header;
	object_class->set_content_type = message_partial_set_content_type;
}

static void
g_mime_message_partial_init (GMimeMessagePartial *partial, GMimeMessagePartialClass *klass)
{
	partial->id = NULL;
	partial->number = -1;
	partial->total = -1;
}

static void
g_mime_message_partial_finalize (GObject *object)
{
	GMimeMessagePartial *partial = (GMimeMessagePartial *) object;
	
	g_free (partial->id);
	
	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
message_partial_init (GMimeObject *object)
{
	/* no-op */
	GMIME_OBJECT_CLASS (parent_class)->init (object);
}

static void
message_partial_add_header (GMimeObject *object, const char *header, const char *value)
{
	/* RFC 1864 states that you cannot set a Content-MD5 on a message part */
	if (!g_strcasecmp ("Content-MD5", header))
		return;
	
	/* Make sure that the header is a Content-* header, else it
           doesn't belong on a mime part */
	
	if (!g_strncasecmp ("Content-", header, 8))
		GMIME_OBJECT_CLASS (parent_class)->add_header (object, header, value);
}

static void
message_partial_set_header (GMimeObject *object, const char *header, const char *value)
{
	/* RFC 1864 states that you cannot set a Content-MD5 on a message part */
	if (!g_strcasecmp ("Content-MD5", header))
		return;
	
	/* Make sure that the header is a Content-* header, else it
           doesn't belong on a mime part */
	
	if (!g_strncasecmp ("Content-", header, 8))
		GMIME_OBJECT_CLASS (parent_class)->set_header (object, header, value);
}

static void
message_partial_set_content_type (GMimeObject *object, GMimeContentType *content_type)
{
	GMimeMessagePartial *partial = (GMimeMessagePartial *) object;
	const char *value;
	
	value = g_mime_content_type_get_parameter (content_type, "id");
	g_free (partial->id);
	partial->id = g_strdup (value);
	
	value = g_mime_content_type_get_parameter (content_type, "number");
	partial->number = value ? atoi (value) : -1;
	
	value = g_mime_content_type_get_parameter (content_type, "total");
	partial->total = value ? atoi (value) : -1;
	
	GMIME_OBJECT_CLASS (parent_class)->set_content_type (object, content_type);
}


/**
 * g_mime_message_partial_new:
 * @id: message/partial part id
 * @number: message/partial part number
 * @total: total number of message/partial parts
 *
 * Creates a new MIME message/partial object.
 *
 * Returns an empty MIME message/partial object.
 **/
GMimeMessagePartial *
g_mime_message_partial_new (const char *id, int number, int total)
{
	GMimeMessagePartial *partial;
	GMimeContentType *type;
	char *num;
	
	partial = g_object_new (GMIME_TYPE_MESSAGE_PARTIAL, NULL, NULL);
	
	type = g_mime_content_type_new ("message", "partial");
	
	partial->id = g_strdup (id);
	g_mime_content_type_set_parameter (type, "id", id);
	
	partial->number = number;
	num = g_strdup_printf ("%d", number);
	g_mime_content_type_set_parameter (type, "number", num);
	g_free (num);
	
	partial->total = total;
	num = g_strdup_printf ("%d", total);
	g_mime_content_type_set_parameter (type, "total", num);
	g_free (num);
	
	g_mime_object_set_content_type (GMIME_OBJECT (partial), type);
	
	return partial;
}


/**
 * g_mime_message_partial_get_id:
 * @partial: message/partial object
 *
 * Gets the message/partial id parameter value.
 *
 * Returns the message/partial id or %NULL on fail.
 **/
const char *
g_mime_message_partial_get_id (GMimeMessagePartial *partial)
{
	g_return_val_if_fail (GMIME_IS_MESSAGE_PARTIAL (partial), NULL);
	
	return partial->id;
}


/**
 * g_mime_message_partial_get_number:
 * @partial: message/partial object
 *
 * Gets the message/partial part number.
 *
 * Returns the message/partial part number or -1 on fail.
 **/
int
g_mime_message_partial_get_number (GMimeMessagePartial *partial)
{
	g_return_val_if_fail (GMIME_IS_MESSAGE_PARTIAL (partial), -1);
	
	return partial->number;
}


/**
 * g_mime_message_partial_get_total:
 * @partial: message/partial object
 *
 * Gets the total number of message/partial parts needed to
 * reconstruct the original message.
 *
 * Returns the total number of message/partial parts needed to
 * reconstruct the original message or -1 on fail.
 **/
int
g_mime_message_partial_get_total (GMimeMessagePartial *partial)
{
	g_return_val_if_fail (GMIME_IS_MESSAGE_PARTIAL (partial), -1);
	
	return partial->total;
}


static int
partial_compare (const void *v1, const void *v2)
{
	GMimeMessagePartial **partial1 = (GMimeMessagePartial **) v1;
	GMimeMessagePartial **partial2 = (GMimeMessagePartial **) v2;
	int num1, num2;
	
	num1 = g_mime_message_partial_get_number (*partial1);
	num2 = g_mime_message_partial_get_number (*partial2);
	
	return num1 - num2;
}


/**
 * g_mime_message_partial_reconstruct_message:
 * @partials: an array of message/partial mime parts
 * @num: the number of elements in @partials
 *
 * Reconstructs the GMimeMessage from the given message/partial parts
 * in @partials.
 *
 * Returns a GMimeMessage object on success or %NULL on fail.
 **/
GMimeMessage *
g_mime_message_partial_reconstruct_message (GMimeMessagePartial **partials, size_t num)
{
	GMimeMessagePartial *partial;
	GMimeDataWrapper *wrapper;
	GMimeStream *cat, *stream;
	GMimeMessage *message;
	GMimeParser *parser;
	int total, number;
	const char *id;
	size_t i;
	
	g_return_val_if_fail (num > 0, NULL);
	
	id = g_mime_message_partial_get_id (partials[0]);
	if (!id)
		return NULL;
	
	/* get them into the correct order... */
	qsort ((void *) partials, num, sizeof (gpointer),
	       partial_compare);
	
	/* only the last message/partial part is REQUIRED to have the total parameter */
	total = g_mime_message_partial_get_total (partials[num - 1]);
	if (num != total)
		return NULL;
	
	cat = g_mime_stream_cat_new ();
	
	for (i = 0; i < num; i++) {
		const char *partial_id;
		
		partial = partials[i];
		
		/* sanity check to make sure this part belongs... */
		partial_id = g_mime_message_partial_get_id (partial);
		if (!partial_id || strcmp (id, partial_id))
			goto exception;
		
		/* sanity check to make sure we aren't missing any parts */
		number = g_mime_message_partial_get_number (partial);
		if (number != i + 1)
			goto exception;
		
		wrapper = g_mime_part_get_content_object (GMIME_PART (partial));
		stream = g_mime_data_wrapper_get_stream (wrapper);
		g_object_unref (wrapper);
		
		g_mime_stream_reset (stream);
		g_mime_stream_cat_add_source (GMIME_STREAM_CAT (cat), stream);
		g_mime_stream_unref (stream);
	}
	
	parser = g_mime_parser_new ();
	g_mime_parser_init_with_stream (parser, cat);
	g_mime_stream_unref (cat);
	
	message = g_mime_parser_construct_message (parser);
	g_object_unref (parser);
	
	return message;
	
 exception:
	
	g_mime_stream_unref (cat);
	
	return NULL;
}


static void
header_copy (const char *name, const char *value, gpointer user_data)
{
	GMimeMessage *message = (GMimeMessage *) user_data;
	
	g_mime_object_add_header (GMIME_OBJECT (message), name, value);
}

static GMimeMessage *
message_partial_message_new (GMimeMessage *base)
{
	GMimeMessage *message;
	
	message = g_mime_message_new (FALSE);
	g_mime_header_foreach (GMIME_OBJECT (base)->headers, header_copy, message);
	
	return message;
}


/**
 * g_mime_message_partial_split_message:
 * @message: message object
 * @max_size: max size
 * @nparts: number of parts
 *
 * Splits @message into an array of #GMimeMessage objects each
 * containing a single #GMimeMessagePartial object containing
 * @max_size bytes or fewer. @nparts is set to the number of
 * #GMimeMessagePartial objects created.
 *
 * Returns an array of #GMimeMessage objects and sets @nparts to th
 * number of messages returned or %NULL on fail.
 **/
GMimeMessage **
g_mime_message_partial_split_message (GMimeMessage *message, size_t max_size, size_t *nparts)
{
	GMimeMessage **messages;
	GMimeMessagePartial *partial;
	GMimeStream *stream, *substream;
	GMimeDataWrapper *wrapper;
	GPtrArray *parts;
	const char *id;
	off_t start;
	size_t len;
	int i;
	
	*nparts = 0;
	
	g_return_val_if_fail (GMIME_IS_MESSAGE (message), NULL);
	
	stream = g_mime_stream_mem_new ();
	
	if (g_mime_object_write_to_stream (GMIME_OBJECT (message), stream) == -1) {
		g_mime_stream_unref (stream);
		return NULL;
	}
	
	g_mime_stream_reset (stream);
	
	len = g_mime_stream_length (stream);
	
	/* optimization */
	if (len <= max_size) {
		g_mime_stream_unref (stream);
		g_mime_object_ref (GMIME_OBJECT (message));
		
		messages = g_malloc (sizeof (void *) * 2);
		messages[0] = message;
		*nparts = 1;
		
		return messages;
	}
	
	parts = g_ptr_array_new ();
	for (i = 0, start = 0; start < len; i++) {
		substream = g_mime_stream_substream (stream, start, MIN (len, start + max_size));
		g_ptr_array_add (parts, substream);
		start += max_size;
	}
	
	id = g_mime_message_get_message_id (message);
	
	for (i = 0; i < parts->len; i++) {
		partial = g_mime_message_partial_new (id, i + 1, parts->len);
		wrapper = g_mime_data_wrapper_new_with_stream (GMIME_STREAM (parts->pdata[i]),
							       GMIME_PART_ENCODING_DEFAULT);
		g_mime_stream_unref (GMIME_STREAM (parts->pdata[i]));
		g_mime_part_set_content_object (GMIME_PART (partial), wrapper);
		g_object_unref (wrapper);
		
		parts->pdata[i] = message_partial_message_new (message);
		g_mime_message_set_mime_part (GMIME_MESSAGE (parts->pdata[i]), GMIME_OBJECT (partial));
		g_mime_object_unref (GMIME_OBJECT (partial));
	}
	
	g_mime_stream_unref (stream);
	
	messages = (GMimeMessage **) parts->pdata;
	*nparts = parts->len;
	
	g_ptr_array_free (parts, FALSE);
	
	return messages;
}