Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
* [sup-talk] [PATCH] detect and set charset on text/* attachments
@ 2009-10-19 14:07 Helge Titlestad
  2009-11-03 17:16 ` William Morgan
  0 siblings, 1 reply; 6+ messages in thread
From: Helge Titlestad @ 2009-10-19 14:07 UTC (permalink / raw)
  To: sup-talk

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

I got some feedback from non-suppers that my utf-8 text attachments were
messed up. When I checked they (the MIME headers) lacked any info on charset,
which I believe should be set for text/*.

Here's a patch that uses the chardet gem to (try to) detect the appropriate charset
and sets it in the Content-Type header.

Can't guarantee its robustness - have only tried on a couple of text files and
one non-text file.

Please tell me if I should use some different way of sending patches... This git
flow is a bit new to me. (=
-- 
alge

[-- Attachment #2: 0001-Detect-charset-for-text-file-attachments.patch --]
[-- Type: application/octet-stream, Size: 1927 bytes --]

From 735a5ceb757599af71702d4ece8d29cb11f2c65b Mon Sep 17 00:00:00 2001
From: Helge Titlestad <helgedt@tihlde.org>
Date: Mon, 19 Oct 2009 16:03:56 +0200
Subject: [PATCH] Detect charset for text/* file attachments.

Adds dependency on chardet gem, and uses it to detect the charset.
---
 README.txt      |    1 +
 Rakefile        |    1 +
 lib/sup/util.rb |    8 +++++++-
 3 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/README.txt b/README.txt
index 4204270..3a98fa3 100644
--- a/README.txt
+++ b/README.txt
@@ -106,6 +106,7 @@ Current limitations which will be fixed:
  - mime-types
  - gettext
  - fastthread
+ - chardet
 
 == INSTALL:
 
diff --git a/Rakefile b/Rakefile
index 67cd0d2..3fb0d5e 100644
--- a/Rakefile
+++ b/Rakefile
@@ -57,6 +57,7 @@ spec = Gem::Specification.new do |s|
   s.add_dependency "mime-types", "~> 1"
   s.add_dependency "gettext"
   s.add_dependency "fastthread"
+  s.add_dependency "chardet", ">= 0.9.0"
 end
 
 Rake::GemPackageTask.new(spec) do |pkg|
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index f99e1c1..ef7b892 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -3,6 +3,7 @@ require 'lockfile'
 require 'mime/types'
 require 'pathname'
 require 'set'
+require 'UniversalDetector'
 
 ## time for some monkeypatching!
 class Lockfile
@@ -71,8 +72,13 @@ module RMail
 
     def self.make_attachment payload, mime_type, encoding, filename
       a = Message.new
+
+      cs = UniversalDetector::chardet(payload)['encoding'] if mime_type =~ /^text\//i
+      ct = "#{mime_type}; name=#{filename.inspect}"
+      ct += "; charset=#{cs}" if cs
+      
       a.header.add "Content-Disposition", "attachment; filename=#{filename.inspect}"
-      a.header.add "Content-Type", "#{mime_type}; name=#{filename.inspect}"
+      a.header.add "Content-Type", ct
       a.header.add "Content-Transfer-Encoding", encoding if encoding
       a.body =
         case encoding
-- 
1.5.6.5


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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread
* [sup-talk] [PATCH] detect and set charset on text/* attachments
@ 2012-06-21 20:26 Helge Titlestad
  2012-06-22  8:48 ` Gaute Hope
  0 siblings, 1 reply; 6+ messages in thread
From: Helge Titlestad @ 2012-06-21 20:26 UTC (permalink / raw)
  To: sup-talk

I got some feedback from non-suppers that my utf-8 text attachments were
messed up. When I checked they (the MIME headers) lacked any info on charset,
which I believe should be set for text/*.

Here's a patch that uses the chardet gem to (try to) detect the appropriate charset
and sets it in the Content-Type header.
_______________________________________________
sup-talk mailing list
sup-talk@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-talk


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-06-22 10:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-19 14:07 [sup-talk] [PATCH] detect and set charset on text/* attachments Helge Titlestad
2009-11-03 17:16 ` William Morgan
2009-11-03 19:23   ` Helge Titlestad
2012-06-21 20:26 Helge Titlestad
2012-06-22  8:48 ` Gaute Hope
2012-06-22 10:29   ` Helge Titlestad

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