Froze rails gems
[depot.git] / vendor / rails / actionmailer / lib / action_mailer / quoting.rb
1 module ActionMailer
2 module Quoting #:nodoc:
3 # Convert the given text into quoted printable format, with an instruction
4 # that the text be eventually interpreted in the given charset.
5 def quoted_printable(text, charset)
6 text = text.gsub( /[^a-z ]/i ) { quoted_printable_encode($&) }.
7 gsub( / /, "_" )
8 "=?#{charset}?Q?#{text}?="
9 end
10
11 # Convert the given character to quoted printable format, taking into
12 # account multi-byte characters (if executing with $KCODE="u", for instance)
13 def quoted_printable_encode(character)
14 result = ""
15 character.each_byte { |b| result << "=%02x" % b }
16 result
17 end
18
19 # A quick-and-dirty regexp for determining whether a string contains any
20 # characters that need escaping.
21 if !defined?(CHARS_NEEDING_QUOTING)
22 CHARS_NEEDING_QUOTING = /[\000-\011\013\014\016-\037\177-\377]/
23 end
24
25 # Quote the given text if it contains any "illegal" characters
26 def quote_if_necessary(text, charset)
27 text = text.dup.force_encoding(Encoding::ASCII_8BIT) if text.respond_to?(:force_encoding)
28
29 (text =~ CHARS_NEEDING_QUOTING) ?
30 quoted_printable(text, charset) :
31 text
32 end
33
34 # Quote any of the given strings if they contain any "illegal" characters
35 def quote_any_if_necessary(charset, *args)
36 args.map { |v| quote_if_necessary(v, charset) }
37 end
38
39 # Quote the given address if it needs to be. The address may be a
40 # regular email address, or it can be a phrase followed by an address in
41 # brackets. The phrase is the only part that will be quoted, and only if
42 # it needs to be. This allows extended characters to be used in the
43 # "to", "from", "cc", "bcc" and "reply-to" headers.
44 def quote_address_if_necessary(address, charset)
45 if Array === address
46 address.map { |a| quote_address_if_necessary(a, charset) }
47 elsif address =~ /^(\S.*)\s+(<.*>)$/
48 address = $2
49 phrase = quote_if_necessary($1.gsub(/^['"](.*)['"]$/, '\1'), charset)
50 "\"#{phrase}\" #{address}"
51 else
52 address
53 end
54 end
55
56 # Quote any of the given addresses, if they need to be.
57 def quote_any_address_if_necessary(charset, *args)
58 args.map { |v| quote_address_if_necessary(v, charset) }
59 end
60 end
61 end