try really hard to convert inbound mail to utf8

This commit is contained in:
joshua stein 2014-01-06 14:49:16 -06:00
parent 831b576325
commit 9b5ab9b978
2 changed files with 34 additions and 0 deletions

View File

@ -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

View File

@ -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