printf refactor
This commit is contained in:
parent
ffa0c279bf
commit
928da93e8a
269
bot.sh
269
bot.sh
|
@ -2,175 +2,176 @@
|
|||
|
||||
. bot.properties
|
||||
input=".bot.cfg"
|
||||
echo "Starting session: $(date "+[%y:%m:%d %T]")" > $log
|
||||
echo "NICK $nick" > $input
|
||||
echo "USER $nick 0 * :$nick" >> $input
|
||||
echo "MODE $nick +B" >> $input
|
||||
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 save_info {
|
||||
now_playing=$(<$npfile)
|
||||
}
|
||||
|
||||
function parse_dj {
|
||||
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
|
||||
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
|
||||
}
|
||||
|
||||
function msg {
|
||||
echo "PRIVMSG ${1} :${2}" >> $input
|
||||
printf "PRIVMSG %s :%s\r\n" "$1" "$2" >> $input
|
||||
}
|
||||
|
||||
# save info on startup
|
||||
save_info
|
||||
# save current dj info on startup
|
||||
now_playing=$(<$npfile)
|
||||
dj=$(parse_dj)
|
||||
|
||||
# main loop
|
||||
tail -f $input | telnet $server $port | while read res
|
||||
tail -f $input | telnet $server $port | while read -r buf
|
||||
do
|
||||
# log the session
|
||||
echo "$(date "+[%y:%m:%d %T]")$res" >> $log
|
||||
# trim \r's
|
||||
line=$(printf "%b" "$buf" | tr -d $'\r')
|
||||
printf "%s\n" "$line"
|
||||
|
||||
# now playing
|
||||
if [[ $now_playing != "$(<$npfile)" ]]; then
|
||||
save_info
|
||||
[[ ! -z "${dj}" ]] && echo -e "PRIVMSG #$channel :\x0303$now_playing" >> $input
|
||||
fi
|
||||
# log the session
|
||||
printf "%s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line" >> $log
|
||||
|
||||
# new dj!
|
||||
if [[ $dj != $(parse_dj) ]]; then
|
||||
dj=$(parse_dj)
|
||||
if [[ ! -z "${dj}" ]]; then
|
||||
for chan in $notify_channels; do
|
||||
msg "#$chan" "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done
|
||||
|
||||
while read -u 10 subscriber; do
|
||||
msg $subscriber "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done 10< subscribers.txt
|
||||
|
||||
while read -u 11 subscriber email_addr; do
|
||||
sed -e "s/<dj>/$dj/g" \
|
||||
-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
|
||||
done 11< email-subscribers.txt
|
||||
|
||||
for json in . ./club ./team; do
|
||||
TOOT_JSON_PATH=$json toot "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
|
||||
done
|
||||
# now playing
|
||||
if [[ $now_playing != "$(<$npfile)" ]]; then
|
||||
now_playing=$(<$npfile)
|
||||
[[ ! -z "${dj}" ]] && \
|
||||
printf "PRIVMSG #%s :\x0303%s\r\n" "$channel" "$now_playing" >> $input
|
||||
fi
|
||||
fi
|
||||
|
||||
# do things when you see output
|
||||
case "$res" in
|
||||
# respond to ping requests from the server
|
||||
PING*)
|
||||
echo "$res" | sed "s/I/O/" >> $input
|
||||
;;
|
||||
# new dj!
|
||||
if [[ $dj != $(parse_dj) ]]; then
|
||||
dj=$(parse_dj)
|
||||
if [[ ! -z "${dj}" ]]; then
|
||||
for chan in $notify_channels; do
|
||||
msg "#$chan" "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done
|
||||
|
||||
# make sure we're joined
|
||||
*376*|*404*)
|
||||
for chan in $notify_channels; do
|
||||
echo "JOIN #$chan" >> $input
|
||||
done
|
||||
;;
|
||||
while IFS= read -r -u 10 subscriber; do
|
||||
msg $subscriber "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done 10< subscribers.txt
|
||||
|
||||
# run when a message is seen
|
||||
*PRIVMSG*)
|
||||
echo "$res"
|
||||
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
|
||||
from=$(echo "$res" | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
|
||||
echo $who $from
|
||||
while IFS= read -r -u 11 subscriber email_addr; do
|
||||
sed -e "s/<dj>/$dj/g" \
|
||||
-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
|
||||
done 11< email-subscribers.txt
|
||||
|
||||
# "#" would mean it's a channel
|
||||
if [ "$(echo "$from" | grep '^#')" ]; then
|
||||
test "$(echo "$res" | grep ":$nick:")" || continue
|
||||
will=$(echo "$res" | perl -pe "s/.*:$nick:(.*)/\1/")
|
||||
else
|
||||
will=$(echo "$res" | perl -pe "s/.*$nick :(.*)/\1/")
|
||||
from=$who
|
||||
fi
|
||||
will=$(echo "$will" | perl -pe "s/^ //")
|
||||
com=$(echo "$will" | cut -d " " -f1)
|
||||
for json in ./*-toot.json; do
|
||||
toot --creds $json "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
case "$com" in
|
||||
subscribe)
|
||||
if grep -q $who subscribers.txt; then
|
||||
msg $from "you're already subscribed! :)"
|
||||
else
|
||||
echo $who >> subscribers.txt
|
||||
msg $from "i'll send you a direct message when a dj starts streaming!"
|
||||
fi
|
||||
;;
|
||||
# do things when you see output
|
||||
case "$line" in
|
||||
# respond to ping requests from the server
|
||||
PING*)
|
||||
printf "%s\r\n" "$line" | sed "s/I/O/" >> $input
|
||||
;;
|
||||
|
||||
unsubscribe)
|
||||
sed -i "/$who/d" subscribers.txt
|
||||
msg $from "i'll stop sending you updates."
|
||||
;;
|
||||
# make sure we're joined
|
||||
*376*|*404*)
|
||||
for chan in $notify_channels; do
|
||||
printf "JOIN #%s\r\n" "$chan" >> $input
|
||||
done
|
||||
;;
|
||||
|
||||
email-subscribe)
|
||||
if grep -q $who email-subscribers.txt; then
|
||||
msg $from "you're already subscribed! :)"
|
||||
else
|
||||
email_addr=$(echo "$will" | cut -d " " -f2)
|
||||
echo "$who $email_addr" >> email-subscribers.txt
|
||||
msg $from "i'll send you an email when a dj starts streaming!"
|
||||
fi
|
||||
;;
|
||||
# run when a message is seen
|
||||
*PRIVMSG*)
|
||||
who=$(printf "%s" "$line" | sed -E "s/:(.*)\!.*@.*/\1/")
|
||||
from=$(printf "%s" "$line" | sed -E "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
|
||||
|
||||
email-unsubscribe)
|
||||
sed -i "/$who/d" email-subscribers.txt
|
||||
msg $from "i'll stop sending you email updates."
|
||||
;;
|
||||
# "#" would mean it's a channel
|
||||
if [[ $from =~ ^#.* ]]; then
|
||||
test "$(printf "%s" "$line" | grep ":$nick:")" || continue
|
||||
args=$(printf "%s" "$line" | sed -E "s/.*:$nick:(.*)/\1/")
|
||||
else
|
||||
args=$(printf "%s" "$line" | sed -E "s/.*$nick :(.*)/\1/")
|
||||
from="$who"
|
||||
fi
|
||||
|
||||
time)
|
||||
msg $from "$(TZ=UTC date)"
|
||||
;;
|
||||
# trim leading and split args on space
|
||||
args=( $(printf "%s" "$args" | sed -E "s/^ //") )
|
||||
|
||||
np)
|
||||
msg $from "$now_playing"
|
||||
;;
|
||||
case "${args[0]}" in
|
||||
subscribe)
|
||||
if grep -q "$who" subscribers.txt; then
|
||||
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!"
|
||||
fi
|
||||
;;
|
||||
|
||||
dj)
|
||||
msg $from "${dj:-"no one"} is on the air now"
|
||||
;;
|
||||
unsubscribe)
|
||||
sed -i "/$who/d" subscribers.txt
|
||||
msg $from "i'll stop sending you updates."
|
||||
;;
|
||||
|
||||
link)
|
||||
msg $from "$link"
|
||||
;;
|
||||
email-subscribe)
|
||||
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!"
|
||||
else
|
||||
msg $from "please provide an address to email when a dj starts streaming"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
source)
|
||||
msg $from "$source"
|
||||
;;
|
||||
email-unsubscribe)
|
||||
sed -i "/$who/d" email-subscribers.txt
|
||||
msg $from "i'll stop sending you email updates."
|
||||
;;
|
||||
|
||||
schedule)
|
||||
msg $from "$schedule"
|
||||
;;
|
||||
time)
|
||||
msg $from "$(TZ=UTC date)"
|
||||
;;
|
||||
|
||||
help)
|
||||
msg $from "hey hi, my commands are subscribe, unsubscribe, np, dj, link, slogan, and schedule. if you prefer email notifications, you can use radiobot: email-subscribe you@example.com"
|
||||
;;
|
||||
np)
|
||||
msg $from "$now_playing"
|
||||
;;
|
||||
|
||||
radio|slogan)
|
||||
msg $from "$(shuf -n1 slogans.txt)"
|
||||
;;
|
||||
dj)
|
||||
msg $from "${dj:-"no one"} is on the air now"
|
||||
;;
|
||||
|
||||
paymybills)
|
||||
msg $from "whaddya mean?! i'm broker than you!"
|
||||
;;
|
||||
link)
|
||||
msg $from "$link"
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "no command: $res"
|
||||
;;
|
||||
source)
|
||||
msg $from "$source"
|
||||
;;
|
||||
|
||||
esac
|
||||
;;
|
||||
schedule)
|
||||
msg $from "$schedule"
|
||||
;;
|
||||
|
||||
# else
|
||||
*)
|
||||
echo "$res"
|
||||
;;
|
||||
esac
|
||||
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."
|
||||
;;
|
||||
|
||||
radio|slogan)
|
||||
msg $from "$(shuf -n1 slogans.txt)"
|
||||
;;
|
||||
|
||||
paymybills)
|
||||
msg $from "whaddya mean?! i'm broker than you!"
|
||||
;;
|
||||
|
||||
*) ;;
|
||||
|
||||
esac
|
||||
;;
|
||||
|
||||
# else
|
||||
*) ;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
|
Reference in New Issue