Cleaned up the code a bit, the formatting at least, there's plenty more to do.

This commit is contained in:
Storm Dragon 2020-08-13 20:46:09 -04:00
parent 273e24a63a
commit 5984a2dbb2

258
bot.sh
View File

@ -1,18 +1,17 @@
#!/bin/bash
if [ "$(whoami)" = "root" ]; then
echo "This bot should not be ran as root."
exit 1
echo "This bot should not be ran as root."
exit 1
fi
[ -f functions.sh ] && source functions.sh
[ -f bot.cfg ] && source bot.cfg
input=".botinput"
close_bot()
{
echo -en "QUIT :$quitMessage\r\n" >> "$input"
rm "$input"
exit 0
close_bot() {
echo -en "QUIT :$quitMessage\r\n" >> "$input"
rm "$input"
exit 0
}
trap close_bot EXIT $?
@ -22,128 +21,127 @@ echo "NICK $nick" > "$input"
echo "USER $user" >> "$input"
echo "JOIN #$channel" >> "$input"
tail -f "$input" | telnet "$server" "$port" | while read result
do
# log the session
echo "$(date "+[$dateFormat]") $result" >> "$log"
# do things when you see output
case "$result" in
# respond to ping requests from the server
PING*)
echo "${result/I/O}" >> "$input"
;;
# for pings on nick/user
*"You have not"*)
echo "JOIN #$channel" | tee -a "$input"
;;
# 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
;;
# run when someone joins
*"JOIN :#"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
if [ "$who" = "$nick" ]; then
continue
fi
tail -f "$input" | telnet "$server" "$port" | while read -r result ; do
# log the session
echo "$(date "+[$dateFormat]") $result" >> "$log"
# do things when you see output
case "$result" in
# respond to ping requests from the server
PING*)
echo "${result/I/O}" >> "$input"
;;
# for pings on nick/user
*"You have not"*)
echo "JOIN #$channel" | tee -a "$input"
;;
# 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
;;
# run when someone joins
*"JOIN :#"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
if [ "$who" = "$nick" ]; then
continue
fi
echo "MODE #$channel +o $who" | tee -a "$input"
if [ "${greet^^}" = "TRUE" ]; then
set -f
./triggers/greet/greet.sh "$who" "$from"
set +f
fi
;;
# run when someone leaves
*"PART #"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
if [ "$who" = "$nick" ]; then
continue
fi
if [ "${leave^^}" = "TRUE" ]; then
set -f
./triggers/bye/bye.sh "$who" "$from"
set +f
fi
;;
# 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.
# Call link trigger if msg contains a link:
if [[ "$result" =~ .*http://|https://|www\..* ]]; then
set -f
echo "Calling link.sh with \"$who\" \"$from\" \"$result\"" >> "$log"
./triggers/link/link.sh "$who" "$from" "$result"
set -f
# 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"
# 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
else
set -f
./triggers/keywords/keywords.sh "$who" "$from" "$result"
set +f
fi
# "#" would mean it's a channel
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
set -f
./modules/help/help.sh "$who" "$from"
set +f
continue
fi
set -f
./modules/$command/$command.sh "$who" "$from" $will
set +f
;;
*)
echo "$result" >> "$log"
;;
esac
if [ "${greet^^}" = "TRUE" ]; then
set -f
./triggers/greet/greet.sh "$who" "$from"
set +f
fi
;;
# run when someone leaves
*"PART #"*)
who="${result%%!*}"
who="${who:1}"
from="${result#*#}"
from="#$from"
if [ "$who" = "$nick" ]; then
continue
fi
if [ "${leave^^}" = "TRUE" ]; then
set -f
./triggers/bye/bye.sh "$who" "$from"
set +f
fi
;;
# 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.
# Call link trigger if msg contains a link:
if [[ "$result" =~ .*http://|https://|www\..* ]]; then
set -f
echo "Calling link.sh with \"$who\" \"$from\" \"$result\"" >> "$log"
./triggers/link/link.sh "$who" "$from" "$result"
set -f
# 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"
# 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
else
set -f
./triggers/keywords/keywords.sh "$who" "$from" "$result"
set +f
fi
# "#" would mean it's a channel
if [ "$(echo "$from" | grep '#')" ]; then
[[ "$(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
set -f
./modules/help/help.sh "$who" "$from"
set +f
continue
fi
set -f
./modules/$command/$command.sh "$who" "$from" $will
set +f
;;
*)
echo "$result" >> "$log"
;;
esac
done