implement ENV varible usage

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-23 13:07:20 +02:00
parent 1957133d9b
commit f412a2921a

View File

@ -10,7 +10,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.70-dev2-6-gc527d17 #### $$VERSION$$ v0.70-dev2-7-g1957133
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -29,22 +29,26 @@ if [ -t 1 ] && [ "$TERM" != "" ]; then
NC='\e[0m' NC='\e[0m'
fi fi
# get location of bashbot.sh an change to bashbot dir # get location and name of bashbot.sh
SCRIPT="./$(basename "$0")" SCRIPT="$0"
SCRIPTDIR="$(dirname "$0")" SCRIPTDIR="$(dirname "$0")"
RUNDIR="${BASHBOT_VAR:-${SCRIPTDIR}}"
[ "${RUNDIR}" = "${SCRIPTDIR}" ] && SCRIPT="./$(basename "${SCRIPT}")"
RUNUSER="${USER}" # USER is overwritten by bashbot array, $USER may not work later on... RUNUSER="${USER}" # USER is overwritten by bashbot array, $USER may not work later on...
if [ "$1" != "source" ] && ! cd "${SCRIPTDIR}" ; then if [ "$1" != "source" ] && ! cd "${RUNDIR}" ; then
echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}" echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
exit 1 exit 1
fi fi
if [ ! -w "." ]; then if [ ! -w "." ]; then
echo -e "${ORANGE}WARNING: ${SCRIPTDIR} is not writeable!${NC}" echo -e "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NC}"
ls -ld . ls -ld .
fi fi
TOKENFILE="./token" TOKENFILE="${BASHBOT_ETC:-.}/token"
if [ ! -f "${TOKENFILE}" ]; then if [ ! -f "${TOKENFILE}" ]; then
if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then
echo "Running headless, run ${SCRIPT} init first!" echo "Running headless, run ${SCRIPT} init first!"
@ -58,7 +62,7 @@ if [ ! -f "${TOKENFILE}" ]; then
fi fi
fi fi
JSONSHFILE="JSON.sh/JSON.sh" JSONSHFILE="${BASHBOT_JSONSH:-${RUNDIR}}/JSON.sh/JSON.sh"
if [ ! -f "${JSONSHFILE}" ]; then if [ ! -f "${JSONSHFILE}" ]; then
echo "Seems to be first run, Downloading ${JSONSHFILE}..." echo "Seems to be first run, Downloading ${JSONSHFILE}..."
mkdir "JSON.sh" 2>/dev/null; mkdir "JSON.sh" 2>/dev/null;
@ -66,7 +70,7 @@ if [ ! -f "${JSONSHFILE}" ]; then
chmod +x "${JSONSHFILE}" chmod +x "${JSONSHFILE}"
fi fi
BOTADMIN="./botadmin" BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
if [ ! -f "${BOTADMIN}" ]; then if [ ! -f "${BOTADMIN}" ]; then
if [ "${CLEAR}" = "" ]; then if [ "${CLEAR}" = "" ]; then
echo "Running headless, set botadmin to AUTO MODE!" echo "Running headless, set botadmin to AUTO MODE!"
@ -81,13 +85,13 @@ if [ ! -f "${BOTADMIN}" ]; then
fi fi
fi fi
BOTACL="./botacl" BOTACL="${BASHBOT_ETC:-.}/botacl"
if [ ! -f "${BOTACL}" ]; then if [ ! -f "${BOTACL}" ]; then
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}" echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
echo "" >"${BOTACL}" echo "" >"${BOTACL}"
fi fi
TMPDIR="./tmp-bot-bash" TMPDIR="${BASHBOT_VAR:-.}/tmp-bot-bash"
if [ ! -d "${TMPDIR}" ]; then if [ ! -d "${TMPDIR}" ]; then
mkdir "${TMPDIR}" mkdir "${TMPDIR}"
elif [ ! -w "${TMPDIR}" ]; then elif [ ! -w "${TMPDIR}" ]; then
@ -97,7 +101,7 @@ elif [ ! -w "${TMPDIR}" ]; then
exit 2 exit 2
fi fi
COUNTFILE="./count" COUNTFILE="${BASHBOT_VAR:-.}/count"
if [ ! -f "${COUNTFILE}" ]; then if [ ! -f "${COUNTFILE}" ]; then
echo "" >"${COUNTFILE}" echo "" >"${COUNTFILE}"
elif [ ! -w "${COUNTFILE}" ]; then elif [ ! -w "${COUNTFILE}" ]; then
@ -107,7 +111,7 @@ elif [ ! -w "${COUNTFILE}" ]; then
exit 2 exit 2
fi fi
COMMANDS="./commands.sh" COMMANDS="${BASHBOT_COMMANDS:-${RUNDIR}}/commands.sh"
if [ "$1" != "source" ]; then if [ "$1" != "source" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
${CLEAR} ${CLEAR}
@ -176,7 +180,7 @@ send_message() {
} }
if [ "$no_keyboard" != "" ]; then if [ "$no_keyboard" != "" ]; then
echo "remove_keyboard $chat $text" > ${TMPDIR:-.}/prova echo "remove_keyboard $chat $text" > "${TMPDIR:-.}/prova"
remove_keyboard "$chat" "$text" remove_keyboard "$chat" "$text"
sent=y sent=y
fi fi
@ -249,7 +253,7 @@ delete_message() {
# usage: status="$(get_chat_member_status "chat" "user")" # usage: status="$(get_chat_member_status "chat" "user")"
get_chat_member_status() { get_chat_member_status() {
curl -s "$GETMEMBER_URL" -F "chat_id=$1" -F "user_id=$2" | "./${JSONSHFILE}" -s -b -n | sed -n -e '/\["result","status"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' curl -s "$GETMEMBER_URL" -F "chat_id=$1" -F "user_id=$2" | "${JSONSHFILE}" -s -b -n | sed -n -e '/\["result","status"\]/ s/.*\][ \t]"\(.*\)"$/\1/p'
} }
kick_chat_member() { kick_chat_member() {
@ -393,7 +397,7 @@ remove_keyboard() {
get_file() { get_file() {
[ "$1" = "" ] && return [ "$1" = "" ] && return
echo "${FILE_URL}$(curl -s "${GET_URL}" -F "file_id=$1" | "./${JSONSHFILE}" -s -b -n | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)" echo "${FILE_URL}$(curl -s "${GET_URL}" -F "file_id=$1" | "${JSONSHFILE}" -s -b -n | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)"
} }
send_file() { send_file() {
@ -523,7 +527,7 @@ process_client() {
copname="$ME"_"${CHAT[ID]}" copname="$ME"_"${CHAT[ID]}"
source commands.sh source commands.sh
tmpcount="COUNT${CHAT[ID]}" tmpcount="COUNT${CHAT[ID]}"
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNTFILE} grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>"${COUNTFILE}"
# To get user count execute bash bashbot.sh count # To get user count execute bash bashbot.sh count
} }
JsonGetString() { JsonGetString() {
@ -613,7 +617,7 @@ start_bot() {
local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))" local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))"
while true; do { while true; do {
UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./${JSONSHFILE})" UPDATE="$(curl -s "$UPD_URL$OFFSET" | "${JSONSHFILE}")"
# Offset # Offset
OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
@ -657,7 +661,7 @@ bot_init() {
# get bot name # get bot name
getBotName() { getBotName() {
res="$(curl -s "$ME_URL")" res="$(curl -s "$ME_URL")"
echo "$res" | "./${JSONSHFILE}" -s -b -n | JsonGetString '"result","username"' echo "$res" | "${JSONSHFILE}" -s -b -n | JsonGetString '"result","username"'
} }
ME="$(getBotName)" ME="$(getBotName)"