infra/pi/tbot.sh

99 lines
3.3 KiB
Bash
Raw Normal View History

2024-10-26 01:16:48 +02:00
bgpids="$$"
function msg() {
echo "PRIVMSG $@" >> /tmp/$pid.in
}
pid=$$
rm /tmp/$pid.in /tmp/$pid.out
touch /tmp/$pid.in /tmp/$pid.out
tail -f /tmp/$pid.in | telnet $@ 6667 > /tmp/$pid.out &
tnpid=$!
bgpids="$bgpids $!"
tail -f /tmp/$pid.out | (
# Receiver
while true; do
read -r line
line=$(tr -d "*" <<< "$line")
echo "$line"
IFS=' ' read -ra split <<< "$line"
msg=""
for ((i=3; i<${#split[@]}; i++)) ; do
msg="$msg ${split[$i]}"
done
msg="${msg:2}"
channel="${split[2]}"
echo GOT MSG "$msg" in "$channel"
read -ra cmd <<< "$msg"
for arg in ${cmd[@]} ; do
case "$arg" in http*)
url=$(curl -H 'Content-Type: application/json' -H 'Authorization: Bearer 801fae8344e9c65b112a2acef9bdeeec9335b297' -X POST -d "{\"long_url\":\"$arg\"}" https://api-ssl.bitly.com/v4/shorten)
url=$(sed -E "s/^.*\"link\":\"//" <<< $url | sed -E "s/\".*$//g")
msg $channel ":I shortened that URL for you: $url"
;; esac
done
case "$msg" in
"Nickname is already in use.")
if [ $channel = "URLBot" ] ; then
echo "QUIT :Stopping." >> /tmp/$pid.in
sleep 1
kill $(cat /tmp/$pid.pids) >& /dev/null &
exit 0
fi
;;
"-disconnect")
#echo "QUIT :Stopping." >> /tmp/$pid.in
#sleep 1
#kill $(cat /tmp/$pid.pids) >& /dev/null &
#exit 0
;;
-join\ *)
msg $channel :Joining "${cmd[1]}"
echo "JOIN ${cmd[1]}" >> /tmp/$pid.in
msg "${cmd[1]}" ":This is a bot to shorten URLs on IRC so console users who can't copy/paste can easily access URLs. You can make it leave using -leave, and you can make it rejoin by going to ###urlbot and typing -join #channel."
;;
"-test")
msg $channel 'OK!'
;;
"-sandwich")
msg $channel 'Spaghetti'
;;
"-leave")
if [ "$channel" = '###urlbot' ]; then
msg $channel ':I will not leave the ###urlbot channel.'
else
msg $channel ':Goodbye.'
echo "PART $channel" >> /tmp/$pid.in
fi
;;
"URLBot")
msg $channel ":URLBot commands - They also work in /msg, but the bot won't respond that way."
msg $channel ':-join <channel> : Makes the bot join a channel'
msg $channel ':-test : Makes the bot reply "OK!"'
msg $channel ':-leave : Makes the bot leave this channel'
;;
esac
done
) &
bgpids="$bgpids $!"
(
while kill -0 $pid $tnpid >& /dev/null; do
sleep 20
echo "PONG $@" >> /tmp/$pid.in
done
echo "QUIT Stopping." >> /tmp/$pid.in
sleep 1
kill $(cat /tmp/$pid.pids) >& /dev/null &
exit 0
) &
bgpids="$bgpids $!"
echo "$bgpids" > /tmp/$pid.pids
sleep 2
echo "USER URLBot * * ." >> /tmp/$pid.in
echo "NICK URLBot" >> /tmp/$pid.in
sleep 2
echo "JOIN ###urlbot" >> /tmp/$pid.in
while true; do sleep 1; done