diff options
author | xengineering <me@xengineering.eu> | 2023-07-04 22:10:55 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-04 22:15:17 +0200 |
commit | 48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch) | |
tree | a317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/streams.go | |
parent | 5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff) | |
parent | d9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff) | |
download | limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip |
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from
the token and group them as a []xml.Token slice for further processing.
While this is still possible, receiving aswell as sending has switched
to define structs with XML tags which can be marshalled and unmarshalled
with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement()
functions.
Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal
and https://pkg.go.dev/encoding/xml#Marshal.
This merge reduces with its changes the number of code lines in the XMPP
implementation by around 41 percent!
Diffstat (limited to 'xmpp/streams.go')
-rw-r--r-- | xmpp/streams.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/xmpp/streams.go b/xmpp/streams.go new file mode 100644 index 0000000..9f6ffe8 --- /dev/null +++ b/xmpp/streams.go @@ -0,0 +1,64 @@ +package xmpp + +import ( + "encoding/xml" + "log" +) + +type streamFeatures struct { + SaslMechanisms []string `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms>mechanism"` + Bind *bool `xml:"urn:ietf:params:xml:ns:xmpp-bind bind,omitempty"` +} + +func handleStreamFeatures(s *session, f streamFeatures) { + if len(f.SaslMechanisms) > 0 { + for _, v := range f.SaslMechanisms { + if v == "PLAIN" { + s.sasl() + return + } + } + log.Println("No compatible SASL mechanism found!") + return + } + + if f.Bind != nil { + s.sendBind() + return + } +} + +func openStream(e *xml.Encoder, jid string) { + start := xml.StartElement{ + xml.Name{"jabber:client", "stream:stream"}, + []xml.Attr{ + xml.Attr{xml.Name{"", "from"}, jid}, + xml.Attr{xml.Name{"", "to"}, domainpart(jid)}, + xml.Attr{xml.Name{"", "version"}, "1.0"}, + xml.Attr{xml.Name{"", "xml:lang"}, "en"}, + xml.Attr{xml.Name{"", "xmlns:stream"}, "http://etherx.jabber.org/streams"}, + }, + } + + err := e.EncodeToken(start) + if err != nil { + log.Println("Could not encode stream start!") + } + err = e.Flush() + if err != nil { + log.Println("Could not flush after stream start!") + } +} + +func closeStream(e *xml.Encoder) { + end := xml.EndElement{xml.Name{"jabber:client", "stream:stream"}} + + err := e.EncodeToken(end) + if err != nil { + log.Println("Could not encode stream end!") + } + err = e.Flush() + if err != nil { + log.Println("Could not flush after stream end!") + } +} |