[balsa/69-resend-message: 6/11] Various: Refactor resending a message




commit 35db0456aafd15a92890e53bbb38657c3462bfc0
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue Mar 1 14:29:33 2022 -0500

    Various: Refactor resending a message
    
    Move the GMimeMessage manipulation from src/sendmsg-window.c to libbalsa/send.c;
    see https://gitlab.gnome.org/GNOME/balsa/-/issues/69#note_1396270 and replies.
    
    Also fix saving and continuing a resend message.
    
    modified:   libbalsa/send.c
    modified:   src/balsa-index.c
    modified:   src/message-window.c
    modified:   src/sendmsg-window.c
    modified:   src/sendmsg-window.h

 libbalsa/send.c      | 100 +++++++++--
 src/balsa-index.c    |   2 +-
 src/message-window.c |   4 +-
 src/sendmsg-window.c | 470 +++++++++++++++++++++++----------------------------
 src/sendmsg-window.h |   4 +-
 5 files changed, 303 insertions(+), 277 deletions(-)
---
diff --git a/libbalsa/send.c b/libbalsa/send.c
index e69e250f7..e67f5430f 100644
--- a/libbalsa/send.c
+++ b/libbalsa/send.c
@@ -445,7 +445,8 @@ lbs_message_queue_real(LibBalsaMessage    *message,
                 g_mime_part_set_content_encoding(GMIME_PART
                                                      (mime_msgs[i]->mime_part),
                                                  GMIME_CONTENT_ENCODING_7BIT);
-                libbalsa_set_message_id(mime_msgs[i]);
+                if (i > 0)
+                    libbalsa_set_message_id(mime_msgs[i]);
             }
             if (rc) {
                 /* Temporarily modify message by changing its mime_msg: */
@@ -1576,6 +1577,7 @@ libbalsa_message_postpone(LibBalsaMessage *message,
                           GError         **error)
 {
     GMimeMessage *mime_message;
+    gboolean resending;
 
     mime_message = libbalsa_message_get_mime_message(message);
     if (mime_message == NULL) {
@@ -1585,6 +1587,17 @@ libbalsa_message_postpone(LibBalsaMessage *message,
             return FALSE;
     }
 
+    resending = libbalsa_message_get_user_header(message, "X-Balsa-Resend") != NULL;
+    if (resending) {
+        /* Cosmetics: copy the resent message's subject to the containing message,
+         * so that it displays helpfully in the draftbox */
+        GMimeObject *part = g_mime_message_get_mime_part(mime_message);
+        GMimeMessage *resend_mime_message =
+            g_mime_message_part_get_message(GMIME_MESSAGE_PART(part));
+        const char *subject = g_mime_message_get_subject(resend_mime_message);
+        g_mime_message_set_subject(mime_message, subject, NULL);
+    }
+
     if (extra_headers != NULL) {
         GMimeObject *object = GMIME_OBJECT(mime_message);
         gint i;
@@ -1628,7 +1641,6 @@ libbalsa_set_message_id(GMimeMessage *mime_message)
     gchar *message_id;
     guint8 *src;
     gchar *dst;
-    gboolean is_resend;
 
     g_mutex_lock(&mutex);
     if (rand == NULL) {
@@ -1678,19 +1690,76 @@ libbalsa_set_message_id(GMimeMessage *mime_message)
         }
     }
 
-    is_resend = g_mime_object_get_header(GMIME_OBJECT(mime_message), "Resent-From") != NULL;
-    if (is_resend) {
-        g_mime_object_prepend_header(GMIME_OBJECT(mime_message),
-                                     "Resent-Message-ID",
-                                     message_id,
-                                     NULL);
-    } else {
-        g_mime_message_set_message_id(mime_message, message_id);
-    }
+    g_mime_message_set_message_id(mime_message, message_id);
 
     g_free(message_id);
 }
 
+static void
+lbs_prepend_resent_header(GMimeObject         *object,
+                          InternetAddressList *list,
+                          const char          *field)
+{
+    if (ia_list_not_empty(list)) {
+        char *value = internet_address_list_to_string(list, NULL, FALSE);
+        g_mime_object_prepend_header(object, field, value, NULL);
+        g_free(value);
+    }
+}
+
+static void
+lbs_add_resent_headers(LibBalsaMessage *message, GMimeMessage *mime_message)
+{
+    LibBalsaMessageHeaders *headers = libbalsa_message_get_headers(message);
+    GMimeObject *object = GMIME_OBJECT(mime_message);
+    const char *mailbox = "";
+    char *value;
+    GDateTime *date_time;
+
+    lbs_prepend_resent_header(object, headers->bcc_list, "Resent-BCC");
+    lbs_prepend_resent_header(object, headers->cc_list, "Resent-CC");
+    lbs_prepend_resent_header(object, headers->to_list, "Resent-To");
+
+    if (ia_list_not_empty(headers->from)) {
+        InternetAddress *ia = internet_address_list_get_address(headers->from, 0);
+        while (ia != NULL && INTERNET_ADDRESS_IS_GROUP(ia))
+            ia = internet_address_list_get_address(INTERNET_ADDRESS_GROUP( ia)->members, 0);
+        if (ia != NULL)
+            mailbox = INTERNET_ADDRESS_MAILBOX(ia)->addr;
+    }
+    g_mime_object_prepend_header(object, "Resent-From", mailbox, NULL);
+
+    date_time = g_date_time_new_from_unix_local(headers->date);
+    value = g_mime_utils_header_format_date(date_time);
+    g_date_time_unref(date_time);
+    g_mime_object_prepend_header(object, "Resent-Date", value, NULL);
+    g_free(value);
+}
+
+static void
+create_mime_message_resend(LibBalsaMessage  *message,
+                           const char       *message_id,
+                           GMimeMessage    **return_message)
+{
+    GMimeObject *part;
+    GMimeMessage *mime_message;
+
+    part = g_mime_message_get_mime_part(*return_message);
+    mime_message = g_mime_message_part_get_message(GMIME_MESSAGE_PART(part));
+
+    g_mime_object_prepend_header(GMIME_OBJECT(mime_message),
+                                 "Resent-Message-ID",
+                                 message_id,
+                                 NULL);
+
+    lbs_add_resent_headers(message, mime_message);
+    libbalsa_message_set_subject(message, g_mime_message_get_subject(mime_message));
+
+    g_object_ref(*return_message);
+    libbalsa_message_set_mime_message(message, mime_message);
+    g_set_object(return_message, mime_message);
+    g_object_unref(*return_message);
+}
 
 /* balsa_create_msg:
    copies message to msg.
@@ -1701,6 +1770,7 @@ libbalsa_create_msg(LibBalsaMessage *message,
                     GError         **error)
 {
     GMimeMessage *mime_message;
+    gboolean resending;
 
     mime_message = libbalsa_message_get_mime_message(message);
     if (mime_message == NULL) {
@@ -1712,6 +1782,14 @@ libbalsa_create_msg(LibBalsaMessage *message,
 
     libbalsa_set_message_id(mime_message);
 
+    resending = libbalsa_message_get_user_header(message, "X-Balsa-Resend") != NULL;
+    if (resending) {
+        const char *message_id = g_mime_message_get_message_id(mime_message);
+        char *resent_message_id = g_strdup_printf("<%s>", message_id);
+        create_mime_message_resend(message, resent_message_id, &mime_message);
+        g_free(resent_message_id);
+    }
+
     return LIBBALSA_MESSAGE_CREATE_OK;
 }
 
diff --git a/src/balsa-index.c b/src/balsa-index.c
index 7297d9a21..3fe4b3b5a 100644
--- a/src/balsa-index.c
+++ b/src/balsa-index.c
@@ -1690,7 +1690,7 @@ bndx_compose_foreach(BalsaIndex * index, SendType send_type)
             sm = sendmsg_window_reply(mailbox, msgno, send_type);
             break;
         case SEND_RESEND:
-            sm = sendmsg_window_resend(mailbox, msgno);
+            sm = sendmsg_window_forward(mailbox, msgno, send_type);
             break;
         case SEND_CONTINUE:
             sm = sendmsg_window_continue(mailbox, msgno);
diff --git a/src/message-window.c b/src/message-window.c
index 933774c3c..e624fbd0f 100644
--- a/src/message-window.c
+++ b/src/message-window.c
@@ -449,7 +449,7 @@ mw_forward_attached_activated(GSimpleAction * action, GVariant * parameter,
 
     sendmsg_window_forward(libbalsa_message_get_mailbox(mw->message),
                            libbalsa_message_get_msgno(mw->message),
-                           TRUE);
+                           SEND_FORWARD_ATTACH);
 }
 
 static void
@@ -462,7 +462,7 @@ mw_forward_inline_activated(GSimpleAction * action, GVariant * parameter,
 
     sendmsg_window_forward(libbalsa_message_get_mailbox(mw->message),
                            libbalsa_message_get_msgno(mw->message),
-                           FALSE);
+                           SEND_FORWARD_INLINE);
 }
 
 #if 0
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index 479cc2c2c..b7ea8208e 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -2973,7 +2973,9 @@ continue_body(BalsaSendmsg * bsmsg, LibBalsaMessage * message)
 
     /* if the first part is of type text/plain with a NULL filename, it
        was the message... */
-    if (body != NULL && body->filename == NULL) {
+    if (body != NULL &&
+        body->filename == NULL &&
+        libbalsa_message_body_type(body) == LIBBALSA_MESSAGE_BODY_TYPE_TEXT) {
         GString *rbdy;
         gchar *body_type = libbalsa_message_body_get_mime_type(body);
         gint llen = -1;
@@ -4926,86 +4928,34 @@ sw_required_charset(BalsaSendmsg * bsmsg, const gchar * text)
     return charset;
 }
 
-static void
-bsmsg_add_resent_header(BalsaSendmsg *bsmsg,
-                        GMimeObject  *mime_object,
-                        const char   *address_view_type,
-                        const char   *field)
-{
-    InternetAddressList *list;
-    char *value;
-
-    list = libbalsa_address_view_get_list(bsmsg->recipient_view, address_view_type);
-    value = internet_address_list_to_string(list, NULL, FALSE);
-    g_mime_object_prepend_header(mime_object, field, value, NULL);
-    g_free(value);
-}
-
-static LibBalsaMessage *
-bsmsg2resent_message(BalsaSendmsg *bsmsg)
-{
-    LibBalsaMessage *message;
-    GMimeMessage *mime_message;
-    GMimeObject *mime_object;
-    InternetAddress *ia;
-    char *value;
-    GDateTime *date_time;
-
-    message = libbalsa_message_new();
-
-    mime_message = libbalsa_message_get_mime_message(bsmsg->parent_message);
-    libbalsa_message_set_mime_message(message, g_object_ref(mime_message));
-
-    mime_object = GMIME_OBJECT(mime_message);
-
-    ia = libbalsa_identity_get_address(bsmsg->ident);
-    value = internet_address_to_string(ia, NULL, FALSE);
-    g_mime_object_prepend_header(mime_object, "Resent-From", value, NULL);
-    g_free(value);
-
-    bsmsg_add_resent_header(bsmsg, mime_object, "To:", "Resent-To");
-    bsmsg_add_resent_header(bsmsg, mime_object, "CC:", "Resent-Cc");
-    bsmsg_add_resent_header(bsmsg, mime_object, "BCC:", "Resent-Bcc");
-
-    /* get the fcc-box from the option menu widget */
-    bsmsg->fcc_url =
-        g_strdup(balsa_mblist_mru_option_menu_get(bsmsg->fcc[1]));
-
-    date_time = g_date_time_new_now_local();
-    value = g_mime_utils_header_format_date(date_time);
-    g_date_time_unref(date_time);
-    g_mime_object_prepend_header(mime_object, "Resent-Date", value, NULL);
-    g_free(value);
-
-    return message;
-}
-
 static LibBalsaMessage *
 bsmsg2message(BalsaSendmsg * bsmsg)
 {
     LibBalsaMessage *message;
     LibBalsaMessageHeaders *headers;
     LibBalsaMessageBody *body;
-    gchar *tmp;
     GtkTextIter start, end;
     LibBalsaIdentity *ident = bsmsg->ident;
     InternetAddress *ia = libbalsa_identity_get_address(ident);
     GtkTextBuffer *buffer;
     GtkTextBuffer *new_buffer = NULL;
 
+    message = libbalsa_message_new();
+
     if (bsmsg->type == SEND_RESEND)
-        return bsmsg2resent_message(bsmsg);
+        libbalsa_message_set_user_header(message, "X-Balsa-Resend", "message is to be resent");
 
-    message = libbalsa_message_new();
     headers = libbalsa_message_get_headers(message);
 
     headers->from = internet_address_list_new ();
     internet_address_list_add(headers->from, ia);
 
-    tmp = gtk_editable_get_chars(GTK_EDITABLE(bsmsg->subject[1]), 0, -1);
-    strip_chars(tmp, "\r\n");
-    libbalsa_message_set_subject(message, tmp);
-    g_free(tmp);
+    if (bsmsg->subject[1] != NULL) {
+        char *subject = gtk_editable_get_chars(GTK_EDITABLE(bsmsg->subject[1]), 0, -1);
+        strip_chars(subject, "\r\n");
+        libbalsa_message_set_subject(message, subject);
+        g_free(subject);
+    }
 
     headers->to_list =
         libbalsa_address_view_get_list(bsmsg->recipient_view, "To:");
@@ -5021,92 +4971,97 @@ bsmsg2message(BalsaSendmsg * bsmsg)
     bsmsg->fcc_url =
         g_strdup(balsa_mblist_mru_option_menu_get(bsmsg->fcc[1]));
 
-    headers->reply_to =
-        libbalsa_address_view_get_list(bsmsg->replyto_view, "Reply To:");
-
-    if (bsmsg->req_mdn)
-       libbalsa_message_set_dispnotify(message, ia);
     libbalsa_message_set_request_dsn(message, bsmsg->req_dsn);
 
+    if (bsmsg->type != SEND_RESEND) {
+        headers->reply_to = libbalsa_address_view_get_list(bsmsg->replyto_view, "Reply To:");
 
-    sw_set_header_from_path(message, "Face", libbalsa_identity_get_face_path(ident),
-            /* Translators: please do not translate Face. */
-                            _("Could not load Face header file %s: %s"));
-    sw_set_header_from_path(message, "X-Face", libbalsa_identity_get_x_face_path(ident),
-            /* Translators: please do not translate Face. */
-                            _("Could not load X-Face header file %s: %s"));
+        if (bsmsg->req_mdn)
+            libbalsa_message_set_dispnotify(message, ia);
 
-    libbalsa_message_set_references(message, bsmsg->references);
-    bsmsg->references = NULL; /* steal it */
 
-    if (bsmsg->in_reply_to != NULL) {
-        libbalsa_message_set_in_reply_to(message,
-                                         g_list_prepend(NULL,
-                                                        g_strdup(bsmsg->in_reply_to)));
-    }
+        sw_set_header_from_path(message, "Face", libbalsa_identity_get_face_path(ident),
+                /* Translators: please do not translate Face. */
+                                _("Could not load Face header file %s: %s"));
+        sw_set_header_from_path(message, "X-Face", libbalsa_identity_get_x_face_path(ident),
+                /* Translators: please do not translate Face. */
+                                _("Could not load X-Face header file %s: %s"));
 
-    body = libbalsa_message_body_new(message);
+        libbalsa_message_set_references(message, bsmsg->references);
+        bsmsg->references = NULL; /* steal it */
 
-    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
-    gtk_text_buffer_get_bounds(buffer, &start, &end);
+        if (bsmsg->in_reply_to != NULL) {
+            libbalsa_message_set_in_reply_to(message,
+                                             g_list_prepend(NULL,
+                                                            g_strdup(bsmsg->in_reply_to)));
+        }
 
-    if (bsmsg->flow) {
-        /* Copy the message text to a new buffer: */
-        GtkTextTagTable *table;
+        body = libbalsa_message_body_new(message);
 
-        table = gtk_text_buffer_get_tag_table(buffer);
-        new_buffer = gtk_text_buffer_new(table);
+        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
+        gtk_text_buffer_get_bounds(buffer, &start, &end);
 
-        tmp = gtk_text_iter_get_text(&start, &end);
-        gtk_text_buffer_set_text(new_buffer, tmp, -1);
-        g_free(tmp);
+        if (bsmsg->flow) {
+            /* Copy the message text to a new buffer: */
+            GtkTextTagTable *table;
+            char *text;
 
-        /* Remove spaces before a newline: */
-        gtk_text_buffer_get_bounds(new_buffer, &start, &end);
-        libbalsa_unwrap_buffer(new_buffer, &start, -1);
-        gtk_text_buffer_get_bounds(new_buffer, &start, &end);
-    }
-
-    /* Copy the buffer text to the message: */
-    body->buffer = gtk_text_iter_get_text(&start, &end);
-    if (new_buffer)
-        g_object_unref(new_buffer);
-
-    if (bsmsg->send_mp_alt)
-        body->html_buffer =
-            libbalsa_text_to_html(LIBBALSA_MESSAGE_GET_SUBJECT(message), body->buffer,
-                                  bsmsg->spell_check_lang);
-    if (bsmsg->flow)
-       body->buffer =
-           libbalsa_wrap_rfc2646(body->buffer, balsa_app.wraplength,
-                                  TRUE, FALSE, TRUE);
-
-    /* Ildar reports that, when a message contains both text/plain and
-     * text/html parts, some broken MUAs use the charset from the
-     * text/plain part to display the text/html part; the latter is
-     * encoded as UTF-8 by add_mime_body_plain (send.c), so we'll use
-     * the same encoding for the text/plain part.
-     * https://bugzilla.gnome.org/show_bug.cgi?id=580704 */
-    body->charset =
-        g_strdup(bsmsg->send_mp_alt ?
-                 "UTF-8" : sw_required_charset(bsmsg, body->buffer));
-    libbalsa_message_append_part(message, body);
+            table = gtk_text_buffer_get_tag_table(buffer);
+            new_buffer = gtk_text_buffer_new(table);
+
+            text = gtk_text_iter_get_text(&start, &end);
+            gtk_text_buffer_set_text(new_buffer, text, -1);
+            g_free(text);
+
+            /* Remove spaces before a newline: */
+            gtk_text_buffer_get_bounds(new_buffer, &start, &end);
+            libbalsa_unwrap_buffer(new_buffer, &start, -1);
+            gtk_text_buffer_get_bounds(new_buffer, &start, &end);
+        }
+
+        /* Copy the buffer text to the message: */
+        body->buffer = gtk_text_iter_get_text(&start, &end);
+        if (new_buffer)
+            g_object_unref(new_buffer);
+
+        if (bsmsg->send_mp_alt)
+            body->html_buffer =
+                libbalsa_text_to_html(LIBBALSA_MESSAGE_GET_SUBJECT(message), body->buffer,
+                                      bsmsg->spell_check_lang);
+        if (bsmsg->flow)
+            body->buffer =
+                libbalsa_wrap_rfc2646(body->buffer, balsa_app.wraplength,
+                                      TRUE, FALSE, TRUE);
+
+        /* Ildar reports that, when a message contains both text/plain and
+         * text/html parts, some broken MUAs use the charset from the
+         * text/plain part to display the text/html part; the latter is
+         * encoded as UTF-8 by add_mime_body_plain (send.c), so we'll use
+         * the same encoding for the text/plain part.
+         * https://bugzilla.gnome.org/show_bug.cgi?id=580704 */
+        body->charset =
+            g_strdup(bsmsg->send_mp_alt ?
+                     "UTF-8" : sw_required_charset(bsmsg, body->buffer));
+        libbalsa_message_append_part(message, body);
+
+        if (balsa_app.has_openpgp || balsa_app.has_smime) {
+            libbalsa_message_set_gpg_mode(message,
+                (bsmsg->gpg_mode & LIBBALSA_PROTECT_MODE) != 0 ? bsmsg->gpg_mode : 0);
+            libbalsa_message_set_attach_pubkey(message, bsmsg->attach_pubkey);
+            libbalsa_message_set_identity(message, ident);
+        } else {
+            libbalsa_message_set_gpg_mode(message, 0);
+            libbalsa_message_set_attach_pubkey(message, FALSE);
+        }
+    }
 
     /* add attachments */
-    if (bsmsg->tree_view)
+    if (bsmsg->tree_view != NULL) {
         gtk_tree_model_foreach(BALSA_MSG_ATTACH_MODEL(bsmsg),
                                attachment2message, message);
+    }
 
     headers->date = time(NULL);
-    if (balsa_app.has_openpgp || balsa_app.has_smime) {
-        libbalsa_message_set_gpg_mode(message,
-            (bsmsg->gpg_mode & LIBBALSA_PROTECT_MODE) != 0 ? bsmsg->gpg_mode : 0);
-        libbalsa_message_set_attach_pubkey(message, bsmsg->attach_pubkey);
-        libbalsa_message_set_identity(message, ident);
-    } else {
-        libbalsa_message_set_gpg_mode(message, 0);
-        libbalsa_message_set_attach_pubkey(message, FALSE);
-    }
 
     /* remember the parent window */
     g_object_set_data(G_OBJECT(message), "parent-window",
@@ -5475,7 +5430,6 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
     LibBalsaMsgCreateResult result;
     LibBalsaMessage *message;
     LibBalsaMailbox *fcc;
-    LibBalsaMailbox *parent_mailbox = NULL;
     GtkTreeIter iter;
     GError * error = NULL;
 
@@ -5551,11 +5505,6 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
                                _("sending message with GPG mode %d"),
                                libbalsa_message_get_gpg_mode(message));
 
-    if (bsmsg->type == SEND_RESEND)
-        parent_mailbox = libbalsa_message_get_mailbox(bsmsg->parent_message);
-    if (parent_mailbox != NULL)
-        libbalsa_mailbox_lock_store(parent_mailbox);
-
     if(queue_only)
        result = libbalsa_message_queue(message, balsa_app.outbox, fcc,
                                        libbalsa_identity_get_smtp_server(bsmsg->ident),
@@ -5567,10 +5516,6 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
                                                                           balsa_app.send_progress_dialog,
                                        GTK_WINDOW(balsa_app.main_window),
                                        bsmsg->flow, &error);
-
-    if (parent_mailbox != NULL)
-        libbalsa_mailbox_unlock_store(parent_mailbox);
-
     if (result == LIBBALSA_MESSAGE_CREATE_OK) {
        if (bsmsg->parent_message != NULL) {
             LibBalsaMailbox *mailbox = libbalsa_message_get_mailbox(bsmsg->parent_message);
@@ -6552,8 +6497,7 @@ sendmsg_window_new_from_list(LibBalsaMailbox * mailbox,
     switch(type) {
     case SEND_FORWARD_ATTACH:
     case SEND_FORWARD_INLINE:
-        bsmsg = sendmsg_window_forward(mailbox, msgno,
-                                       type == SEND_FORWARD_ATTACH);
+        bsmsg = sendmsg_window_forward(mailbox, msgno, type);
         break;
     default:
         g_assert_not_reached(); /* since it hardly makes sense... */
@@ -6719,8 +6663,7 @@ sendmsg_window_set_title(BalsaSendmsg * bsmsg)
         break;
 
     case SEND_RESEND:
-        title_format = _("Resend message to %s: %s");
-        subject_string = LIBBALSA_MESSAGE_GET_SUBJECT(bsmsg->parent_message);
+        title_format = _("Resend message to %s");
         break;
 
     default:
@@ -6732,7 +6675,7 @@ sendmsg_window_set_title(BalsaSendmsg * bsmsg)
     to_string = internet_address_list_to_string(list, NULL, FALSE);
     g_object_unref(list);
 
-    if (subject_string == NULL)
+    if (bsmsg->subject[1] != NULL)
         subject_string = gtk_entry_get_text(GTK_ENTRY(bsmsg->subject[1]));
 
     title = g_strdup_printf(title_format, to_string ? to_string : "", subject_string);
@@ -7177,41 +7120,67 @@ sendmsg_window_reply_embedded(LibBalsaMessageBody *part,
 }
 
 BalsaSendmsg*
-sendmsg_window_forward(LibBalsaMailbox *mailbox, guint msgno,
-                       gboolean attach)
+sendmsg_window_forward(LibBalsaMailbox *mailbox, guint msgno, SendType send_type)
 {
-    LibBalsaMessage *message =
-        libbalsa_mailbox_get_message(mailbox, msgno);
-    SendType send_type = attach ? SEND_FORWARD_ATTACH : SEND_FORWARD_INLINE;
-    BalsaSendmsg *bsmsg = sendmsg_window_new(send_type);
+    LibBalsaMessage *message;
+    BalsaSendmsg *bsmsg;
 
-    g_assert(message);
+    g_return_val_if_fail(LIBBALSA_IS_MAILBOX(mailbox), NULL);
+    message = libbalsa_mailbox_get_message(mailbox, msgno);
+    g_return_val_if_fail(LIBBALSA_IS_MESSAGE(message), NULL);
 
-    if (attach) {
-       if(!attach_message(bsmsg, message))
-            balsa_information_parented(GTK_WINDOW(bsmsg->window),
-                                       LIBBALSA_INFORMATION_WARNING,
-                                       _("Attaching message failed.\n"
-                                         "Possible reason: not enough temporary space"));
-        bsmsg->state = SENDMSG_STATE_CLEAN;
-        bsmsg_set_subject_from_body(bsmsg, libbalsa_message_get_body_list(message), bsmsg->ident);
-    } else {
-        bsm_prepare_for_setup(message);
-        fill_body_from_message(bsmsg, message, QUOTE_NOPREFIX);
-        bsm_finish_setup(bsmsg, libbalsa_message_get_body_list(message));
+    bsmsg = sendmsg_window_new(send_type);
+
+    switch (send_type) {
+        case SEND_FORWARD_ATTACH:
+            if (!attach_message(bsmsg, message)) {
+                balsa_information_parented(GTK_WINDOW(bsmsg->window),
+                                           LIBBALSA_INFORMATION_WARNING,
+                                           _("Attaching message failed.\n"
+                                             "Possible reason: not enough temporary space"));
+            }
+            bsmsg->state = SENDMSG_STATE_CLEAN;
+            bsmsg_set_subject_from_body(bsmsg, libbalsa_message_get_body_list(message), bsmsg->ident);
+            if (libbalsa_identity_get_sig_whenforward(bsmsg->ident))
+                insert_initial_sig(bsmsg);
+
+            break;
+
+        case SEND_RESEND:
+            if (!attach_message(bsmsg, message)) {
+                balsa_information_parented(GTK_WINDOW(bsmsg->window),
+                                           LIBBALSA_INFORMATION_WARNING,
+                                           _("Attaching message failed.\n"
+                                             "Possible reason: not enough temporary space"));
+            }
+
+            break;
+
+        case SEND_FORWARD_INLINE:
+            {
+                GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
+                GtkTextIter pos;
+
+                bsm_prepare_for_setup(message);
+                fill_body_from_message(bsmsg, message, QUOTE_NOPREFIX);
+                bsm_finish_setup(bsmsg, libbalsa_message_get_body_list(message));
+
+                if (libbalsa_identity_get_sig_whenforward(bsmsg->ident))
+                    insert_initial_sig(bsmsg);
+
+                gtk_text_buffer_get_start_iter(buffer, &pos);
+                gtk_text_buffer_place_cursor(buffer, &pos);
+                gtk_text_buffer_insert_at_cursor(buffer, "\n", 1);
+                gtk_text_buffer_get_start_iter(buffer, &pos);
+                gtk_text_buffer_place_cursor(buffer, &pos);
+            }
+
+            break;
+
+        default:
+            g_assert_not_reached();
     }
-    if (libbalsa_identity_get_sig_whenforward(bsmsg->ident))
-        insert_initial_sig(bsmsg);
-    if(!attach) {
-        GtkTextBuffer *buffer =
-            gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsmsg->text));
-        GtkTextIter pos;
-        gtk_text_buffer_get_start_iter(buffer, &pos);
-        gtk_text_buffer_place_cursor(buffer, &pos);
-        gtk_text_buffer_insert_at_cursor(buffer, "\n", 1);
-        gtk_text_buffer_get_start_iter(buffer, &pos);
-        gtk_text_buffer_place_cursor(buffer, &pos);
-     }
+
     return bsmsg;
 }
 
@@ -7220,6 +7189,7 @@ sendmsg_window_continue(LibBalsaMailbox * mailbox, guint msgno)
 {
     LibBalsaMessage *message =
         libbalsa_mailbox_get_message(mailbox, msgno);
+    gboolean resending;
     BalsaSendmsg *bsmsg;
     const gchar *postpone_hdr;
     GList *list, *refs = NULL;
@@ -7234,7 +7204,9 @@ sendmsg_window_continue(LibBalsaMailbox * mailbox, guint msgno)
         return NULL;
     }
 
-    bsmsg = sendmsg_window_new(SEND_CONTINUE);
+    resending = libbalsa_message_get_user_header(message, "X-Balsa-Resend") != NULL;
+    bsmsg = sendmsg_window_new(resending ? SEND_RESEND : SEND_CONTINUE);
+
     bsmsg->is_continue = TRUE;
     bsm_prepare_for_setup(message);
     bsmsg->draft_message = message;
@@ -7243,89 +7215,67 @@ sendmsg_window_continue(LibBalsaMailbox * mailbox, guint msgno)
     set_identity(bsmsg, message);
     setup_headers_from_message(bsmsg, message);
 
-    libbalsa_address_view_set_from_list(bsmsg->replyto_view,
-                                        "Reply To:",
-                                        libbalsa_message_get_headers(message)->reply_to);
-    in_reply_to = libbalsa_message_get_in_reply_to(message);
-    if (in_reply_to != NULL)
-        bsmsg->in_reply_to =
-            g_strconcat("<", in_reply_to->data, ">", NULL);
-
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-Crypto")))
-        bsmsg_setup_gpg_ui_by_mode(bsmsg, atoi(postpone_hdr));
-    postpone_hdr = libbalsa_message_get_user_header(message, "X-Balsa-Att-Pubkey");
-    if (postpone_hdr != NULL) {
-       sw_action_set_active(bsmsg, "attpubkey", atoi(postpone_hdr) != 0);
-    }
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-MDN")))
-        sw_action_set_active(bsmsg, "request-mdn", atoi(postpone_hdr) != 0);
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-DSN")))
-        sw_action_set_active(bsmsg, "request-dsn", atoi(postpone_hdr) != 0);
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-Lang"))) {
-        GtkWidget *langs =
-            gtk_menu_item_get_submenu(GTK_MENU_ITEM
-                                      (bsmsg->current_language_menu));
-        GList *children =
-            gtk_container_get_children(GTK_CONTAINER(langs));
-        set_locale(bsmsg, postpone_hdr);
-        for (list = children; list; list = list->next) {
-            GtkCheckMenuItem *menu_item = list->data;
-            const gchar *lang;
-
-            lang = g_object_get_data(G_OBJECT(menu_item),
-                                     BALSA_LANGUAGE_MENU_LANG);
-            if (strcmp(lang, postpone_hdr) == 0)
-                gtk_check_menu_item_set_active(menu_item, TRUE);
+    if (resending) {
+        continue_body(bsmsg, message);
+    } else {
+        libbalsa_address_view_set_from_list(bsmsg->replyto_view,
+                                            "Reply To:",
+                                            libbalsa_message_get_headers(message)->reply_to);
+        in_reply_to = libbalsa_message_get_in_reply_to(message);
+        if (in_reply_to != NULL)
+            bsmsg->in_reply_to =
+                g_strconcat("<", in_reply_to->data, ">", NULL);
+
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-Crypto")))
+            bsmsg_setup_gpg_ui_by_mode(bsmsg, atoi(postpone_hdr));
+        postpone_hdr = libbalsa_message_get_user_header(message, "X-Balsa-Att-Pubkey");
+        if (postpone_hdr != NULL) {
+            sw_action_set_active(bsmsg, "attpubkey", atoi(postpone_hdr) != 0);
         }
-        g_list_free(children);
-    }
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-Format")))
-        sw_action_set_active(bsmsg, "flowed", strcmp(postpone_hdr, "Fixed"));
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-MP-Alt")))
-        sw_action_set_active(bsmsg, "send-html", !strcmp(postpone_hdr, "yes"));
-    if ((postpone_hdr =
-         libbalsa_message_get_user_header(message, "X-Balsa-Send-Type")))
-        bsmsg->type = atoi(postpone_hdr);
-
-    for (list = libbalsa_message_get_references(message); list != NULL; list = list->next)
-        refs = g_list_prepend(refs, g_strdup(list->data));
-    bsmsg->references = g_list_reverse(refs);
-
-    continue_body(bsmsg, message);
-    bsm_finish_setup(bsmsg, libbalsa_message_get_body_list(message));
-    g_idle_add((GSourceFunc) sw_grab_focus_to_text,
-               g_object_ref(bsmsg->text));
-    return bsmsg;
-}
-
-/*
- * sendmsg_window_resend
- *
- * Opens a small compose window for resending the message to new
- * recipient(s).
- *
- * https://datatracker.ietf.org/doc/html/rfc5322#section-3.6.6
- */
-BalsaSendmsg *
-sendmsg_window_resend(LibBalsaMailbox *mailbox, guint msgno)
-{
-    LibBalsaMessage *message;
-    BalsaSendmsg *bsmsg;
-
-    g_return_val_if_fail(LIBBALSA_IS_MAILBOX(mailbox), NULL);
-    message = libbalsa_mailbox_get_message(mailbox, msgno);
-    g_return_val_if_fail(LIBBALSA_IS_MESSAGE(message), NULL);
-
-    bsmsg = sendmsg_window_new(SEND_RESEND);
-    bsmsg->parent_message = message;
-    libbalsa_mailbox_open(mailbox, NULL);
-    set_identity(bsmsg, message);
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-MDN")))
+            sw_action_set_active(bsmsg, "request-mdn", atoi(postpone_hdr) != 0);
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-DSN")))
+            sw_action_set_active(bsmsg, "request-dsn", atoi(postpone_hdr) != 0);
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-Lang"))) {
+            GtkWidget *langs =
+                gtk_menu_item_get_submenu(GTK_MENU_ITEM
+                                          (bsmsg->current_language_menu));
+            GList *children =
+                gtk_container_get_children(GTK_CONTAINER(langs));
+            set_locale(bsmsg, postpone_hdr);
+            for (list = children; list; list = list->next) {
+                GtkCheckMenuItem *menu_item = list->data;
+                const gchar *lang;
+
+                lang = g_object_get_data(G_OBJECT(menu_item),
+                                         BALSA_LANGUAGE_MENU_LANG);
+                if (strcmp(lang, postpone_hdr) == 0)
+                    gtk_check_menu_item_set_active(menu_item, TRUE);
+            }
+            g_list_free(children);
+        }
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-Format")))
+            sw_action_set_active(bsmsg, "flowed", strcmp(postpone_hdr, "Fixed"));
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-MP-Alt")))
+            sw_action_set_active(bsmsg, "send-html", !strcmp(postpone_hdr, "yes"));
+        if ((postpone_hdr =
+             libbalsa_message_get_user_header(message, "X-Balsa-Send-Type")))
+            bsmsg->type = atoi(postpone_hdr);
+
+        for (list = libbalsa_message_get_references(message); list != NULL; list = list->next)
+            refs = g_list_prepend(refs, g_strdup(list->data));
+        bsmsg->references = g_list_reverse(refs);
+
+        continue_body(bsmsg, message);
+        bsm_finish_setup(bsmsg, libbalsa_message_get_body_list(message));
+        g_idle_add((GSourceFunc) sw_grab_focus_to_text, g_object_ref(bsmsg->text));
+    }
 
     return bsmsg;
 }
diff --git a/src/sendmsg-window.h b/src/sendmsg-window.h
index 0ab21e7ae..d1125ec3b 100644
--- a/src/sendmsg-window.h
+++ b/src/sendmsg-window.h
@@ -114,10 +114,8 @@ G_BEGIN_DECLS
     BalsaSendmsg *sendmsg_window_reply_embedded(LibBalsaMessageBody *part,
                                                 SendType reply_type);
 
-    BalsaSendmsg *sendmsg_window_resend(LibBalsaMailbox *mailbox, guint msgno);
-
     BalsaSendmsg *sendmsg_window_forward(LibBalsaMailbox *,
-                                         guint msgno, gboolean attach);
+                                         guint msgno, SendType send_type);
     BalsaSendmsg *sendmsg_window_continue(LibBalsaMailbox *,
                                           guint msgno);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]