service: Enable running additional commands from a global context

Some commands do not require admin rights but are also useful to
run in a global context. For example, help.
This commit is contained in:
delthas 2023-01-19 18:40:40 +01:00 committed by Simon Ser
parent d17c7d57f2
commit f05bd84787
1 changed files with 12 additions and 2 deletions

View File

@ -53,6 +53,7 @@ type serviceCommand struct {
handle func(ctx *serviceContext, params []string) error handle func(ctx *serviceContext, params []string) error
children serviceCommandSet children serviceCommandSet
admin bool admin bool
global bool
} }
func sendServiceNOTICE(dc *downstreamConn, text string) { func sendServiceNOTICE(dc *downstreamConn, text string) {
@ -143,7 +144,7 @@ func handleServiceCommand(ctx *serviceContext, words []string) {
ctx.print("error: you must be an admin to use this command") ctx.print("error: you must be an admin to use this command")
return return
} }
if !cmd.admin && ctx.user == nil { if !cmd.global && ctx.user == nil {
ctx.print("error: this command must be run as a user (try running with user run)") ctx.print("error: this command must be run as a user (try running with user run)")
return return
} }
@ -220,6 +221,7 @@ func init() {
usage: "[command]", usage: "[command]",
desc: "print help message", desc: "print help message",
handle: handleServiceHelp, handle: handleServiceHelp,
global: true,
}, },
"network": { "network": {
children: serviceCommandSet{ children: serviceCommandSet{
@ -288,30 +290,36 @@ func init() {
desc: "show a list of users and their current status", desc: "show a list of users and their current status",
handle: handleUserStatus, handle: handleUserStatus,
admin: true, admin: true,
global: true,
}, },
"create": { "create": {
usage: "-username <username> -password <password> [-disable-password] [-admin true|false] [-nick <nick>] [-realname <realname>] [-enabled true|false]", usage: "-username <username> -password <password> [-disable-password] [-admin true|false] [-nick <nick>] [-realname <realname>] [-enabled true|false]",
desc: "create a new soju user", desc: "create a new soju user",
handle: handleUserCreate, handle: handleUserCreate,
admin: true, admin: true,
global: true,
}, },
"update": { "update": {
usage: "[username] [-password <password>] [-disable-password] [-admin true|false] [-nick <nick>] [-realname <realname>] [-enabled true|false]", usage: "[username] [-password <password>] [-disable-password] [-admin true|false] [-nick <nick>] [-realname <realname>] [-enabled true|false]",
desc: "update a user", desc: "update a user",
handle: handleUserUpdate, handle: handleUserUpdate,
global: true,
}, },
"delete": { "delete": {
usage: "<username> [confirmation token]", usage: "<username> [confirmation token]",
desc: "delete a user", desc: "delete a user",
handle: handleUserDelete, handle: handleUserDelete,
global: true,
}, },
"run": { "run": {
usage: "<username> <command>", usage: "<username> <command>",
desc: "run a command as another user", desc: "run a command as another user",
handle: handleUserRun, handle: handleUserRun,
admin: true, admin: true,
global: true,
}, },
}, },
global: true,
}, },
"channel": { "channel": {
children: serviceCommandSet{ children: serviceCommandSet{
@ -338,12 +346,14 @@ func init() {
desc: "show server statistics", desc: "show server statistics",
handle: handleServiceServerStatus, handle: handleServiceServerStatus,
admin: true, admin: true,
global: true,
}, },
"notice": { "notice": {
usage: "<notice>", usage: "<notice>",
desc: "broadcast a notice to all connected bouncer users", desc: "broadcast a notice to all connected bouncer users",
handle: handleServiceServerNotice, handle: handleServiceServerNotice,
admin: true, admin: true,
global: true,
}, },
}, },
admin: true, admin: true,
@ -357,7 +367,7 @@ func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, admin
if cmd.admin && !admin { if cmd.admin && !admin {
continue continue
} }
if !cmd.admin && global { if !cmd.global && global {
continue continue
} }
words := append(prefix, name) words := append(prefix, name)