diff --git a/bashbot.sh b/bashbot.sh index 29045a2..34b8e46 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.35-dev-27-gca9ea1b +#### $$VERSION$$ v1.35-dev-28-g3f331dc ################################################################## # emmbeded system may claim bash but it is not @@ -1103,105 +1103,27 @@ start_bot() { done } +# fallback version, full version is in bin/bashbot_init.in.sh # initialize bot environment, user and permissions bot_init() { [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 - local runuser chown touser botname DEBUG="$1" - # upgrade from old version - # currently no action - printf "Check for Update actions ...\n" - printf "Done.\n" # load addons on startup printf "Initialize modules and addons ...\n" for addons in "${ADDONDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh - [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" + [ -r "${addons}" ] && source "${addons}" "init" "$1" done printf "Done.\n" - # ask for bashbot user - runuser="${RUNUSER}"; [ "${UID}" = "0" ] && runuser="nobody" - printf "Enter User to run bashbot [${runuser}]: " - read -r chown - [ -z "${chown}" ] && chown="${runuser}"; touser="${chown%:*}" - # check user ... - if ! id "${touser}" &>/dev/null; then - printf "${RED}User \"${touser}\" does not exist!${NN}" - exit 3 - elif [[ "${UID}" != "0" && "${touser}" != "${runuser}" ]]; then - # different user but not root ... - printf "${ORANGE}You are not root, adjusting permissions may fail. Try \"sudo ./bashbot.sh init\"${NN}Press to stop or to continue..." 1>&2 - [ -n "${INTERACTIVE}" ] && read -r runuser - fi - # check if mycommands exist - if [ ! -r "${BASHBOT_ETC:-.}/mycommands.sh" ]; then - printf "Mycommands.sh not found, copy ${GREY}lean file, xamples or one${NC} to mycommands.sh? (c/e/N) N\b" - read -r ANSWER - [[ "${ANSWER}" =~ ^[cC] ]] && cp -f "${BASHBOT_ETC:-.}/mycommands.sh.clean" "${BASHBOT_ETC:-.}/mycommands.sh" - [[ "${ANSWER}" =~ ^[eE] ]] && cp -f "${BASHBOT_ETC:-.}/mycommands.sh.dist" "${BASHBOT_ETC:-.}/mycommands.sh" - # offer to copy config also - if [ ! -r "${BASHBOT_ETC:-.}/mycommands.conf" ]; then - printf "Mycommands config file not found, copy ${GREY}mycommands.conf.dist${NC} to mycommands.conf? (Y/n) Y\b" - read -r ANSWER - [[ "${ANSWER}" =~ ^[nN] ]] || cp -f "${BASHBOT_ETC:-.}/mycommands.conf.dist" "${BASHBOT_ETC:-.}/mycommands.conf" - fi - # adjust INLINE CALLBACK MEONLY SILENCER - if [ -w "${BASHBOT_ETC:-.}/mycommands.conf" ]; then - printf "Activate processing for ${GREY}nline queries, allback buttons, oth or one${NC} in mycommands.sh? (i/c/b/N) N\b" - read -r ANSWER - [[ "${ANSWER}" =~ ^[iIbB] ]] && sed -i '/INLINE="/ s/^.*$/export INLINE="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" - [[ "${ANSWER}" =~ ^[cCbB] ]] && sed -i '/CALLBACK="/ s/^.*$/export CALLBACK="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" - printf "Always ignore commands for other Bots in chat ${GREY}(/cmd@other_bot)${NC}? (y/N) N\b" - read -r ANSWER - [[ "${ANSWER}" =~ ^[yY] ]] && sed -i '/MEONLY="/ s/^.*$/export MEONLY="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" - printf "Delete administrative messages in chats ${GREY}(pinned, user join/leave, ...)${NC}? (y/N) N\b" - read -r ANSWER - [[ "${ANSWER}" =~ ^[yY] ]] && sed -i '/SILENCER="/ s/^.*$/export SILENCER="yes"/' "${BASHBOT_ETC:-.}/mycommands.conf" - fi - printf "Done.\n" - fi # adjust permissions - printf "Adjusting files and permissions for user \"${touser}\" ...\n" + printf "Adjusting files and permissions ...\n" chmod 711 . chmod -R o-w ./* chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" logs "${LOGDIR}/"*.log 2>/dev/null chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner find . -name '*.jssh*' -exec chmod u+w \{\} + - chown -Rf "${chown}" . ./* printf "Done.\n" - # adjust values in bashbot.rc - if [ -w "bashbot.rc" ]; then - printf "Adjust user and botname in bashbot.rc ...\n" - sed -i '/^[# ]*runas=/ s|runas=.*$|runas="'"${touser}"'"|' "bashbot.rc" - sed -i '/^[# ]*bashbot=/ s|bashbot=.*$|bashbot="cd '"${PWD}"'; '"${PWD}"'/'"${0##*/}"'"|' "bashbot.rc" - botname="$(getConfigKey "botname")" - [ -n "${botname}" ] && sed -i '/^[# ]*name=/ s|name=.*$|name="'"${botname}"'"|' "bashbot.rc" - printf "Done.\n" - fi - # ask to check bottoken online - if [ -z "$(getConfigKey "botid")" ]; then - printf "Seems to be your first init. Should I verify your bot token online? (y/N) N\b" - read -r ANSWER - if [[ "${ANSWER}" =~ ^[Yy] ]]; then - printf "${GREEN}Contacting telegram to verify your bot token ...${NN}" - $0 botname - fi - fi - # check if botconf seems valid - printf "${GREEN}This is your bot config:${NN}${GREY}" - sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]'; printf "${NC}" - if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[${o9o9o9}]+$ ]]; 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 - printf "Copy bot config to ${BOTCONFIG}.jssh.ok ...\n" - cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok" - fi - else - printf "${ORANGE}Bot config may incomplete, pls check.${NN}" - fi - # show result - printf "${GREY}"; ls -ldp "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh ./*.conf 2>/dev/null; printf "${NC}" + _exec_if_function my_init } if ! _is_function send_message ; then diff --git a/bin/bashbot_init.inc.sh b/bin/bashbot_init.inc.sh index 5b86a82..042e9e0 100644 --- a/bin/bashbot_init.inc.sh +++ b/bin/bashbot_init.inc.sh @@ -11,19 +11,126 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 27.01.2021 13:42 # -#### $$VERSION$$ v1.35-dev-27-gca9ea1b +#### $$VERSION$$ v1.35-dev-28-g3f331dc #=============================================================================== - +# shellcheck disable=SC2059 ########## # commands to execute before bot_init() is called -######### -# uncomment to overwrite default init -# bot_init() { my_init(); } ######## # called after default init is finished my_init() { : # ypur commands here } + + +######### +# extended initialisation for advanced features like: +# +# running bto as rest +# +# +# delete from here to disable extended init +bot_init() { + [ -n "${BASHBOT_HOME}" ] && cd "${BASHBOT_HOME}" || exit 1 + local runuser chown touser botname DEBUG="$1" + # upgrade from old version + # currently no action + printf "Check for Update actions ...\n" + printf "Done.\n" + # load addons on startup + printf "Initialize modules and addons ...\n" + for addons in "${ADDONDIR:-.}"/*.sh ; do + # shellcheck source=./modules/aliases.sh + [ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}" + done + printf "Done.\n" + # ask for bashbot user + # shellcheck disable=SC2153 + runuser="${RUNUSER}"; [ "${UID}" = "0" ] && runuser="nobody" + printf "Enter User to run bashbot [${runuser}]: " + read -r chown + [ -z "${chown}" ] && chown="${runuser}"; touser="${chown%:*}" + # check user ... + if ! id "${touser}" &>/dev/null; then + printf "${RED}User \"${touser}\" does not exist!${NN}" + exit 3 + elif [[ "${UID}" != "0" && "${touser}" != "${runuser}" ]]; then + # different user but not root ... + printf "${ORANGE}You are not root, adjusting permissions may fail. Try \"sudo ./bashbot.sh init\"${NN}Press to stop or to continue..." 1>&2 + [ -n "${INTERACTIVE}" ] && read -r runuser + fi + # check if mycommands exist + if [ ! -r "${BASHBOT_ETC:-.}/mycommands.sh" ]; then + printf "Mycommands.sh not found, copy ${GREY}lean file, xamples or one${NC} to mycommands.sh? (c/e/N) N\b" + read -r ANSWER + [[ "${ANSWER}" =~ ^[cC] ]] && cp -f "${BASHBOT_ETC:-.}/mycommands.sh.clean" "${BASHBOT_ETC:-.}/mycommands.sh" + [[ "${ANSWER}" =~ ^[eE] ]] && cp -f "${BASHBOT_ETC:-.}/mycommands.sh.dist" "${BASHBOT_ETC:-.}/mycommands.sh" + # offer to copy config also + if [ ! -r "${BASHBOT_ETC:-.}/mycommands.conf" ]; then + printf "Mycommands config file not found, copy ${GREY}mycommands.conf.dist${NC} to mycommands.conf? (Y/n) Y\b" + read -r ANSWER + [[ "${ANSWER}" =~ ^[nN] ]] || cp -f "${BASHBOT_ETC:-.}/mycommands.conf.dist" "${BASHBOT_ETC:-.}/mycommands.conf" + fi + # adjust INLINE CALLBACK MEONLY SILENCER + if [ -w "${BASHBOT_ETC:-.}/mycommands.conf" ]; then + printf "Activate processing for ${GREY}nline queries, allback buttons, oth or one${NC} in mycommands.sh? (i/c/b/N) N\b" + read -r ANSWER + [[ "${ANSWER}" =~ ^[iIbB] ]] && sed -i '/INLINE="/ s/^.*$/export INLINE="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" + [[ "${ANSWER}" =~ ^[cCbB] ]] && sed -i '/CALLBACK="/ s/^.*$/export CALLBACK="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" + printf "Always ignore commands for other Bots in chat ${GREY}(/cmd@other_bot)${NC}? (y/N) N\b" + read -r ANSWER + [[ "${ANSWER}" =~ ^[yY] ]] && sed -i '/MEONLY="/ s/^.*$/export MEONLY="1"/' "${BASHBOT_ETC:-.}/mycommands.conf" + printf "Delete administrative messages in chats ${GREY}(pinned, user join/leave, ...)${NC}? (y/N) N\b" + read -r ANSWER + [[ "${ANSWER}" =~ ^[yY] ]] && sed -i '/SILENCER="/ s/^.*$/export SILENCER="yes"/' "${BASHBOT_ETC:-.}/mycommands.conf" + fi + printf "Done.\n" + fi + # adjust permissions + printf "Adjusting files and permissions for user \"${touser}\" ...\n" + chmod 711 . + chmod -R o-w ./* + chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" logs "${LOGDIR}/"*.log 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTACL}" 2>/dev/null + # jsshDB must writeable by owner + find . -name '*.jssh*' -exec chmod u+w \{\} + + chown -Rf "${chown}" . ./* + printf "Done.\n" + # adjust values in bashbot.rc + if [ -w "bashbot.rc" ]; then + printf "Adjust user and botname in bashbot.rc ...\n" + sed -i '/^[# ]*runas=/ s|runas=.*$|runas="'"${touser}"'"|' "bashbot.rc" + sed -i '/^[# ]*bashbot=/ s|bashbot=.*$|bashbot="cd '"${PWD}"'; '"${PWD}"'/'"${0##*/}"'"|' "bashbot.rc" + botname="$(getConfigKey "botname")" + [ -n "${botname}" ] && sed -i '/^[# ]*name=/ s|name=.*$|name="'"${botname}"'"|' "bashbot.rc" + printf "Done.\n" + fi + # ask to check bottoken online + if [ -z "$(getConfigKey "botid")" ]; then + printf "Seems to be your first init. Should I verify your bot token online? (y/N) N\b" + read -r ANSWER + if [[ "${ANSWER}" =~ ^[Yy] ]]; then + printf "${GREEN}Contacting telegram to verify your bot token ...${NN}" + $0 botname + fi + fi + # check if botconf seems valid + printf "${GREEN}This is your bot config:${NN}${GREY}" + sed 's/^/\t/' "${BOTCONFIG}.jssh" | grep -vF '["bot_config_key"]'; printf "${NC}" + if check_token "$(getConfigKey "bottoken")" && [[ "$(getConfigKey "botadmin")" =~ ^[${o9o9o9}]+$ ]]; 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 + printf "Copy bot config to ${BOTCONFIG}.jssh.ok ...\n" + cp "${BOTCONFIG}.jssh" "${BOTCONFIG}.jssh.ok" + fi + else + printf "${ORANGE}Bot config may incomplete, pls check.${NN}" + fi + # show result + printf "${GREY}"; ls -ldp "${DATADIR}" "${LOGDIR}" ./*.jssh* ./*.sh ./*.conf 2>/dev/null; printf "${NC}" + _exec_if_function my_init +}