diff --git a/bot.sh b/bot.sh index 0add202..8a448ea 100755 --- a/bot.sh +++ b/bot.sh @@ -4,8 +4,16 @@ 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 + +# 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 # Variables important to modules need to be exported here. export allowList @@ -14,19 +22,23 @@ export input export ignoreList export quitMessage +# Function called on exit to remove the temporary input file. rm_input() { if [[ -f "$input" ]]; then rm -f "$input" fi } +# Trap exiting ffrom the program to remove the temporary input file. trap rm_input EXIT +# Set up the connection. 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" echo "NICK $nick" > "$input" echo "USER $user" >> "$input" echo "JOIN #$channel" >> "$input" +# The main loop of the program where we watch for output from irc. tail -f "$input" | telnet "$server" "$port" | while read -r result ; do # log the session echo "$(date "+[$dateFormat]") $result" >> "$log" diff --git a/functions.sh b/functions.sh index 08a92be..d24cea6 100644 --- a/functions.sh +++ b/functions.sh @@ -8,30 +8,26 @@ close_bot() { echo -en "QUIT :${quitMessage}\r\n" >> "$input" } -msg() -{ - local msg="PRIVMSG $1 :" -shift -echo -en "${msg}$@\r\n" | tee -a "$input" +msg() { + local msg="PRIVMSG $1 :" + shift + echo -en "${msg}$@\r\n" | tee -a "$input" } -nick() -{ - local msg="NICK $1" -shift -echo -en "${msg}\r\n" | tee -a "$input" +nick() { + local msg="NICK $1" + shift + echo -en "${msg}\r\n" | tee -a "$input" } -reply() -{ -shift - local msg="PRIVMSG $1 :" -echo -en "${msg}$@\r\n" | tee -a "$input" +reply() { + shift + local msg="PRIVMSG $1 :" + echo -en "${msg}$@\r\n" | tee -a "$input" } -act() -{ - local msg="PRIVMSG $1 :\x01ACTION" -shift -echo -en "$msg $@\x01\r\n" | tee -a "$input" +act() { + local msg="PRIVMSG $1 :\x01ACTION" + shift + echo -en "$msg $@\x01\r\n" | tee -a "$input" }