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
2020-08-13 20:46:09 -04:00
echo "This bot should not be ran as root."
exit 1
2017-04-05 19:00:57 -04:00
fi
2020-08-18 19:40:26 -04:00
# Load required files.
for i in "bot.cfg" "functions.sh" ; do
if [ [ -f " $i " ] ] ; then
source " $i "
else
echo " Could not find required file \" ${ i } \". "
exit 1
fi
done
2020-08-17 21:20:12 -04:00
# Variables important to modules need to be exported here.
2020-08-17 19:51:38 -04:00
export allowList
2020-08-18 10:04:46 -04:00
export channel
2020-08-17 21:20:12 -04:00
export input
2020-08-17 19:51:38 -04:00
export ignoreList
2020-11-22 18:55:18 -05:00
export nick
2020-08-17 21:20:12 -04:00
export quitMessage
2016-09-05 21:44:04 -04:00
2020-08-18 19:40:26 -04:00
# Function called on exit to remove the temporary input file.
2020-08-17 21:20:12 -04:00
rm_input( ) {
if [ [ -f " $input " ] ] ; then
2020-08-17 21:27:05 -04:00
rm -f " $input "
2020-08-17 21:20:12 -04:00
fi
2016-09-05 21:44:04 -04:00
}
2020-08-18 19:40:26 -04:00
# Trap exiting ffrom the program to remove the temporary input file.
2020-08-17 21:20:12 -04:00
trap rm_input EXIT
2016-09-05 21:44:04 -04:00
2020-08-18 19:40:26 -04:00
# Set up the connection.
2020-08-17 21:20:12 -04:00
echo -e " Session started $( date "+%I:%M%p%n %A, %B %d, %Y" ) .\n\nTo gracefully exit, make sure you are in the allow list and send the command exit to the bot.\n\n " | tee " $log "
2020-08-10 13:25:05 -04:00
echo " NICK $nick " > " $input "
echo " USER $user " >> " $input "
echo " JOIN # $channel " >> " $input "
2013-07-16 18:19:17 -04:00
2020-08-18 19:40:26 -04:00
# The main loop of the program where we watch for output from irc.
2020-08-13 20:46:09 -04:00
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
2020-08-20 17:58:54 -04:00
# Handle nick changes
":" *"NICK :" *)
# Get the original nick
originalNick = " ${ result # : } "
originalNick = " ${ originalNick %%!* } "
# If the old nick was in the ignore list, update it.
if [ [ " ${ originalNick } " = ~ [ ${ ignoreList } ] ] ] ; then
export ignoreList = " ${ ignoreList / ${ originalNick } / ${ result # : * : } } "
fi
; ;
2020-08-13 20:46:09 -04:00
# 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
2016-09-06 22:35:15 -04:00
echo " MODE # $channel +o $who " | tee -a " $input "
2020-08-13 20:46:09 -04:00
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
; ;
2020-08-14 21:17:43 -04:00
# run when a private message is seen
*"PRIVMSG " [ [ :alnum:] -_] *)
echo " $result " >> " $log "
who = " ${ result %%!* } "
who = " ${ who : 1 } "
from = " ${ who %!* } "
command = " ${ result # : * PRIVMSG [[ : alnum : ]_-]* : } "
command = " ${ command //# / } "
will = " ${ command #* } "
command = " ${ command %% * } "
if [ [ " $from " = ~ $allowList ] ] ; then
2020-08-15 23:25:16 -04:00
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 " " # $channel " $will
# Enable wildcards
set +f
else
reply " $who " " $( shuf -n1 "response/error.txt" ) "
fi
2020-08-14 21:17:43 -04:00
else
reply " $who " "You are not in the allowed list for this bot. If you think this is an error, please contact the bot's administrator."
fi
; ;
2020-08-13 20:46:09 -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.
# 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
2020-08-19 20:42:23 -04:00
# To make sure that modules are only called when they are supposed to be, had to combine string monipulation with regexp.
2020-08-19 20:23:59 -04:00
elif [ [ " ${ result # : * : } " = ~ ^[ ${ botCaller } ] [ a-zA-Z_] .* ] ] ; then
2020-08-13 20:46:09 -04:00
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
2020-08-14 21:17:43 -04:00
if ! [ [ " $who " = ~ $ignoreList ] ] ; then
set -f
./triggers/keywords/keywords.sh " $who " " $from " " $result "
set +f
fi
2020-08-13 20:46:09 -04:00
fi
; ;
*)
echo " $result " >> " $log "
; ;
esac
2013-07-16 18:19:17 -04:00
done
2020-08-17 21:20:12 -04:00
rm_input
exit 0