Skip to content

Commit

Permalink
🔀 Merge pull request #178 from nevans/logout-bang
Browse files Browse the repository at this point in the history
✨ Add #logout! to combine logout and disconnect
  • Loading branch information
nevans authored Sep 23, 2023
2 parents 634854b + fa36434 commit 6cd6d87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
27 changes: 25 additions & 2 deletions lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def client_thread # :nodoc:

# Disconnects from the server.
#
# Related: #logout
# Related: #logout, #logout!
def disconnect
return if disconnected?
begin
Expand Down Expand Up @@ -1067,11 +1067,34 @@ def noop
# to inform the command to inform the server that the client is done with
# the connection.
#
# Related: #disconnect
# Related: #disconnect, #logout!
def logout
send_command("LOGOUT")
end

# Calls #logout then, after receiving the TaggedResponse for the +LOGOUT+,
# calls #disconnect. Returns the TaggedResponse from +LOGOUT+. Returns
# +nil+ when the client is already disconnected, in contrast to #logout
# which raises an exception.
#
# If #logout raises a StandardError, a warning will be printed but the
# exception will not be re-raised.
#
# This is useful in situations where the connection must be dropped, for
# example for security or after tests. If logout errors need to be handled,
# use #logout and #disconnect instead.
#
# Related: #logout, #disconnect
def logout!
logout unless disconnected?
rescue => ex
warn "%s during <Net::IMAP %s:%s>logout!: %s" % [
ex.class, host, port, ex
]
ensure
disconnect
end

# Sends a {STARTTLS command [IMAP4rev1 §6.2.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.1]
# to start a TLS session.
#
Expand Down
3 changes: 2 additions & 1 deletion test/net/imap/fake_server/command_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def get_command

# TODO: convert bad command exception to tagged BAD response, when possible
def parse(buf)
/\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or raise "bad request"
/\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or
raise "bad request: %p" [buf]
case $2.upcase
when "LOGIN", "SELECT", "ENABLE", "AUTHENTICATE"
Command.new $1, $2, scan_astrings($3), buf
Expand Down
3 changes: 1 addition & 2 deletions test/net/imap/fake_server/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def with_client(*args, **kwargs)
yield client
ensure
if client && !client.disconnected?
client.logout rescue pp $!
client.disconnect unless client.disconnected?
client.logout!
end
end

Expand Down

0 comments on commit 6cd6d87

Please sign in to comment.