2020-06-17 17:39:05 +00:00
#!/usr/bin/env bash
2018-10-23 13:44:45 +00:00
2020-06-17 15:37:15 +00:00
# read configs
2018-10-23 13:44:45 +00:00
. bot.properties
2020-06-17 19:31:24 +00:00
# not registered and identified
reg = ""
2020-06-21 03:09:19 +00:00
# reset input file
rm input.txt && touch input.txt
2020-06-17 17:39:05 +00:00
printf "starting %s: %s\n" " $nick " " $( date "+[%y:%m:%d %T]" ) "
2018-10-27 06:11:48 +00:00
2020-06-17 15:37:15 +00:00
function send {
2020-06-17 17:39:05 +00:00
printf "> %s: %s\n" " $( date "+[%y:%m:%d %T]" ) " " $1 "
2020-06-18 06:03:47 +00:00
printf "%b\r\n" " $1 " >> input.txt
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
}
2020-06-17 17:39:05 +00:00
# save current dj info on startup
now_playing = $( <$npfile )
dj = $( parse_dj)
2020-06-17 15:37:15 +00:00
# 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-06-17 17:39:05 +00:00
tail -f input.txt | 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
2020-06-17 17:39:05 +00:00
line = $( printf %b " $buf " | tr -d $'\r' )
2020-06-18 04:51:39 +00:00
2020-06-17 15:37:15 +00:00
# log the received line
2020-06-17 17:39:05 +00:00
printf "< %s: %s\n" " $( date "+[%y:%m:%d %T]" ) " " $line "
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-18 04:51:39 +00:00
# print the new song if a dj is streaming
if [ -n " $dj " ] ; then
msg " # $channel " " \x0303 $now_playing "
fi
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
2020-06-27 19:21:16 +00:00
while read -r -u 11 subscriber email_addr; do
2020-01-17 04:52:15 +00:00
sed -e " s/<dj>/ $dj /g " \
-e " s|<link>| $link |g " \
-e " s/<now_playing>/ $now_playing /g " \
2020-06-27 19:21:16 +00:00
-e " s/<email_addr>/ $subscriber < $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-19 14:10:49 +00:00
toot --creds " $json " " dj $dj is now streaming live on #tilderadio! 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:49:34 +00:00
send " $( printf %s " $line " | sed "s/I/O/" ) "
2020-01-17 04:52:15 +00:00
; ;
2020-06-17 19:31:24 +00:00
# log in to services
*001*) # RPL_WELCOME
if [ -z " $reg " ] && [ -f account.ini ] ; then
. account.ini
send " SQUERY NickServ identify $account $password "
fi
; ;
# grab account name if authentication successful
*900*) # RPL_LOGGEDIN
reg = $( printf %s " $line " | sed -E "s/.* (.*) :.*/\1/" )
printf "successfully logged in as '%s'\n" " $reg "
; ;
2020-01-17 04:52:15 +00:00
# make sure we're joined
2020-06-17 19:31:24 +00:00
*376*) # RPL_ENDOFMOTD
2020-01-17 04:52:15 +00:00
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
2020-06-17 17:57:36 +00:00
if printf %s " $from " | grep -q "^#" ; then
2020-06-17 19:31:24 +00:00
# channel messages need to contain our nick
2020-06-17 17:57:36 +00:00
printf %s " $line " | grep -q " : $nick : " || continue
trailing = $( printf %s " $line " | sed -E " s/.*: $nick :(.*)/\1/ " )
2020-01-17 04:52:15 +00:00
else
2020-06-17 17:57:36 +00:00
trailing = $( 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
2020-06-17 17:57:36 +00:00
IFS = " " read -r -a args <<< " $( printf %s " $trailing " | sed -E "s/^ //" ) "
2020-01-17 04:52:15 +00:00
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)
2020-06-17 17:39:05 +00:00
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)
2020-06-17 17:39:05 +00:00
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
; ;
2020-06-23 00:46:21 +00:00
time| date| now)
2020-06-27 23:35:15 +00:00
msg " $from " " UTC time is currently $( LANG = C 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
; ;
2020-06-27 23:27:36 +00:00
schedule| upnext| un)
2020-06-23 00:32:42 +00:00
# get next scheduled streaming time
json = $( curl -s "https://radio.tildeverse.org/api/station/1/schedule?rows=1" | jq ".[]" )
2020-06-23 00:46:21 +00:00
nextdj = $( jq -r .name <<< " $json " )
2020-06-27 23:35:15 +00:00
nexttime = $( LANG = C TZ = UTC date -d " $( jq -r .start <<< " $json " ) " )
2020-06-28 16:45:07 +00:00
nexttimesecs = $( LANG = C TZ = UTC date -d " $nexttime " +%s)
timediff = $( TZ = UTC awk -v d = " $nexttimesecs " 'BEGIN { then=d; now=systime(); print strftime("%Hh %Mm", then - now); }' )
2020-06-28 15:42:33 +00:00
msg " $from " " $schedule - $nextdj will be on at $nexttime (in $timediff ) "
2020-01-17 04:52:15 +00:00
; ;
help )
2020-06-23 00:46:21 +00:00
msg " $from " "hey hi, my commands are np, dj, link, slogan, schedule, time, [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