summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-05-18 20:51:16 +0200
committerxengineering <me@xengineering.eu>2023-05-18 20:51:16 +0200
commit05f50be52b03c7e84ba47d3b5349321920748f8b (patch)
tree9f0b2a0734849b70d53a5276cae9b2954ed7c6a7
parent8fce634716a998721cc5fb4663a7c0379c6df633 (diff)
downloadlimox-05f50be52b03c7e84ba47d3b5349321920748f8b.tar
limox-05f50be52b03c7e84ba47d3b5349321920748f8b.tar.zst
limox-05f50be52b03c7e84ba47d3b5349321920748f8b.zip
Add xmpp/encoder.go
This simplifies the double-encoding to the TCP connection and the debug console.
-rw-r--r--xmpp/encoder.go79
-rw-r--r--xmpp/xmpp.go54
2 files changed, 89 insertions, 44 deletions
diff --git a/xmpp/encoder.go b/xmpp/encoder.go
new file mode 100644
index 0000000..434ca68
--- /dev/null
+++ b/xmpp/encoder.go
@@ -0,0 +1,79 @@
+package xmpp
+
+import (
+ "crypto/tls"
+ "encoding/xml"
+ "os"
+)
+
+type encoder struct {
+ tcp *xml.Encoder
+ debug *xml.Encoder
+}
+
+func newEncoder(conn *tls.Conn) encoder {
+ e := encoder{
+ xml.NewEncoder(conn),
+ xml.NewEncoder(os.Stdout),
+ }
+ e.tcp.Indent("", "")
+ e.debug.Indent("C: ", " ")
+
+ return e
+}
+
+func (e *encoder) encodeNow(t xml.Token) error {
+ err := e.encode(t)
+ if err != nil {
+ return err
+ }
+
+ err = e.flush()
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (e *encoder) encode(t xml.Token) error {
+ err := e.tcp.EncodeToken(t)
+ if err != nil {
+ return err
+ }
+
+ err = e.debug.EncodeToken(t)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (e *encoder) flush() error {
+ err := e.tcp.Flush()
+ if err != nil {
+ return err
+ }
+
+ err = e.debug.Flush()
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (e *encoder) Close() error {
+ err := e.tcp.Close()
+ if err != nil {
+ return err
+ }
+
+ err = e.debug.Close()
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go
index 69fe48d..4412a96 100644
--- a/xmpp/xmpp.go
+++ b/xmpp/xmpp.go
@@ -28,13 +28,14 @@ func Run(ch chan Event, jid string, pwd string) {
go decoder.run()
defer decoder.stop()
- enc := xml.NewEncoder(conn)
+ enc := newEncoder(conn)
defer enc.Close()
+
dbg := xml.NewEncoder(os.Stdout)
defer dbg.Close()
- end := sendStreamStart(enc, dbg, jid)
- defer sendStreamEnd(enc, dbg, end)
+ end := sendStreamStart(&enc, jid)
+ defer sendStreamEnd(&enc, end)
ch <- ConnectEvent
defer func() { ch <- DisconnectEvent }()
@@ -67,7 +68,7 @@ func setupConn(jid string) (*tls.Conn, error) {
return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots})
}
-func sendStreamStart(enc *xml.Encoder, dbg *xml.Encoder, jid string) xml.EndElement {
+func sendStreamStart(enc *encoder, jid string) xml.EndElement {
start := xml.StartElement{
xml.Name{"jabber:client", "stream:stream"},
[]xml.Attr{
@@ -79,52 +80,17 @@ func sendStreamStart(enc *xml.Encoder, dbg *xml.Encoder, jid string) xml.EndElem
},
}
- err := enc.EncodeToken(start)
- if err != nil {
- log.Println("Could not encode stream start to TCP channel!")
- }
-
- err = enc.Flush()
- if err != nil {
- log.Println("Could not flush TCP channel XML encoder!")
- }
-
- dbg.Indent("C: ", " ")
-
- err = dbg.EncodeToken(start)
- if err != nil {
- log.Println("Could not encode stream start to debug output!")
- }
-
- err = dbg.Flush()
+ err := enc.encodeNow(start)
if err != nil {
- log.Println("Could not flush debug XML encoder!")
+ log.Println("Could not encode stream start!")
}
return start.End()
}
-func sendStreamEnd(enc *xml.Encoder, dbg *xml.Encoder, end xml.EndElement) {
-
- err := enc.EncodeToken(end)
- if err != nil {
- log.Println("Could not encode stream end to TCP channel!")
- }
-
- err = enc.Flush()
- if err != nil {
- log.Println("Could not flush TCP channel XML encoder!")
- }
-
- dbg.Indent("C: ", " ")
-
- err = dbg.EncodeToken(end)
- if err != nil {
- log.Println("Could not encode stream end to debug output!")
- }
-
- err = dbg.Flush()
+func sendStreamEnd(enc *encoder, end xml.EndElement) {
+ err := enc.encodeNow(end)
if err != nil {
- log.Println("Could not flush debug XML encoder!")
+ log.Println("Could not encode stream end!")
}
}