From f5611ae3f92e641ee74baf806add060b9341aee1 Mon Sep 17 00:00:00 2001 From: delthas Date: Sun, 7 Jun 2020 01:27:07 +0200 Subject: [PATCH] Add support for admin-restricted service commands This is preparatory work for creating new users from a service command. This adds support for specifying specific service commands as admin-restricted. Only admin users can run these commands. These commands won't show up in the help when run from a non-admin user, unless the user is requesting help for that specific command. --- service.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/service.go b/service.go index ca36542..9894668 100644 --- a/service.go +++ b/service.go @@ -40,6 +40,7 @@ type serviceCommand struct { desc string handle func(dc *downstreamConn, params []string) error children serviceCommandSet + admin bool } func sendServiceNOTICE(dc *downstreamConn, text string) { @@ -70,6 +71,10 @@ func handleServicePRIVMSG(dc *downstreamConn, text string) { sendServicePRIVMSG(dc, fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err)) return } + if cmd.admin && !dc.user.Admin { + sendServicePRIVMSG(dc, fmt.Sprintf(`error: you must be an admin to use this command`)) + return + } if err := cmd.handle(dc, params); err != nil { sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err)) @@ -165,14 +170,17 @@ func init() { } } -func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, l *[]string) { +func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, admin bool, l *[]string) { for name, cmd := range cmds { + if cmd.admin && !admin { + continue + } words := append(prefix, name) if len(cmd.children) == 0 { s := strings.Join(words, " ") *l = append(*l, s) } else { - appendServiceCommandSetHelp(cmd.children, words, l) + appendServiceCommandSetHelp(cmd.children, words, admin, l) } } } @@ -187,7 +195,7 @@ func handleServiceHelp(dc *downstreamConn, params []string) error { if len(cmd.children) > 0 { var l []string - appendServiceCommandSetHelp(cmd.children, words, &l) + appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l) sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", ")) } else { text := strings.Join(words, " ") @@ -200,7 +208,7 @@ func handleServiceHelp(dc *downstreamConn, params []string) error { } } else { var l []string - appendServiceCommandSetHelp(serviceCommands, nil, &l) + appendServiceCommandSetHelp(serviceCommands, nil, dc.user.Admin, &l) sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", ")) } return nil