]> git.cworth.org Git - sup/commitdiff
Factor the Iconv decoding code, and be more lenient on encodings.
authorNicolas Pouillard <nicolas.pouillard@gmail.com>
Fri, 14 Nov 2008 12:31:02 +0000 (13:31 +0100)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Thu, 20 Nov 2008 15:16:19 +0000 (07:16 -0800)
lib/sup/message.rb
lib/sup/rfc2047.rb
lib/sup/util.rb

index 944dd88947207295e143fb3df1bf0c131ba83511..e01e24582b7b99510f0ebfa8f857fff6b78cf8e2 100644 (file)
@@ -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 }
index 947de022bc41715c641585e7fe73b4405aac7b6e..fcd5cf071262565ef0594ac736b5c758d44710d5 100644 (file)
@@ -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
index 9909022ffa9b0b1c5796f4fbdff925087dec2519..6a6da5e57cada47fe30fee7b2aa6ecfed576b4d7 100644 (file)
@@ -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