sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/client/comm.go

84 lines
1.9 KiB
Go
Raw Normal View History

2018-02-10 13:34:42 +01:00
package client
import (
"encoding/xml"
"dev.sum7.eu/genofire/yaja/xmpp"
2018-02-10 13:34:42 +01:00
)
func (client *Client) Read() (*xml.StartElement, error) {
for {
2018-02-11 22:03:58 +01:00
nextToken, err := client.in.Token()
2018-02-10 13:34:42 +01:00
if err != nil {
return nil, err
}
switch nextToken.(type) {
case xml.StartElement:
element := nextToken.(xml.StartElement)
client.Logging.Debug("recv xml: ", xmpp.XMLStartElementToString(&element))
2018-02-10 13:34:42 +01:00
return &element, nil
}
}
}
2018-02-11 22:03:58 +01:00
func (client *Client) Decode(p interface{}, element *xml.StartElement) error {
err := client.in.DecodeElement(p, element)
if err != nil {
client.Logging.Debugf("decode failed xml: %s to: %v", xmpp.XMLStartElementToString(element), p)
2018-02-11 22:03:58 +01:00
} else {
client.Logging.Debugf("decode xml: %s to: %v with children %s", xmpp.XMLStartElementToString(element), p, xmpp.XMLChildrenString(p))
2018-02-11 22:03:58 +01:00
}
return err
}
func (client *Client) ReadDecode(p interface{}) error {
2018-02-10 13:34:42 +01:00
element, err := client.Read()
if err != nil {
return err
}
var iq *xmpp.IQClient
iq, ok := p.(*xmpp.IQClient)
2018-02-10 13:34:42 +01:00
if !ok {
iq = &xmpp.IQClient{}
2018-02-10 13:34:42 +01:00
}
2018-02-11 22:03:58 +01:00
err = client.Decode(iq, element)
2018-02-10 13:34:42 +01:00
if err == nil && iq.Ping != nil {
2018-02-11 22:03:58 +01:00
client.Logging.Info("ReadElement: auto answer ping")
iq.Type = xmpp.IQTypeResult
2018-02-10 13:34:42 +01:00
iq.To = iq.From
iq.From = client.JID
2018-02-11 22:03:58 +01:00
client.Send(iq)
2018-02-10 13:34:42 +01:00
return nil
}
if ok {
return err
}
2018-02-11 22:03:58 +01:00
return client.Decode(p, element)
}
func (client *Client) encode(p interface{}) error {
err := client.out.Encode(p)
if err != nil {
client.Logging.Debugf("encode failed %v", p)
} else {
client.Logging.Debugf("encode %v with children %s", p, xmpp.XMLChildrenString(p))
2018-02-11 22:03:58 +01:00
}
return err
2018-02-10 13:34:42 +01:00
}
func (client *Client) Send(p interface{}) error {
msg, ok := p.(*xmpp.MessageClient)
2018-02-10 13:34:42 +01:00
if ok {
msg.From = client.JID
2018-02-11 22:03:58 +01:00
return client.encode(msg)
2018-02-10 13:34:42 +01:00
}
iq, ok := p.(*xmpp.IQClient)
2018-02-10 13:34:42 +01:00
if ok {
iq.From = client.JID
2018-02-11 22:03:58 +01:00
return client.encode(iq)
2018-02-10 13:34:42 +01:00
}
pc, ok := p.(*xmpp.PresenceClient)
2018-02-10 13:34:42 +01:00
if ok {
pc.From = client.JID
2018-02-11 22:03:58 +01:00
return client.encode(pc)
2018-02-10 13:34:42 +01:00
}
2018-02-11 22:03:58 +01:00
return client.encode(p)
2018-02-10 13:34:42 +01:00
}