From a8c9f5f9c1940f49e2a2b8904d7612f23921009b Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sun, 20 Jun 2021 00:14:05 -0500 Subject: [PATCH] Add helpful variables for user messages --- rowbot | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/rowbot b/rowbot index fe5c975..6287400 100755 --- a/rowbot +++ b/rowbot @@ -765,8 +765,26 @@ else user "$ident" "$realname" fi +# Always available: +# - from: name of the entity sending the message +# - ident: username of the entity sending the message +# - host: host of the entity sending the message +# - cmd: IRC command or numeric +# - args: array of arguments to the command + +# Available if the last parameter starts with ":" +# - words: array of words, split by whitespace, of the last parameter + +# Available if `cmd` is set to "privmsg": +# - to: location to send the message back to + +# Additionally, if args[-1] starts with the trigger: +# - action: directive specified by the sender +# - action_line: data sent by the sender without the trigger or action +# - action_args: words sent by the sender without the trigger or action + while recv line; do - params=( ) + args=( ) has_words=no orig_line=$line @@ -783,22 +801,36 @@ while recv line; do while [[ $line ]]; do if [[ ${line:0:1} = : ]]; then - params+=("${line:1}") + args+=( "${line:1}" ) line="" has_words=yes else - param=${line%% *} - params+=("$param") - line=${line#"$param"} line=${line# } + arg=${line%% *} + args+=( "$arg" ) + line=${line#"$arg"} line=${line# } fi done if [[ $has_words = yes ]]; then - read -ra words <<< "${params[@]:(-1)}" + read -ra words <<< "${args[@]:(-1)}" else words=( ) fi + if [[ ${cmd^^} = PRIVMSG ]]; then + to=${args[0]} last=${args[@]:(-1)} + + if [[ ${to:0:1} = \# ]]; then + to=$from + fi + + if [[ $last = "$trigger"* ]]; then + action=${last#"$trigger"} action=${action## *} + action_line=${last#"$trigger$action" } + read -ra action_args <<< "$action_line" + fi + fi + skip_handler=0 while IFS= read -r hook; do @@ -806,14 +838,14 @@ while recv line; do (( skip_handler |= $? )) done < <(compgen -A function "hook_pre_${cmd^^}_") - if hash "on_${cmd^^}" 2>/dev/null; then + if has "on_${cmd^^}"; then if (( ! skip_handler )); then "on_${cmd^^}" else debug "handler for %s was skipped" "${cmd^^}" fi else - warn "unhandled line: %s" "$orig_line" + warn "unhandled line: %s" "$orig_line" fi while IFS= read -r hook; do