Implement a Markov chain hook
This commit is contained in:
parent
cc084a8801
commit
555d9c728c
1 changed files with 79 additions and 1 deletions
80
rowbot
80
rowbot
|
@ -115,7 +115,7 @@ prog_args=( "$@" )
|
|||
server=irc.libera.chat port=6667 tls=no client_cert=
|
||||
nick=rowbot-dev ident=rowbot realname=rowbot chan=
|
||||
trigger=\` fact_root=. reload=no level=info log_fd=1 log=
|
||||
sys_root=sysfacts owner=${USER:-uplime} dev=no
|
||||
sys_root=sysfacts owner=${USER:-uplime} dev=no markov_seed=
|
||||
|
||||
###
|
||||
# apply custom config
|
||||
|
@ -185,6 +185,25 @@ fi
|
|||
# apply reloaded values
|
||||
###
|
||||
|
||||
if [[ $markov_seed ]]; then
|
||||
if [[ -f $markov_seed ]]; then
|
||||
declare -A markov_chains enabled
|
||||
read -ra words < "$markov_seed"
|
||||
|
||||
for (( idx = 0; idx < ${#words[@]} - 2; idx++ )); do
|
||||
key="${words[$idx]} ${words[$idx + 1]}"
|
||||
|
||||
if [[ -z ${markov_chains[$key]} ]]; then
|
||||
markov_chains[$key]=${words[$idx + 2]}
|
||||
else
|
||||
markov_chains[$key]+=" ${words[$idx + 2]}"
|
||||
fi
|
||||
done
|
||||
else
|
||||
die "seed file does not exist: %s" "$markov_seed"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ $reload = yes ]]; then
|
||||
reload_vars=(
|
||||
nick ident host level log log_fd alarm_pid tls_pid in_sock
|
||||
|
@ -656,6 +675,65 @@ hook_pre_PRIVMSG_CTCP() {
|
|||
return 1
|
||||
}
|
||||
|
||||
hook_post_PRIVMSG_markov() {
|
||||
if [[ ! ${markov_chains@A} ]]; then
|
||||
return 0
|
||||
elif [[ ${to:0:1} = \# && ${enabled[$to]} != yes ]]; then
|
||||
return 0
|
||||
elif (( ${#action_args[@]} < 3 )); then
|
||||
return 0
|
||||
fi
|
||||
|
||||
local idx key
|
||||
|
||||
for (( idx = 0; idx < ${#action_args[@]} - 2; idx++ )); do
|
||||
key="${action_args[$idx]} ${action_args[$idx + 1]}"
|
||||
|
||||
if [[ -z ${markov_chains[$key]} ]]; then
|
||||
markov_chains[$key]=${words[$idx + 2]}
|
||||
else
|
||||
markov_chains[$key]+=" ${words[$idx + 2]}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
hook_cmd_markov() {
|
||||
local keys idx key str chains next vals val_idx
|
||||
|
||||
if [[ ! ${markov_chains@A} ]]; then
|
||||
if [[ $action = markov ]]; then
|
||||
privmsg "$to" "markov chains are not enabled"
|
||||
fi
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
case $action in
|
||||
enable)
|
||||
enabled[$to]=yes
|
||||
privmsg "$to" "now recording $to"
|
||||
;;
|
||||
disable)
|
||||
enabled[$to]=no
|
||||
privmsg "$to" "no longer recording $to"
|
||||
;;
|
||||
markov)
|
||||
keys=("${!markov_chains[@]}")
|
||||
(( idx = RANDOM % ${#keys[@]} ))
|
||||
key=${keys[$idx]} str=$key
|
||||
chains=0
|
||||
|
||||
while [[ -v markov_chains[$key] ]] && (( chains++ < 100 )); do
|
||||
read -ra vals <<< "${markov_chains[$key]}"
|
||||
(( val_idx = RANDOM % ${#vals[@]} ))
|
||||
next="$key ${vals[$val_idx]}" next=${next#* }
|
||||
str+=" ${next#* }" key=$next
|
||||
done
|
||||
|
||||
privmsg "$to" "$from: $str"
|
||||
esac
|
||||
}
|
||||
|
||||
hook_cmd_factoids() {
|
||||
local key val facts msg
|
||||
|
||||
|
|
Loading…
Reference in a new issue