diff options
author | xengineering <me@xengineering.eu> | 2023-05-11 20:22:31 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-11 20:22:31 +0200 |
commit | 0dd1fb9f2158dec48c8dc55a078b3d79d475c9d9 (patch) | |
tree | 88580bbd6b31b6e83dee2ca02d5c2fa9576693a9 | |
parent | 8569dfec15368986773e67d8c1598c0e02b39084 (diff) | |
download | limox-0dd1fb9f2158dec48c8dc55a078b3d79d475c9d9.tar limox-0dd1fb9f2158dec48c8dc55a078b3d79d475c9d9.tar.zst limox-0dd1fb9f2158dec48c8dc55a078b3d79d475c9d9.zip |
Restructure xmpp()
-rw-r--r-- | xmpp.go | 22 |
1 files changed, 8 insertions, 14 deletions
@@ -1,11 +1,11 @@ package main import ( - "os" "crypto/tls" "crypto/x509" "encoding/xml" "log" + "os" ) type XmppEvent uint8 @@ -16,32 +16,34 @@ const ( ) func (l *Limox) xmpp(jid string, pwd string) { + defer func() { l.XmppEvents <- XmppDisconnect }() conn, err := setupConn(jid) if err != nil { l.XmppEvents <- err return } - - enc := xml.NewEncoder(conn) + defer conn.Close() receiver := make(chan xml.Token) termination := make(chan bool) go rxRoutine(conn, receiver, termination) + enc := xml.NewEncoder(conn) dbg := xml.NewEncoder(os.Stdout) end := sendStreamStart(enc, jid) + defer sendStreamEnd(enc, end) + l.XmppEvents <- XmppConnect - var closing bool = false for { select { case ev := <-l.GuiEvents: switch ev { case Disconnect: termination <- true - closing = true + return default: log.Fatalf("Unknown GuiEvent '%d'!\n", ev) } @@ -50,15 +52,7 @@ func (l *Limox) xmpp(jid string, pwd string) { dbg.EncodeToken(rx) dbg.Flush() } - - if closing { - break - } } - - sendStreamEnd(enc, end) - conn.Close() - l.XmppEvents <- XmppDisconnect } func setupConn(jid string) (*tls.Conn, error) { @@ -91,7 +85,7 @@ func rxRoutine(conn *tls.Conn, tokens chan xml.Token, terminator chan bool) { } } -func sendStreamStart(enc *xml.Encoder, jid string) (xml.EndElement) { +func sendStreamStart(enc *xml.Encoder, jid string) xml.EndElement { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, []xml.Attr{ |