]> git.openstreetmap.org Git - rails.git/blob - config/initializers/mail.rb
Replace the file_column plugin with paperclip
[rails.git] / config / initializers / mail.rb
1 module Mail
2   class Ruby18
3     def Ruby18.b_value_decode(str)
4       match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
5       if match
6         encoding = match[1]
7         str = Ruby18.decode_base64(match[2])
8         require 'iconv'
9         str = Iconv.conv("UTF-8//TRANSLIT//IGNORE", encoding, str)
10       end
11       str
12     end
13
14     def Ruby18.q_value_decode(str)
15       match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
16       if match
17         encoding = match[1]
18         str = Encodings::QuotedPrintable.decode(match[2].gsub(/_/, '=20'))
19         require 'iconv'
20         str = Iconv.conv("UTF-8//TRANSLIT//IGNORE", encoding, str)
21       end
22       str
23     end
24   end
25
26   class Message
27     def decoded_with_text
28       if self.text?
29         decode_body_as_text
30       else
31         decoded_without_text
32       end
33     end
34
35     alias_method_chain :decoded, :text
36
37     def text?
38       has_content_type? ? !!(main_type =~ /^text$/i) : false
39     end
40
41   private
42
43     def decode_body_as_text
44       body_text = body.decoded
45       if charset
46         if RUBY_VERSION < '1.9'
47           require 'iconv'
48           return Iconv.conv("UTF-8//TRANSLIT//IGNORE", charset, body_text)
49         else
50           if encoding = Encoding.find(charset) rescue nil
51             body_text.force_encoding(encoding)
52             return body_text.encode(Encoding::UTF_8)
53           end
54         end
55       end
56       body_text
57     end    
58   end
59 end