shellcheck: quote variables and create send() fn

This commit is contained in:
Ben Harris 2020-06-17 11:37:15 -04:00
parent 45364a3ad0
commit 118ec4e189
1 changed files with 46 additions and 42 deletions

88
bot.sh
View File

@ -1,54 +1,58 @@
#!/bin/bash
# read configs
. bot.properties
input=".bot.cfg"
# save current dj info on startup
now_playing=$(<$npfile)
dj=$(parse_dj)
printf "Starting session: %s\n" "$(date "+[%y:%m:%d %T]")" > $log
# connect and register
printf "NICK %s\r\n" "$nick" > $input
printf "USER %s 0 * :%s\r\n" "$nick" "$nick" >> $input
printf "MODE %s +B\r\n" "$nick" >> $input
function send {
printf "> %s\n" "$1" >> $log
printf "%s\r\n" "$1" >> $input
}
function msg {
send "PRIVMSG $1 :$2"
}
function parse_dj {
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
}
function msg {
printf "PRIVMSG %s :%s\r\n" "$1" "$2" >> $input
}
# save current dj info on startup
now_playing=$(<$npfile)
dj=$(parse_dj)
# connect and register
send "NICK $nick"
send "USER $nick 0 * :$nick"
send "MODE $nick +B"
# main loop
tail -f $input | telnet $server $port | while read -r buf
do
# trim \r's
line=$(printf "%b" "$buf" | tr -d $'\r')
printf "%s\n" "$line"
# log the session
printf "%s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line" >> $log
# log the received line
printf "< %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line" >> $log
# now playing
if [[ $now_playing != "$(<$npfile)" ]]; then
if [ "$now_playing" != "$(<$npfile)" ]; then
now_playing=$(<$npfile)
[[ ! -z "${dj}" ]] && \
printf "PRIVMSG #%s :\x0303%s\r\n" "$channel" "$now_playing" >> $input
[ -n "$dj" ] && \
msg "$channel" "\x0303$now_playing"
fi
# new dj!
if [[ $dj != $(parse_dj) ]]; then
if [ "$dj" != "$(parse_dj)" ]; then
dj=$(parse_dj)
if [[ ! -z "${dj}" ]]; then
if [ -n "$dj" ]; then
for chan in $notify_channels; do
msg "#$chan" "$dj is now online playing $now_playing! tune in now here: $link"
done
while IFS= read -r -u 10 subscriber; do
msg $subscriber "$dj is now online playing $now_playing! tune in now here: $link"
msg "$subscriber" "$dj is now online playing $now_playing! tune in now here: $link"
done 10< subscribers.txt
while IFS= read -r -u 11 subscriber email_addr; do
@ -56,11 +60,11 @@ do
-e "s|<link>|$link|g" \
-e "s/<now_playing>/$now_playing/g" \
-e "s/<email_addr>/$email_addr/g" dj-online-email.tmpl \
| sendmail $email_addr
| sendmail "$email_addr"
done 11< email-subscribers.txt
for json in ./*-toot.json; do
toot --creds $json "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
toot --creds "$json" "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
done
fi
fi
@ -69,13 +73,13 @@ do
case "$line" in
# respond to ping requests from the server
PING*)
printf "%s\r\n" "$line" | sed "s/I/O/" >> $input
msg "$(printf %s "$line" | sed "s/I/O/")"
;;
# make sure we're joined
*376*|*404*)
for chan in $notify_channels; do
printf "JOIN #%s\r\n" "$chan" >> $input
send "JOIN #$chan"
done
;;
@ -99,70 +103,70 @@ do
case "${args[0]}" in
subscribe)
if grep -q "$who" subscribers.txt; then
msg $from "you're already subscribed! :)"
msg "$from" "you're already subscribed! :)"
else
printf "%s\n" "$who" >> subscribers.txt
msg $from "i'll send you a direct message when a dj starts streaming!"
msg "$from" "i'll send you a direct message when a dj starts streaming!"
fi
;;
unsubscribe)
sed -i "/$who/d" subscribers.txt
msg $from "i'll stop sending you updates."
msg "$from" "i'll stop sending you updates."
;;
email-subscribe)
if grep -q $who email-subscribers.txt; then
msg $from "you're already subscribed! :)"
if grep -q "$who" email-subscribers.txt; then
msg "$from" "you're already subscribed! :)"
else
if (( "${#args[1]}" > 0 )); then
printf "%s %s\n" "$who" "${args[1]}" >> email-subscribers.txt
msg $from "i'll send you an email when a dj starts streaming!"
msg "$from" "i'll send you an email when a dj starts streaming!"
else
msg $from "please provide an address to email when a dj starts streaming"
msg "$from" "please provide an address to email when a dj starts streaming"
fi
fi
;;
email-unsubscribe)
sed -i "/$who/d" email-subscribers.txt
msg $from "i'll stop sending you email updates."
msg "$from" "i'll stop sending you email updates."
;;
time)
msg $from "$(TZ=UTC date)"
msg "$from" "$(TZ=UTC date)"
;;
np)
msg $from "$now_playing"
msg "$from" "$now_playing"
;;
dj)
msg $from "${dj:-"no one"} is on the air now"
msg "$from" "${dj:-"no one"} is on the air now"
;;
link)
msg $from "$link"
msg "$from" "$link"
;;
source)
msg $from "$source"
msg "$from" "$source"
;;
schedule)
msg $from "$schedule"
msg "$from" "$schedule"
;;
help)
msg $from "hey hi, my commands are np, dj, link, slogan, schedule, [un]subscribe, email-[un]subscribe. please provide an address when subscribing via email."
msg "$from" "hey hi, my commands are np, dj, link, slogan, schedule, [un]subscribe, email-[un]subscribe. please provide an address when subscribing via email."
;;
radio|slogan)
msg $from "$(shuf -n1 slogans.txt)"
msg "$from" "$(shuf -n1 slogans.txt)"
;;
paymybills)
msg $from "whaddya mean?! i'm broker than you!"
msg "$from" "whaddya mean?! i'm broker than you!"
;;
*) ;;