aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mail_handler/backends/mail_extensions.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-06-04 15:03:02 +0100
committerLouise Crow <louise.crow@gmail.com>2013-06-04 15:03:02 +0100
commita885764b65916020d9182073b38f6951a20d4b8c (patch)
tree0988651c144b65a8e46b28b376b2e72a5947d934 /lib/mail_handler/backends/mail_extensions.rb
parenteb1c465162420ad62c16dccb983cb28aa89a4639 (diff)
parenta919141992a40599f99b32bd4a8312a0009f3f7a (diff)
Merge branch 'release/0.11'0.11.0.3
Diffstat (limited to 'lib/mail_handler/backends/mail_extensions.rb')
-rw-r--r--lib/mail_handler/backends/mail_extensions.rb66
1 files changed, 65 insertions, 1 deletions
diff --git a/lib/mail_handler/backends/mail_extensions.rb b/lib/mail_handler/backends/mail_extensions.rb
index f756abd1a..322c49bb5 100644
--- a/lib/mail_handler/backends/mail_extensions.rb
+++ b/lib/mail_handler/backends/mail_extensions.rb
@@ -64,4 +64,68 @@ module Mail
end.join(";\r\n\s")
end
end
-end \ No newline at end of file
+
+ # HACK: Backport encoding fixes for Ruby 1.8 from Mail 2.5
+ # Can be removed when we no longer support Ruby 1.8
+ class Ruby18
+ def Ruby18.b_value_decode(str)
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
+ if match
+ encoding = match[1]
+ str = Ruby18.decode_base64(match[2])
+ # Adding and removing trailing spaces is a workaround
+ # for Iconv.conv throwing an exception if it finds an
+ # invalid character at the end of the string, even
+ # with UTF-8//IGNORE:
+ # http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
+ str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str + " ")[0...-4]
+ end
+ str
+ end
+
+ def Ruby18.q_value_decode(str)
+ match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
+ if match
+ encoding = match[1]
+ string = match[2].gsub(/_/, '=20')
+ # Remove trailing = if it exists in a Q encoding
+ string = string.sub(/\=$/, '')
+ str = Encodings::QuotedPrintable.decode(string)
+ # Adding and removing trailing spaces is a workaround
+ # for Iconv.conv throwing an exception if it finds an
+ # invalid character at the end of the string, even
+ # with UTF-8//IGNORE:
+ # http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
+ str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str + " ")[0...-4]
+ end
+ str
+ end
+
+ private
+
+ def Ruby18.fix_encoding(encoding)
+ case encoding.upcase
+ when 'UTF8'
+ 'UTF-8'
+ else
+ encoding
+ end
+ end
+ end
+ class Ruby19
+
+ def Ruby19.q_value_decode(str)
+ match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
+ if match
+ encoding = match[1]
+ str = Encodings::QuotedPrintable.decode(match[2].gsub(/_/, '=20'))
+ # Backport line from mail 2.5 to strip a trailing = character
+ # Remove trailing = if it exists in a Q encoding
+ str = str.sub(/\=$/, '')
+ str.force_encoding(fix_encoding(encoding))
+ end
+ decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
+ decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
+ end
+ end
+end