Skip to content

Commit

Permalink
✨ Add greeting code data to responses
Browse files Browse the repository at this point in the history
This simplifies caching of the server capabilities, as many servers will
send their capabilities inside the greeting.  Needed for #31.
  • Loading branch information
nevans committed Jan 8, 2023
1 parent 448d341 commit 0304ca3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ def initialize(host, port_or_options = {},
else
@usessl = false
end
@responses = Hash.new([].freeze)
@responses = Hash.new {|h, k| h[k] = [] }
@tagged_responses = {}
@response_handlers = []
@tagged_response_arrival = new_cond
Expand All @@ -2150,6 +2150,7 @@ def initialize(host, port_or_options = {},
if @greeting.nil?
raise Error, "connection closed"
end
record_untagged_response_code(@greeting)
if @greeting.name == "BYE"
raise ByeResponseError, @greeting
end
Expand Down Expand Up @@ -2214,10 +2215,7 @@ def receive_responses
end
when UntaggedResponse
record_response(resp.name, resp.data)
if resp.data.instance_of?(ResponseText) &&
(code = resp.data.code)
record_response(code.name, code.data)
end
record_untagged_response_code(resp)
if resp.name == "BYE" && @logout_command_tag.nil?
@sock.close
@exception = ByeResponseError.new(resp)
Expand Down Expand Up @@ -2295,6 +2293,13 @@ def get_response
return @parser.parse(buff)
end

def record_untagged_response_code(resp)
if resp.data.instance_of?(ResponseText) &&
(code = resp.data.code)
record_response(code.name, code.data)
end
end

def record_response(name, data)
unless @responses.has_key?(name)
@responses[name] = []
Expand Down
5 changes: 5 additions & 0 deletions test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,12 @@ def test_responses
end
begin
imap = Net::IMAP.new(server_addr, port: port)
# responses available before SELECT/EXAMINE
assert_equal(%w[IMAP4REV1 AUTH=PLAIN STARTTLS],
imap.responses("CAPABILITY", &:last))
resp = imap.select "INBOX"
# responses are cleared after SELECT/EXAMINE
assert_equal(nil, imap.responses("CAPABILITY", &:last))
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
[resp.class, resp.tag, resp.name])
assert_equal([172], imap.responses { _1["EXISTS"] })
Expand Down

0 comments on commit 0304ca3

Please sign in to comment.