diff --git a/bashbot.sh b/bashbot.sh index 8d7906f..3bd3b77 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.21-0-gc85af77 +#### $$VERSION$$ v1.21-6-g5f6476d ################################################################## # 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 +# 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 # returns true if command exist _exists() { @@ -80,22 +88,22 @@ _is_function() { # 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 _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 } setConfigKey() { - [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + [[ "$1" =~ ^[-${azAZ09},._]+$ ]] || return 3 [ -z "${BOTCONFIG}" ] && return 1 printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh" } 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 } # check if $1 seems a valid token # return true if token seems to be valid check_token(){ - [[ "${1}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]] && return 0 + [[ "${1}" =~ ^[${R090909}]{8,10}:[${azAZ09}_-]{35}$ ]] && return 0 return 1 } # log $1 with date @@ -278,11 +286,11 @@ if ! check_token "${BOTTOKEN}"; then "${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" - 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" [ -n "$(getConfigKey "botid")" ] && printf "\t${GREEN}Did you mean: \"${NC}$(getConfigKey "botid")${GREEN}\" ?${NN}" 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 "\n" fi @@ -428,14 +436,14 @@ if ! _exists iconv; then fi TIMEOUT="${BASHBOT_TIMEOUT}" -[[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20" +[[ "$TIMEOUT" =~ ^[${R090909}]+$ ]] || TIMEOUT="20" # usage: sendJson "chat" "JSON" "URL" sendJson(){ local json chat="" if [ -n "${1}" ]; then 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 # compose final json json='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<"$2")"'}' @@ -527,7 +535,7 @@ fi # $1 function $2 sleep $3 ... $n arguments sendJsonRetry(){ 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}" case "${retry}" in 'sendJson'*) @@ -1135,7 +1143,7 @@ bot_init() { # check if botconf seems valid printf "${GREEN}This is your bot config:${NN}" 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" read -r ANSWER if [[ -z "${ANSWER}" || "${ANSWER}" =~ ^[^Nn] ]]; then diff --git a/doc/4_expert.md b/doc/4_expert.md index a4f6f9a..d197256 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -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. #### 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_): 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) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v1.21-5-g8a095bc +#### $$VERSION$$ v1.21-6-g5f6476d diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index d27288b..fed9ea4 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # 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 # @@ -28,8 +28,7 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" 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! -# there are two solutions: set "LC_COLLATE=C" or set bash option "globasciiranges" -# to be independent of the settings mentioned above, we define our own "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: