-
Notifications
You must be signed in to change notification settings - Fork 2
/
responses.go
120 lines (105 loc) · 2.67 KB
/
responses.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
package sortthread
import (
"strconv"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/responses"
)
type SortResponse struct {
Ids []uint32
}
type ThreadResponse struct {
Threads []*Thread
}
func (r *SortResponse) Handle(resp imap.Resp) error {
name, fields, ok := imap.ParseNamedResp(resp)
if !ok || name != "SORT" {
return responses.ErrUnhandled
}
r.Ids = make([]uint32, len(fields))
for i, f := range fields {
if id, err := imap.ParseNumber(f); err != nil {
return err
} else {
r.Ids[i] = id
}
}
return nil
}
func (r *SortResponse) WriteTo(w *imap.Writer) error {
fields := make([]interface{}, 0, len(r.Ids)+1)
fields = append(fields, imap.RawString("SORT"))
for _, id := range r.Ids {
fields = append(fields, imap.RawString(strconv.FormatInt(int64(id), 10)))
}
return imap.NewUntaggedResp(fields).WriteTo(w)
}
func (r *ThreadResponse) Handle(resp imap.Resp) error {
name, fields, ok := imap.ParseNamedResp(resp)
if !ok || name != "THREAD" {
return responses.ErrUnhandled
}
if threads, err := parseThreadResp(fields); err != nil {
return err
} else {
r.Threads = threads
}
return nil
}
func parseThreadResp(fields []interface{}) ([]*Thread, error) {
var parent *Thread
var siblings []*Thread
for _, f := range fields {
switch f := f.(type) {
case string, imap.RawString:
id, err := imap.ParseNumber(f)
if err != nil {
return nil, err
}
t := Thread{Id: id}
if parent == nil {
siblings = append(siblings, &t)
} else {
parent.Children = append(t.Children, &t)
}
parent = &t
case []interface{}:
t, err := parseThreadResp(f)
if err != nil {
return nil, err
}
// Parent doesn't exist, e.g. didn't match the search
// criteria. Let's ignore the parent thread.
if parent == nil {
siblings = append(siblings, t...)
} else {
parent.Children = append(parent.Children, t...)
}
default:
return nil, responses.ErrUnhandled
}
}
return siblings, nil
}
func formatThread(thread *Thread) []interface{} {
f := make([]interface{}, 0, 1+len(thread.Children))
f = append(f, imap.RawString(strconv.FormatInt(int64(thread.Id), 10)))
if len(thread.Children) == 1 {
f = append(f, formatThread(thread.Children[0])...)
} else {
for _, c := range thread.Children {
f = append(f, formatThread(c))
}
}
return f
}
func formatThreadResp(threads []*Thread) []interface{} {
fields := make([]interface{}, 0, len(threads)+1)
fields = append(fields, imap.RawString("THREAD"))
for _, t := range threads {
fields = append(fields, formatThread(t))
}
return fields
}
func (r *ThreadResponse) WriteTo(w *imap.Writer) error {
return imap.NewUntaggedResp(formatThreadResp(r.Threads)).WriteTo(w)
}