Files
slacker-bot/manage.go
T
2026-11-01 11:47:41 -05:00

236 lines
6.2 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"regexp"
"slices"
"strings"
)
var mentionRe = regexp.MustCompile(`^<@([UW][A-Z0-9]+)(?:\|[^>]*)?>[,.]?$`)
type grantCmd struct {
toDefault bool
users []string
actions []Action
}
func parseGrant(args string) (grantCmd, error) {
var g grantCmd
words := strings.Fields(args)
i := 0
if len(words) > 0 && strings.ToLower(words[0]) == "default" {
g.toDefault, i = true, 1
}
for ; i < len(words); i++ {
if m := mentionRe.FindStringSubmatch(words[i]); m != nil {
g.users = append(g.users, m[1])
continue
}
w := strings.ToLower(strings.Trim(words[i], ",."))
if w == "all" || slices.Contains(grantActions, Action(w)) {
g.actions = append(g.actions, Action(w))
continue
}
if len(g.users) == 0 && !g.toDefault {
return g, errNotCommand
}
return g, fmt.Errorf("I didn't understand `%s`", words[i])
}
switch {
case len(g.users) == 0 && !g.toDefault:
return g, errNotCommand
case len(g.users) == 0:
return g, errors.New("name at least one person, like `grant default @alice all`")
}
return g, nil
}
func parseToggle(word string) (on, ok bool) {
switch strings.ToLower(word) {
case "on", "yes", "true", "allow", "allowed", "enable":
return true, true
case "off", "no", "false", "deny", "denied", "disable":
return false, true
}
return false, false
}
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 []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, "", 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 *PermissionsConfig) error) error {
return b.store.UpdatePermissions(change)
}
switch verb {
case "grant", "revoke":
g, err := parseGrant(args)
if errors.Is(err, 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 == 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, ActManage) && !g.toDefault && !p.CanManage(channel, m.user) {
return denied(false)
}
err = update(func(p *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 []Action
if g.toDefault {
list := after.Default
for _, a := range grantActions {
if 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(allActions, Action(words[0])) {
return false
}
on, ok := parseToggle(words[1])
if !ok {
return false
}
if !p.IsManager(m.user) {
return denied(true)
}
if err := update(func(p *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 := 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 *PermissionsConfig) error {
if op == "add" {
p.AddManagers(g.users)
return nil
}
return p.RemoveManagers(g.users)
})
if errors.Is(err, 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 == 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 *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
}