2018-10-23 13:44:45 +00:00
#!/bin/bash
2020-06-17 15:37:15 +00:00
# read configs
2018-10-23 13:44:45 +00:00
. bot.properties
input = ".bot.cfg"
2020-06-17 15:37:15 +00:00
# save current dj info on startup
now_playing = $( <$npfile )
dj = $( parse_dj)
2018-10-27 06:11:48 +00:00
2020-06-17 15:37:15 +00:00
printf "Starting session: %s\n" " $( date "+[%y:%m:%d %T]" ) " > $log
2018-10-27 06:11:48 +00:00
2020-06-17 15:37:15 +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 {
2020-06-17 15:37:15 +00:00
send " PRIVMSG $1 : $2 "
2019-05-20 16:47:23 +00:00
}
2020-06-17 15:37:15 +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' )
2020-06-17 15:37:15 +00:00
# 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
2020-06-17 15:37:15 +00:00
if [ " $now_playing " != " $( <$npfile ) " ] ; then
2020-01-17 04:52:15 +00:00
now_playing = $( <$npfile )
2020-06-17 15:37:15 +00:00
[ -n " $dj " ] && \
msg " $channel " " \x0303 $now_playing "
2020-01-17 04:52:15 +00:00
fi
# new dj!
2020-06-17 15:37:15 +00:00
if [ " $dj " != " $( parse_dj) " ] ; then
2020-01-17 04:52:15 +00:00
dj = $( parse_dj)
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
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 \
2020-06-17 15:37:15 +00:00
| sendmail " $email_addr "
2020-01-17 04:52:15 +00:00
done 11< email-subscribers.txt
for json in ./*-toot.json; do
2020-06-17 15:37:15 +00:00
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*)
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
msg " $from " "you're already subscribed! :)"
2020-01-17 04:52:15 +00:00
else
printf "%s\n" " $who " >> subscribers.txt
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
msg " $from " "i'll stop sending you updates."
2020-01-17 04:52:15 +00:00
; ;
email-subscribe)
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
msg " $from " "i'll send you an email when a dj starts streaming!"
2020-01-17 04:52:15 +00:00
else
2020-06-17 15:37:15 +00:00
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
2020-06-17 15:37:15 +00:00
msg " $from " "i'll stop sending you email updates."
2020-01-17 04:52:15 +00:00
; ;
time )
2020-06-17 15:37:15 +00:00
msg " $from " " $( TZ = UTC date) "
2020-01-17 04:52:15 +00:00
; ;
np)
2020-06-17 15:37:15 +00:00
msg " $from " " $now_playing "
2020-01-17 04:52:15 +00:00
; ;
dj)
2020-06-17 15:37:15 +00:00
msg " $from " " ${ dj :- "no one" } is on the air now "
2020-01-17 04:52:15 +00:00
; ;
link)
2020-06-17 15:37:15 +00:00
msg " $from " " $link "
2020-01-17 04:52:15 +00:00
; ;
source )
2020-06-17 15:37:15 +00:00
msg " $from " " $source "
2020-01-17 04:52:15 +00:00
; ;
schedule)
2020-06-17 15:37:15 +00:00
msg " $from " " $schedule "
2020-01-17 04:52:15 +00:00
; ;
help )
2020-06-17 15:37:15 +00:00
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)
2020-06-17 15:37:15 +00:00
msg " $from " " $( shuf -n1 slogans.txt) "
2020-01-17 04:52:15 +00:00
; ;
paymybills)
2020-06-17 15:37:15 +00:00
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