[balsa/69-resend-message: 7/16] sendmsg-window: Clean up case SEND_RESEND




commit 42b2efb5d6fe9208ccb1b6b6bb8142d55c7f9ce2
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu Feb 24 14:18:09 2022 -0500

    sendmsg-window: Clean up case SEND_RESEND
    
    Reimplement sendmsg_window_resend(), and fix various issues.

 src/sendmsg-window.c | 232 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 159 insertions(+), 73 deletions(-)
---
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index 6c77b0844..da04be6ee 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -1042,16 +1042,18 @@ update_bsmsg_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity* ident)
     gtk_combo_box_set_active(GTK_COMBO_BOX(bsmsg->from[1]),
                              g_list_index(balsa_app.identities, ident));
 
-    addr = libbalsa_identity_get_replyto(ident);
-    if (addr != NULL && addr[0] != '\0') {
-        libbalsa_address_view_set_from_string(bsmsg->replyto_view,
-                                              "Reply To:",
-                                              addr);
-        gtk_widget_show(bsmsg->replyto[0]);
-        gtk_widget_show(bsmsg->replyto[1]);
-    } else if (!sw_action_get_active(bsmsg, "reply-to")) {
-        gtk_widget_hide(bsmsg->replyto[0]);
-        gtk_widget_hide(bsmsg->replyto[1]);
+    if (bsmsg->replyto[0] != NULL) {
+        addr = libbalsa_identity_get_replyto(ident);
+        if (addr != NULL && addr[0] != '\0') {
+            libbalsa_address_view_set_from_string(bsmsg->replyto_view,
+                                                  "Reply To:",
+                                                  addr);
+            gtk_widget_show(bsmsg->replyto[0]);
+            gtk_widget_show(bsmsg->replyto[1]);
+        } else if (!sw_action_get_active(bsmsg, "reply-to")) {
+            gtk_widget_hide(bsmsg->replyto[0]);
+            gtk_widget_hide(bsmsg->replyto[1]);
+        }
     }
 
     addr = libbalsa_identity_get_bcc(bsmsg->ident);
@@ -1103,7 +1105,9 @@ update_bsmsg_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity* ident)
         g_object_unref(bcc_list);
     }
 
-    if (bsmsg->type != SEND_RESEND) {
+    if (bsmsg->type == SEND_RESEND) {
+        g_set_object(&bsmsg->ident, ident);
+    } else {
         GtkTextBuffer *buffer;
         GtkTextIter start, end;
         gint replace_offset = 0;
@@ -1148,12 +1152,12 @@ update_bsmsg_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity* ident)
         old_forward_string = libbalsa_identity_get_forward_string(old_ident);
 
         if (((replen = strlen(old_forward_string)) > 0) &&
-       (strncmp(subject, old_reply_string, replen) == 0)) {
+            (strncmp(subject, old_reply_string, replen) == 0)) {
             tmpstr = g_strconcat(reply_string, &(subject[replen]), NULL);
             gtk_entry_set_text(GTK_ENTRY(bsmsg->subject[1]), tmpstr);
             g_free(tmpstr);
         } else if (((fwdlen = strlen(old_forward_string)) > 0) &&
-              (strncmp(subject, old_forward_string, fwdlen) == 0)) {
+                   (strncmp(subject, old_forward_string, fwdlen) == 0)) {
             tmpstr = g_strconcat(forward_string, &(subject[fwdlen]), NULL);
             gtk_entry_set_text(GTK_ENTRY(bsmsg->subject[1]), tmpstr);
             g_free(tmpstr);
@@ -1199,34 +1203,34 @@ update_bsmsg_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity* ident)
             message_split = g_strsplit(message_text, "\n-- \n", 0);
             siglen = g_utf8_strlen(old_sig, -1);
 
-       /* check the special case of starting a message with a sig */
-       compare_str = g_strconcat("\n", message_split[0], NULL);
+            /* check the special case of starting a message with a sig */
+            compare_str = g_strconcat("\n", message_split[0], NULL);
 
-       if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
-           g_free(compare_str);
-           replace_identity_signature(bsmsg, ident, old_ident,
+            if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
+                g_free(compare_str);
+                replace_identity_signature(bsmsg, ident, old_ident,
                                            &replace_offset, siglen - 1, new_sig);
-           found_sig = TRUE;
-       } else {
+                found_sig = TRUE;
+            } else {
                 gint i;
 
                 g_free(compare_str);
                 for (i = 0; message_split[i] != NULL; i++) {
-               /* put sig separator back to search */
-               compare_str = g_strconcat("\n-- \n", message_split[i], NULL);
+                    /* put sig separator back to search */
+                    compare_str = g_strconcat("\n-- \n", message_split[i], NULL);
 
-               /* try to find occurance of old signature */
-               if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
-                   replace_identity_signature(bsmsg, ident, old_ident,
+                    /* try to find occurance of old signature */
+                    if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
+                        replace_identity_signature(bsmsg, ident, old_ident,
                                                    &replace_offset, siglen,
                                                    new_sig);
-                   found_sig = TRUE;
-               }
+                        found_sig = TRUE;
+                    }
 
-               replace_offset +=
-                   g_utf8_strlen(i ? compare_str : message_split[i], -1);
-               g_free(compare_str);
-           }
+                    replace_offset +=
+                        g_utf8_strlen(i ? compare_str : message_split[i], -1);
+                    g_free(compare_str);
+                }
             }
             /* if no sig seperators found, do a slower brute force
              * approach.  We could have stopped earlier if the message was
@@ -1236,28 +1240,28 @@ update_bsmsg_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity* ident)
                 compare_str = message_text;
                 replace_offset = 0;
 
-           /* check the special case of starting a message with a sig */
-           tmpstr = g_strconcat("\n", message_text, NULL);
+                /* check the special case of starting a message with a sig */
+                tmpstr = g_strconcat("\n", message_text, NULL);
 
-           if (g_ascii_strncasecmp(old_sig, tmpstr, siglen) == 0) {
-               g_free(tmpstr);
-               replace_identity_signature(bsmsg, ident, old_ident,
+                if (g_ascii_strncasecmp(old_sig, tmpstr, siglen) == 0) {
+                    g_free(tmpstr);
+                    replace_identity_signature(bsmsg, ident, old_ident,
                                                &replace_offset, siglen - 1,
                                                new_sig);
-           } else {
-               g_free(tmpstr);
-               replace_offset++;
-               compare_str = g_utf8_next_char(compare_str);
-               while (*compare_str) {
-                   if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
-                       replace_identity_signature(bsmsg, ident, old_ident,
+                } else {
+                    g_free(tmpstr);
+                    replace_offset++;
+                    compare_str = g_utf8_next_char(compare_str);
+                    while (*compare_str) {
+                        if (g_ascii_strncasecmp(old_sig, compare_str, siglen) == 0) {
+                            replace_identity_signature(bsmsg, ident, old_ident,
                                                        &replace_offset, siglen,
                                                        new_sig);
-                   }
-                   replace_offset++;
-                   compare_str = g_utf8_next_char(compare_str);
-               }
-           }
+                        }
+                        replace_offset++;
+                        compare_str = g_utf8_next_char(compare_str);
+                    }
+                }
             }
             g_strfreev(message_split);
         }
@@ -4074,11 +4078,13 @@ setup_headers_from_identity(BalsaSendmsg* bsmsg, LibBalsaIdentity *ident)
     gtk_combo_box_set_active(GTK_COMBO_BOX(bsmsg->from[1]),
                              g_list_index(balsa_app.identities, ident));
 
-    addr = libbalsa_identity_get_replyto(ident);
-    if (addr != NULL)
-        libbalsa_address_view_set_from_string(bsmsg->replyto_view,
-                                              "Reply To:",
-                                              addr);
+    if (bsmsg->replyto_view != NULL) {
+        addr = libbalsa_identity_get_replyto(ident);
+        if (addr != NULL)
+            libbalsa_address_view_set_from_string(bsmsg->replyto_view,
+                                                  "Reply To:",
+                                                  addr);
+    }
 
     addr = libbalsa_identity_get_bcc(ident);
     if (addr != NULL)
@@ -4914,6 +4920,60 @@ 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)
 {
@@ -4927,6 +4987,9 @@ bsmsg2message(BalsaSendmsg * bsmsg)
     GtkTextBuffer *buffer;
     GtkTextBuffer *new_buffer = NULL;
 
+    if (bsmsg->type == SEND_RESEND)
+        return bsmsg2resent_message(bsmsg);
+
     message = libbalsa_message_new();
     headers = libbalsa_message_get_headers(message);
 
@@ -5061,6 +5124,9 @@ subject_not_empty(BalsaSendmsg * bsmsg)
     GtkWidget *subj_entry;
     gint response;
 
+    if (bsmsg->subject[1] == NULL)
+        return TRUE;
+
     /* read the subject widget and verify that it is contains something else
        than spaces */
     subj = gtk_entry_get_text(GTK_ENTRY(bsmsg->subject[1]));
@@ -5403,6 +5469,7 @@ send_message_handler(BalsaSendmsg * bsmsg, gboolean queue_only)
     LibBalsaMsgCreateResult result;
     LibBalsaMessage *message;
     LibBalsaMailbox *fcc;
+    LibBalsaMailbox *parent_mailbox = NULL;
     GtkTreeIter iter;
     GError * error = NULL;
 
@@ -5478,6 +5545,11 @@ 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),
@@ -5489,6 +5561,10 @@ 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);
@@ -6053,8 +6129,9 @@ check_readiness(BalsaSendmsg * bsmsg)
 {
     gboolean ready =
         libbalsa_address_view_n_addresses(bsmsg->recipient_view) > 0;
-    if (ready
-        && libbalsa_address_view_n_addresses(bsmsg->replyto_view) < 0)
+    if (ready &&
+        bsmsg->replyto_view != NULL &&
+        libbalsa_address_view_n_addresses(bsmsg->replyto_view) < 0)
         ready = FALSE;
 
     bsmsg->ready_to_send = ready;
@@ -6612,6 +6689,7 @@ sendmsg_window_set_title(BalsaSendmsg * bsmsg)
     gchar *title_format;
     InternetAddressList *list;
     gchar *to_string;
+    const gchar *subject_string = NULL;
     gchar *title;
 
     switch (bsmsg->type) {
@@ -6626,6 +6704,11 @@ sendmsg_window_set_title(BalsaSendmsg * bsmsg)
         title_format = _("Forward message to %s: %s");
         break;
 
+    case SEND_RESEND:
+        title_format = _("Resend message to %s: %s");
+        subject_string = LIBBALSA_MESSAGE_GET_SUBJECT(bsmsg->parent_message);
+        break;
+
     default:
         title_format = _("New message to %s: %s");
         break;
@@ -6635,8 +6718,10 @@ sendmsg_window_set_title(BalsaSendmsg * bsmsg)
     to_string = internet_address_list_to_string(list, NULL, FALSE);
     g_object_unref(list);
 
-    title = g_strdup_printf(title_format, to_string ? to_string : "",
-                            gtk_entry_get_text(GTK_ENTRY(bsmsg->subject[1])));
+    if (subject_string == NULL)
+        subject_string = gtk_entry_get_text(GTK_ENTRY(bsmsg->subject[1]));
+
+    title = g_strdup_printf(title_format, to_string ? to_string : "", subject_string);
     g_free(to_string);
     gtk_window_set_title(GTK_WINDOW(bsmsg->window), title);
     g_free(title);
@@ -6940,8 +7025,6 @@ sendmsg_window_new(SendType send_type)
        g_signal_connect(balsa_app.main_window, "delete-event",
                         G_CALLBACK(delete_event_cb), bsmsg);
 
-        setup_headers_from_identity(bsmsg, bsmsg->ident);
-
         /* Finish setting up the spell checker */
 #if HAVE_GSPELL || HAVE_GTKSPELL
         if (current_locale != NULL) {
@@ -6951,6 +7034,8 @@ sendmsg_window_new(SendType send_type)
         set_locale(bsmsg, current_locale);
     }
 
+    setup_headers_from_identity(bsmsg, bsmsg->ident);
+
     return bsmsg;
 }
 
@@ -7205,27 +7290,28 @@ sendmsg_window_continue(LibBalsaMailbox * mailbox, guint msgno)
     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 =
-        libbalsa_mailbox_get_message(mailbox, msgno);
-    char *name, *tmp_file_name;
+    LibBalsaMessage *message;
     BalsaSendmsg *bsmsg;
 
-    g_assert(message != NULL);
-
-    if (!libbalsa_mktempdir(&tmp_file_name))
-       return NULL;
-    name = g_strdup_printf("%s/resend-message", tmp_file_name);
-    g_free(tmp_file_name);
-
-    if (!libbalsa_message_save(message, name)) {
-        g_free(name);
-        return NULL;
-    }
+    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);
 
     return bsmsg;
 }


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