Add plugin support

This commit is contained in:
Nick Chambers 2021-07-12 00:22:31 -05:00 committed by Nick Chambers
parent 63a025e5be
commit 3a824f6d44
1 changed files with 59 additions and 0 deletions

59
rowbot
View File

@ -614,6 +614,64 @@ irc_on_473() {
log_error "%s: %s" "${msg_args[1]}" "${msg_args[2]}"
}
###
# plugin api
###
plugin_reg() {
declare -n plugins=irc_plugin_array_"$1"
local plugin
if [[ -v plugins ]]; then
for plugin in "${plugins[@]}"; do
if [[ $plugin = "$2" ]]; then
return 1
fi
done
else
plugins=( )
fi
plugins+=( "$2" )
}
plugin_run() {
# This is a false positive.
# shellcheck disable=SC2178
declare -n plugins=irc_plugin_array_"$1"
shift
local plugin
if [[ -v plugins ]]; then
for plugin in "${plugins[@]}"; do
"$plugin" "$@"
done
fi
}
###
# plugins
###
# lime-o-meter
limeometer() {
# We don't care about failures here, for better or for worse.
# shellcheck disable=SC2155
local limes=$(random 1 42) limeification
if [[ ${msg[from]} = Time-Warp ]]; then
limes=42
fi
(( limeification = (limes * 100) / 42 ))
irc_privmsg "${msg[to]}" "$limes limes (limes to $limeification%)"
}
on_init_limeometer() {
plugin_reg uplime limeometer
}
###
# irc send handlers
###
@ -806,5 +864,6 @@ while net_recv line; do
# This variable will be used in later code.
# shellcheck disable=SC2034
read -ra action_args <<< "$action_line"
plugin_run "$action"
fi
done