diff options
Diffstat (limited to 'xmpp/xmpp.go')
-rw-r--r-- | xmpp/xmpp.go | 54 |
1 files changed, 10 insertions, 44 deletions
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!") } } |