PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/mail-2.2.5/lib/mail/encodings.rb

http://github.com/IronLanguages/main
Ruby | 268 lines | 144 code | 29 blank | 95 comment | 17 complexity | c179f4d0bd0dc57d9123caa170532819 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. # encoding: utf-8
  2. module Mail
  3. # Raised when attempting to decode an unknown encoding type
  4. class UnknownEncodingType < StandardError #:nodoc:
  5. end
  6. module Encodings
  7. include Mail::Patterns
  8. extend Mail::Utilities
  9. @transfer_encodings = {}
  10. # Register transfer encoding
  11. #
  12. # Example
  13. #
  14. # Encodings.register "base64", Mail::Encodings::Base64
  15. def Encodings.register(name, cls)
  16. @transfer_encodings[get_name(name)] = cls
  17. end
  18. # Is the encoding we want defined?
  19. #
  20. # Example:
  21. #
  22. # Encodings.defined?(:base64) #=> true
  23. def Encodings.defined?( str )
  24. @transfer_encodings.include? get_name(str)
  25. end
  26. # Gets a defined encoding type, QuotedPrintable or Base64 for now.
  27. #
  28. # Each encoding needs to be defined as a Mail::Encodings::ClassName for
  29. # this to work, allows us to add other encodings in the future.
  30. #
  31. # Example:
  32. #
  33. # Encodings.get_encoding(:base64) #=> Mail::Encodings::Base64
  34. def Encodings.get_encoding( str )
  35. @transfer_encodings[get_name(str)]
  36. end
  37. def Encodings.get_all
  38. @transfer_encodings.values
  39. end
  40. def Encodings.get_name(enc)
  41. enc = enc.to_s.gsub("-", "_").downcase
  42. end
  43. # Encodes a parameter value using URI Escaping, note the language field 'en' can
  44. # be set using Mail::Configuration, like so:
  45. #
  46. # Mail.defaults.do
  47. # param_encode_language 'jp'
  48. # end
  49. #
  50. # The character set used for encoding will either be the value of $KCODE for
  51. # Ruby < 1.9 or the encoding on the string passed in.
  52. #
  53. # Example:
  54. #
  55. # Mail::Encodings.param_encode("This is fun") #=> "us-ascii'en'This%20is%20fun"
  56. def Encodings.param_encode(str)
  57. case
  58. when str.ascii_only? && str =~ TOKEN_UNSAFE
  59. %Q{"#{str}"}
  60. when str.ascii_only?
  61. str
  62. else
  63. RubyVer.param_encode(str)
  64. end
  65. end
  66. # Decodes a parameter value using URI Escaping.
  67. #
  68. # Example:
  69. #
  70. # Mail::Encodings.param_decode("This%20is%20fun", 'us-ascii') #=> "This is fun"
  71. #
  72. # str = Mail::Encodings.param_decode("This%20is%20fun", 'iso-8559-1')
  73. # str.encoding #=> 'ISO-8859-1' ## Only on Ruby 1.9
  74. # str #=> "This is fun"
  75. def Encodings.param_decode(str, encoding)
  76. RubyVer.param_decode(str, encoding)
  77. end
  78. # Decodes or encodes a string as needed for either Base64 or QP encoding types in
  79. # the =?<encoding>?[QB]?<string>?=" format.
  80. #
  81. # The output type needs to be :decode to decode the input string or :encode to
  82. # encode the input string. The character set used for encoding will either be
  83. # the value of $KCODE for Ruby < 1.9 or the encoding on the string passed in.
  84. #
  85. # On encoding, will only send out Base64 encoded strings.
  86. def Encodings.decode_encode(str, output_type)
  87. case
  88. when output_type == :decode
  89. Encodings.value_decode(str)
  90. else
  91. if str.ascii_only?
  92. str
  93. else
  94. Encodings.b_value_encode(str, find_encoding(str))
  95. end
  96. end
  97. end
  98. # Decodes a given string as Base64 or Quoted Printable, depending on what
  99. # type it is.
  100. #
  101. # String has to be of the format =?<encoding>?[QB]?<string>?=
  102. def Encodings.value_decode(str)
  103. # Optimization: If there's no encoded-words in the string, just return it
  104. return str unless str.index("=?")
  105. str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  106. # Split on white-space boundaries with capture, so we capture the white-space as well
  107. str.split(/([ \t])/).map do |text|
  108. if text.index('=?') != 0
  109. text
  110. else
  111. # Join QP encoded-words that are adjacent to avoid decoding partial chars
  112. text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
  113. # Separate encoded-words with a space, so we can treat them one by one
  114. text.gsub!(/\?\=\=\?/, '?= =?')
  115. text.split(/ /).map do |word|
  116. case
  117. when word.to_str =~ /=\?.+\?[Bb]\?/m
  118. b_value_decode(word)
  119. when text.to_str =~ /=\?.+\?[Qq]\?/m
  120. q_value_decode(word)
  121. else
  122. word.to_str
  123. end
  124. end
  125. end
  126. end.join("")
  127. end
  128. # Takes an encoded string of the format =?<encoding>?[QB]?<string>?=
  129. def Encodings.unquote_and_convert_to(str, to_encoding)
  130. original_encoding, string = split_encoding_from_string( str )
  131. output = value_decode( str ).to_s
  132. if original_encoding.to_s.downcase.gsub("-", "") == to_encoding.to_s.downcase.gsub("-", "")
  133. output
  134. elsif original_encoding && to_encoding
  135. begin
  136. require 'iconv'
  137. Iconv.iconv(to_encoding, original_encoding, output).first
  138. rescue Iconv::IllegalSequence, Iconv::InvalidEncoding, Errno::EINVAL
  139. # the 'from' parameter specifies a charset other than what the text
  140. # actually is...not much we can do in this case but just return the
  141. # unconverted text.
  142. #
  143. # Ditto if either parameter represents an unknown charset, like
  144. # X-UNKNOWN.
  145. output
  146. end
  147. else
  148. output
  149. end
  150. end
  151. def Encodings.address_encode(address, charset = 'utf-8')
  152. if address.is_a?(Array)
  153. # loop back through for each element
  154. address.map { |a| Encodings.address_encode(a, charset) }.join(", ")
  155. else
  156. # find any word boundary that is not ascii and encode it
  157. encode_non_usascii(address, charset)
  158. end
  159. end
  160. def Encodings.encode_non_usascii(address, charset)
  161. return address if address.ascii_only?
  162. us_ascii = %Q{\x00-\x7f}
  163. # Encode any non usascii strings embedded inside of quotes
  164. address.gsub!(/(".*?[^#{us_ascii}].+?")/) { |s| Encodings.b_value_encode(unquote(s), charset) }
  165. # Then loop through all remaining items and encode as needed
  166. tokens = address.split(/\s/)
  167. tokens.each_with_index.map do |word, i|
  168. if word.ascii_only?
  169. word
  170. else
  171. previous_non_ascii = tokens[i-1] && !tokens[i-1].ascii_only?
  172. if previous_non_ascii
  173. word = " #{word}"
  174. end
  175. Encodings.b_value_encode(word, charset)
  176. end
  177. end.join(' ')
  178. end
  179. # Encode a string with Base64 Encoding and returns it ready to be inserted
  180. # as a value for a field, that is, in the =?<charset>?B?<string>?= format
  181. #
  182. # Example:
  183. #
  184. # Encodings.b_value_encode('This is あ string', 'UTF-8')
  185. # #=> "=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?="
  186. def Encodings.b_value_encode(str, encoding = nil)
  187. return str if str.to_s.ascii_only?
  188. string, encoding = RubyVer.b_value_encode(str, encoding)
  189. string.each_line.map do |str|
  190. "=?#{encoding}?B?#{str.chomp}?="
  191. end.join(" ")
  192. end
  193. # Encode a string with Quoted-Printable Encoding and returns it ready to be inserted
  194. # as a value for a field, that is, in the =?<charset>?Q?<string>?= format
  195. #
  196. # Example:
  197. #
  198. # Encodings.q_value_encode('This is あ string', 'UTF-8')
  199. # #=> "=?UTF-8?Q?This_is_=E3=81=82_string?="
  200. def Encodings.q_value_encode(str, encoding = nil)
  201. return str if str.to_s.ascii_only?
  202. string, encoding = RubyVer.q_value_encode(str, encoding)
  203. string.gsub!("=\r\n", '') # We already have limited the string to the length we want
  204. string.each_line.map do |str|
  205. "=?#{encoding}?Q?#{str.chomp.gsub(/ /, '_')}?="
  206. end.join(" ")
  207. end
  208. private
  209. # Decodes a Base64 string from the "=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?=" format
  210. #
  211. # Example:
  212. #
  213. # Encodings.b_value_encode("=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?=")
  214. # #=> 'This is あ string'
  215. def Encodings.b_value_decode(str)
  216. RubyVer.b_value_decode(str)
  217. end
  218. # Decodes a Quoted-Printable string from the "=?UTF-8?Q?This_is_=E3=81=82_string?=" format
  219. #
  220. # Example:
  221. #
  222. # Encodings.b_value_encode("=?UTF-8?Q?This_is_=E3=81=82_string?=")
  223. # #=> 'This is あ string'
  224. def Encodings.q_value_decode(str)
  225. RubyVer.q_value_decode(str).gsub(/_/, ' ')
  226. end
  227. def Encodings.split_encoding_from_string( str )
  228. match = str.match(/\=\?([^?]+)?\?[QB]\?(.+)?\?\=/mi)
  229. if match
  230. [match[1], match[2]]
  231. else
  232. nil
  233. end
  234. end
  235. def Encodings.find_encoding(str)
  236. RUBY_VERSION >= '1.9' ? str.encoding : $KCODE
  237. end
  238. end
  239. end