better handling of sourcing files

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-20 16:26:16 +02:00
parent 2eac362dc7
commit b1aef7d6f9
26 changed files with 86 additions and 87 deletions

View File

@ -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

View File

@ -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
++++++++++++++++++++++++++++++++++++++++++++

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -63,5 +63,5 @@ fi
#### [Prev Function Reference](6_function.md)
#### $$VERSION$$ 0.70-dev-11-g41b8e69
#### $$VERSION$$ 0.70-dev-16-g2eac362

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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