1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
48 #endif /*HAVE_CONFIG_H */
50 #include <modest-tny-msg.h>
51 #include "modest-text-utils.h"
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);
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,
67 GList *attachments, gint *attached, GError **err)
72 gint tmp_attached = 0;
75 new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL), FALSE);
76 header = tny_msg_get_header (new_msg);
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);
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);
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);
92 if ((subject != NULL) && (strlen(subject) > 0))
93 tny_header_set_subject (TNY_HEADER (header), subject);
95 content_type = get_content_type(body);
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.
101 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
105 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
108 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
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);
115 /* Add attachments */
117 tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
119 *attached = tmp_attached;
121 g_object_unref(header);
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)
139 new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL), (images != NULL));
140 header = tny_msg_get_header (new_msg);
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);
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);
153 if ((subject != NULL) && (strlen(subject) > 0))
154 tny_header_set_subject (TNY_HEADER (header), subject);
156 content_type = get_content_type(plain_body);
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.
162 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
166 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
169 tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
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);
176 /* Add attachments */
177 tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
179 *attached = tmp_attached;
180 add_images (new_msg, images, err);
182 g_object_unref(header);
188 /* FIXME: this func copy from modest-mail-operation: refactor */
190 add_body_part (TnyMsg *msg,
192 const gchar *content_type)
194 TnyMimePart *text_body_part = NULL;
195 TnyStream *text_body_stream;
197 /* Create the stream */
198 text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
199 (body, (body ? strlen(body) : 0)));
201 text_body_part = modest_formatter_create_body_part (NULL, msg);
203 /* Construct MIME part */
204 tny_stream_reset (text_body_stream);
205 tny_mime_part_construct (text_body_part,
207 content_type, "7bit");
208 tny_stream_reset (text_body_stream);
210 g_object_unref (G_OBJECT(text_body_part));
213 g_object_unref (text_body_stream);
215 return text_body_part;
219 add_html_body_part (TnyMsg *msg,
222 TnyMimePart *html_body_part = NULL;
223 TnyStream *html_body_stream;
225 /* Create the stream */
226 html_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
227 (body, strlen(body)));
229 /* Create body part if needed */
230 html_body_part = modest_formatter_create_body_part (NULL, msg);
232 /* Construct MIME part */
233 tny_stream_reset (html_body_stream);
234 tny_mime_part_construct (html_body_part,
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);
241 g_object_unref (G_OBJECT(html_body_part));
244 g_object_unref (html_body_stream);
246 return html_body_part;
250 copy_mime_part (TnyMimePart *part, GError **err)
252 TnyMimePart *result = NULL;
253 const gchar *attachment_content_type;
254 const gchar *attachment_filename;
255 const gchar *attachment_cid;
257 TnyIterator *iterator;
258 TnyStream *attachment_stream;
262 if (TNY_IS_MSG (part)) {
267 result = tny_platform_factory_new_mime_part (
268 modest_runtime_get_platform_factory());
270 attachment_content_type = tny_mime_part_get_content_type (part);
272 /* get mime part headers */
273 attachment_filename = tny_mime_part_get_filename (part);
274 attachment_cid = tny_mime_part_get_content_id (part);
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);
285 ret = tny_stream_reset (attachment_stream);
286 ret = tny_mime_part_construct (result,
288 attachment_content_type,
290 ret = tny_stream_reset (attachment_stream);
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);
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));
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);
310 tny_mime_part_set_content_id (result, subpart_cid);
311 g_object_unref (subpart_copy);
313 g_object_unref (subpart);
316 tny_iterator_next (iterator);
318 g_object_unref (iterator);
319 g_object_unref (parts);
320 g_object_unref (attachment_stream);
326 add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err)
329 TnyMimePart *attachment_part, *old_attachment;
333 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
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) {
342 tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
345 const gchar *filename;
346 filename = tny_mime_part_get_filename (old_attachment);
348 tny_mime_part_set_filename (attachment_part, filename);
350 tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
353 tny_mime_part_set_transfer_encoding (TNY_MIME_PART (attachment_part), "base64");
354 ret = tny_mime_part_add_part (TNY_MIME_PART (part), attachment_part);
357 tny_mime_part_set_content_id (attachment_part, old_cid);
358 g_object_unref (attachment_part);
366 add_images (TnyMsg *msg, GList *images_list, GError **err)
368 TnyMimePart *related_part = NULL;
369 const gchar *content_type;
371 content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
373 if ((content_type != NULL) && !strcasecmp (content_type, "multipart/related")) {
374 related_part = g_object_ref (msg);
375 } else if ((content_type != NULL) && !strcasecmp (content_type, "multipart/mixed")) {
376 TnyList *parts = TNY_LIST (tny_simple_list_new ());
377 TnyIterator *iter = NULL;
378 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
379 iter = tny_list_create_iterator (parts);
381 while (!tny_iterator_is_done (iter)) {
382 TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
383 if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
388 g_object_unref (part);
389 tny_iterator_next (iter);
391 g_object_unref (iter);
392 g_object_unref (parts);
395 if (related_part != NULL) {
396 /* TODO: attach images in their proper place */
397 add_attachments (related_part, images_list, TRUE, err);
398 g_object_unref (related_part);
404 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html, gboolean *is_html)
409 GtkTextIter start, end;
411 gboolean result_was_html = TRUE;
413 g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
415 body = modest_tny_msg_find_body_part(msg, want_html);
419 buf = gtk_text_buffer_new (NULL);
420 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
421 tny_stream_reset (stream);
422 tny_mime_part_decode_to_stream (body, stream, NULL);
423 tny_stream_reset (stream);
425 gtk_text_buffer_get_bounds (buf, &start, &end);
426 to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
427 if (tny_mime_part_content_type_is (body, "text/plain")) {
428 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
430 to_quote = to_quote_converted;
431 result_was_html = FALSE;
434 g_object_unref (buf);
435 g_object_unref (G_OBJECT(stream));
436 g_object_unref (G_OBJECT(body));
439 *is_html = result_was_html;
446 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
448 const gchar *desired_mime_type = want_html ? "text/html" : "text/plain";
449 TnyMimePart *part = NULL;
450 TnyList *parts = NULL;
451 TnyIterator *iter = NULL;
452 gchar *header_content_type;
453 gchar *header_content_type_lower = NULL;
458 /* If it's an application multipart, then we don't get into as we don't
459 * support them (for example application/sml or wap messages */
460 header_content_type = modest_tny_mime_part_get_header_value (msg, "Content-Type");
461 if (header_content_type) {
462 header_content_type = g_strstrip (header_content_type);
463 header_content_type_lower = g_ascii_strdown (header_content_type, -1);
465 if (header_content_type_lower &&
466 g_str_has_prefix (header_content_type_lower, "multipart/") &&
467 !g_str_has_prefix (header_content_type_lower, "multipart/signed") &&
468 strstr (header_content_type_lower, "application/")) {
469 g_free (header_content_type_lower);
470 g_free (header_content_type);
473 g_free (header_content_type_lower);
474 g_free (header_content_type);
476 parts = TNY_LIST (tny_simple_list_new());
477 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
479 iter = tny_list_create_iterator(parts);
481 /* no parts? assume it's single-part message */
482 if (tny_iterator_is_done(iter)) {
484 gboolean is_text_part;
485 g_object_unref (G_OBJECT(iter));
486 content_type = modest_tny_mime_part_get_content_type (msg);
487 if (content_type == NULL)
490 g_str_has_prefix (content_type, "text/");
491 g_free (content_type);
492 /* if this part cannot be a supported body return NULL */
496 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
500 gchar *tmp, *content_type = NULL;
501 gboolean has_content_disp_name = FALSE;
503 part = TNY_MIME_PART(tny_iterator_get_current (iter));
506 g_warning ("%s: not a valid mime part", __FUNCTION__);
507 tny_iterator_next (iter);
511 /* it's a message --> ignore */
512 if (part && TNY_IS_MSG (part)) {
513 g_object_unref (part);
515 tny_iterator_next (iter);
519 /* we need to strdown the content type, because
520 * tny_mime_part_has_content_type does not do it...
522 content_type = g_ascii_strdown (tny_mime_part_get_content_type (part), -1);
524 /* mime-parts with a content-disposition header (either 'inline' or 'attachment')
525 * and a 'name=' thingy cannot be body parts
528 tmp = modest_tny_mime_part_get_header_value (part, "Content-Disposition");
530 gchar *content_disp = g_ascii_strdown(tmp, -1);
532 has_content_disp_name = g_strstr_len (content_disp, strlen(content_disp), "name=") != NULL;
533 g_free (content_disp);
536 if (g_str_has_prefix (content_type, desired_mime_type) &&
537 !has_content_disp_name &&
538 !modest_tny_mime_part_is_attachment_for_modest (part)) {
539 /* we found the desired mime-type! */
540 g_free (content_type);
543 } else if (g_str_has_prefix(content_type, "multipart")) {
545 /* multipart? recurse! */
546 g_object_unref (part);
547 g_free (content_type);
548 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
552 g_free (content_type);
555 g_object_unref (G_OBJECT(part));
559 tny_iterator_next (iter);
561 } while (!tny_iterator_is_done(iter));
564 g_object_unref (G_OBJECT(iter));
565 g_object_unref (G_OBJECT(parts));
567 /* if were trying to find an HTML part and couldn't find it,
568 * try to find a text/plain part instead
570 if (!part && want_html)
571 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
573 return part; /* this maybe NULL, this is not an error; some message just don't have a body
579 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
581 g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
583 return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
588 #define MODEST_TNY_MSG_PARENT_UID "parent-uid"
591 create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
592 const gchar *signature, gboolean is_reply,
593 guint type /*ignored*/, GList *attachments)
596 TnyHeader *new_header;
599 TnyMimePart *body = NULL;
600 ModestFormatter *formatter;
601 gchar *subject_prefix;
602 gboolean no_text_part;
606 g_object_ref (header);
608 header = tny_msg_get_header (msg);
610 /* Get body from original msg. Always look for the text/plain
611 part of the message to create the reply/forwarded mail */
613 body = modest_tny_msg_find_body_part (msg, FALSE);
615 if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT,
617 formatter = modest_formatter_new ("text/html", signature);
619 formatter = modest_formatter_new ("text/plain", signature);
622 /* if we don't have a text-part */
623 no_text_part = (!body) || (strcmp (tny_mime_part_get_content_type (body), "text/html")==0);
625 /* when we're reply, include the text part if we have it, or nothing otherwise. */
627 new_msg = modest_formatter_quote (formatter, no_text_part ? NULL: body, header,
630 /* for attachements; inline if there is a text part, and include the
631 * full old mail if there was none */
633 new_msg = modest_formatter_attach (formatter, msg, header);
635 new_msg = modest_formatter_inline (formatter, body, header,
640 g_object_unref (G_OBJECT(formatter));
642 g_object_unref (G_OBJECT(body));
644 /* Fill the header */
645 new_header = tny_msg_get_header (new_msg);
646 tny_header_set_from (new_header, from);
647 tny_header_set_replyto (new_header, from);
649 /* Change the subject */
651 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
653 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
654 old_subject = tny_header_dup_subject (header);
656 (gchar *) modest_text_utils_derived_subject (old_subject,
658 g_free (old_subject);
659 g_free (subject_prefix);
660 tny_header_set_subject (new_header, (const gchar *) new_subject);
661 g_free (new_subject);
663 /* get the parent uid, and set it as a gobject property on the new msg */
664 parent_uid = modest_tny_folder_get_header_unique_id (header);
665 g_object_set_data_full (G_OBJECT(new_msg), MODEST_TNY_MSG_PARENT_UID,
668 /* set modest as the X-Mailer
669 * we could this in the platform factory, but then the header
670 * would show up before all the others.
672 tny_mime_part_set_header_pair (TNY_MIME_PART (msg), "X-Mailer", "Modest "
676 g_object_unref (G_OBJECT (new_header));
677 g_object_unref (G_OBJECT (header));
678 /* ugly to unref it here instead of in the calling func */
680 if (!is_reply & !no_text_part) {
681 add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, NULL);
688 modest_tny_msg_get_parent_uid (TnyMsg *msg)
690 g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
692 return g_object_get_data (G_OBJECT(msg), MODEST_TNY_MSG_PARENT_UID);
695 static gchar *get_signed_protocol (TnyMimePart *part)
697 TnyList *header_pairs;
698 TnyIterator *iterator;
699 gchar *result = NULL;
701 header_pairs = TNY_LIST (tny_simple_list_new ());
702 tny_mime_part_get_header_pairs (part, header_pairs);
703 iterator = tny_list_create_iterator (header_pairs);
705 while (!result && !tny_iterator_is_done (iterator)) {
709 pair = TNY_PAIR (tny_iterator_get_current (iterator));
710 name = tny_pair_get_name (pair);
711 if (name && !g_ascii_strcasecmp (name, "Content-Type")) {
713 s = tny_pair_get_value (pair);
715 s = strstr (s, "protocol=");
721 t = strstr (s, "\"");
725 result = g_strndup (s, t - s);
730 g_object_unref (pair);
731 tny_iterator_next (iterator);
734 g_object_unref (iterator);
735 g_object_unref (header_pairs);
741 modest_tny_msg_get_attachments_parent (TnyMsg *msg)
744 const gchar *content_type;
748 content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
749 if (content_type && !strcmp (content_type, "multipart/signed")) {
750 TnyList *msg_children;
751 TnyIterator *iterator;
752 gchar *signed_protocol;
754 msg_children = TNY_LIST (tny_simple_list_new ());
755 tny_mime_part_get_parts (TNY_MIME_PART (msg), msg_children);
757 iterator = tny_list_create_iterator (msg_children);
758 signed_protocol = get_signed_protocol (TNY_MIME_PART (msg));
760 while (!result && !tny_iterator_is_done (iterator)) {
763 part = TNY_MIME_PART (tny_iterator_get_current (iterator));
764 if (signed_protocol) {
765 const gchar *part_content_type;
767 part_content_type = tny_mime_part_get_content_type (part);
768 if (part_content_type && g_ascii_strcasecmp (part_content_type, signed_protocol)) {
769 result = g_object_ref (part);
772 result = g_object_ref (part);
775 g_object_unref (part);
776 tny_iterator_next (iterator);
779 g_object_unref (iterator);
780 g_free (signed_protocol);
781 g_object_unref (msg_children);
783 if (result == NULL) {
784 result = g_object_ref (msg);
793 add_if_attachment (gpointer data, gpointer user_data)
796 GList **attachments_list;
798 part = TNY_MIME_PART (data);
799 attachments_list = ((GList **) user_data);
801 if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part))) {
802 *attachments_list = g_list_prepend (*attachments_list, part);
808 modest_tny_msg_create_forward_msg (TnyMsg *msg,
810 const gchar *signature,
811 ModestTnyMsgForwardType forward_type)
814 TnyList *parts = NULL;
815 GList *attachments_list = NULL;
816 TnyMimePart *part_to_check = NULL;
818 g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
820 part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
822 /* Add attachments */
823 parts = TNY_LIST (tny_simple_list_new());
824 tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
825 tny_list_foreach (parts, add_if_attachment, &attachments_list);
827 new_msg = create_reply_forward_mail (msg, NULL, from, signature, FALSE, forward_type,
831 if (attachments_list) {
832 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
833 g_list_free (attachments_list);
835 g_object_unref (G_OBJECT (parts));
836 g_object_unref (part_to_check);
844 count_addresses (const gchar* addresses)
852 if (*addresses == ',' || *addresses == ';')
861 /* get the new To:, based on the old header,
862 * result is newly allocated or NULL in case of error
865 get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
866 ModestTnyMsgReplyMode reply_mode)
872 /* according to RFC2369 (http://www.faqs.org/rfcs/rfc2369.html), we
873 * can identify Mailing-List posts by the List-Help header.
874 * for mailing lists, both the Reply-To: and From: should be included
875 * in the new To:; for now, we're ignoring List-Post
877 gchar* list_help = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg),
879 gboolean is_mailing_list = (list_help != NULL);
883 /* reply to sender, use ReplyTo or From */
884 old_reply_to = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg),
886 old_from = tny_header_dup_from (header);
888 if (!old_from && !old_reply_to) {
889 g_warning ("%s: failed to get either Reply-To: or From: from header",
894 /* for mailing lists, use both Reply-To and From if we did a
897 if (is_mailing_list && reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL &&
898 old_reply_to && old_from && strcmp (old_from, old_reply_to) != 0)
899 new_to = g_strjoin (",", old_reply_to, old_from, NULL);
901 /* otherwise use either Reply-To: (preferred) or From: */
902 new_to = g_strdup (old_reply_to ? old_reply_to : old_from);
904 g_free (old_reply_to);
906 /* in case of ReplyAll, we need to add the Recipients in the old To: */
907 if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
908 gchar *old_to = tny_header_dup_to (header);
910 g_warning ("%s: no To: address found in source mail",
913 /* append the old To: */
914 gchar *tmp = g_strjoin (",", new_to, old_to, NULL);
920 /* remove duplicate entries */
921 gchar *tmp = modest_text_utils_remove_duplicate_addresses (new_to);
925 /* now, strip me (the new From:) from the new_to, but only if
926 * there are >1 addresses there */
927 if (count_addresses (new_to) > 1) {
928 gchar *tmp = modest_text_utils_remove_address (new_to, from);
938 /* get the new Cc:, based on the old header,
939 * result is newly allocated or NULL in case of error */
941 get_new_cc (TnyHeader *header, const gchar* from)
943 gchar *old_cc, *result, *dup;
945 old_cc = tny_header_dup_cc (header);
949 /* remove me (the new From:) from the Cc: list */
950 dup = modest_text_utils_remove_address (old_cc, from);
951 result = modest_text_utils_remove_duplicate_addresses (dup);
958 modest_tny_msg_get_references (TnyMsg *msg, gchar **message_id, gchar **references, gchar **in_reply_to)
961 TnyIterator *iterator;
964 gchar *l_in_reply_to;
966 g_return_if_fail (TNY_IS_MSG (msg));
970 l_in_reply_to = NULL;
972 headers = TNY_LIST (tny_simple_list_new ());
973 tny_mime_part_get_header_pairs (TNY_MIME_PART (msg), headers);
975 iterator = tny_list_create_iterator (headers);
976 while (!tny_iterator_is_done (iterator)) {
980 pair = TNY_PAIR (tny_iterator_get_current (iterator));
981 name = tny_pair_get_name (pair);
982 if (!g_strcasecmp (name, "References")) {
983 if (l_references) g_free (l_references);
984 l_references = g_strdup (tny_pair_get_value (pair));
985 } else if (!g_strcasecmp (name, "In-Reply-To")) {
986 if (l_in_reply_to) g_free (l_in_reply_to);
987 l_in_reply_to = g_strdup (tny_pair_get_value (pair));
988 } else if (!g_strcasecmp (name, "Message-ID")) {
989 if (l_message_id) g_free (l_message_id);
990 l_message_id = g_strdup (tny_pair_get_value (pair));
993 g_object_unref (pair);
994 tny_iterator_next (iterator);
997 g_object_unref (iterator);
998 g_object_unref (headers);
1001 *message_id = l_message_id;
1003 g_free (l_message_id);
1007 *in_reply_to = l_in_reply_to;
1009 g_free (l_in_reply_to);
1013 *references = l_references;
1015 g_free (l_references);
1020 set_references (TnyMsg *reply_msg, TnyMsg *original_msg)
1022 gchar *orig_references, *orig_in_reply_to, *orig_message_id;
1023 gchar *references, *in_reply_to;
1025 modest_tny_msg_get_references (original_msg, &orig_message_id, &orig_references, &orig_in_reply_to);
1030 if (orig_message_id)
1031 in_reply_to = g_strdup (orig_message_id);
1033 if (orig_references) {
1034 if (orig_message_id)
1035 references = g_strconcat (orig_references, "\n ", orig_message_id, NULL);
1037 references = g_strdup (orig_references);
1039 } else if (orig_in_reply_to) {
1040 if (orig_message_id)
1041 references = g_strconcat (orig_in_reply_to, "\n ", orig_message_id, NULL);
1043 references = g_strdup (orig_in_reply_to);
1044 } else if (orig_message_id) {
1045 references = g_strdup (orig_message_id);
1048 g_free (orig_references);
1049 g_free (orig_in_reply_to);
1050 g_free (orig_message_id);
1053 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "In-Reply-To", in_reply_to);
1056 tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "References", references);
1061 modest_tny_msg_create_reply_msg (TnyMsg *msg,
1064 const gchar *signature,
1065 ModestTnyMsgReplyType reply_type,
1066 ModestTnyMsgReplyMode reply_mode)
1068 TnyMsg *new_msg = NULL;
1069 TnyHeader *new_header;
1070 gchar *new_to = NULL;
1071 TnyList *parts = NULL;
1072 GList *attachments_list = NULL;
1073 TnyMimePart *part_to_check = NULL;
1075 g_return_val_if_fail (msg && TNY_IS_MSG(msg), NULL);
1077 part_to_check = modest_tny_msg_get_attachments_parent (TNY_MSG (msg));
1079 parts = TNY_LIST (tny_simple_list_new());
1080 tny_mime_part_get_parts (TNY_MIME_PART (part_to_check), parts);
1081 tny_list_foreach (parts, add_if_attachment, &attachments_list);
1083 new_msg = create_reply_forward_mail (msg, header, from, signature, TRUE, reply_type,
1086 set_references (new_msg, msg);
1087 if (attachments_list != NULL) {
1088 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
1089 g_list_free (attachments_list);
1091 g_object_unref (parts);
1093 /* Fill the header */
1095 g_object_ref (header);
1097 header = tny_msg_get_header (msg);
1100 new_header = tny_msg_get_header(new_msg);
1101 new_to = get_new_to (msg, header, from, reply_mode);
1103 g_warning ("%s: failed to get new To:", __FUNCTION__);
1105 tny_header_set_to (new_header, new_to);
1109 if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
1110 gchar *new_cc = get_new_cc (header, from);
1112 tny_header_set_cc (new_header, new_cc);
1118 g_object_unref (G_OBJECT (new_header));
1119 g_object_unref (G_OBJECT (header));
1120 g_object_unref (G_OBJECT (part_to_check));
1127 is_ascii(const gchar *s)
1132 if (s[0] & 128 || s[0] < 32)
1140 get_content_type(const gchar *s)
1144 type = g_string_new("text/plain");
1146 if (g_utf8_validate(s, -1, NULL)) {
1147 g_string_append(type, "; charset=\"utf-8\"");
1149 /* it should be impossible to reach this, but better safe than sorry */
1150 g_warning("invalid utf8 in message");
1151 g_string_append(type, "; charset=\"latin1\"");
1154 return g_string_free(type, FALSE);
1158 modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
1159 guint64 parts_count,
1164 /* estimation of headers size */
1167 /* We add a 20% of size due to the increase in 7bit encoding */
1169 result += strlen (plain_body) * 120 / 100;
1172 result += strlen (html_body) * 120 / 100;
1175 /* 256 bytes per additional part because of their headers */
1176 result += parts_count * 256;
1178 /* 150% of increase per encoding */
1179 result += parts_size * 3 / 2;
1185 modest_tny_msg_header_get_all_recipients_list (TnyHeader *header)
1187 GSList *recipients = NULL;
1188 gchar *from = NULL, *to = NULL, *cc = NULL, *bcc = NULL;
1193 from = tny_header_dup_from (header);
1194 to = tny_header_dup_to (header);
1195 cc = tny_header_dup_cc (header);
1196 bcc = tny_header_dup_bcc (header);
1200 recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (from));
1202 recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (to));
1204 recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (cc));
1206 recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (bcc));
1217 modest_tny_msg_get_all_recipients_list (TnyMsg *msg)
1219 TnyHeader *header = NULL;
1220 GSList *recipients = NULL;
1225 header = tny_msg_get_header (msg);
1229 recipients = modest_tny_msg_header_get_all_recipients_list (header);
1230 g_object_unref (header);