| 1 |
package extensions |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/emersion/go-imap" |
| 8 |
"github.com/emersion/go-imap/client" |
| 9 |
"github.com/emersion/go-imap/responses" |
| 10 |
"github.com/emersion/go-imap/utf7" |
| 11 |
) |
| 12 |
|
| 13 |
// A LIST-STATUS client |
| 14 |
type ListStatusClient struct { |
| 15 |
c *client.Client |
| 16 |
} |
| 17 |
|
| 18 |
func NewListStatusClient(c *client.Client) *ListStatusClient { |
| 19 |
return &ListStatusClient{c} |
| 20 |
} |
| 21 |
|
| 22 |
// SupportListStatus checks if the server supports the LIST-STATUS extension. |
| 23 |
func (c *ListStatusClient) SupportListStatus() (bool, error) { |
| 24 |
return c.c.Support("LIST-STATUS") |
| 25 |
} |
| 26 |
|
| 27 |
// ListStatus performs a LIST-STATUS command, listing mailboxes and also |
| 28 |
// retrieving the requested status items. A nil channel can be passed in order |
| 29 |
// to only retrieve the STATUS responses |
| 30 |
func (c *ListStatusClient) ListStatus( |
| 31 |
ref string, |
| 32 |
name string, |
| 33 |
items []imap.StatusItem, |
| 34 |
ch chan *imap.MailboxInfo, |
| 35 |
) ([]*imap.MailboxStatus, error) { |
| 36 |
if ch != nil { |
| 37 |
defer close(ch) |
| 38 |
} |
| 39 |
|
| 40 |
if c.c.State() != imap.AuthenticatedState && c.c.State() != imap.SelectedState { |
| 41 |
return nil, client.ErrNotLoggedIn |
| 42 |
} |
| 43 |
|
| 44 |
cmd := &ListStatusCommand{ |
| 45 |
Reference: ref, |
| 46 |
Mailbox: name, |
| 47 |
Items: items, |
| 48 |
} |
| 49 |
res := &ListStatusResponse{Mailboxes: ch} |
| 50 |
|
| 51 |
status, err := c.c.Execute(cmd, res) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
return res.Statuses, status.Err() |
| 56 |
} |
| 57 |
|
| 58 |
// ListStatusCommand is a LIST command, as defined in RFC 3501 section 6.3.8. If |
| 59 |
// Subscribed is set to true, LSUB will be used instead. Mailbox statuses will |
| 60 |
// be returned if Items is not nil |
| 61 |
type ListStatusCommand struct { |
| 62 |
Reference string |
| 63 |
Mailbox string |
| 64 |
|
| 65 |
Subscribed bool |
| 66 |
Items []imap.StatusItem |
| 67 |
} |
| 68 |
|
| 69 |
func (cmd *ListStatusCommand) Command() *imap.Command { |
| 70 |
name := "LIST" |
| 71 |
if cmd.Subscribed { |
| 72 |
name = "LSUB" |
| 73 |
} |
| 74 |
|
| 75 |
enc := utf7.Encoding.NewEncoder() |
| 76 |
ref, _ := enc.String(cmd.Reference) |
| 77 |
mailbox, _ := enc.String(cmd.Mailbox) |
| 78 |
|
| 79 |
items := make([]string, len(cmd.Items)) |
| 80 |
if cmd.Items != nil { |
| 81 |
for i, item := range cmd.Items { |
| 82 |
items[i] = string(item) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
args := fmt.Sprintf("RETURN (STATUS (%s))", strings.Join(items, " ")) |
| 87 |
return &imap.Command{ |
| 88 |
Name: name, |
| 89 |
Arguments: []any{ref, mailbox, imap.RawString(args)}, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// A LIST-STATUS response |
| 94 |
type ListStatusResponse struct { |
| 95 |
Mailboxes chan *imap.MailboxInfo |
| 96 |
Subscribed bool |
| 97 |
Statuses []*imap.MailboxStatus |
| 98 |
} |
| 99 |
|
| 100 |
func (r *ListStatusResponse) Name() string { |
| 101 |
if r.Subscribed { |
| 102 |
return "LSUB" |
| 103 |
} else { |
| 104 |
return "LIST" |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func (r *ListStatusResponse) Handle(resp imap.Resp) error { |
| 109 |
name, _, ok := imap.ParseNamedResp(resp) |
| 110 |
if !ok { |
| 111 |
return responses.ErrUnhandled |
| 112 |
} |
| 113 |
switch name { |
| 114 |
case "LIST": |
| 115 |
if r.Mailboxes == nil { |
| 116 |
return nil |
| 117 |
} |
| 118 |
res := responses.List{Mailboxes: r.Mailboxes} |
| 119 |
return res.Handle(resp) |
| 120 |
case "STATUS": |
| 121 |
res := responses.Status{ |
| 122 |
Mailbox: new(imap.MailboxStatus), |
| 123 |
} |
| 124 |
err := res.Handle(resp) |
| 125 |
if err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
r.Statuses = append(r.Statuses, res.Mailbox) |
| 129 |
default: |
| 130 |
return responses.ErrUnhandled |
| 131 |
} |
| 132 |
|
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
func (r *ListStatusResponse) WriteTo(w *imap.Writer) error { |
| 137 |
respName := r.Name() |
| 138 |
|
| 139 |
for mbox := range r.Mailboxes { |
| 140 |
fields := []any{imap.RawString(respName)} |
| 141 |
fields = append(fields, mbox.Format()...) |
| 142 |
|
| 143 |
resp := imap.NewUntaggedResp(fields) |
| 144 |
if err := resp.WriteTo(w); err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
} |
| 148 |
return nil |
| 149 |
} |