2018-10-23 13:44:45 +00:00
#!/bin/bash
. bot.properties
input = ".bot.cfg"
2019-07-10 05:01:28 +00:00
echo " Starting session: $( date "+[%y:%m:%d %T]" ) " > $log
2018-10-23 13:44:45 +00:00
echo " NICK $nick " > $input
2019-05-17 14:34:45 +00:00
echo " USER $nick 0 * : $nick " >> $input
2018-10-30 21:36:00 +00:00
echo " MODE $nick +B " >> $input
2018-10-23 13:44:45 +00:00
2018-10-27 06:11:48 +00:00
function save_info {
now_playing = $( <$npfile )
}
2018-10-24 19:47:52 +00:00
function parse_dj {
2018-10-27 06:11:48 +00:00
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
2018-10-24 19:47:52 +00:00
}
2019-05-20 16:47:23 +00:00
function msg {
echo " PRIVMSG ${ 1 } : ${ 2 } " >> $input
}
2019-07-10 05:01:28 +00:00
# save info on startup
2018-10-27 06:11:48 +00:00
save_info
2018-10-26 23:16:50 +00:00
dj = $( parse_dj)
2018-10-27 06:11:48 +00:00
# main loop
2018-10-23 13:44:45 +00:00
tail -f $input | telnet $server $port | while read res
do
# log the session
echo " $( date "+[%y:%m:%d %T]" ) $res " >> $log
2019-07-10 05:01:28 +00:00
# now playing
if [ [ $now_playing != " $( <$npfile ) " ] ] ; then
save_info
[ [ ! -z " ${ dj } " ] ] && echo -e " PRIVMSG # $channel :\x0303 $now_playing " >> $input
fi
2018-10-27 06:11:48 +00:00
# new dj!
if [ [ $dj != $( parse_dj) ] ] ; then
dj = $( parse_dj)
if [ [ ! -z " ${ dj } " ] ] ; then
2019-05-17 15:01:54 +00:00
for chan in $notify_channels ; do
2019-07-10 05:01:28 +00:00
msg " # $chan " " $dj is now online playing $now_playing ! tune in now here: $link "
2019-05-17 15:01:54 +00:00
done
2018-11-18 02:12:12 +00:00
2018-10-30 21:28:33 +00:00
while read -u 10 subscriber; do
2019-07-10 05:01:28 +00:00
msg $subscriber " $dj is now online playing $now_playing ! tune in now here: $link "
2018-10-30 21:28:33 +00:00
done 10< subscribers.txt
2019-05-17 15:01:54 +00:00
2019-07-10 04:24:43 +00:00
while read -u 11 subscriber email_addr; do
2019-07-10 05:01:28 +00:00
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 \
2019-07-10 04:33:44 +00:00
| sendmail $email_addr
2019-07-10 04:24:43 +00:00
done 11< email-subscribers.txt
2019-05-17 15:01:54 +00:00
for json in . ./yourtilde ./team; do
TOOT_JSON_PATH = $json toot " dj $dj is now streaming live on https://tilderadio.org! tune in here: $link "
done
2018-10-27 06:11:48 +00:00
fi
2018-10-23 13:44:45 +00:00
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
; ;
2018-10-23 14:10:20 +00:00
# make sure we're joined
*376*| *404*)
2019-05-17 15:01:54 +00:00
for chan in $notify_channels ; do
echo " JOIN # $chan " >> $input
done
2018-10-23 14:10:20 +00:00
; ;
2018-10-30 21:28:33 +00:00
# run when a message is seen
2018-10-30 22:03:40 +00:00
*PRIVMSG*)
2018-10-30 21:28:33 +00:00
echo " $res "
who = $( echo " $res " | perl -pe "s/:(.*)\!.*@.*/\1/" )
from = $( echo " $res " | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/" )
2018-10-30 21:36:00 +00:00
echo $who $from
2018-10-30 21:28:33 +00:00
# "#" would mean it's a channel
if [ " $( echo " $from " | grep '^#' ) " ] ; then
test " $( echo " $res " | grep " : $nick : " ) " || continue
2018-10-30 21:39:10 +00:00
will = $( echo " $res " | perl -pe " s/.*: $nick :(.*)/\1/ " )
2018-10-30 21:28:33 +00:00
else
2018-10-30 21:39:10 +00:00
will = $( echo " $res " | perl -pe " s/.* $nick :(.*)/\1/ " )
2018-10-30 21:28:33 +00:00
from = $who
fi
2018-10-30 21:39:10 +00:00
will = $( echo " $will " | perl -pe "s/^ //" )
2018-10-30 21:28:33 +00:00
com = $( echo " $will " | cut -d " " -f1)
case " $com " in
subscribe)
2019-05-17 15:01:54 +00:00
if grep -q $who subscribers.txt; then
2019-05-20 16:47:23 +00:00
msg $from "you're already subscribed! :)"
2019-05-17 15:01:54 +00:00
else
echo $who >> subscribers.txt
2019-05-20 16:47:23 +00:00
msg $from "i'll send you a direct message when a dj starts streaming!"
2019-05-17 15:01:54 +00:00
fi
2018-10-30 21:28:33 +00:00
; ;
unsubscribe)
2018-10-30 22:09:42 +00:00
sed -i " / $who /d " subscribers.txt
2019-05-20 16:47:23 +00:00
msg $from "i'll stop sending you updates."
2018-10-30 21:28:33 +00:00
; ;
2019-07-10 04:24:43 +00:00
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
; ;
email-unsubscribe)
sed -i " / $who /d " email-subscribers.txt
msg $from "i'll stop sending you email updates."
; ;
2019-02-17 20:05:21 +00:00
time )
2019-05-20 16:47:23 +00:00
msg $from " $( TZ = UTC date) "
2019-02-17 20:05:21 +00:00
; ;
2018-10-30 21:28:33 +00:00
np)
2019-05-20 16:47:23 +00:00
msg $from " $now_playing "
2018-10-30 21:28:33 +00:00
; ;
dj)
2019-05-20 16:47:23 +00:00
msg $from " ${ dj :- "no one" } is on the air now "
2018-10-30 21:28:33 +00:00
; ;
2019-02-18 22:59:44 +00:00
link)
2019-05-20 16:47:23 +00:00
msg $from " $link "
; ;
2019-05-25 04:05:17 +00:00
source )
msg $from " $source "
; ;
2019-05-20 16:47:23 +00:00
schedule)
msg $from " $schedule "
2019-02-18 22:59:44 +00:00
; ;
2018-10-30 21:36:00 +00:00
help )
2019-07-10 04:24:43 +00:00
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"
2018-10-30 21:36:00 +00:00
; ;
2019-05-17 15:01:54 +00:00
radio| slogan)
2019-07-10 04:38:19 +00:00
msg $from " $( shuf -n1 slogans.txt) "
2018-10-31 04:33:09 +00:00
; ;
paymybills)
2019-05-20 16:47:23 +00:00
msg $from "whaddya mean?! i'm broker than you!"
2018-10-31 04:33:09 +00:00
; ;
2018-10-30 21:36:00 +00:00
*)
echo " no command: $res "
; ;
2018-10-30 21:28:33 +00:00
esac
; ;
2018-10-23 13:44:45 +00:00
# else
*)
echo " $res "
; ;
esac
done
2019-05-20 16:47:23 +00:00