Skip to content

Commit 8c20002

Browse files
authored
🔀 Merge pull request #72 from ruby/unselect-command
Add the UNSELECT command
2 parents 11757d8 + 34f35ee commit 8c20002

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/net/imap.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,20 @@ def close
796796
send_command("CLOSE")
797797
end
798798

799+
# Sends an {UNSELECT command [IMAP4rev2
800+
# §6.4.2]}[https://www.rfc-editor.org/rfc/rfc9051#section-6.4.2] to free the
801+
# session resources for a mailbox and return to the "_authenticated_" state.
802+
# This is the same as #close, except that <tt>\\Deleted</tt> messages are
803+
# not removed from the mailbox.
804+
#
805+
# ===== Capabilities
806+
#
807+
# The server's capabilities must include +UNSELECT+
808+
# [RFC3691[https://tools.ietf.org/html/rfc3691]].
809+
def unselect
810+
send_command("UNSELECT")
811+
end
812+
799813
# Sends a EXPUNGE command to permanently remove from the currently
800814
# selected mailbox all messages that have the \Deleted flag set.
801815
def expunge

test/net/imap/test_imap.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,73 @@ def test_uidplus_responses
869869
end
870870
end
871871

872+
def yields_in_test_server_thread(
873+
greeting = "* OK [CAPABILITY IMAP4rev1 AUTH=PLAIN STARTTLS] test server\r\n"
874+
)
875+
server = create_tcp_server
876+
port = server.addr[1]
877+
@threads << Thread.start do
878+
sock = server.accept
879+
gets = ->{
880+
buf = "".b
881+
buf << sock.gets until /\A([^ ]+) ([^ ]+) ?(.*)\r\n\z/mn =~ buf
882+
[$1, $2, $3]
883+
}
884+
begin
885+
sock.print(greeting)
886+
last_tag = yield sock, gets
887+
sock.print("* BYE terminating connection\r\n")
888+
sock.print("#{last_tag} OK LOGOUT completed\r\n") if last_tag
889+
ensure
890+
sock.close
891+
server.close
892+
end
893+
end
894+
port
895+
end
896+
897+
def test_close
898+
requests = Queue.new
899+
port = yields_in_test_server_thread do |sock, gets|
900+
requests.push(gets[])
901+
sock.print("RUBY0001 OK CLOSE completed\r\n")
902+
requests.push(gets[])
903+
"RUBY0002"
904+
end
905+
begin
906+
imap = Net::IMAP.new(server_addr, :port => port)
907+
resp = imap.close
908+
assert_equal(["RUBY0001", "CLOSE", ""], requests.pop)
909+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
910+
[resp.class, resp.tag, resp.name])
911+
imap.logout
912+
assert_equal(["RUBY0002", "LOGOUT", ""], requests.pop)
913+
ensure
914+
imap.disconnect if imap
915+
end
916+
end
917+
918+
def test_unselect
919+
requests = Queue.new
920+
port = yields_in_test_server_thread do |sock, gets|
921+
requests.push(gets[])
922+
sock.print("RUBY0001 OK UNSELECT completed\r\n")
923+
requests.push(gets[])
924+
"RUBY0002"
925+
end
926+
begin
927+
imap = Net::IMAP.new(server_addr, :port => port)
928+
resp = imap.unselect
929+
assert_equal(["RUBY0001", "UNSELECT", ""], requests.pop)
930+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
931+
[resp.class, resp.tag, resp.name])
932+
imap.logout
933+
assert_equal(["RUBY0002", "LOGOUT", ""], requests.pop)
934+
ensure
935+
imap.disconnect if imap
936+
end
937+
end
938+
872939
private
873940

874941
def imaps_test

0 commit comments

Comments
 (0)