sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 4eaef3e76dfddd2a0792a89ddb5bb2dae3af6b40
parent 7e2bb889e617302c82e28d04cb2e0db5d35fbb08
Author: Damien Leone <damien.leone@fensalir.fr>
Date:   Sun, 27 Feb 2011 19:05:35 +0100

edit-message-mode: Add an optional account selector

Set to true by default, it allows you to change your account in
edit-mode using a horizontal selector. It doesn't change any of the
previous behaviour since the proper account is selected by default, it
also handles customized choice in case the user edited the From field
manually.

Signed-off-by: Hamish Downer 

Diffstat:
M lib/sup.rb | 1 +
M lib/sup/modes/edit-message-mode.rb | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -297,6 +297,7 @@ EOS
         :ask_for_cc => true,
         :ask_for_bcc => false,
         :ask_for_subject => true,
+        :account_selector => true,
         :confirm_no_attachments => true,
         :confirm_top_posting => true,
         :jump_to_open_message => true,
diff --git a/lib/sup/modes/edit-message-mode.rb b/lib/sup/modes/edit-message-mode.rb
@@ -114,6 +114,27 @@ EOS
     @selectors = []
     @selector_label_width = 0
 
+    if $config[:account_selector]
+      ## Duplicate e-mail strings to prevent a "can't modify frozen
+      ## object" crash triggered by the String::display_length()
+      ## method in util.rb
+      user_emails_copy = []
+      AccountManager.user_emails.each { |e| user_emails_copy.push e.dup }
+
+      @account_selector =
+        HorizontalSelector.new "Account:", AccountManager.user_accounts + [nil], user_emails_copy + ["Customized"]
+
+      if @header["From"] =~ /<?(\S+@(\S+?))>?$/
+        @account_selector.set_to AccountManager.account_for($1)
+        @account_user = ""
+      else
+        @account_selector.set_to nil
+        @account_user = @header["From"]
+      end
+
+      add_selector @account_selector
+    end
+
     @crypto_selector =
       if CryptoManager.have_crypto?
         HorizontalSelector.new "Crypto:", [:none] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.keys, ["None"] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.values
@@ -162,6 +183,8 @@ EOS
   def edit_subject; edit_field "Subject" end
 
   def edit_message
+    old_from = @header["From"] if @account_selector
+
     @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
     @file.puts format_headers(@header - NON_EDITABLE_HEADERS).first
     @file.puts
@@ -178,6 +201,12 @@ EOS
 
     header, @body = parse_file @file.path
     @header = header - NON_EDITABLE_HEADERS
+
+    if @account_selector and @header["From"] != old_from
+      @account_user = @header["From"]
+      @account_selector.set_to nil
+    end
+
     handle_new_text @header, @body
     update
 
@@ -242,6 +271,7 @@ protected
     if curpos < @selectors.length
       @selectors[curpos].roll_left
       buffer.mark_dirty
+      update if @account_selector
     else
       col_left
     end
@@ -251,6 +281,7 @@ protected
     if curpos < @selectors.length
       @selectors[curpos].roll_right
       buffer.mark_dirty
+      update if @account_selector
     else
       col_right
     end
@@ -262,6 +293,11 @@ protected
   end
 
   def update
+    if @account_selector
+      account = @account_selector.val
+      @header["From"] = account && account.full_address || @account_user
+    end
+
     regen_text
     buffer.mark_dirty if buffer
   end
@@ -479,6 +515,13 @@ protected
       if contacts
         text = contacts.map { |s| s.full_address }.join(", ")
         @header[field] = parse_header field, text
+
+        if @account_selector and field == "From"
+          @account_user = @header["From"]
+          @account_selector.set_to nil
+        end
+
+        rerun_crypto_selector_hook
         update
       end
     end