Skip to content

Commit 66fad03

Browse files
committed
✨ Add #utf8_enabled?
This returns whether UTF-8 string encoding has been enabled for the connection.
1 parent 3ed83af commit 66fad03

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/net/imap.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ module Net
326326
#
327327
# <em>In general, #capable? should be used rather than explicitly sending a
328328
# +CAPABILITY+ command to the server.</em>
329+
# - #enable: Enables backwards incompatible server extensions.
330+
# <em>Requires the +ENABLE+ or +IMAP4rev2+ capability.</em>
331+
# - #utf8_enabled?: Returns whether UTF-8 string encoding has been enabled.
329332
#
330333
# === Handling server responses
331334
#
@@ -552,6 +555,7 @@ module Net
552555
# ==== RFC6855: <tt>UTF8=ACCEPT</tt>, <tt>UTF8=ONLY</tt>
553556
#
554557
# - See #enable for information about support for UTF-8 string encoding.
558+
# - #utf8_enabled?: Returns whether UTF-8 string encoding has been enabled.
555559
#
556560
# ==== RFC7162: +CONDSTORE+
557561
#
@@ -3167,6 +3171,11 @@ def enable(*capabilities)
31673171
end
31683172
end
31693173

3174+
# Returns whether UTF-8 string encoding has been enabled for the connection.
3175+
#
3176+
# See #enable.
3177+
def utf8_enabled?; @utf8_strings end
3178+
31703179
# Sends an {IDLE command [RFC2177 §3]}[https://www.rfc-editor.org/rfc/rfc6851#section-3]
31713180
# {[IMAP4rev2 §6.3.13]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.3.13]
31723181
# that waits for notifications of new or expunged messages. Yields

lib/net/imap/command_data.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def send_string_data(str, tag = nil)
8686
# * ASCII only (for any ASCII compatible encoding)
8787
# * or valid UTF-8 (when the connection supports it)
8888
def text_encodable?(str)
89-
str.ascii_only? || (@utf8_strings &&
89+
str.ascii_only? || (utf8_enabled? &&
9090
str.encoding == Encoding::UTF_8 &&
9191
str.valid_encoding?)
9292
end

test/net/imap/test_imap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ def imap.test_args(*args) = send_command("TEST", *args)
10181018

10191019
# After enabling UTF-8 strings
10201020
imap.enable(:utf8)
1021+
assert imap.utf8_enabled?
10211022
server.commands.pop.args => ["UTF8=ACCEPT"]
10221023

10231024
imap.test_args "quoted-utf8", "αβγδε"

test/net/imap/test_imap_enable.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ def test_enable
1414
) do |server, imap|
1515
cmdq = server.commands
1616

17+
refute imap.utf8_enabled?
18+
1719
enabled = imap.enable(%w[CONDSTORE x-pig-latin])
1820
assert_equal "RUBY0001 ENABLE CONDSTORE x-pig-latin", cmdq.pop.raw.strip
1921
assert_equal %w[CONDSTORE], enabled
22+
refute imap.utf8_enabled?
2023

2124
enabled = imap.enable(:utf8, "condstore QResync")
2225
assert_equal("RUBY0002 ENABLE UTF8=ACCEPT condstore QResync",
2326
cmdq.pop.raw.strip)
2427
assert_equal %w[UTF8=ACCEPT], enabled
28+
assert imap.utf8_enabled?
2529

2630
assert_empty cmdq
2731

@@ -35,6 +39,21 @@ def test_enable
3539
end
3640
end
3741

42+
test("enable IMAP4rev2") do
43+
with_fake_server(
44+
with_extensions: %i[ENABLE CONDSTORE IMAP4rev2 UTF8=ACCEPT],
45+
capabilities_enablable: %w[CONDSTORE UTF8=ACCEPT IMAP4rev2]
46+
) do |server, imap|
47+
cmdq = server.commands
48+
49+
enabled = imap.enable("IMAP4rev2")
50+
assert_equal "RUBY0001 ENABLE IMAP4rev2", cmdq.pop.raw.strip
51+
assert imap.utf8_enabled?
52+
53+
assert_empty cmdq
54+
end
55+
end
56+
3857
test("missing server ENABLED response") do
3958
with_fake_server do |server, imap|
4059
server.on "ENABLE", &:done_ok

0 commit comments

Comments
 (0)