diff options
author | xengineering <me@xengineering.eu> | 2023-06-30 14:32:58 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-06-30 15:30:27 +0200 |
commit | 62a17dd6584009cad58fceee6b1ada74edf1d38f (patch) | |
tree | b4c6c833179ff92662efbd352dc66fc9884c8ca0 | |
parent | dd1cb9c23392d4d43198d60879fecf61fe4503b7 (diff) | |
download | limox-62a17dd6584009cad58fceee6b1ada74edf1d38f.tar limox-62a17dd6584009cad58fceee6b1ada74edf1d38f.tar.zst limox-62a17dd6584009cad58fceee6b1ada74edf1d38f.zip |
Implement resource binding request
This is the first step of resource binding which is a mandatory part of
establishing an XMPP connection.
-rw-r--r-- | xmpp/jid.go | 46 | ||||
-rw-r--r-- | xmpp/stream_pair.go | 2 |
2 files changed, 47 insertions, 1 deletions
diff --git a/xmpp/jid.go b/xmpp/jid.go index d7107d4..138744c 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -2,6 +2,9 @@ package xmpp import ( "encoding/xml" + "log" + "fmt" + "math/rand" ) // domainpart extracts the domain name from a JID / XMPP address. See @@ -52,3 +55,46 @@ func hasBind(e []xml.Token) bool { return false } + +func (s *session) sendBind() { + iqStart := xml.StartElement{ + xml.Name{"jabber:client", "iq"}, + []xml.Attr{ + xml.Attr{xml.Name{"", "id"}, fmt.Sprintf("%016x", rand.Uint64())}, + 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() + + 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, + } + + for _, v := range tokens { + err := s.ed.encodeToken(v) + if err != nil { + log.Println("Could not encode ressource binding!") + return + } + } +} diff --git a/xmpp/stream_pair.go b/xmpp/stream_pair.go index 9be880f..4690f57 100644 --- a/xmpp/stream_pair.go +++ b/xmpp/stream_pair.go @@ -99,7 +99,7 @@ func streamFeaturesHandler(s *session, e []xml.Token) { } if hasBind(e) { - log.Println("Stream supports ressource binding") + s.sendBind() return } |