diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 82c478ed0..f5d1fe1de 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -789,6 +789,20 @@ def close send_command("CLOSE") end + # Sends an {UNSELECT command [IMAP4rev2 + # ยง6.4.2]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.4.2] to free the + # session resources for a mailbox and return to the "_authenticated_" state. + # This is the same as #close, except that \\Deleted messages are + # not removed from the mailbox. + # + # ===== Capabilities + # + # The server's capabilities must include +UNSELECT+ + # [RFC3691[https://tools.ietf.org/html/rfc3691]]. + def unselect + send_command("UNSELECT") + end + # Sends a EXPUNGE command to permanently remove from the currently # selected mailbox all messages that have the \Deleted flag set. def expunge diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb index 720cb3c5e..308c83da4 100644 --- a/test/net/imap/test_imap.rb +++ b/test/net/imap/test_imap.rb @@ -915,6 +915,27 @@ def test_close end end + def test_unselect + requests = Queue.new + port = yields_in_test_server_thread do |sock, gets| + requests.push(gets[]) + sock.print("RUBY0001 OK UNSELECT completed\r\n") + requests.push(gets[]) + "RUBY0002" + end + begin + imap = Net::IMAP.new(server_addr, :port => port) + resp = imap.unselect + assert_equal(["RUBY0001", "UNSELECT", ""], requests.pop) + assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"], + [resp.class, resp.tag, resp.name]) + imap.logout + assert_equal(["RUBY0002", "LOGOUT", ""], requests.pop) + ensure + imap.disconnect if imap + end + end + private def imaps_test