Fixes NB#122697, prevent DoS attacks when replying to emails with malformed "Reply...
[modest] / src / modest-tny-msg.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31 #include <gtkhtml/gtkhtml.h>
32 #include <tny-gtk-text-buffer-stream.h>
33 #include <tny-simple-list.h>
34 #include <tny-folder.h>
35 #include <modest-runtime.h>
36 #include <modest-defs.h>
37 #include "modest-formatter.h"
38 #include <tny-camel-mem-stream.h>
39 #include <tny-camel-mime-part.h>
40 #include <glib/gprintf.h>
41 #include <modest-tny-folder.h>
42 #include "modest-tny-mime-part.h"
43 #include <modest-error.h>
44
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif /*HAVE_CONFIG_H */
49
50 #include <modest-tny-msg.h>
51 #include "modest-text-utils.h"
52
53 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
54                                     const gchar *content_type);
55 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
56 static gint add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err);
57 static void add_images (TnyMsg *msg, GList *attachments_list, GError **err);
58 static char * get_content_type(const gchar *s);
59 static gboolean is_ascii(const gchar *s);
60
61
62 TnyMsg*
63 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
64                     const gchar *bcc, const gchar* subject,
65                     const gchar *references, const gchar *in_reply_to,
66                     const gchar *body,
67                     GList *attachments, gint *attached, GError **err)
68 {
69         TnyMsg *new_msg;
70         TnyHeader *header;
71         gchar *content_type;
72         gint tmp_attached = 0;
73
74         /* Create new msg */
75         new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL), FALSE);
76         header  = tny_msg_get_header (new_msg);
77
78         if ((from != NULL) && (strlen(from) > 0)) {
79                 tny_header_set_from (TNY_HEADER (header), from);
80                 tny_header_set_replyto (TNY_HEADER (header), from);
81         }
82         if ((mailto != NULL) && (strlen(mailto) > 0)) {
83                 gchar *removed_to = modest_text_utils_remove_duplicate_addresses (mailto);
84                 tny_header_set_to (TNY_HEADER (header), removed_to);
85                 g_free (removed_to);
86         }
87         if ((cc != NULL) && (strlen(cc) > 0))
88                 tny_header_set_cc (TNY_HEADER (header), cc);
89         if ((bcc != NULL) && (strlen(bcc) > 0))
90                 tny_header_set_bcc (TNY_HEADER (header), bcc);
91
92         if ((subject != NULL) && (strlen(subject) > 0))
93                 tny_header_set_subject (TNY_HEADER (header), subject);
94
95         content_type = get_content_type(body);
96
97         /* set modest as the X-Mailer
98          * we could this in the platform factory, but then the header
99          * would show up before all the others.
100          */
101         tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
102                                        VERSION);
103
104         if (references)
105                 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
106
107         if (in_reply_to)
108                 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
109
110         /* Add the body of the new mail */
111         /* This is needed even if body is NULL or empty. */
112         add_body_part (new_msg, body, content_type);
113         g_free (content_type);
114
115         /* Add attachments */
116         if (attachments)
117                 tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
118         if (attached)
119                 *attached = tmp_attached;
120         if (header)
121                 g_object_unref(header);
122
123         return new_msg;
124 }
125
126 TnyMsg*
127 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
128                                const gchar *bcc, const gchar* subject, 
129                                const gchar *references, const gchar *in_reply_to,
130                                const gchar *html_body, const gchar *plain_body,
131                                GList *attachments, GList *images, gint *attached, GError **err)
132 {
133         TnyMsg *new_msg;
134         TnyHeader *header;
135         gchar *content_type;
136         gint tmp_attached;
137         
138         /* Create new msg */
139         new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL), (images != NULL));
140         header  = tny_msg_get_header (new_msg);
141         
142         if ((from != NULL) && (strlen(from) > 0)) {
143                 tny_header_set_from (TNY_HEADER (header), from);
144                 tny_header_set_replyto (TNY_HEADER (header), from);
145         }
146         if ((mailto != NULL) && (strlen(mailto) > 0)) 
147                 tny_header_set_to (TNY_HEADER (header), mailto);
148         if ((cc != NULL) && (strlen(cc) > 0)) 
149                 tny_header_set_cc (TNY_HEADER (header), cc);
150         if ((bcc != NULL) && (strlen(bcc) > 0)) 
151                 tny_header_set_bcc (TNY_HEADER (header), bcc);
152         
153         if ((subject != NULL) && (strlen(subject) > 0)) 
154                 tny_header_set_subject (TNY_HEADER (header), subject);
155
156         content_type = get_content_type(plain_body);
157         
158         /* set modest as the X-Mailer
159          * we could this in the platform factory, but then the header
160          * would show up before all the others.
161          */
162         tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
163                                        VERSION);
164
165         if (references)
166                 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
167
168         if (in_reply_to)
169                 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
170         
171         /* Add the body of the new mail */      
172         add_body_part (new_msg, plain_body, content_type);
173         add_html_body_part (new_msg, html_body);
174         g_free (content_type);
175                        
176         /* Add attachments */
177         tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
178         if (attached)
179                 *attached = tmp_attached;
180         add_images (new_msg, images, err);
181         if (header)
182                 g_object_unref(header);
183
184         return new_msg;
185 }
186
187
188 /* FIXME: this func copy from modest-mail-operation: refactor */
189 static TnyMimePart *
190 add_body_part (TnyMsg *msg, 
191                const gchar *body,
192                const gchar *content_type)
193 {
194         TnyMimePart *text_body_part = NULL;
195         TnyStream *text_body_stream;
196
197         /* Create the stream */
198         text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
199                                         (body, (body ? strlen(body) : 0)));
200
201         text_body_part = modest_formatter_create_body_part (NULL, msg);
202
203         /* Construct MIME part */
204         tny_stream_reset (text_body_stream);
205         tny_mime_part_construct (text_body_part,
206                                  text_body_stream,
207                                  content_type, "7bit");
208         tny_stream_reset (text_body_stream);
209
210         g_object_unref (G_OBJECT(text_body_part));
211
212         /* Clean */
213         g_object_unref (text_body_stream);
214
215         return text_body_part;
216 }
217
218 static TnyMimePart *
219 add_html_body_part (TnyMsg *msg, 
220                     const gchar *body)
221 {
222         TnyMimePart *html_body_part = NULL;
223         TnyStream *html_body_stream;
224
225         /* Create the stream */
226         html_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
227                                         (body, strlen(body)));
228
229         /* Create body part if needed */
230         html_body_part = modest_formatter_create_body_part (NULL, msg);
231
232         /* Construct MIME part */
233         tny_stream_reset (html_body_stream);
234         tny_mime_part_construct (html_body_part,
235                                  html_body_stream,
236                                  "text/html; charset=utf-8", 
237                                  "7bit"); /* Sometimes it might be needed 
238                                              to make this one a 8bit! */
239         tny_stream_reset (html_body_stream);
240
241         g_object_unref (G_OBJECT(html_body_part));
242
243         /* Clean */
244         g_object_unref (html_body_stream);
245
246         return html_body_part;
247 }
248
249 static TnyMimePart *
250 copy_mime_part (TnyMimePart *part, GError **err)
251 {
252         TnyMimePart *result = NULL;
253         const gchar *attachment_content_type;
254         const gchar *attachment_filename;
255         const gchar *attachment_cid;
256         TnyList *parts;
257         TnyIterator *iterator;
258         TnyStream *attachment_stream;
259         const gchar *enc;
260         gint ret;
261         
262         if (TNY_IS_MSG (part)) {
263                 g_object_ref (part);
264                 return part;
265         }
266
267         result = tny_platform_factory_new_mime_part (
268                 modest_runtime_get_platform_factory());
269
270         attachment_content_type = tny_mime_part_get_content_type (part);
271
272         /* get mime part headers */
273         attachment_filename = tny_mime_part_get_filename (part);
274         attachment_cid = tny_mime_part_get_content_id (part);
275         
276         /* fill the stream */
277         attachment_stream = tny_mime_part_get_decoded_stream (part);
278         enc = tny_mime_part_get_transfer_encoding (part);
279         if (attachment_stream == NULL) {
280                 if (err != NULL && *err == NULL)
281                         g_set_error (err, MODEST_MAIL_OPERATION_ERROR, MODEST_MAIL_OPERATION_ERROR_FILE_IO, _("TODO: couldn't retrieve attachment"));
282                 g_object_unref (result);
283                 return NULL;
284         } else {
285                 ret = tny_stream_reset (attachment_stream);
286                 ret = tny_mime_part_construct (result,
287                                                attachment_stream,
288                                                attachment_content_type, 
289                                                enc);
290                 ret = tny_stream_reset (attachment_stream);
291         }
292         
293         /* set other mime part fields */
294         tny_mime_part_set_filename (result, attachment_filename);
295         tny_mime_part_set_content_id (result, attachment_cid);
296
297         /* copy subparts */
298         parts = tny_simple_list_new ();
299         tny_mime_part_get_parts (part, parts);
300         iterator = tny_list_create_iterator (parts);
301         while (!tny_iterator_is_done (iterator)) {
302                 TnyMimePart *subpart = TNY_MIME_PART (tny_iterator_get_current (iterator));
303                 if (subpart) {
304                         const gchar *subpart_cid;
305                         TnyMimePart *subpart_copy = copy_mime_part (subpart, err);
306                         if (subpart_copy != NULL) {
307                                 subpart_cid = tny_mime_part_get_content_id (subpart);
308                                 tny_mime_part_add_part (result, subpart_copy);
309                                 if (subpart_cid)
310                                         tny_mime_part_set_content_id (result, subpart_cid);
311                                 g_object_unref (subpart_copy);
312                         }
313                         g_object_unref (subpart);
314                 }
315
316                 tny_iterator_next (iterator);
317         }
318         g_object_unref (iterator);
319         g_object_unref (parts);
320         g_object_unref (attachment_stream);
321
322         return result;
323 }
324
325 static gint
326 add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err)
327 {
328         GList *pos;
329         TnyMimePart *attachment_part, *old_attachment;
330         gint ret;
331         gint attached = 0;
332
333         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
334
335                 old_attachment = pos->data;
336                 if (!tny_mime_part_is_purged (old_attachment)) {
337                         gchar *old_cid;
338                         old_cid = g_strdup (tny_mime_part_get_content_id (old_attachment));
339                         attachment_part = copy_mime_part (old_attachment, err);
340                         if (attachment_part != NULL) {
341                                 if (add_inline) {
342                                         tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
343                                                                        "inline");
344                                 } else {
345                                         const gchar *filename;
346                                         filename = tny_mime_part_get_filename (old_attachment);
347                                         if (filename)
348                                                 tny_mime_part_set_filename (attachment_part, filename);
349                                         else
350                                                 tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
351                                                                                "attachment");
352                                 }
353                                 if (!TNY_IS_MSG (old_attachment))  {
354                                         tny_mime_part_set_transfer_encoding (TNY_MIME_PART (attachment_part), "base64");
355                                 }
356                                 ret = tny_mime_part_add_part (TNY_MIME_PART (part), attachment_part);
357                                 attached++;
358                                 if (old_cid)
359                                         tny_mime_part_set_content_id (attachment_part, old_cid);
360                                 g_object_unref (attachment_part);
361                         }
362                         g_free (old_cid);
363                 }
364         }
365         return attached;
366 }
367
368 static void
369 add_images (TnyMsg *msg, GList *images_list, GError **err)
370 {
371         TnyMimePart *related_part = NULL;
372         const gchar *content_type;
373
374         content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
375
376         if ((content_type != NULL) && !strcasecmp (content_type, "multipart/related")) {
377                 related_part = g_object_ref (msg);
378         } else if ((content_type != NULL) && !strcasecmp (content_type, "multipart/mixed")) {
379                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
380                 TnyIterator *iter = NULL;
381                 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
382                 iter = tny_list_create_iterator (parts);
383
384                 while (!tny_iterator_is_done (iter)) {
385                         TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
386                         if (part && !g_ascii_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
387                                 related_part = part;
388                                 break;
389                         }
390                         if (part)
391                                 g_object_unref (part);
392                         tny_iterator_next (iter);
393                 }
394                 g_object_unref (iter);
395                 g_object_unref (parts);
396         }
397
398         if (related_part != NULL) {
399                 /* TODO: attach images in their proper place */
400                 add_attachments (related_part, images_list, TRUE, err);
401                 g_object_unref (related_part);
402         }
403 }
404
405
406 gchar * 
407 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html, gboolean *is_html)
408 {
409         TnyStream *stream;
410         TnyMimePart *body;
411         GtkTextBuffer *buf;
412         GtkTextIter start, end;
413         gchar *to_quote;
414         gboolean result_was_html = TRUE;
415
416         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
417         
418         body = modest_tny_msg_find_body_part(msg, want_html);
419         if (!body)
420                 return NULL;
421
422         buf = gtk_text_buffer_new (NULL);
423         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
424         tny_stream_reset (stream);
425         tny_mime_part_decode_to_stream (body, stream, NULL);
426         tny_stream_reset (stream);
427         
428         gtk_text_buffer_get_bounds (buf, &start, &end);
429         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
430         if (tny_mime_part_content_type_is (body, "text/plain")) {
431                 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
432                 g_free (to_quote);
433                 to_quote = to_quote_converted;
434                 result_was_html = FALSE;
435         }
436
437         g_object_unref (buf);
438         g_object_unref (G_OBJECT(stream));
439         g_object_unref (G_OBJECT(body));
440
441         if (is_html != NULL)
442                 *is_html = result_was_html;
443
444         return to_quote;
445 }
446
447
448 static TnyMimePart*
449 modest_tny_msg_find_body_part_in_alternative (TnyMimePart *msg, gboolean want_html)
450 {
451         TnyList *parts;
452         TnyIterator *iter;
453         TnyMimePart *part = NULL;
454         TnyMimePart *first_part = NULL;
455         const gchar *desired_mime_type = want_html ? "text/html" : "text/plain";
456
457         parts = TNY_LIST (tny_simple_list_new());
458         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
459
460         for (iter  = tny_list_create_iterator(parts); 
461              !tny_iterator_is_done (iter);
462              tny_iterator_next (iter)) {
463                 gchar *content_type;
464                 gboolean is_body;
465
466                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
467
468                 if (first_part == NULL) {
469                         g_object_ref (part);
470                         first_part = part;
471                 }
472
473                 is_body = FALSE;
474                 content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
475                 is_body = g_str_has_prefix (content_type, desired_mime_type);
476                 if (is_body)
477                         break;
478
479                 g_object_unref (part);
480                 part = NULL;
481
482         }
483         g_object_unref (iter);
484         g_object_unref (parts);
485
486         if (part == NULL) {
487                 return first_part;
488         } else {
489                 if (first_part) g_object_unref (first_part);
490                 return part;
491         }
492 }
493
494 TnyMimePart*
495 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
496 {
497         TnyMimePart *part = NULL;
498         TnyList *parts = NULL;
499         TnyIterator *iter = NULL;
500         gchar *header_content_type;
501         gchar *header_content_type_lower = NULL;
502         
503         if (!msg)
504                 return NULL;
505
506         /* If it's an application multipart, then we don't get into as we don't
507          * support them (for example application/sml or wap messages */
508         header_content_type = modest_tny_mime_part_get_header_value (msg, "Content-Type");
509         if (header_content_type) {
510                 header_content_type = g_strstrip (header_content_type);
511                 header_content_type_lower = g_ascii_strdown (header_content_type, -1);
512         }
513         if (header_content_type_lower && 
514             g_str_has_prefix (header_content_type_lower, "multipart/") &&
515             !g_str_has_prefix (header_content_type_lower, "multipart/signed") &&
516             strstr (header_content_type_lower, "application/")) {
517                 g_free (header_content_type_lower);
518                 g_free (header_content_type);
519                 return NULL;
520         }       
521         if (header_content_type_lower && 
522             g_str_has_prefix (header_content_type_lower, "multipart/alternative")) {
523                 g_free (header_content_type_lower);
524                 g_free (header_content_type);
525                 return modest_tny_msg_find_body_part_in_alternative (msg, want_html);
526         }       
527         g_free (header_content_type_lower);
528         g_free (header_content_type);
529
530         parts = TNY_LIST (tny_simple_list_new());
531         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
532
533         iter  = tny_list_create_iterator(parts);
534
535         /* no parts? assume it's single-part message */
536         if (tny_iterator_is_done(iter)) {
537                 gchar *content_type;
538                 gboolean is_text_part;
539                 g_object_unref (G_OBJECT(iter));
540                 content_type = modest_tny_mime_part_get_content_type (msg);
541                 if (content_type == NULL)
542                         return NULL;
543                 is_text_part = 
544                         g_str_has_prefix (content_type, "text/");
545                 g_free (content_type);
546                 /* if this part cannot be a supported body return NULL */
547                 if (!is_text_part) {
548                         return NULL;
549                 } else {
550                         return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
551                 }
552         } else {
553                 do {
554                         gchar *tmp, *content_type = NULL;
555                         gboolean has_content_disp_name = FALSE;
556
557                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
558
559                         if (!part) {
560                                 g_warning ("%s: not a valid mime part", __FUNCTION__);
561                                 tny_iterator_next (iter);
562                                 continue;
563                         }
564
565                         /* it's a message --> ignore */
566                         if (part && TNY_IS_MSG (part)) {
567                                 g_object_unref (part);
568                                 part = NULL;
569                                 tny_iterator_next (iter);
570                                 continue;
571                         }                       
572
573                         /* we need to strdown the content type, because
574                          * tny_mime_part_has_content_type does not do it...
575                          */
576                         content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
577                         
578                         /* mime-parts with a content-disposition header (either 'inline' or 'attachment')
579                          * and a 'name=' thingy cannot be body parts
580                          */
581                                 
582                         tmp = modest_tny_mime_part_get_header_value (part, "Content-Disposition");
583                         if (tmp) {
584                                 gchar *content_disp = g_ascii_strdown(tmp, -1);
585                                 g_free (tmp);
586                                 has_content_disp_name = g_strstr_len (content_disp, strlen(content_disp), "name=") != NULL;
587                                 g_free (content_disp);
588                         }
589                         
590                         if (g_str_has_prefix (content_type, "text/") && 
591                             !has_content_disp_name &&
592                             !modest_tny_mime_part_is_attachment_for_modest (part)) {
593                                 /* we found the body. Doesn't have to be the desired mime part, first
594                                    text/ part in a mixed is the body */
595                                 g_free (content_type);
596                                 break;
597
598                         } else if (g_str_has_prefix(content_type, "multipart/alternative")) {
599
600                                 /* multipart? recurse! */
601                                 g_object_unref (part);
602                                 g_free (content_type);
603                                 part = modest_tny_msg_find_body_part_in_alternative (part, want_html);
604                                 if (part)
605                                         break;
606
607                         } else  if (g_str_has_prefix(content_type, "multipart")) {
608
609                                 /* multipart? recurse! */
610                                 g_object_unref (part);
611                                 g_free (content_type);
612                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
613                                 if (part)
614                                         break;
615                         } else
616                                 g_free (content_type);
617                         
618                         if (part) {
619                                 g_object_unref (G_OBJECT(part));
620                                 part = NULL;
621                         }
622                         
623                         tny_iterator_next (iter);
624                         
625                 } while (!tny_iterator_is_done(iter));
626         }
627         
628         g_object_unref (G_OBJECT(iter));
629         g_object_unref (G_OBJECT(parts));
630
631         return part; /* this maybe NULL, this is not an error; some message just don't have a body
632                       * part */
633 }
634
635
636 TnyMimePart*
637 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
638 {
639         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
640         
641         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
642                                                              want_html);
643 }
644
645
646 #define MODEST_TNY_MSG_PARENT_UID "parent-uid"
647
648 static TnyMsg *
649 create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
650                            const gchar *signature, gboolean is_reply,
651                            guint type /*ignored*/, GList *attachments)
652 {
653         TnyMsg *new_msg;
654         TnyHeader *new_header;
655         gchar *old_subject;
656         gchar *new_subject;
657         TnyMimePart *body = NULL;
658         TnyMimePart *html_body = NULL;
659         ModestFormatter *formatter;
660         gchar *subject_prefix;
661         gboolean no_text_part;
662         gchar *parent_uid;
663         gboolean forward_as_attach = FALSE;
664
665         if (header)
666                 g_object_ref (header);
667         else
668                 header = tny_msg_get_header (msg);
669
670         /* Get body from original msg. Always look for the text/plain
671            part of the message to create the reply/forwarded mail */
672         if (msg != NULL) {
673                 body   = modest_tny_msg_find_body_part (msg, FALSE);
674                 html_body = modest_tny_msg_find_body_part (msg, TRUE);
675         }
676
677         if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT,
678                                   NULL))
679                 formatter = modest_formatter_new ("text/html", signature);
680         else
681                 formatter = modest_formatter_new ("text/plain", signature);
682
683
684         /* if we don't have a text-part */
685         no_text_part = (!body) || (strcmp (tny_mime_part_get_content_type (body), "text/html")==0);
686
687         /* when we're reply, include the text part if we have it, or nothing otherwise. */
688         if (is_reply)
689                 new_msg = modest_formatter_quote  (formatter, body, header,
690                                                     attachments);
691         else {
692                 if (no_text_part || (html_body && (strcmp (tny_mime_part_get_content_type (html_body), "text/html")==0))) {
693                         forward_as_attach = TRUE;
694                         new_msg = modest_formatter_attach (formatter, msg, header);
695                 } else {
696                         forward_as_attach = FALSE;
697                         new_msg = modest_formatter_inline  (formatter, body, header,
698                                                             attachments);
699                 }
700         }
701
702         g_object_unref (G_OBJECT(formatter));
703         if (body)
704                 g_object_unref (G_OBJECT(body));
705         if (html_body)
706                 g_object_unref (G_OBJECT(html_body));
707
708         /* Fill the header */
709         new_header = tny_msg_get_header (new_msg);
710         tny_header_set_from (new_header, from);
711         tny_header_set_replyto (new_header, from);
712
713         /* Change the subject */
714         if (is_reply)
715                 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
716         else
717                 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
718         old_subject = tny_header_dup_subject (header);
719         new_subject =
720                 (gchar *) modest_text_utils_derived_subject (old_subject,
721                                                              subject_prefix);
722         g_free (old_subject);
723         g_free (subject_prefix);
724         tny_header_set_subject (new_header, (const gchar *) new_subject);
725         g_free (new_subject);
726
727         /* get the parent uid, and set it as a gobject property on the new msg */
728         parent_uid = modest_tny_folder_get_header_unique_id (header);
729         g_object_set_data_full (G_OBJECT(new_msg), MODEST_TNY_MSG_PARENT_UID,
730                                 parent_uid, g_free);
731
732         /* set modest as the X-Mailer
733          * we could this in the platform factory, but then the header
734          * would show up before all the others.
735          */
736         tny_mime_part_set_header_pair (TNY_MIME_PART (msg), "X-Mailer", "Modest "
737                                        VERSION);
738
739         /* Clean */
740         g_object_unref (G_OBJECT (new_header));
741         g_object_unref (G_OBJECT (header));
742         /* ugly to unref it here instead of in the calling func */
743
744         if (!is_reply & !forward_as_attach) {
745                 add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, NULL);
746         }
747
748         return new_msg;
749 }
750
751 const gchar*
752 modest_tny_msg_get_parent_uid (TnyMsg *msg)
753 {
754         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
755         
756         return g_object_get_data (G_OBJECT(msg), MODEST_TNY_MSG_PARENT_UID);
757 }
758
759 static gchar *get_signed_protocol (TnyMimePart *part)
760 {
761         TnyList *header_pairs;
762         TnyIterator *iterator;
763         gchar *result = NULL;
764
765         header_pairs = TNY_LIST (tny_simple_list_new ());
766         tny_mime_part_get_header_pairs (part, header_pairs);
767         iterator = tny_list_create_iterator (header_pairs);
768
769         while (!result && !tny_iterator_is_done (iterator)) {
770                 TnyPair *pair;
771                 const gchar *name;
772
773                 pair = TNY_PAIR (tny_iterator_get_current (iterator));
774                 name = tny_pair_get_name (pair);
775                 if (name && !g_ascii_strcasecmp (name, "Content-Type")) {
776                         const gchar *s;
777                         s = tny_pair_get_value (pair);
778                         if (s) {
779                                 s = strstr (s, "protocol=");
780                                 if (s) {
781                                         const gchar *t;
782                                         s += 9;
783                                         if (*s == '\"') {
784                                                 s++;
785                                                 t = strstr (s, "\"");
786                                         } else {
787                                                 t = strstr (s, ";");
788                                         }
789                                         result = g_strndup (s, t - s);
790                                 }
791                         }
792                 }
793
794                 g_object_unref (pair);
795                 tny_iterator_next (iterator);
796         }
797
798         g_object_unref (iterator);
799         g_object_unref (header_pairs);
800
801         return result;
802 }
803
804 TnyMimePart *
805 modest_tny_msg_get_attachments_parent (TnyMsg *msg)
806 {
807         TnyMimePart *result;
808         const gchar *content_type;
809
810         result = NULL;
811
812         content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
813         if (content_type && !strcmp (content_type, "multipart/signed")) {
814                 TnyList *msg_children;
815                 TnyIterator *iterator;
816                 gchar *signed_protocol;
817
818                 msg_children = TNY_LIST (tny_simple_list_new ());
819                 tny_mime_part_get_parts (TNY_MIME_PART (msg), msg_children);
820
821                 iterator = tny_list_create_iterator (msg_children);
822                 signed_protocol = get_signed_protocol (TNY_MIME_PART (msg));
823                         
824                 while (!result && !tny_iterator_is_done (iterator)) {
825                         TnyMimePart *part;
826
827                         part = TNY_MIME_PART (tny_iterator_get_current (iterator));
828                         if (signed_protocol) {
829                                 const gchar *part_content_type;
830
831                                 part_content_type = tny_mime_part_get_content_type (part);
832                                 if (part_content_type && g_ascii_strcasecmp (part_content_type, signed_protocol)) {
833                                         result = g_object_ref (part);
834                                 }
835                         } else {
836                                 result = g_object_ref (part);
837                         }
838
839                         g_object_unref (part);
840                         tny_iterator_next (iterator);
841                 }
842
843                 g_object_unref (iterator);
844                 g_free (signed_protocol);
845                 g_object_unref (msg_children);
846         }
847         if (result == NULL) {
848                 result = g_object_ref (msg);
849         }
850
851         return result;
852 }
853
854
855
856 static void
857 add_if_attachment (gpointer data, gpointer user_data)
858 {
859         TnyMimePart *part;
860         GList **attachments_list;
861
862         part = TNY_MIME_PART (data);
863         attachments_list = ((GList **) user_data);
864
865         if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part))) {
866                 *attachments_list = g_list_prepend (*attachments_list, part);
867                 g_object_ref (part);
868         }
869 }
870
871 TnyMsg* 
872 modest_tny_msg_create_forward_msg (TnyMsg *msg, 
873                                    const gchar *from,
874                                    const gchar *signature,
875                                    ModestTnyMsgForwardType forward_type)
876 {
877         TnyMsg *new_msg;
878         TnyList *parts = NULL;
879         GList *attachments_list = NULL;
880         TnyMimePart *part_to_check = NULL;
881
882         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
883
884         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
885         
886         /* Add attachments */
887         parts = TNY_LIST (tny_simple_list_new());
888         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
889         tny_list_foreach (parts, add_if_attachment, &attachments_list);
890
891         new_msg = create_reply_forward_mail (msg, NULL, from, signature, FALSE, forward_type,
892                                              attachments_list);
893
894         /* Clean */
895         if (attachments_list) {
896                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
897                 g_list_free (attachments_list);
898         }
899         g_object_unref (G_OBJECT (parts));
900         g_object_unref (part_to_check);
901
902         return new_msg;
903 }
904
905
906
907 static gint
908 count_addresses (const gchar* addresses)
909 {
910         gint count = 1;
911
912         if (!addresses)
913                 return 0;
914         
915         while (*addresses) {
916                 if (*addresses == ',' || *addresses == ';')
917                         ++count;
918                 ++addresses;
919         }
920         
921         return count;
922 }
923
924 static void
925 remove_undisclosed_recipients (gchar **recipients)
926 {
927         GSList *addresses, *node;
928         gboolean is_first;
929         GString *result;
930
931         g_return_if_fail (recipients);
932         addresses = modest_text_utils_split_addresses_list (*recipients);
933
934         is_first = TRUE;
935         result = g_string_new ("");
936         for (node = addresses; node != NULL; node = g_slist_next (node)) {
937                 const gchar *address = (const gchar *) node->data;
938
939                 if (address && strstr (address, "undisclosed-recipients"))
940                         continue;
941
942                 if (is_first)
943                         is_first = FALSE;
944                 else
945                         result = g_string_append (result, ", ");
946
947                 result = g_string_append (result, address);
948         }
949         g_slist_foreach (addresses, (GFunc)g_free, NULL);
950         g_slist_free (addresses);
951
952         g_free (*recipients);
953         *recipients = g_string_free (result, FALSE);
954 }
955
956
957 /* get the new To:, based on the old header,
958  * result is newly allocated or NULL in case of error
959  * */
960 static gchar*
961 get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
962             ModestTnyMsgReplyMode reply_mode)
963 {
964         const gchar *reply_header = "Reply-To:";
965         const gchar *from_header = "From:";
966         gchar* old_reply_to;
967         gchar* old_from;
968         gchar* new_to;
969         gchar* tmp;
970         
971         /* according to RFC2369 (http://www.faqs.org/rfcs/rfc2369.html), we
972          * can identify Mailing-List posts by the List-Help header.
973          * for mailing lists, both the Reply-To: and From: should be included
974          * in the new To:; for now, we're ignoring List-Post
975          */
976         gchar* list_help = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
977                                                                   "List-Help");
978         gboolean is_mailing_list = (list_help != NULL);
979         g_free (list_help);
980
981
982         /* reply to sender, use ReplyTo or From */
983         old_reply_to = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
984                                                               "Reply-To"); 
985         old_from     = tny_header_dup_from (header);
986
987         if (!old_from && !old_reply_to) {
988                 g_debug ("%s: failed to get either Reply-To: or From: from header",
989                            __FUNCTION__);
990                 return NULL;
991         }
992
993         /* Prevent DoS attacks caused by malformed emails */
994         if (old_from)
995                 old_from = modest_text_utils_get_secure_header (old_from,
996                                                                 from_header);
997         if (old_reply_to)
998                 old_reply_to = modest_text_utils_get_secure_header (old_reply_to,
999                                                                     reply_header);
1000
1001         /* for mailing lists, use both Reply-To and From if we did a
1002          * 'Reply All:'
1003          * */
1004         if (is_mailing_list && reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL &&
1005             old_reply_to && old_from && strcmp (old_from, old_reply_to) != 0)
1006                 new_to = g_strjoin (",", old_reply_to, old_from, NULL);
1007         else
1008                 /* otherwise use either Reply-To: (preferred) or From: */
1009                 new_to = g_strdup (old_reply_to ? old_reply_to : old_from);
1010         g_free (old_from);
1011         g_free (old_reply_to);
1012
1013         /* in case of ReplyAll, we need to add the Recipients in the old To: */
1014         if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
1015                 gchar *old_to = tny_header_dup_to (header);
1016                 if (!old_to) 
1017                         g_debug ("%s: no To: address found in source mail",
1018                                    __FUNCTION__);
1019                 else {
1020                         /* append the old To: */
1021                         gchar *tmp = g_strjoin (",", new_to, old_to, NULL);
1022                         g_free (new_to);
1023                         new_to = tmp;
1024                         g_free (old_to);
1025                 }
1026
1027                 /* remove duplicate entries */
1028                 gchar *tmp = modest_text_utils_remove_duplicate_addresses (new_to);
1029                 g_free (new_to);
1030                 new_to = tmp;
1031                 
1032                 /* now, strip me (the new From:) from the new_to, but only if
1033                  * there are >1 addresses there */
1034                 if (count_addresses (new_to) > 1) {
1035                         gchar *tmp = modest_text_utils_remove_address (new_to, from);
1036                         g_free (new_to);
1037                         new_to = tmp;
1038                 }
1039         }
1040
1041         tmp = modest_text_utils_simplify_recipients (new_to);
1042         remove_undisclosed_recipients  (&tmp);
1043         g_free (new_to);
1044         new_to = tmp;
1045
1046         return new_to;
1047 }
1048
1049
1050 /* get the new Cc:, based on the old header,
1051  * result is newly allocated or NULL in case of error */
1052 static gchar*
1053 get_new_cc (TnyHeader *header, const gchar* from, const gchar *new_to)
1054 {
1055         gchar *old_cc, *result, *dup;
1056
1057         old_cc = tny_header_dup_cc (header);
1058         if (!old_cc)
1059                 return NULL;
1060
1061         /* remove me (the new From:) from the Cc: list */
1062         dup =  modest_text_utils_remove_address (old_cc, from);
1063
1064         if (new_to) {
1065                 gchar **to_parts, **current;
1066
1067                 to_parts = g_strsplit (new_to, ",", 0);
1068                 for (current = to_parts; current && *current != '\0'; current++) {
1069                         gchar *dup2;
1070
1071                         dup2 = modest_text_utils_remove_address (dup, g_strstrip (*current));
1072                         g_free (dup);
1073                         dup = dup2;
1074                 }
1075                 g_strfreev (to_parts);
1076         }
1077
1078         result = modest_text_utils_remove_duplicate_addresses (dup);
1079         g_free (dup);
1080         dup = result;
1081         result = modest_text_utils_simplify_recipients (dup);
1082         remove_undisclosed_recipients  (&result);
1083         g_free (dup);
1084         g_free (old_cc);
1085         return result;
1086 }
1087
1088 void 
1089 modest_tny_msg_get_references (TnyMsg *msg, gchar **message_id, gchar **references, gchar **in_reply_to)
1090 {
1091         TnyList *headers;
1092         TnyIterator *iterator;
1093         gchar *l_message_id;
1094         gchar *l_references;
1095         gchar *l_in_reply_to;
1096
1097         g_return_if_fail (TNY_IS_MSG (msg));
1098
1099         l_message_id = NULL;
1100         l_references = NULL;
1101         l_in_reply_to = NULL;
1102
1103         headers = TNY_LIST (tny_simple_list_new ());
1104         tny_mime_part_get_header_pairs (TNY_MIME_PART (msg), headers);
1105
1106         iterator = tny_list_create_iterator (headers);
1107         while (!tny_iterator_is_done (iterator)) {
1108                 TnyPair *pair;
1109                 const gchar *name;
1110
1111                 pair = TNY_PAIR (tny_iterator_get_current (iterator));
1112                 name = tny_pair_get_name (pair);
1113                 if (!g_ascii_strcasecmp (name, "References")) {
1114                         if (l_references) g_free (l_references);
1115                         l_references = g_strdup (tny_pair_get_value (pair));
1116                 } else if (!g_ascii_strcasecmp (name, "In-Reply-To")) {
1117                         if (l_in_reply_to) g_free (l_in_reply_to);
1118                         l_in_reply_to = g_strdup (tny_pair_get_value (pair));
1119                 } else if (!g_ascii_strcasecmp (name, "Message-ID")) {
1120                         if (l_message_id) g_free (l_message_id);
1121                         l_message_id = g_strdup (tny_pair_get_value (pair));
1122                 }
1123
1124                 g_object_unref (pair);
1125                 tny_iterator_next (iterator);
1126         }
1127
1128         g_object_unref (iterator);
1129         g_object_unref (headers);
1130
1131         if (message_id) {
1132                 *message_id = l_message_id;
1133         } else {
1134                 g_free (l_message_id);
1135         }
1136
1137         if (in_reply_to) {
1138                 *in_reply_to = l_in_reply_to;
1139         } else {
1140                 g_free (l_in_reply_to);
1141         }
1142
1143         if (references) {
1144                 *references = l_references;
1145         } else {
1146                 g_free (l_references);
1147         }
1148 }
1149
1150 static void 
1151 set_references (TnyMsg *reply_msg, TnyMsg *original_msg)
1152 {
1153         gchar *orig_references, *orig_in_reply_to, *orig_message_id;
1154         gchar *references, *in_reply_to;
1155
1156         modest_tny_msg_get_references (original_msg, &orig_message_id, &orig_references, &orig_in_reply_to);
1157
1158         references = NULL;
1159         in_reply_to = NULL;
1160
1161         if (orig_message_id)
1162                 in_reply_to = g_strdup (orig_message_id);
1163
1164         if (orig_references) {
1165                 if (orig_message_id)
1166                         references = g_strconcat (orig_references, "\n        ", orig_message_id, NULL);
1167                 else
1168                         references = g_strdup (orig_references);
1169
1170         } else if (orig_in_reply_to) {
1171                 if (orig_message_id)
1172                         references = g_strconcat (orig_in_reply_to, "\n        ", orig_message_id, NULL);
1173                 else
1174                         references = g_strdup (orig_in_reply_to);
1175         } else if (orig_message_id) {
1176                 references = g_strdup (orig_message_id);
1177         }
1178
1179         g_free (orig_references);
1180         g_free (orig_in_reply_to);
1181         g_free (orig_message_id);
1182
1183         if (in_reply_to) {
1184                 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "In-Reply-To", in_reply_to);
1185         }
1186         if (references) {
1187                 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "References", references);
1188         }
1189 }
1190
1191 TnyMsg* 
1192 modest_tny_msg_create_reply_msg (TnyMsg *msg,
1193                                  TnyHeader *header,
1194                                  const gchar *from,
1195                                  const gchar *signature,
1196                                  ModestTnyMsgReplyType reply_type,
1197                                  ModestTnyMsgReplyMode reply_mode)
1198 {
1199         TnyMsg *new_msg = NULL;
1200         TnyHeader *new_header;
1201         gchar *new_to = NULL;
1202         TnyList *parts = NULL;
1203         GList *attachments_list = NULL;
1204         TnyMimePart *part_to_check = NULL;
1205
1206         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
1207
1208         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
1209
1210         parts = TNY_LIST (tny_simple_list_new());
1211         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
1212         tny_list_foreach (parts, add_if_attachment, &attachments_list);
1213
1214         new_msg = create_reply_forward_mail (msg, header, from, signature, TRUE, reply_type,
1215                                              attachments_list);
1216
1217         set_references (new_msg, msg);
1218         if (attachments_list != NULL) {
1219                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
1220                 g_list_free (attachments_list);
1221         }
1222         g_object_unref (parts);
1223
1224         /* Fill the header */
1225         if (header)
1226                 g_object_ref (header);
1227         else
1228                 header = tny_msg_get_header (msg);
1229
1230         
1231         new_header = tny_msg_get_header(new_msg);
1232         new_to = get_new_to (msg, header, from, reply_mode);
1233         if (!new_to)
1234                 g_debug ("%s: failed to get new To:", __FUNCTION__);
1235         else {
1236                 tny_header_set_to (new_header, new_to);
1237         }
1238
1239         if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
1240                 gchar *new_cc = get_new_cc (header, from, new_to);
1241                 if (new_cc) { 
1242                         tny_header_set_cc (new_header, new_cc);
1243                         g_free (new_cc);
1244                 }
1245         }
1246
1247         if (new_to)
1248                 g_free (new_to);
1249
1250         /* Clean */
1251         g_object_unref (G_OBJECT (new_header));
1252         g_object_unref (G_OBJECT (header));
1253         g_object_unref (G_OBJECT (part_to_check));
1254
1255         return new_msg;
1256 }
1257
1258
1259 static gboolean
1260 is_ascii(const gchar *s)
1261 {
1262         if (!s)
1263                 return TRUE;
1264         while (s[0]) {
1265                 if (s[0] & 128 || s[0] < 32)
1266                         return FALSE;
1267                 s++;
1268         }
1269         return TRUE;
1270 }
1271
1272 static char *
1273 get_content_type(const gchar *s)
1274 {
1275         GString *type;
1276         
1277         type = g_string_new("text/plain");
1278         if (!is_ascii(s)) {
1279                 if (g_utf8_validate(s, -1, NULL)) {
1280                         g_string_append(type, "; charset=\"utf-8\"");
1281                 } else {
1282                         /* it should be impossible to reach this, but better safe than sorry */
1283                         g_debug("invalid utf8 in message");
1284                         g_string_append(type, "; charset=\"latin1\"");
1285                 }
1286         }
1287         return g_string_free(type, FALSE);
1288 }
1289
1290 guint64
1291 modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
1292                               guint64 parts_count,
1293                               guint64 parts_size)
1294 {
1295         guint64 result;
1296
1297         /* estimation of headers size */
1298         result = 1024;
1299
1300         /* We add a 20% of size due to the increase in 7bit encoding */
1301         if (plain_body) {
1302                 result += strlen (plain_body) * 120 / 100;
1303         }
1304         if (html_body) {
1305                 result += strlen (html_body) * 120 / 100;
1306         }
1307
1308         /* 256 bytes per additional part because of their headers */
1309         result += parts_count * 256;
1310
1311         /* 150% of increase per encoding */
1312         result += parts_size * 3 / 2;
1313
1314         return result;
1315 }
1316
1317 GSList *
1318 modest_tny_msg_header_get_all_recipients_list (TnyHeader *header)
1319 {
1320         GSList *recipients = NULL;
1321         gchar *from = NULL, *to = NULL, *cc = NULL, *bcc = NULL;
1322         gchar *after_remove;
1323         GString *buffer;
1324         gboolean add_separator = TRUE;
1325
1326         if (header == NULL)
1327                 return NULL;
1328
1329         buffer = g_string_new ("");
1330
1331         from = tny_header_dup_from (header);
1332         to = tny_header_dup_to (header);
1333         cc = tny_header_dup_cc (header);
1334         bcc = tny_header_dup_bcc (header);
1335
1336         recipients = NULL;
1337         if (from) {
1338                 buffer = g_string_append (buffer, from);
1339                 add_separator = TRUE;
1340         }
1341         if (to) {
1342                 if (add_separator)
1343                         buffer = g_string_append (buffer, "; ");
1344                 else
1345                         add_separator = TRUE;
1346
1347                 buffer = g_string_append (buffer, to);
1348         }
1349         if (cc) {
1350                 if (add_separator)
1351                         buffer = g_string_append (buffer, "; ");
1352                 else
1353                         add_separator = TRUE;
1354
1355                 buffer = g_string_append (buffer, cc);
1356         }
1357         if (bcc) {
1358                 if (add_separator)
1359                         buffer = g_string_append (buffer, "; ");
1360                 else
1361                         add_separator = TRUE;
1362
1363                 buffer = g_string_append (buffer, bcc);
1364         }
1365
1366         after_remove = modest_text_utils_remove_duplicate_addresses (buffer->str);
1367         g_string_free (buffer, TRUE);
1368
1369         recipients = modest_text_utils_split_addresses_list (after_remove);
1370         g_free (after_remove);
1371
1372         return recipients;
1373 }
1374
1375 GSList *
1376 modest_tny_msg_get_all_recipients_list (TnyMsg *msg)
1377 {
1378         TnyHeader *header = NULL;
1379         GSList *recipients = NULL;
1380
1381         if (msg == NULL)
1382                 return NULL;
1383
1384         header = tny_msg_get_header (msg);
1385         if (header == NULL)
1386                 return NULL;
1387
1388         recipients = modest_tny_msg_header_get_all_recipients_list (header);
1389         g_object_unref (header);
1390
1391         return recipients;
1392 }
1393