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($
&) }.
8 "=?#{charset}?Q?#{text}?="
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
)
15 character
.each_byte
{ |b
| result
<< "=%02X" % b
}
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]/
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)
29 (text
=~ CHARS_NEEDING_QUOTING
) ?
30 quoted_printable(text
, charset
) :
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
) }
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
)
46 address
.map
{ |a
| quote_address_if_necessary(a
, charset
) }
47 elsif address
=~
/^(\S.*)\s+(<.*>)$/
49 phrase
= quote_if_necessary($1.gsub(/^['"](.*)['"]$/, '\1'), charset
)
50 "\"#{phrase}\" #{address}"
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
) }