stormbot/bot.sh

82 lines
2.5 KiB
Bash
Raw Normal View History

2013-07-19 12:53:40 -04:00
#!/bin/bash
2013-07-16 18:19:17 -04:00
[ -f functions.sh ] && source functions.sh
2016-09-02 10:58:05 -04:00
[ -f bot.properties ] && source bot.properties
2013-07-19 12:53:40 -04:00
input=".bot.cfg"
2016-09-02 12:51:56 -04:00
echo "Starting session: $(date "+[%y:%m:%d %T]")" | tee $log
2013-07-19 12:53:40 -04:00
echo "NICK $nick" > $input
echo "USER $user" >> $input
2016-09-02 10:58:05 -04:00
for c in ${channel[@]} ; do
echo "JOIN #$c" | tee -a $input
done
2013-07-16 18:19:17 -04:00
2016-09-02 10:58:05 -04:00
tail -f $input | telnet $server $port | while read res
2013-07-16 21:17:56 -04:00
do
2013-07-22 13:29:05 -04:00
# log the session
2016-09-02 12:51:56 -04:00
echo "$(date "+[%y:%m:%d %T]")$res" | tee -a $log
2013-07-16 21:17:56 -04:00
# do things when you see output
2013-07-16 18:19:17 -04:00
case "$res" in
2013-07-17 16:11:00 -04:00
# respond to ping requests from the server
2013-07-16 18:19:17 -04:00
PING*)
2013-07-19 12:53:40 -04:00
echo "$res" | sed "s/I/O/" >> $input
2013-07-16 18:19:17 -04:00
;;
2013-07-16 21:17:56 -04:00
# for pings on nick/user
2013-07-16 18:19:17 -04:00
*"You have not"*)
2013-07-19 12:53:40 -04:00
echo "JOIN #$channel" >> $input
2013-07-16 18:19:17 -04:00
;;
2013-07-17 16:11:00 -04:00
# run when someone joins
2016-09-02 12:51:56 -04:00
*JOIN*)
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
chan="$(echo "$res" | cut -d '#' -f2)"
chan="#$chan"
if [ "$who" = "$nick" ]; then
continue
fi
2013-07-19 12:53:40 -04:00
echo "MODE #$channel +o $who" >> $input
./triggers/greet/greet.sh $who $chan
;;
2016-09-04 12:43:22 -04:00
# run when someone leaves
*PART #*)
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
chan="$(echo "$res" | cut -d '#' -f2)"
chan="#$chan"
if [ "$who" = "$nick" ]; then
continue
fi
echo "MODE #$channel +o $who" >> $input
./triggers/greet/bye.sh $who $chan
;;
2013-07-17 16:11:00 -04:00
# run when a message is seen
*PRIVMSG*)
echo "$res"
2013-07-19 12:53:40 -04:00
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
from=$(echo "$res" | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
2016-09-04 12:43:22 -04:00
# This looks to be the spot where triggers should be called
# Call link trigger if msg contains a link:
if [[ "$res" =~ .*http://|https://|www\..* ]]; then
msg "$from" "This contains a url. Unfortunately my master is being lazy and hasn't made a url parser, so I can't tell you where it goes."
fi
# "#" would mean it's a channel
2016-09-04 12:43:22 -04:00
if [ "$(echo "$from" | grep '#')" ]; then
test "$(echo "$res" | grep ":$nick:")" || continue
2013-07-19 12:53:40 -04:00
will=$(echo "$res" | perl -pe "s/.*:$nick:(.*)/\1/")
else
2013-07-19 12:53:40 -04:00
will=$(echo "$res" | perl -pe "s/.*$nick :(.*)/\1/")
from=$who
fi
2013-07-22 12:16:31 -04:00
will=$(echo "$will" | perl -pe "s/^ //")
com=$(echo "$will" | cut -d " " -f1)
2016-09-04 12:43:22 -04:00
if [ -z "$(ls modules/ | grep -i -- "$com")" ] || [ -z "$com" ]; then
./modules/help/help.sh $who $from
continue
fi
./modules/$com/$com.sh $who $from $(echo "$will" | cut -d " " -f2-99)
2013-07-16 18:19:17 -04:00
;;
*)
2016-09-04 12:43:22 -04:00
chan="$(echo "$res" | cut -d '#' -f2)"
chan="#$chan"
2013-07-16 18:19:17 -04:00
echo "$res"
;;
esac
done