Compare commits

..

No commits in common. "c33ebdd52cd336e139832bed1145eedf62d653d4" and "e933bc443abfa1476d59c1dbf981f8a5746192b4" have entirely different histories.

1 changed files with 71 additions and 190 deletions

261
rowbot
View File

@ -13,32 +13,6 @@ stty -echoctl
# cerealizers
put_assoc_array() {
declare -n assoc_array=$1
declare -n scalar=RB_AA_${1^^}
local key
for key in "${!assoc_array[@]}"; do
scalar+=${#key},${#assoc_array[$key]}:$key${assoc_array[$key]}
done
export "RB_AA_${1^^}"
}
get_assoc_array() {
declare -n assoc_array=$1
declare -n scalar=RB_AA_${1^^}
while [[ $scalar ]]; do
key_len=${scalar%%,*} val_len=${scalar#*,}
val_len=${val_len%%:*} scalar=${scalar#"$key_len","$val_len":}
assoc_array[${scalar:0:key_len}]=${scalar:key_len:val_len}
scalar=${scalar:key_len + val_len}
done
unset "RB_AA_${1^^}"
}
put_array() {
# The variable named array is a nameref to an array
# shellcheck disable=SC2178
@ -56,12 +30,13 @@ put_array() {
get_array() {
declare -n array=$1
declare -n scalar=RB_A_${1^^}
local len val
local len val idx=0
while [[ $scalar ]]; do
len=${scalar%%:*} scalar=${scalar#"$len":}
val=${scalar:0:len} scalar=${scalar:len}
array+=( "$val" )
array[$idx]=$val
(( idx += 1 ))
done
unset "RB_A_${1^^}"
@ -97,6 +72,23 @@ b64_encode() {
# code reloading helpers
get_option() {
local var_name=${1//-/_}
local env_var=${var_name^^}
if [[ ! -v config[$1] || -v OPT_OW ]]; then
if [[ -v $env_var ]]; then
config[$1]=${!env_var}
elif [[ -v opts[$1] ]]; then
config[$1]=${opts[$1]}
elif [[ -v $var_name ]]; then
config[$1]=${!var_name}
elif (( $# > 1 )); then
config[$1]=$2
fi
fi
}
is_reloaded() {
[[ $RELOADED = yes ]] || (( RELOAD_COUNT ))
}
@ -140,7 +132,11 @@ shuffle() {
done
}
# process management and friends
# misc
is_running () {
kill -0 "$1" 2>/dev/null
}
die() {
local fmt=$1
@ -160,12 +156,6 @@ has() {
fi
}
is_running () {
kill -0 "$1" 2>/dev/null
}
# misc
run_callbacks() {
if (( ! $# )); then
return 1
@ -230,12 +220,12 @@ url() {
}
###
# Prepare rowbot's configuration
# configure rowbot's environment
###
# parse command line arguments
declare -A config opts
declare -A opts
cmd_line=( "${@:0}" )
while (( $# )); do
@ -265,8 +255,6 @@ done
# load custom configuration files
mapfile -t old_set < <(compgen -v)
for file do
if [[ -f $file ]]; then
# These files (if any) are provided dynamically at run-time.
@ -277,131 +265,27 @@ for file do
fi
done
mapfile -t new_set < <(compgen -v)
for new in "${new_set[@]}"; do
found=0
for old in "${old_set[@]}"; do
if [[ $new = "$old" ]]; then
found=1
break
fi
done
if (( !found )); then
config[$new]=${!new}
fi
done
for setting in "${!opts[@]}"; do
config[$setting]=${opts[$setting]}
done
while IFS= read -r setting; do
config[${setting,,}]=${!setting}
done < <(compgen -e)
# cleanup
unset key opts old_set file new_set new found old setting
unset key file
###
# state management
# load default config
###
state_resolve() {
local ns=${NS-global} managed found=0
declare -gA __rowbot_state_store_"$ns"
declare -n ns_config=__rowbot_state_store_"$ns"
declare -A config
if [[ $ns != global ]]; then
for managed in "${states_managed[@]}"; do
if [[ $managed = "$ns" ]]; then
found=1
break
fi
done
if (( !found )); then
states_managed+=( "$ns" )
fi
fi
if [[ -v config[$1] ]]; then
ns_config[$1]=${config[$1]}
elif [[ -v DEFAULT ]]; then
ns_config[$1]=$DEFAULT
else
return 1
fi
}
state_get() {
local ns=${NS-global}
# The `ns_config` variable is a reference to an array
# shellcheck disable=SC2178
declare -n ns_config=__rowbot_state_store_"$ns"
if [[ -v ns_config[$1] ]]; then
printf %s "${ns_config[$1]}"
elif [[ -v DEFAULT ]]; then
printf %s "$DEFAULT"
else
return 1
fi
}
state_has() {
local ns=${NS-global} found=1 managed
declare -n ns_config=__rowbot_state_store_"$ns"
for managed in "${ns_config[@]}"; do
if [[ $managed = "$1" ]]; then
found=0
break
fi
done
return "$found"
}
on_sys_init_001_state() {
states_managed=( global )
}
on_sys_before_999_state() {
local managed
for managed in "${states_managed[@]}"; do
put_assoc_array __rowbot_state_store_"$managed"
done
put_array states_managed
}
on_sys_after_001_state() {
local managed
get_array states_managed
for managed in "${states_managed[@]}"; do
get_assoc_array __rowbot_state_store_"$managed"
done
}
get_option owner "${USER:-uplime}"
get_option trigger \`
get_option dev yes
###
# logger
###
log() {
if NS=log state_has fd; then
# The only possible fail conditions are already checked for.
# shellcheck disable=SC2155
local level=$(NS=log state_get level) fd=$(NS=log state_get fd)
if (( log_levels[$level] <= log_levels[$LOG_LEVEL] )); then
printf "%s: $1\n" "${LOG_LEVEL^^}" "${@:2}" >&"$fd"
fi
if [[ -v LOG_LEVEL ]] && (( log_levels[$log_level] <= log_levels[$LOG_LEVEL] )); then
printf "%s: $1\n" "${LOG_LEVEL^^}" "${@:2}" >&"$log_fd"
fi
}
@ -437,50 +321,40 @@ log_has_level() {
return 1
}
on_sys_init_005_log() {
on_sys_init_001_log() {
declare -gA log_levels=( [trace]=1 [debug]=2 [info]=3 [warn]=4 [error]=5 )
NS=log DEFAULT=info state_resolve level
get_option log-level info
if ! log_has_level "$(NS=log state_get level)"; then
die "%s is not a valid logging level" "$(NS=log state_get level)"
if ! log_has_level "${config[log-level]}"; then
die "%s is not a valid logging level" "${config[log-level]}"
fi
local log_fd=1
get_option log ""
get_option overwrite no
log_level=${config[log-level]}
if [[ -v ${config[log]} ]]; then
if [[ ${config[log]} ]]; then
if [[ ${config[overwrite]} = yes ]]; then
exec {log_fd}>"${config[log]}"
else
exec {log_fd}>>"${config[log]}"
fi
else
log_fd=1
fi
NS=log DEFAULT=$log_fd state_resolve fd
}
on_sys_before_999_log() {
if NS=log state_has fd; then
# The only possible fail condition is already checked for.
# shellcheck disable=2155
local fd=$(NS=log state_get fd)
if (( fd != 1 )); then
log_debug "shutting logger down for reload"
exec {fd}>&-
fi
if [[ -v log_fd ]] && (( log_fd != 1 )); then
log_debug "shutting logger down for reload"
exec {log_fd}>&-
fi
}
on_sys_exit_999_log() {
if NS=log state_has fd; then
# The only possible fail condition is already checked for.
# shellcheck disable=2155
local fd=$(NS=log state_get fd)
if (( fd != 1 )); then
log_debug "shutting logger down for good"
exec {fd}>&-
fi
if [[ -v log_fd ]] && (( log_fd != 1 )); then
log_debug "shutting logger down for good"
exec {log_fd}>&-
fi
}
@ -488,24 +362,31 @@ on_sys_exit_999_log() {
# bootup/shutdown sequence
###
on_sys_init_010_bootup() {
DEFAULT=${USER:-uplime} state_resolve owner
DEFAULT=\` state_resolve trigger
DEFAULT=yes state_resolve dev
}
on_sys_first_005_bootup() {
on_sys_first_001_bootup() {
log_info "rowbot's pid is %d" "$$"
}
on_sys_before_995_bootup() {
on_sys_before_999_bootup() {
local setting setting_name
log_debug "storing the config"
put_assoc_array config
for setting in "${!config[@]}"; do
setting_name=${setting//-/_}
export "CONFIG_${setting_name^^}=${config[$setting]}"
log_trace "storing config key %s with value %s" "CONFIG_${setting_name^^}" "${config[$setting]}"
done
}
on_sys_after_005_bootup() {
on_sys_after_001_bootup() {
log_debug "reloading the config"
get_assoc_array config
local setting setting_name
while IFS= read -r setting; do
setting_name=${setting#CONFIG_} setting_name=${setting_name//_/-}
config[${setting_name,,}]=${!setting}
unset "$setting"
log_trace "getting config key %s with value %s" "CONFIG_${setting_name^^}" "${config[$setting]}"
done < <(compgen -e CONFIG_)
}
on_sys_exit_999_bootup() {
@ -688,11 +569,11 @@ on_msg_005_welcome() {
}
on_sys_before_001_welcome() {
put_assoc_array isupport
put_array isupport
}
on_sys_after_999_welcome() {
get_assoc_array isupport
get_array isupport
}
on_sys_register_999_welcome() {