-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.go
144 lines (124 loc) · 3.29 KB
/
commands.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package sortthread
import (
"errors"
"io"
"strings"
"github.com/emersion/go-imap"
)
// SortCommand is a SORT command.
type SortCommand struct {
SortCriteria []SortCriterion
Charset string
SearchCriteria *imap.SearchCriteria
}
func (cmd *SortCommand) Command() *imap.Command {
args := []interface{}{
formatSortCriteria(cmd.SortCriteria),
cmd.Charset,
}
args = append(args, cmd.SearchCriteria.Format()...)
return &imap.Command{
Name: "SORT",
Arguments: args,
}
}
func parseSortCriteria(fields interface{}) ([]SortCriterion, error) {
list, ok := fields.([]interface{})
if !ok {
return nil, errors.New("List is required as a sort criteria")
}
result := make([]SortCriterion, 0, len(list))
reverse := false
for _, crit := range list {
crit, ok := crit.(string)
if !ok {
return nil, errors.New("String is required as a sort key")
}
if strings.EqualFold(crit, "REVERSE") {
reverse = true
continue
}
crit = strings.ToUpper(crit)
switch crit {
// TODO: Fix types for constants.
case string(SortArrival), SortCc, SortDate, SortFrom, SortSize, SortSubject, SortTo:
default:
return nil, errors.New("Unknown sort criteria: " + crit)
}
result = append(result, SortCriterion{
Field: SortField(crit),
Reverse: reverse,
})
reverse = false
}
if reverse {
return nil, errors.New("Missing sort key after REVERSE")
}
return result, nil
}
func (cmd *SortCommand) Parse(fields []interface{}) error {
if len(fields) < 3 {
return errors.New("Not enough SORT arguments")
}
var err error
cmd.SortCriteria, err = parseSortCriteria(fields[0])
if err != nil {
return err
}
// Charset parameter for SORT is specified without "CHARSET"
// and is required.
charset, ok := fields[1].(string)
if !ok {
return errors.New("String is required as a charset")
}
charset = strings.ToLower(charset)
var charsetReader func(io.Reader) io.Reader
if charset != "utf-8" && charset != "us-ascii" && charset != "" {
charsetReader = func(r io.Reader) io.Reader {
r, _ = imap.CharsetReader(charset, r)
return r
}
}
cmd.SearchCriteria = &imap.SearchCriteria{}
return cmd.SearchCriteria.ParseWithCharset(fields[2:], charsetReader)
}
// ThreadCommand is a THREAD command.
type ThreadCommand struct {
Algorithm ThreadAlgorithm
Charset string
SearchCriteria *imap.SearchCriteria
}
func (cmd *ThreadCommand) Command() *imap.Command {
return &imap.Command{
Name: "THREAD",
Arguments: []interface{}{
formatThreadAlgorithm(cmd.Algorithm),
cmd.Charset,
cmd.SearchCriteria.Format(),
},
}
}
func (cmd *ThreadCommand) Parse(fields []interface{}) error {
if len(fields) < 3 {
return errors.New("Not enough THREAD argments")
}
algo, ok := fields[0].(string)
if !ok {
return errors.New("First argument should be a string")
}
charset, ok := fields[1].(string)
if !ok {
return errors.New("Second argument should be a string")
}
charset = strings.ToLower(charset)
var charsetReader func(io.Reader) io.Reader
if charset != "utf-8" && charset != "us-ascii" && charset != "" {
charsetReader = func(r io.Reader) io.Reader {
r, _ = imap.CharsetReader(charset, r)
return r
}
}
cmd.Algorithm = ThreadAlgorithm(algo)
cmd.SearchCriteria = &imap.SearchCriteria{}
return cmd.SearchCriteria.ParseWithCharset(fields[2:], charsetReader)
}