2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
2018-02-11 23:28:21 +01:00
|
|
|
"fmt"
|
2018-02-10 13:34:42 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *Tester) StartBot(status *Status) {
|
2018-02-11 22:03:58 +01:00
|
|
|
logger := log.New()
|
|
|
|
logger.SetLevel(t.LoggingBots)
|
|
|
|
logCTX := logger.WithFields(log.Fields{
|
|
|
|
"type": "bot",
|
|
|
|
"jid": status.client.JID.Full(),
|
|
|
|
})
|
2018-02-10 13:34:42 +01:00
|
|
|
for {
|
|
|
|
|
|
|
|
element, err := status.client.Read()
|
|
|
|
if err != nil {
|
2018-02-11 23:28:21 +01:00
|
|
|
status.Disconnect(fmt.Sprintf("could not read any more data from socket: %s", err))
|
2018-02-10 13:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
errMSG := &messages.StreamError{}
|
2018-02-11 22:03:58 +01:00
|
|
|
err = status.client.Decode(errMSG, element)
|
2018-02-11 11:15:11 +01:00
|
|
|
if err == nil {
|
2018-02-12 07:05:39 +01:00
|
|
|
status.Disconnect(fmt.Sprintf("recv stream error: %s: %s -> %s", errMSG.Text, messages.XMLChildrenString(errMSG.StreamErrorGroup), messages.XMLChildrenString(errMSG.Other)))
|
2018-02-11 11:15:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
iq := &messages.IQClient{}
|
2018-02-11 22:03:58 +01:00
|
|
|
err = status.client.Decode(iq, element)
|
2018-02-10 13:34:42 +01:00
|
|
|
if err == nil {
|
|
|
|
if iq.Ping != nil {
|
|
|
|
logCTX.Debug("answer ping")
|
|
|
|
iq.Type = messages.IQTypeResult
|
|
|
|
iq.To = iq.From
|
|
|
|
iq.From = status.client.JID
|
2018-02-11 22:03:58 +01:00
|
|
|
status.client.Send(iq)
|
2018-02-10 13:34:42 +01:00
|
|
|
} 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{}
|
2018-02-11 22:03:58 +01:00
|
|
|
err = status.client.Decode(pres, element)
|
2018-02-10 13:34:42 +01:00
|
|
|
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
|
2018-02-11 22:03:58 +01:00
|
|
|
status.client.Send(pres)
|
2018-02-11 14:05:01 +01:00
|
|
|
logPres.Debugf("accept new subscribe")
|
|
|
|
|
|
|
|
pres.Type = messages.PresenceTypeSubscribe
|
2018-02-11 19:35:32 +01:00
|
|
|
pres.ID = ""
|
2018-02-11 22:03:58 +01:00
|
|
|
status.client.Send(pres)
|
2018-02-11 14:05:01 +01:00
|
|
|
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{}
|
2018-02-11 22:03:58 +01:00
|
|
|
err = status.client.Decode(msg, element)
|
2018-02-10 13:34:42 +01:00
|
|
|
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" {
|
2018-02-11 23:28:21 +01:00
|
|
|
status.Disconnect("recv msg with error not auth")
|
2018-02-11 19:35:32 +01:00
|
|
|
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"})
|
2018-02-11 23:58:03 +01:00
|
|
|
case "admin":
|
|
|
|
if len(msgText) == 2 {
|
|
|
|
botAdmin(strings.SplitN(msgText[1], " ", 2), logCTX, status, msg.From, botAllowed(t.Admins, status.account.Admins))
|
|
|
|
} else {
|
|
|
|
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "list, add JID-BARE, del JID-BARE"})
|
|
|
|
}
|
2018-02-11 22:03:58 +01:00
|
|
|
case "disconnect":
|
|
|
|
first := true
|
|
|
|
allAdmins := ""
|
|
|
|
isAdmin := false
|
2018-02-11 23:58:03 +01:00
|
|
|
for _, jid := range botAllowed(t.Admins, status.account.Admins) {
|
2018-02-11 22:03:58 +01:00
|
|
|
if first {
|
|
|
|
first = false
|
2018-02-11 22:43:42 +01:00
|
|
|
allAdmins += jid.Bare()
|
2018-02-11 22:03:58 +01:00
|
|
|
} else {
|
2018-02-11 22:43:42 +01:00
|
|
|
allAdmins += ", " + jid.Bare()
|
2018-02-11 22:03:58 +01:00
|
|
|
}
|
|
|
|
if jid.Bare() == msg.From.Bare() {
|
|
|
|
isAdmin = true
|
|
|
|
status.client.Send(messages.MessageClient{Type: msg.Type, To: jid, Body: "last message, disconnect requested by " + msg.From.Bare()})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isAdmin {
|
2018-02-11 23:28:21 +01:00
|
|
|
status.Disconnect(fmt.Sprintf("disconnect by admin '%s'", msg.From.Bare()))
|
2018-02-11 22:03:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "not allowed, ask " + allAdmins})
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
case "checkmsg":
|
|
|
|
if len(msgText) == 2 {
|
|
|
|
t.UpdateConnectionStatus(msg.From, status.client.JID, msgText[1])
|
|
|
|
} else {
|
|
|
|
logCTX.Debug("undetect")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
logCTX.Debug("undetect")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 20:05:18 +01:00
|
|
|
func botAllowed(list []*messages.JID, toConvert map[string]interface{}) []*messages.JID {
|
2018-02-11 23:58:03 +01:00
|
|
|
alist := list
|
|
|
|
for jid, _ := range toConvert {
|
2018-02-13 20:05:18 +01:00
|
|
|
alist = append(alist, messages.NewJID(jid))
|
2018-02-11 23:58:03 +01:00
|
|
|
}
|
|
|
|
return alist
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:05:18 +01:00
|
|
|
func botAdmin(cmd []string, log *log.Entry, status *Status, from *messages.JID, allowed []*messages.JID) {
|
2018-02-11 23:58:03 +01:00
|
|
|
msg := ""
|
|
|
|
if len(cmd) == 2 {
|
|
|
|
isAdmin := false
|
|
|
|
for _, jid := range allowed {
|
|
|
|
if jid.Bare() == from.Bare() {
|
|
|
|
isAdmin = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if status.account.Admins == nil {
|
|
|
|
status.account.Admins = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
if !isAdmin {
|
|
|
|
msg = "not allowed"
|
|
|
|
} else if cmd[0] == "add" {
|
|
|
|
status.account.Admins[cmd[1]] = true
|
|
|
|
msg = "ack"
|
|
|
|
} else if cmd[0] == "del" {
|
|
|
|
delete(status.account.Admins, cmd[1])
|
|
|
|
msg = "ack"
|
|
|
|
} else {
|
|
|
|
msg = "unkown command"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(cmd) == 1 && cmd[0] == "list" {
|
|
|
|
for jid, _ := range status.account.Admins {
|
|
|
|
if msg == "" {
|
|
|
|
msg += "admins are: " + jid
|
|
|
|
} else {
|
|
|
|
msg += ", " + jid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = "unkown command"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.client.Send(messages.MessageClient{Type: messages.MessageTypeChat, To: from, Body: msg})
|
|
|
|
}
|