start v0.6-dev3 - optimize quoting and grep use

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-12 17:18:20 +02:00
parent ae157c4042
commit fe9443fe4d
14 changed files with 74 additions and 68 deletions

View File

@ -75,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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

View File

@ -119,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.52-0-gdb7b19f latexmath:[\[VERSION\]] v0.6-dev3-0-gae157c4
++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2181 # shellcheck disable=SC2181

View File

@ -10,13 +10,13 @@
# 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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-1-gae157c4
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
# - 1 can't change to dir # - 1 can't change to dir
# - 2 can't write to tmp, count or token # - 2 can't write to tmp, count or token
# - 3 user not found # - 3 user / command not found
# - 4 unkown command # - 4 unkown command
# are we runnig in a terminal? # are we runnig in a terminal?
@ -43,12 +43,6 @@ if [ ! -w "." ]; then
ls -ld . ls -ld .
fi fi
if [ ! -f "JSON.sh/JSON.sh" ]; then
echo "You did not clone recursively! Downloading JSON.sh..."
git clone http://github.com/dominictarr/JSON.sh
echo "JSON.sh has been downloaded. Proceeding."
fi
TOKEN="./token" TOKEN="./token"
if [ ! -f "${TOKEN}" ]; then if [ ! -f "${TOKEN}" ]; then
if [ "${CLEAR}" == "" ]; then if [ "${CLEAR}" == "" ]; then
@ -57,12 +51,18 @@ if [ ! -f "${TOKEN}" ]; then
else else
${CLEAR} ${CLEAR}
echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${RED}TOKEN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
read -r token read -r token
echo "${token}" > "${TOKEN}" echo "${token}" > "${TOKEN}"
fi fi
fi fi
if [ ! -f "JSON.sh/JSON.sh" ]; then
echo "You did not clone recursively! Downloading JSON.sh..."
git clone http://github.com/dominictarr/JSON.sh
echo "JSON.sh has been downloaded. Proceeding."
fi
BOTADMIN="./botadmin" BOTADMIN="./botadmin"
if [ ! -f "${BOTADMIN}" ]; then if [ ! -f "${BOTADMIN}" ]; then
if [ "${CLEAR}" == "" ]; then if [ "${CLEAR}" == "" ]; then
@ -104,42 +104,47 @@ elif [ ! -w "${COUNT}" ]; then
exit 2 exit 2
fi 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}"
fi
source "commands.sh" "source" # shellcheck source=./commands.sh
URL='https://api.telegram.org/bot'$TOKEN source "${COMMANDS}" "source"
URL='https://api.telegram.org/bot'"$TOKEN"
MSG_URL=$URL'/sendMessage' MSG_URL="$URL"'/sendMessage'
LEAVE_URL=$URL'/leaveChat' LEAVE_URL="$URL"'/leaveChat'
KICK_URL=$URL'/kickChatMember' KICK_URL="$URL"'/kickChatMember'
UNBAN_URL=$URL'/unbanChatMember' UNBAN_URL="$URL"'/unbanChatMember'
PHO_URL=$URL'/sendPhoto' PHO_URL="$URL"'/sendPhoto'
AUDIO_URL=$URL'/sendAudio' AUDIO_URL="$URL"'/sendAudio'
DOCUMENT_URL=$URL'/sendDocument' DOCUMENT_URL="$URL"'/sendDocument'
STICKER_URL=$URL'/sendSticker' STICKER_URL="$URL"'/sendSticker'
VIDEO_URL=$URL'/sendVideo' VIDEO_URL="$URL"'/sendVideo'
VOICE_URL=$URL'/sendVoice' VOICE_URL="$URL"'/sendVoice'
LOCATION_URL=$URL'/sendLocation' LOCATION_URL="$URL"'/sendLocation'
VENUE_URL=$URL'/sendVenue' VENUE_URL="$URL"'/sendVenue'
ACTION_URL=$URL'/sendChatAction' ACTION_URL="$URL"'/sendChatAction'
FORWARD_URL=$URL'/forwardMessage' FORWARD_URL="$URL"'/forwardMessage'
INLINE_QUERY=$URL'/answerInlineQuery' INLINE_QUERY="$URL"'/answerInlineQuery'
ME_URL=$URL'/getMe' DELETE_URL="$URL"'/deleteMessage'
DELETE_URL=$URL'/deleteMessage' GETMEMBER_URL="$URL"'/getChatMember'
GETMEMBER_URL=$URL'/getChatMember' ME_URL="$URL"'/getMe'
ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)" ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","username"\] s/.*\][ \t]"\(.*\)"$/\1/p')"
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' FILE_URL='https://api.telegram.org/file/bot'"$TOKEN"'/'
UPD_URL=$URL'/getUpdates?offset=' UPD_URL="$URL"'/getUpdates?offset='
GET_URL=$URL'/getFile' GET_URL="$URL"'/getFile'
OFFSET=0 OFFSET=0
declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO
urlencode() {
echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g'
}
send_message() { send_message() {
local text arg keyboard file lat long title address sent local text arg keyboard file lat long title address sent
@ -148,7 +153,6 @@ send_message() {
text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')" text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')"
arg="$3" arg="$3"
[ "$arg" != "safe" ] && { [ "$arg" != "safe" ] && {
#text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts
text="${text// mynewlinestartshere /$'\r\n'}" text="${text// mynewlinestartshere /$'\r\n'}"
no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')"
@ -194,10 +198,10 @@ send_message() {
send_text() { send_text() {
case "$2" in case "$2" in
html_parse_mode*) 'html_parse_mode'*)
send_html_message "$1" "${2//html_parse_mode}" send_html_message "$1" "${2//html_parse_mode}"
;; ;;
markdown_parse_mode*) 'markdown_parse_mode'*)
send_markdown_message "$1" "${2//markdown_parse_mode}" send_markdown_message "$1" "${2//markdown_parse_mode}"
;; ;;
*) *)
@ -271,7 +275,7 @@ user_is_botadmin() {
} }
user_is_allowed() { user_is_allowed() {
local acl; acl="$1" local acl="$1"
[ "$1" == "" ] && return 1 [ "$1" == "" ] && return 1
grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0 grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0
[ "$2" != "" ] && acl="${acl}:$2" [ "$2" != "" ] && acl="${acl}:$2"
@ -352,7 +356,6 @@ answer_inline_query() {
send_keyboard() { send_keyboard() {
local chat="$1" local chat="$1"
local text="$2" local text="$2"
shift 2
local keyboard=init local keyboard=init
OLDIFS=$IFS OLDIFS=$IFS
IFS=$(echo -en "\"") IFS=$(echo -en "\"")
@ -365,12 +368,11 @@ send_keyboard() {
remove_keyboard() { remove_keyboard() {
local chat="$1" local chat="$1"
local text="$2" local text="$2"
shift 2
res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"remove_keyboard\": true}")" res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"remove_keyboard\": true}")"
} }
get_file() { get_file() {
[ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)" [ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | sed -n -e '\["result","file_path"\] s/.*\][ \t]"\(.*\)"$/\1/p')"
} }
send_file() { send_file() {
@ -424,24 +426,24 @@ send_file() {
# typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location # typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location
send_action() { send_action() {
[ "$2" = "" ] && return [ "$2" == "" ] && return
res="$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")" res="$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")"
} }
send_location() { send_location() {
[ "$3" = "" ] && return [ "$3" == "" ] && return
res="$(curl -s "$LOCATION_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3")" res="$(curl -s "$LOCATION_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3")"
} }
send_venue() { send_venue() {
[ "$5" = "" ] && return [ "$5" == "" ] && return
[ "$6" != "" ] add="-F \"foursquare_id=$6\"" [ "$6" != "" ] add="-F \"foursquare_id=$6\""
res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5")" res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5")"
} }
forward() { forward() {
[ "$3" = "" ] && return [ "$3" == "" ] && return
res="$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")" res="$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")"
} }
@ -465,7 +467,7 @@ checkback() {
} }
checkproc() { checkproc() {
tmux ls | grep -q "$1${copname}"; res=$? tmux ls | grep -qF "$1${copname}"; res=$?
} }
killback() { killback() {
@ -562,12 +564,13 @@ process_client() {
rm "$TMP" rm "$TMP"
# Tmux # Tmux
copname="$ME"_"${CHAT[ID]}" copname="$ME"'_'"${CHAT[ID]}"
source commands.sh # shellcheck source=./commands.sh
source "${COMMANDS}"
tmpcount="COUNT${CHAT[ID]}" tmpcount="COUNT${CHAT[ID]}"
grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT} grep -qF "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT}
# To get user count execute bash bashbot.sh count # To get user count execute bash bashbot.sh count
} }
@ -577,7 +580,7 @@ while [ "$1" == "startbot" ]; do {
UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)" UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)"
# Offset # Offset
OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)" OFFSET="$(echo "$UPDATE" | sed -n -e '\["result",[0-9]*,"update_id"\] s/.*\][ \t]//p')"
OFFSET=$((OFFSET+1)) OFFSET=$((OFFSET+1))
if [ "$OFFSET" != "1" ]; then if [ "$OFFSET" != "1" ]; then

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.52-1-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
# #
# shellcheck disable=SC2154 # shellcheck disable=SC2154
# shellcheck disable=SC2034 # shellcheck disable=SC2034

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

View File

@ -153,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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4

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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
# 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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
# 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.52-0-gdb7b19f #### $$VERSION$$ v0.6-dev3-0-gae157c4
# shellcheck disable=SC2016 # shellcheck disable=SC2016
# #
# Easy Versioning in git: # Easy Versioning in git:
@ -40,7 +40,10 @@ unset IFS
VERSION="$(git describe --tags --long)" VERSION="$(git describe --tags --long)"
echo "Update files to version $VERSION ..." echo "Update files to version $VERSION ..."
for file in * doc/* FILES="* doc/*"
[ "$1" != "" ] && FILES="$*"
for file in $FILES
do do
[ ! -f "$file" ] && continue [ ! -f "$file" ] && continue
#[ "$file" == "version" ] && continue #[ "$file" == "version" ] && continue