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/jid.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/jid.go')
-rw-r--r-- | xmpp/jid.go | 67 |
1 files changed, 25 insertions, 42 deletions
diff --git a/xmpp/jid.go b/xmpp/jid.go index fd0d7ae..9580ad5 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -41,62 +41,45 @@ func username(jid string) string { return "" } -func hasBind(e []xml.Token) bool { - bind := xml.Name{`urn:ietf:params:xml:ns:xmpp-bind`, `bind`} - - for _, v := range e { - switch s := v.(type) { - case xml.StartElement: - if s.Name == bind { - return true - } - } - } - - return false +type bindRequest struct { + Bind struct { + Xmlns string `xml:"xmlns,attr"` + Resource struct { + Content string `xml:",chardata"` + } `xml:"resource"` + } `xml:"bind"` } func (s *session) sendBind() { + s.resourceReq = fmt.Sprintf("%016x", rand.Uint64()) - iqStart := xml.StartElement{ + start := xml.StartElement{ xml.Name{"jabber:client", "iq"}, []xml.Attr{ xml.Attr{xml.Name{"", "id"}, s.resourceReq}, xml.Attr{xml.Name{"", "type"}, "set"}, }, } - iqEnd := iqStart.End() - bindStart := xml.StartElement{ - xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"}, - []xml.Attr{}, - } - bindEnd := bindStart.End() + inner := bindRequest{} + inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind" + inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) - resourceStart := xml.StartElement{ - xml.Name{"", "resource"}, - []xml.Attr{}, - } - resourceEnd := resourceStart.End() - - name := xml.CharData("limox-" + fmt.Sprintf("%08x", rand.Uint32())) - - tokens := [...]xml.Token{ - iqStart, - bindStart, - resourceStart, - name, - resourceEnd, - bindEnd, - iqEnd, + err := s.tx.EncodeElement(inner, start) + if err != nil { + log.Println("Could not encode ressource binding!") } +} - for _, v := range tokens { - err := s.ed.encodeToken(v) - if err != nil { - log.Println("Could not encode ressource binding!") - return - } +type iqResponse struct { + Jid string `xml:"urn:ietf:params:xml:ns:xmpp-bind bind>jid"` +} + +func handleIqResponse(s *session, i iqResponse) { + if i.Jid != "" { + s.jid = i.Jid + s.sendPresence() + return } } |