Guard against invalid logging levels

This commit is contained in:
Nick Chambers 2021-06-20 01:45:19 -05:00
parent 2ce50300ce
commit 5dc09e254f
1 changed files with 54 additions and 29 deletions

83
rowbot
View File

@ -6,31 +6,6 @@
shopt -s nullglob dotglob extglob
###
# utilities
###
parent() {
(( BASHPID == $$ ))
}
has() {
hash "$1" 2>/dev/null
}
die() {
local status=1
if (( $# > 1 )) && [[ $1 = -s ]]; then
status=$2
shift
shift
fi
error "$@"
exit "$status"
}
###
# logger
###
@ -62,6 +37,43 @@ error() {
LEVEL=error log "$@"
}
###
# utilities
###
parent() {
(( BASHPID == $$ ))
}
has() {
hash "$1" 2>/dev/null
}
die() {
local status=1
if (( $# > 1 )) && [[ $1 = -s ]]; then
status=$2
shift
shift
fi
error "$@"
exit "$status"
}
is-log-level() {
local level
for level in "${!levels[@]}"; do
if [[ ${1,,} = "$level" ]]; then
return 0
fi
done
return 1
}
###
# argument parser for parsing arguments
##
@ -126,6 +138,16 @@ if [[ -v opts[tls] ]]; then
tls=${opts[tls]}
fi
# This is a false positive.
# shellcheck disable=SC2102
if [[ -v opts[level] ]]; then
if is-log-level "${opts[level]}"; then
level=${opts[level],,}
else
die "%s is not a valid logging level" "${opts[level]}"
fi
fi
if [[ $tls = yes ]]; then
# This is a false positive.
# shellcheck disable=SC2102
@ -140,8 +162,7 @@ fi
config=(
server port nick ident realname chan trigger
fact_root reload dev level log owner sys_root
reload
fact_root reload dev log owner sys_root reload
)
for opt in "${config[@]}"; do
@ -685,8 +706,12 @@ hook_cmd_control_panel() {
exec "$0" --reload "${original_args[@]}"
;;
level)
level=${action_args[0]}
privmsg "$to" "log level is now set to $level"
if is-log-level "${action_args[0]}"; then
level=${action_args[0],,}
privmsg "$to" "log level is now set to $level"
else
privmsg "$to" "${action_args[0]} is not a valid logging level"
fi
;;
dev)
if [[ $dev = yes ]]; then