-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_part.go
More file actions
207 lines (186 loc) · 5.9 KB
/
client_part.go
File metadata and controls
207 lines (186 loc) · 5.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package xmppcore
import (
"encoding/xml"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/jackal-xmpp/stravaganza/v2"
)
type ClientPart struct {
features []ElemHandler
channel Channel
attr PartAttr
logger Logger
conn Conn
elemRunner
}
func NewClientPart(conn Conn, logger Logger, s *PartAttr) *ClientPart {
channel := NewXChannel(conn, false)
if s.ID == "" {
s.ID = uuid.New().String()
}
return &ClientPart{
features: []ElemHandler{},
channel: channel,
logger: logger,
elemRunner: ElemRunner(channel),
attr: *s,
conn: conn,
}
}
func (od *ClientPart) Attr() *PartAttr {
return &od.attr
}
func (od *ClientPart) Channel() Channel {
return od.channel
}
func (od *ClientPart) WithFeature(h ElemHandler) {
od.features = append(od.features, h)
}
func (od *ClientPart) Logger() Logger {
return od.logger
}
func (od *ClientPart) ID() string {
return od.attr.ID
}
func (od *ClientPart) Conn() Conn {
return od.conn
}
func (od *ClientPart) Negotiate() error {
od.Channel().Open(od.Attr())
var header xml.StartElement
if err := od.Channel().WaitHeader(&header); err != nil {
return err
}
return od.handleFeatures(header)
}
func (od *ClientPart) Stop() {
od.Quit()
}
// +---------------------+
// | open TCP connection |
// +---------------------+
// |
// v
// +---------------+
// | send initial |<-------------------------+
// | stream header | ^
// +---------------+ |
// | |
// v |
// +------------------+ |
// | receive response | |
// | stream header | |
// +------------------+ |
// | |
// v |
// +----------------+ |
// | receive stream | |
// +------------------>| features | |
// ^ {OPTIONAL} +----------------+ |
// | | |
// | v |
// | +<-----------------+ |
// | | |
// | {empty?} ----> {all voluntary?} ----> {some mandatory?} |
// | | no | no | |
// | | yes | yes | yes |
// | | v v |
// | | +---------------+ +----------------+ |
// | | | MAY negotiate | | MUST negotiate | |
// | | | any or none | | one feature | |
// | | +---------------+ +----------------+ |
// | v | | |
// | +---------+ v | |
// | | DONE |<----- {negotiate?} | |
// | +---------+ no | | |
// | yes | | |
// | v v |
// | +--------->+<---------+ |
// | | |
// | v |
// +<-------------------------- {restart mandatory?} ------------>+
// no yes
func (od *ClientPart) handleFeatures(header xml.StartElement) error {
for {
features, err := od.serverFeatures()
handle:
if err != nil || len(features) == 0 {
return err
}
f, rest := od.selectOne(features)
handled, err := od.handle(f)
if err != nil {
return err
}
if !handled {
features = rest
goto handle
}
if err := od.Channel().Open(od.Attr()); err != nil {
return err
}
if err := od.Channel().WaitHeader(&header); err != nil {
return err
}
}
}
func (od *ClientPart) Run() chan error {
return od.elemRunner.Run(od)
}
func (od *ClientPart) handle(f stravaganza.Element) (handled bool, err error) {
for _, h := range od.features {
if handled, err = h.Handle(f, od); handled {
if err != nil {
return
}
break
}
}
if !handled && od.isMandatory(f) {
err = fmt.Errorf("feature %s not handled", f.Name())
}
return
}
func (od *ClientPart) OnOpenHeader(header xml.StartElement) error {
return errors.New("unexpected open header")
}
func (od *ClientPart) OnWhiteSpace(bs []byte) {}
func (od *ClientPart) OnCloseToken() {
od.Quit()
}
func (od *ClientPart) selectOne(features []stravaganza.Element) (f stravaganza.Element, rest []stravaganza.Element) {
priorities := []string{"starttls", "mechanisms", "bind"}
for _, s := range priorities {
var i int
for i, f = range features {
if f.Name() == s {
rest = append(features[:i], features[i+1:]...)
return
}
}
}
f = features[0]
rest = features[1:]
return
}
func (od *ClientPart) isMandatory(elem stravaganza.Element) bool {
if elem.Name() == "starttls" || elem.Name() == "bind" {
return elem.Child("required") != nil
}
if elem.Name() == "compression" {
return false
}
return true
}
func (od *ClientPart) serverFeatures() (res []stravaganza.Element, err error) {
var elem stravaganza.Element
if err = od.channel.NextElement(&elem); err != nil {
return
}
if elem.Name() != "features" {
return
}
res = elem.AllChildren()
return
}