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"
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp"
|
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp/base"
|
2018-02-10 13:34:42 +01:00
|
|
|
)
|
|
|
|
|
2018-02-22 18:13:17 +01:00
|
|
|
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{
|
2018-02-22 18:13:17 +01:00
|
|
|
"log": "bot",
|
|
|
|
"jid": status.client.JID.Full(),
|
2018-02-11 22:03:58 +01:00
|
|
|
})
|
2018-02-22 18:13:17 +01:00
|
|
|
go func(status *Status) {
|
|
|
|
if err := status.client.Start(); err != nil {
|
|
|
|
status.Disconnect(err.Error())
|
|
|
|
} else {
|
|
|
|
status.Disconnect("safe closed")
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
2018-02-22 18:13:17 +01:00
|
|
|
}(status)
|
|
|
|
logCTX.Info("start bot")
|
|
|
|
defer logCTX.Info("quit bot")
|
|
|
|
for {
|
|
|
|
element, more := status.client.Recv()
|
|
|
|
if !more {
|
|
|
|
logCTX.Info("could not recv msg, closed")
|
2018-02-11 11:15:11 +01:00
|
|
|
return
|
|
|
|
}
|
2018-02-22 18:13:17 +01:00
|
|
|
logCTX.Debugf("recv msg %v", element)
|
2018-02-11 11:15:11 +01:00
|
|
|
|
2018-02-22 18:13:17 +01:00
|
|
|
switch element.(type) {
|
|
|
|
case *xmpp.PresenceClient:
|
|
|
|
pres := element.(*xmpp.PresenceClient)
|
2018-02-11 14:05:01 +01:00
|
|
|
sender := pres.From
|
|
|
|
logPres := logCTX.WithField("from", sender.Full())
|
2018-02-22 18:13:17 +01:00
|
|
|
switch pres.Type {
|
|
|
|
case xmpp.PresenceTypeSubscribe:
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Debugf("recv presence subscribe")
|
2018-02-14 18:49:26 +01:00
|
|
|
pres.Type = xmpp.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")
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
pres.Type = xmpp.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")
|
2018-02-22 18:13:17 +01:00
|
|
|
case xmpp.PresenceTypeSubscribed:
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence accepted subscribe")
|
2018-02-22 18:13:17 +01:00
|
|
|
case xmpp.PresenceTypeUnsubscribe:
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence remove subscribe")
|
2018-02-22 18:13:17 +01:00
|
|
|
case xmpp.PresenceTypeUnsubscribed:
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Info("recv presence removed subscribe")
|
2018-02-22 18:13:17 +01:00
|
|
|
case xmpp.PresenceTypeUnavailable:
|
2018-02-11 19:35:32 +01:00
|
|
|
logPres.Debug("recv presence unavailable")
|
2018-02-22 18:13:17 +01:00
|
|
|
default:
|
2018-02-14 18:49:26 +01:00
|
|
|
logCTX.Warnf("recv presence unsupported: %s -> %s", pres.Type, xmpp.XMLChildrenString(pres))
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
2018-02-22 18:13:17 +01:00
|
|
|
case *xmpp.MessageClient:
|
|
|
|
msg := element.(*xmpp.MessageClient)
|
|
|
|
logMSG := logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body)
|
|
|
|
msgText := strings.SplitN(msg.Body, " ", 2)
|
|
|
|
switch msgText[0] {
|
|
|
|
|
|
|
|
case "ping":
|
|
|
|
status.client.Send(xmpp.MessageClient{Type: msg.Type, To: msg.From, Body: "pong"})
|
|
|
|
logMSG.Info("answer ping")
|
|
|
|
|
|
|
|
case "admin":
|
|
|
|
if len(msgText) == 2 {
|
|
|
|
botAdmin(strings.SplitN(msgText[1], " ", 2), logMSG, status, msg.From, botAllowed(t.Admins, status.account.Admins))
|
2018-02-11 22:03:58 +01:00
|
|
|
} else {
|
2018-02-22 18:13:17 +01:00
|
|
|
status.client.Send(xmpp.MessageClient{Type: msg.Type, To: msg.From, Body: "list, add JID-BARE, del JID-BARE"})
|
|
|
|
logMSG.Info("answer admin help")
|
2018-02-11 22:03:58 +01:00
|
|
|
}
|
2018-02-22 18:13:17 +01:00
|
|
|
case "disconnect":
|
|
|
|
first := true
|
|
|
|
allAdmins := ""
|
|
|
|
isAdmin := false
|
|
|
|
fromBare := msg.From
|
|
|
|
for _, jid := range botAllowed(t.Admins, status.account.Admins) {
|
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
allAdmins += ", "
|
|
|
|
}
|
|
|
|
allAdmins += jid.Bare().String()
|
|
|
|
if jid.Bare().IsEqual(fromBare) {
|
|
|
|
isAdmin = true
|
|
|
|
status.client.Send(xmpp.MessageClient{Type: msg.Type, To: jid, Body: "last message, disconnect requested by " + fromBare.String()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isAdmin {
|
|
|
|
status.Disconnect(fmt.Sprintf("disconnect by admin '%s'", fromBare.String()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
status.client.Send(xmpp.MessageClient{Type: msg.Type, To: msg.From, Body: "not allowed, ask " + allAdmins})
|
|
|
|
logMSG.Info("answer disconnect not allowed")
|
2018-02-11 22:03:58 +01:00
|
|
|
|
2018-02-22 18:13:17 +01:00
|
|
|
case "checkmsg":
|
|
|
|
if len(msgText) == 2 {
|
|
|
|
t.updateConnectionStatus(msg.From, status.client.JID, msgText[1])
|
|
|
|
} else {
|
|
|
|
logMSG.Debug("undetect")
|
2018-02-11 22:03:58 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 18:13:17 +01:00
|
|
|
default:
|
|
|
|
logMSG.Debug("undetect")
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
default:
|
2018-02-22 18:13:17 +01:00
|
|
|
logCTX.Debug("unhandle")
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
func botAllowed(list []*xmppbase.JID, toConvert map[string]interface{}) []*xmppbase.JID {
|
2018-02-11 23:58:03 +01:00
|
|
|
alist := list
|
2018-02-14 10:46:43 +01:00
|
|
|
for jid := range toConvert {
|
2018-02-14 18:49:26 +01:00
|
|
|
alist = append(alist, xmppbase.NewJID(jid))
|
2018-02-11 23:58:03 +01:00
|
|
|
}
|
|
|
|
return alist
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
func botAdmin(cmd []string, log *log.Entry, status *Status, from *xmppbase.JID, allowed []*xmppbase.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 {
|
2018-02-14 18:49:26 +01:00
|
|
|
msg = "unknown command"
|
2018-02-11 23:58:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(cmd) == 1 && cmd[0] == "list" {
|
2018-02-14 10:46:43 +01:00
|
|
|
for jid := range status.account.Admins {
|
2018-02-11 23:58:03 +01:00
|
|
|
if msg == "" {
|
|
|
|
msg += "admins are: " + jid
|
|
|
|
} else {
|
|
|
|
msg += ", " + jid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-14 18:49:26 +01:00
|
|
|
msg = "unknown command"
|
2018-02-11 23:58:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
status.client.Send(xmpp.MessageClient{Type: xmpp.MessageTypeChat, To: from, Body: msg})
|
2018-02-22 18:13:17 +01:00
|
|
|
log.Infof("admin[%s]: %s", from.String(), msg)
|
2018-02-11 23:58:03 +01:00
|
|
|
}
|