2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *Tester) StartBot(status *Status) {
|
|
|
|
for {
|
|
|
|
logCTX := log.WithField("jid", status.client.JID.Full())
|
|
|
|
|
|
|
|
element, err := status.client.Read()
|
|
|
|
if err != nil {
|
|
|
|
logCTX.Errorf("read client %s", err)
|
|
|
|
status.client.Close()
|
|
|
|
status.Login = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
errMSG := &messages.StreamError{}
|
|
|
|
err = status.client.In.DecodeElement(errMSG, element)
|
|
|
|
if err == nil {
|
2018-02-11 19:35:32 +01:00
|
|
|
logCTX.Errorf("recv stream error: %s: %s", errMSG.Text, messages.XMLChildrenString(errMSG.Any))
|
2018-02-11 11:15:11 +01:00
|
|
|
status.client.Close()
|
|
|
|
status.Login = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
iq := &messages.IQClient{}
|
|
|
|
err = status.client.In.DecodeElement(iq, element)
|
|
|
|
if err == nil {
|
|
|
|
if iq.Ping != nil {
|
|
|
|
logCTX.Debug("answer ping")
|
|
|
|
iq.Type = messages.IQTypeResult
|
|
|
|
iq.To = iq.From
|
|
|
|
iq.From = status.client.JID
|
|
|
|
status.client.Out.Encode(iq)
|
|
|
|
} else {
|
2018-02-11 19:35:32 +01:00
|
|
|
logCTX.Warnf("recv iq unsupport: %s", messages.XMLChildrenString(iq))
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pres := &messages.PresenceClient{}
|
|
|
|
err = status.client.In.DecodeElement(pres, element)
|
|
|
|
if err == nil {
|
2018-02-11 14:05:01 +01:00
|
|
|
sender := pres.From
|
|
|
|
logPres := logCTX.WithField("from", sender.Full())
|
2018-02-10 13:34:42 +01:00
|
|
|
if pres.Type == messages.PresenceTypeSubscribe {
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Debugf("recv presence subscribe")
|
2018-02-10 13:34:42 +01:00
|
|
|
pres.Type = messages.PresenceTypeSubscribed
|
2018-02-11 14:05:01 +01:00
|
|
|
pres.To = sender
|
|
|
|
pres.From = nil
|
|
|
|
status.client.Out.Encode(pres)
|
|
|
|
logPres.Debugf("accept new subscribe")
|
|
|
|
|
|
|
|
pres.Type = messages.PresenceTypeSubscribe
|
2018-02-11 19:35:32 +01:00
|
|
|
pres.ID = ""
|
2018-02-11 14:05:01 +01:00
|
|
|
status.client.Out.Encode(pres)
|
|
|
|
logPres.Info("request also subscribe")
|
|
|
|
} else if pres.Type == messages.PresenceTypeSubscribed {
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence accepted subscribe")
|
2018-02-11 14:05:01 +01:00
|
|
|
} else if pres.Type == messages.PresenceTypeUnsubscribe {
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence remove subscribe")
|
2018-02-11 14:05:01 +01:00
|
|
|
} else if pres.Type == messages.PresenceTypeUnsubscribed {
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence removed subscribe")
|
|
|
|
} else if pres.Type == messages.PresenceTypeUnavailable {
|
|
|
|
logPres.Debug("recv presence unavailable")
|
2018-02-10 13:34:42 +01:00
|
|
|
} else {
|
2018-02-11 19:35:32 +01:00
|
|
|
logCTX.Warnf("recv presence unsupported: %s -> %s", pres.Type, messages.XMLChildrenString(pres))
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &messages.MessageClient{}
|
|
|
|
err = status.client.In.DecodeElement(msg, element)
|
|
|
|
if err != nil {
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX.Warnf("unsupport xml recv: %s <-> %v", err, element)
|
2018-02-10 13:34:42 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-02-11 11:15:11 +01:00
|
|
|
logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body)
|
2018-02-10 13:34:42 +01:00
|
|
|
if msg.Error != nil {
|
2018-02-11 19:35:32 +01:00
|
|
|
if msg.Error.Type == "auth" {
|
|
|
|
logCTX.Warnf("recv msg with error not auth")
|
|
|
|
status.Login = false
|
|
|
|
status.client.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logCTX.Debugf("recv msg with error %s[%s]: %s -> %s -> %s", msg.Error.Code, msg.Error.Type, msg.Error.Text, messages.XMLChildrenString(msg.Error.StanzaErrorGroup), messages.XMLChildrenString(msg.Error.Other))
|
2018-02-10 13:34:42 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
msgText := strings.SplitN(msg.Body, " ", 2)
|
|
|
|
switch msgText[0] {
|
|
|
|
|
|
|
|
case "ping":
|
|
|
|
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "pong"})
|
|
|
|
|
|
|
|
case "checkmsg":
|
|
|
|
if len(msgText) == 2 {
|
|
|
|
t.UpdateConnectionStatus(msg.From, status.client.JID, msgText[1])
|
|
|
|
} else {
|
|
|
|
logCTX.Debug("undetect")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
logCTX.Debug("undetect")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|