diff --git a/README.md b/README.md index 20127e9..d5706cf 100644 --- a/README.md +++ b/README.md @@ -102,4 +102,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/README.txt b/README.txt index 6482eff..38d6a2d 100644 --- a/README.txt +++ b/README.txt @@ -166,5 +166,5 @@ That's it! If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -latexmath:[\[VERSION\]] 0.70-dev-11-g41b8e69 +latexmath:[\[VERSION\]] 0.70-dev-16-g2eac362 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/bashbot.rc b/bashbot.rc index f897dc1..b1abc0e 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index a7cfbc6..a21c1d1 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,7 +10,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # # Exit Codes: # - 0 sucess (hopefully) @@ -34,7 +34,7 @@ SCRIPT="./$(basename "$0")" SCRIPTDIR="$(dirname "$0")" RUNUSER="${USER}" # USER is overwritten by bashbot array, $USER may not work later on... -if ! cd "${SCRIPTDIR}" ; then +if [ "$1" != "source" ] && ! cd "${SCRIPTDIR}" ; then echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}" exit 1 fi @@ -95,28 +95,31 @@ elif [ ! -w "${TMPDIR}" ]; then exit 2 fi -COUNT="./count" -if [ ! -f "${COUNT}" ]; then - touch "${COUNT}" -elif [ ! -w "${COUNT}" ]; then +COUNTFILE="./count" +if [ ! -f "${COUNTFILE}" ]; then + touch "${COUNTFILE}" +elif [ ! -w "${COUNTFILE}" ]; then ${CLEAR} - echo -e "${RED}ERROR: Can't write to ${COUNT}!.${NC}" - ls -l "${COUNT}" + echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" + ls -l "${COUNTFILE}" exit 2 fi COMMANDS="./commands.sh" -if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then - ${CLEAR} - echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" - exit 3 - ls -l "${COMMANDS}" +if [ "$1" != "source" ]; then + if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then + ${CLEAR} + echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" + ls -l "${COMMANDS}" + exit 3 + fi + # shellcheck source=./commands.sh + source "${COMMANDS}" "source" fi -# shellcheck source=./commands.sh -source "${COMMANDS}" "source" -URL='https://api.telegram.org/bot'$TOKEN +BOTTOKEN="$(cat "${TOKENFILE}")" +URL='https://api.telegram.org/bot'$BOTTOKEN MSG_URL=$URL'/sendMessage' @@ -139,7 +142,7 @@ DELETE_URL=$URL'/deleteMessage' GETMEMBER_URL=$URL'/getChatMember' -FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' +FILE_URL='https://api.telegram.org/file/bot'$BOTTOKEN'/' UPD_URL=$URL'/getUpdates?offset=' GET_URL=$URL'/getFile' OFFSET=0 @@ -592,7 +595,7 @@ process_client() { source commands.sh tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} + grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNTFILE} # To get user count execute bash bashbot.sh count } # get bot name @@ -636,7 +639,10 @@ else fi # source the script with source as param to use functions in other scripts -while [ "$1" = "startbot" ]; do { +# do not execute if read from other scripts + +if [ "$1" != "source" ]; then + while [ "$1" = "startbot" ]; do { UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" @@ -652,10 +658,10 @@ while [ "$1" = "startbot" ]; do { fi fi -}; done + }; done -case "$1" in + case "$1" in "outproc") until [ "$line" = "imprettydarnsuredatdisisdaendofdacmd" ];do line="" @@ -665,15 +671,15 @@ case "$1" in rm -f -r "${TMPDIR:-.}/$3" ;; "count") - echo "A total of $(wc -l <"${COUNT}") users used me." + echo "A total of $(wc -l <"${COUNTFILE}") users used me." exit ;; "broadcast") - NUMCOUNT="$(wc -l <"${COUNT}")" + NUMCOUNT="$(wc -l <"${COUNTFILE}")" echo "Sending the broadcast $* to $NUMCOUNT users." [ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5" shift - while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNT}" + while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" ;; "start") ${CLEAR} @@ -696,8 +702,8 @@ case "$1" in chown -R "$TOUSER" . ./* chmod 711 . chmod -R a-w ./* - chmod -R u+w "${COUNT}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null - chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null + chmod -R u+w "${COUNTFILE}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null + chmod -R o-r,o-w "${COUNTFILE}" "${TMPDIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null ls -la exit fi @@ -754,19 +760,19 @@ case "$1" in tmux attach -t "$ME" ;; "source") - echo "OK" - exit + # this should never happen + echo "OK" ;; *) echo -e "${RED}${ME}: BAD REQUEST${NC}" echo -e "${RED}Available arguments: outproc, count, broadcast, start, suspendback, resumeback, kill, killback, help, attach${NC}" exit 4 ;; -esac + esac -# warn if root -if [[ "$(id -u)" -eq "0" ]] ; then + # warn if root + if [[ "$(id -u)" -eq "0" ]] ; then echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}" -fi - + fi +fi # end source diff --git a/commands.sh b/commands.sh index 6d9f1a8..0802547 100755 --- a/commands.sh +++ b/commands.sh @@ -4,7 +4,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 @@ -20,15 +20,16 @@ unset IFS # set -f # if you are paranoid use set -f to disable globbing -# change Info anf Help to fit your needs -bashbot_info() { +if [ "$1" != "source" ]; then + # change Info anf Help to fit your needs + bashbot_info() { send_markdown_message "${1}" 'This is bashbot, the Telegram bot written entirely in bash. It features background tasks and interactive chats, and can serve as an interface for CLI programs. It currently can send, recieve and forward messages, custom keyboards, photos, audio, voice, documents, locations and video files. ' -} + } -bashbot_help() { + bashbot_help() { send_markdown_message "${1}" '*Available commands*: *• /start*: _Start bot and get this message_. *• /info*: _Get shorter info message about this bot_. @@ -39,23 +40,21 @@ bashbot_help() { Written by Drew (@topkecleon), Daniil Gentili (@danogentili) and KayM(@gnadelwartz). Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ' -} + } # some handy shortcuts, e.g.: -_is_botadmin() { + _is_botadmin() { user_is_botadmin "${USER[ID]}" -} -_is_admin() { + } + _is_admin() { user_is_admin "${CHAT[ID]}" "${USER[ID]}" -} -_is_allowed() { # $1 = resource + } + _is_allowed() { # $1 = resource user_is_allowed "${USER[ID]}" "$1" "${CHAT[ID]}" -} - + } +fi if [ "$1" = "source" ];then - # Place the token in the token file - TOKEN="$(cat token)" # Set INLINE to 1 in order to receive inline queries. # To enable this option in your bot, send the /setinline command to @BotFather. INLINE="0" diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index f5b5621..c843364 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index 93587d4..4fc338f 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index c83f945..2e3b73d 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index d6366c1..5198560 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,3 +1,3 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 bashbot.rc diff --git a/dev/version.sh b/dev/version.sh index 0aaeffb..b447894 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index be910c1..d7efa22 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/2_usage.md b/doc/2_usage.md index 678049e..7ba298c 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 89dbff0..e43b0e3 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/4_expert.md b/doc/4_expert.md index d078646..b917540 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/5_practice.md b/doc/5_practice.md index 04a3552..065c894 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/6_reference.md b/doc/6_reference.md index 5f7f10b..11b7883 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -348,5 +348,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) #### [Next Developer Rules](7_develop.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/doc/7_develop.md b/doc/7_develop.md index f20af33..4e4f79f 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -63,5 +63,5 @@ fi #### [Prev Function Reference](6_function.md) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 diff --git a/examples/bashbot.cron b/examples/bashbot.cron index 9a7b4be..1f47f3a 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index a4ad950..38e2761 100755 --- a/examples/calc.sh +++ b/examples/calc.sh @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index e07cd4f..cdf6210 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -2,7 +2,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question,sh b/examples/question,sh index ee55964..29aac83 100755 --- a/examples/question,sh +++ b/examples/question,sh @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index ee55964..29aac83 100755 --- a/examples/question.sh +++ b/examples/question.sh @@ -3,7 +3,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) -#### $$VERSION$$ 0.70-dev-11-g41b8e69 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh index 893f2c0..6122c72 100755 --- a/test/ALL-tests.sh +++ b/test/ALL-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ 0.70-dev-15-g074a103 +#### $$VERSION$$ 0.70-dev-16-g2eac362 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -12,6 +12,8 @@ cd "${GIT_DIR}/.." || exit 1 TESTENV="/tmp/bashbot.test$$" cp -r . "${TESTENV}" +cd "${TESTENV}" || exit 1 + #set -e fail=0 tests=0 diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 52cb6a1..00b9713 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,10 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-15-g074a103 - -# magic to ensure that we're always inside the root of our application, -# no matter from which directory we'll run script -GIT_DIR=$(git rev-parse --git-dir) -cd "$GIT_DIR/.." || exit 1 +#### $$VERSION$$ 0.70-dev-16-g2eac362 dev/hooks/pre-commit.sh diff --git a/test/a-push-test.sh b/test/a-push-test.sh index aeb3b6a..9ef525d 100755 --- a/test/a-push-test.sh +++ b/test/a-push-test.sh @@ -1,9 +1,4 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-15-g074a103 - -# magic to ensure that we're always inside the root of our application, -# no matter from which directory we'll run script -GIT_DIR=$(git rev-parse --git-dir) -cd "$GIT_DIR/.." || exit 1 +#### $$VERSION$$ 0.70-dev-16-g2eac362 dev/hooks/pre-push.sh diff --git a/test/b-init-test.sh b/test/c-init-test.sh similarity index 78% rename from test/b-init-test.sh rename to test/c-init-test.sh index a1192b4..b539622 100755 --- a/test/b-init-test.sh +++ b/test/c-init-test.sh @@ -1,10 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ 0.70-dev-15-g074a103 - -# magic to ensure that we're always inside the root of our application, -# no matter from which directory we'll run script -GIT_DIR=$(git rev-parse --git-dir) -cd "$GIT_DIR/.." || exit 1 +#### $$VERSION$$ 0.70-dev-16-g2eac362 TOKENFILE="./token" TESTTOKEN="bashbottestscript" @@ -47,3 +42,10 @@ else echo "Token not correct or not written!" exit 1 fi + +echo "Test Sourcing of bashbot.sh ..." +trap exit 1 EXIT + +# shellcheck source=./bashbot.sh +source "$1/bashbot.sh" source +trap '' EXIT