Skip to content

Commit e5bcb67

Browse files
authored
🔀 Merge pull request #114 from arnt/utf8accept
Minor bits for UTF8=ACCEPT
2 parents a00e2e3 + e0656f3 commit e5bcb67

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

lib/net/imap.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ module Net
491491
#
492492
# ==== RFC6855: <tt>UTF8=ACCEPT</tt>, <tt>UTF8=ONLY</tt>
493493
#
494-
# - See #enable for information about support foi UTF-8 string encoding.
494+
# - See #enable for information about support for UTF-8 string encoding.
495495
#
496496
#--
497497
# ==== RFC7888: <tt>LITERAL+</tt>, +LITERAL-+
@@ -1931,20 +1931,14 @@ def uid_thread(algorithm, search_keys, charset)
19311931
# UTF-7}[::decode_utf7] for mailbox names, or RFC2047 encoded-words for
19321932
# message headers.
19331933
#
1934-
# *Note:* For now, strings with 8-bit characters are still _sent_ using
1935-
# "literal" syntax. A future update will change how commands send UTF-8
1936-
# strings when <tt>UTF8=ACCEPT</tt> is enabled. This update should be
1937-
# backward-compatible.
1938-
#
19391934
# *Note:* <em>A future update may set string encodings slightly
19401935
# differently</em>, e.g: "US-ASCII" when UTF-8 is not enabled, and "UTF-8"
19411936
# when it is. Currently, the encoding of strings sent as "quoted" or
1942-
# "text" will _always_ be "UTF-8", even when a 7-bit encoding is used
1943-
# (e.g. UTF-7, encoded-words, quoted-printable, base64). And currently,
1944-
# string "literals" sent by the server will always have an "ASCII-8BIT"
1945-
# (binary) encoding, even if they must contain UTF-8 data---although a
1946-
# server _should_ use "quoted" strings once <tt>UTF8=ACCEPT</tt> is
1947-
# enabled.
1937+
# "text" will _always_ be "UTF-8", even when only ASCII characters are
1938+
# used (e.g. "Subject: Agenda") And currently, string "literals" sent
1939+
# by the server will always have an "ASCII-8BIT" (binary)
1940+
# encoding, even if they generally contain UTF-8 data, if they are
1941+
# text at all.
19481942
#
19491943
# [<tt>"UTF8=ONLY"</tt> [RFC6855[https://tools.ietf.org/html/rfc6855]]]
19501944
#
@@ -1974,7 +1968,10 @@ def enable(*capabilities)
19741968
.join(' ')
19751969
synchronize do
19761970
send_command("ENABLE #{capabilities}")
1977-
return @responses.delete("ENABLED")[-1]
1971+
result = @responses.delete("ENABLED")[-1]
1972+
@utf8_strings ||= result.include? "UTF8=ACCEPT"
1973+
@utf8_strings ||= result.include? "IMAP4REV2"
1974+
result
19781975
end
19791976
end
19801977

@@ -2222,6 +2219,7 @@ def initialize(host, port_or_options = {},
22222219
@port = options[:port] || (options[:ssl] ? SSL_PORT : PORT)
22232220
@tag_prefix = "RUBY"
22242221
@tagno = 0
2222+
@utf8_strings = false
22252223
@open_timeout = options[:open_timeout] || 30
22262224
@idle_response_timeout = options[:idle_response_timeout] || 5
22272225
@parser = ResponseParser.new

lib/net/imap/command_data.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ def send_data(data, tag = nil)
5252
end
5353

5454
def send_string_data(str, tag = nil)
55-
case str
56-
when ""
55+
if str.empty?
5756
put_string('""')
58-
when /[\x80-\xff\r\n]/n
59-
# literal
57+
elsif str.match?(/[\r\n]/n)
58+
# literal, because multiline
6059
send_literal(str, tag)
61-
when /[(){ \x00-\x1f\x7f%*"\\]/n
60+
elsif !str.ascii_only?
61+
if @utf8_strings
62+
# quoted string
63+
send_quoted_string(str)
64+
else
65+
# literal, because of non-ASCII bytes
66+
send_literal(str, tag)
67+
end
68+
elsif str.match?(/[(){ \x00-\x1f\x7f%*"\\]/n)
6269
# quoted string
6370
send_quoted_string(str)
6471
else
@@ -67,7 +74,7 @@ def send_string_data(str, tag = nil)
6774
end
6875

6976
def send_quoted_string(str)
70-
put_string('"' + str.gsub(/["\\]/n, "\\\\\\&") + '"')
77+
put_string('"' + str.gsub(/["\\]/, "\\\\\\&") + '"')
7178
end
7279

7380
def send_literal(str, tag = nil)

0 commit comments

Comments
 (0)