More code cleanup on the main bot and its supporting functions.

This commit is contained in:
Storm Dragon 2020-08-18 19:40:26 -04:00
parent c5f5c41a7d
commit 0eccf775da
2 changed files with 30 additions and 22 deletions

16
bot.sh
View File

@ -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"

View File

@ -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"
}