sum7
/
yaja
Archived
1
0
Fork 0

add prototype of admin management by bot

This commit is contained in:
Martin/Geno 2018-02-11 23:58:03 +01:00
parent 3d250f04e6
commit bbb13ea537
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 59 additions and 7 deletions

View File

@ -5,5 +5,5 @@ import "dev.sum7.eu/genofire/yaja/model"
type Account struct { type Account struct {
JID *model.JID `json:"jid"` JID *model.JID `json:"jid"`
Password string `json:"password"` Password string `json:"password"`
Admins []*model.JID `json:"admins"` Admins map[string]interface{} `json:"admins"`
} }

View File

@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/messages" "dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model"
) )
func (t *Tester) StartBot(status *Status) { func (t *Tester) StartBot(status *Status) {
@ -99,12 +100,17 @@ func (t *Tester) StartBot(status *Status) {
case "ping": case "ping":
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "pong"}) status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "pong"})
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"})
}
case "disconnect": case "disconnect":
first := true first := true
allAdmins := "" allAdmins := ""
isAdmin := false isAdmin := false
for _, jid := range append(t.Admins, status.account.Admins...) { for _, jid := range botAllowed(t.Admins, status.account.Admins) {
if first { if first {
first = false first = false
allAdmins += jid.Bare() allAdmins += jid.Bare()
@ -135,3 +141,49 @@ func (t *Tester) StartBot(status *Status) {
} }
} }
} }
func botAllowed(list []*model.JID, toConvert map[string]interface{}) []*model.JID {
alist := list
for jid, _ := range toConvert {
alist = append(alist, model.NewJID(jid))
}
return alist
}
func botAdmin(cmd []string, log *log.Entry, status *Status, from *model.JID, allowed []*model.JID) {
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})
}

View File

@ -39,8 +39,8 @@ func (s *Status) Disconnect(reason string) {
msg := &messages.MessageClient{ msg := &messages.MessageClient{
Body: fmt.Sprintf("you recieve a notify that '%s' disconnect: %s", s.JID.Full(), reason), Body: fmt.Sprintf("you recieve a notify that '%s' disconnect: %s", s.JID.Full(), reason),
} }
for _, jid := range s.account.Admins { for jid, _ := range s.account.Admins {
msg.To = jid msg.To = model.NewJID(jid)
if err := s.backupClient.Send(msg); err != nil { if err := s.backupClient.Send(msg); err != nil {
s.client.Send(msg) s.client.Send(msg)
} }