This repository has been archived on 2020-07-16. You can view files and clone it, but cannot push or open issues or pull requests.
radiobot/bot.sh

182 lines
5.4 KiB
Bash
Raw Normal View History

2018-10-23 13:44:45 +00:00
#!/bin/bash
# read configs
2018-10-23 13:44:45 +00:00
. bot.properties
input=".bot.cfg"
# save current dj info on startup
now_playing=$(<$npfile)
dj=$(parse_dj)
2018-10-27 06:11:48 +00:00
printf "Starting session: %s\n" "$(date "+[%y:%m:%d %T]")" > $log
2018-10-27 06:11:48 +00:00
function send {
printf "> %s\n" "$1" >> $log
printf "%s\r\n" "$1" >> $input
2018-10-24 19:47:52 +00:00
}
2019-05-20 16:47:23 +00:00
function msg {
send "PRIVMSG $1 :$2"
2019-05-20 16:47:23 +00:00
}
function parse_dj {
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
}
# connect and register
send "NICK $nick"
send "USER $nick 0 * :$nick"
send "MODE $nick +B"
2018-10-26 23:16:50 +00:00
2018-10-27 06:11:48 +00:00
# main loop
2020-01-17 04:52:15 +00:00
tail -f $input | telnet $server $port | while read -r buf
2018-10-23 13:44:45 +00:00
do
2020-01-17 04:52:15 +00:00
# trim \r's
line=$(printf "%b" "$buf" | tr -d $'\r')
# log the received line
printf "< %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line" >> $log
2020-01-17 04:52:15 +00:00
# now playing
if [ "$now_playing" != "$(<$npfile)" ]; then
2020-01-17 04:52:15 +00:00
now_playing=$(<$npfile)
[ -n "$dj" ] && \
msg "$channel" "\x0303$now_playing"
2020-01-17 04:52:15 +00:00
fi
# new dj!
if [ "$dj" != "$(parse_dj)" ]; then
2020-01-17 04:52:15 +00:00
dj=$(parse_dj)
if [ -n "$dj" ]; then
2020-01-17 04:52:15 +00:00
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"
2020-01-17 04:52:15 +00:00
done 10< subscribers.txt
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"
2020-01-17 04:52:15 +00:00
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"
2020-01-17 04:52:15 +00:00
done
fi
2018-10-27 06:11:48 +00:00
fi
2020-01-17 04:52:15 +00:00
# do things when you see output
case "$line" in
# respond to ping requests from the server
PING*)
msg "$(printf %s "$line" | sed "s/I/O/")"
2020-01-17 04:52:15 +00:00
;;
# make sure we're joined
*376*|*404*)
for chan in $notify_channels; do
send "JOIN #$chan"
2020-01-17 04:52:15 +00:00
done
;;
# 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/")
# "#" would mean it's a channel
if [[ $from =~ ^#.* ]]; then
2020-06-17 15:40:06 +00:00
test "$(printf %s "$line" | grep ":$nick:")" || continue
args=$(printf %s "$line" | sed -E "s/.*:$nick:(.*)/\1/")
2020-01-17 04:52:15 +00:00
else
2020-06-17 15:40:06 +00:00
args=$(printf %s "$line" | sed -E "s/.*$nick :(.*)/\1/")
2020-01-17 04:52:15 +00:00
from="$who"
fi
# trim leading and split args on space
args=( $(printf "%s" "$args" | sed -E "s/^ //") )
case "${args[0]}" in
subscribe)
if grep -q "$who" subscribers.txt; then
msg "$from" "you're already subscribed! :)"
2020-01-17 04:52:15 +00:00
else
printf "%s\n" "$who" >> subscribers.txt
msg "$from" "i'll send you a direct message when a dj starts streaming!"
2020-01-17 04:52:15 +00:00
fi
;;
unsubscribe)
sed -i "/$who/d" subscribers.txt
msg "$from" "i'll stop sending you updates."
2020-01-17 04:52:15 +00:00
;;
email-subscribe)
if grep -q "$who" email-subscribers.txt; then
msg "$from" "you're already subscribed! :)"
2020-01-17 04:52:15 +00:00
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!"
2020-01-17 04:52:15 +00:00
else
msg "$from" "please provide an address to email when a dj starts streaming"
2020-01-17 04:52:15 +00:00
fi
fi
;;
email-unsubscribe)
sed -i "/$who/d" email-subscribers.txt
msg "$from" "i'll stop sending you email updates."
2020-01-17 04:52:15 +00:00
;;
time)
msg "$from" "$(TZ=UTC date)"
2020-01-17 04:52:15 +00:00
;;
np)
msg "$from" "$now_playing"
2020-01-17 04:52:15 +00:00
;;
dj)
msg "$from" "${dj:-"no one"} is on the air now"
2020-01-17 04:52:15 +00:00
;;
link)
msg "$from" "$link"
2020-01-17 04:52:15 +00:00
;;
source)
msg "$from" "$source"
2020-01-17 04:52:15 +00:00
;;
schedule)
msg "$from" "$schedule"
2020-01-17 04:52:15 +00:00
;;
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."
2020-01-17 04:52:15 +00:00
;;
radio|slogan)
msg "$from" "$(shuf -n1 slogans.txt)"
2020-01-17 04:52:15 +00:00
;;
paymybills)
msg "$from" "whaddya mean?! i'm broker than you!"
2020-01-17 04:52:15 +00:00
;;
*) ;;
esac
;;
# else
*) ;;
esac
2018-10-23 13:44:45 +00:00
done
2019-05-20 16:47:23 +00:00