2018-02-13 20:05:18 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp"
|
2018-02-13 20:05:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultChannelSize = 30
|
|
|
|
|
|
|
|
func (client *Client) Start() error {
|
2018-02-14 18:49:26 +01:00
|
|
|
client.iq = make(chan *xmpp.IQClient, DefaultChannelSize)
|
|
|
|
client.presence = make(chan *xmpp.PresenceClient, DefaultChannelSize)
|
|
|
|
client.mesage = make(chan *xmpp.MessageClient, DefaultChannelSize)
|
|
|
|
client.reply = make(map[string]chan *xmpp.IQClient)
|
2018-02-13 20:05:18 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
element, err := client.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
errMSG := &xmpp.StreamError{}
|
2018-02-13 20:05:18 +01:00
|
|
|
err = client.Decode(errMSG, element)
|
|
|
|
if err == nil {
|
2018-02-14 18:49:26 +01:00
|
|
|
return fmt.Errorf("recv stream error: %s: %s -> %s", errMSG.Text, xmpp.XMLChildrenString(errMSG.StreamErrorGroup), xmpp.XMLChildrenString(errMSG.Other))
|
2018-02-13 20:05:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
iq := &xmpp.IQClient{}
|
2018-02-13 20:05:18 +01:00
|
|
|
err = client.Decode(iq, element)
|
|
|
|
if err == nil {
|
|
|
|
if iq.Ping != nil {
|
|
|
|
client.Logging.Debug("answer ping")
|
2018-02-14 18:49:26 +01:00
|
|
|
iq.Type = xmpp.IQTypeResult
|
2018-02-13 20:05:18 +01:00
|
|
|
iq.To = iq.From
|
|
|
|
iq.From = client.JID
|
|
|
|
client.Send(iq)
|
|
|
|
} else {
|
|
|
|
if client.skipError && iq.Error != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch, ok := client.reply[iq.ID]; ok {
|
|
|
|
delete(client.reply, iq.ID)
|
|
|
|
ch <- iq
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
client.iq <- iq
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
pres := &xmpp.PresenceClient{}
|
2018-02-13 20:05:18 +01:00
|
|
|
err = client.Decode(pres, element)
|
|
|
|
if err == nil {
|
|
|
|
if client.skipError && pres.Error != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
client.presence <- pres
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
msg := &xmpp.MessageClient{}
|
2018-02-13 20:05:18 +01:00
|
|
|
err = client.Decode(msg, element)
|
|
|
|
if err == nil {
|
|
|
|
if client.skipError && msg.Error != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
client.mesage <- msg
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
client.Logging.Warnf("unsupport xml recv: %v", element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
func (client *Client) SendRecv(iq *xmpp.IQClient) *xmpp.IQClient {
|
2018-02-13 20:05:18 +01:00
|
|
|
if iq.ID == "" {
|
2018-02-14 18:49:26 +01:00
|
|
|
iq.ID = xmpp.CreateCookieString()
|
2018-02-13 20:05:18 +01:00
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
ch := make(chan *xmpp.IQClient, 1)
|
2018-02-13 20:05:18 +01:00
|
|
|
client.reply[iq.ID] = ch
|
|
|
|
client.Send(iq)
|
|
|
|
defer close(ch)
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
func (client *Client) RecvIQ() *xmpp.IQClient {
|
2018-02-13 20:05:18 +01:00
|
|
|
return <-client.iq
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
func (client *Client) RecvPresence() *xmpp.PresenceClient {
|
2018-02-13 20:05:18 +01:00
|
|
|
return <-client.presence
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
func (client *Client) RecvMessage() *xmpp.MessageClient {
|
2018-02-13 20:05:18 +01:00
|
|
|
return <-client.mesage
|
|
|
|
}
|