Archive of RubyForge sup-devel mailing list
 help / color / mirror / Atom feed
* [sup-devel] (REVIEW NEEDED) [PATCH] Recode attachments to UTF-8 when forwarding
@ 2010-07-19 21:38 Michael Stapelberg
  0 siblings, 0 replies; only message in thread
From: Michael Stapelberg @ 2010-07-19 21:38 UTC (permalink / raw)
  To: sup-devel

[-- Attachment #1: Type: text/plain, Size: 1261 bytes --]

Hi,

can you please review the attached patch? It recodes attachments from whichever
charset they are encoded in to UTF-8 when forwarding messages in order to
please rmail (it crashes with attachments encoded in Windows-1252 otherwise).

FYI, the crash backtrace is the following (pathes shortened):
--- Encoding::CompatibilityError from thread: main
incompatible character encodings: UTF-8 and ASCII-8BIT
rmail-1.0.0/lib/rmail/serialize.rb:112:in `serialize_low'
rmail-1.0.0/lib/rmail/serialize.rb:99:in `block in serialize_low'
rmail-1.0.0/lib/rmail/message.rb:155:in `block in each_part'
rmail-1.0.0/lib/rmail/message.rb:154:in `each'
rmail-1.0.0/lib/rmail/message.rb:154:in `each_part'
rmail-1.0.0/lib/rmail/serialize.rb:96:in `serialize_low'
rmail-1.0.0/lib/rmail/serialize.rb:63:in `serialize'
rmail-1.0.0/lib/rmail/message.rb:146:in `to_s'
sup/lib/sup/modes/edit-message-mode.rb:344:in `puts'
sup/lib/sup/modes/edit-message-mode.rb:344:in `block in send_message'
sup/lib/sup/modes/edit-message-mode.rb:344:in `popen'
sup/lib/sup/modes/edit-message-mode.rb:344:in `send_message'
sup/lib/sup/mode.rb:59:in `handle_input'
sup/lib/sup/buffer.rb:277:in `handle_input'

So, if you have a better way of handling this, please let us know.

Best regards,
Michael

[-- Attachment #2: 0001-Recode-attachments-to-UTF-8-when-forwarding.patch --]
[-- Type: application/octet-stream, Size: 3970 bytes --]

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


[-- Attachment #3: Type: text/plain, Size: 143 bytes --]

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-07-19 21:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-19 21:38 [sup-devel] (REVIEW NEEDED) [PATCH] Recode attachments to UTF-8 when forwarding Michael Stapelberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox