diff options
author | xengineering <me@xengineering.eu> | 2023-05-09 21:21:47 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-10 17:45:18 +0200 |
commit | a3f1bd19945491608bb3acc9f408e0cfbc84331d (patch) | |
tree | 0d94db5cf7b4795e2758a541ace94656666f69ea /xmpp.go | |
parent | a812aabfe1d96fbdaf5a40dd8fb6654d943a8380 (diff) | |
download | limox-a3f1bd19945491608bb3acc9f408e0cfbc84331d.tar limox-a3f1bd19945491608bb3acc9f408e0cfbc84331d.tar.zst limox-a3f1bd19945491608bb3acc9f408e0cfbc84331d.zip |
Implement stream start with xml.StartElement
The currently used method with struct marshalling and string cutting is
just a workaround.
Diffstat (limited to 'xmpp.go')
-rw-r--r-- | xmpp.go | 59 |
1 files changed, 27 insertions, 32 deletions
@@ -5,14 +5,9 @@ import ( "crypto/x509" "encoding/xml" "log" - "strings" "reflect" ) -const ( - StreamCloser = `</stream:stream>` -) - type XmppEvent uint8 const ( @@ -61,8 +56,7 @@ func (l *Limox) xmpp(jid string, pwd string) { log.Println("Done!") }() - sendStreamStart(conn, jid) - + end := sendStreamStart(conn, jid) l.XmppEvents <- XmppConnect var closing bool = false @@ -85,8 +79,7 @@ func (l *Limox) xmpp(jid string, pwd string) { } } - log.Print("C: " + StreamCloser) - conn.Write([]byte(StreamCloser)) + sendStreamEnd(conn, end) l.XmppEvents <- XmppDisconnect } @@ -106,35 +99,37 @@ func logToken(t xml.Token, isTx bool) { } } -type StreamInit struct { - XMLName xml.Name `xml:"stream:stream"` - From string `xml:"from,attr"` - To string `xml:"to,attr"` - Version string `xml:"version,attr"` - XmlLang string `xml:"xml:lang,attr"` - Xmlns string `xml:"xmlns,attr"` - XmlnsStream string `xml:"xmlns:stream,attr"` -} - -func sendStreamStart(t *tls.Conn, jid string) { - data := StreamInit{ - From: jid, - To: domainpart(jid), - Version: "1.0", - XmlLang: "en", - Xmlns: "jabber:client", - XmlnsStream: "http://etherx.jabber.org/streams", +func sendStreamStart(t *tls.Conn, jid string) (xml.EndElement) { + 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"}, + }, } - serialized, err := xml.Marshal(data) + e := xml.NewEncoder(t) + err := e.EncodeToken(start) if err != nil { - log.Fatal(err) + log.Fatal("Could not encode stream start!") } + e.Close() - str := strings.TrimSuffix(string(serialized), StreamCloser) - log.Print("C: " + str) + logToken(start, true) + return start.End() +} - t.Write([]byte(serialized)) +func sendStreamEnd(t *tls.Conn, end xml.EndElement) { + e := xml.NewEncoder(t) + err := e.EncodeToken(end) + if err != nil { + log.Fatal("Could not encode stream end!") + } + e.Close() + logToken(end, true) } // domainpart extracts the domain name from a JID / XMPP address. See |