summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xmpp/xmpp.go50
1 files changed, 33 insertions, 17 deletions
diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go
index 1d00739..624090b 100644
--- a/xmpp/xmpp.go
+++ b/xmpp/xmpp.go
@@ -18,27 +18,54 @@ const (
type Conn struct {
ch chan Event
jid, pwd string
+ tcp *tls.Conn
}
func NewConn(ch chan Event, jid string, pwd string) *Conn {
- c := Conn{ch, jid, pwd}
+ c := Conn{
+ ch: ch,
+ jid: jid,
+ pwd: pwd}
return &c
}
+func (c *Conn) Connect() error {
+ var err error
+
+ domain := domainpart(c.jid)
+
+ roots, err := x509.SystemCertPool()
+ if err != nil {
+ log.Println(err)
+ return err
+ }
+
+ c.tcp, err = tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots})
+ if err != nil {
+ log.Println(err)
+ return err
+ }
+
+ return nil
+}
+
+func (c *Conn) Disconnect() {
+ c.tcp.Close()
+}
+
func (c *Conn) Run() {
- conn, err := setupConn(c.jid)
+ err := c.Connect()
if err != nil {
- log.Print(err)
return
}
- defer conn.Close()
+ defer c.Disconnect()
- decoder := newDecoder(conn)
+ decoder := newDecoder(c.tcp)
go decoder.run()
defer decoder.stop()
- enc := newEncoder(conn)
+ enc := newEncoder(c.tcp)
defer enc.Close()
tr := newTokenRouter(&enc)
@@ -68,17 +95,6 @@ func (c *Conn) Run() {
}
}
-func setupConn(jid string) (*tls.Conn, error) {
- domain := domainpart(jid)
-
- roots, err := x509.SystemCertPool()
- if err != nil {
- return nil, err
- }
-
- return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots})
-}
-
func sendStreamStart(enc *encoder, jid string) xml.EndElement {
start := xml.StartElement{
xml.Name{"jabber:client", "stream:stream"},