+
+
+static gint
+count_addresses (const gchar* addresses)
+{
+ gint count = 1;
+
+ if (!addresses)
+ return 0;
+
+ while (*addresses) {
+ if (*addresses == ',' || *addresses == ';')
+ ++count;
+ ++addresses;
+ }
+
+ return count;
+}
+
+static void
+remove_undisclosed_recipients (gchar **recipients)
+{
+ GSList *addresses, *node;
+ gboolean is_first;
+ GString *result;
+
+ g_return_if_fail (recipients);
+ addresses = modest_text_utils_split_addresses_list (*recipients);
+
+ is_first = TRUE;
+ result = g_string_new ("");
+ for (node = addresses; node != NULL; node = g_slist_next (node)) {
+ const gchar *address = (const gchar *) node->data;
+
+ if (address && strstr (address, "undisclosed-recipients"))
+ continue;
+
+ if (is_first)
+ is_first = FALSE;
+ else
+ result = g_string_append (result, ", ");
+
+ result = g_string_append (result, address);
+ }
+ g_slist_foreach (addresses, (GFunc)g_free, NULL);
+ g_slist_free (addresses);
+
+ g_free (*recipients);
+ *recipients = g_string_free (result, FALSE);
+}
+
+
+/* get the new To:, based on the old header,
+ * result is newly allocated or NULL in case of error
+ * */
+static gchar*
+get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
+ ModestTnyMsgReplyMode reply_mode)
+{
+ const gchar *reply_header = "Reply-To:";
+ const gchar *from_header = "From:";
+ gchar* old_reply_to;
+ gchar* old_from;
+ gchar* new_to;
+ gchar* tmp;
+
+ /* according to RFC2369 (http://www.faqs.org/rfcs/rfc2369.html), we
+ * can identify Mailing-List posts by the List-Help header.
+ * for mailing lists, both the Reply-To: and From: should be included
+ * in the new To:; for now, we're ignoring List-Post
+ */
+ gchar* list_help = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg),
+ "List-Help");
+ gboolean is_mailing_list = (list_help != NULL);
+ g_free (list_help);
+
+
+ /* reply to sender, use ReplyTo or From */
+ 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) {
+ g_debug ("%s: failed to get either Reply-To: or From: from header",
+ __FUNCTION__);
+ return NULL;
+ }
+
+ /* Prevent DoS attacks caused by malformed emails */
+ if (old_from) {
+ gchar *tmp = old_from;
+ old_from = modest_text_utils_get_secure_header ((const gchar *) tmp, from_header);
+ g_free (tmp);
+ }
+ if (old_reply_to) {
+ gchar *tmp = old_reply_to;
+ old_reply_to = modest_text_utils_get_secure_header ((const gchar *) tmp, reply_header);
+ g_free (tmp);
+ }
+
+ /* for mailing lists, use both Reply-To and From if we did a
+ * 'Reply All:'
+ * */
+ if (is_mailing_list && reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL &&
+ old_reply_to && old_from && strcmp (old_from, old_reply_to) != 0)
+ new_to = g_strjoin (",", old_reply_to, old_from, NULL);
+ else
+ /* 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) {
+ gchar *old_to = tny_header_dup_to (header);
+ if (!old_to)
+ g_debug ("%s: no To: address found in source mail",
+ __FUNCTION__);
+ else {
+ /* append the old To: */
+ gchar *tmp = g_strjoin (",", new_to, old_to, NULL);
+ g_free (new_to);
+ new_to = tmp;
+ g_free (old_to);
+ }
+
+ /* remove duplicate entries */
+ gchar *tmp = modest_text_utils_remove_duplicate_addresses (new_to);
+ g_free (new_to);
+ new_to = tmp;
+
+ /* now, strip me (the new From:) from the new_to, but only if
+ * there are >1 addresses there */
+ if (count_addresses (new_to) > 1) {
+ gchar *tmp = modest_text_utils_remove_address (new_to, from);
+ g_free (new_to);
+ new_to = tmp;
+ }
+ }
+
+ tmp = modest_text_utils_simplify_recipients (new_to);
+ remove_undisclosed_recipients (&tmp);
+ g_free (new_to);
+ new_to = tmp;
+
+ return new_to;
+}
+
+
+/* get the new Cc:, based on the old header,
+ * result is newly allocated or NULL in case of error */
+static gchar*
+get_new_cc (TnyHeader *header, const gchar* from, const gchar *new_to)
+{
+ 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 */
+ dup = modest_text_utils_remove_address (old_cc, from);
+
+ if (new_to) {
+ gchar **to_parts, **current;
+
+ to_parts = g_strsplit (new_to, ",", 0);
+ for (current = to_parts; current && *current != '\0'; current++) {
+ gchar *dup2;
+
+ dup2 = modest_text_utils_remove_address (dup, g_strstrip (*current));
+ g_free (dup);
+ dup = dup2;
+ }
+ g_strfreev (to_parts);
+ }
+
+ result = modest_text_utils_remove_duplicate_addresses (dup);
+ g_free (dup);
+ dup = result;
+ result = modest_text_utils_simplify_recipients (dup);
+ remove_undisclosed_recipients (&result);
+ 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_ascii_strcasecmp (name, "References")) {
+ if (l_references) g_free (l_references);
+ l_references = g_strdup (tny_pair_get_value (pair));
+ } else if (!g_ascii_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_ascii_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);
+ }
+}
+