Add helpful variables for user messages

This commit is contained in:
Nick Chambers 2021-06-20 00:14:05 -05:00
parent 41a4842920
commit e976de6b63
1 changed files with 40 additions and 8 deletions

48
rowbot
View File

@ -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