summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-04-18 20:57:32 +0200
committerxengineering <me@xengineering.eu>2023-05-03 21:21:36 +0200
commit7029ebc4b2a1394f3fe2150f12624b30ef7d6d2b (patch)
treef1fbb5f8b4775845e48a39d17d4723a0c0fdab67
parent3f5b78f173d500b84aac0032d5c5e47873a17843 (diff)
downloadlimox-7029ebc4b2a1394f3fe2150f12624b30ef7d6d2b.tar
limox-7029ebc4b2a1394f3fe2150f12624b30ef7d6d2b.tar.zst
limox-7029ebc4b2a1394f3fe2150f12624b30ef7d6d2b.zip
Implement XML encoding
The <stream> element is the first to encode and is added with this commit.
-rw-r--r--limox.go4
-rw-r--r--xmpp.go43
2 files changed, 44 insertions, 3 deletions
diff --git a/limox.go b/limox.go
index 3ca21b3..9b7c235 100644
--- a/limox.go
+++ b/limox.go
@@ -85,14 +85,14 @@ func (l *Limox) run() error {
func (l *Limox) buttonCallback() {
switch l.State {
case Disconnected:
- log.Println("Starting connection establishment")
+ log.Println("Starting connection establishment ...")
go l.xmpp(l.JidEditor.Text(), l.PwdEditor.Text())
l.State = Connecting
case Connecting:
log.Println("Aborted connection establishment")
l.State = Disconnected
case Connected:
- log.Println("Disconnected")
+ log.Println("Disconnecting ...")
l.GuiEvents <- Disconnect
l.State = Disconnected
}
diff --git a/xmpp.go b/xmpp.go
index abc098d..f1f0d22 100644
--- a/xmpp.go
+++ b/xmpp.go
@@ -1,9 +1,15 @@
package main
import (
- "log"
"crypto/tls"
"crypto/x509"
+ "encoding/xml"
+ "log"
+ "strings"
+)
+
+const (
+ StreamCloser = `</stream:stream>`
)
type XmppEvent uint8
@@ -32,6 +38,8 @@ func (l *Limox) xmpp(jid string, pwd string) {
}
l.XmppEvents <- XmppConnect
+ sendStreamStart(conn, jid)
+
var closing bool = false
for {
ev := <-l.GuiEvents
@@ -46,10 +54,43 @@ func (l *Limox) xmpp(jid string, pwd string) {
}
}
+ log.Print("C: " + StreamCloser)
+ conn.Write([]byte(StreamCloser))
conn.Close()
l.XmppEvents <- XmppDisconnect
}
+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",
+ }
+
+ serialized, err := xml.Marshal(data)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ str := strings.TrimSuffix(string(serialized), StreamCloser)
+ log.Print("C: " + str)
+
+ t.Write([]byte(serialized))
+}
+
// 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 {