* src/modest-text-utils.c:
[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-formatter.h"
37 #include <tny-camel-stream.h>
38 #include <tny-camel-mime-part.h>
39 #include <camel/camel-stream-buffer.h>
40 #include <camel/camel-stream-mem.h>
41 #include <glib/gprintf.h>
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif /*HAVE_CONFIG_H */
46
47 #include <modest-tny-msg.h>
48 #include "modest-text-utils.h"
49
50 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
51                                     const gchar *content_type);
52 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
53 static void add_attachments (TnyMsg *msg, GList *attachments_list);
54 static char * get_content_type(const gchar *s);
55 static gboolean is_ascii(const gchar *s);
56
57
58 TnyMsg*
59 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
60                     const gchar *bcc, const gchar* subject, const gchar *body,
61                     GList *attachments)
62 {
63         TnyMsg *new_msg;
64         TnyHeader *header;
65         gchar *content_type;
66         
67         /* Create new msg */
68         new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL));
69         header  = tny_msg_get_header (new_msg);
70         
71         if ((from != NULL) && (strlen(from) > 0)) {
72                 tny_header_set_from (TNY_HEADER (header), from);
73                 tny_header_set_replyto (TNY_HEADER (header), from);
74         }
75         if ((mailto != NULL) && (strlen(mailto) > 0)) 
76                 tny_header_set_to (TNY_HEADER (header), mailto);
77         if ((cc != NULL) && (strlen(cc) > 0)) 
78                 tny_header_set_cc (TNY_HEADER (header), cc);
79         if ((bcc != NULL) && (strlen(bcc) > 0)) 
80                 tny_header_set_bcc (TNY_HEADER (header), bcc);
81         
82         if ((subject != NULL) && (strlen(subject) > 0)) 
83                 tny_header_set_subject (TNY_HEADER (header), subject);
84
85         content_type = get_content_type(body);
86                 
87         /* Add the body of the new mail */
88         /* This is needed even if body is NULL or empty. */
89         add_body_part (new_msg, body, content_type);
90         g_free (content_type);
91                        
92         /* Add attachments */
93         if (attachments)
94                 add_attachments (new_msg, attachments);
95
96         return new_msg;
97 }
98
99 TnyMsg*
100 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
101                                const gchar *bcc, const gchar* subject, 
102                                const gchar *html_body, const gchar *plain_body,
103                                GList *attachments)
104 {
105         TnyMsg *new_msg;
106         TnyHeader *header;
107         gchar *content_type;
108         
109         /* Create new msg */
110         new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL));
111         header  = tny_msg_get_header (new_msg);
112         
113         if ((from != NULL) && (strlen(from) > 0)) {
114                 tny_header_set_from (TNY_HEADER (header), from);
115                 tny_header_set_replyto (TNY_HEADER (header), from);
116         }
117         if ((mailto != NULL) && (strlen(mailto) > 0)) 
118                 tny_header_set_to (TNY_HEADER (header), mailto);
119         if ((cc != NULL) && (strlen(cc) > 0)) 
120                 tny_header_set_cc (TNY_HEADER (header), cc);
121         if ((bcc != NULL) && (strlen(bcc) > 0)) 
122                 tny_header_set_bcc (TNY_HEADER (header), bcc);
123         
124         if ((subject != NULL) && (strlen(subject) > 0)) 
125                 tny_header_set_subject (TNY_HEADER (header), subject);
126
127         content_type = get_content_type(plain_body);
128                 
129         /* Add the body of the new mail */      
130         add_body_part (new_msg, plain_body, content_type);
131         add_html_body_part (new_msg, html_body);
132         g_free (content_type);
133                        
134         /* Add attachments */
135         add_attachments (new_msg, attachments);
136
137         return new_msg;
138 }
139
140
141 /* FIXME: this func copy from modest-mail-operation: refactor */
142 static TnyMimePart *
143 add_body_part (TnyMsg *msg, 
144                const gchar *body,
145                const gchar *content_type)
146 {
147         TnyMimePart *text_body_part = NULL;
148         TnyStream *text_body_stream;
149
150         /* Create the stream */
151         text_body_stream = TNY_STREAM (tny_camel_stream_new
152                                        (camel_stream_mem_new_with_buffer
153                                         (body, (body ? strlen(body) : 0))));
154
155         text_body_part = modest_formatter_create_body_part (NULL, msg);
156
157         /* Construct MIME part */
158         tny_stream_reset (text_body_stream);
159         tny_mime_part_construct_from_stream (text_body_part,
160                                              text_body_stream,
161                                              content_type);
162         tny_stream_reset (text_body_stream);
163
164         g_object_unref (G_OBJECT(text_body_part));
165
166         /* Clean */
167         g_object_unref (text_body_stream);
168
169         return text_body_part;
170 }
171
172 static TnyMimePart *
173 add_html_body_part (TnyMsg *msg, 
174                     const gchar *body)
175 {
176         TnyMimePart *html_body_part = NULL;
177         TnyStream *html_body_stream;
178
179         /* Create the stream */
180         html_body_stream = TNY_STREAM (tny_camel_stream_new
181                                        (camel_stream_mem_new_with_buffer
182                                         (body, strlen(body))));
183
184         /* Create body part if needed */
185         html_body_part = modest_formatter_create_body_part (NULL, msg);
186
187         /* Construct MIME part */
188         tny_stream_reset (html_body_stream);
189         tny_mime_part_construct_from_stream (html_body_part,
190                                              html_body_stream,
191                                              "text/html; charset=utf-8");
192         tny_stream_reset (html_body_stream);
193
194         g_object_unref (G_OBJECT(html_body_part));
195
196         /* Clean */
197         g_object_unref (html_body_stream);
198
199         return html_body_part;
200 }
201
202 static TnyMimePart *
203 copy_mime_part (TnyMimePart *part)
204 {
205         TnyMimePart *result = NULL;
206         const gchar *attachment_content_type;
207         const gchar *attachment_filename;
208         const gchar *attachment_cid;
209         TnyList *parts;
210         TnyIterator *iterator;
211         TnyStream *attachment_stream;
212
213         if (TNY_IS_MSG (part)) {
214                 g_object_ref (part);
215                 return part;
216         }
217
218         result = tny_platform_factory_new_mime_part (
219                 modest_runtime_get_platform_factory());
220
221         attachment_content_type = tny_mime_part_get_content_type (part);
222
223         /* get mime part headers */
224         attachment_filename = tny_mime_part_get_filename (part);
225         attachment_cid = tny_mime_part_get_content_id (part);
226         
227         /* fill the stream */
228         attachment_stream = tny_mime_part_get_stream (part);
229         tny_stream_reset (attachment_stream);
230         tny_mime_part_construct_from_stream (result,
231                                              attachment_stream,
232                                              attachment_content_type);
233         tny_stream_reset (attachment_stream);
234         
235         /* set other mime part fields */
236         tny_mime_part_set_filename (result, attachment_filename);
237         tny_mime_part_set_content_id (result, attachment_cid);
238
239         /* copy subparts */
240         parts = tny_simple_list_new ();
241         tny_mime_part_get_parts (part, parts);
242         iterator = tny_list_create_iterator (parts);
243         while (!tny_iterator_is_done (iterator)) {
244                 TnyMimePart *subpart = TNY_MIME_PART (tny_iterator_get_current (iterator));
245                 if (subpart) {
246                         TnyMimePart *subpart_copy = copy_mime_part (subpart);
247                         tny_mime_part_add_part (result, subpart_copy);
248                         g_object_unref (subpart);
249                 }
250
251                 tny_iterator_next (iterator);
252         }
253         g_object_unref (iterator);
254         g_object_unref (parts);
255         g_object_unref (attachment_stream);
256
257         return result;
258 }
259
260 static void
261 add_attachments (TnyMsg *msg, GList *attachments_list)
262 {
263         GList *pos;
264         TnyMimePart *attachment_part, *old_attachment;
265
266         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
267
268                 old_attachment = pos->data;
269                 attachment_part = copy_mime_part (old_attachment);
270                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
271                 g_object_unref (attachment_part);
272         }
273 }
274
275
276 gchar * 
277 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html, gboolean *is_html)
278 {
279         TnyStream *stream;
280         TnyMimePart *body;
281         GtkTextBuffer *buf;
282         GtkTextIter start, end;
283         gchar *to_quote;
284         gboolean result_was_html = TRUE;
285
286         body = modest_tny_msg_find_body_part(msg, want_html);
287         if (!body)
288                 return NULL;
289
290         buf = gtk_text_buffer_new (NULL);
291         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
292         tny_stream_reset (stream);
293         tny_mime_part_decode_to_stream (body, stream);
294         tny_stream_reset (stream);
295         
296         gtk_text_buffer_get_bounds (buf, &start, &end);
297         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
298         if (tny_mime_part_content_type_is (body, "text/plain")) {
299                 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
300                 g_free (to_quote);
301                 to_quote = to_quote_converted;
302                 result_was_html = FALSE;
303         }
304
305         g_object_unref (buf);
306         g_object_unref (G_OBJECT(stream));
307         g_object_unref (G_OBJECT(body));
308
309         if (is_html != NULL)
310                 *is_html = result_was_html;
311
312         return to_quote;
313 }
314
315
316 TnyMimePart*
317 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
318 {
319         const gchar *mime_type = want_html ? "text/html" : "text/plain";
320         TnyMimePart *part = NULL;
321         TnyList *parts = NULL;
322         TnyIterator *iter = NULL;
323
324         if (!msg)
325                 return NULL;
326
327         parts = TNY_LIST (tny_simple_list_new());
328         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
329
330         iter  = tny_list_create_iterator(parts);
331
332         /* no parts? assume it's single-part message */
333         if (tny_iterator_is_done(iter)) {
334                 g_object_unref (G_OBJECT(iter));
335                 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
336         } else {
337                 gchar *content_type = NULL;
338                 do {
339                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
340                         if (part && TNY_IS_MSG (part)) {
341                                 g_object_unref (part);
342                                 tny_iterator_next (iter);
343                                 continue;
344                         }
345
346                         /* we need to strdown the content type, because
347                          * tny_mime_part_has_content_type does not do it...
348                          */
349                         if (part) {
350                                 content_type = g_ascii_strdown
351                                         (tny_mime_part_get_content_type (part), -1);
352                         }
353                                                         
354                         if (g_str_has_prefix (content_type, mime_type) &&
355                             !tny_mime_part_is_attachment (part))
356                                 break;
357                         
358                         if (g_str_has_prefix(content_type, "multipart")) {
359                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
360                                 if (part)
361                                         break;
362                         }
363
364                         if (part)
365                                 g_object_unref (G_OBJECT(part));
366
367                         part = NULL;
368                         
369                         g_free (content_type);
370                         content_type = NULL;
371
372                         tny_iterator_next (iter);
373                         
374                 } while (!tny_iterator_is_done(iter));
375                 g_free (content_type);
376         }
377         
378         g_object_unref (G_OBJECT(iter));
379         g_object_unref (G_OBJECT(parts));
380
381         /* if were trying to find an HTML part and couldn't find it,
382          * try to find a text/plain part instead
383          */
384         if (!part && want_html) 
385                 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
386         else
387                 return part; /* this maybe NULL, this is not an error; some message just don't have a body
388                               * part */
389 }
390
391
392 TnyMimePart*
393 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
394 {
395         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
396                                                              want_html);
397 }
398
399
400 static TnyMsg *
401 create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from, const gchar *signature, 
402                            gboolean is_reply, guint type, GList *attachments)
403 {
404         TnyMsg *new_msg;
405         TnyHeader *new_header;
406         gchar *new_subject;
407         TnyMimePart *body = NULL;
408         ModestFormatter *formatter;
409         gchar *subject_prefix;
410
411         /* Get body from original msg. Always look for the text/plain
412            part of the message to create the reply/forwarded mail */
413         if (header)
414                 g_object_ref (header);
415         else
416                 header = tny_msg_get_header (msg);
417
418         if (msg != NULL)
419                 body   = modest_tny_msg_find_body_part (msg, !is_reply);
420
421         /* TODO: select the formatter from account prefs */
422         if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
423                 formatter = modest_formatter_new ("text/html", signature);
424         else
425                 formatter = modest_formatter_new ("text/plain", signature);
426
427         /* Format message body */
428         if (is_reply) {
429                 switch (type) {
430                 case MODEST_TNY_MSG_REPLY_TYPE_CITE:
431                 default:
432                         new_msg = modest_formatter_cite  (formatter, body, header);
433                         break;
434                 case MODEST_TNY_MSG_REPLY_TYPE_QUOTE:
435                         new_msg = modest_formatter_quote (formatter, body, header, attachments);
436                         break;
437                 }
438         } else {
439                 switch (type) {
440                 case MODEST_TNY_MSG_FORWARD_TYPE_INLINE:
441                 default:
442                         if (strcmp (tny_mime_part_get_content_type (body), "text/html")==0)
443                                 new_msg = modest_formatter_attach (formatter, msg, header);
444                         else 
445                                 new_msg = modest_formatter_inline  (formatter, body, header, attachments);
446                         break;
447                 case MODEST_TNY_MSG_FORWARD_TYPE_ATTACHMENT:
448                         new_msg = modest_formatter_attach (formatter, msg, header);
449                         break;
450                 }
451         }
452         g_object_unref (G_OBJECT(formatter));
453         if (body)
454                 g_object_unref (G_OBJECT(body));
455         
456         /* Fill the header */
457         new_header = tny_msg_get_header (new_msg);      
458         tny_header_set_from (new_header, from);
459         tny_header_set_replyto (new_header, from);
460
461         /* Change the subject */
462         if (is_reply)
463                 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
464         else
465                 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
466         new_subject =
467                 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
468                                                              subject_prefix);
469         g_free (subject_prefix);
470         tny_header_set_subject (new_header, (const gchar *) new_subject);
471         g_free (new_subject);
472
473         /* Clean */
474         g_object_unref (G_OBJECT (new_header));
475         g_object_unref (G_OBJECT (header));
476
477         return new_msg;
478 }
479
480 static void
481 add_if_attachment (gpointer data, gpointer user_data)
482 {
483         TnyMimePart *part;
484         GList **attachments_list;
485
486         part = TNY_MIME_PART (data);
487         attachments_list = ((GList **) user_data);
488
489         if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part))) {
490                 *attachments_list = g_list_prepend (*attachments_list, part);
491                 g_object_ref (part);
492         }
493 }
494
495 TnyMsg* 
496 modest_tny_msg_create_forward_msg (TnyMsg *msg, 
497                                    const gchar *from,
498                                    const gchar *signature,
499                                    ModestTnyMsgForwardType forward_type)
500 {
501         TnyMsg *new_msg;
502         TnyList *parts = NULL;
503         GList *attachments_list = NULL;
504
505         /* Add attachments */
506         parts = TNY_LIST (tny_simple_list_new());
507         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
508         tny_list_foreach (parts, add_if_attachment, &attachments_list);
509
510         new_msg = create_reply_forward_mail (msg, NULL, from, signature, FALSE, forward_type, attachments_list);
511         add_attachments (new_msg, attachments_list);
512
513         /* Clean */
514         if (attachments_list)
515                 g_list_free (attachments_list);
516         g_object_unref (G_OBJECT (parts));
517
518         return new_msg;
519 }
520
521 TnyMsg* 
522 modest_tny_msg_create_reply_msg (TnyMsg *msg,
523                                  TnyHeader *header,
524                                  const gchar *from,
525                                  const gchar *signature,
526                                  ModestTnyMsgReplyType reply_type,
527                                  ModestTnyMsgReplyMode reply_mode)
528 {
529         TnyMsg *new_msg = NULL;
530         TnyHeader *new_header;
531         const gchar* reply_to;
532         gchar *new_cc = NULL;
533         const gchar *cc = NULL, *bcc = NULL;
534         GString *tmp = NULL;
535         TnyList *parts = NULL;
536         GList *attachments_list = NULL;
537
538         /* Add attachments */
539         if (msg != NULL) {
540                 parts = TNY_LIST (tny_simple_list_new());
541                 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
542                 tny_list_foreach (parts, add_if_attachment, &attachments_list);
543         }
544
545         new_msg = create_reply_forward_mail (msg, header, from, signature, TRUE, reply_type, attachments_list);
546         if (attachments_list) {
547                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
548                 g_list_free (attachments_list);
549         }
550         if (parts)
551                 g_object_unref (G_OBJECT (parts));
552
553         /* Fill the header */
554         if (header)
555                 g_object_ref (header);
556         else
557                 header = tny_msg_get_header (msg);
558         new_header = tny_msg_get_header (new_msg);
559         reply_to = tny_header_get_replyto (header);
560
561         if (reply_to)
562                 tny_header_set_to (new_header, reply_to);
563         else
564                 tny_header_set_to (new_header, tny_header_get_from (header));
565
566         switch (reply_mode) {
567         case MODEST_TNY_MSG_REPLY_MODE_SENDER:
568                 /* Do not fill neither cc nor bcc */
569                 break;
570         case MODEST_TNY_MSG_REPLY_MODE_LIST:
571                 /* TODO */
572                 break;
573         case MODEST_TNY_MSG_REPLY_MODE_ALL:
574                 /* Concatenate to, cc and bcc */
575                 cc = tny_header_get_cc (header);
576                 bcc = tny_header_get_bcc (header);
577
578                 tmp = g_string_new (tny_header_get_to (header));
579                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
580                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
581
582                /* Remove my own address from the cc list. TODO:
583                   remove also the To: of the new message, needed due
584                   to the new reply_to feature */
585                 new_cc = (gchar *)
586                         modest_text_utils_remove_address ((const gchar *) tmp->str,
587                                                           from);
588                 /* FIXME: remove also the mails from the new To: */
589                 tny_header_set_cc (new_header, new_cc);
590
591                 /* Clean */
592                 g_string_free (tmp, TRUE);
593                 g_free (new_cc);
594                 break;
595         }
596
597         /* Clean */
598         g_object_unref (G_OBJECT (new_header));
599         g_object_unref (G_OBJECT (header));
600
601         return new_msg;
602 }
603
604
605 static gboolean
606 is_ascii(const gchar *s)
607 {
608         if (!s)
609                 return TRUE;
610         while (s[0]) {
611                 if (s[0] & 128 || s[0] < 32)
612                         return FALSE;
613                 s++;
614         }
615         return TRUE;
616 }
617
618 static char *
619 get_content_type(const gchar *s)
620 {
621         GString *type;
622         
623         type = g_string_new("text/plain");
624         if (!is_ascii(s)) {
625                 if (g_utf8_validate(s, -1, NULL)) {
626                         g_string_append(type, "; charset=\"utf-8\"");
627                 } else {
628                         /* it should be impossible to reach this, but better safe than sorry */
629                         g_warning("invalid utf8 in message");
630                         g_string_append(type, "; charset=\"latin1\"");
631                 }
632         }
633         return g_string_free(type, FALSE);
634 }