Files
slacker-bot/slackerbot/internal/bot/manage.go
T

189 lines
5.1 KiB
Go

package bot
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"slackerbot/internal/commands"
"slackerbot/internal/model"
)
func mentions(users []string) string {
out := make([]string, len(users))
for i, u := range users {
out[i] = "<@" + u + ">"
}
return strings.Join(out, ", ")
}
func actionList(actions []model.Action) string {
if len(actions) == 0 {
return "nothing"
}
names := make([]string, len(actions))
for i, a := range actions {
names[i] = string(a)
}
return strings.Join(names, ", ")
}
// manageCommand handles permission changes. It returns false if the message
// isn't one of these commands, so it goes to the agent.
func (b *Bot) manageCommand(ctx context.Context, m incoming, verb, args string) bool {
_, kind := m.owner()
p := b.store.Permissions()
reply := func(text string) bool {
b.say(ctx, m.channel, m.threadTS, text)
return true
}
denied := func(global bool) bool {
who := "user managers"
if !global {
who = "user managers and people with *manage* permission here"
}
names := make([]string, len(p.Managers))
for i, u := range p.Managers {
names[i] = b.displayName(ctx, u)
}
return reply(fmt.Sprintf(":lock: Only %s can do that. User managers: %s.", who, strings.Join(names, ", ")))
}
update := func(change func(p *model.PermissionsConfig) error) error {
return b.store.UpdatePermissions(change)
}
switch verb {
case "grant", "revoke":
g, err := commands.ParseGrant(args)
if errors.Is(err, commands.ErrNotCommand) {
return false
}
if err != nil {
return reply(":warning: " + err.Error() + ". Try `grant @alice move scale` or `revoke @alice`.")
}
channel := m.channel
if g.ToDefault {
channel = ""
if !p.IsManager(m.user) {
return denied(true)
}
} else {
if kind == model.OwnerUser {
return reply("DMs don't have a super-user list. User managers can switch DM permissions with `dm <permission> on|off`.")
}
if !p.CanManage(channel, m.user) {
return denied(false)
}
}
if verb == "grant" && len(g.Actions) == 0 {
return reply(":warning: Say what to grant, like `grant @alice move scale` (or `all`, or `manage`).")
}
if slices.Contains(g.Actions, model.ActManage) && !g.ToDefault && !p.CanManage(channel, m.user) {
return denied(false)
}
err = update(func(p *model.PermissionsConfig) error {
if verb == "grant" {
p.Grant(channel, g.Users, g.Actions)
} else {
p.Revoke(channel, g.Users, g.Actions)
}
return nil
})
if err != nil {
return reply(":warning: " + err.Error())
}
where := "in this channel"
if g.ToDefault {
where = "in every channel that uses the default list"
}
after := b.store.Permissions()
var lines []string
for _, u := range g.Users {
var actions []model.Action
if g.ToDefault {
list := after.Default
for _, a := range model.GrantActions {
if model.Grants(list[u], a) {
actions = append(actions, a)
}
}
} else {
actions = after.ActionsFor(channel, u)
}
lines = append(lines, fmt.Sprintf("• <@%s> can now: %s", u, actionList(actions)))
}
return reply(fmt.Sprintf("Done, %s:\n%s", where, strings.Join(lines, "\n")))
case "dm":
words := strings.Fields(strings.ToLower(args))
if len(words) != 2 || !slices.Contains(model.AllActions, model.Action(words[0])) {
return false
}
on, ok := commands.ParseToggle(words[1])
if !ok {
return false
}
if !p.IsManager(m.user) {
return denied(true)
}
if err := update(func(p *model.PermissionsConfig) error { p.DM[words[0]] = on; return nil }); err != nil {
return reply(":warning: " + err.Error())
}
state := "turned off"
if on {
state = "allowed"
}
return reply(fmt.Sprintf("Done. Changing *%s* on your own DM container is now %s for everyone.", words[0], state))
case "managers":
words := strings.Fields(args)
if len(words) == 0 {
return reply("*User managers:* " + mentions(p.Managers))
}
op := strings.ToLower(words[0])
g, err := commands.ParseGrant(strings.Join(words[1:], " "))
if (op != "add" && op != "remove") || err != nil || len(g.Actions) > 0 {
return false
}
if !p.IsManager(m.user) {
return denied(true)
}
err = update(func(p *model.PermissionsConfig) error {
if op == "add" {
p.AddManagers(g.Users)
return nil
}
return p.RemoveManagers(g.Users)
})
if errors.Is(err, model.ErrLastManager) {
return reply(":warning: That would leave nobody able to manage permissions. Add someone else first.")
}
if err != nil {
return reply(":warning: " + err.Error())
}
verbText := "are now user managers"
if op == "remove" {
verbText = "are no longer user managers"
}
return reply(fmt.Sprintf("Done. %s %s.", mentions(g.Users), verbText))
case "permissions":
if strings.ToLower(args) != "reset" {
return false
}
if kind == model.OwnerUser {
return reply("DMs don't have a super-user list to reset.")
}
if !p.CanManage(m.channel, m.user) {
return denied(false)
}
if err := update(func(p *model.PermissionsConfig) error { delete(p.Channels, m.channel); return nil }); err != nil {
return reply(":warning: " + err.Error())
}
return reply("Done. This channel uses the default super-user list again.")
}
return false
}