avoid unintended file overwrite

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-20 20:12:36 +02:00
parent 25cc9a5015
commit d52ea8c240
2 changed files with 14 additions and 9 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-56-gf119c44 #### $$VERSION$$ v0.98-dev-65-g25cc9a5
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -57,6 +57,7 @@ _round_float() {
} }
setConfigKey() { setConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[ -z "${BOTCONFIG}" ] && return 1
printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh" printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh"
} }
getConfigKey() { getConfigKey() {
@ -135,7 +136,7 @@ UPDATELOG="${LOGDIR}/BASHBOT.log"
if [ -z "${BOTTOKEN}" ]; then if [ -z "${BOTTOKEN}" ]; then
# BOTCONFIG does not exist, create # BOTCONFIG does not exist, create
[ ! -f "${BOTCONFIG}.jssh" ] && [ ! -f "${BOTCONFIG}.jssh" ] &&
printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" printf '["bot_config_key"]\t"config_key_value"\n' >>"${BOTCONFIG}.jssh"
# BOTTOKEN empty read ask user # BOTTOKEN empty read ask user
if [ -z "$(getConfigKey "bottoken")" ]; then if [ -z "$(getConfigKey "bottoken")" ]; then
# convert old token # convert old token
@ -186,7 +187,7 @@ if [ -z "${BOTTOKEN}" ]; then
fi fi
# setup count file # setup count file
if [ ! -f "${COUNTFILE}.jssh" ]; then if [ ! -f "${COUNTFILE}.jssh" ]; then
printf '["counted_user_chat_id"]\t"num_messages_seen"\n' > "${COUNTFILE}.jssh" printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh"
# convert old file on creation # convert old file on creation
if [ -r "${COUNTFILE}" ];then if [ -r "${COUNTFILE}" ];then
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
@ -198,7 +199,7 @@ if [ -z "${BOTTOKEN}" ]; then
fi fi
# setup blocked file # setup blocked file
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh" printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >>"${BLOCKEDFILE}.jssh"
fi fi
fi fi
@ -424,6 +425,7 @@ sendJsonRetry(){
# stdout is written to ERROR.log # stdout is written to ERROR.log
# $1 result $2 function $3 .. $n original arguments, $3 is Chat_id # $1 result $2 function $3 .. $n original arguments, $3 is Chat_id
sendJsonResult(){ sendJsonResult(){
local offset=0
BOTSENT=( ) BOTSENT=( )
[ -n "${BASHBOTDEBUG}" ] && printf "\n%s: New Result ==========\n%s\n" "$(date)" "$1" >>"${LOGDIR}/MESSAGE.log" [ -n "${BASHBOTDEBUG}" ] && printf "\n%s: New Result ==========\n%s\n" "$(date)" "$1" >>"${LOGDIR}/MESSAGE.log"
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")"
@ -439,12 +441,15 @@ sendJsonResult(){
BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")" BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")"
BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")" BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")"
else else
BOTSENT[OK]="false"
BOTSENT[ERROR]="999" BOTSENT[ERROR]="999"
BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection" BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection"
fi fi
# log error # log error
[[ "${BOTSENT[ERROR]}" = "400" && "${BOTSENT[DESCRIPTION]}" == *"starting at byte offset"* ]] &&\
offset="${BOTSENT[DESCRIPTION]%* }"
printf "%s: RESULT=%s FUNC=%s CHAT[ID]=%s ERROR=%s DESC=%s ACTION=%s\n" "$(date)"\ printf "%s: RESULT=%s FUNC=%s CHAT[ID]=%s ERROR=%s DESC=%s ACTION=%s\n" "$(date)"\
"${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" "${4:0:60}" "${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" "${4:${offset}:100}"
# warm path, do not retry on error, also if we use wegt # warm path, do not retry on error, also if we use wegt
[ -n "${BASHBOT_RETRY}${BASHBOT_WGET}" ] && return [ -n "${BASHBOT_RETRY}${BASHBOT_WGET}" ] && return

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.98-dev-59-g21f29a4 #### $$VERSION$$ v0.98-dev-65-g25cc9a5
# #
# source from commands.sh to use jsonDB functions # source from commands.sh to use jsonDB functions
# #
@ -206,11 +206,11 @@ jssh_checkDB(){
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
[[ "$1" = *'..'* ]] && return 2 [[ "$1" = *'..'* ]] && return 2
if [[ "$1" == "${BASHBOT_VAR:-.}"* ]] || [[ "$1" == "${BASHBOT_DATA:-.}"* ]]; then if [[ "$1" == "${BASHBOT_VAR:-.}"* ]] || [[ "$1" == "${BASHBOT_DATA:-.}"* ]]; then
DB="$1.jssh" DB="${1}.jssh"
else else
DB="${BASHBOT_VAR:-.}/$1.jssh" DB="${BASHBOT_VAR:-.}/${1}.jssh"
fi fi
printf '%s' "${DB}" [ "${DB}" != ".jssh" ] && printf '%s' "${DB}"
} }