* [sup-talk] [PATCH] Factor the Iconv decoding code, and be more lenient on encodings.
@ 2008-11-14 12:31 Nicolas Pouillard
2008-11-20 15:17 ` William Morgan
0 siblings, 1 reply; 2+ messages in thread
From: Nicolas Pouillard @ 2008-11-14 12:31 UTC (permalink / raw)
---
lib/sup/message.rb | 3 +--
lib/sup/rfc2047.rb | 7 +------
lib/sup/util.rb | 16 ++++++++++++++++
3 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/lib/sup/message.rb b/lib/sup/message.rb
index 944dd88..e01e245 100644
--- a/lib/sup/message.rb
+++ b/lib/sup/message.rb
@@ -434,11 +434,10 @@ private
end
def self.convert_from body, charset
- charset = "utf-8" if charset =~ /UTF_?8/i
begin
raise MessageFormatError, "RubyMail decode returned a null body" unless body
return body unless charset
- Iconv.iconv($encoding + "//IGNORE", charset, body + " ").join[0 .. -2]
+ Iconv.easy_decode($encoding, charset, body)
rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence, MessageFormatError => e
Redwood::log "warning: error (#{e.class.name}) decoding message body from #{charset}: #{e.message}"
File.open(File.join(BASE_DIR,"unable-to-decode.txt"), "w") { |f| f.write body }
diff --git a/lib/sup/rfc2047.rb b/lib/sup/rfc2047.rb
index 947de02..fcd5cf0 100644
--- a/lib/sup/rfc2047.rb
+++ b/lib/sup/rfc2047.rb
@@ -52,13 +52,8 @@ module Rfc2047
# WORD.
end
- charset = "utf-8" if charset =~ /UTF_?8/i
-
- # Convert:
- #
- # Remember - Iconv.open(to, from)!
begin
- text = Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
+ Iconv.easy_decode(target, charset, text)
rescue Iconv::InvalidCharacter
text
end
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index 9909022..6a6da5e 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -617,3 +617,19 @@ class FinishLine
@m.synchronize { !@over && @over = true }
end
end
+
+class Iconv
+ def self.easy_decode target, charset, text
+ return text if charset =~ /^x-unknown$/i
+ charset = case charset
+ when /UTF[-_]?8/i: "utf-8"
+ when /(iso[-_])?latin[-_]?1$/i: "ISO-8859-1"
+ else charset
+ end
+
+ # Convert:
+ #
+ # Remember - Iconv.open(to, from)!
+ Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
+ end
+end
--
1.5.5.rc3
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-11-20 15:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-14 12:31 [sup-talk] [PATCH] Factor the Iconv decoding code, and be more lenient on encodings Nicolas Pouillard
2008-11-20 15:17 ` William Morgan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox