rowbot/rowbot

501 lines
8.3 KiB
Plaintext
Raw Normal View History

2021-06-14 22:38:50 +00:00
#!/usr/bin/env bash
2021-06-14 23:35:23 +00:00
###
# logger
###
declare -A levels=(
[debug]=1 [info]=2
[warn]=3 [error]=4
)
log() {
if [[ -v LEVEL ]] && (( levels[$level] <= levels[$LEVEL] )); then
2021-06-14 23:45:40 +00:00
printf "%s: $1\n" "${LEVEL^^}" "${@:2}" >&"$log"
2021-06-14 23:35:23 +00:00
fi
}
debug() {
LEVEL=debug log "$@"
}
info() {
LEVEL=info log "$@"
}
warn() {
LEVEL=warn log "$@"
}
error() {
LEVEL=error log "$@"
}
2021-06-14 22:38:50 +00:00
###
2021-06-14 23:15:49 +00:00
# argument parser for parsing arguments
2021-06-14 22:38:50 +00:00
##
original_args=("$0" "$@")
2021-06-14 22:38:50 +00:00
declare -A opts
while (( $# )); do
case $1 in
--*=*)
key=${1#--} key=${key%%=*}
opts[$key]=${1#--*=}
;;
--no-*)
key=${1#--no-}
2021-06-14 22:41:05 +00:00
opts[$key]=no
2021-06-14 22:38:50 +00:00
;;
--)
shift
break
;;
--*)
key=${1#--}
2021-06-14 22:41:05 +00:00
opts[$key]=yes
2021-06-14 22:38:50 +00:00
;;
*)
break
esac
shift
done
2021-06-14 22:41:05 +00:00
###
# default config
##
server=${opts[server]:-irc.libera.chat}
tls=${opts[tls]:-no}
2021-06-14 23:15:49 +00:00
if [[ $tls = yes ]]; then
if ! hash socat 2>/dev/null; then
printf 'please install socat to use tls with rowbot.\n' >&2
exit 1
fi
port=${opts[port]:-6697}
else
2021-06-14 22:41:05 +00:00
port=${opts[port]:-6667}
2021-06-14 23:15:49 +00:00
fi
nick=${opts[nick]:-rowbot-dev}
ident=${opts[ident]:-rowbot}
realname=${opts[realname]:-rowbot}
chan=${opts[chan]:-}
trigger=${opts[trigger]:-\`}
fact_root=${opts[fact-root]:-.}
reload=${opts[reload]:-no}
dev=${opts[dev]:-no}
if [[ -v USER ]]; then
owner=${opts[owner]:-"$USER"}
else
owner=${opts[owner]:-uplime}
fi
2021-06-14 23:45:40 +00:00
level=${opts[log-level]:-info}
if [[ $reload = yes ]]; then
log=$LOG_FD
elif [[ ${opts[log]} ]]; then
2021-06-14 23:45:40 +00:00
exec {log}>"${opts[log]}"
else
log=1
fi
###
# utilities
###
cleanup() {
exec {in_sock}>&- {out_sock}>&- {log}>&-
if [[ $tls = yes ]]; then
kill "$tls_pid"
rm -rf "$sock_dir"
fi
if [[ -v ping_pid ]]; then
kill "$ping_pid"
fi
}
trap cleanup EXIT
2021-06-15 06:33:30 +00:00
2021-06-14 23:15:49 +00:00
###
# net code
###
if [[ $reload = yes ]]; then
in_sock=$IN_SOCK out_sock=$OUT_SOCK
if [[ $tls = yes ]]; then
sock_dir=$SOCK_DIR
tls_pid=$tls_pid
fi
if [[ -v PING_PID ]]; then
ping_pid=$PING_PID
fi
elif [[ $tls = yes ]]; then
sock_dir=$(mktemp -d)
mkfifo "$sock_dir"/rb{in,out}
socat OPENSSL:"$server":"$port" - <"$sock_dir"/rbin >"$sock_dir"/rbout &
tls_pid=$!
exec {out_sock}>"$sock_dir"/rbin {in_sock}<"$sock_dir"/rbout
2021-06-14 22:41:05 +00:00
else
2021-06-14 23:15:49 +00:00
exec {sock}<>/dev/tcp/"$server"/"$port"
in_sock=$sock out_sock=$sock
2021-06-14 22:41:05 +00:00
fi
2021-06-14 23:15:49 +00:00
send() {
local fmt
printf -v fmt "$1" "${@:2}"
printf '%s\r\n' "$fmt" >&"$out_sock"
2021-06-15 06:33:30 +00:00
debug "sending line: %s" "$fmt"
2021-06-14 23:15:49 +00:00
}
recv() {
declare -n sock_line=$1
IFS= read -r "$1" <&"$in_sock"
sock_line=${sock_line%$'\r'}
2021-06-15 06:33:30 +00:00
debug "received line: %s" "$sock_line"
}
###
# irc recv code
###
on_ERROR() {
error "${params[0]}"
exit
}
on_JOIN() {
info "%s has joined %s" "$from" "${params[0]}"
}
2021-06-15 06:33:30 +00:00
on_MODE() {
if (( ${#params[@]} == 2 )); then
info "%s sets mode(s) %s on %s" "$from" "${params[1]}" "${params[0]}"
else
warn "mode line was not handled: %s" "$orig_line"
fi
}
on_NOTICE() {
info "[%s/%s] %s" "$from" "${params[0]}" "${params[1]}"
}
on_PING() {
pong "${params[1]}"
debug "received ping: %s" "${params[0]}"
}
on_PONG() {
debug "received pong: %s" "${params[1]}"
}
2021-06-15 18:32:35 +00:00
on_PRIVMSG() {
info "<%s/%s> %s" "$from" "${params[0]}" "${params[1]}"
}
2021-06-15 06:33:30 +00:00
on_QUIT() {
info "%s has disconnected: %s" "$from" "${params[0]}"
}
on_001() {
info %s "${params[1]}"
if [[ $chan ]]; then
join "$chan"
fi
while true; do
ping "row your bot gently down the stream"
sleep 10
done &
ping_pid=$!
2021-06-15 06:33:30 +00:00
}
on_002() {
info %s "${params[1]}"
}
on_003() {
info %s "${params[1]}"
}
on_004() {
debug "%s " "${params[@]:1}"
}
declare -A isupport
on_005() {
local param key value
for param in "${params[@]:1:${#params[@]}-2}"; do
IFS== read -r key value <<< "$param"
isupport[$key]=$value
debug "isupport: %s = %s" "$key" "$value"
done
}
on_250() {
info %s "${params[1]}"
}
on_251() {
info %s "${params[1]}"
}
on_252() {
info "There are %d operators online" "${params[1]}"
}
on_253() {
info "There are %d unknown connections" "${params[1]}"
}
on_254() {
info "There are %d channels formed" "${params[1]}"
}
on_255() {
info %s "${params[1]}"
}
on_265() {
info %s "${params[3]}"
}
on_266() {
info %s "${params[3]}"
}
2021-06-15 18:32:35 +00:00
on_332() {
info "topic for %s is %s" "${params[1]}" "${params[2]}"
}
on_333() {
local date
printf -v date '%(%c)T' "${params[3]}"
info "topic for %s set by %s at %s" "${params[1]}" "${params[2]}" "$date"
}
on_353() {
info "members of %s: %s" "${params[2]}" "${params[3]}"
}
on_366() {
debug "%s: end of NAMES list" "${params[1]}"
}
2021-06-15 06:33:30 +00:00
on_372() {
info %s "${params[1]}"
}
on_375() {
debug %s "${params[1]}"
}
on_376() {
debug %s "${params[1]}"
}
2021-06-15 18:32:35 +00:00
on_473() {
error "%s: %s" "${params[1]}" "${params[2]}"
}
2021-06-15 06:33:30 +00:00
###
# irc send code
###
join() {
send "JOIN %s" "$1"
}
nick() {
send "NICK %s" "$1"
info "changing nickname to %s" "$1"
}
ping() {
send "PING :%s" "$1"
}
pong() {
send "PONG %s" "$1"
}
privmsg() {
send "PRIVMSG %s :\u200b%s" "$1" "$2"
info "<%s/%s> %s" "$nick" "$1" "$2"
2021-06-15 06:33:30 +00:00
}
user() {
send "USER %s * * :%s" "$ident" "$realname"
}
###
# app hooks
2021-06-15 06:33:30 +00:00
##
hook_post_PRIVMSG_factoids() {
if [[ ${params[0]:0:1} != \# ]]; then
return 0
elif [[ ${words[0]} = "$trigger"* ]]; then
case ${words[0]:${#trigger}} in
is)
if (( ${#words[@]} < 3 )); then
return 0
fi
local key val
key=${params[1]#*"$trigger"is} key=${key# }
val=${key#* } key=${key%% *}
info "%s said in %s to remember %s as %s" "$from" "${params[0]}" "$key" "$val"
privmsg "${params[0]}" "I'm sure I'll remember that."
mkdir -p "$fact_root"/"${params[0]}"
printf %s "$val" > "$fact_root"/"${params[0]}"/"$key"
;;
isnt)
if (( ${#words[@]} < 2 )); then
return 0
fi
local key
key=${params[1]#*"$trigger"isnt} key=${key# }
if [[ -f $fact_root/${params[0]}/$key ]]; then
info "%s said in %s to delete %s" "$from" "${params[0]}" "$key"
privmsg "${params[0]}" "I forgot what that was anyways."
rm -f "$fact_root"/"${params[0]}"/"$key"
fi
;;
ls)
local facts=( "$fact_root"/"${params[0]}"/* )
privmsg "${params[0]}" "${facts[*]##*/}"
;;
*)
local key=${params[1]:1}
if [[ -f $fact_root/${params[0]}/$key ]]; then
privmsg "${params[0]}" "$from: $(<"$fact_root"/"${params[0]}"/"$key")"
fi
esac
fi
2021-06-14 23:15:49 +00:00
}
hook_post_PRIVMSG_control_panel() {
if [[ ${words[0]} = "$trigger"* ]]; then
if [[ $from != $owner && $dev != yes ]]; then
return 0
fi
local to=${params[0]}
if [[ ${params[0]:0:1} != \# ]]; then
to=$from
fi
case ${words[0]:${#trigger}} in
raw)
local cmd
cmd=${params[1]#*"$trigger"raw} cmd=${cmd# }
info "%s is executing command: %s" "$from" "$cmd"
send "$cmd"
;;
join)
join "${words[1]}"
;;
reload)
export IN_SOCK=$in_sock OUT_SOCK=$out_sock LOG_FD=$log
if [[ $tls = yes ]]; then
export SOCK_DIR=$sock_dir
export TLS_PID=$tls_pid
fi
if [[ -v ping_pid ]]; then
export PING_pid=$ping_pid
fi
privmsg "$to" "reloading..."
exec "${original_args[@]}" --reload
;;
esac
fi
}
2021-06-14 23:15:49 +00:00
###
# driver
###
if [[ $reload != yes ]]; then
nick "$nick"
user "$ident" "$realname"
fi
2021-06-15 06:33:30 +00:00
2021-06-14 23:15:49 +00:00
while recv line; do
2021-06-14 23:35:23 +00:00
params=( )
has_words=no
orig_line=$line
2021-06-14 23:23:36 +00:00
2021-06-14 23:35:23 +00:00
if [[ ${line:0:1} = : ]]; then
2021-06-14 23:23:36 +00:00
src=${line%% *} src=${src#:}
line=${line#:"$src"} line=${line# }
from=${src%@*} ident=${from#*!}
from=${from%!*} host=${src#*@}
fi
2021-06-14 23:35:23 +00:00
cmd=${line%% *}
line=${line#"$cmd"}
line=${line# }
2021-06-14 23:23:36 +00:00
while [[ $line ]]; do
2021-06-14 23:35:23 +00:00
if [[ ${line:0:1} = : ]]; then
2021-06-14 23:23:36 +00:00
params+=("${line:1}")
line=""
has_words=yes
else
2021-06-14 23:35:23 +00:00
param=${line%% *}
params+=("$param")
2021-06-14 23:23:36 +00:00
line=${line#"$param"} line=${line# }
fi
done
if [[ $has_words = yes ]]; then
read -ra words <<< "${params[@]:(-1)}"
else
words=( )
fi
skip_handler=0
2021-06-15 06:33:30 +00:00
while IFS= read -r hook; do
"$hook"
(( skip_handler |= $? ))
done < <(compgen -A function "hook_pre_${cmd^^}_")
2021-06-15 06:33:30 +00:00
2021-06-14 23:23:36 +00:00
if hash "on_${cmd^^}" 2>/dev/null; then
if (( ! skip_handler )); then
"on_${cmd^^}"
else
debug "handler for %s was skipped" "${cmd^^}"
fi
2021-06-14 23:23:36 +00:00
else
2021-06-14 23:35:23 +00:00
warn "unhandled line: %s" "$orig_line"
2021-06-14 23:23:36 +00:00
fi
while IFS= read -r hook; do
"$hook"
done < <(compgen -A function "hook_post_${cmd^^}_")
2021-06-14 23:15:49 +00:00
done