Added support for setting References and In-Reply-To: mime fields
[modest] / src / modest-tny-msg.c
index 23f465f..7f3a030 100644 (file)
@@ -33,6 +33,7 @@
 #include <tny-simple-list.h>
 #include <tny-folder.h>
 #include <modest-runtime.h>
+#include <modest-defs.h>
 #include "modest-formatter.h"
 #include <tny-camel-mem-stream.h>
 #include <tny-camel-mime-part.h>
@@ -60,7 +61,9 @@ static gboolean is_ascii(const gchar *s);
 
 TnyMsg*
 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
-                   const gchar *bcc, const gchar* subject, const gchar *body,
+                   const gchar *bcc, const gchar* subject, 
+                   const gchar *references, const gchar *in_reply_to,
+                   const gchar *body,
                    GList *attachments, gint *attached, GError **err)
 {
        TnyMsg *new_msg;
@@ -76,8 +79,11 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
                tny_header_set_from (TNY_HEADER (header), from);
                tny_header_set_replyto (TNY_HEADER (header), from);
        }
-       if ((mailto != NULL) && (strlen(mailto) > 0)) 
-               tny_header_set_to (TNY_HEADER (header), mailto);
+       if ((mailto != NULL) && (strlen(mailto) > 0)) {
+               gchar *removed_to = modest_text_utils_remove_duplicate_addresses (mailto);
+               tny_header_set_to (TNY_HEADER (header), removed_to);
+               g_free (removed_to);
+       }
        if ((cc != NULL) && (strlen(cc) > 0)) 
                tny_header_set_cc (TNY_HEADER (header), cc);
        if ((bcc != NULL) && (strlen(bcc) > 0)) 
@@ -94,6 +100,12 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
         */
        tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
                                       VERSION);
+
+       if (references)
+               tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
+
+       if (in_reply_to)
+               tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
        
        /* Add the body of the new mail */
        /* This is needed even if body is NULL or empty. */
@@ -114,6 +126,7 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
 TnyMsg*
 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
                               const gchar *bcc, const gchar* subject, 
+                              const gchar *references, const gchar *in_reply_to,
                               const gchar *html_body, const gchar *plain_body,
                               GList *attachments, GList *images, gint *attached, GError **err)
 {
@@ -149,6 +162,12 @@ modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gch
        tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "X-Mailer", "Modest "
                                       VERSION);
 
+       if (references)
+               tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "References", references);
+
+       if (in_reply_to)
+               tny_mime_part_set_header_pair (TNY_MIME_PART (new_msg), "In-Reply-To", in_reply_to);
+       
        /* Add the body of the new mail */      
        add_body_part (new_msg, plain_body, content_type);
        add_html_body_part (new_msg, html_body);
@@ -364,9 +383,9 @@ add_images (TnyMsg *msg, GList *images_list, GError **err)
                        if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
                                related_part = part;
                                break;
-                       } else {
-                               g_object_unref (part);
                        }
+                       if (part)
+                               g_object_unref (part);
                        tny_iterator_next (iter);
                }
                g_object_unref (iter);
@@ -462,19 +481,14 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
        /* no parts? assume it's single-part message */
        if (tny_iterator_is_done(iter)) {
                gchar *content_type;
-               gchar *content_type_lower;
                gboolean is_text_part;
                g_object_unref (G_OBJECT(iter));
-               content_type = g_strdup (tny_mime_part_get_content_type (msg));
+               content_type = modest_tny_mime_part_get_content_type (msg);
                if (content_type == NULL)
                        return NULL;
-               content_type = g_strstrip (content_type);
-               content_type_lower = g_ascii_strdown (content_type, -1);
-               g_free (content_type);
                is_text_part = 
-                       g_str_has_prefix (content_type_lower, "text/") ||
-                       g_str_has_prefix (content_type_lower, "message/rfc822");
-               g_free (content_type_lower);
+                       g_str_has_prefix (content_type, "text/");
+               g_free (content_type);
                /* if this part cannot be a supported body return NULL */
                if (!is_text_part) {
                        return NULL;
@@ -490,6 +504,7 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
 
                        if (!part) {
                                g_warning ("%s: not a valid mime part", __FUNCTION__);
+                               tny_iterator_next (iter);
                                continue;
                        }
 
@@ -585,7 +600,8 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
        ModestFormatter *formatter;
        gchar *subject_prefix;
        gboolean no_text_part;
-       
+       gchar *parent_uid;
+
        if (header)
                g_object_ref (header);
        else
@@ -595,17 +611,17 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
           part of the message to create the reply/forwarded mail */
        if (msg != NULL)
                body   = modest_tny_msg_find_body_part (msg, FALSE);
-       
+
        if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT,
                                  NULL))
                formatter = modest_formatter_new ("text/html", signature);
        else
                formatter = modest_formatter_new ("text/plain", signature);
-       
+
 
        /* if we don't have a text-part */
        no_text_part = (!body) || (strcmp (tny_mime_part_get_content_type (body), "text/html")==0);
-       
+
        /* when we're reply, include the text part if we have it, or nothing otherwise. */
        if (is_reply)
                new_msg = modest_formatter_quote  (formatter, no_text_part ? NULL: body, header,
@@ -620,13 +636,13 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
                                                            attachments);
                }
        }
-       
+
        g_object_unref (G_OBJECT(formatter));
        if (body)
                g_object_unref (G_OBJECT(body));
-       
+
        /* Fill the header */
-       new_header = tny_msg_get_header (new_msg);      
+       new_header = tny_msg_get_header (new_msg);
        tny_header_set_from (new_header, from);
        tny_header_set_replyto (new_header, from);
 
@@ -643,13 +659,11 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
        g_free (subject_prefix);
        tny_header_set_subject (new_header, (const gchar *) new_subject);
        g_free (new_subject);
-       
+
        /* get the parent uid, and set it as a gobject property on the new msg */
-       if (new_msg) {
-               gchar* parent_uid = modest_tny_folder_get_header_unique_id (header);
-               g_object_set_data_full (G_OBJECT(new_msg), MODEST_TNY_MSG_PARENT_UID,
-                                       parent_uid, g_free);
-       }
+       parent_uid = modest_tny_folder_get_header_unique_id (header);
+       g_object_set_data_full (G_OBJECT(new_msg), MODEST_TNY_MSG_PARENT_UID,
+                               parent_uid, g_free);
 
        /* set modest as the X-Mailer
         * we could this in the platform factory, but then the header
@@ -752,7 +766,7 @@ static gchar*
 get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
            ModestTnyMsgReplyMode reply_mode)
 {
-       const gchar* old_reply_to;
+       gchar* old_reply_to;
        gchar* old_from;
        gchar* new_to;
        
@@ -768,12 +782,11 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
 
 
        /* reply to sender, use ReplyTo or From */
-       //old_reply_to = tny_header_get_replyto (header);
        old_reply_to = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
                                                              "Reply-To"); 
        old_from     = tny_header_dup_from (header);
        
-       if (!old_from &&  !old_reply_to) {
+       if (!old_from && !old_reply_to) {
                g_warning ("%s: failed to get either Reply-To: or From: from header",
                           __FUNCTION__);
                return NULL;
@@ -789,6 +802,7 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
                /* otherwise use either Reply-To: (preferred) or From: */
                new_to = g_strdup (old_reply_to ? old_reply_to : old_from);
        g_free (old_from);
+       g_free (old_reply_to);
 
        /* in case of ReplyAll, we need to add the Recipients in the old To: */
        if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
@@ -827,19 +841,122 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
 static gchar*
 get_new_cc (TnyHeader *header, const gchar* from)
 {
-       gchar *old_cc;
-       gchar *result;
+       gchar *old_cc, *result, *dup;
 
        old_cc = tny_header_dup_cc (header);
        if (!old_cc)
                return NULL;
 
        /* remove me (the new From:) from the Cc: list */
-       result =  modest_text_utils_remove_address (old_cc, from);
+       dup =  modest_text_utils_remove_address (old_cc, from);
+       result = modest_text_utils_remove_duplicate_addresses (dup);
+       g_free (dup);
        g_free (old_cc);
        return result;
 }
 
+void 
+modest_tny_msg_get_references (TnyMsg *msg, gchar **message_id, gchar **references, gchar **in_reply_to)
+{
+       TnyList *headers;
+       TnyIterator *iterator;
+       gchar *l_message_id;
+       gchar *l_references;
+       gchar *l_in_reply_to;
+
+       g_return_if_fail (TNY_IS_MSG (msg));
+
+       l_message_id = NULL;
+       l_references = NULL;
+       l_in_reply_to = NULL;
+
+       headers = TNY_LIST (tny_simple_list_new ());
+       tny_mime_part_get_header_pairs (TNY_MIME_PART (msg), headers);
+
+       iterator = tny_list_create_iterator (headers);
+       while (!tny_iterator_is_done (iterator)) {
+               TnyPair *pair;
+               const gchar *name;
+
+               pair = TNY_PAIR (tny_iterator_get_current (iterator));
+               name = tny_pair_get_name (pair);
+               if (!g_strcasecmp (name, "References")) {
+                       if (l_references) g_free (l_references);
+                       l_references = g_strdup (tny_pair_get_value (pair));
+               } else if (!g_strcasecmp (name, "In-Reply-To")) {
+                       if (l_in_reply_to) g_free (l_in_reply_to);
+                       l_in_reply_to = g_strdup (tny_pair_get_value (pair));
+               } else if (!g_strcasecmp (name, "Message-ID")) {
+                       if (l_message_id) g_free (l_message_id);
+                       l_message_id = g_strdup (tny_pair_get_value (pair));
+               }
+
+               g_object_unref (pair);
+               tny_iterator_next (iterator);
+       }
+
+       g_object_unref (iterator);
+       g_object_unref (headers);
+
+       if (message_id) {
+               *message_id = l_message_id;
+       } else {
+               g_free (l_message_id);
+       }
+
+       if (in_reply_to) {
+               *in_reply_to = l_in_reply_to;
+       } else {
+               g_free (l_in_reply_to);
+       }
+
+       if (references) {
+               *references = l_references;
+       } else {
+               g_free (l_references);
+       }
+}
+
+static void 
+set_references (TnyMsg *reply_msg, TnyMsg *original_msg)
+{
+       gchar *orig_references, *orig_in_reply_to, *orig_message_id;
+       gchar *references, *in_reply_to;
+
+       modest_tny_msg_get_references (original_msg, &orig_message_id, &orig_references, &orig_in_reply_to);
+
+       references = NULL;
+       in_reply_to = NULL;
+
+       if (orig_message_id)
+               in_reply_to = g_strdup (orig_message_id);
+
+       if (orig_references) {
+               if (orig_message_id)
+                       references = g_strconcat (orig_references, "\n        ", orig_message_id, NULL);
+               else
+                       references = g_strdup (orig_references);
+
+       } else if (orig_in_reply_to) {
+               if (orig_message_id)
+                       references = g_strconcat (orig_in_reply_to, "\n        ", orig_message_id, NULL);
+               else
+                       references = g_strdup (orig_in_reply_to);
+       } else if (orig_message_id) {
+               references = g_strdup (orig_message_id);
+       }
+
+       g_free (orig_references);
+       g_free (orig_in_reply_to);
+       g_free (orig_message_id);
+
+       if (in_reply_to) {
+               tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "In-Reply-To", in_reply_to);
+       }
+       if (references) {
+               tny_mime_part_set_header_pair (TNY_MIME_PART (reply_msg), "References", references);
+       }
+}
 
 TnyMsg* 
 modest_tny_msg_create_reply_msg (TnyMsg *msg,
@@ -863,12 +980,13 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
 
        new_msg = create_reply_forward_mail (msg, header, from, signature, TRUE, reply_type,
                                             attachments_list);
+
+       set_references (new_msg, msg);
        if (attachments_list != NULL) {
                g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
                g_list_free (attachments_list);
        }
-       if (parts)
-               g_object_unref (G_OBJECT (parts));
+       g_object_unref (parts);
 
        /* Fill the header */
        if (header)
@@ -884,8 +1002,8 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
        else {
                tny_header_set_to (new_header, new_to);
                g_free (new_to);
-       }       
-       
+       }
+
        if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
                gchar *new_cc = get_new_cc (header, from);
                if (new_cc) { 
@@ -893,7 +1011,7 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
                        g_free (new_cc);
                }
        }
-       
+
        /* Clean */
        g_object_unref (G_OBJECT (new_header));
        g_object_unref (G_OBJECT (header));
@@ -959,3 +1077,55 @@ modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
 
        return result;
 }
+
+GSList *
+modest_tny_msg_header_get_all_recipients_list (TnyHeader *header)
+{
+       GSList *recipients = NULL;
+       gchar *from = NULL, *to = NULL, *cc = NULL, *bcc = NULL;
+
+       if (header == NULL)
+               return NULL;
+
+       from = tny_header_dup_from (header);
+       to = tny_header_dup_to (header);
+       cc = tny_header_dup_cc (header);
+       bcc = tny_header_dup_bcc (header);
+
+       recipients = NULL;
+       if (from)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (from));
+       if (to)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (to));
+       if (cc)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (cc));
+       if (bcc)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (bcc));
+
+       g_free (from);
+       g_free (to);
+       g_free (cc);
+       g_free (bcc);
+
+       return recipients;
+}
+
+GSList *
+modest_tny_msg_get_all_recipients_list (TnyMsg *msg)
+{
+       TnyHeader *header = NULL;
+       GSList *recipients = NULL;
+
+       if (msg == NULL)
+               return NULL;
+
+       header = tny_msg_get_header (msg);
+       if (header == NULL)
+               return NULL;
+
+       recipients = modest_tny_msg_header_get_all_recipients_list (header);
+       g_object_unref (header);
+
+       return recipients;
+}
+