sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit f8ca3a0d051d0e3158383c0dd3b289a2eeb0f1d4
parent 8b98322a2a6bb0cbc6ea8901eface46ea6f24e92
Author: Gaudenz Steinlin <gaudenz@soziologie.ch>
Date:   Tue, 12 Oct 2010 23:20:53 +0200

Encode multipart messages for crypt operations

Sup crashed when trying to encode a multipart message for crypto
operations. This encodes each part of the message separately. It also
changes the encoding for text/* parts to quoted-printable.

This only concerns the transfer encoding and does not change the charset
in any way.

Diffstat:
M lib/sup/modes/edit-message-mode.rb | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/lib/sup/modes/edit-message-mode.rb b/lib/sup/modes/edit-message-mode.rb
@@ -403,8 +403,11 @@ protected
     if @crypto_selector && @crypto_selector.val != :none
       from_email = Person.from_address(@header["From"]).email
       to_email = [@header["To"], @header["Cc"], @header["Bcc"]].flatten.compact.map { |p| Person.from_address(p).email }
-      m.header["Content-Transfer-Encoding"] = 'base64'
-      m.body = [m.body].pack('m')
+      if m.multipart?
+        m.each_part {|p| p = transfer_encode p}
+      else
+        m = transfer_encode m
+      end
 
       m = CryptoManager.send @crypto_selector.val, from_email, to_email, m
     end
@@ -520,6 +523,25 @@ private
       []
     end
   end
+
+  def transfer_encode msg_part
+    ## return the message unchanged if it's already encoded
+    if (msg_part.header["Content-Transfer-Encoding"] == "base64" ||
+        msg_part.header["Content-Transfer-Encoding"] == "quoted-printable")
+      return msg_part
+    end
+
+    ## encode to quoted-printable for all text/* MIME types,
+    ## use base64 otherwise
+    if msg_part.header["Content-Type"] =~ /text\/.*/
+      msg_part.header["Content-Transfer-Encoding"] = 'quoted-printable'
+      msg_part.body = [msg_part.body].pack('M')
+    else
+      msg_part.header["Content-Transfer-Encoding"] = 'base64'
+      msg_part.body = [msg_part.body].pack('m')
+    end
+    msg_part
+  end
 end
 
 end