summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xmpp.go22
1 files changed, 8 insertions, 14 deletions
diff --git a/xmpp.go b/xmpp.go
index d7c8790..90a0355 100644
--- a/xmpp.go
+++ b/xmpp.go
@@ -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{