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.
This commit is contained in:
delthas 2020-06-07 01:27:07 +02:00 committed by Simon Ser
parent d1b4faa529
commit f5611ae3f9
1 changed files with 12 additions and 4 deletions

View File

@ -40,6 +40,7 @@ type serviceCommand struct {
desc string desc string
handle func(dc *downstreamConn, params []string) error handle func(dc *downstreamConn, params []string) error
children serviceCommandSet children serviceCommandSet
admin bool
} }
func sendServiceNOTICE(dc *downstreamConn, text string) { 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)) sendServicePRIVMSG(dc, fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err))
return 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 { if err := cmd.handle(dc, params); err != nil {
sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err)) 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 { for name, cmd := range cmds {
if cmd.admin && !admin {
continue
}
words := append(prefix, name) words := append(prefix, name)
if len(cmd.children) == 0 { if len(cmd.children) == 0 {
s := strings.Join(words, " ") s := strings.Join(words, " ")
*l = append(*l, s) *l = append(*l, s)
} else { } 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 { if len(cmd.children) > 0 {
var l []string var l []string
appendServiceCommandSetHelp(cmd.children, words, &l) appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l)
sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", ")) sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
} else { } else {
text := strings.Join(words, " ") text := strings.Join(words, " ")
@ -200,7 +208,7 @@ func handleServiceHelp(dc *downstreamConn, params []string) error {
} }
} else { } else {
var l []string var l []string
appendServiceCommandSetHelp(serviceCommands, nil, &l) appendServiceCommandSetHelp(serviceCommands, nil, dc.user.Admin, &l)
sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", ")) sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
} }
return nil return nil