otherinbox-mail 2.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/CHANGELOG.rdoc +607 -0
  2. data/CONTRIBUTING.md +45 -0
  3. data/Dependencies.txt +3 -0
  4. data/Gemfile +26 -0
  5. data/Gemfile.lock +44 -0
  6. data/README.md +663 -0
  7. data/Rakefile +40 -0
  8. data/TODO.rdoc +9 -0
  9. data/lib/VERSION +4 -0
  10. data/lib/mail.rb +101 -0
  11. data/lib/mail/attachments_list.rb +104 -0
  12. data/lib/mail/body.rb +291 -0
  13. data/lib/mail/configuration.rb +75 -0
  14. data/lib/mail/core_extensions/nil.rb +17 -0
  15. data/lib/mail/core_extensions/object.rb +13 -0
  16. data/lib/mail/core_extensions/shell_escape.rb +56 -0
  17. data/lib/mail/core_extensions/smtp.rb +25 -0
  18. data/lib/mail/core_extensions/string.rb +33 -0
  19. data/lib/mail/core_extensions/string/access.rb +145 -0
  20. data/lib/mail/core_extensions/string/multibyte.rb +78 -0
  21. data/lib/mail/elements.rb +14 -0
  22. data/lib/mail/elements/address.rb +306 -0
  23. data/lib/mail/elements/address_list.rb +74 -0
  24. data/lib/mail/elements/content_disposition_element.rb +30 -0
  25. data/lib/mail/elements/content_location_element.rb +25 -0
  26. data/lib/mail/elements/content_transfer_encoding_element.rb +24 -0
  27. data/lib/mail/elements/content_type_element.rb +35 -0
  28. data/lib/mail/elements/date_time_element.rb +26 -0
  29. data/lib/mail/elements/envelope_from_element.rb +34 -0
  30. data/lib/mail/elements/message_ids_element.rb +29 -0
  31. data/lib/mail/elements/mime_version_element.rb +26 -0
  32. data/lib/mail/elements/phrase_list.rb +21 -0
  33. data/lib/mail/elements/received_element.rb +30 -0
  34. data/lib/mail/encodings.rb +274 -0
  35. data/lib/mail/encodings/7bit.rb +31 -0
  36. data/lib/mail/encodings/8bit.rb +31 -0
  37. data/lib/mail/encodings/base64.rb +33 -0
  38. data/lib/mail/encodings/binary.rb +31 -0
  39. data/lib/mail/encodings/quoted_printable.rb +38 -0
  40. data/lib/mail/encodings/transfer_encoding.rb +58 -0
  41. data/lib/mail/envelope.rb +35 -0
  42. data/lib/mail/field.rb +234 -0
  43. data/lib/mail/field_list.rb +33 -0
  44. data/lib/mail/fields.rb +35 -0
  45. data/lib/mail/fields/bcc_field.rb +56 -0
  46. data/lib/mail/fields/cc_field.rb +55 -0
  47. data/lib/mail/fields/comments_field.rb +41 -0
  48. data/lib/mail/fields/common/address_container.rb +16 -0
  49. data/lib/mail/fields/common/common_address.rb +125 -0
  50. data/lib/mail/fields/common/common_date.rb +42 -0
  51. data/lib/mail/fields/common/common_field.rb +51 -0
  52. data/lib/mail/fields/common/common_message_id.rb +44 -0
  53. data/lib/mail/fields/common/parameter_hash.rb +58 -0
  54. data/lib/mail/fields/content_description_field.rb +19 -0
  55. data/lib/mail/fields/content_disposition_field.rb +69 -0
  56. data/lib/mail/fields/content_id_field.rb +63 -0
  57. data/lib/mail/fields/content_location_field.rb +42 -0
  58. data/lib/mail/fields/content_transfer_encoding_field.rb +50 -0
  59. data/lib/mail/fields/content_type_field.rb +198 -0
  60. data/lib/mail/fields/date_field.rb +57 -0
  61. data/lib/mail/fields/from_field.rb +55 -0
  62. data/lib/mail/fields/in_reply_to_field.rb +55 -0
  63. data/lib/mail/fields/keywords_field.rb +44 -0
  64. data/lib/mail/fields/message_id_field.rb +83 -0
  65. data/lib/mail/fields/mime_version_field.rb +53 -0
  66. data/lib/mail/fields/optional_field.rb +13 -0
  67. data/lib/mail/fields/received_field.rb +75 -0
  68. data/lib/mail/fields/references_field.rb +55 -0
  69. data/lib/mail/fields/reply_to_field.rb +55 -0
  70. data/lib/mail/fields/resent_bcc_field.rb +55 -0
  71. data/lib/mail/fields/resent_cc_field.rb +55 -0
  72. data/lib/mail/fields/resent_date_field.rb +35 -0
  73. data/lib/mail/fields/resent_from_field.rb +55 -0
  74. data/lib/mail/fields/resent_message_id_field.rb +34 -0
  75. data/lib/mail/fields/resent_sender_field.rb +62 -0
  76. data/lib/mail/fields/resent_to_field.rb +55 -0
  77. data/lib/mail/fields/return_path_field.rb +65 -0
  78. data/lib/mail/fields/sender_field.rb +67 -0
  79. data/lib/mail/fields/structured_field.rb +51 -0
  80. data/lib/mail/fields/subject_field.rb +16 -0
  81. data/lib/mail/fields/to_field.rb +55 -0
  82. data/lib/mail/fields/unstructured_field.rb +191 -0
  83. data/lib/mail/header.rb +265 -0
  84. data/lib/mail/indifferent_hash.rb +146 -0
  85. data/lib/mail/mail.rb +255 -0
  86. data/lib/mail/matchers/has_sent_mail.rb +124 -0
  87. data/lib/mail/message.rb +2059 -0
  88. data/lib/mail/multibyte.rb +42 -0
  89. data/lib/mail/multibyte/chars.rb +474 -0
  90. data/lib/mail/multibyte/exceptions.rb +8 -0
  91. data/lib/mail/multibyte/unicode.rb +392 -0
  92. data/lib/mail/multibyte/utils.rb +60 -0
  93. data/lib/mail/network.rb +14 -0
  94. data/lib/mail/network/delivery_methods/exim.rb +53 -0
  95. data/lib/mail/network/delivery_methods/file_delivery.rb +40 -0
  96. data/lib/mail/network/delivery_methods/sendmail.rb +62 -0
  97. data/lib/mail/network/delivery_methods/smtp.rb +153 -0
  98. data/lib/mail/network/delivery_methods/smtp_connection.rb +74 -0
  99. data/lib/mail/network/delivery_methods/test_mailer.rb +40 -0
  100. data/lib/mail/network/retriever_methods/base.rb +63 -0
  101. data/lib/mail/network/retriever_methods/imap.rb +168 -0
  102. data/lib/mail/network/retriever_methods/pop3.rb +140 -0
  103. data/lib/mail/network/retriever_methods/test_retriever.rb +47 -0
  104. data/lib/mail/parsers/address_lists.rb +64 -0
  105. data/lib/mail/parsers/address_lists.treetop +19 -0
  106. data/lib/mail/parsers/content_disposition.rb +535 -0
  107. data/lib/mail/parsers/content_disposition.treetop +46 -0
  108. data/lib/mail/parsers/content_location.rb +139 -0
  109. data/lib/mail/parsers/content_location.treetop +20 -0
  110. data/lib/mail/parsers/content_transfer_encoding.rb +162 -0
  111. data/lib/mail/parsers/content_transfer_encoding.treetop +20 -0
  112. data/lib/mail/parsers/content_type.rb +967 -0
  113. data/lib/mail/parsers/content_type.treetop +68 -0
  114. data/lib/mail/parsers/date_time.rb +114 -0
  115. data/lib/mail/parsers/date_time.treetop +11 -0
  116. data/lib/mail/parsers/envelope_from.rb +194 -0
  117. data/lib/mail/parsers/envelope_from.treetop +32 -0
  118. data/lib/mail/parsers/message_ids.rb +45 -0
  119. data/lib/mail/parsers/message_ids.treetop +15 -0
  120. data/lib/mail/parsers/mime_version.rb +144 -0
  121. data/lib/mail/parsers/mime_version.treetop +19 -0
  122. data/lib/mail/parsers/phrase_lists.rb +45 -0
  123. data/lib/mail/parsers/phrase_lists.treetop +15 -0
  124. data/lib/mail/parsers/received.rb +71 -0
  125. data/lib/mail/parsers/received.treetop +11 -0
  126. data/lib/mail/parsers/rfc2045.rb +464 -0
  127. data/lib/mail/parsers/rfc2045.treetop +36 -0
  128. data/lib/mail/parsers/rfc2822.rb +5341 -0
  129. data/lib/mail/parsers/rfc2822.treetop +410 -0
  130. data/lib/mail/parsers/rfc2822_obsolete.rb +3768 -0
  131. data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
  132. data/lib/mail/part.rb +116 -0
  133. data/lib/mail/parts_list.rb +55 -0
  134. data/lib/mail/patterns.rb +34 -0
  135. data/lib/mail/utilities.rb +215 -0
  136. data/lib/mail/version.rb +24 -0
  137. data/lib/mail/version_specific/ruby_1_8.rb +98 -0
  138. data/lib/mail/version_specific/ruby_1_9.rb +113 -0
  139. data/lib/tasks/corpus.rake +125 -0
  140. data/lib/tasks/treetop.rake +10 -0
  141. metadata +253 -0
@@ -0,0 +1,215 @@
1
+ # encoding: utf-8
2
+ module Mail
3
+ module Utilities
4
+ include Patterns
5
+
6
+ # Returns true if the string supplied is free from characters not allowed as an ATOM
7
+ def atom_safe?( str )
8
+ not ATOM_UNSAFE === str
9
+ end
10
+
11
+ # If the string supplied has ATOM unsafe characters in it, will return the string quoted
12
+ # in double quotes, otherwise returns the string unmodified
13
+ def quote_atom( str )
14
+ atom_safe?( str ) ? str : dquote(str)
15
+ end
16
+
17
+ # If the string supplied has PHRASE unsafe characters in it, will return the string quoted
18
+ # in double quotes, otherwise returns the string unmodified
19
+ def quote_phrase( str )
20
+ if RUBY_VERSION >= '1.9'
21
+ original_encoding = str.encoding
22
+ str.force_encoding('ASCII-8BIT')
23
+ if (PHRASE_UNSAFE === str)
24
+ dquote(str).force_encoding(original_encoding)
25
+ else
26
+ str.force_encoding(original_encoding)
27
+ end
28
+ else
29
+ (PHRASE_UNSAFE === str) ? dquote(str) : str
30
+ end
31
+ end
32
+
33
+ # Returns true if the string supplied is free from characters not allowed as a TOKEN
34
+ def token_safe?( str )
35
+ not TOKEN_UNSAFE === str
36
+ end
37
+
38
+ # If the string supplied has TOKEN unsafe characters in it, will return the string quoted
39
+ # in double quotes, otherwise returns the string unmodified
40
+ def quote_token( str )
41
+ token_safe?( str ) ? str : dquote(str)
42
+ end
43
+
44
+ # Wraps supplied string in double quotes unless it is already wrapped.
45
+ #
46
+ # Additionally will escape any double quotation marks in the string with a single
47
+ # backslash in front of the '"' character.
48
+ def dquote( str )
49
+ match = str.match(/^"(.*)?"$/)
50
+ str = match[1] if match
51
+ # First remove all escaped double quotes:
52
+ str = str.gsub(/\\"/, '"')
53
+ # Then wrap and re-escape all double quotes
54
+ '"' + str.gsub(/["]/n) {|s| '\\' + s } + '"'
55
+ end
56
+
57
+ # Unwraps supplied string from inside double quotes.
58
+ #
59
+ # Example:
60
+ #
61
+ # string = '"This is a string"'
62
+ # unquote(string) #=> 'This is a string'
63
+ def unquote( str )
64
+ match = str.match(/^"(.*?)"$/)
65
+ match ? match[1] : str
66
+ end
67
+
68
+ # Wraps a string in parenthesis and escapes any that are in the string itself.
69
+ #
70
+ # Example:
71
+ #
72
+ # paren( 'This is a string' ) #=> '(This is a string)'
73
+ def paren( str )
74
+ RubyVer.paren( str )
75
+ end
76
+
77
+ # Unwraps a string from being wrapped in parenthesis
78
+ #
79
+ # Example:
80
+ #
81
+ # str = '(This is a string)'
82
+ # unparen( str ) #=> 'This is a string'
83
+ def unparen( str )
84
+ match = str.match(/^\((.*?)\)$/)
85
+ match ? match[1] : str
86
+ end
87
+
88
+ # Wraps a string in angle brackets and escapes any that are in the string itself
89
+ #
90
+ # Example:
91
+ #
92
+ # bracket( 'This is a string' ) #=> '<This is a string>'
93
+ def bracket( str )
94
+ RubyVer.bracket( str )
95
+ end
96
+
97
+ # Unwraps a string from being wrapped in parenthesis
98
+ #
99
+ # Example:
100
+ #
101
+ # str = '<This is a string>'
102
+ # unbracket( str ) #=> 'This is a string'
103
+ def unbracket( str )
104
+ match = str.match(/^\<(.*?)\>$/)
105
+ match ? match[1] : str
106
+ end
107
+
108
+ # Escape parenthesies in a string
109
+ #
110
+ # Example:
111
+ #
112
+ # str = 'This is (a) string'
113
+ # escape_paren( str ) #=> 'This is \(a\) string'
114
+ def escape_paren( str )
115
+ RubyVer.escape_paren( str )
116
+ end
117
+
118
+ def uri_escape( str )
119
+ uri_parser.escape(str)
120
+ end
121
+
122
+ def uri_unescape( str )
123
+ uri_parser.unescape(str)
124
+ end
125
+
126
+ def uri_parser
127
+ @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
128
+ end
129
+
130
+ # Matches two objects with their to_s values case insensitively
131
+ #
132
+ # Example:
133
+ #
134
+ # obj2 = "This_is_An_object"
135
+ # obj1 = :this_IS_an_object
136
+ # match_to_s( obj1, obj2 ) #=> true
137
+ def match_to_s( obj1, obj2 )
138
+ obj1.to_s.downcase == obj2.to_s.downcase
139
+ end
140
+
141
+ # Capitalizes a string that is joined by hyphens correctly.
142
+ #
143
+ # Example:
144
+ #
145
+ # string = 'resent-from-field'
146
+ # capitalize_field( string ) #=> 'Resent-From-Field'
147
+ def capitalize_field( str )
148
+ str.to_s.split("-").map { |v| v.capitalize }.join("-")
149
+ end
150
+
151
+ # Takes an underscored word and turns it into a class name
152
+ #
153
+ # Example:
154
+ #
155
+ # constantize("hello") #=> "Hello"
156
+ # constantize("hello-there") #=> "HelloThere"
157
+ # constantize("hello-there-mate") #=> "HelloThereMate"
158
+ def constantize( str )
159
+ str.to_s.split(/[-_]/).map { |v| v.capitalize }.to_s
160
+ end
161
+
162
+ # Swaps out all underscores (_) for hyphens (-) good for stringing from symbols
163
+ # a field name.
164
+ #
165
+ # Example:
166
+ #
167
+ # string = :resent_from_field
168
+ # dasherize ( string ) #=> 'resent_from_field'
169
+ def dasherize( str )
170
+ str.to_s.gsub('_', '-')
171
+ end
172
+
173
+ # Swaps out all hyphens (-) for underscores (_) good for stringing to symbols
174
+ # a field name.
175
+ #
176
+ # Example:
177
+ #
178
+ # string = :resent_from_field
179
+ # underscoreize ( string ) #=> 'resent_from_field'
180
+ def underscoreize( str )
181
+ str.to_s.downcase.gsub('-', '_')
182
+ end
183
+
184
+ if RUBY_VERSION <= '1.8.6'
185
+
186
+ def map_lines( str, &block )
187
+ results = []
188
+ str.each_line do |line|
189
+ results << yield(line)
190
+ end
191
+ results
192
+ end
193
+
194
+ def map_with_index( enum, &block )
195
+ results = []
196
+ enum.each_with_index do |token, i|
197
+ results[i] = yield(token, i)
198
+ end
199
+ results
200
+ end
201
+
202
+ else
203
+
204
+ def map_lines( str, &block )
205
+ str.each_line.map(&block)
206
+ end
207
+
208
+ def map_with_index( enum, &block )
209
+ enum.each_with_index.map(&block)
210
+ end
211
+
212
+ end
213
+
214
+ end
215
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ module Mail
3
+ module VERSION
4
+
5
+ version = {}
6
+ File.read(File.join(File.dirname(__FILE__), '../', 'VERSION')).each_line do |line|
7
+ type, value = line.chomp.split(":")
8
+ next if type =~ /^\s+$/ || value =~ /^\s+$/
9
+ version[type] = value
10
+ end
11
+
12
+ MAJOR = version['major']
13
+ MINOR = version['minor']
14
+ PATCH = version['patch']
15
+ BUILD = version['build']
16
+
17
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
18
+
19
+ def self.version
20
+ STRING
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ module Mail
4
+ class Ruby18
5
+ require 'base64'
6
+
7
+ # Escapes any parenthesis in a string that are unescaped. This can't
8
+ # use the Ruby 1.9.1 regexp feature of negative look behind so we have
9
+ # to do two replacement, first unescape everything, then re-escape it
10
+ def Ruby18.escape_paren( str )
11
+ re = /\\\)/
12
+ str = str.gsub(re) { |s| ')'}
13
+ re = /\\\(/
14
+ str = str.gsub(re) { |s| '('}
15
+ re = /([\(\)])/ # Only match unescaped parens
16
+ str.gsub(re) { |s| '\\' + s }
17
+ end
18
+
19
+ def Ruby18.paren( str )
20
+ str = $1 if str =~ /^\((.*)?\)$/
21
+ str = escape_paren( str )
22
+ '(' + str + ')'
23
+ end
24
+
25
+ def Ruby18.escape_bracket( str )
26
+ re = /\\\>/
27
+ str = str.gsub(re) { |s| '>'}
28
+ re = /\\\</
29
+ str = str.gsub(re) { |s| '<'}
30
+ re = /([\<\>])/ # Only match unescaped parens
31
+ str.gsub(re) { |s| '\\' + s }
32
+ end
33
+
34
+ def Ruby18.bracket( str )
35
+ str = $1 if str =~ /^\<(.*)?\>$/
36
+ str = escape_bracket( str )
37
+ '<' + str + '>'
38
+ end
39
+
40
+ def Ruby18.decode_base64(str)
41
+ Base64.decode64(str) if str
42
+ end
43
+
44
+ def Ruby18.encode_base64(str)
45
+ Base64.encode64(str)
46
+ end
47
+
48
+ def Ruby18.has_constant?(klass, string)
49
+ klass.constants.include?( string )
50
+ end
51
+
52
+ def Ruby18.get_constant(klass, string)
53
+ klass.const_get( string )
54
+ end
55
+
56
+ def Ruby18.b_value_encode(str, encoding)
57
+ # Ruby 1.8 requires an encoding to work
58
+ raise ArgumentError, "Must supply an encoding" if encoding.nil?
59
+ encoding = encoding.to_s.upcase.gsub('_', '-')
60
+ [Encodings::Base64.encode(str), encoding]
61
+ end
62
+
63
+ def Ruby18.b_value_decode(str)
64
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
65
+ if match
66
+ encoding = match[1]
67
+ str = Ruby18.decode_base64(match[2])
68
+ end
69
+ str
70
+ end
71
+
72
+ def Ruby18.q_value_encode(str, encoding)
73
+ # Ruby 1.8 requires an encoding to work
74
+ raise ArgumentError, "Must supply an encoding" if encoding.nil?
75
+ encoding = encoding.to_s.upcase.gsub('_', '-')
76
+ [Encodings::QuotedPrintable.encode(str), encoding]
77
+ end
78
+
79
+ def Ruby18.q_value_decode(str)
80
+ match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
81
+ if match
82
+ encoding = match[1]
83
+ str = Encodings::QuotedPrintable.decode(match[2].gsub(/_/, '=20'))
84
+ end
85
+ str
86
+ end
87
+
88
+ def Ruby18.param_decode(str, encoding)
89
+ URI.unescape(str)
90
+ end
91
+
92
+ def Ruby18.param_encode(str)
93
+ encoding = $KCODE.to_s.downcase
94
+ language = Configuration.instance.param_encode_language
95
+ "#{encoding}'#{language}'#{URI.escape(str)}"
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+
3
+ module Mail
4
+ class Ruby19
5
+
6
+ # Escapes any parenthesis in a string that are unescaped this uses
7
+ # a Ruby 1.9.1 regexp feature of negative look behind
8
+ def Ruby19.escape_paren( str )
9
+ re = /(?<!\\)([\(\)])/ # Only match unescaped parens
10
+ str.gsub(re) { |s| '\\' + s }
11
+ end
12
+
13
+ def Ruby19.paren( str )
14
+ str = $1 if str =~ /^\((.*)?\)$/
15
+ str = escape_paren( str )
16
+ '(' + str + ')'
17
+ end
18
+
19
+ def Ruby19.escape_bracket( str )
20
+ re = /(?<!\\)([\<\>])/ # Only match unescaped brackets
21
+ str.gsub(re) { |s| '\\' + s }
22
+ end
23
+
24
+ def Ruby19.bracket( str )
25
+ str = $1 if str =~ /^\<(.*)?\>$/
26
+ str = escape_bracket( str )
27
+ '<' + str + '>'
28
+ end
29
+
30
+ def Ruby19.decode_base64(str)
31
+ str.unpack( 'm' ).first
32
+ end
33
+
34
+ def Ruby19.encode_base64(str)
35
+ [str].pack( 'm' )
36
+ end
37
+
38
+ def Ruby19.has_constant?(klass, string)
39
+ klass.const_defined?( string, false )
40
+ end
41
+
42
+ def Ruby19.get_constant(klass, string)
43
+ klass.const_get( string )
44
+ end
45
+
46
+ def Ruby19.b_value_encode(str, encoding = nil)
47
+ encoding = str.encoding.to_s
48
+ [Ruby19.encode_base64(str), encoding]
49
+ end
50
+
51
+ def Ruby19.b_value_decode(str)
52
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
53
+ if match
54
+ encoding = match[1]
55
+ str = Ruby19.decode_base64(match[2])
56
+ str.force_encoding(fix_encoding(encoding))
57
+ end
58
+ decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
59
+ decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
60
+ end
61
+
62
+ def Ruby19.q_value_encode(str, encoding = nil)
63
+ encoding = str.encoding.to_s
64
+ [Encodings::QuotedPrintable.encode(str), encoding]
65
+ end
66
+
67
+ def Ruby19.q_value_decode(str)
68
+ match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
69
+ if match
70
+ encoding = match[1]
71
+ str = Encodings::QuotedPrintable.decode(match[2].gsub(/_/, '=20'))
72
+ str.force_encoding(fix_encoding(encoding))
73
+ end
74
+ decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
75
+ decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
76
+ end
77
+
78
+ def Ruby19.param_decode(str, encoding)
79
+ string = uri_parser.unescape(str)
80
+ string.force_encoding(encoding) if encoding
81
+ string
82
+ end
83
+
84
+ def Ruby19.param_encode(str)
85
+ encoding = str.encoding.to_s.downcase
86
+ language = Configuration.instance.param_encode_language
87
+ "#{encoding}'#{language}'#{uri_parser.escape(str)}"
88
+ end
89
+
90
+ def Ruby19.uri_parser
91
+ @uri_parser ||= URI::Parser.new
92
+ end
93
+
94
+ # mails somtimes includes invalid encodings like iso885915 or utf8 so we transform them to iso885915 or utf8
95
+ # TODO: add this as a test somewhere
96
+ # Encoding.list.map{|e| [e.to_s.upcase==fix_encoding(e.to_s.downcase.gsub("-", "")), e.to_s] }.select {|a,b| !b}
97
+ # Encoding.list.map{|e| [e.to_s==fix_encoding(e.to_s), e.to_s] }.select {|a,b| !b}
98
+ def Ruby19.fix_encoding(encoding)
99
+ case encoding
100
+ # ISO-8859-15, ISO-2022-JP and alike
101
+ when /iso-?(\d{4})-?(\w{1,2})/i then return "ISO-#{$1}-#{$2}"
102
+ # "ISO-2022-JP-KDDI" and alike
103
+ when /iso-?(\d{4})-?(\w{1,2})-?(\w*)/i then return "ISO-#{$1}-#{$2}-#{$3}"
104
+ # UTF-8, UTF-32BE and alike
105
+ when /utf-?(\d{1,2})?(\w{1,2})/i then return "UTF-#{$1}#{$2}".gsub(/\A(UTF-(?:16|32))\z/, '\\1BE')
106
+ # Windows-1252 and alike
107
+ when /Windows-?(.*)/i then return "Windows-#{$1}"
108
+ #more aliases to be added if needed
109
+ else return encoding
110
+ end
111
+ end
112
+ end
113
+ end