Skip to content

Commit a40c0cf

Browse files
arntnevans
authored andcommitted
Add support for ENABLE (RFC 5161)
1 parent dcc36a8 commit a40c0cf

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

lib/net/imap.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ module Net
199199
# [IDLE[https://tools.ietf.org/html/rfc2177]],
200200
# [NAMESPACE[https://tools.ietf.org/html/rfc2342]],
201201
# [UNSELECT[https://tools.ietf.org/html/rfc3691]],
202+
# [ENABLE[https://tools.ietf.org/html/rfc5161]],
202203
#--
203-
# TODO: [ENABLE[https://tools.ietf.org/html/rfc5161]],
204204
# TODO: [LIST-EXTENDED[https://tools.ietf.org/html/rfc5258]],
205205
# TODO: [LIST-STATUS[https://tools.ietf.org/html/rfc5819]],
206206
#++
@@ -1879,6 +1879,27 @@ def uid_thread(algorithm, search_keys, charset)
18791879
return thread_internal("UID THREAD", algorithm, search_keys, charset)
18801880
end
18811881

1882+
# Sends an {ENABLE command [RFC5161 §3.2]}[https://www.rfc-editor.org/rfc/rfc5161#section-3.1]
1883+
# {[IMAP4rev2 §6.3.1]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.3.1]
1884+
# to enable the specified extenstions, which may be either an
1885+
# array or a string.
1886+
#
1887+
# Some of the extensions that use ENABLE permit the server to send
1888+
# syntax that this class cannot parse. Caution is advised.
1889+
#
1890+
# ===== Capabilities
1891+
#
1892+
# The server's capabilities must include +ENABLE+
1893+
# [RFC5161[https://tools.ietf.org/html/rfc5161]] or IMAP4REV2
1894+
# [RFC9051[https://tools.ietf.org/html/rfc9051]].
1895+
1896+
def enable(extensions)
1897+
synchronize do
1898+
send_command("ENABLE #{[extensions].flatten.join(' ')}")
1899+
return @responses.delete("ENABLED")[-1]
1900+
end
1901+
end
1902+
18821903
# Sends an {IDLE command [RFC2177 §3]}[https://www.rfc-editor.org/rfc/rfc6851#section-3]
18831904
# {[IMAP4rev2 §6.3.13]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.3.13]
18841905
# that waits for notifications of new or expunged messages. Yields

lib/net/imap/response_parser.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def response_untagged
160160
return capability_response
161161
when /\A(?:NOOP)\z/ni
162162
return ignored_response
163+
when /\A(?:ENABLED)\z/ni
164+
return enabled_response
163165
else
164166
return text_response
165167
end
@@ -974,6 +976,13 @@ def capability_response
974976
UntaggedResponse.new(name, capability_data, @str)
975977
end
976978

979+
def enabled_response
980+
token = match(T_ATOM)
981+
name = token.value.upcase
982+
match(T_SPACE)
983+
UntaggedResponse.new(name, capability_data, @str)
984+
end
985+
977986
def capability_data
978987
data = []
979988
while true

test/net/imap/test_imap.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,37 @@ def test_uidplus_responses
869869
end
870870
end
871871

872+
def test_enable
873+
server = create_tcp_server
874+
port = server.addr[1]
875+
requests = []
876+
start_server do
877+
sock = server.accept
878+
begin
879+
sock.print("* OK test server\r\n")
880+
requests.push(sock.gets)
881+
sock.print("* ENABLED SMTPUTF8\r\n")
882+
sock.print("RUBY0001 OK \r\n")
883+
sock.gets
884+
sock.print("* BYE terminating connection\r\n")
885+
sock.print("RUBY0002 OK LOGOUT completed\r\n")
886+
ensure
887+
sock.close
888+
server.close
889+
end
890+
end
891+
892+
begin
893+
imap = Net::IMAP.new(server_addr, port: port)
894+
response = imap.enable(["SMTPUTF8", "X-NO-SUCH-THING"])
895+
assert_equal("RUBY0001 ENABLE SMTPUTF8 X-NO-SUCH-THING\r\n", requests.pop)
896+
assert_equal(response, ["SMTPUTF8"])
897+
imap.logout
898+
ensure
899+
imap.disconnect if imap
900+
end
901+
end
902+
872903
def yields_in_test_server_thread(
873904
read_timeout: 2, # requires ruby 3.2+
874905
timeout: 10,

test/net/imap/test_imap_response_parser.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ def test_capability
222222
)
223223
end
224224

225+
def test_enable
226+
parser = Net::IMAP::ResponseParser.new
227+
response = parser.parse("* ENABLED SMTPUTF8\r\n")
228+
assert_equal("ENABLED", response.name)
229+
assert_equal(1, response.data.length)
230+
assert_equal("SMTPUTF8", response.data.first)
231+
end
232+
225233
def test_id
226234
parser = Net::IMAP::ResponseParser.new
227235
response = parser.parse("* ID NIL\r\n")

0 commit comments

Comments
 (0)