create setconfigKey, clean botconfig on update startup

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-13 21:39:52 +02:00
parent cdc6dd323d
commit e0b9f6f870
2 changed files with 21 additions and 13 deletions

View File

@ -11,7 +11,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.98-dev-7-gca05095 #### $$VERSION$$ v0.98-dev-8-gcdc6dd3
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -53,6 +53,10 @@ _round_float() {
local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0" local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0"
LC_ALL=C printf "%.${digit}f" "${1}" LC_ALL=C printf "%.${digit}f" "${1}"
} }
setConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh"
}
getConfigKey() { getConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1
@ -121,7 +125,7 @@ if [ -z "${BOTTOKEN}" ]; then
if [ -z "$(getConfigKey "bottoken")" ]; then if [ -z "$(getConfigKey "bottoken")" ]; then
# convert old token # convert old token
if [ -r "${TOKENFILE}" ]; then if [ -r "${TOKENFILE}" ]; then
printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh" token="$(< "${TOKENFILE}")"
# no old token avalible ask user # no old token avalible ask user
elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then
echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!"
@ -130,25 +134,27 @@ if [ -z "${BOTTOKEN}" ]; then
${CLEAR} ${CLEAR}
echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${RED}TOKEN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
read -r BOTTOKEN read -r token
printf '["bottoken"]\t"%s"\n' "${BOTTOKEN}" >> "${BOTDATABASE}.jssh"
fi fi
[ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTDATABASE}.jssh"
fi fi
# setup botadmin file # setup botadmin file
if [ ! -f "${BOTADMIN}" ]; then if [ -z "$(getConfigKey "botadmin")" ]; then
if [ -z "${CLEAR}" ]; then # convert old token
if [ -r "${BOTADMIN}" ]; then
admin="$(< "${BOTADMIN}")"
elif [ -z "${CLEAR}" ]; then
echo "Running headless, set botadmin to AUTO MODE!" echo "Running headless, set botadmin to AUTO MODE!"
printf '%s\n' '?' > "${BOTADMIN}"
else else
${CLEAR} ${CLEAR}
echo -e "${RED}BOTADMIN MISSING.${NC}" echo -e "${RED}BOTADMIN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}" echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
read -r admin read -r admin
[ -z "${admin}" ] && admin='?'
printf '%s\n' "${admin}" > "${BOTADMIN}"
fi fi
[ -z "${admin}" ] && admin='?'
printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTDATABASE}.jssh"
fi fi
# setup botacl file # setup botacl file
if [ ! -f "${BOTACL}" ]; then if [ ! -f "${BOTACL}" ]; then
@ -825,6 +831,8 @@ start_bot() {
# cleanup countfile on startup # cleanup countfile on startup
jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}"
[ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock"
jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTDATABASE}"
[ -f "${BOTDATABASE}.jssh.flock" ] && rm -f "${BOTDATABASE}.jssh.flock"
########## ##########
# bot is ready, start processing updates ... # bot is ready, start processing updates ...
@ -998,7 +1006,7 @@ if [ "${SOURCE}" != "yes" ]; then
[[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue
(( USERS++ )) (( USERS++ ))
if [ -n "$*" ]; then if [ -n "$*" ]; then
send_markdown_message "${MSG}" "$*" send_message "${MSG}" "$*"
echo -e ".\c" echo -e ".\c"
sleep 0.1 sleep 0.1
fi fi

View File

@ -5,7 +5,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.96-0-g3871ca9 #### $$VERSION$$ v0.98-dev-8-gcdc6dd3
# source once magic, function named like file # source once magic, function named like file
eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
@ -50,11 +50,11 @@ user_is_admin() {
} }
user_is_botadmin() { user_is_botadmin() {
local admin; admin="$(head -n 1 "${BOTADMIN}")" local admin; admin="$(getConfigKey "botadmin")"
[ "${admin}" = "${1}" ] && return 0 [ "${admin}" = "${1}" ] && return 0
[ "${admin}" = "${2}" ] && return 0 [ "${admin}" = "${2}" ] && return 0
[[ "${admin}" = "@*" ]] && [[ "${admin}" = "${2}" ]] && return 0 [[ "${admin}" = "@*" ]] && [[ "${admin}" = "${2}" ]] && return 0
if [ "${admin}" = "?" ]; then printf '%s\n' "${1:-?}" >"${BOTADMIN}"; return 0; fi if [ "${admin}" = "?" ]; then setConfigKey "${1:-?}"; return 0; fi
return 1 return 1
} }