package xmpp import ( "encoding/xml" "log" ) 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!") } } func iqHandler(s *session, e []xml.Token) { isResult := false idMatches := false result := xml.Attr{xml.Name{"", "type"}, "result"} id := xml.Attr{xml.Name{"", "id"}, s.resourceReq} switch start := e[0].(type) { case xml.StartElement: for _, v := range start.Attr { if v == result { isResult = true } if v == id { idMatches = true } } if isResult && idMatches { s.sendPresence() } } } type streamFeatures struct { Mechanisms []struct { Type string `xml:",chardata"` } `xml:"mechanisms>mechanism"` } func streamFeaturesHandler(s *xml.StartElement, d *xml.Decoder, c chan<- any) { e := streamFeatures{} err := d.DecodeElement(&e, s) if err != nil { log.Printf("Could not decode stream features: %v\n", err) } else { c <- e } }