From 576738e85ec01d361578d2f517d030d1c4fdfd40 Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Thu, 8 Jul 2021 01:15:01 -0500 Subject: [PATCH] Lay the groundwork for responding to IRC messages --- rowbot | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/rowbot b/rowbot index 8b91fd1..e1efd9b 100755 --- a/rowbot +++ b/rowbot @@ -57,6 +57,14 @@ has() { fi } +is_action() { + [[ ${msg[cmd]} = PRIVMSG && ${msg_args[-1]:0:${#config[trigger]}} = "${config[trigger]}" ]] +} + +is_chan() { + [[ ${msg_args[${1:-0}]} = \# ]] +} + is_parent() { (( BASHPID == $$ )) } @@ -79,11 +87,12 @@ run_callbacks() { return 1 fi - local filter=$1 + local status=0 filter=$1 shift while IFS= read -r; do "$REPLY" "$@" + (( status |= $? )) done < <(compgen -A function "$filter") return 0 @@ -614,6 +623,7 @@ while net_recv line; do # alphabet values msg[cmd]=${line%% *} line=${line#"${msg[cmd]}"} line=${line# } + msg[cmd]=${msg[cmd]^^} log_debug "parsing message command %s" "${msg[cmd]}" # parse the remaining values into white-space separated arguments @@ -633,7 +643,7 @@ while net_recv line; do fi done - if [[ ${msg[cmd]^^} = @(PRIVMSG|NOTICE) ]]; then + if [[ ${msg[cmd]} = @(PRIVMSG|NOTICE) ]]; then case ${msg_args[-1]} in "["*"]") (( msg[score] += 20 )) @@ -645,9 +655,24 @@ while net_recv line; do log_debug "bot score is %d" "${msg[score]}" - if has irc_on_"${msg[cmd]^^}"; then - irc_on_"${msg[cmd]^^}" + if has irc_on_"${msg[cmd]}"; then + 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 log_warn "unhandled line: %s" "${msg[original]}" 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