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 #!/bin/bash
if [ "$(whoami)" = "root" ]; then if [ "$(whoami)" = "root" ]; then
echo "This bot should not be ran as root." echo "This bot should not be ran as root."
exit 1 exit 1
fi fi
[ -f functions.sh ] && source functions.sh [ -f functions.sh ] && source functions.sh
[ -f bot.cfg ] && source bot.cfg [ -f bot.cfg ] && source bot.cfg
input=".botinput" input=".botinput"
close_bot() close_bot() {
{ echo -en "QUIT :$quitMessage\r\n" >> "$input"
echo -en "QUIT :$quitMessage\r\n" >> "$input" rm "$input"
rm "$input" exit 0
exit 0
} }
trap close_bot EXIT $? trap close_bot EXIT $?
@ -22,128 +21,127 @@ echo "NICK $nick" > "$input"
echo "USER $user" >> "$input" echo "USER $user" >> "$input"
echo "JOIN #$channel" >> "$input" echo "JOIN #$channel" >> "$input"
tail -f "$input" | telnet "$server" "$port" | while read result tail -f "$input" | telnet "$server" "$port" | while read -r result ; do
do # log the session
# log the session echo "$(date "+[$dateFormat]") $result" >> "$log"
echo "$(date "+[$dateFormat]") $result" >> "$log" # do things when you see output
# do things when you see output case "$result" in
case "$result" in # respond to ping requests from the server
# respond to ping requests from the server PING*)
PING*) echo "${result/I/O}" >> "$input"
echo "${result/I/O}" >> "$input" ;;
;; # for pings on nick/user
# for pings on nick/user *"You have not"*)
*"You have not"*) echo "JOIN #$channel" | tee -a "$input"
echo "JOIN #$channel" | tee -a "$input" ;;
;; # Run on kick
# Run on kick :*!*@*" KICK "*" $nick :"*)
:*!*@*" KICK "*" $nick :"*) if [ "$autoRejoinChannel" = "true" ]; then
if [ "$autoRejoinChannel" = "true" ]; then echo "JOIN #$channel" | tee -a "$input"
echo "JOIN #$channel" | tee -a "$input" fi
fi if [ "$curseKicker" = "true" ]; then
if [ "$curseKicker" = "true" ]; then kickerName="${result%!*}"
kickerName="${result%!*}" kickerName="${kickerName:1}"
kickerName="${kickerName:1}" kickerChannel="${result##*#}"
kickerChannel="${result##*#}" kickerChannel="#${kickerChannel%% *}"
kickerChannel="#${kickerChannel%% *}" msg "$kickerChannel" "$kickerName: $(shuf -e -n1 "fuck you" "go fuck yourself")!"
msg "$kickerChannel" "$kickerName: $(shuf -e -n1 "fuck you" "go fuck yourself")!" fi
fi ;;
;; # run when someone joins
# run when someone joins *"JOIN :#"*)
*"JOIN :#"*) who="${result%%!*}"
who="${result%%!*}" who="${who:1}"
who="${who:1}" from="${result#*#}"
from="${result#*#}" from="#$from"
from="#$from" if [ "$who" = "$nick" ]; then
if [ "$who" = "$nick" ]; then continue
continue fi
fi
echo "MODE #$channel +o $who" | tee -a "$input" echo "MODE #$channel +o $who" | tee -a "$input"
if [ "${greet^^}" = "TRUE" ]; then if [ "${greet^^}" = "TRUE" ]; then
set -f set -f
./triggers/greet/greet.sh "$who" "$from" ./triggers/greet/greet.sh "$who" "$from"
set +f set +f
fi fi
;; ;;
# run when someone leaves # run when someone leaves
*"PART #"*) *"PART #"*)
who="${result%%!*}" who="${result%%!*}"
who="${who:1}" who="${who:1}"
from="${result#*#}" from="${result#*#}"
from="#$from" from="#$from"
if [ "$who" = "$nick" ]; then if [ "$who" = "$nick" ]; then
continue continue
fi fi
if [ "${leave^^}" = "TRUE" ]; then if [ "${leave^^}" = "TRUE" ]; then
set -f set -f
./triggers/bye/bye.sh "$who" "$from" ./triggers/bye/bye.sh "$who" "$from"
set +f set +f
fi fi
;; ;;
# run when a message is seen # run when a message is seen
*PRIVMSG*) *PRIVMSG*)
echo "$result" >> "$log" echo "$result" >> "$log"
who="${result%%!*}" who="${result%%!*}"
who="${who:1}" who="${who:1}"
from="${result#*#}" from="${result#*#}"
from="${from%% *}" from="${from%% *}"
from="#${from:-$channel}" from="#${from:-$channel}"
# Trigger stuff happens here. # Trigger stuff happens here.
# Call link trigger if msg contains a link: # Call link trigger if msg contains a link:
if [[ "$result" =~ .*http://|https://|www\..* ]]; then if [[ "$result" =~ .*http://|https://|www\..* ]]; then
set -f set -f
echo "Calling link.sh with \"$who\" \"$from\" \"$result\"" >> "$log" echo "Calling link.sh with \"$who\" \"$from\" \"$result\"" >> "$log"
./triggers/link/link.sh "$who" "$from" "$result" ./triggers/link/link.sh "$who" "$from" "$result"
set -f set -f
# Although this calls modules, it triggers on text other than the bot's nick # Although this calls modules, it triggers on text other than the bot's nick
elif [[ "$result" =~ ^.*PRIVMSG.*:[${botCaller}].* ]]; then elif [[ "$result" =~ ^.*PRIVMSG.*:[${botCaller}].* ]]; then
command="${result#*:[[:punct:]]}" command="${result#*:[[:punct:]]}"
command="${command//# /}" command="${command//# /}"
will="${command#* }" will="${command#* }"
command="${command%% *}" command="${command%% *}"
if command -v "./modules/${command% *}/${command% *}.sh" ; then if command -v "./modules/${command% *}/${command% *}.sh" ; then
echo "Calling module ./modules/${command% *}/${command% *}/${command% *}.sh \"$who\" \"$from\" $will" >> "$log" echo "Calling module ./modules/${command% *}/${command% *}/${command% *}.sh \"$who\" \"$from\" $will" >> "$log"
# Disable wildcards # Disable wildcards
set -f set -f
./modules/${command% *}/${command% *}.sh "$who" "$from" $will ./modules/${command% *}/${command% *}.sh "$who" "$from" $will
# Enable wildcards # Enable wildcards
set +f set +f
else else
./modules/say/say.sh "$who" "$from" "$who: $(shuf -n1 "response/error.txt")" ./modules/say/say.sh "$who" "$from" "$who: $(shuf -n1 "response/error.txt")"
fi fi
else else
set -f set -f
./triggers/keywords/keywords.sh "$who" "$from" "$result" ./triggers/keywords/keywords.sh "$who" "$from" "$result"
set +f set +f
fi fi
# "#" would mean it's a channel # "#" would mean it's a channel
if [ "$(echo "$from" | grep '#')" ]; then if [ "$(echo "$from" | grep '#')" ]; then
test "$(echo "$result" | grep ":$nick:")" || continue [[ "$(echo "$result" | grep ":$nick:")" ]] || continue
will="${result#*#*:}" will="${result#*#*:}"
will="${will#*:}" will="${will#*:}"
else else
will="${result:1}" will="${result:1}"
will="${will#* :}" will="${will#* :}"
from="$who" from="$who"
fi fi
# Had to turn on globbing to remove all leading whitespace, then turn it off again afterwards. # Had to turn on globbing to remove all leading whitespace, then turn it off again afterwards.
shopt -s extglob shopt -s extglob
will="${will##*( )}" will="${will##*( )}"
shopt -u extglob shopt -u extglob
command="${will%% *}" command="${will%% *}"
will="${will#* }" will="${will#* }"
if [ -z "$(ls modules/ | grep -i -- "$command")" ] || [ -z "$command" ]; then if [ -z "$(ls modules/ | grep -i -- "$command")" ] || [ -z "$command" ]; then
set -f set -f
./modules/help/help.sh "$who" "$from" ./modules/help/help.sh "$who" "$from"
set +f set +f
continue continue
fi fi
set -f set -f
./modules/$command/$command.sh "$who" "$from" $will ./modules/$command/$command.sh "$who" "$from" $will
set +f set +f
;; ;;
*) *)
echo "$result" >> "$log" echo "$result" >> "$log"
;; ;;
esac esac
done done