Lay the groundwork for responding to IRC messages

This commit is contained in:
Nick Chambers 2021-07-08 01:15:01 -05:00 committed by Nick Chambers
parent 99783ba792
commit 576738e85e
1 changed files with 29 additions and 4 deletions

33
rowbot
View File

@ -57,6 +57,14 @@ has() {
fi fi
} }
is_action() {
[[ ${msg[cmd]} = PRIVMSG && ${msg_args[-1]:0:${#config[trigger]}} = "${config[trigger]}" ]]
}
is_chan() {
[[ ${msg_args[${1:-0}]} = \# ]]
}
is_parent() { is_parent() {
(( BASHPID == $$ )) (( BASHPID == $$ ))
} }
@ -79,11 +87,12 @@ run_callbacks() {
return 1 return 1
fi fi
local filter=$1 local status=0 filter=$1
shift shift
while IFS= read -r; do while IFS= read -r; do
"$REPLY" "$@" "$REPLY" "$@"
(( status |= $? ))
done < <(compgen -A function "$filter") done < <(compgen -A function "$filter")
return 0 return 0
@ -614,6 +623,7 @@ while net_recv line; do
# alphabet values # alphabet values
msg[cmd]=${line%% *} line=${line#"${msg[cmd]}"} line=${line# } msg[cmd]=${line%% *} line=${line#"${msg[cmd]}"} line=${line# }
msg[cmd]=${msg[cmd]^^}
log_debug "parsing message command %s" "${msg[cmd]}" log_debug "parsing message command %s" "${msg[cmd]}"
# parse the remaining values into white-space separated arguments # parse the remaining values into white-space separated arguments
@ -633,7 +643,7 @@ while net_recv line; do
fi fi
done done
if [[ ${msg[cmd]^^} = @(PRIVMSG|NOTICE) ]]; then if [[ ${msg[cmd]} = @(PRIVMSG|NOTICE) ]]; then
case ${msg_args[-1]} in case ${msg_args[-1]} in
"["*"]") "["*"]")
(( msg[score] += 20 )) (( msg[score] += 20 ))
@ -645,9 +655,24 @@ while net_recv line; do
log_debug "bot score is %d" "${msg[score]}" log_debug "bot score is %d" "${msg[score]}"
if has irc_on_"${msg[cmd]^^}"; then if has irc_on_"${msg[cmd]}"; then
irc_on_"${msg[cmd]^^}" if run_callbacks "on_msg_${msg[cmd]}_"; then
log_debug "handler for %s was skipped" "${msg[cmd]}"
else
irc_on_"${msg[cmd]}"
fi
run_callbacks "on_late_msg_${msg[cmd]}_"
else else
log_warn "unhandled line: %s" "${msg[original]}" log_warn "unhandled line: %s" "${msg[original]}"
fi fi
if is_action; then
action=${msg_args[-1]#"${config[trigger]}"} action=${action%% *}
action_line=${msg_args[-1]#"$trigger$action"}
read -r action_line <<< "$action_line"
# This variable will be used in later code.
# shellcheck disable=SC2034
read -ra action_args <<< "$action_line"
fi
done done