From 9b5ab9b978bcdde0e14f0b91c0de943b2797d50b Mon Sep 17 00:00:00 2001 From: joshua stein Date: Mon, 6 Jan 2014 14:49:16 -0600 Subject: [PATCH] try really hard to convert inbound mail to utf8 --- lib/monkey.rb | 32 ++++++++++++++++++++++++++++++++ script/parse_inbound_mail | 2 ++ 2 files changed, 34 insertions(+) diff --git a/lib/monkey.rb b/lib/monkey.rb index 644c1f8..3c0fd73 100644 --- a/lib/monkey.rb +++ b/lib/monkey.rb @@ -9,3 +9,35 @@ module ActiveRecord end end end + +class String + def forcibly_convert_to_utf8 + begin + if self.encoding.to_s == "UTF-8" && self.valid_encoding? + return self + end + + str = self.dup.force_encoding("binary").encode("utf-8", + :invalid => :replace, :undef => :replace, :replace => "?") + + if !str.valid_encoding? || str.encoding.to_s != "UTF-8" + raise Encoding::UndefinedConversionError + end + + rescue Encoding::UndefinedConversionError => e + str = self.chars.map{|c| + begin + c.encode("UTF-8", :invalid => :replace, :undef => :replace) + rescue + "?".encode("UTF-8") + end + }.join + + if !str.valid_encoding? + raise "still bogus encoding" + end + end + + str + end +end diff --git a/script/parse_inbound_mail b/script/parse_inbound_mail index c24dc92..7a22aa7 100755 --- a/script/parse_inbound_mail +++ b/script/parse_inbound_mail @@ -35,6 +35,8 @@ while !STDIN.eof? message += STDIN.gets.to_s end +message = message.forcibly_convert_to_utf8 + if message.match(/^X-BeenThere: #{Rails.application.shortname}-/i) # avoid looping exit