Bashbot Version 0.52 - user acls added

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-12 13:14:33 +02:00
parent db7b19fbc4
commit 1144a1ff69
14 changed files with 106 additions and 21 deletions

View File

@ -8,7 +8,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.
[Download from github](https://github.com/topkecleon/telegram-bot-bash/releases) [Download latest release from github](https://github.com/topkecleon/telegram-bot-bash/releases)
Released to the public domain wherever applicable. Released to the public domain wherever applicable.
Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/).
@ -25,7 +25,8 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
``` ```
3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather.
## Update bashbot
[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) and copy all files to bashbot dir. run ```sudo ./bashbot.sh init```.
## Getting started ## Getting started
- [Create your first telegram bot](doc/1_firstbot.md) - [Create your first telegram bot](doc/1_firstbot.md)
@ -35,6 +36,7 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
- Send Messages - Send Messages
- Send files, location etc. - Send files, location etc.
- [Advanced Features](doc/3_advanced.md) - [Advanced Features](doc/3_advanced.md)
- Access Control
- Interactive Chats - Interactive Chats
- Background Jobs - Background Jobs
- Inline queries - Inline queries
@ -73,4 +75,4 @@ No - its not less (in)secure as any other Bot written in any other language. But
If you feel that there's something missing or if you found a bug, feel free to submit a pull request! If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
#### $$VERSION$$ v0.6-dev2-8-g649fe44 #### $$VERSION$$ v0.52-0-gdb7b19f

View File

@ -11,8 +11,8 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M
Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1. Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.
https://github.com/topkecleon/telegram-bot-bash/releases[Download from https://github.com/topkecleon/telegram-bot-bash/releases[Download latest
github] release from github]
Released to the public domain wherever applicable. Elsewhere, consider Released to the public domain wherever applicable. Elsewhere, consider
it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2]. it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2].
@ -34,6 +34,13 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
follow the instructions. At this stage you are asked for your Bots token follow the instructions. At this stage you are asked for your Bots token
given by botfather. given by botfather.
Update bashbot
~~~~~~~~~~~~~~
https://github.com/topkecleon/telegram-bot-bash/releases[Download latest
update zip from github] and copy all files to bashbot dir. run
`sudo ./bashbot.sh init`.
Getting started Getting started
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
@ -44,6 +51,7 @@ Getting started
* Send Messages * Send Messages
* Send files, location etc. * Send files, location etc.
* link:doc/3_advanced.md[Advanced Features] * link:doc/3_advanced.md[Advanced Features]
* Access Control
* Interactive Chats * Interactive Chats
* Background Jobs * Background Jobs
* Inline queries * Inline queries
@ -111,5 +119,5 @@ That's it!
If you feel that there's something missing or if you found a bug, feel If you feel that there's something missing or if you found a bug, feel
free to submit a pull request! free to submit a pull request!
latexmath:[\[VERSION\]] v0.6-dev2-8-g649fe44 latexmath:[\[VERSION\]] v0.52-0-gdb7b19f
++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++

View File

@ -7,7 +7,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$$ v0.6-dev2-8-g649fe44 #### $$VERSION$$ v0.52-0-gdb7b19f
SHELL=/bin/sh SHELL=/bin/sh

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# description: Start or stop telegram-bash-bot # description: Start or stop telegram-bash-bot
# #
#### $$VERSION$$ v0.6-dev2-13-g88cf2d9 #### $$VERSION$$ v0.52-0-gdb7b19f
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2181 # shellcheck disable=SC2181

View File

@ -10,7 +10,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$$ v0.6-dev2-14-g56cb1bb #### $$VERSION$$ v0.52-0-gdb7b19f
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -78,6 +78,12 @@ if [ ! -f "${BOTADMIN}" ]; then
fi fi
fi fi
BOTACL="./botacl"
if [ ! -f "${BOTACL}" ]; then
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
touch "${BOTACL}"
fi
TMPDIR="./tmp-bot-bash" TMPDIR="./tmp-bot-bash"
if [ ! -d "${TMPDIR}" ]; then if [ ! -d "${TMPDIR}" ]; then
mkdir "${TMPDIR}" mkdir "${TMPDIR}"
@ -264,6 +270,16 @@ user_is_botadmin() {
return 1 return 1
} }
user_is_allowed() {
local acl; acl="$1"
[ "$1" == "" ] && return 1
grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0
[ "$2" != "" ] && acl="${acl}:$2"
grep -F -xq "${acl}:*" <"${BOTACL}" && return 0
[ "$3" != "" ] && acl="${acl}:$3"
grep -F -xq "${acl}" <"${BOTACL}"
}
answer_inline_query() { answer_inline_query() {
case "$2" in case "$2" in
"article") "article")
@ -618,7 +634,7 @@ case "$1" in
chmod 711 . chmod 711 .
chmod -R a-w ./* chmod -R a-w ./*
chmod -R u+w "${COUNT}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null chmod -R u+w "${COUNT}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null
chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKEN}" "${BOTADMIN}" 2>/dev/null chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKEN}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
ls -la ls -la
exit exit
fi fi

View File

@ -4,7 +4,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$$ v0.6-dev2-14-g56cb1bb #### $$VERSION$$ v0.52-0-gdb7b19f
# #
# shellcheck disable=SC2154 # shellcheck disable=SC2154
# shellcheck disable=SC2034 # shellcheck disable=SC2034
@ -119,7 +119,10 @@ else
'/start') '/start')
send_action "${CHAT[ID]}" "typing" send_action "${CHAT[ID]}" "typing"
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*." user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
bashbot_help "${CHAT[ID]}" if user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then
bot_help "${CHAT[ID]}"
else
send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot."
;; ;;
'/leavechat') # bot leave chat if user is admin in chat '/leavechat') # bot leave chat if user is admin in chat

View File

@ -70,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
``` ```
3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather.
#### $$VERSION$$ v0.6-dev2-10-gdb64978 #### $$VERSION$$ v0.52-0-gdb7b19f

View File

@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video
send_action "${CHAT[ID]}" "action" send_action "${CHAT[ID]}" "action"
``` ```
#### $$VERSION$$ v0.6-dev2-10-gdb64978 #### $$VERSION$$ v0.52-0-gdb7b19f

View File

@ -1,5 +1,61 @@
## Advanced Features ## Advanced Features
### Access control
Bashbot offers functions to check if a has Telegram capabilities like chat admin or chat creator:
```bash
# return true if user is admin/owner of the bot
# -> botadmin is stored in file './botadmin'
user_is_botadmin "user"
# return true if user is creator or admin of a chat
user_is_admin "chat" "user"
# return true if user is creator of a chat or it's a one to one chat
user_is_creator "chat" "user"
# examples:
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *CHATADMIN*."
```
In addtion you can configure individual capabilities for users in the file ```./botacl```:
```bash
# botacl
# if a user is not listed here, function 'user_is_allowed' will always return false
#
# Format:
# user:ressource:chat
# allow user 123456789 access to all resources in all chats
123456789:*:*
# allow user 12131415 to start bot in all chats
12131415:start:*
# allow user 987654321 only to start bot in chat 98979695
987654321:start:98979695
# * are only allowed on the right hand side and not for user!
# the following exaples are NOT valid!
*:*:*
*:start:*
*:*:98979695
```
you have to check yourself if a user is allowed to to something by calling function ```user_is_allowed```.
example to check if a user is allowed to start bot:
```bash
case "$MESSAGE" in
'/start')
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
if user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then
bot_help "${CHAT[ID]}"
else
send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot."
;;
esac
```
### Interactive Chats ### Interactive Chats
To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script. To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script.
The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel). The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel).
@ -97,5 +153,5 @@ To send stickers through an *inline query*:
answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
``` ```
#### $$VERSION$$ v0.6-dev2-10-gdb64978 #### $$VERSION$$ v0.52-0-gdb7b19f

View File

@ -102,5 +102,5 @@ An example crontab is provided in ```bashbot.cron```.
- if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` - if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot```
#### $$VERSION$$ v0.6-dev2-10-gdb64978 #### $$VERSION$$ v0.52-0-gdb7b19f

View File

@ -111,5 +111,5 @@ In bashbot.sh line 490:
``` ```
As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-) As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-)
#### $$VERSION$$ v0.6-dev2-10-gdb64978 #### $$VERSION$$ v0.52-0-gdb7b19f

2
notify
View File

@ -2,7 +2,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$$ v0.6-dev2-8-g649fe44 #### $$VERSION$$ v0.52-0-gdb7b19f
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # 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. # 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$$ v0.6-dev2-8-g649fe44 #### $$VERSION$$ v0.52-0-gdb7b19f
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# #
#### $$VERSION$$ v0.6-dev2-9-gf4e277b #### $$VERSION$$ v0.52-0-gdb7b19f
# shellcheck disable=SC2016 # shellcheck disable=SC2016
# #
# Easy Versioning in git: # Easy Versioning in git: