bashbot.sh implement new a-z soluition

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-04 16:03:53 +01:00
parent 5f6476d691
commit 0798f1ae00
3 changed files with 23 additions and 16 deletions

View File

@ -30,7 +30,7 @@
# 8 - curl/wget missing # 8 - curl/wget missing
# 10 - not bash! # 10 - not bash!
# #
#### $$VERSION$$ v1.21-0-gc85af77 #### $$VERSION$$ v1.21-6-g5f6476d
################################################################## ##################################################################
# emmbeded system may claim bash but it is not # emmbeded system may claim bash but it is not
@ -63,6 +63,14 @@ if [ "$({ LC_ALL=C.utf-8 printf "%b" "\u1111"; } 2>/dev/null)" = "\u1111" ]; the
fi fi
# in UTF-8 äöü etc. are part of [:alnum:] and ranges (e.g. a-z), but we want ASCII a-z ranges!
# for more information see doc/4_expert.md#Character_classes
azazaz='abcdefghijklmnopqrstuvwxyz' # a-z :lower:
AZAZAZ='ABCDEFGHIJKLMNOPQRSTUVWXYZ' # A-Z :upper:
R090909='0123456789' # 0-9 :digit:
azAZaz="${azazaz}${AZAZAZ}" # a-zA-Z :alpha:
azAZ09="${azAZaz}${R090909}" # a-zA-z0-9 :alnum:
# some important helper functions # some important helper functions
# returns true if command exist # returns true if command exist
_exists() { _exists() {
@ -80,22 +88,22 @@ _is_function() {
# round $1 in international notation! , returns float with $2 decimal digits # round $1 in international notation! , returns float with $2 decimal digits
# if $2 is not given or is not a positive number zero is assumed # if $2 is not given or is not a positive number zero is assumed
_round_float() { _round_float() {
local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0" local digit="${2}"; [[ "${2}" =~ ^[${R090909}]+$ ]] || digit="0"
{ LC_ALL=C.utf-8 printf "%.${digit}f" "${1}"; } 2>/dev/null { LC_ALL=C.utf-8 printf "%.${digit}f" "${1}"; } 2>/dev/null
} }
setConfigKey() { setConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-${azAZ09},._]+$ ]] || return 3
[ -z "${BOTCONFIG}" ] && return 1 [ -z "${BOTCONFIG}" ] && return 1
printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh" printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh"
} }
getConfigKey() { getConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-${azAZ09},._]+$ ]] || return 3
[ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1 [ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1
} }
# check if $1 seems a valid token # check if $1 seems a valid token
# return true if token seems to be valid # return true if token seems to be valid
check_token(){ check_token(){
[[ "${1}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]] && return 0 [[ "${1}" =~ ^[${R090909}]{8,10}:[${azAZ09}_-]{35}$ ]] && return 0
return 1 return 1
} }
# log $1 with date # log $1 with date
@ -278,11 +286,11 @@ if ! check_token "${BOTTOKEN}"; then
"<your_bot_id>${RED}:${NC}<35_alphanumeric_characters-hash> ${RED}e.g. =>${NC} 123456789${RED}:${NC}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4\n\n"\ "<your_bot_id>${RED}:${NC}<35_alphanumeric_characters-hash> ${RED}e.g. =>${NC} 123456789${RED}:${NC}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4\n\n"\
"${GREY}Your bot token: '${NC}${BOTTOKEN//:/${RED}:${NC}}'\n" "${GREY}Your bot token: '${NC}${BOTTOKEN//:/${RED}:${NC}}'\n"
if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then if [[ ! "${BOTTOKEN}" =~ ^[${R090909}]{8,10}: ]]; then
printf "${GREY}\tHint: Bot id not a number or wrong len: ${NC}$(($(wc -c <<<"${BOTTOKEN%:*}")-1)) ${GREY}but should be${NC} 8-10\n" printf "${GREY}\tHint: Bot id not a number or wrong len: ${NC}$(($(wc -c <<<"${BOTTOKEN%:*}")-1)) ${GREY}but should be${NC} 8-10\n"
[ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}" [ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}"
fi fi
[[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ [[ ! "${BOTTOKEN}" =~ :[${azAZ09}_-]{35}$ ]] &&\
printf "${GREY}\tHint: Hash contains invalid character or has not len${NC} 35 ${GREY}, hash len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n" printf "${GREY}\tHint: Hash contains invalid character or has not len${NC} 35 ${GREY}, hash len is ${NC}$(($(wc -c <<<"${BOTTOKEN#*:}")-1))\n"
printf "\n" printf "\n"
fi fi
@ -428,14 +436,14 @@ if ! _exists iconv; then
fi fi
TIMEOUT="${BASHBOT_TIMEOUT}" TIMEOUT="${BASHBOT_TIMEOUT}"
[[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20" [[ "$TIMEOUT" =~ ^[${R090909}]+$ ]] || TIMEOUT="20"
# usage: sendJson "chat" "JSON" "URL" # usage: sendJson "chat" "JSON" "URL"
sendJson(){ sendJson(){
local json chat="" local json chat=""
if [ -n "${1}" ]; then if [ -n "${1}" ]; then
chat='"chat_id":'"${1}"',' chat='"chat_id":'"${1}"','
[[ "${1}" == *[!0-9-]* ]] && chat='"chat_id":"'"${1}"' NAN",' # chat id not a number! [[ "${1}" == *[!${R090909}-]* ]] && chat='"chat_id":"'"${1}"' NAN",' # chat id not a number!
fi fi
# compose final json # compose final json
json='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<"$2")"'}' json='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<"$2")"'}'
@ -527,7 +535,7 @@ fi
# $1 function $2 sleep $3 ... $n arguments # $1 function $2 sleep $3 ... $n arguments
sendJsonRetry(){ sendJsonRetry(){
local retry="${1}"; shift local retry="${1}"; shift
[[ "${1}" =~ ^\ *[0-9.]+\ *$ ]] && sleep "${1}"; shift [[ "${1}" =~ ^\ *[${R090909}.]+\ *$ ]] && sleep "${1}"; shift
printf "%s: RETRY %s %s %s\n" "$(date)" "${retry}" "${1}" "${2:0:60}" printf "%s: RETRY %s %s %s\n" "$(date)" "${retry}" "${1}" "${2:0:60}"
case "${retry}" in case "${retry}" in
'sendJson'*) 'sendJson'*)
@ -1135,7 +1143,7 @@ bot_init() {
# check if botconf seems valid # check if botconf seems valid
printf "${GREEN}This is your bot config:${NN}" printf "${GREEN}This is your bot config:${NN}"
sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]' sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]'
if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[${R090909}]+$ ]]; then
printf "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b" printf "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b"
read -r ANSWER read -r ANSWER
if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then

View File

@ -9,7 +9,7 @@ two bytes for encoding and covers almost all `Latin` alphabets, also `Greek`, `C
`Hebrew`, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details. `Hebrew`, `Arabic` and more. See [Wikipedia](https://en.wikipedia.org/wiki/UTF-8) for more details.
#### Setting up your Environment #### Setting up your Environment
In general `bash` and `GNU` utitities are UTF-8 aware aware if you to setup your environment In general `bash` and `GNU` utitities are UTF-8 aware if you to setup your environment
and your scripts accordingly (_locale setting_): and your scripts accordingly (_locale setting_):
1. Your Terminal and Editor must support UTF-8: 1. Your Terminal and Editor must support UTF-8:
@ -434,5 +434,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
#### [Prev Advanced Use](3_advanced.md) #### [Prev Advanced Use](3_advanced.md)
#### [Next Best Practice](5_practice.md) #### [Next Best Practice](5_practice.md)
#### $$VERSION$$ v1.21-5-g8a095bc #### $$VERSION$$ v1.21-6-g5f6476d

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$$ v1.21-4-g966ee5d #### $$VERSION$$ v1.21-6-g5f6476d
# #
# source from commands.sh to use jsonDB functions # source from commands.sh to use jsonDB functions
# #
@ -28,8 +28,7 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
export JSSH_LOCKNAME=".flock" export JSSH_LOCKNAME=".flock"
# in UTF-8 äöü etc. are part of [:alnum:] and ranges (e.g. a-z), but we want ASCII a-z ranges! # in UTF-8 äöü etc. are part of [:alnum:] and ranges (e.g. a-z), but we want ASCII a-z ranges!
# there are two solutions: set "LC_COLLATE=C" or set bash option "globasciiranges" # for more information see doc/4_expert.md#Character_classes
# to be independent of the settings mentioned above, we define our own "ranges"
azazaz='abcdefghijklmnopqrstuvwxyz' # a-z :lower: azazaz='abcdefghijklmnopqrstuvwxyz' # a-z :lower:
AZAZAZ='ABCDEFGHIJKLMNOPQRSTUVWXYZ' # A-Z :upper: AZAZAZ='ABCDEFGHIJKLMNOPQRSTUVWXYZ' # A-Z :upper:
R090909='0123456789' # 0-9 :digit: R090909='0123456789' # 0-9 :digit: