diff options
Diffstat (limited to 'xmpp/jid.go')
-rw-r--r-- | xmpp/jid.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/xmpp/jid.go b/xmpp/jid.go index 90c1509..0446d19 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -1,5 +1,12 @@ package xmpp +import ( + "encoding/xml" + "fmt" + "log" + "math/rand" +) + // domainpart extracts the domain name from a JID / XMPP address. See // https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. func domainpart(jid string) string { @@ -33,3 +40,61 @@ 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 +} + +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 + } + } +} |