diff --git a/lib/net/imap.rb b/lib/net/imap.rb index a3ebbb86..2c5d8172 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -868,7 +868,7 @@ def client_thread # :nodoc: # Disconnects from the server. # - # Related: #logout + # Related: #logout, #logout! def disconnect return if disconnected? begin @@ -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 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. # diff --git a/test/net/imap/fake_server/command_reader.rb b/test/net/imap/fake_server/command_reader.rb index 2fd15ef2..ecfef530 100644 --- a/test/net/imap/fake_server/command_reader.rb +++ b/test/net/imap/fake_server/command_reader.rb @@ -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 diff --git a/test/net/imap/fake_server/test_helper.rb b/test/net/imap/fake_server/test_helper.rb index c35b1d6a..eee6a153 100644 --- a/test/net/imap/fake_server/test_helper.rb +++ b/test/net/imap/fake_server/test_helper.rb @@ -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