stormbot/bot.sh

150 lines
4.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
2017-04-05 19:00:57 -04:00
if [ "$(whoami)" = "root" ]; then
echo "This bot should not be ran as root."
exit 1
fi
[ -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" > "$input"
echo "USER $user" >> "$input"
echo "JOIN #$channel" >> "$input"
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" >> "$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/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"*)
echo "JOIN #$channel" | tee -a "$input"
2013-07-16 18:19:17 -04:00
;;
# Run on kick
:*!*@*" KICK "*" $nick :"*)
if [ "$autoRejoinChannel" = "true" ]; then
echo "JOIN #$channel" | tee -a "$input"
fi
if [ "$curseKicker" = "true" ]; then
kickerName="${result%!*}"
kickerName="${kickerName:1}"
kickerChannel="${result##*#}"
kickerChannel="#${kickerChannel%% *}"
msg "$kickerChannel" "$kickerName: $(shuf -e -n1 "fuck you" "go fuck yourself")!"
fi
;;
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" >> "$log"
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="${from%% *}"
from="#${from:-$channel}"
# 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
2020-08-09 21:32:58 -04:00
set -f
2020-08-10 09:34:53 -04:00
echo "Calling link.sh with \"$who\" \"$from\" \"$result\"" >> "$log"
./triggers/link/link.sh "$who" "$from" "$result"
2020-08-09 21:32:58 -04:00
set -f
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.*:[${botCaller}].* ]]; then
command="${result#*:[[:punct:]]}"
command="${command//# /}"
will="${command#* }"
command="${command%% *}"
if command -v "./modules/${command% *}/${command% *}.sh" ; then
echo "Calling module ./modules/${command% *}/${command% *}/${command% *}.sh \"$who\" \"$from\" $will" >> "$log"
2017-04-02 10:41:28 -04:00
# Disable wildcards
set -f
./modules/${command% *}/${command% *}.sh "$who" "$from" $will
# Enable wildcards
set +f
else
./modules/say/say.sh "$who" "$from" "$who: $(shuf -n1 "response/error.txt")"
fi
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
;;
*)
echo "$result" >> "$log"
2013-07-16 18:19:17 -04:00
;;
esac
done