From 45447d2e77886ec3392c1ff0cd58d7e95fe8b265 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 17 Aug 2020 21:20:12 -0400 Subject: [PATCH] the bot can now exit gracefully. --- .gitignore | 4 ++-- bot.sh | 24 +++++++++++++++--------- functions.sh | 8 ++++++-- modules/exit/exit.sh | 8 ++++++++ 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100755 modules/exit/exit.sh diff --git a/.gitignore b/.gitignore index 4013ec9..84b64ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -/response/error.txt +response/error.txt log.txt -.botinput +.* bot.cfg bot.cfg diff --git a/bot.sh b/bot.sh index 02ba138..90efc6b 100755 --- a/bot.sh +++ b/bot.sh @@ -6,19 +6,22 @@ if [ "$(whoami)" = "root" ]; then fi [ -f functions.sh ] && source functions.sh [ -f bot.cfg ] && source bot.cfg -export allowList -export ignoreList -input=".botinput" -close_bot() { - echo -en "QUIT :${quitMessage}\r\n" | telnet "$server" "$port" - rm "$input" - exit 0 +# Variables important to modules need to be exported here. +export allowList +export input +export ignoreList +export quitMessage + +rm_input() { + if [[ -f "$input" ]]; then + rm "$input" + fi } -trap close_bot EXIT $? +trap rm_input EXIT -echo "Session started $(date "+%I:%M%p%n %A, %B %d, %Y")" | 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 "USER $user" >> "$input" echo "JOIN #$channel" >> "$input" @@ -149,3 +152,6 @@ tail -f "$input" | telnet "$server" "$port" | while read -r result ; do ;; esac done + +rm_input +exit 0 diff --git a/functions.sh b/functions.sh index 5c92f8f..08a92be 100644 --- a/functions.sh +++ b/functions.sh @@ -1,9 +1,13 @@ #!/bin/bash -if [ -z "$input" ]; then -input=".botinput" +if [[ -z "$input" ]]; then + input="$(mktemp .XXXXXX)" fi +close_bot() { + echo -en "QUIT :${quitMessage}\r\n" >> "$input" +} + msg() { local msg="PRIVMSG $1 :" diff --git a/modules/exit/exit.sh b/modules/exit/exit.sh new file mode 100755 index 0000000..19f42d5 --- /dev/null +++ b/modules/exit/exit.sh @@ -0,0 +1,8 @@ +[ -f functions.sh ] && source functions.sh + +user=$1 +shift +shift +if [[ "$user" =~ $allowList ]]; then + close_bot +fi