2017-12-16 23:20:46 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-14 18:49:26 +01:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/model"
|
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp/base"
|
2017-12-16 23:20:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
Log *log.Entry
|
|
|
|
|
|
|
|
Conn net.Conn
|
|
|
|
Out *xml.Encoder
|
|
|
|
In *xml.Decoder
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
JID *xmppbase.JID
|
2017-12-16 23:20:46 +01:00
|
|
|
account *model.Account
|
|
|
|
|
|
|
|
Messages chan interface{}
|
|
|
|
close chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(conn net.Conn, level log.Level) *Client {
|
|
|
|
logger := log.New()
|
|
|
|
logger.SetLevel(level)
|
|
|
|
client := &Client{
|
2017-12-17 15:39:36 +01:00
|
|
|
Conn: conn,
|
|
|
|
Log: log.NewEntry(logger),
|
|
|
|
In: xml.NewDecoder(conn),
|
|
|
|
Out: xml.NewEncoder(conn),
|
2017-12-17 17:50:51 +01:00
|
|
|
Messages: make(chan interface{}),
|
2017-12-17 15:39:36 +01:00
|
|
|
close: make(chan interface{}),
|
2017-12-16 23:20:46 +01:00
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SetConnecting(conn net.Conn) {
|
|
|
|
client.Conn = conn
|
|
|
|
client.In = xml.NewDecoder(conn)
|
|
|
|
client.Out = xml.NewEncoder(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Read() (*xml.StartElement, error) {
|
|
|
|
for {
|
|
|
|
nextToken, err := client.In.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch nextToken.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
element := nextToken.(xml.StartElement)
|
|
|
|
return &element, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) OnClose() <-chan interface{} {
|
|
|
|
return client.close
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Close() {
|
|
|
|
client.close <- true
|
|
|
|
client.Conn.Close()
|
|
|
|
}
|