stormbot/bot.sh

95 lines
3.0 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
*"JOIN :#"*)
2016-09-02 12:51:56 -04:00
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
if [ "${greet^^}" = "TRUE" ]; then
./triggers/greet/greet.sh $who $chan
fi
;;
2016-09-04 12:43:22 -04:00
# run when someone leaves
*"PART #"*)
2016-09-04 12:43:22 -04:00
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
if [ "${leave^^}" = "TRUE" ]; then
./triggers/bye/bye.sh $who $chan
fi
2016-09-04 12:43:22 -04:00
;;
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
2016-09-04 17:22:05 -04:00
./triggers/link/link.sh "$who" "$from" "$res"
# Although this calls modules, it triggers on text other than the bot's nick
elif [[ "$res" =~ *PRIVMSG\ \#${from#*#}\ :$triggers* ]]; then
com"${res#*:$triggers}"
com"${com//# /}"
2016-09-04 17:22:05 -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 "${com#* }"
2016-09-04 12:43:22 -04:00
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
will=$(echo "$will" | perl -pe "s/^ +//")
2013-07-22 12:16:31 -04:00
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