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 { list := []rune(jid) for i, v := range list { if v == '/' { list = list[:i] break } } for i, v := range list { if v == '@' { list = list[i+1:] break } } return string(list) } func username(jid string) string { list := []rune(jid) for i, v := range list { if v == '@' { return string(list[:i]) } } 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()) start := xml.StartElement{ xml.Name{"jabber:client", "iq"}, []xml.Attr{ xml.Attr{xml.Name{"", "id"}, s.resourceReq}, xml.Attr{xml.Name{"", "type"}, "set"}, }, } inner := bindRequest{} inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind" inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) err := s.ed.tx.EncodeElement(inner, start) if err != nil { log.Println("Could not encode ressource binding!") } }