Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/net/imap/command_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def send_data(data, tag = nil)
end
end

UNQUOTABLE_CHARS = /\0\r\n/n
ASTRING_SPECIALS = /[(){ \x00-\x1f\x7f%*"\\]/n
UNQUOTABLE_CHARS = /\0\r\n/
ASTRING_SPECIALS = /[(){ \x00-\x1f\x7f%*"\\]/
private_constant :UNQUOTABLE_CHARS, :ASTRING_SPECIALS

# Sends generic strings formatted as +astring+:
Expand All @@ -72,22 +72,25 @@ def send_string_data(str, tag = nil)
if str.empty?
# same as send_quoted_string(str), but incompatible encoding is allowed
put_string('""')
elsif str.match?(UNQUOTABLE_CHARS)
send_literal(str, tag)
elsif !str.ascii_only?
if @utf8_strings
send_quoted_string(str)
else
send_literal(str, tag)
end
elsif str.match?(ASTRING_SPECIALS)
send_quoted_string(str)
else
elsif str.ascii_only? && !str.match?(ASTRING_SPECIALS)
# valid +astring+ atom: non-empty, ASCII only, no ASTRING_SPECIALS
put_string(str)
elsif text_encodable?(str) && !str.match?(UNQUOTABLE_CHARS)
send_quoted_string(str)
else
send_literal(str, tag)
end
end

# Encodable as +text+ (which is a superset of +quoted+):
# * ASCII only (for any ASCII compatible encoding)
# * or valid UTF-8 (when the connection supports it)
def text_encodable?(str)
str.ascii_only? || (@utf8_strings &&
str.encoding == Encoding::UTF_8 &&
str.valid_encoding?)
end

def send_quoted_string(str) = QuotedString.new(data: str).send_data(self)

def send_binary_literal(*, **) = send_literal(*, **, binary: true)
Expand Down
24 changes: 24 additions & 0 deletions test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -994,18 +994,42 @@ def imap.test_args(*args) = send_command("TEST", *args)
assert_equal("sync-literal-utf8 ({10+}\r\nαβγδε)".b,
server.commands.pop.args)

imap.test_args "utf8-with-wrong-encoding", "αβγδε".b
assert_equal("utf8-with-wrong-encoding {10+}\r\nαβγδε".b,
server.commands.pop.args)

imap.test_args "invalid-utf8", "\x80".b.force_encoding("UTF-8")
assert_equal("invalid-utf8 {1+}\r\n\x80".b,
server.commands.pop.args)

# Before enabling UTF-8 strings, without non-synchronizing literals
imap.config.max_non_synchronizing_literal = -1
imap.test_args "sync-literal-utf8", ["αβγδε"]
assert_equal("sync-literal-utf8 ({10}\r\nαβγδε)".b,
server.commands.pop.args)

imap.test_args "utf8-with-wrong-encoding", "αβγδε".b
assert_equal("utf8-with-wrong-encoding {10}\r\nαβγδε".b,
server.commands.pop.args)

imap.test_args "invalid-utf8", "\x80".b.force_encoding("UTF-8")
assert_equal("invalid-utf8 {1}\r\n\x80".b,
server.commands.pop.args)

# After enabling UTF-8 strings
imap.enable(:utf8)
server.commands.pop.args => ["UTF8=ACCEPT"]

imap.test_args "quoted-utf8", "αβγδε"
assert_equal 'quoted-utf8 "αβγδε"'.b, server.commands.pop.args

imap.test_args "utf8-with-wrong-encoding", "αβγδε".b
assert_equal("utf8-with-wrong-encoding {10}\r\nαβγδε".b,
server.commands.pop.args)

imap.test_args "invalid-utf8", "\x80".b.force_encoding("UTF-8")
assert_equal("invalid-utf8 {1}\r\n\x80".b,
server.commands.pop.args)
end
end

Expand Down
Loading