-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_compress_feature.go
51 lines (44 loc) · 1.24 KB
/
client_compress_feature.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
package xmppcore
import (
"github.com/jackal-xmpp/stravaganza/v2"
)
type clientCompressFeature struct {
supported map[string]BuildCompressor
IDAble
}
func ClientCompressFeature() clientCompressFeature {
return clientCompressFeature{supported: make(map[string]BuildCompressor), IDAble: CreateIDAble()}
}
func (ccf *clientCompressFeature) Support(name string, b BuildCompressor) {
ccf.supported[name] = b
}
func (ccf clientCompressFeature) Match(elem stravaganza.Element) bool {
return elem.Name() == "compression"
}
func (ccf clientCompressFeature) Handle(elem stravaganza.Element, part Part) (catched bool, err error) {
if !ccf.Match(elem) {
return false, nil
}
catched = true
methods := elem.AllChildren()
var selected string
for _, m := range methods {
if _, o := ccf.supported[m.Text()]; o {
selected = m.Text()
break
}
}
compress := stravaganza.NewBuilder("compress").
WithAttribute("xmlns", NSCompress).
WithChild(stravaganza.NewBuilder("method").WithText(selected).Build()).Build()
if err = part.Channel().SendElement(compress); err != nil {
return
}
if err = part.Channel().NextElement(&elem); err != nil {
return
}
if elem.Name() == "compressed" {
part.Conn().StartCompress(ccf.supported[selected])
}
return
}