Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-multipart.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-multipart.c : Abstract class for a multipart */
3 /*
4  *
5  * Author :
6  *  Bertrand Guiheneuf <bertrand@helixcode.com>
7  *
8  * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of version 2 of the GNU Lesser General Public
12  * License as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <string.h> /* strlen() */
31 #include <time.h>   /* for time */
32 #include <unistd.h> /* for getpid */
33
34 #include <libedataserver/md5-utils.h>
35
36 #include "camel-exception.h"
37 #include "camel-mime-part.h"
38 #include "camel-multipart.h"
39 #include "camel-stream-mem.h"
40
41 #define d(x)
42
43 static gboolean              is_offline        (CamelDataWrapper *data_wrapper);
44 static void                  add_part          (CamelMultipart *multipart,
45                                                 CamelMimePart *part);
46 static void                  add_part_at       (CamelMultipart *multipart,
47                                                 CamelMimePart *part,
48                                                 guint index);
49 static void                  remove_part       (CamelMultipart *multipart,
50                                                 CamelMimePart *part);
51 static CamelMimePart *       remove_part_at    (CamelMultipart *multipart,
52                                                 guint index);
53 static CamelMimePart *       get_part          (CamelMultipart *multipart,
54                                                 guint index);
55 static guint                 get_number        (CamelMultipart *multipart);
56 static void                  set_boundary      (CamelMultipart *multipart,
57                                                 const char *boundary);
58 static const gchar *         get_boundary      (CamelMultipart *multipart);
59 static ssize_t               write_to_stream   (CamelDataWrapper *data_wrapper,
60                                                 CamelStream *stream);
61 static void                  unref_part        (gpointer data, gpointer user_data);
62
63 static int construct_from_parser(CamelMultipart *multipart, struct _CamelMimeParser *mp);
64
65 static CamelDataWrapperClass *parent_class = NULL;
66
67
68
69 /* Returns the class for a CamelMultipart */
70 #define CMP_CLASS(so) CAMEL_MULTIPART_CLASS (CAMEL_OBJECT_GET_CLASS(so))
71
72 /* Returns the class for a CamelDataWrapper */
73 #define CDW_CLASS(so) CAMEL_DATA_WRAPPER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
74
75
76 static void
77 camel_multipart_class_init (CamelMultipartClass *camel_multipart_class)
78 {
79         CamelDataWrapperClass *camel_data_wrapper_class =
80                 CAMEL_DATA_WRAPPER_CLASS (camel_multipart_class);
81
82         parent_class = (CamelDataWrapperClass *) camel_data_wrapper_get_type ();
83
84         /* virtual method definition */
85         camel_multipart_class->add_part = add_part;
86         camel_multipart_class->add_part_at = add_part_at;
87         camel_multipart_class->remove_part = remove_part;
88         camel_multipart_class->remove_part_at = remove_part_at;
89         camel_multipart_class->get_part = get_part;
90         camel_multipart_class->get_number = get_number;
91         camel_multipart_class->set_boundary = set_boundary;
92         camel_multipart_class->get_boundary = get_boundary;
93         camel_multipart_class->construct_from_parser = construct_from_parser;
94
95         /* virtual method overload */
96         camel_data_wrapper_class->write_to_stream = write_to_stream;
97         camel_data_wrapper_class->decode_to_stream = write_to_stream;
98         camel_data_wrapper_class->is_offline = is_offline;
99 }
100
101 static void
102 camel_multipart_init (gpointer object, gpointer klass)
103 {
104         CamelMultipart *multipart = CAMEL_MULTIPART (object);
105
106         camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (multipart),
107                                           "multipart/mixed");
108         multipart->preface = NULL;
109         multipart->postface = NULL;
110 }
111
112 static void
113 camel_multipart_finalize (CamelObject *object)
114 {
115         CamelMultipart *multipart = CAMEL_MULTIPART (object);
116
117         g_list_foreach (multipart->parts, unref_part, NULL);
118
119         /*if (multipart->boundary)
120           g_free (multipart->boundary);*/
121         if (multipart->preface)
122                 g_free (multipart->preface);
123         if (multipart->postface)
124                 g_free (multipart->postface);
125 }
126
127
128 CamelType
129 camel_multipart_get_type (void)
130 {
131         static CamelType camel_multipart_type = CAMEL_INVALID_TYPE;
132
133         if (camel_multipart_type == CAMEL_INVALID_TYPE) {
134                 camel_multipart_type = camel_type_register (camel_data_wrapper_get_type (), "CamelMultipart",
135                                                             sizeof (CamelMultipart),
136                                                             sizeof (CamelMultipartClass),
137                                                             (CamelObjectClassInitFunc) camel_multipart_class_init,
138                                                             NULL,
139                                                             (CamelObjectInitFunc) camel_multipart_init,
140                                                             (CamelObjectFinalizeFunc) camel_multipart_finalize);
141         }
142
143         return camel_multipart_type;
144 }
145
146 static void
147 unref_part (gpointer data, gpointer user_data)
148 {
149         CamelObject *part = data;
150
151         camel_object_unref (part);
152 }
153
154 /**
155  * camel_multipart_new:
156  *
157  * Create a new #CamelMultipart object.
158  *
159  * Returns a new #CamelMultipart object
160  **/
161 CamelMultipart *
162 camel_multipart_new (void)
163 {
164         CamelMultipart *multipart;
165
166         multipart = (CamelMultipart *)camel_object_new (CAMEL_MULTIPART_TYPE);
167         multipart->preface = NULL;
168         multipart->postface = NULL;
169
170         return multipart;
171 }
172
173
174 static void
175 add_part (CamelMultipart *multipart, CamelMimePart *part)
176 {
177         multipart->parts = g_list_append (multipart->parts, part);
178         camel_object_ref (part);
179 }
180
181
182 /**
183  * camel_multipart_add_part:
184  * @multipart: a #CamelMultipart object
185  * @part: a #CamelMimePart to add
186  *
187  * Appends the part to the multipart object.
188  **/
189 void
190 camel_multipart_add_part (CamelMultipart *multipart, CamelMimePart *part)
191 {
192         g_return_if_fail (CAMEL_IS_MULTIPART (multipart));
193         g_return_if_fail (CAMEL_IS_MIME_PART (part));
194
195         CMP_CLASS (multipart)->add_part (multipart, part);
196 }
197
198
199 static void
200 add_part_at (CamelMultipart *multipart, CamelMimePart *part, guint index)
201 {
202         multipart->parts = g_list_insert (multipart->parts, part, index);
203         camel_object_ref (part);
204 }
205
206 /**
207  * camel_multipart_add_part_at:
208  * @multipart: a #CamelMultipart object
209  * @part: a #CamelMimePart to add
210  * @index: index to add the multipart at
211  *
212  * Adds the part to the multipart object after the @index'th
213  * element. If @index is greater than the number of parts, it is
214  * equivalent to #camel_multipart_add_part.
215  **/
216 void
217 camel_multipart_add_part_at (CamelMultipart *multipart,
218                              CamelMimePart *part, guint index)
219 {
220         g_return_if_fail (CAMEL_IS_MULTIPART (multipart));
221         g_return_if_fail (CAMEL_IS_MIME_PART (part));
222
223         CMP_CLASS (multipart)->add_part_at (multipart, part, index);
224 }
225
226
227 static void
228 remove_part (CamelMultipart *multipart, CamelMimePart *part)
229 {
230         if (!multipart->parts)
231                 return;
232         multipart->parts = g_list_remove (multipart->parts, part);
233         camel_object_unref (part);
234 }
235
236 /**
237  * camel_multipart_remove_part:
238  * @multipart: a #CamelMultipart object
239  * @part: a #CamelMimePart to remove
240  *
241  * Removes @part from @multipart.
242  **/
243 void
244 camel_multipart_remove_part (CamelMultipart *multipart,
245                              CamelMimePart *part)
246 {
247         g_return_if_fail (CAMEL_IS_MULTIPART (multipart));
248         g_return_if_fail (CAMEL_IS_MIME_PART (part));
249
250         CMP_CLASS (multipart)->remove_part (multipart, part);
251 }
252
253
254 static CamelMimePart *
255 remove_part_at (CamelMultipart *multipart, guint index)
256 {
257         GList *parts_list;
258         GList *part_to_remove;
259         CamelMimePart *removed_part;
260
261         if (!(multipart->parts))
262                 return NULL;
263
264         parts_list = multipart->parts;
265         part_to_remove = g_list_nth (parts_list, index);
266         if (!part_to_remove) {
267                 g_warning ("CamelMultipart::remove_part_at: "
268                            "part to remove is NULL\n");
269                 return NULL;
270         }
271         removed_part = CAMEL_MIME_PART (part_to_remove->data);
272
273         multipart->parts = g_list_remove_link (parts_list, part_to_remove);
274         if (part_to_remove->data)
275                 camel_object_unref (part_to_remove->data);
276         g_list_free_1 (part_to_remove);
277
278         return removed_part;
279 }
280
281 /**
282  * camel_multipart_remove_part_at:
283  * @multipart: a #CamelMultipart object
284  * @index: a zero-based index indicating the part to remove
285  *
286  * Remove the indicated part from the multipart object.
287  *
288  * Returns the removed part. Note that it is #camel_object_unref'ed
289  * before being returned, which may cause it to be destroyed.
290  **/
291 CamelMimePart *
292 camel_multipart_remove_part_at (CamelMultipart *multipart, guint index)
293 {
294         g_return_val_if_fail (CAMEL_IS_MULTIPART (multipart), NULL);
295
296         return CMP_CLASS (multipart)->remove_part_at (multipart, index);
297 }
298
299
300 static CamelMimePart *
301 get_part (CamelMultipart *multipart, guint index)
302 {
303         GList *part;
304
305         if (!(multipart->parts))
306                 return NULL;
307
308         part = g_list_nth (multipart->parts, index);
309         if (part)
310                 return CAMEL_MIME_PART (part->data);
311         else
312                 return NULL;
313 }
314
315 /**
316  * camel_multipart_get_part:
317  * @multipart: a #CamelMultipart object
318  * @index: a zero-based index indicating the part to get
319  *
320  * Returns the indicated subpart, or %NULL
321  **/
322 CamelMimePart *
323 camel_multipart_get_part (CamelMultipart *multipart, guint index)
324 {
325         g_return_val_if_fail (CAMEL_IS_MULTIPART (multipart), NULL);
326
327         return CMP_CLASS (multipart)->get_part (multipart, index);
328 }
329
330
331 static guint
332 get_number (CamelMultipart *multipart)
333 {
334         return g_list_length (multipart->parts);
335 }
336
337 /**
338  * camel_multipart_get_number:
339  * @multipart: a #CamelMultipart object
340  *
341  * Returns the number of subparts in @multipart
342  **/
343 guint
344 camel_multipart_get_number (CamelMultipart *multipart)
345 {
346         g_return_val_if_fail (CAMEL_IS_MULTIPART (multipart), 0);
347
348         return CMP_CLASS (multipart)->get_number (multipart);
349 }
350
351
352 static void
353 set_boundary (CamelMultipart *multipart, const char *boundary)
354 {
355         CamelDataWrapper *cdw = CAMEL_DATA_WRAPPER (multipart);
356         char *bgen, digest[16], bbuf[27], *p;
357         int state, save;
358
359         g_return_if_fail (cdw->mime_type != NULL);
360
361         if (!boundary) {
362                 /* Generate a fairly random boundary string. */
363                 bgen = g_strdup_printf ("%p:%lu:%lu", multipart,
364                                         (unsigned long) getpid(),
365                                         (unsigned long) time(0));
366                 md5_get_digest (bgen, strlen (bgen), digest);
367                 g_free (bgen);
368                 strcpy (bbuf, "=-");
369                 p = bbuf + 2;
370                 state = save = 0;
371                 p += camel_base64_encode_step (digest, 16, FALSE, p, &state, &save);
372                 *p = '\0';
373
374                 boundary = bbuf;
375         }
376
377         camel_content_type_set_param (cdw->mime_type, "boundary", boundary);
378 }
379
380 /**
381  * camel_multipart_set_boundary:
382  * @multipart: a #CamelMultipart object
383  * @boundary: the message boundary, or %NULL
384  *
385  * Sets the message boundary for @multipart to @boundary. This should
386  * be a string which does not occur anywhere in any of @multipart's
387  * subparts. If @boundary is %NULL, a randomly-generated boundary will
388  * be used.
389  **/
390 void
391 camel_multipart_set_boundary (CamelMultipart *multipart, const char *boundary)
392 {
393         g_return_if_fail (CAMEL_IS_MULTIPART (multipart));
394
395         CMP_CLASS (multipart)->set_boundary (multipart, boundary);
396 }
397
398
399 static const gchar *
400 get_boundary (CamelMultipart *multipart)
401 {
402         CamelDataWrapper *cdw = CAMEL_DATA_WRAPPER (multipart);
403
404         g_return_val_if_fail (cdw->mime_type != NULL, NULL);
405         return camel_content_type_param (cdw->mime_type, "boundary");
406 }
407
408 /**
409  * camel_multipart_get_boundary:
410  * @multipart: a #CamelMultipart object
411  *
412  * Returns the boundary
413  **/
414 const char *
415 camel_multipart_get_boundary (CamelMultipart *multipart)
416 {
417         return CMP_CLASS (multipart)->get_boundary (multipart);
418 }
419
420 static gboolean
421 is_offline (CamelDataWrapper *data_wrapper)
422 {
423         CamelMultipart *multipart = CAMEL_MULTIPART (data_wrapper);
424         GList *node;
425         CamelDataWrapper *part;
426
427         if (parent_class->is_offline (data_wrapper))
428                 return TRUE;
429         for (node = multipart->parts; node; node = node->next) {
430                 part = node->data;
431                 if (camel_data_wrapper_is_offline (part))
432                         return TRUE;
433         }
434
435         return FALSE;
436 }
437
438 /* this is MIME specific, doesn't belong here really */
439 static ssize_t
440 write_to_stream (CamelDataWrapper *data_wrapper, CamelStream *stream)
441 {
442         CamelMultipart *multipart = CAMEL_MULTIPART (data_wrapper);
443         const gchar *boundary;
444         ssize_t total = 0;
445         ssize_t count;
446         GList *node;
447
448         /* get the bundary text */
449         boundary = camel_multipart_get_boundary (multipart);
450         
451         /* we cannot write a multipart without a boundary string */
452         g_return_val_if_fail (boundary, -1);
453         
454         /*
455          * write the preface text (usually something like
456          *   "This is a mime message, if you see this, then
457          *    your mail client probably doesn't support ...."
458          */
459         if (multipart->preface) {
460                 count = camel_stream_write_string (stream, multipart->preface);
461                 if (count == -1)
462                         return -1;
463                 total += count;
464         }
465
466         /*
467          * Now, write all the parts, separated by the boundary
468          * delimiter
469          */
470         node = multipart->parts;
471         while (node) {
472                 count = camel_stream_printf (stream, "\n--%s\n", boundary);
473                 if (count == -1)
474                         return -1;
475                 total += count;
476
477                 count = camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (node->data), stream);
478                 if (count == -1)
479                         return -1;
480                 total += count;
481                 node = node->next;
482         }
483
484         /* write the terminating boudary delimiter */
485         count = camel_stream_printf (stream, "\n--%s--\n", boundary);
486         if (count == -1)
487                 return -1;
488         total += count;
489
490         /* and finally the postface */
491         if (multipart->postface) {
492                 count = camel_stream_write_string (stream, multipart->postface);
493                 if (count == -1)
494                         return -1;
495                 total += count;
496         }
497
498         return total;
499 }
500
501 /**
502  * camel_multipart_set_preface:
503  * @multipart: a #CamelMultipart object
504  * @preface: the multipart preface
505  * 
506  * Set the preface text for this multipart.  Will be written out infront
507  * of the multipart.  This text should only include US-ASCII strings, and
508  * be relatively short, and will be ignored by any MIME mail client.
509  **/
510 void
511 camel_multipart_set_preface(CamelMultipart *multipart, const char *preface)
512 {
513         if (multipart->preface != preface) {
514                 g_free(multipart->preface);
515                 if (preface)
516                         multipart->preface = g_strdup(preface);
517                 else
518                         multipart->preface = NULL;
519         }
520 }
521
522 /**
523  * camel_multipart_set_postface:
524  * @multipart: a #CamelMultipart object
525  * @postface: multipat postface
526  * 
527  * Set the postfix text for this multipart.  Will be written out after
528  * the last boundary of the multipart, and ignored by any MIME mail
529  * client.
530  *
531  * Generally postface texts should not be sent with multipart messages.
532  **/
533 void
534 camel_multipart_set_postface(CamelMultipart *multipart, const char *postface)
535 {
536         if (multipart->postface != postface) {
537                 g_free(multipart->postface);
538                 if (postface)
539                         multipart->postface = g_strdup(postface);
540                 else
541                         multipart->postface = NULL;
542         }
543 }
544
545 static int
546 construct_from_parser(CamelMultipart *multipart, struct _CamelMimeParser *mp)
547 {
548         int err;
549         CamelContentType *content_type;
550         CamelMimePart *bodypart;
551         char *buf;
552         size_t len;
553         
554         g_assert(camel_mime_parser_state(mp) == CAMEL_MIME_PARSER_STATE_MULTIPART);
555         
556         /* FIXME: we should use a came-mime-mutlipart, not jsut a camel-multipart, but who cares */
557         d(printf("Creating multi-part\n"));
558                 
559         content_type = camel_mime_parser_content_type(mp);
560         camel_multipart_set_boundary(multipart,
561                                      camel_content_type_param(content_type, "boundary"));
562         
563         while (camel_mime_parser_step(mp, &buf, &len) != CAMEL_MIME_PARSER_STATE_MULTIPART_END) {
564                 camel_mime_parser_unstep(mp);
565                 bodypart = camel_mime_part_new();
566                 camel_mime_part_construct_from_parser(bodypart, mp);
567                 camel_multipart_add_part(multipart, bodypart);
568                 camel_object_unref((CamelObject *)bodypart);
569         }
570         
571         /* these are only return valid data in the MULTIPART_END state */
572         camel_multipart_set_preface(multipart, camel_mime_parser_preface (mp));
573         camel_multipart_set_postface(multipart, camel_mime_parser_postface (mp));
574         
575         err = camel_mime_parser_errno(mp);
576         if (err != 0) {
577                 errno = err;
578                 return -1;
579         } else
580                 return 0;
581 }
582
583
584 /**
585  * camel_multipart_construct_from_parser:
586  * @multipart: a #CamelMultipart object
587  * @parser: a #CamelMimeParser object
588  *
589  * Construct a multipart from a parser.
590  *
591  * Returns %0 on success or %-1 on fail
592  **/
593 int
594 camel_multipart_construct_from_parser(CamelMultipart *multipart, struct _CamelMimeParser *mp)
595 {
596         g_return_val_if_fail(CAMEL_IS_MULTIPART(multipart), -1);
597
598         return CMP_CLASS(multipart)->construct_from_parser(multipart, mp);
599 }