2018-02-10 13:34:42 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) ReadElement(p interface{}) error {
|
|
|
|
element, err := client.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var iq *messages.IQClient
|
|
|
|
iq, ok := p.(*messages.IQClient)
|
|
|
|
if !ok {
|
|
|
|
iq = &messages.IQClient{}
|
|
|
|
}
|
|
|
|
err = client.In.DecodeElement(iq, element)
|
|
|
|
if err == nil && iq.Ping != nil {
|
|
|
|
log.Println("answer ping")
|
|
|
|
iq.Type = messages.IQTypeResult
|
|
|
|
iq.To = iq.From
|
|
|
|
iq.From = client.JID
|
|
|
|
client.Out.Encode(iq)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return client.In.DecodeElement(p, element)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Send(p interface{}) error {
|
|
|
|
msg, ok := p.(*messages.MessageClient)
|
|
|
|
if ok {
|
|
|
|
msg.From = client.JID
|
2018-02-11 14:05:01 +01:00
|
|
|
if msg.ID == "" {
|
|
|
|
msg.ID = utils.CreateCookieString()
|
|
|
|
}
|
2018-02-10 13:34:42 +01:00
|
|
|
return client.Out.Encode(msg)
|
|
|
|
}
|
|
|
|
iq, ok := p.(*messages.IQClient)
|
|
|
|
if ok {
|
|
|
|
iq.From = client.JID
|
2018-02-11 14:05:01 +01:00
|
|
|
if iq.ID == "" {
|
|
|
|
iq.ID = utils.CreateCookieString()
|
|
|
|
}
|
2018-02-10 13:34:42 +01:00
|
|
|
return client.Out.Encode(iq)
|
|
|
|
}
|
|
|
|
pc, ok := p.(*messages.PresenceClient)
|
|
|
|
if ok {
|
|
|
|
pc.From = client.JID
|
2018-02-11 14:05:01 +01:00
|
|
|
if pc.ID == "" {
|
|
|
|
pc.ID = utils.CreateCookieString()
|
|
|
|
}
|
2018-02-10 13:34:42 +01:00
|
|
|
return client.Out.Encode(pc)
|
|
|
|
}
|
|
|
|
return client.Out.Encode(p)
|
|
|
|
}
|