@@ -8,9 +8,15 @@ import (
88 "github.com/emersion/go-imap/responses"
99)
1010
11- // ErrNoMailboxSelected is returned if a command that requires a mailbox to be
12- // selected is called when there isn't.
13- var ErrNoMailboxSelected = errors .New ("No mailbox selected" )
11+ var (
12+ // ErrNoMailboxSelected is returned if a command that requires a mailbox to be
13+ // selected is called when there isn't.
14+ ErrNoMailboxSelected = errors .New ("No mailbox selected" )
15+
16+ // ErrExtensionUnsupported is returned if a command uses a extension that
17+ // is not supported by the server.
18+ ErrExtensionUnsupported = errors .New ("The required extension is not supported by the server" )
19+ )
1420
1521// Check requests a checkpoint of the currently selected mailbox. A checkpoint
1622// refers to any implementation-dependent housekeeping associated with the
@@ -265,3 +271,30 @@ func (c *Client) Copy(seqset *imap.SeqSet, dest string) error {
265271func (c * Client ) UidCopy (seqset * imap.SeqSet , dest string ) error {
266272 return c .copy (true , seqset , dest )
267273}
274+
275+ // Unselect frees server's resources associated with the selected mailbox and
276+ // returns the server to the authenticated state. This command performs the same
277+ // actions as Close, except that no messages are permanently removed from the
278+ // currently selected mailbox.
279+ //
280+ // If client does not support the UNSELECT extension, ErrExtensionUnsupported
281+ // is returned.
282+ func (c * Client ) Unselect () error {
283+ if ok , err := c .Support ("UNSELECT" ); ! ok || err != nil {
284+ return ErrExtensionUnsupported
285+ }
286+
287+ if c .State () != imap .SelectedState {
288+ return ErrNoMailboxSelected
289+ }
290+
291+ cmd := & commands.Unselect {}
292+ if status , err := c .Execute (cmd , nil ); err != nil {
293+ return err
294+ } else if err := status .Err (); err != nil {
295+ return err
296+ }
297+
298+ c .SetState (imap .AuthenticatedState , nil )
299+ return nil
300+ }
0 commit comments