This repository was archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
104 lines (86 loc) · 2.6 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package move
import (
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-imap/commands"
)
// Client is a MOVE client.
type Client struct {
c *client.Client
}
// NewClient creates a new client.
func NewClient(c *client.Client) *Client {
return &Client{c: c}
}
// SupportMove checks if the server supports the MOVE extension.
func (c *Client) SupportMove() (bool, error) {
return c.c.Support(Capability)
}
func (c *Client) move(uid bool, seqset *imap.SeqSet, dest string) error {
if c.c.State() != imap.SelectedState {
return client.ErrNoMailboxSelected
}
var cmd imap.Commander = &Command{
SeqSet: seqset,
Mailbox: dest,
}
if uid {
cmd = &commands.Uid{Cmd: cmd}
}
if status, err := c.c.Execute(cmd, nil); err != nil {
return err
} else {
return status.Err()
}
}
func (c *Client) moveWithFallback(uid bool, seqset *imap.SeqSet, dest string) error {
if ok, err := c.SupportMove(); err != nil {
return err
} else if ok {
return c.move(uid, seqset, dest)
}
if c.c.State() != imap.SelectedState {
return client.ErrNoMailboxSelected
}
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
if uid {
if err := c.c.UidCopy(seqset, dest); err != nil {
return err
}
if err := c.c.UidStore(seqset, item, flags, nil); err != nil {
return err
}
} else {
if err := c.c.Copy(seqset, dest); err != nil {
return err
}
if err := c.c.Store(seqset, item, flags, nil); err != nil {
return err
}
}
return c.c.Expunge(nil)
}
// Move moves the specified message(s) to the end of the specified destination
// mailbox.
func (c *Client) Move(seqset *imap.SeqSet, dest string) error {
return c.move(false, seqset, dest)
}
// UidMove is identical to Move, but seqset is interpreted as containing unique
// identifiers instead of message sequence numbers.
func (c *Client) UidMove(seqset *imap.SeqSet, dest string) error {
return c.move(true, seqset, dest)
}
// MoveWithFallback tries to move if the server supports it. If it doesn't, it
// falls back to copy, store and expunge, as defined in RFC 6851 section 3.3.
func (c *Client) MoveWithFallback(seqset *imap.SeqSet, dest string) error {
return c.moveWithFallback(false, seqset, dest)
}
// UidMoveWithFallback is identical to MoveWithFallback, but seqset is
// interpreted as containing unique identifiers instead of message sequence
// numbers.
func (c *Client) UidMoveWithFallback(seqset *imap.SeqSet, dest string) error {
return c.moveWithFallback(true, seqset, dest)
}
// MoveClient is an alias used to compose multiple client extensions.
type MoveClient = Client