stormbot/bot.sh

131 lines
3.3 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
[ -f bot.cfg ] && source bot.cfg
2016-09-05 21:44:04 -04:00
input=".botinput"
close_bot()
{
echo -en "QUIT :$quitMessage\r\n" >> "$input"
rm "$input"
exit 0
2016-09-05 21:44:04 -04:00
}
trap close_bot EXIT $?
echo "Session started $(date "+%I:%M%p%n %A, %B %d, %Y")" | tee "$log"
echo "NICK $nick" | tee "$input"
echo "USER $user" | tee -a "$input"
2016-09-02 10:58:05 -04:00
for c in ${channel[@]} ; do
echo "JOIN #$c" | tee -a "$input"
sleep 0.5
2016-09-02 10:58:05 -04:00
done
2013-07-16 18:19:17 -04:00
tail -f "$input" | telnet "$server" "$port" | while read result
2013-07-16 21:17:56 -04:00
do
2013-07-22 13:29:05 -04:00
# log the session
echo "$(date "+[$dateFormat]") $result" | tee -a "$log"
2013-07-16 21:17:56 -04:00
# do things when you see output
case "$result" 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*)
echo "$result" | tee -a "$log"
echo "${result/I/O}" | tee -a "$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"*)
echo "JOIN #$channel" | tee -a "$input"
2013-07-16 18:19:17 -04:00
;;
2013-07-17 16:11:00 -04:00
# run when someone joins
*"JOIN :#"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
2016-09-02 12:51:56 -04:00
if [ "$who" = "$nick" ]; then
continue
fi
echo "MODE #$channel +o $who" | tee -a "$input"
if [ "${greet^^}" = "TRUE" ]; then
2017-04-02 10:41:28 -04:00
set -f
./triggers/greet/greet.sh "$who" "$from"
set +f
fi
;;
2016-09-04 12:43:22 -04:00
# run when someone leaves
*"PART #"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
2016-09-04 12:43:22 -04:00
if [ "$who" = "$nick" ]; then
continue
fi
if [ "${leave^^}" = "TRUE" ]; then
2017-04-02 10:41:28 -04:00
set -f
./triggers/bye/bye.sh "$who" "$from"
set +f
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 "$result" | tee -a "$log"
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#${from%% *}"
# Trigger stuff happens here.
2016-09-04 12:43:22 -04:00
# Call link trigger if msg contains a link:
if [[ "$result" =~ .*http://|https://|www\..* ]]; then
./triggers/link/link.sh "$who" "$from" "$result"
2016-09-04 17:22:05 -04:00
# Although this calls modules, it triggers on text other than the bot's nick
elif [[ "$result" =~ ^.*PRIVMSG.*:[[:punct:]].* ]]; then
command="${result#*:[[:punct:]]}"
command="${command//# /}"
will="${command#* }"
command="${command%% *}"
if [ -z "$(ls modules/ | grep -i -- "$command")" ] || [ -z "$command" ]; then
2016-09-04 17:22:05 -04:00
continue
fi
2017-04-02 10:41:28 -04:00
# Disable wildcards
set -f
./modules/${command% *}/${command% *}.sh "$who" "$from" $will
# Enable wildcards
set +f
2016-09-06 14:25:46 -04:00
else
2017-04-02 10:41:28 -04:00
set -f
./triggers/keywords/keywords.sh "$who" "$from" "$result"
2017-04-02 10:41:28 -04:00
set +f
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 "$result" | grep ":$nick:")" || continue
will="${result#*#*:}"
will="${will#*:}"
else
will="${result:1}"
will="${will#* :}"
from="$who"
fi
# Had to turn on globbing to remove all leading whitespace, then turn it off again afterwards.
shopt -s extglob
will="${will##*( )}"
shopt -u extglob
command="${will%% *}"
will="${will#* }"
if [ -z "$(ls modules/ | grep -i -- "$command")" ] || [ -z "$command" ]; then
2017-04-02 10:41:28 -04:00
set -f
./modules/help/help.sh "$who" "$from"
set +f
continue
fi
2017-04-02 10:41:28 -04:00
set -f
./modules/$command/$command.sh "$who" "$from" $will
set +f
2013-07-16 18:19:17 -04:00
;;
*)
2016-09-07 08:39:08 -04:00
echo "$result" | tee -a "$log"
2013-07-16 18:19:17 -04:00
;;
esac
done