From c0a633f42d9b8ce16fc98125683ca7832f7c41d7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 14 May 2020 19:47:37 +0200 Subject: [PATCH] optimze sourcing of modules --- bashbot.sh | 19 ++++++++++--------- commands.sh | 12 +++++++----- modules/aliases.sh | 5 ++++- modules/answerInline.sh | 5 ++++- modules/background.sh | 5 ++++- modules/chatMember.sh | 5 ++++- modules/jsonDB.sh | 5 ++++- modules/sendMessage.sh | 5 ++++- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 28edf03..e0209cb 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-pre-0-gac2ec02 +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # # Exit Codes: # - 0 sucess (hopefully) @@ -39,7 +39,7 @@ RUNDIR="$(dirname "$0")" MODULEDIR="${SCRIPTDIR}/modules" - +# adjust locations based on source and real name if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then SOURCE="yes" else @@ -69,20 +69,21 @@ if [ ! -w "." ]; then ls -ld . fi - +# if BOTTOKEN is empty read from file TOKENFILE="${BASHBOT_ETC:-.}/token" -if [ ! -f "${TOKENFILE}" ]; then +if [ -z "${BOTTOKEN}" ] && [ ! -f "${TOKENFILE}" ]; then if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then - echo "Running headless, run ${SCRIPT} init first!" + echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" exit 2 else ${CLEAR} echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" - read -r token - printf '%s\n' "${token}" > "${TOKENFILE}" + read -r BOTTOKEN + printf '%s\n' "${BOTTOKEN}" > "${TOKENFILE}" fi fi +[ -z "${BOTTOKEN}" ] && BOTTOKEN="$(< "${TOKENFILE}")" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" if [ ! -f "${BOTADMIN}" ]; then @@ -124,7 +125,6 @@ elif [ ! -w "${COUNTFILE}" ]; then exit 2 fi -BOTTOKEN="$(< "${TOKENFILE}")" URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" ME_URL=$URL'/getMe' @@ -155,7 +155,7 @@ fi # load modules for modules in "${MODULEDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh - [ -r "${modules}" ] && source "${modules}" "source" + if ! _is_function "$(basename "${modules}")" && [ -r "${modules}" ]; then source "${modules}" "source"; fi done ################# @@ -677,6 +677,7 @@ bot_init() { # upgrade from old version local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${DATADIR}"; rmdir "${OLDTMP}"; } + # no more existing modules [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" # load addons on startup for addons in "${ADDONDIR:-.}"/*.sh ; do diff --git a/commands.sh b/commands.sh index d139b5e..e48f35e 100644 --- a/commands.sh +++ b/commands.sh @@ -15,7 +15,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-pre-0-gac2ec02 +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # # adjust your language setting here, e.g.when run from other user or cron. @@ -51,12 +51,14 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ' # load modues on startup and always on on debug -if [ "${1}" = "startbot" ] || [[ "${1}" = *"debug"* ]] ; then - # load all readable modules - for modules in "${MODULEDIR:-.}"/*.sh ; do +if [ "${1}" != "" ]; then + # load all readable modules + for modules in "${MODULEDIR:-.}"/*.sh ; do + if [[ "${1}" == *"debug"* ]] || ! _is_function "$(basename "${modules}")"; then # shellcheck source=./modules/aliases.sh [ -r "${modules}" ] && source "${modules}" "${1}" - done + fi + done fi # diff --git a/modules/aliases.sh b/modules/aliases.sh index 0347c11..853c1b7 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -5,10 +5,13 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # # source from commands.sh to use the aliases +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" + # easy handling of users: _is_botadmin() { user_is_botadmin "${USER[ID]}" diff --git a/modules/answerInline.sh b/modules/answerInline.sh index 578217a..339a688 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.sh @@ -5,10 +5,13 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # source from commands.sh to use the inline functions +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" + INLINE_QUERY=$URL'/answerInlineQuery' answer_inline_query() { diff --git a/modules/background.sh b/modules/background.sh index 78eebec..b5f85ba 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,10 +5,13 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # source from commands.sh if you want ro use interactive or background jobs +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" + ###### # interactive and background functions diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 9f220d1..5053164 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.sh @@ -5,7 +5,10 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 + +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" # source from commands.sh to use the member functions diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 35e1e4a..b304cd3 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,13 +5,16 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 # # source from commands.sh to use jsonDB functions # # jsonDB provides simple functions to read and store bash Arrays # from to file in JSON.sh output format, its a simple key/value storage. +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" + # read content of a file in JSON.sh format into given ARRAY # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $2 filename, must be relative to BASHBOT_ETC, and not contain '..' diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index c5762e3..eeadfbc 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,10 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.94-dev3-0-geef955a +#### $$VERSION$$ v0.94-pre-1-g4aa7561 + +# source once magic, function named like file +eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" # source from commands.sh to use the sendMessage functions