Changeset 328 in code


Ignore:
Timestamp:
Jun 8, 2020, 8:30:09 PM (5 years ago)
Author:
delthas
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/service.go

    r325 r328  
    4141        handle   func(dc *downstreamConn, params []string) error
    4242        children serviceCommandSet
     43        admin    bool
    4344}
    4445
     
    6970        if err != nil {
    7071                sendServicePRIVMSG(dc, fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err))
     72                return
     73        }
     74        if cmd.admin && !dc.user.Admin {
     75                sendServicePRIVMSG(dc, fmt.Sprintf(`error: you must be an admin to use this command`))
    7176                return
    7277        }
     
    166171}
    167172
    168 func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, l *[]string) {
     173func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, admin bool, l *[]string) {
    169174        for name, cmd := range cmds {
     175                if cmd.admin && !admin {
     176                        continue
     177                }
    170178                words := append(prefix, name)
    171179                if len(cmd.children) == 0 {
     
    173181                        *l = append(*l, s)
    174182                } else {
    175                         appendServiceCommandSetHelp(cmd.children, words, l)
     183                        appendServiceCommandSetHelp(cmd.children, words, admin, l)
    176184                }
    177185        }
     
    188196                if len(cmd.children) > 0 {
    189197                        var l []string
    190                         appendServiceCommandSetHelp(cmd.children, words, &l)
     198                        appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l)
    191199                        sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
    192200                } else {
     
    201209        } else {
    202210                var l []string
    203                 appendServiceCommandSetHelp(serviceCommands, nil, &l)
     211                appendServiceCommandSetHelp(serviceCommands, nil, dc.user.Admin, &l)
    204212                sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
    205213        }
Note: See TracChangeset for help on using the changeset viewer.