use printf

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-12-26 21:18:18 +01:00
parent 2e878fd864
commit 03bdecb056
4 changed files with 78 additions and 76 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$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-dev-0-g2e878fd
# #
# Exit Codes: # Exit Codes:
# - 0 success (hopefully) # - 0 success (hopefully)
@ -24,16 +24,17 @@
# - 7 can't get bottoken # - 7 can't get bottoken
# - 8 curl/wget missing # - 8 curl/wget missing
# - 10 not bash! # - 10 not bash!
# shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117 # shellcheck disable=SC2140,SC2031,SC2120,SC1091,SC1117,SC2059
# emmbeded system may claim bash but it is not # emmbeded system may claim bash but it is not
# check for bash like ARRAY handlung # check for bash like ARRAY handlung
if ! (unset a; set -A a a; eval "a=(a b)"; eval '[ -n "${a[1]}" ]'; ) > /dev/null 2>&1; then if ! (unset a; set -A a a; eval "a=(a b)"; eval '[ -n "${a[1]}" ]'; ) > /dev/null 2>&1; then
echo "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!"; printf "Error: Current shell does not support ARRAY's, may be busbox ash shell. pls install a real bash!\n"
exit 10 exit 10
fi fi
# are we running in a terminal? # are we running in a terminal?
NN="\n"
if [ -t 1 ] && [ -n "$TERM" ]; then if [ -t 1 ] && [ -n "$TERM" ]; then
CLEAR='clear' CLEAR='clear'
RED='\e[31m' RED='\e[31m'
@ -41,16 +42,17 @@ if [ -t 1 ] && [ -n "$TERM" ]; then
ORANGE='\e[35m' ORANGE='\e[35m'
GREY='\e[1;30m' GREY='\e[1;30m'
NC='\e[0m' NC='\e[0m'
NN="${NN}"
fi fi
# telegram uses utf-8 characters, check if we have an utf-8 charset # telegram uses utf-8 characters, check if we have an utf-8 charset
if [ "${LANG}" = "${LANG%[Uu][Tt][Ff]*}" ]; then if [ "${LANG}" = "${LANG%[Uu][Tt][Ff]*}" ]; then
echo -e "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}" printf "${ORANGE}Warning: Telegram uses utf-8, but looks like you are using non utf-8 locale:${NC} ${LANG}\n"
fi fi
# we need some bash 4+ features, check for old bash by feature # we need some bash 4+ features, check for old bash by feature
if [ "$({ LC_ALL=C.utf-8 echo -e "\u1111"; } 2>/dev/null)" = "\u1111" ]; then if [ "$({ LC_ALL=C.utf-8 echo -e "\u1111"; } 2>/dev/null)" = "\u1111" ]; then
echo -e "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NC}" printf "${ORANGE}Warning: Missing unicode '\uxxxx' support, missing C.utf-8 locale or to old bash version.${NN}"
fi fi
@ -117,7 +119,7 @@ debug_checks(){ {
# some linux, e.g. manajro seems not to have C locale activated by default # some linux, e.g. manajro seems not to have C locale activated by default
if _exists locale && [ "$(locale -a | grep -c -e "^C$" -e "^C.utf8$")" -lt 2 ]; then if _exists locale && [ "$(locale -a | grep -c -e "^C$" -e "^C.utf8$")" -lt 2 ]; then
echo -e "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NC}" printf "${ORANGE}Warning: locale ${NC}${GREY}C${NC}${ORANGE} and/or ${NC}${GREY}C.utf8${NC}${ORANGE} seems missing, use \"${NC}${GREY}locale -a${NC}${ORANGE}\" to show what locales are installed on your system.${NN}"
fi fi
# get location and name of bashbot.sh # get location and name of bashbot.sh
@ -134,7 +136,7 @@ if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then
fi fi
BOTCOMMANDS="start, stop, status, help, init, suspendback, resumeback, killback" BOTCOMMANDS="start, stop, status, help, init, suspendback, resumeback, killback"
[[ -z "$1" && -z "${SOURCE}" ]] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit [[ -z "$1" && -z "${SOURCE}" ]] && printf "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit
if [ "$1" = "help" ]; then if [ "$1" = "help" ]; then
HELP="${BASHBOT_HOME:-.}/README" HELP="${BASHBOT_HOME:-.}/README"
if [ -n "${CLEAR}" ];then if [ -n "${CLEAR}" ];then
@ -159,14 +161,14 @@ RUNUSER="${USER}" # USER is overwritten by bashbot array :-(, save original
# OK everything setup, lets start # OK everything setup, lets start
if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}" printf "${RED}ERROR: Can't change to ${RUNDIR} ...${NN}"
exit 1 exit 1
else else
RUNDIR="." RUNDIR="."
fi fi
if [ ! -w "." ]; then if [ ! -w "." ]; then
echo -e "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NC}" printf "${ORANGE}WARNING: ${RUNDIR} is not writeable!${NN}"
ls -ld . ls -ld .
fi fi
@ -190,12 +192,12 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
token="$(< "${TOKENFILE}")" token="$(< "${TOKENFILE}")"
# no old token, ask user # no old token, ask user
elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then
echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" printf "Running headless, set BOTTOKEN or run ${SCRIPT} init first!\n"
exit 2 exit 2
else else
${CLEAR} ${CLEAR}
echo -e "${RED}TOKEN MISSING.${NC}" printf "${RED}TOKEN MISSING.${NN}"
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" printf "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NN}"
read -r token read -r token
fi fi
[ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh" [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh"
@ -206,12 +208,12 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
if [ -r "${BOTADMIN}" ]; then if [ -r "${BOTADMIN}" ]; then
admin="$(< "${BOTADMIN}")" admin="$(< "${BOTADMIN}")"
elif [ -z "${CLEAR}" ]; then elif [ -z "${CLEAR}" ]; then
echo "Running headless, set botadmin to AUTO MODE!" printf "Running headless, set botadmin to AUTO MODE!\n"
else else
${CLEAR} ${CLEAR}
echo -e "${RED}BOTADMIN MISSING.${NC}" printf "${RED}BOTADMIN MISSING.${NN}"
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}" printf "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NN}"
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}" printf "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NN}"
read -r admin read -r admin
fi fi
[ -z "${admin}" ] && admin='?' [ -z "${admin}" ] && admin='?'
@ -219,14 +221,14 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
fi fi
# setup botacl file # setup botacl file
if [ ! -f "${BOTACL}" ]; then if [ ! -f "${BOTACL}" ]; then
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}" printf "${ORANGE}Create empty ${BOTACL} file.${NC}"
printf '\n' >"${BOTACL}" printf '\n' >"${BOTACL}"
fi fi
# setup data dir file # setup data dir file
if [ ! -d "${DATADIR}" ]; then if [ ! -d "${DATADIR}" ]; then
mkdir "${DATADIR}" mkdir "${DATADIR}"
elif [ ! -w "${DATADIR}" ]; then elif [ ! -w "${DATADIR}" ]; then
echo -e "${RED}ERROR: Can't write to ${DATADIR}!.${NC}" printf "${RED}ERROR: Can't write to ${DATADIR}!.${NN}"
ls -ld "${DATADIR}" ls -ld "${DATADIR}"
exit 2 exit 2
fi fi
@ -238,7 +240,7 @@ if [[ -z "${BOTTOKEN}" && ! -f "${BOTCONFIG}.jssh" ]]; then
sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh"
fi fi
elif [ ! -w "${COUNTFILE}.jssh" ]; then elif [ ! -w "${COUNTFILE}.jssh" ]; then
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}"
ls -l "${COUNTFILE}.jssh" ls -l "${COUNTFILE}.jssh"
exit 2 exit 2
fi fi
@ -262,13 +264,13 @@ if [ -z "${BOTTOKEN}" ]; then
BOTTOKEN="$(getConfigKey "bottoken")" BOTTOKEN="$(getConfigKey "bottoken")"
if [ -z "${BOTTOKEN}" ]; then if [ -z "${BOTTOKEN}" ]; then
BOTERROR="Warning: can't get bot token, try to recover working config..." BOTERROR="Warning: can't get bot token, try to recover working config..."
echo -e "${ORANGE}${BOTERROR}${NC} \c" printf "${ORANGE}${BOTERROR}${NC} "
if [ -r "${BOTCONFIG}.jssh.ok" ]; then if [ -r "${BOTCONFIG}.jssh.ok" ]; then
log_error "${BOTERROR}" log_error "${BOTERROR}"
cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; echo "OK" cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh"; printf "OK\n"
BOTTOKEN="$(getConfigKey "bottoken")" BOTTOKEN="$(getConfigKey "bottoken")"
else else
echo -e "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}" printf "\n${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NN}"
exit 7 exit 7
fi fi
fi fi
@ -276,16 +278,16 @@ fi
# BOTTOKEN format checks # BOTTOKEN format checks
if ! check_token "${BOTTOKEN}"; then if ! check_token "${BOTTOKEN}"; then
echo -e "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NC}" printf "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NN}"
echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c" printf "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}"
echo -e "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NC}" printf "${GREY}8-10 digits${RED}:${GREY}35 alphanumeric characters + '_-'${NN}"
echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}" printf "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NN}"
if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}: ]]; then
echo -e "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NC}" printf "${ORANGE}Possible problem in the digits part, len is $(($(wc -c <<<"${BOTTOKEN%:*}")-1))${NN}"
[ -n "$(getConfigKey "botid")" ] && echo -e "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NC}" [ -n "$(getConfigKey "botid")" ] && printf "${GREY}Did you mean: \"${NC}$(getConfigKey "botid")${GREY}\" ?${NN}"
fi fi
[[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\ [[ ! "${BOTTOKEN}" =~ :[a-zA-Z0-9_-]{35}$ ]] &&\
echo -e "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" printf "${ORANGE}Possible problem in the characters part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NN}"
fi fi
@ -317,7 +319,7 @@ export res CAPTION ME
COMMANDS="${BASHBOT_ETC:-.}/commands.sh" COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
if [ -z "${SOURCE}" ]; then if [ -z "${SOURCE}" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" printf "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NN}"
ls -l "${COMMANDS}" ls -l "${COMMANDS}"
exit 3 exit 3
fi fi
@ -338,11 +340,11 @@ done
# do we have BSD sed # do we have BSD sed
if ! sed '1ia' </dev/null 2>/dev/null; then if ! sed '1ia' </dev/null 2>/dev/null; then
echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" printf "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NN}"
fi fi
#jsonDB is now mandatory #jsonDB is now mandatory
if ! _is_function jssh_newDB ; then if ! _is_function jssh_newDB ; then
echo -e "${RED}ERROR: Mandatory module jsonDB is missing or not readable!" printf "${RED}ERROR: Mandatory module jsonDB is missing or not readable!${NN}"
exit 6 exit 6
fi fi
@ -523,9 +525,9 @@ else
else else
# ups, no curl AND no wget # ups, no curl AND no wget
if [ -n "${BASHBOT_WGET}" ]; then if [ -n "${BASHBOT_WGET}" ]; then
echo -e "${RED}Error: You set BASHBOT_WGET but no wget found!${NC}" printf "${RED}Error: You set BASHBOT_WGET but no wget found!${NN}"
else else
echo -e "${RED}Error: curl and wget not found, install curl!${NC}" printf "${RED}Error: curl and wget not found, install curl!${NN}"
fi fi
exit 8 exit 8
fi fi
@ -649,7 +651,7 @@ getBotName() {
# save botname and id # save botname and id
setConfigKey "botname" "${BOTARRAY["result","username"]}" setConfigKey "botname" "${BOTARRAY["result","username"]}"
setConfigKey "botid" "${BOTARRAY["result","id"]}" setConfigKey "botid" "${BOTARRAY["result","id"]}"
echo "${BOTARRAY["result","username"]}" printf "${BOTARRAY["result","username"]}\n"
} }
# pure bash implementation, done by KayM (@gnadelwartz) # pure bash implementation, done by KayM (@gnadelwartz)
@ -1102,42 +1104,42 @@ bot_init() {
[ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1
local DEBUG="$1" local DEBUG="$1"
# upgrade from old version # upgrade from old version
echo "Check for Update actions ..." printf "Check for Update actions ...\n"
local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash"
[ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; } [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; }
# no more existing modules # no more existing modules
[ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh"
# load addons on startup # load addons on startup
echo "Done." printf "Done.\n"
echo "Initialize modules and addons ..." printf "Initialize modules and addons ...\n"
for addons in "${ADDONDIR:-.}"/*.sh ; do for addons in "${ADDONDIR:-.}"/*.sh ; do
# shellcheck source=./modules/aliases.sh # shellcheck source=./modules/aliases.sh
[ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}"
done done
echo "Done." printf "Done.\n"
if [[ ! -d "logs" ]]; then if [[ ! -d "logs" ]]; then
echo "Move Logfiles ..." printf "Move Logfiles ...\n"
mkdir logs 2>/dev/null mkdir logs 2>/dev/null
for MVLOG in DEBUG.log MESSAGE.log ERROR.log BASHBOT.log for MVLOG in DEBUG.log MESSAGE.log ERROR.log BASHBOT.log
do do
[ -f "${MVLOG}" ] && mv "${MVLOG}" logs 2>/dev/null [ -f "${MVLOG}" ] && mv "${MVLOG}" logs 2>/dev/null
done done
echo "Done." printf "Done.\n"
fi fi
# setup bashbot # setup bashbot
[[ "${UID}" -eq "0" ]] && RUNUSER="nobody" [[ "${UID}" -eq "0" ]] && RUNUSER="nobody"
echo -n "Enter User to run bashbot [$RUNUSER]: " printf "Enter User to run bashbot [$RUNUSER]: "
read -r TOUSER read -r TOUSER
[ -z "$TOUSER" ] && TOUSER="$RUNUSER" [ -z "$TOUSER" ] && TOUSER="$RUNUSER"
if ! id "$TOUSER" &>/dev/null; then if ! id "$TOUSER" &>/dev/null; then
echo -e "${RED}User \"$TOUSER\" not found!${NC}" printf "${RED}User \"$TOUSER\" not found!${NN}"
exit 3 exit 3
else else
# shellcheck disable=SC2009 # shellcheck disable=SC2009
oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )" oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )"
[ -n "${oldbot}" ] && \ [ -n "${oldbot}" ] && \
echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}" printf "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NN}${oldbot}\n"
echo "Adjusting files and permissions for user \"${TOUSER}\" ..." printf "Adjusting files and permissions for user \"${TOUSER}\" ...\n"
[ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc"
chown -R "$TOUSER" . ./* chown -R "$TOUSER" . ./*
chmod 711 . chmod 711 .
@ -1146,45 +1148,45 @@ bot_init() {
chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
# jsshDB must writeable by owner # jsshDB must writeable by owner
find . -name '*.jssh*' -exec chmod u+w \{\} + find . -name '*.jssh*' -exec chmod u+w \{\} +
echo "Done." printf "Done.\n"
fi fi
# ask to check bottoken online # ask to check bottoken online
if [ -z "$(getConfigKey "botid")" ]; then if [ -z "$(getConfigKey "botid")" ]; then
echo -e "Seems to be your first init. Should I verify your bot token online? (y/N) N\b\c" printf "Seems to be your first init. Should I verify your bot token online? (y/N) N\b"
read -r ANSWER read -r ANSWER
if [[ "${ANSWER}" =~ ^[Yy] ]]; then if [[ "${ANSWER}" =~ ^[Yy] ]]; then
echo -e "${GREEN}Contacting telegram to verify your bot token ...${NC}" printf "${GREEN}Contacting telegram to verify your bot token ...${NN}"
$0 botname $0 botname
fi fi
fi fi
# check if botconf if seems valid # check if botconf if seems valid
echo -e "${GREEN}This is your bot config:${NC}" 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 [[ "$(getConfigKey "bottoken")" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ && "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then if [[ "$(getConfigKey "bottoken")" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ && "$(getConfigKey "botadmin")" =~ ^[0-9]+$ ]]; then
echo -e "Bot config seems to be valid. Should I make a backup copy? (Y/n) Y\b\c" 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
echo "Copy bot config to ${BOTCONFIG}.jssh.ok ..." printf "Copy bot config to ${BOTCONFIG}.jssh.ok ...\n"
cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok" cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok"
fi fi
else else
echo -e "${ORANGE}Bot config may not complete, pls check.${NC}" printf "${ORANGE}Bot config may not complete, pls check.${NN}"
fi fi
# show result # show result
ls -ld "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh 2>/dev/null ls -ld "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh 2>/dev/null
} }
if ! _is_function send_message ; then if ! _is_function send_message ; then
echo -e "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NC}" printf "${RED}ERROR: send_message is not available, did you deactivate ${MODULEDIR}/sendMessage.sh?${NN}"
exit 1 exit 1
fi fi
# get location of JSON.sh, download if not exist # get location of JSON.sh, download if not exist
JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}" JSONSHFILE="${BASHBOT_JSONSH:-${SCRIPTDIR}/JSON.sh/JSON.sh}"
[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3 [[ "${JSONSHFILE}" != *"/JSON.sh" ]] && printf "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NN}" && exit 3
if [ ! -f "${JSONSHFILE}" ]; then if [ ! -f "${JSONSHFILE}" ]; then
echo "Seems to be first run, Downloading ${JSONSHFILE}..." printf "Seems to be first run, Downloading ${JSONSHFILE}...\n"
[ "${SCRIPTDIR}/JSON.sh/JSON.sh" = "${JSONSHFILE}" ] &&\ [ "${SCRIPTDIR}/JSON.sh/JSON.sh" = "${JSONSHFILE}" ] &&\
mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh" mkdir "${SCRIPTDIR}/JSON.sh" 2>/dev/null && chmod +w "${SCRIPTDIR}/JSON.sh"
getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}" getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}"
@ -1210,14 +1212,14 @@ if [ -z "${SOURCE}" ]; then
ME="$(getBotName)" ME="$(getBotName)"
if [ -n "${ME}" ]; then if [ -n "${ME}" ]; then
# ok we have a connection and got botname, save it # ok we have a connection and got botname, save it
[ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}" [ -n "${CLEAR}" ] && printf "${GREY}Bottoken is valid ...${NN}"
jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}" jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}"
rm -f "${BOTCONFIG}.jssh.flock" rm -f "${BOTCONFIG}.jssh.flock"
else else
echo -e "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NC}" printf "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NN}"
ME="$(getConfigKey "botname")" ME="$(getConfigKey "botname")"
if [ -z "$ME" ]; then if [ -z "$ME" ]; then
echo -e "${RED}ERROR: No cached botname, can't continue! ...${NC}" printf "${RED}ERROR: No cached botname, can't continue! ...${NN}"
exit 1 exit 1
fi fi
fi fi
@ -1226,8 +1228,8 @@ if [ -z "${SOURCE}" ]; then
;;& ;;&
# used to send output of background and interactive to chats # used to send output of background and interactive to chats
"outproc") # $2 chat_id $3 identifier of job, internal use only! "outproc") # $2 chat_id $3 identifier of job, internal use only!
[ -z "$3" ] && echo "No job identifier" && exit 3 [ -z "$3" ] && printf "No job identifier\n" && exit 3
[ -z "$2" ] && echo "No chat to send to" && exit 3 [ -z "$2" ] && printf "No chat to send to\n" && exit 3
ME="$(getConfigKey "botname")" ME="$(getConfigKey "botname")"
# read until terminated # read until terminated
while read -r line ;do while read -r line ;do
@ -1289,7 +1291,7 @@ if [ -z "${SOURCE}" ]; then
;; ;;
# send message to all users # send message to all users
'broadcast') 'broadcast')
echo -e "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NC}" printf "${ORANGE}Broadcast is a separate command now, see ${BASHBOT_HOME:-.}/bin/send_broadcast.sh --help${NN}"
"${BASHBOT_HOME:-.}"/bin/send_broadcast.sh --help "${BASHBOT_HOME:-.}"/bin/send_broadcast.sh --help
exit exit
;; ;;
@ -1299,10 +1301,10 @@ if [ -z "${SOURCE}" ]; then
SESSION="${ME:-_bot}-startbot" SESSION="${ME:-_bot}-startbot"
BOTPID="$(proclist "${SESSION}")" BOTPID="$(proclist "${SESSION}")"
if [ -n "${BOTPID}" ]; then if [ -n "${BOTPID}" ]; then
echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}" printf "${GREEN}Bot is running with UID ${RUNUSER}.${NN}"
exit exit
else else
echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}"
exit 5 exit 5
fi fi
debug_checks "end status" "$@" debug_checks "end status" "$@"
@ -1319,9 +1321,9 @@ if [ -z "${SOURCE}" ]; then
printf "Session Name: %s\n" "${SESSION}" printf "Session Name: %s\n" "${SESSION}"
sleep 1 sleep 1
if [ -n "$(proclist "${SESSION}")" ]; then if [ -n "$(proclist "${SESSION}")" ]; then
echo -e "${GREEN}Bot started successfully.${NC}" printf "${GREEN}Bot started successfully.${NN}"
else else
echo -e "${RED}An error occurred while starting the bot.${NC}" printf "${RED}An error occurred while starting the bot.${NN}"
exit 5 exit 5
fi fi
debug_checks "end start" "$@" debug_checks "end start" "$@"
@ -1337,34 +1339,34 @@ if [ -z "${SOURCE}" ]; then
# inform botadmin about stop # inform botadmin about stop
ADMIN="$(getConfigKey "botadmin")" ADMIN="$(getConfigKey "botadmin")"
[ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." & [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." &
echo -e "${GREEN}OK. Bot stopped successfully.${NC}" printf "${GREEN}OK. Bot stopped successfully.${NN}"
else else
echo -e "${RED}An error occurred while stopping bot.${NC}" printf "${RED}An error occurred while stopping bot.${NN}"
exit 5 exit 5
fi fi
else else
echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" printf "${ORANGE}No Bot running with UID ${RUNUSER}.${NN}"
fi fi
debug_checks "end stop" "$@" debug_checks "end stop" "$@"
exit exit
;; ;;
# suspend, resume or kill background jobs # suspend, resume or kill background jobs
"suspendb"*|"resumeb"*|"killb"*) "suspendb"*|"resumeb"*|"killb"*)
_is_function job_control || { echo -e "${RED}Module background is not available!${NC}"; exit 3; } _is_function job_control || { printf "${RED}Module background is not available!${NN}"; exit 3; }
ME="$(getConfigKey "botname")" ME="$(getConfigKey "botname")"
job_control "$1" job_control "$1"
debug_checks "end background $1" "$@" debug_checks "end background $1" "$@"
;; ;;
*) *)
echo -e "${RED}${REALME##*/}: unknown command${NC}" printf "${RED}${REALME##*/}: unknown command${NN}"
echo -e "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit printf "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NN}" && exit
exit 4 exit 4
;; ;;
esac esac
# warn if root # warn if root
if [[ "${UID}" -eq "0" ]] ; then if [[ "${UID}" -eq "0" ]] ; then
echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" printf "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NN}"
echo -e "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NC}" printf "${ORANGE}You are at HIGH RISK when running a Telegram BOT with root privileges!${NN}"
fi fi
fi # end source fi # end source

View File

@ -22,7 +22,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de # AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 23.12.2020 16:52 # CREATED: 23.12.2020 16:52
# #
#### $$VERSION$$ v1.20-4-g1bd1727 #### $$VERSION$$ v1.21-dev-0-g2e878fd
#=============================================================================== #===============================================================================
#### ####

View File

@ -21,7 +21,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de # AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 25.12.2020 20:24 # CREATED: 25.12.2020 20:24
# #
#### $$VERSION$$ v1.20-4-g1bd1727 #### $$VERSION$$ v1.21-dev-0-g2e878fd
#=============================================================================== #===============================================================================
#### ####

View File

@ -21,7 +21,7 @@
# AUTHOR: KayM (gnadelwartz), kay@rrr.de # AUTHOR: KayM (gnadelwartz), kay@rrr.de
# CREATED: 16.12.2020 11:34 # CREATED: 16.12.2020 11:34
# #
#### $$VERSION$$ v1.20-4-g1bd1727 #### $$VERSION$$ v1.21-dev-0-g2e878fd
#=============================================================================== #===============================================================================
#### ####