follow my own rule: quote whenver possible

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-03-29 17:52:00 +01:00
parent 0a90edbe52
commit 6f1c32817a
6 changed files with 70 additions and 54 deletions

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.49-1-g851be83 #### $$VERSION$$ v0.49-2-g0a90edb
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.49-1-g851be83 #### $$VERSION$$ v0.49-2-g0a90edb
# #
### BEGIN INIT INFO ### BEGIN INIT INFO
# Provides: bashbot # Provides: bashbot
@ -33,18 +33,18 @@ case "$1" in
$runcmd "$start start" # >/dev/null 2>&1 </dev/null $runcmd "$start start" # >/dev/null 2>&1 </dev/null
RETVAL=$? RETVAL=$?
if [ "$RETVAL" = "0" ]; then if [ "$RETVAL" = "0" ]; then
touch $lockfile >/dev/null 2>&1 touch "$lockfile" >/dev/null 2>&1
fi fi
;; ;;
'stop') 'stop')
$runcmd "$start kill" $runcmd "$start kill"
RETVAL=$? RETVAL=$?
if [ "$RETVAL" = "0" ]; then if [ "$RETVAL" = "0" ]; then
rm -f $lockfile rm -f "$lockfile"
fi fi
;; ;;
'status') 'status')
ps -f -u $runas | grep "$name" | grep -qF "bashbot.sh startbot" ps -f -u "$runas" | grep "$name" | grep -qF "bashbot.sh startbot"
if [ "$?" = "0" ]; then if [ "$?" = "0" ]; then
echo "$name is running" echo "$name is running"
RETVAL=0 RETVAL=0

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.49-1-g851be83 #### $$VERSION$$ v0.49-2-g0a90edb
# get location of bashbot.sh an change to bashbot dir # get location of bashbot.sh an change to bashbot dir
SCRIPT="./$(basename $0)" SCRIPT="./$(basename $0)"
@ -18,8 +18,8 @@ SCRIPTDIR="$(dirname $0)"
cd "${SCRIPTDIR}" cd "${SCRIPTDIR}"
# are we runnig in a terminal? # are we runnig in a terminal?
if [ -t 1 ] ; then if [ -t 1 ] || [ "$TERM" != "" ]; then
CLEAR="clear" CLEAR='clear'
RED='\e[31m' RED='\e[31m'
GREEN='\e[32m' GREEN='\e[32m'
ORANGE='\e[35m' ORANGE='\e[35m'
@ -43,7 +43,7 @@ if [ ! -f "token" ]; then
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${NC}"
read token read token
echo "$token" >> token echo "$token" >> "token"
fi fi
TMPDIR="./tmp-bot-bash" TMPDIR="./tmp-bot-bash"
@ -52,7 +52,7 @@ if [ ! -d "$TMPDIR" ]; then
elif [ ! -w "$TMPDIR" ]; then elif [ ! -w "$TMPDIR" ]; then
$CLEAR $CLEAR
echo -e "${RED}ERROR: Can't write to $TMPDIR!.${NC}" echo -e "${RED}ERROR: Can't write to $TMPDIR!.${NC}"
ls -ld $TMPDIR ls -ld "$TMPDIR"
exit 1 exit 1
fi fi
@ -62,12 +62,12 @@ if [ ! -f "$COUNT" ]; then
elif [ ! -w "$COUNT" ]; then elif [ ! -w "$COUNT" ]; then
$CLEAR $CLEAR
echo -e "${RED}ERROR: can't write to $COUNT!.${NC}" echo -e "${RED}ERROR: can't write to $COUNT!.${NC}"
ls -l $COUNT ls -l "$COUNT"
exit 1 exit 1
fi fi
source commands.sh source source "commands.sh" "source"
URL='https://api.telegram.org/bot'$TOKEN URL='https://api.telegram.org/bot'$TOKEN
@ -167,46 +167,46 @@ send_text() {
send_normal_message() { send_normal_message() {
text="$2" text="$2"
until [ $(echo -n "$text" | wc -m) -eq 0 ]; do until [ "$(echo -n "$text" | wc -m)" -eq "0" ]; do
res=$(curl -s "$MSG_URL" -d "chat_id=$1" --data-urlencode "text=${text:0:4096}") res="$(curl -s "$MSG_URL" -d "chat_id=$1" --data-urlencode "text=${text:0:4096}")"
text="${text:4096}" text="${text:4096}"
done done
} }
send_markdown_message() { send_markdown_message() {
text="$2" text="$2"
until [ $(echo -n "$text" | wc -m) -eq 0 ]; do until [ "$(echo -n "$text" | wc -m)" -eq "0" ]; do
res=$(curl -s "$MSG_URL" -d "chat_id=$1" --data-urlencode "text=${text:0:4096}" -d "parse_mode=markdown" -d "disable_web_page_preview=true") res="$(curl -s "$MSG_URL" -d "chat_id=$1" --data-urlencode "text=${text:0:4096}" -d "parse_mode=markdown" -d "disable_web_page_preview=true")"
text="${text:4096}" text="${text:4096}"
done done
} }
send_html_message() { send_html_message() {
text="$2" text="$2"
until [ $(echo -n "$text" | wc -m) -eq 0 ]; do until [ "$(echo -n "$text" | wc -m)" -eq "0" ]; do
res=$(curl -s "$MSG_URL" -F "chat_id=$1" --data-urlencode "text=${text:0:4096}" -F "parse_mode=html") res="$(curl -s "$MSG_URL" -F "chat_id=$1" --data-urlencode "text=${text:0:4096}" -F "parse_mode=html")"
text="${text:4096}" text="${text:4096}"
done done
} }
delete_message() { delete_message() {
res=$(curl -s "$DELETE_URL" -F "chat_id=$1" -F "message_id=$2") res="$(curl -s "$DELETE_URL" -F "chat_id=$1" -F "message_id=$2")"
} }
kick_chat_member() { kick_chat_member() {
res=$(curl -s "$KICK_URL" -F "chat_id=$1" -F "user_id=$2") res="$(curl -s "$KICK_URL" -F "chat_id=$1" -F "user_id=$2")"
} }
unban_chat_member() { unban_chat_member() {
res=$(curl -s "$UNBAN_URL" -F "chat_id=$1" -F "user_id=$2") res="$(curl -s "$UNBAN_URL" -F "chat_id=$1" -F "user_id=$2")"
} }
leave_chat() { leave_chat() {
res=$(curl -s "$LEAVE_URL" -F "chat_id=$1") res="$(curl -s "$LEAVE_URL" -F "chat_id=$1")"
} }
answer_inline_query() { answer_inline_query() {
case $2 in case "$2" in
"article") "article")
InlineQueryResult='[{"type":"'$2'","id":"'$RANDOM'","title":"'$3'","message_text":"'$4'"}]' InlineQueryResult='[{"type":"'$2'","id":"'$RANDOM'","title":"'$3'","message_text":"'$4'"}]'
;; ;;
@ -270,7 +270,7 @@ answer_inline_query() {
esac esac
res=$(curl -s "$INLINE_QUERY" -F "inline_query_id=$1" -F "results=$InlineQueryResult") res="$(curl -s "$INLINE_QUERY" -F "inline_query_id=$1" -F "results=$InlineQueryResult")"
} }
@ -284,14 +284,14 @@ send_keyboard() {
for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done
IFS=$OLDIFS IFS=$OLDIFS
local keyboard=${keyboard/init, /} local keyboard=${keyboard/init, /}
res=$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}") res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")"
} }
remove_keyboard() { remove_keyboard() {
local chat="$1" local chat="$1"
local text="$2" local text="$2"
shift 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() {
@ -342,31 +342,31 @@ send_file() {
;; ;;
esac esac
send_action $chat_id $STATUS send_action $chat_id $STATUS
res=$(curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$CAPTION") res="$(curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$CAPTION")"
} }
# 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" $add) res="$(curl -s "$VENUE_URL" -F "chat_id=$1" -F "latitude=$2" -F "longitude=$3" -F "title=$4" -F "address=$5" $add)"
} }
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")"
} }
@ -456,17 +456,17 @@ process_client() {
fi fi
# Audio # Audio
URLS[AUDIO]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","audio","file_id"\]' | cut -f 2 | cut -d '"' -f 2))" URLS[AUDIO]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","audio","file_id"\]' | cut -f 2 | cut -d '"' -f 2)")"
# Document # Document
URLS[DOCUMENT]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","document","file_id"\]' | cut -f 2 | cut -d '"' -f 2))" URLS[DOCUMENT]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","document","file_id"\]' | cut -f 2 | cut -d '"' -f 2)")"
# Photo # Photo
URLS[PHOTO]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"\]' | cut -f 2 | cut -d '"' -f 2 | sed -n '$p'))" URLS[PHOTO]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"\]' | cut -f 2 | cut -d '"' -f 2 | sed -n '$p')")"
# Sticker # Sticker
URLS[STICKER]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","sticker","file_id"\]' | cut -f 2 | cut -d '"' -f 2))" URLS[STICKER]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","sticker","file_id"\]' | cut -f 2 | cut -d '"' -f 2)")"
# Video # Video
URLS[VIDEO]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","video","file_id"\]' | cut -f 2 | cut -d '"' -f 2))" URLS[VIDEO]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","video","file_id"\]' | cut -f 2 | cut -d '"' -f 2)")"
# Voice # Voice
URLS[VOICE]="$(get_file $(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","voice","file_id"\]' | cut -f 2 | cut -d '"' -f 2))" URLS[VOICE]="$(get_file "$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","voice","file_id"\]' | cut -f 2 | cut -d '"' -f 2)")"
# Contact # Contact
CONTACT[NUMBER]="$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","contact","phone_number"\]' | cut -f 2 | cut -d '"' -f 2)" CONTACT[NUMBER]="$(echo "$UPDATE" | egrep '\["result",'$PROCESS_NUMBER',"message","contact","phone_number"\]' | cut -f 2 | cut -d '"' -f 2)"
@ -526,9 +526,9 @@ case "$1" in
;; ;;
"broadcast") "broadcast")
echo "Sending the broadcast $* to $(wc -l ${COUNT} | sed 's/count//g')users." echo "Sending the broadcast $* to $(wc -l ${COUNT} | sed 's/count//g')users."
[ $(wc -l ${COUNT} | sed 's/ count//g') -gt 300 ] && sleep="sleep 0.5" [ "$(wc -l ${COUNT} | sed 's/ count//g')" -gt "300" ] && sleep="sleep 0.5"
shift shift
for f in $(cat ${COUNT});do send_message ${f//COUNT} "$*"; $sleep;done for f in "$(cat ${COUNT})";do send_message ${f//COUNT} "$*"; $sleep;done
;; ;;
"start") "start")
$CLEAR $CLEAR
@ -539,7 +539,7 @@ case "$1" in
;; ;;
"init") # adjust users and permissions "init") # adjust users and permissions
MYUSER="$USER" MYUSER="$USER"
[[ $(id -u) -eq 0 ]] && MYUSER="nobody" [[ "$(id -u)" -eq "0" ]] && MYUSER="nobody"
echo -n "Enter User to run basbot [$MYUSER]: " echo -n "Enter User to run basbot [$MYUSER]: "
read TOUSER read TOUSER
[ "$TOUSER" = "" ] && TOUSER="$MYUSER" [ "$TOUSER" = "" ] && TOUSER="$MYUSER"
@ -573,7 +573,7 @@ case "$1" in
JOB="${JOB%:*}" JOB="${JOB%:*}"
fifo="back-${JOB}-${ME}_${CHAT[ID]}" # compose fifo from jobname, $ME (botname) and CHAT[ID] fifo="back-${JOB}-${ME}_${CHAT[ID]}" # compose fifo from jobname, $ME (botname) and CHAT[ID]
echo "restartbackground ${PROG} ${fifo}" echo "restartbackground ${PROG} ${fifo}"
( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo}) 2>/dev/null ( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r "$TMPDIR/${fifo})" 2>/dev/null
mkfifo "$TMPDIR/${fifo}" mkfifo "$TMPDIR/${fifo}"
TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>$TMPDIR/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}" TMUX= tmux new-session -d -s "${fifo}" "${PROG} &>$TMPDIR/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>$TMPDIR/${fifo}"
TMUX= tmux new-session -d -s sendprocess_${fifo} "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" TMUX= tmux new-session -d -s sendprocess_${fifo} "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}"
@ -582,14 +582,14 @@ case "$1" in
;; ;;
"kill") "kill")
$CLEAR $CLEAR
tmux kill-session -t $ME &>/dev/null tmux kill-session -t "$ME" &>/dev/null
send_markdown_message "${CHAT[ID]}" "*Bot stopped*" send_markdown_message "${CHAT[ID]}" "*Bot stopped*"
echo -e "${GREEN}OK. Bot stopped successfully.${NC}" echo -e "${GREEN}OK. Bot stopped successfully.${NC}"
;; ;;
"killback" | "suspendback") "killback" | "suspendback")
$CLEAR $CLEAR
echo -e "${GREEN}Stopping background processes ...${NC}" echo -e "${GREEN}Stopping background processes ...${NC}"
for FILE in ${TMPDIR}/*-back.cmd; do for FILE in $"{TMPDIR}/*-back.cmd"; do
if [ "$FILE" == "${TMPDIR}/*-back.cmd" ]; then if [ "$FILE" == "${TMPDIR}/*-back.cmd" ]; then
echo -e "${RED}No background processes.${NC}"; break echo -e "${RED}No background processes.${NC}"; break
else else
@ -597,17 +597,17 @@ case "$1" in
JOB="${REMOVE#*:}" JOB="${REMOVE#*:}"
fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}" fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}"
echo "killbackground ${fifo}" echo "killbackground ${fifo}"
[ "$1" == "killback" ] && rm -f $FILE # remove job [ "$1" == "killback" ] && rm -f "$FILE" # remove job
( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo}) 2>/dev/null ( tmux kill-session -t "${fifo}"; tmux kill-session -t sendprocess_${fifo}; rm -f -r $TMPDIR/${fifo}) 2>/dev/null
fi fi
done done
;; ;;
"help") "help")
$CLEAR $CLEAR
less README.md less "README.md"
;; ;;
"attach") "attach")
tmux attach -t $ME tmux attach -t "$ME"
;; ;;
"source") "source")
echo "OK" echo "OK"
@ -619,7 +619,7 @@ case "$1" in
esac esac
# warn if root # warn if root
if [[ $(id -u) -eq 0 ]] ; then if [[ "$(id -u)" -eq "0" ]] ; then
echo -e "\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" 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}" echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}"
fi fi

11
notify
View File

@ -2,13 +2,16 @@
# 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.49-0-g64255eb #### $$VERSION$$ v0.49-2-g0a90edb
# 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
export LC_ALL=C.UTF-8 export'LC_ALL=C.UTF-8'
export LANG=C.UTF-8 export 'LANG=C.UTF-8'
export LANGUAGE=C.UTF-8 export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
# discard STDIN for background jobs! # discard STDIN for background jobs!
cat >/dev/null & cat >/dev/null &

View File

@ -2,7 +2,17 @@
# 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.49-0-g64255eb
#### $$VERSION$$ v0.49-2-g0a90edb
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8'
export 'LANG=C.UTF-8'
export 'LANGUAGE=C.UTF-8'
unset IFS
# set -f # if you are paranoid use set -f to disable globbing
echo "Why hello there. echo "Why hello there.
Would you like some tea (y/n)?" Would you like some tea (y/n)?"

View File

@ -1,5 +1,7 @@
#!/bin/sh #!/bin/sh
# #
#### $$VERSION$$ v0.49-2-g0a90edb
#
# Easy Versioning in git: # Easy Versioning in git:
# #
# for setting your Version in git use e.g.: # for setting your Version in git use e.g.:
@ -30,8 +32,9 @@
# #
# run this script to (re)place Version number in files # run this script to (re)place Version number in files
# #
#### $$VERSION$$ v0.49-0-g64255eb
# unset IFS
# set -f # if you are paranoid use set -f to disable globbing
VERSION="`git describe --tags --long`" VERSION="`git describe --tags --long`"
echo "Update files to version $VERSION ..." echo "Update files to version $VERSION ..."