From ac632cc9bfe5815303f9dfa671e41f4f88678f07 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Mon, 19 Jul 2010 23:33:00 +0200
Subject: [PATCH] Recode attachments to UTF-8 when forwarding
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is necessary because rmail crashes if we don’t (and the attachment
is encoded in Windows-1252, for example).
---
 lib/sup/message-chunks.rb     |    4 +++-
 lib/sup/message.rb            |    2 +-
 lib/sup/modes/forward-mode.rb |    2 +-
 lib/sup/util.rb               |    9 ++++++++-
 4 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb
index c275f41..cc613af 100644
--- a/lib/sup/message-chunks.rb
+++ b/lib/sup/message-chunks.rb
@@ -83,8 +83,9 @@ EOS
     attr_reader :content_type, :filename, :lines, :raw_content
     bool_reader :quotable
 
-    def initialize content_type, filename, encoded_content, sibling_types
+    def initialize content_type, charset, filename, encoded_content, sibling_types
       @content_type = content_type.downcase
+      @charset = charset
       @filename = filename
       @quotable = false # changed to true if we can parse it through the
                         # mime-decode hook, or if it's plain text
@@ -113,6 +114,7 @@ EOS
       end
     end
 
+    def charset; @charset end
     def color; :none end
     def patina_color; :attachment_color end
     def patina_text
diff --git a/lib/sup/message.rb b/lib/sup/message.rb
index 9f488af..57f1df1 100644
--- a/lib/sup/message.rb
+++ b/lib/sup/message.rb
@@ -505,7 +505,7 @@ private
         @attachments.push filename.downcase unless filename =~ /^sup-attachment-/
         add_label :attachment unless filename =~ /^sup-attachment-/
         content_type = (m.header.content_type || "application/unknown").downcase # sometimes RubyMail gives us nil
-        [Chunk::Attachment.new(content_type, filename, m, sibling_types)]
+        [Chunk::Attachment.new(content_type, m.charset, filename, m, sibling_types)]
 
       ## otherwise, it's body text
       else
diff --git a/lib/sup/modes/forward-mode.rb b/lib/sup/modes/forward-mode.rb
index 9428b4b..0779b4a 100644
--- a/lib/sup/modes/forward-mode.rb
+++ b/lib/sup/modes/forward-mode.rb
@@ -43,7 +43,7 @@ class ForwardMode < EditMessageMode
 
     attachments.each do |c|
       mime_type = MIME::Types[c.content_type].first || MIME::Types["application/octet-stream"].first
-      attachment_hash[c.filename] = RMail::Message.make_attachment c.raw_content, mime_type.content_type, mime_type.encoding, c.filename
+      attachment_hash[c.filename] = RMail::Message.make_attachment c.raw_content, mime_type.content_type, mime_type.encoding, c.filename, c.charset
     end
 
     mode = ForwardMode.new :message => opts[:message], :to => to, :cc => cc, :bcc => bcc, :attachments => attachment_hash
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index d19caca..31cf824 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -79,11 +79,18 @@ module RMail
       end
     end
 
-    def self.make_attachment payload, mime_type, encoding, filename
+    def self.make_attachment payload, mime_type, encoding, filename, charset
       a = Message.new
       a.header.add "Content-Disposition", "attachment; filename=#{filename.inspect}"
       a.header.add "Content-Type", "#{mime_type}; name=#{filename.inspect}"
       a.header.add "Content-Transfer-Encoding", encoding if encoding
+      ## if we have charset information, we recode the attachment to UTF-8 because
+      ## rmail will not accept it otherwise. This is a bit kludgy since a mailer
+      ## should not touch forwarded content, IMO (but it's better than crashing).
+      if charset
+        a.header.add "Content-Charset", "UTF-8"
+        payload = payload.force_encoding(charset).encode("UTF-8")
+      end
       a.body =
         case encoding
         when "base64"
-- 
1.6.5

