diff options
author | nicholas a. evans <nicholas.evans@gmail.com> | 2021-05-04 14:52:38 -0400 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2021-05-06 15:20:34 +0900 |
commit | 337c0e312bc4e8a13ee90b9d7b102664661cf9a7 (patch) | |
tree | 09dfc8084af66b3f683f27741aba92d4e30ef784 /lib/net | |
parent | 4dc7b82427bafab4878f120931ef5b0988778b75 (diff) |
[ruby/net-imap] Move UTF7 & datetime formatting to net/imap/data_encoding
Partially implements #10.
https://github.com/ruby/net-imap/commit/0d43c5e856
Diffstat (limited to 'lib/net')
-rw-r--r-- | lib/net/imap.rb | 41 | ||||
-rw-r--r-- | lib/net/imap/data_encoding.rb | 47 |
2 files changed, 48 insertions, 40 deletions
diff --git a/lib/net/imap.rb b/lib/net/imap.rb index fe56bc0810..862b10ee39 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -23,6 +23,7 @@ rescue LoadError end require_relative "imap/command_data" +require_relative "imap/data_encoding" require_relative "imap/response_data" require_relative "imap/response_parser" @@ -1054,46 +1055,6 @@ module Net end end - # Decode a string from modified UTF-7 format to UTF-8. - # - # UTF-7 is a 7-bit encoding of Unicode [UTF7]. IMAP uses a - # slightly modified version of this to encode mailbox names - # containing non-ASCII characters; see [IMAP] section 5.1.3. - # - # Net::IMAP does _not_ automatically encode and decode - # mailbox names to and from UTF-7. - def self.decode_utf7(s) - return s.gsub(/&([^-]+)?-/n) { - if $1 - ($1.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE) - else - "&" - end - } - end - - # Encode a string from UTF-8 format to modified UTF-7. - def self.encode_utf7(s) - return s.gsub(/(&)|[^\x20-\x7e]+/) { - if $1 - "&-" - else - base64 = [$&.encode(Encoding::UTF_16BE)].pack("m0") - "&" + base64.delete("=").tr("/", ",") + "-" - end - }.force_encoding("ASCII-8BIT") - end - - # Formats +time+ as an IMAP-style date. - def self.format_date(time) - return time.strftime('%d-%b-%Y') - end - - # Formats +time+ as an IMAP-style date-time. - def self.format_datetime(time) - return time.strftime('%d-%b-%Y %H:%M %z') - end - private CRLF = "\r\n" # :nodoc: diff --git a/lib/net/imap/data_encoding.rb b/lib/net/imap/data_encoding.rb new file mode 100644 index 0000000000..d8449f582c --- /dev/null +++ b/lib/net/imap/data_encoding.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Net + class IMAP < Protocol + + # Decode a string from modified UTF-7 format to UTF-8. + # + # UTF-7 is a 7-bit encoding of Unicode [UTF7]. IMAP uses a + # slightly modified version of this to encode mailbox names + # containing non-ASCII characters; see [IMAP] section 5.1.3. + # + # Net::IMAP does _not_ automatically encode and decode + # mailbox names to and from UTF-7. + def self.decode_utf7(s) + return s.gsub(/&([^-]+)?-/n) { + if $1 + ($1.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE) + else + "&" + end + } + end + + # Encode a string from UTF-8 format to modified UTF-7. + def self.encode_utf7(s) + return s.gsub(/(&)|[^\x20-\x7e]+/) { + if $1 + "&-" + else + base64 = [$&.encode(Encoding::UTF_16BE)].pack("m0") + "&" + base64.delete("=").tr("/", ",") + "-" + end + }.force_encoding("ASCII-8BIT") + end + + # Formats +time+ as an IMAP-style date. + def self.format_date(time) + return time.strftime('%d-%b-%Y') + end + + # Formats +time+ as an IMAP-style date-time. + def self.format_datetime(time) + return time.strftime('%d-%b-%Y %H:%M %z') + end + + end +end |