#!/usr/bin/env bash # read configs . bot.properties # not registered and identified reg="" # reset input file rm input.txt && touch input.txt printf "starting %s: %s\n" "$nick" "$(date "+[%y:%m:%d %T]")" function send { printf "> %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$1" printf "%b\r\n" "$1" >> input.txt } function msg { send "PRIVMSG $1 :$2" } function parse_dj { grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs } # save current dj info on startup now_playing=$(<$npfile) dj=$(parse_dj) # connect and register send "NICK $nick" send "USER $nick 0 * :$nick" send "MODE $nick +B" # main loop tail -f input.txt | telnet $server $port | while read -r buf do # trim \r's line=$(printf %b "$buf" | tr -d $'\r') # log the received line printf "< %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line" # now playing if [ "$now_playing" != "$(<$npfile)" ]; then now_playing=$(<$npfile) # print the new song if a dj is streaming if [ -n "$dj" ]; then msg "#$channel" "\x0303$now_playing" fi fi # new dj! if [ "$dj" != "$(parse_dj)" ]; then dj=$(parse_dj) if [ -n "$dj" ]; then 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" done 10< subscribers.txt while read -r -u 11 subscriber email_addr; do sed -e "s//$dj/g" \ -e "s||$link|g" \ -e "s//$now_playing/g" \ -e "s//$subscriber <$email_addr>/g" dj-online-email.tmpl \ | sendmail "$email_addr" done 11< email-subscribers.txt for json in ./*-toot.json; do toot --creds "$json" "dj $dj is now streaming live on #tilderadio! tune in here: $link" done fi fi # do things when you see output case "$line" in # respond to ping requests from the server PING*) send "$(printf %s "$line" | sed "s/I/O/")" ;; # 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" ;; # make sure we're joined *376*) # RPL_ENDOFMOTD for chan in $notify_channels; do send "JOIN #$chan" 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 printf %s "$from" | grep -q "^#"; then # channel messages need to contain our nick printf %s "$line" | grep -q ":$nick:" || continue trailing=$(printf %s "$line" | sed -E "s/.*:$nick:(.*)/\1/") else trailing=$(printf %s "$line" | sed -E "s/.*$nick :(.*)/\1/") from="$who" fi # trim leading and split args on space IFS=" " read -r -a args <<< "$(printf %s "$trailing" | sed -E "s/^ //")" 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 ;; unsubscribe) sed -i"" "/$who/d" subscribers.txt msg "$from" "i'll stop sending you updates." ;; 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 ;; email-unsubscribe) sed -i"" "/$who/d" email-subscribers.txt msg "$from" "i'll stop sending you email updates." ;; time|date|now) msg "$from" "UTC time is currently $(LANG=C TZ=UTC date)" ;; np) msg "$from" "$now_playing" ;; dj) msg "$from" "${dj:-"no one"} is on the air now" ;; link) msg "$from" "$link" ;; source) msg "$from" "$source" ;; schedule|upnext|un) # get next scheduled streaming time json=$(curl -s "https://radio.tildeverse.org/api/station/1/schedule?rows=1" | jq ".[]") nextdj=$(jq -r .name <<< "$json") nexttime=$(LANG=C TZ=UTC date -d "$(jq -r .start <<< "$json")") 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); }') msg "$from" "$schedule - $nextdj will be on at $nexttime (in $timediff)" ;; help) 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." ;; radio|slogan) msg "$from" "$(shuf -n1 slogans.txt)" ;; paymybills) msg "$from" "whaddya mean?! i'm broker than you!" ;; *) ;; esac ;; # else *) ;; esac done