Remove addresses already in To: on creatin ReplyToAll Cc: (fixes NB#125907)
[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                         const gchar *old_cid;
338                         old_cid = 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                 }
363         }
364         return attached;
365 }
366
367 static void
368 add_images (TnyMsg *msg, GList *images_list, GError **err)
369 {
370         TnyMimePart *related_part = NULL;
371         const gchar *content_type;
372
373         content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
374
375         if ((content_type != NULL) && !strcasecmp (content_type, "multipart/related")) {
376                 related_part = g_object_ref (msg);
377         } else if ((content_type != NULL) && !strcasecmp (content_type, "multipart/mixed")) {
378                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
379                 TnyIterator *iter = NULL;
380                 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
381                 iter = tny_list_create_iterator (parts);
382
383                 while (!tny_iterator_is_done (iter)) {
384                         TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
385                         if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
386                                 related_part = part;
387                                 break;
388                         }
389                         if (part)
390                                 g_object_unref (part);
391                         tny_iterator_next (iter);
392                 }
393                 g_object_unref (iter);
394                 g_object_unref (parts);
395         }
396
397         if (related_part != NULL) {
398                 /* TODO: attach images in their proper place */
399                 add_attachments (related_part, images_list, TRUE, err);
400                 g_object_unref (related_part);
401         }
402 }
403
404
405 gchar * 
406 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html, gboolean *is_html)
407 {
408         TnyStream *stream;
409         TnyMimePart *body;
410         GtkTextBuffer *buf;
411         GtkTextIter start, end;
412         gchar *to_quote;
413         gboolean result_was_html = TRUE;
414
415         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
416         
417         body = modest_tny_msg_find_body_part(msg, want_html);
418         if (!body)
419                 return NULL;
420
421         buf = gtk_text_buffer_new (NULL);
422         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
423         tny_stream_reset (stream);
424         tny_mime_part_decode_to_stream (body, stream, NULL);
425         tny_stream_reset (stream);
426         
427         gtk_text_buffer_get_bounds (buf, &start, &end);
428         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
429         if (tny_mime_part_content_type_is (body, "text/plain")) {
430                 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
431                 g_free (to_quote);
432                 to_quote = to_quote_converted;
433                 result_was_html = FALSE;
434         }
435
436         g_object_unref (buf);
437         g_object_unref (G_OBJECT(stream));
438         g_object_unref (G_OBJECT(body));
439
440         if (is_html != NULL)
441                 *is_html = result_was_html;
442
443         return to_quote;
444 }
445
446
447 static TnyMimePart*
448 modest_tny_msg_find_body_part_in_alternative (TnyMimePart *msg, gboolean want_html)
449 {
450         TnyList *parts;
451         TnyIterator *iter;
452         TnyMimePart *part = NULL;
453         TnyMimePart *first_part = NULL;
454         const gchar *desired_mime_type = want_html ? "text/html" : "text/plain";
455
456         parts = TNY_LIST (tny_simple_list_new());
457         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
458
459         for (iter  = tny_list_create_iterator(parts); 
460              !tny_iterator_is_done (iter);
461              tny_iterator_next (iter)) {
462                 gchar *content_type;
463                 gboolean is_body;
464
465                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
466
467                 if (first_part == NULL) {
468                         g_object_ref (part);
469                         first_part = part;
470                 }
471
472                 is_body = FALSE;
473                 content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
474                 is_body = g_str_has_prefix (content_type, desired_mime_type);
475                 if (is_body)
476                         break;
477
478                 g_object_unref (part);
479                 part = NULL;
480
481         }
482         g_object_unref (iter);
483         g_object_unref (parts);
484
485         if (part == NULL) {
486                 return first_part;
487         } else {
488                 if (first_part) g_object_unref (first_part);
489                 return part;
490         }
491 }
492
493 TnyMimePart*
494 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
495 {
496         TnyMimePart *part = NULL;
497         TnyList *parts = NULL;
498         TnyIterator *iter = NULL;
499         gchar *header_content_type;
500         gchar *header_content_type_lower = NULL;
501         
502         if (!msg)
503                 return NULL;
504
505         /* If it's an application multipart, then we don't get into as we don't
506          * support them (for example application/sml or wap messages */
507         header_content_type = modest_tny_mime_part_get_header_value (msg, "Content-Type");
508         if (header_content_type) {
509                 header_content_type = g_strstrip (header_content_type);
510                 header_content_type_lower = g_ascii_strdown (header_content_type, -1);
511         }
512         if (header_content_type_lower && 
513             g_str_has_prefix (header_content_type_lower, "multipart/") &&
514             !g_str_has_prefix (header_content_type_lower, "multipart/signed") &&
515             strstr (header_content_type_lower, "application/")) {
516                 g_free (header_content_type_lower);
517                 g_free (header_content_type);
518                 return NULL;
519         }       
520         if (header_content_type_lower && 
521             g_str_has_prefix (header_content_type_lower, "multipart/alternative")) {
522                 g_free (header_content_type_lower);
523                 g_free (header_content_type);
524                 return modest_tny_msg_find_body_part_in_alternative (msg, want_html);
525         }       
526         g_free (header_content_type_lower);
527         g_free (header_content_type);
528
529         parts = TNY_LIST (tny_simple_list_new());
530         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
531
532         iter  = tny_list_create_iterator(parts);
533
534         /* no parts? assume it's single-part message */
535         if (tny_iterator_is_done(iter)) {
536                 gchar *content_type;
537                 gboolean is_text_part;
538                 g_object_unref (G_OBJECT(iter));
539                 content_type = modest_tny_mime_part_get_content_type (msg);
540                 if (content_type == NULL)
541                         return NULL;
542                 is_text_part = 
543                         g_str_has_prefix (content_type, "text/");
544                 g_free (content_type);
545                 /* if this part cannot be a supported body return NULL */
546                 if (!is_text_part) {
547                         return NULL;
548                 } else {
549                         return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
550                 }
551         } else {
552                 do {
553                         gchar *tmp, *content_type = NULL;
554                         gboolean has_content_disp_name = FALSE;
555
556                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
557
558                         if (!part) {
559                                 g_warning ("%s: not a valid mime part", __FUNCTION__);
560                                 tny_iterator_next (iter);
561                                 continue;
562                         }
563
564                         /* it's a message --> ignore */
565                         if (part && TNY_IS_MSG (part)) {
566                                 g_object_unref (part);
567                                 part = NULL;
568                                 tny_iterator_next (iter);
569                                 continue;
570                         }                       
571
572                         /* we need to strdown the content type, because
573                          * tny_mime_part_has_content_type does not do it...
574                          */
575                         content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
576                         
577                         /* mime-parts with a content-disposition header (either 'inline' or 'attachment')
578                          * and a 'name=' thingy cannot be body parts
579                          */
580                                 
581                         tmp = modest_tny_mime_part_get_header_value (part, "Content-Disposition");
582                         if (tmp) {
583                                 gchar *content_disp = g_ascii_strdown(tmp, -1);
584                                 g_free (tmp);
585                                 has_content_disp_name = g_strstr_len (content_disp, strlen(content_disp), "name=") != NULL;
586                                 g_free (content_disp);
587                         }
588                         
589                         if (g_str_has_prefix (content_type, "text/") && 
590                             !has_content_disp_name &&
591                             !modest_tny_mime_part_is_attachment_for_modest (part)) {
592                                 /* we found the body. Doesn't have to be the desired mime part, first
593                                    text/ part in a mixed is the body */
594                                 g_free (content_type);
595                                 break;
596
597                         } else if (g_str_has_prefix(content_type, "multipart/alternative")) {
598
599                                 /* multipart? recurse! */
600                                 g_object_unref (part);
601                                 g_free (content_type);
602                                 part = modest_tny_msg_find_body_part_in_alternative (part, want_html);
603                                 if (part)
604                                         break;
605
606                         } else  if (g_str_has_prefix(content_type, "multipart")) {
607
608                                 /* multipart? recurse! */
609                                 g_object_unref (part);
610                                 g_free (content_type);
611                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
612                                 if (part)
613                                         break;
614                         } else
615                                 g_free (content_type);
616                         
617                         if (part) {
618                                 g_object_unref (G_OBJECT(part));
619                                 part = NULL;
620                         }
621                         
622                         tny_iterator_next (iter);
623                         
624                 } while (!tny_iterator_is_done(iter));
625         }
626         
627         g_object_unref (G_OBJECT(iter));
628         g_object_unref (G_OBJECT(parts));
629
630         return part; /* this maybe NULL, this is not an error; some message just don't have a body
631                       * part */
632 }
633
634
635 TnyMimePart*
636 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
637 {
638         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
639         
640         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
641                                                              want_html);
642 }
643
644
645 #define MODEST_TNY_MSG_PARENT_UID "parent-uid"
646
647 static TnyMsg *
648 create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
649                            const gchar *signature, gboolean is_reply,
650                            guint type /*ignored*/, GList *attachments)
651 {
652         TnyMsg *new_msg;
653         TnyHeader *new_header;
654         gchar *old_subject;
655         gchar *new_subject;
656         TnyMimePart *body = NULL;
657         TnyMimePart *html_body = NULL;
658         ModestFormatter *formatter;
659         gchar *subject_prefix;
660         gboolean no_text_part;
661         gchar *parent_uid;
662         gboolean forward_as_attach = FALSE;
663
664         if (header)
665                 g_object_ref (header);
666         else
667                 header = tny_msg_get_header (msg);
668
669         /* Get body from original msg. Always look for the text/plain
670            part of the message to create the reply/forwarded mail */
671         if (msg != NULL) {
672                 body   = modest_tny_msg_find_body_part (msg, FALSE);
673                 html_body = modest_tny_msg_find_body_part (msg, TRUE);
674         }
675
676         if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT,
677                                   NULL))
678                 formatter = modest_formatter_new ("text/html", signature);
679         else
680                 formatter = modest_formatter_new ("text/plain", signature);
681
682
683         /* if we don't have a text-part */
684         no_text_part = (!body) || (strcmp (tny_mime_part_get_content_type (body), "text/html")==0);
685
686         /* when we're reply, include the text part if we have it, or nothing otherwise. */
687         if (is_reply)
688                 new_msg = modest_formatter_quote  (formatter, body, header,
689                                                     attachments);
690         else {
691                 if (no_text_part || (html_body && (strcmp (tny_mime_part_get_content_type (html_body), "text/html")==0))) {
692                         forward_as_attach = TRUE;
693                         new_msg = modest_formatter_attach (formatter, msg, header);
694                 } else {
695                         forward_as_attach = FALSE;
696                         new_msg = modest_formatter_inline  (formatter, body, header,
697                                                             attachments);
698                 }
699         }
700
701         g_object_unref (G_OBJECT(formatter));
702         if (body)
703                 g_object_unref (G_OBJECT(body));
704         if (html_body)
705                 g_object_unref (G_OBJECT(html_body));
706
707         /* Fill the header */
708         new_header = tny_msg_get_header (new_msg);
709         tny_header_set_from (new_header, from);
710         tny_header_set_replyto (new_header, from);
711
712         /* Change the subject */
713         if (is_reply)
714                 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
715         else
716                 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
717         old_subject = tny_header_dup_subject (header);
718         new_subject =
719                 (gchar *) modest_text_utils_derived_subject (old_subject,
720                                                              subject_prefix);
721         g_free (old_subject);
722         g_free (subject_prefix);
723         tny_header_set_subject (new_header, (const gchar *) new_subject);
724         g_free (new_subject);
725
726         /* get the parent uid, and set it as a gobject property on the new msg */
727         parent_uid = modest_tny_folder_get_header_unique_id (header);
728         g_object_set_data_full (G_OBJECT(new_msg), MODEST_TNY_MSG_PARENT_UID,
729                                 parent_uid, g_free);
730
731         /* set modest as the X-Mailer
732          * we could this in the platform factory, but then the header
733          * would show up before all the others.
734          */
735         tny_mime_part_set_header_pair (TNY_MIME_PART (msg), "X-Mailer", "Modest "
736                                        VERSION);
737
738         /* Clean */
739         g_object_unref (G_OBJECT (new_header));
740         g_object_unref (G_OBJECT (header));
741         /* ugly to unref it here instead of in the calling func */
742
743         if (!is_reply & !forward_as_attach) {
744                 add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, NULL);
745         }
746
747         return new_msg;
748 }
749
750 const gchar*
751 modest_tny_msg_get_parent_uid (TnyMsg *msg)
752 {
753         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
754         
755         return g_object_get_data (G_OBJECT(msg), MODEST_TNY_MSG_PARENT_UID);
756 }
757
758 static gchar *get_signed_protocol (TnyMimePart *part)
759 {
760         TnyList *header_pairs;
761         TnyIterator *iterator;
762         gchar *result = NULL;
763
764         header_pairs = TNY_LIST (tny_simple_list_new ());
765         tny_mime_part_get_header_pairs (part, header_pairs);
766         iterator = tny_list_create_iterator (header_pairs);
767
768         while (!result && !tny_iterator_is_done (iterator)) {
769                 TnyPair *pair;
770                 const gchar *name;
771
772                 pair = TNY_PAIR (tny_iterator_get_current (iterator));
773                 name = tny_pair_get_name (pair);
774                 if (name && !g_ascii_strcasecmp (name, "Content-Type")) {
775                         const gchar *s;
776                         s = tny_pair_get_value (pair);
777                         if (s) {
778                                 s = strstr (s, "protocol=");
779                                 if (s) {
780                                         const gchar *t;
781                                         s += 9;
782                                         if (*s == '\"') {
783                                                 s++;
784                                                 t = strstr (s, "\"");
785                                         } else {
786                                                 t = strstr (s, ";");
787                                         }
788                                         result = g_strndup (s, t - s);
789                                 }
790                         }
791                 }
792
793                 g_object_unref (pair);
794                 tny_iterator_next (iterator);
795         }
796
797         g_object_unref (iterator);
798         g_object_unref (header_pairs);
799
800         return result;
801 }
802
803 TnyMimePart *
804 modest_tny_msg_get_attachments_parent (TnyMsg *msg)
805 {
806         TnyMimePart *result;
807         const gchar *content_type;
808
809         result = NULL;
810
811         content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
812         if (content_type && !strcmp (content_type, "multipart/signed")) {
813                 TnyList *msg_children;
814                 TnyIterator *iterator;
815                 gchar *signed_protocol;
816
817                 msg_children = TNY_LIST (tny_simple_list_new ());
818                 tny_mime_part_get_parts (TNY_MIME_PART (msg), msg_children);
819
820                 iterator = tny_list_create_iterator (msg_children);
821                 signed_protocol = get_signed_protocol (TNY_MIME_PART (msg));
822                         
823                 while (!result && !tny_iterator_is_done (iterator)) {
824                         TnyMimePart *part;
825
826                         part = TNY_MIME_PART (tny_iterator_get_current (iterator));
827                         if (signed_protocol) {
828                                 const gchar *part_content_type;
829
830                                 part_content_type = tny_mime_part_get_content_type (part);
831                                 if (part_content_type && g_ascii_strcasecmp (part_content_type, signed_protocol)) {
832                                         result = g_object_ref (part);
833                                 }
834                         } else {
835                                 result = g_object_ref (part);
836                         }
837
838                         g_object_unref (part);
839                         tny_iterator_next (iterator);
840                 }
841
842                 g_object_unref (iterator);
843                 g_free (signed_protocol);
844                 g_object_unref (msg_children);
845         }
846         if (result == NULL) {
847                 result = g_object_ref (msg);
848         }
849
850         return result;
851 }
852
853
854
855 static void
856 add_if_attachment (gpointer data, gpointer user_data)
857 {
858         TnyMimePart *part;
859         GList **attachments_list;
860
861         part = TNY_MIME_PART (data);
862         attachments_list = ((GList **) user_data);
863
864         if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part))) {
865                 *attachments_list = g_list_prepend (*attachments_list, part);
866                 g_object_ref (part);
867         }
868 }
869
870 TnyMsg* 
871 modest_tny_msg_create_forward_msg (TnyMsg *msg, 
872                                    const gchar *from,
873                                    const gchar *signature,
874                                    ModestTnyMsgForwardType forward_type)
875 {
876         TnyMsg *new_msg;
877         TnyList *parts = NULL;
878         GList *attachments_list = NULL;
879         TnyMimePart *part_to_check = NULL;
880
881         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
882
883         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
884         
885         /* Add attachments */
886         parts = TNY_LIST (tny_simple_list_new());
887         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
888         tny_list_foreach (parts, add_if_attachment, &attachments_list);
889
890         new_msg = create_reply_forward_mail (msg, NULL, from, signature, FALSE, forward_type,
891                                              attachments_list);
892
893         /* Clean */
894         if (attachments_list) {
895                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
896                 g_list_free (attachments_list);
897         }
898         g_object_unref (G_OBJECT (parts));
899         g_object_unref (part_to_check);
900
901         return new_msg;
902 }
903
904
905
906 static gint
907 count_addresses (const gchar* addresses)
908 {
909         gint count = 1;
910
911         if (!addresses)
912                 return 0;
913         
914         while (*addresses) {
915                 if (*addresses == ',' || *addresses == ';')
916                         ++count;
917                 ++addresses;
918         }
919         
920         return count;
921 }
922
923
924 /* get the new To:, based on the old header,
925  * result is newly allocated or NULL in case of error
926  * */
927 static gchar*
928 get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
929             ModestTnyMsgReplyMode reply_mode)
930 {
931         gchar* old_reply_to;
932         gchar* old_from;
933         gchar* new_to;
934         gchar* tmp;
935         
936         /* according to RFC2369 (http://www.faqs.org/rfcs/rfc2369.html), we
937          * can identify Mailing-List posts by the List-Help header.
938          * for mailing lists, both the Reply-To: and From: should be included
939          * in the new To:; for now, we're ignoring List-Post
940          */
941         gchar* list_help = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
942                                                                   "List-Help");
943         gboolean is_mailing_list = (list_help != NULL);
944         g_free (list_help);
945
946
947         /* reply to sender, use ReplyTo or From */
948         old_reply_to = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
949                                                               "Reply-To"); 
950         old_from     = tny_header_dup_from (header);
951         
952         if (!old_from && !old_reply_to) {
953                 g_debug ("%s: failed to get either Reply-To: or From: from header",
954                            __FUNCTION__);
955                 return NULL;
956         }
957         
958         /* for mailing lists, use both Reply-To and From if we did a
959          * 'Reply All:'
960          * */
961         if (is_mailing_list && reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL &&
962             old_reply_to && old_from && strcmp (old_from, old_reply_to) != 0)
963                 new_to = g_strjoin (",", old_reply_to, old_from, NULL);
964         else
965                 /* otherwise use either Reply-To: (preferred) or From: */
966                 new_to = g_strdup (old_reply_to ? old_reply_to : old_from);
967         g_free (old_from);
968         g_free (old_reply_to);
969
970         /* in case of ReplyAll, we need to add the Recipients in the old To: */
971         if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
972                 gchar *old_to = tny_header_dup_to (header);
973                 if (!old_to) 
974                         g_debug ("%s: no To: address found in source mail",
975                                    __FUNCTION__);
976                 else {
977                         /* append the old To: */
978                         gchar *tmp = g_strjoin (",", new_to, old_to, NULL);
979                         g_free (new_to);
980                         new_to = tmp;
981                         g_free (old_to);
982                 }
983
984                 /* remove duplicate entries */
985                 gchar *tmp = modest_text_utils_remove_duplicate_addresses (new_to);
986                 g_free (new_to);
987                 new_to = tmp;
988                 
989                 /* now, strip me (the new From:) from the new_to, but only if
990                  * there are >1 addresses there */
991                 if (count_addresses (new_to) > 1) {
992                         gchar *tmp = modest_text_utils_remove_address (new_to, from);
993                         g_free (new_to);
994                         new_to = tmp;
995                 }
996         }
997
998         tmp = modest_text_utils_simplify_recipients (new_to);
999         g_free (new_to);
1000         new_to = tmp;
1001
1002         return new_to;
1003 }
1004
1005
1006 /* get the new Cc:, based on the old header,
1007  * result is newly allocated or NULL in case of error */
1008 static gchar*
1009 get_new_cc (TnyHeader *header, const gchar* from, const gchar *new_to)
1010 {
1011         gchar *old_cc, *result, *dup;
1012
1013         old_cc = tny_header_dup_cc (header);
1014         if (!old_cc)
1015                 return NULL;
1016
1017         /* remove me (the new From:) from the Cc: list */
1018         dup =  modest_text_utils_remove_address (old_cc, from);
1019
1020         if (new_to) {
1021                 gchar **to_parts, **current;
1022
1023                 to_parts = g_strsplit (new_to, ",", 0);
1024                 for (current = to_parts; current && *current != '\0'; current++) {
1025                         gchar *dup2;
1026
1027                         dup2 = modest_text_utils_remove_address (dup, g_strstrip (*current));
1028                         g_free (dup);
1029                         dup = dup2;
1030                 }
1031                 g_strfreev (to_parts);
1032         }
1033
1034         result = modest_text_utils_remove_duplicate_addresses (dup);
1035         g_free (dup);
1036         g_free (old_cc);
1037         return result;
1038 }
1039
1040 void 
1041 modest_tny_msg_get_references (TnyMsg *msg, gchar **message_id, gchar **references, gchar **in_reply_to)
1042 {
1043         TnyList *headers;
1044         TnyIterator *iterator;
1045         gchar *l_message_id;
1046         gchar *l_references;
1047         gchar *l_in_reply_to;
1048
1049         g_return_if_fail (TNY_IS_MSG (msg));
1050
1051         l_message_id = NULL;
1052         l_references = NULL;
1053         l_in_reply_to = NULL;
1054
1055         headers = TNY_LIST (tny_simple_list_new ());
1056         tny_mime_part_get_header_pairs (TNY_MIME_PART (msg), headers);
1057
1058         iterator = tny_list_create_iterator (headers);
1059         while (!tny_iterator_is_done (iterator)) {
1060                 TnyPair *pair;
1061                 const gchar *name;
1062
1063                 pair = TNY_PAIR (tny_iterator_get_current (iterator));
1064                 name = tny_pair_get_name (pair);
1065                 if (!g_strcasecmp (name, "References")) {
1066                         if (l_references) g_free (l_references);
1067                         l_references = g_strdup (tny_pair_get_value (pair));
1068                 } else if (!g_strcasecmp (name, "In-Reply-To")) {
1069                         if (l_in_reply_to) g_free (l_in_reply_to);
1070                         l_in_reply_to = g_strdup (tny_pair_get_value (pair));
1071                 } else if (!g_strcasecmp (name, "Message-ID")) {
1072                         if (l_message_id) g_free (l_message_id);
1073                         l_message_id = g_strdup (tny_pair_get_value (pair));
1074                 }
1075
1076                 g_object_unref (pair);
1077                 tny_iterator_next (iterator);
1078         }
1079
1080         g_object_unref (iterator);
1081         g_object_unref (headers);
1082
1083         if (message_id) {
1084                 *message_id = l_message_id;
1085         } else {
1086                 g_free (l_message_id);
1087         }
1088
1089         if (in_reply_to) {
1090                 *in_reply_to = l_in_reply_to;
1091         } else {
1092                 g_free (l_in_reply_to);
1093         }
1094
1095         if (references) {
1096                 *references = l_references;
1097         } else {
1098                 g_free (l_references);
1099         }
1100 }
1101
1102 static void 
1103 set_references (TnyMsg *reply_msg, TnyMsg *original_msg)
1104 {
1105         gchar *orig_references, *orig_in_reply_to, *orig_message_id;
1106         gchar *references, *in_reply_to;
1107
1108         modest_tny_msg_get_references (original_msg, &orig_message_id, &orig_references, &orig_in_reply_to);
1109
1110         references = NULL;
1111         in_reply_to = NULL;
1112
1113         if (orig_message_id)
1114                 in_reply_to = g_strdup (orig_message_id);
1115
1116         if (orig_references) {
1117                 if (orig_message_id)
1118                         references = g_strconcat (orig_references, "\n        ", orig_message_id, NULL);
1119                 else
1120                         references = g_strdup (orig_references);
1121
1122         } else if (orig_in_reply_to) {
1123                 if (orig_message_id)
1124                         references = g_strconcat (orig_in_reply_to, "\n        ", orig_message_id, NULL);
1125                 else
1126                         references = g_strdup (orig_in_reply_to);
1127         } else if (orig_message_id) {
1128                 references = g_strdup (orig_message_id);
1129         }
1130
1131         g_free (orig_references);
1132         g_free (orig_in_reply_to);
1133         g_free (orig_message_id);
1134
1135         if (in_reply_to) {
1136                 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "In-Reply-To", in_reply_to);
1137         }
1138         if (references) {
1139                 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "References", references);
1140         }
1141 }
1142
1143 TnyMsg* 
1144 modest_tny_msg_create_reply_msg (TnyMsg *msg,
1145                                  TnyHeader *header,
1146                                  const gchar *from,
1147                                  const gchar *signature,
1148                                  ModestTnyMsgReplyType reply_type,
1149                                  ModestTnyMsgReplyMode reply_mode)
1150 {
1151         TnyMsg *new_msg = NULL;
1152         TnyHeader *new_header;
1153         gchar *new_to = NULL;
1154         TnyList *parts = NULL;
1155         GList *attachments_list = NULL;
1156         TnyMimePart *part_to_check = NULL;
1157
1158         g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
1159
1160         part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
1161
1162         parts = TNY_LIST (tny_simple_list_new());
1163         tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
1164         tny_list_foreach (parts, add_if_attachment, &attachments_list);
1165
1166         new_msg = create_reply_forward_mail (msg, header, from, signature, TRUE, reply_type,
1167                                              attachments_list);
1168
1169         set_references (new_msg, msg);
1170         if (attachments_list != NULL) {
1171                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
1172                 g_list_free (attachments_list);
1173         }
1174         g_object_unref (parts);
1175
1176         /* Fill the header */
1177         if (header)
1178                 g_object_ref (header);
1179         else
1180                 header = tny_msg_get_header (msg);
1181
1182         
1183         new_header = tny_msg_get_header(new_msg);
1184         new_to = get_new_to (msg, header, from, reply_mode);
1185         if (!new_to)
1186                 g_debug ("%s: failed to get new To:", __FUNCTION__);
1187         else {
1188                 tny_header_set_to (new_header, new_to);
1189         }
1190
1191         if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
1192                 gchar *new_cc = get_new_cc (header, from, new_to);
1193                 if (new_cc) { 
1194                         tny_header_set_cc (new_header, new_cc);
1195                         g_free (new_cc);
1196                 }
1197         }
1198
1199         if (new_to)
1200                 g_free (new_to);
1201
1202         /* Clean */
1203         g_object_unref (G_OBJECT (new_header));
1204         g_object_unref (G_OBJECT (header));
1205         g_object_unref (G_OBJECT (part_to_check));
1206
1207         return new_msg;
1208 }
1209
1210
1211 static gboolean
1212 is_ascii(const gchar *s)
1213 {
1214         if (!s)
1215                 return TRUE;
1216         while (s[0]) {
1217                 if (s[0] & 128 || s[0] < 32)
1218                         return FALSE;
1219                 s++;
1220         }
1221         return TRUE;
1222 }
1223
1224 static char *
1225 get_content_type(const gchar *s)
1226 {
1227         GString *type;
1228         
1229         type = g_string_new("text/plain");
1230         if (!is_ascii(s)) {
1231                 if (g_utf8_validate(s, -1, NULL)) {
1232                         g_string_append(type, "; charset=\"utf-8\"");
1233                 } else {
1234                         /* it should be impossible to reach this, but better safe than sorry */
1235                         g_debug("invalid utf8 in message");
1236                         g_string_append(type, "; charset=\"latin1\"");
1237                 }
1238         }
1239         return g_string_free(type, FALSE);
1240 }
1241
1242 guint64
1243 modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
1244                               guint64 parts_count,
1245                               guint64 parts_size)
1246 {
1247         guint64 result;
1248
1249         /* estimation of headers size */
1250         result = 1024;
1251
1252         /* We add a 20% of size due to the increase in 7bit encoding */
1253         if (plain_body) {
1254                 result += strlen (plain_body) * 120 / 100;
1255         }
1256         if (html_body) {
1257                 result += strlen (html_body) * 120 / 100;
1258         }
1259
1260         /* 256 bytes per additional part because of their headers */
1261         result += parts_count * 256;
1262
1263         /* 150% of increase per encoding */
1264         result += parts_size * 3 / 2;
1265
1266         return result;
1267 }
1268
1269 GSList *
1270 modest_tny_msg_header_get_all_recipients_list (TnyHeader *header)
1271 {
1272         GSList *recipients = NULL;
1273         gchar *from = NULL, *to = NULL, *cc = NULL, *bcc = NULL;
1274         gchar *after_remove;
1275         GString *buffer;
1276         gboolean add_separator = TRUE;
1277
1278         if (header == NULL)
1279                 return NULL;
1280
1281         buffer = g_string_new ("");
1282
1283         from = tny_header_dup_from (header);
1284         to = tny_header_dup_to (header);
1285         cc = tny_header_dup_cc (header);
1286         bcc = tny_header_dup_bcc (header);
1287
1288         recipients = NULL;
1289         if (from) {
1290                 buffer = g_string_append (buffer, from);
1291                 add_separator = TRUE;
1292         }
1293         if (to) {
1294                 if (add_separator)
1295                         buffer = g_string_append (buffer, "; ");
1296                 else
1297                         add_separator = TRUE;
1298
1299                 buffer = g_string_append (buffer, to);
1300         }
1301         if (cc) {
1302                 if (add_separator)
1303                         buffer = g_string_append (buffer, "; ");
1304                 else
1305                         add_separator = TRUE;
1306
1307                 buffer = g_string_append (buffer, cc);
1308         }
1309         if (bcc) {
1310                 if (add_separator)
1311                         buffer = g_string_append (buffer, "; ");
1312                 else
1313                         add_separator = TRUE;
1314
1315                 buffer = g_string_append (buffer, bcc);
1316         }
1317
1318         after_remove = modest_text_utils_remove_duplicate_addresses (buffer->str);
1319         g_string_free (buffer, TRUE);
1320
1321         recipients = modest_text_utils_split_addresses_list (after_remove);
1322         g_free (after_remove);
1323
1324         return recipients;
1325 }
1326
1327 GSList *
1328 modest_tny_msg_get_all_recipients_list (TnyMsg *msg)
1329 {
1330         TnyHeader *header = NULL;
1331         GSList *recipients = NULL;
1332
1333         if (msg == NULL)
1334                 return NULL;
1335
1336         header = tny_msg_get_header (msg);
1337         if (header == NULL)
1338                 return NULL;
1339
1340         recipients = modest_tny_msg_header_get_all_recipients_list (header);
1341         g_object_unref (header);
1342
1343         return recipients;
1344 }
1345