More code cleanup on the main bot and its supporting functions.
This commit is contained in:
parent
c5f5c41a7d
commit
0eccf775da
16
bot.sh
16
bot.sh
@ -4,8 +4,16 @@ 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 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.
|
# Variables important to modules need to be exported here.
|
||||||
export allowList
|
export allowList
|
||||||
@ -14,19 +22,23 @@ export input
|
|||||||
export ignoreList
|
export ignoreList
|
||||||
export quitMessage
|
export quitMessage
|
||||||
|
|
||||||
|
# Function called on exit to remove the temporary input file.
|
||||||
rm_input() {
|
rm_input() {
|
||||||
if [[ -f "$input" ]]; then
|
if [[ -f "$input" ]]; then
|
||||||
rm -f "$input"
|
rm -f "$input"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Trap exiting ffrom the program to remove the temporary input file.
|
||||||
trap rm_input EXIT
|
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 -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 "NICK $nick" > "$input"
|
||||||
echo "USER $user" >> "$input"
|
echo "USER $user" >> "$input"
|
||||||
echo "JOIN #$channel" >> "$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
|
tail -f "$input" | telnet "$server" "$port" | while read -r result ; do
|
||||||
# log the session
|
# log the session
|
||||||
echo "$(date "+[$dateFormat]") $result" >> "$log"
|
echo "$(date "+[$dateFormat]") $result" >> "$log"
|
||||||
|
36
functions.sh
36
functions.sh
@ -8,30 +8,26 @@ close_bot() {
|
|||||||
echo -en "QUIT :${quitMessage}\r\n" >> "$input"
|
echo -en "QUIT :${quitMessage}\r\n" >> "$input"
|
||||||
}
|
}
|
||||||
|
|
||||||
msg()
|
msg() {
|
||||||
{
|
local msg="PRIVMSG $1 :"
|
||||||
local msg="PRIVMSG $1 :"
|
shift
|
||||||
shift
|
echo -en "${msg}$@\r\n" | tee -a "$input"
|
||||||
echo -en "${msg}$@\r\n" | tee -a "$input"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nick()
|
nick() {
|
||||||
{
|
local msg="NICK $1"
|
||||||
local msg="NICK $1"
|
shift
|
||||||
shift
|
echo -en "${msg}\r\n" | tee -a "$input"
|
||||||
echo -en "${msg}\r\n" | tee -a "$input"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reply()
|
reply() {
|
||||||
{
|
shift
|
||||||
shift
|
local msg="PRIVMSG $1 :"
|
||||||
local msg="PRIVMSG $1 :"
|
echo -en "${msg}$@\r\n" | tee -a "$input"
|
||||||
echo -en "${msg}$@\r\n" | tee -a "$input"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
act()
|
act() {
|
||||||
{
|
local msg="PRIVMSG $1 :\x01ACTION"
|
||||||
local msg="PRIVMSG $1 :\x01ACTION"
|
shift
|
||||||
shift
|
echo -en "$msg $@\x01\r\n" | tee -a "$input"
|
||||||
echo -en "$msg $@\x01\r\n" | tee -a "$input"
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user