Add IRC boiler plate for higher level features

This commit is contained in:
Nick Chambers 2021-07-06 07:48:36 -05:00 committed by Nick Chambers
parent ea7146d57d
commit 1b3970d103
1 changed files with 126 additions and 18 deletions

144
rowbot
View File

@ -4,7 +4,7 @@
# feature switch toggling
###
shopt -s dotglob extglob lastpipe nullglob
shopt -s dotglob extglob nullglob
###
# utility and helper functions
@ -31,7 +31,7 @@ die() {
}
get_option() {
if (( $# != 2 )); then
if (( ! $# )); then
return 1
fi
@ -44,7 +44,7 @@ get_option() {
config[$1]=${opts[$1]}
elif [[ -v $var_name ]]; then
config[$1]=${!var_name}
else
elif (( $# > 1 )); then
config[$1]=$2
fi
}
@ -65,6 +65,10 @@ is_reloaded() {
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 ))
}
is_running () {
kill -0 "$1" 2>/dev/null
}
random() {
local min=$1 max=$2
(( (RANDOM % max) + min ))
@ -141,15 +145,6 @@ unset key file
declare -A config
# irc registration settings
get_option nick rowbot-dev
get_option ident rowbot
get_option realname rowbot
get_option chan ""
# bot control settings
get_option owner "${USER:-uplime}"
get_option trigger \`
get_option dev yes
@ -160,12 +155,15 @@ get_option dev yes
on_sys_first_001_bootup() {
log_info "rowbot's pid is %d" "$$"
get_option nick rowbot-dev
get_option ident rowbot
get_option realname rowbot
}
on_sys_first_999_bootup() {
log_debug "registering with the server"
nick "${config[nick]}"
user "${config[ident]}" "${config[realname]}"
irc_nick "${config[nick]}"
irc_user "${config[ident]}" "${config[realname]}"
}
on_sys_before_999_bootup() {
@ -351,7 +349,7 @@ on_sys_after_002_net() {
unset IRC_SOCK
else
sock_dir=$SOCK_DIR tls_pid=$TLS_PID out_sock=$OUT_SOCK in_sock=$IN_SOCK
unset sock_dir tls_pid out_sock in_sock
unset SOCK_DIR TLS_PID OUT_SOCK IN_SOCK
fi
}
@ -370,15 +368,100 @@ on_sys_exit_998_net() {
fi
}
###
# irc magic
###
magic_annoyatron900() {
irc_ping "row your bot gently down the stream"
}
on_sys_init_999_magic() {
get_option chan ""
}
on_sys_before_999_magic() {
if [[ -v alarm_pid ]]; then
export ALARM_PID=$alarm_pid
fi
}
on_sys_after_999_magic() {
trap magic_annoyatron900 USR1
if [[ -v ALARM_PID ]]; then
alarm_pid=$ALARM_PID
fi
}
on_sys_register_999_magic() {
if [[ ${config[chan]} ]]; then
irc_join "$chan"
fi
while true; do
read -rt 10 </dev/zero
KILL -USR1 "$$"
done &
alarm_pid=$!
trap magic_annoyatron900 USR1
log_debug "process %d is being annoying" "$alarm_pid"
}
on_sys_exit_997_magic() {
log_debug "shutting down annoyatron900"
if [[ -v alarm_pid ]] && is_running "$alarm_pid"; then
kill -STOP "$alarm_pid"
fi
}
# nick=${args[0]}
# who "$nick" %%uht,42
###
# irc receive handlers
###
irc_on_NOTICE() {
log_info "[%s/%s] %s" "${msg[from]}" "${msg_args[0]}" "${msg_args[1]}"
}
irc_on_PING() {
irc_pong "${msg_args[1]}"
log_debug "received ping: %s" "${msg_args[0]}"
}
irc_on_PONG() {
log_debug "received pong: %s" "${msg_args[1]}"
}
irc_on_001() {
log_info %s "${msg_args[1]}"
run_callbacks on_sys_register_
run_callbacks on_register_
}
###
# irc send handlers
###
nick() {
irc_join() {
local chans
printf -v chans %s, "$@"
net_send "JOIN %s" "${chans%,}"
}
irc_nick() {
net_send "NICK :%s" "$1"
}
user() {
irc_ping() {
net_send "PING :%s" "$1"
}
irc_user() {
net_send "USER %s 0 * :%s" "$1" "$2"
}
@ -430,7 +513,7 @@ fi
###
while net_recv line; do
declare -A msg=([words]=no [original]="$line")
declare -A msg=( [words]=no [original]="$line" [score]=0 )
# parse prefix in the style of nick!ident@host
@ -438,14 +521,27 @@ while net_recv line; do
prefix=${line%% *} prefix=${prefix#:} line=${line#:"$prefix"} line=${line# }
log_debug "parsing message prefix %s" "$prefix"
msg[host]=${prefix#*@} prefix=${prefix%"${msg[host]}"} prefix=${prefix%@}
msg[from]=${msg[host]}
if [[ $prefix ]]; then
msg[ident]=${prefix#*!}
msg[from]=${msg[ident]}
if [[ ${msg[ident]} != "$prefix" ]]; then
msg[nick]=${prefix%!*}
msg[from]=${msg[nick]}
fi
fi
if [[ /${msg[host]}/ = */bot/* ]]; then
(( msg[score] += 100 ))
fi
if [[ ${msg[nick]} = *-bot ]]; then
(( msg[score] += 30 ))
elif [[ ${msg[nick]} = *bot ]]; then
(( msg[score] += 15 ))
fi
fi
# parse command formatted as a 3 digit integer or as a word consisting of
@ -471,6 +567,18 @@ while net_recv line; do
fi
done
if [[ ${msg[cmd]^^} = @(PRIVMSG|NOTICE) ]]; then
case ${msg_args[-1]} in
"["*"]")
(( msg[score] += 20 ))
;;
$'\xe2\x80\x8b'*)
(( msg[score] += 100 ))
esac
fi
log_debug "bot score is %d" "${msg[score]}"
if has irc_on_"${msg[cmd]^^}"; then
irc_on_"${msg[cmd]^^}"
else