Archive of RubyForge sup-devel mailing list
 help / color / mirror / Atom feed
From: Michael Stapelberg <michael+sup@stapelberg.de>
To: sup-devel <sup-devel@rubyforge.org>
Subject: [sup-devel] (REVIEW NEEDED) [PATCH] Recode attachments to UTF-8 when forwarding
Date: Mon, 19 Jul 2010 23:38:18 +0200	[thread overview]
Message-ID: <1279575318-sup-7427@midna.zekjur.net> (raw)

[-- 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

                 reply	other threads:[~2010-07-19 21:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1279575318-sup-7427@midna.zekjur.net \
    --to=michael+sup@stapelberg.de \
    --cc=sup-devel@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox