From bbb13ea537b854281ca10eab7e6a0e6e08f72298 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 23:58:03 +0100 Subject: [PATCH] add prototype of admin management by bot --- daemon/tester/account.go | 6 ++--- daemon/tester/bot.go | 56 ++++++++++++++++++++++++++++++++++++++-- daemon/tester/status.go | 4 +-- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/daemon/tester/account.go b/daemon/tester/account.go index dd7d075..778ad87 100644 --- a/daemon/tester/account.go +++ b/daemon/tester/account.go @@ -3,7 +3,7 @@ package tester import "dev.sum7.eu/genofire/yaja/model" type Account struct { - JID *model.JID `json:"jid"` - Password string `json:"password"` - Admins []*model.JID `json:"admins"` + JID *model.JID `json:"jid"` + Password string `json:"password"` + Admins map[string]interface{} `json:"admins"` } diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index 8bb5f6d..2307c02 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "dev.sum7.eu/genofire/yaja/messages" + "dev.sum7.eu/genofire/yaja/model" ) func (t *Tester) StartBot(status *Status) { @@ -99,12 +100,17 @@ func (t *Tester) StartBot(status *Status) { case "ping": 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": first := true allAdmins := "" isAdmin := false - for _, jid := range append(t.Admins, status.account.Admins...) { + for _, jid := range botAllowed(t.Admins, status.account.Admins) { if first { first = false 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}) +} diff --git a/daemon/tester/status.go b/daemon/tester/status.go index e8a1a48..afdb313 100644 --- a/daemon/tester/status.go +++ b/daemon/tester/status.go @@ -39,8 +39,8 @@ func (s *Status) Disconnect(reason string) { msg := &messages.MessageClient{ Body: fmt.Sprintf("you recieve a notify that '%s' disconnect: %s", s.JID.Full(), reason), } - for _, jid := range s.account.Admins { - msg.To = jid + for jid, _ := range s.account.Admins { + msg.To = model.NewJID(jid) if err := s.backupClient.Send(msg); err != nil { s.client.Send(msg) }