-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp_core.go
414 lines (373 loc) · 11.1 KB
/
xmpp_core.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package xmppcore
import (
"encoding/xml"
"errors"
"github.com/google/uuid"
"github.com/jackal-xmpp/stravaganza/v2"
)
var (
ErrOnNextTokenTimeout = errors.New("get next token timeout")
ErrOnNextElementnTimeout = errors.New("get next element timeout")
ErrClientIgnoredTheFeature = errors.New("client ignored the feature")
ErrUnhandledElement = errors.New("unhandled element")
)
type Feature interface {
Elem() stravaganza.Element
Mandatory() bool
Handled() bool
ElemHandler
}
type ElemHandler interface {
ID() string
Handle(elem stravaganza.Element, part Part) (catched bool, err error)
}
type IDAble struct {
id string
}
func CreateIDAble() IDAble {
return IDAble{id: uuid.New().String()}
}
func (idable IDAble) ID() string {
return idable.id
}
type Part interface {
ID() string
Attr() *PartAttr
Channel() Channel
WithElemHandler(ElemHandler)
Logger() Logger
Conn() Conn
OnOpenHeader(header xml.StartElement) error
OnCloseToken()
OnWhiteSpace([]byte)
}
type elemRunner struct {
channel Channel
elemHandlers []ElemHandler
handleLimit int
handled int
quit bool
}
func ElemRunner(channel Channel) elemRunner {
return elemRunner{
channel: channel,
handleLimit: -1,
handled: 0,
elemHandlers: []ElemHandler{},
quit: false,
}
}
func (er *elemRunner) WithElemHandler(handler ElemHandler) {
for _, h := range er.elemHandlers {
if h.ID() == handler.ID() {
return
}
}
er.elemHandlers = append(er.elemHandlers, handler)
}
func (er elemRunner) Running() bool {
return !er.quit
}
func (er *elemRunner) SetHandleLimit(limit int) {
er.handleLimit = limit
}
func (er *elemRunner) Quit() {
er.quit = true
er.channel.Close()
}
func (er *elemRunner) Run(part Part) chan error {
errChan := make(chan error)
go func() {
i := 0
for {
i = i + 1
i, err := er.channel.next()
if err != nil {
if er.quit {
errChan <- nil
part.Logger().Printf(LogInfo, "quit!")
return
}
part.Logger().Printf(LogError, "a error from part instance [%s] message handler: %s", part.ID(), err.Error())
errChan <- err
return
}
switch t := i.(type) {
case xml.StartElement:
if err := part.OnOpenHeader(t); err != nil {
errChan <- err
return
}
case xml.CharData:
part.OnWhiteSpace(t)
case xml.EndElement:
part.OnCloseToken()
case stravaganza.Element:
for _, handler := range er.elemHandlers {
if catched, err := handler.Handle(t, part); catched {
er.handled = er.handled + 1
} else if err != nil {
part.Logger().Printf(LogError, "a error occured from part instance [%s] message handler: %s", part.ID(), err.Error())
errChan <- err
}
}
if er.handleLimit > 0 && er.handled >= er.handleLimit {
errChan <- nil
close(errChan)
return
}
}
}
}()
return errChan
}
type PartAttr struct {
ID string
JID JID // client's jid
Domain string // server domain
Version string
Xmlns string
XmlLang string
OpenTag bool
}
func (attr *PartAttr) ToClientHead(elem *xml.StartElement) {
to := ""
if attr.JID.Username != "" {
to = attr.JID.String()
}
attr.head(elem, attr.Domain, to)
}
func (attr *PartAttr) ToServerHead(elem *xml.StartElement) {
from := ""
if attr.JID.Username != "" {
from = attr.JID.String()
}
attr.head(elem, from, attr.Domain)
}
func (attr *PartAttr) head(elem *xml.StartElement, from, to string) {
eattr := []xml.Attr{
{Name: xml.Name{Local: "version"}, Value: attr.Version},
{Name: xml.Name{Local: "id"}, Value: attr.ID},
}
if from != "" {
eattr = append(eattr, xml.Attr{Name: xml.Name{Local: "from"}, Value: from})
}
if to != "" {
eattr = append(eattr, xml.Attr{Name: xml.Name{Local: "to"}, Value: to})
}
if attr.XmlLang != "" {
eattr = append(eattr, xml.Attr{Name: xml.Name{Local: "lang", Space: "xml"}, Value: attr.XmlLang})
}
// if attr.Xmlns != "" {
// eattr = append(eattr, xml.Attr{Name: xml.Name{Local: "xmlns"}, Value: attr.Xmlns})
// }
if !attr.OpenTag {
*elem = xml.StartElement{
Name: xml.Name{Space: NSStream, Local: "stream"},
Attr: eattr}
return
}
*elem = xml.StartElement{
Name: xml.Name{Space: NSFraming, Local: "open"},
Attr: eattr}
}
func (sa *PartAttr) ParseToServer(elem xml.StartElement) error {
isStream := elem.Name.Local == "stream" && elem.Name.Space == NSStream
sa.OpenTag = elem.Name.Local == "open" && elem.Name.Space == NSFraming
if !isStream && !sa.OpenTag {
return ErrNotHeaderStart
}
for _, attr := range elem.Attr {
if attr.Name.Local == "from" && attr.Value != "" {
if err := ParseJID(attr.Value, &sa.JID); err != nil {
return err
}
} else if attr.Name.Local == "to" {
if attr.Value != sa.Domain {
return ErrNotForThisDomainHead
}
} else if attr.Name.Local == "xmlns" {
sa.Xmlns = attr.Value
} else if attr.Name.Local == "version" {
sa.Version = attr.Value
} else if attr.Name.Local == "id" && attr.Value != "" {
sa.ID = attr.Value
} else if attr.Name.Local == "xml:lang" {
sa.XmlLang = attr.Value
}
}
return nil
}
func (sa *PartAttr) ParseToClient(elem xml.StartElement) error {
isStream := elem.Name.Local == "stream" && elem.Name.Space == NSStream
sa.OpenTag = elem.Name.Local == "open" && elem.Name.Space == NSFraming
if !isStream && !sa.OpenTag {
return ErrNotHeaderStart
}
for _, attr := range elem.Attr {
if attr.Name.Local == "to" && attr.Value != "" {
var jid JID
if err := ParseJID(attr.Value, &jid); err != nil {
return err
}
if !jid.Equal(sa.JID) {
return ErrNotForThisDomainHead
}
} else if attr.Name.Local == "from" {
sa.Domain = attr.Value
} else if attr.Name.Local == "version" {
sa.Version = attr.Value
} else if attr.Name.Local == "id" && attr.Value != "" {
sa.ID = attr.Value
}
}
return nil
}
type XPart struct {
channel Channel
features []Feature
logger Logger
conn Conn
attr PartAttr
elemRunner
}
func NewXPart(conn Conn, domain string, logger Logger) *XPart {
channel := NewXChannel(conn, true)
return &XPart{
channel: channel,
features: []Feature{},
logger: logger,
conn: conn,
attr: PartAttr{Domain: domain, ID: uuid.New().String()},
elemRunner: ElemRunner(channel),
}
}
func (part *XPart) ID() string {
return part.attr.ID
}
func (part *XPart) Conn() Conn {
return part.conn
}
func (part *XPart) Channel() Channel {
return part.channel
}
func (part *XPart) WithFeature(f Feature) {
part.features = append(part.features, f)
}
func (part *XPart) Run() chan error {
part.logger.Printf(LogInfo, "part instance [%s] start running", part.attr.ID)
return part.elemRunner.Run(part)
}
func (part *XPart) Attr() *PartAttr {
return &part.attr
}
// +---------------------+
// | 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 (part *XPart) handleFeatures(header xml.StartElement) error {
for {
if err := part.Attr().ParseToServer(header); err != nil {
return err
}
if err := part.Channel().Open(part.Attr()); err != nil {
return err
}
features, hasMandatory := part.unresolvedFeatures()
if !hasMandatory {
return part.handleUnmandatoryFeatures(features)
}
elems := []stravaganza.Element{}
runner := ElemRunner(part.Channel())
runner.SetHandleLimit(1)
for _, f := range features {
runner.WithElemHandler(f)
elems = append(elems, f.Elem())
}
if err := part.notifyFeatures(elems...); err != nil {
return err
}
errChan := runner.Run(part)
if err := <-errChan; err != nil {
return err
}
if err := part.Channel().WaitHeader(&header); err != nil {
return err
}
}
}
func (part *XPart) handleUnmandatoryFeatures(features []Feature) error {
elems := []stravaganza.Element{}
for _, f := range features {
elems = append(elems, f.Elem())
part.WithElemHandler(f)
}
part.notifyFeatures(elems...)
return nil
}
func (part *XPart) unresolvedFeatures() (features []Feature, hasMandatory bool) {
for _, f := range part.features {
if f.Handled() {
continue
}
if f.Mandatory() {
hasMandatory = true
}
features = append(features, f)
}
return
}
func (part *XPart) notifyFeatures(elems ...stravaganza.Element) error {
return part.Channel().SendElement(stravaganza.NewBuilder("features").WithChildren(elems...).Build())
}
func (part *XPart) Logger() Logger {
return part.logger
}
func (part *XPart) Stop() {
part.Quit()
}
func (part *XPart) OnCloseToken() {}
func (part *XPart) OnOpenHeader(header xml.StartElement) error {
return part.handleFeatures(header)
}
func (part *XPart) OnWhiteSpace([]byte) {}