Skip to content

Commit 1b5e0cb

Browse files
committed
✨ Add greeting code data to responses
This simplifies caching of the server capabilities, as many servers will send their capabilities inside the greeting. Needed for #31.
1 parent cfcbcd5 commit 1b5e0cb

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/net/imap.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ def initialize(host, port_or_options = {},
21282128
else
21292129
@usessl = false
21302130
end
2131-
@responses = Hash.new([].freeze)
2131+
@responses = Hash.new {|h, k| h[k] = [] }
21322132
@tagged_responses = {}
21332133
@response_handlers = []
21342134
@tagged_response_arrival = new_cond
@@ -2144,6 +2144,7 @@ def initialize(host, port_or_options = {},
21442144
if @greeting.nil?
21452145
raise Error, "connection closed"
21462146
end
2147+
record_untagged_response_code(@greeting)
21472148
if @greeting.name == "BYE"
21482149
raise ByeResponseError, @greeting
21492150
end
@@ -2208,10 +2209,7 @@ def receive_responses
22082209
end
22092210
when UntaggedResponse
22102211
record_response(resp.name, resp.data)
2211-
if resp.data.instance_of?(ResponseText) &&
2212-
(code = resp.data.code)
2213-
record_response(code.name, code.data)
2214-
end
2212+
record_untagged_response_code(resp)
22152213
if resp.name == "BYE" && @logout_command_tag.nil?
22162214
@sock.close
22172215
@exception = ByeResponseError.new(resp)
@@ -2289,6 +2287,13 @@ def get_response
22892287
return @parser.parse(buff)
22902288
end
22912289

2290+
def record_untagged_response_code(resp)
2291+
if resp.data.instance_of?(ResponseText) &&
2292+
(code = resp.data.code)
2293+
record_response(code.name, code.data)
2294+
end
2295+
end
2296+
22922297
def record_response(name, data)
22932298
unless @responses.has_key?(name)
22942299
@responses[name] = []

test/net/imap/test_imap.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,12 @@ def test_responses
928928
end
929929
begin
930930
imap = Net::IMAP.new(server_addr, port: port)
931+
# responses available before SELECT/EXAMINE
932+
assert_equal(%w[IMAP4REV1 AUTH=PLAIN STARTTLS],
933+
imap.responses("CAPABILITY", &:last))
931934
resp = imap.select "INBOX"
935+
# responses are cleared after SELECT/EXAMINE
936+
assert_equal(nil, imap.responses("CAPABILITY", &:last))
932937
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
933938
[resp.class, resp.tag, resp.name])
934939
assert_equal([172], imap.responses { _1["EXISTS"] })

0 commit comments

Comments
 (0)