aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mail_handler/backends/mail_extensions.rb
diff options
context:
space:
mode:
authorHenare Degan <henare.degan@gmail.com>2013-03-02 15:38:46 +1100
committerHenare Degan <henare.degan@gmail.com>2013-03-02 15:38:53 +1100
commita74855165779821ba531fd3f9c3767fc3d10ac60 (patch)
tree4480052e615b488f1352e433d2e856c32d4d7819 /lib/mail_handler/backends/mail_extensions.rb
parentf01c914aa176586b7394e729f891a63705b5a3aa (diff)
Backport newer Mail code to fix decoding problems. #850
Diffstat (limited to 'lib/mail_handler/backends/mail_extensions.rb')
-rw-r--r--lib/mail_handler/backends/mail_extensions.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/mail_handler/backends/mail_extensions.rb b/lib/mail_handler/backends/mail_extensions.rb
index 83dce5733..29ae32542 100644
--- a/lib/mail_handler/backends/mail_extensions.rb
+++ b/lib/mail_handler/backends/mail_extensions.rb
@@ -63,4 +63,42 @@ module Mail
end.join(";\r\n\s")
end
end
+
+ # 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])
+ str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str)
+ 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)
+ str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str)
+ end
+ str
+ end
+
+ private
+
+ def Ruby18.fix_encoding(encoding)
+ case encoding.upcase
+ when 'UTF8'
+ 'UTF-8'
+ else
+ encoding
+ end
+ end
+ end
end