forked from jordwest/imap-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_copy.go
75 lines (62 loc) · 1.6 KB
/
command_copy.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
package conn
import (
"strings"
"github.com/ramoncasares/ro-imap-server.go/mailstore"
"github.com/ramoncasares/ro-imap-server.go/types"
)
const (
copyArgUID int = 0
copyArgRange int = 1
copyArgMailbox int = 2
)
func cmdCopy(args commandArgs, c *Conn) {
if !c.assertSelected(args.ID(), readWrite) {
return
}
// Check if the target mailbox exists.
targetMailbox := args.Arg(copyArgMailbox)
mbox, err := c.User.MailboxByName(targetMailbox)
if err != nil {
c.writeResponse(args.ID(), "NO [TRYCREATE] "+err.Error())
return
}
// Check if connection is writable.
if c.mailboxWritable != readWrite {
c.writeResponse(args.ID(), "NO read-only connection")
return
}
// Fetch the messages.
seqSet, err := types.InterpretSequenceSet(args.Arg(copyArgRange))
if err != nil {
c.writeResponse(args.ID(), "NO "+err.Error())
return
}
searchByUID := strings.ToUpper(args.Arg(copyArgUID)) == "UID "
var msgs []mailstore.Message
if searchByUID {
msgs = c.SelectedMailbox.MessageSetByUID(seqSet)
} else {
msgs = c.SelectedMailbox.MessageSetBySequenceNumber(seqSet)
}
if len(msgs) == 0 {
c.writeResponse(args.ID(), "NO no messages found")
return
}
for _, msg := range msgs {
_, err := mbox.NewMessage().
SetBody(msg.Body()).
SetHeaders(msg.Header()).
AddFlags(msg.Flags() & types.FlagRecent).
Save()
if err != nil {
// TODO Reverse all previous operations if it failed.
c.writeResponse(args.ID(), "NO "+err.Error())
return
}
}
if searchByUID {
c.writeResponse(args.ID(), "OK UID COPY Completed")
} else {
c.writeResponse(args.ID(), "OK COPY Completed")
}
}