Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
From: Helge Titlestad <helgedt@tihlde.org>
To: sup-talk <sup-talk@rubyforge.org>
Subject: Re: [sup-talk] [PATCH] detect and set charset on text/* attachments
Date: Tue, 03 Nov 2009 20:23:28 +0100	[thread overview]
Message-ID: <1257275607-sup-583@tihlde.org> (raw)
In-Reply-To: <1257264198-sup-8152@masanjin.net>

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

Excerpts from William Morgan's message of Tue Nov 03 18:16:32 +0100 2009:
> It looks like the chardet gem is unmaintained. But someone decided to
> make their own special version called rchardet which is completely
> equivalent version but *is* maintained. 
> 
> I suggest we use rchardet instead of chardet. Would you like to change
> the patch? If not, I will get to it at some point.

Here you go!

One thing I noticed when trying it out: It will set charset 'ascii' for ascii
text, which is allowed by the RFC but "US-ASCII" is preferred. I think I prefer
to not create a special case in the code to change it to US-ASCII or remove the
charset for ascii text, but other people might disagree.
-- 
alge

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

From 094962e04eafc50ba707c0c35885f161c0fc9641 Mon Sep 17 00:00:00 2001
From: Helge Titlestad <helgedt@tihlde.org>
Date: Tue, 3 Nov 2009 20:11:25 +0100
Subject: [PATCH] Detect charset for text/* file attachments.

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

diff --git a/README.txt b/README.txt
index 4204270..184dd09 100644
--- a/README.txt
+++ b/README.txt
@@ -106,6 +106,7 @@ Current limitations which will be fixed:
  - mime-types
  - gettext
  - fastthread
+ - rchardet
 
 == INSTALL:
 
diff --git a/Rakefile b/Rakefile
index 67cd0d2..c8d9243 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 "rchardet", ">= 1.3"
 end
 
 Rake::GemPackageTask.new(spec) do |pkg|
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index f99e1c1..7b747fb 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 'rchardet'
 
 ## time for some monkeypatching!
 class Lockfile
@@ -71,8 +72,14 @@ module RMail
 
     def self.make_attachment payload, mime_type, encoding, filename
       a = Message.new
+
+      cs = CharDet.detect(payload)['encoding'] if mime_type =~ /^text\//i
+debug(cs)
+      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

  reply	other threads:[~2009-11-03 19:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-19 14:07 Helge Titlestad
2009-11-03 17:16 ` William Morgan
2009-11-03 19:23   ` Helge Titlestad [this message]
2012-06-21 20:26 Helge Titlestad
2012-06-22  8:48 ` Gaute Hope
2012-06-22 10:29   ` Helge Titlestad

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=1257275607-sup-583@tihlde.org \
    --to=helgedt@tihlde.org \
    --cc=sup-talk@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