optimize startup and testing for empty string

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-05-14 20:33:30 +02:00
parent c0a633f42d
commit 0e75138548
8 changed files with 133 additions and 121 deletions

View File

@ -11,7 +11,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -23,7 +23,7 @@
# shellcheck disable=SC2140 # shellcheck disable=SC2140
# are we runnig in a terminal? # are we runnig in a terminal?
if [ -t 1 ] && [ "$TERM" != "" ]; then if [ -t 1 ] && [ -n "$TERM" ]; then
CLEAR='clear' CLEAR='clear'
RED='\e[31m' RED='\e[31m'
GREEN='\e[32m' GREEN='\e[32m'
@ -47,17 +47,17 @@ else
MODULEDIR="./$(basename "${MODULEDIR}")" MODULEDIR="./$(basename "${MODULEDIR}")"
fi fi
if [ "$BASHBOT_HOME" != "" ]; then if [ -n "$BASHBOT_HOME" ]; then
SCRIPTDIR="$BASHBOT_HOME" SCRIPTDIR="$BASHBOT_HOME"
[ "${BASHBOT_ETC}" = "" ] && BASHBOT_ETC="$BASHBOT_HOME" [ -z "${BASHBOT_ETC}" ] && BASHBOT_ETC="$BASHBOT_HOME"
[ "${BASHBOT_VAR}" = "" ] && BASHBOT_VAR="$BASHBOT_HOME" [ -z "${BASHBOT_VAR}" ] && BASHBOT_VAR="$BASHBOT_HOME"
fi fi
ADDONDIR="${BASHBOT_ETC:-./addons}" ADDONDIR="${BASHBOT_ETC:-./addons}"
RUNUSER="${USER}" # USER is overwritten by bashbot array RUNUSER="${USER}" # USER is overwritten by bashbot array
if [ "${SOURCE}" != "yes" ] && [ "$BASHBOT_HOME" = "" ] && ! cd "${RUNDIR}" ; then if [ "${SOURCE}" != "yes" ] && [ -z "$BASHBOT_HOME" ] && ! cd "${RUNDIR}" ; then
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}" echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
exit 1 exit 1
else else
@ -69,62 +69,71 @@ if [ ! -w "." ]; then
ls -ld . ls -ld .
fi fi
# if BOTTOKEN is empty read from file #####################
# Setup and check environment if BOTTOKEN is NOT set
TOKENFILE="${BASHBOT_ETC:-.}/token" TOKENFILE="${BASHBOT_ETC:-.}/token"
if [ -z "${BOTTOKEN}" ] && [ ! -f "${TOKENFILE}" ]; then BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then BOTACL="${BASHBOT_ETC:-.}/botacl"
DATADIR="${BASHBOT_VAR:-.}/data-bot-bash"
# !!!!! DEPRECATED !!!!!
COUNTFILE="${BASHBOT_VAR:-.}/count"
# we assume everthing is already set up correctly if we have TOKEN
if [ -n "${BOTTOKEN}" ]; then
# BOTTOKEN empty read from file
if [ ! -f "${TOKENFILE}" ]; then
if [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then
echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!"
exit 2 exit 2
else else
${CLEAR} ${CLEAR}
echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${RED}TOKEN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
read -r BOTTOKEN read -r BOTTOKEN
printf '%s\n' "${BOTTOKEN}" > "${TOKENFILE}" printf '%s\n' "${BOTTOKEN}" > "${TOKENFILE}"
fi fi
fi fi
[ -z "${BOTTOKEN}" ] && BOTTOKEN="$(< "${TOKENFILE}")" [ -z "${BOTTOKEN}" ] && BOTTOKEN="$(< "${TOKENFILE}")"
# setup botadmin file
BOTADMIN="${BASHBOT_ETC:-.}/botadmin" if [ ! -f "${BOTADMIN}" ]; then
if [ ! -f "${BOTADMIN}" ]; then if [ -z "${CLEAR}" ]; then
if [ "${CLEAR}" = "" ]; then
echo "Running headless, set botadmin to AUTO MODE!" echo "Running headless, set botadmin to AUTO MODE!"
printf '%s\n' '?' > "${BOTADMIN}" printf '%s\n' '?' > "${BOTADMIN}"
else else
${CLEAR} ${CLEAR}
echo -e "${RED}BOTADMIN MISSING.${NC}" echo -e "${RED}BOTADMIN MISSING.${NC}"
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}" echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
read -r admin read -r admin
[ "${admin}" = "" ] && admin='?' [ -z "${admin}" ] && admin='?'
printf '%s\n' "${admin}" > "${BOTADMIN}" printf '%s\n' "${admin}" > "${BOTADMIN}"
fi fi
fi fi
# setup botacl file
BOTACL="${BASHBOT_ETC:-.}/botacl" if [ ! -f "${BOTACL}" ]; then
if [ ! -f "${BOTACL}" ]; then
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}" echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
printf '\n' >"${BOTACL}" printf '\n' >"${BOTACL}"
fi fi
# setup data dir file
DATADIR="${BASHBOT_VAR:-.}/data-bot-bash" if [ ! -d "${DATADIR}" ]; then
if [ ! -d "${DATADIR}" ]; then
mkdir "${DATADIR}" mkdir "${DATADIR}"
elif [ ! -w "${DATADIR}" ]; then elif [ ! -w "${DATADIR}" ]; then
echo -e "${RED}ERROR: Can't write to ${DATADIR}!.${NC}" echo -e "${RED}ERROR: Can't write to ${DATADIR}!.${NC}"
ls -ld "${DATADIR}" ls -ld "${DATADIR}"
exit 2 exit 2
fi fi
# setup count file !!!!! DEPRECATED !!!!!
COUNTFILE="${BASHBOT_VAR:-.}/count" if [ ! -f "${COUNTFILE}" ]; then
if [ ! -f "${COUNTFILE}" ]; then
printf '\n' >"${COUNTFILE}" printf '\n' >"${COUNTFILE}"
elif [ ! -w "${COUNTFILE}" ]; then elif [ ! -w "${COUNTFILE}" ]; then
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}"
ls -l "${COUNTFILE}" ls -l "${COUNTFILE}"
exit 2 exit 2
fi
fi fi
##################
# here we start with the real stuff
URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}"
ME_URL=$URL'/getMe' ME_URL=$URL'/getMe'
@ -140,6 +149,8 @@ declare -Ax UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO
export res CAPTION export res CAPTION
#################EW#
# read commamds file if we are not sourced
COMMANDS="${BASHBOT_ETC:-.}/commands.sh" COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
if [ "${SOURCE}" != "yes" ]; then if [ "${SOURCE}" != "yes" ]; then
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
@ -186,13 +197,13 @@ proclist() {
# $1 sting to search for proramm to kill # $1 sting to search for proramm to kill
killallproc() { killallproc() {
local procid; procid="$(proclist "$1")" local procid; procid="$(proclist "$1")"
if [ "${procid}" != "" ] ; then if [ -n "${procid}" ] ; then
# shellcheck disable=SC2046 # shellcheck disable=SC2046
kill $(proclist "$1") kill $(proclist "$1")
sleep 1 sleep 1
procid="$(proclist "$1")" procid="$(proclist "$1")"
# shellcheck disable=SC2046 # shellcheck disable=SC2046
[ "${procid}" != "" ] && kill $(proclist -9 "$1") [ -n "${procid}" ] && kill $(proclist -9 "$1")
fi fi
} }
@ -219,7 +230,7 @@ delete_message() {
} }
get_file() { get_file() {
[ "$1" = "" ] && return [ -z "$1" ] && return
sendJson "" '"file_id": "'"${1}"'"' "${GETFILE_URL}" sendJson "" '"file_id": "'"${1}"'"' "${GETFILE_URL}"
printf '%s\n' "${URL}"/"$(JsonGetString <<< "${res}" '"result","file_path"')" printf '%s\n' "${URL}"/"$(JsonGetString <<< "${res}" '"result","file_path"')"
} }
@ -228,7 +239,7 @@ get_file() {
TIMEOUT="${BASHBOT_TIMEOUT}" TIMEOUT="${BASHBOT_TIMEOUT}"
[[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20" [[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT="20"
if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then
# simple curl or wget call, output to stdout # simple curl or wget call, output to stdout
getJson(){ getJson(){
# shellcheck disable=SC2086 # shellcheck disable=SC2086
@ -237,18 +248,18 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
# usage: sendJson "chat" "JSON" "URL" # usage: sendJson "chat" "JSON" "URL"
sendJson(){ sendJson(){
local chat=""; local chat="";
[ "${1}" != "" ] && chat='"chat_id":'"${1}"',' [ -n "${1}" ] && chat='"chat_id":'"${1}"','
# shellcheck disable=SC2086 # shellcheck disable=SC2086
res="$(curl -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ res="$(curl -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \
-H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )" -H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )"
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
[ "${SOURCE}" != "yes" ] && [ "${BASHBOT_EVENT_SEND[*]}" != "" ] && event_send "send" "$@" & [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" &
} }
#$1 Chat, $2 what , $3 file, $4 URL, $5 caption #$1 Chat, $2 what , $3 file, $4 URL, $5 caption
sendUpload() { sendUpload() {
[ "$#" -lt 4 ] && return [ "$#" -lt 4 ] && return
if [ "$5" != "" ]; then if [ -n "$5" ]; then
# shellcheck disable=SC2086 # shellcheck disable=SC2086
res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )" res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )"
else else
@ -256,7 +267,7 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" res="$(curl -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )"
fi fi
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
[ "${SOURCE}" != "yes" ] && [ "${BASHBOT_EVENT_SEND[*]}" != "" ] && event_send "upload" "$@" & [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" &
} }
else else
# simple curl or wget call outputs result to stdout # simple curl or wget call outputs result to stdout
@ -267,18 +278,18 @@ else
# usage: sendJson "chat" "JSON" "URL" # usage: sendJson "chat" "JSON" "URL"
sendJson(){ sendJson(){
local chat=""; local chat="";
[ "${1}" != "" ] && chat='"chat_id":'"${1}"',' [ -n "${1}" ] && chat='"chat_id":'"${1}"','
# shellcheck disable=SC2086 # shellcheck disable=SC2086
res="$(wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - --post-data='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' \ res="$(wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - --post-data='{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' \
--header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )" --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )"
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
[ "${SOURCE}" != "yes" ] && [ "${BASHBOT_EVENT_SEND[*]}" != "" ] && event_send "send" "$@" & [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "$@" &
} }
sendUpload() { sendUpload() {
sendJson "$1" '"text":"Sorry, wget does not support file upload"' "${MSG_URL}" sendJson "$1" '"text":"Sorry, wget does not support file upload"' "${MSG_URL}"
BOTSENT[OK]="false" BOTSENT[OK]="false"
[ "${SOURCE}" != "yes" ] && [ "${BASHBOT_EVENT_SEND[*]}" != "" ] && event_send "upload" "$@" & [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" &
} }
fi fi
@ -293,11 +304,11 @@ JsonEscape() {
# title caption description markup inlinekeyboard # title caption description markup inlinekeyboard
title2Json(){ title2Json(){
local title caption desc markup keyboard local title caption desc markup keyboard
[ "$1" != "" ] && title=',"title":"'$(JsonEscape "$1")'"' [ -n "$1" ] && title=',"title":"'$(JsonEscape "$1")'"'
[ "$2" != "" ] && caption=',"caption":"'$(JsonEscape "$2")'"' [ -n "$2" ] && caption=',"caption":"'$(JsonEscape "$2")'"'
[ "$3" != "" ] && desc=',"description":"'$(JsonEscape "$3")'"' [ -n "$3" ] && desc=',"description":"'$(JsonEscape "$3")'"'
[ "$4" != "" ] && markup=',"parse_mode":"'$(JsonEscape "$4")'"' [ -n "$4" ] && markup=',"parse_mode":"'$(JsonEscape "$4")'"'
[ "$5" != "" ] && keyboard=',"reply_markup":"'$(JsonEscape "$5")'"' [ -n "$5" ] && keyboard=',"reply_markup":"'$(JsonEscape "$5")'"'
echo "${title}${caption}${desc}${markup}${keyboard}" echo "${title}${caption}${desc}${markup}${keyboard}"
} }
@ -332,7 +343,7 @@ JsonGetValue() {
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
Json2Array() { Json2Array() {
# shellcheck source=./commands.sh # shellcheck source=./commands.sh
[ "$1" = "" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" )
} }
# output ARRAY as JSON.sh style data # output ARRAY as JSON.sh style data
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
@ -360,7 +371,7 @@ process_client() {
CMD=( ); iQUERY=( ) CMD=( ); iQUERY=( )
iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}"
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log"
if [ "${iQUERY[ID]}" = "" ]; then if [ -z "${iQUERY[ID]}" ]; then
process_message "${num}" "${debug}" process_message "${num}" "${debug}"
else else
process_inline "${num}" "${debug}" process_inline "${num}" "${debug}"
@ -372,13 +383,14 @@ process_client() {
source "${COMMANDS}" "${debug}" & source "${COMMANDS}" "${debug}" &
# then all registered addons # then all registered addons
if [ "${iQUERY[ID]}" = "" ]; then if [ -z "${iQUERY[ID]}" ]; then
event_message "${debug}" event_message "${debug}"
else else
event_inline "${debug}" event_inline "${debug}"
fi fi
# last count users # last count users
# !!!!! DEPRECATED !!!!!
tmpcount="COUNT${CHAT[ID]}" tmpcount="COUNT${CHAT[ID]}"
grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}" grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}"
} }
@ -440,7 +452,7 @@ event_message() {
done done
# ${TEXT[*]} event_text # ${TEXT[*]} event_text
if [ "${MESSAGE[0]}" != "" ]; then if [ -n "${MESSAGE[0]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_TEXT[@]}" for key in "${!BASHBOT_EVENT_TEXT[@]}"
do do
@ -448,7 +460,7 @@ event_message() {
done done
# ${CMD[*]} event_cmd # ${CMD[*]} event_cmd
if [ "${CMD[0]}" != "" ]; then if [ -n "${CMD[0]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_CMD[@]}" for key in "${!BASHBOT_EVENT_CMD[@]}"
do do
@ -457,7 +469,7 @@ event_message() {
fi fi
fi fi
# ${REPLYTO[*]} event_replyto # ${REPLYTO[*]} event_replyto
if [ "${REPLYTO[UID]}" != "" ]; then if [ -n "${REPLYTO[UID]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_REPLYTO[@]}" for key in "${!BASHBOT_EVENT_REPLYTO[@]}"
do do
@ -466,7 +478,7 @@ event_message() {
fi fi
# ${FORWARD[*]} event_forward # ${FORWARD[*]} event_forward
if [ "${FORWARD[UID]}" != "" ]; then if [ -n "${FORWARD[UID]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_FORWARD[@]}" for key in "${!BASHBOT_EVENT_FORWARD[@]}"
do do
@ -475,7 +487,7 @@ event_message() {
fi fi
# ${CONTACT[*]} event_contact # ${CONTACT[*]} event_contact
if [ "${CONTACT[FIRST_NAME]}" != "" ]; then if [ -n "${CONTACT[FIRST_NAME]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_CONTACT[@]}" for key in "${!BASHBOT_EVENT_CONTACT[@]}"
do do
@ -485,7 +497,7 @@ event_message() {
# ${VENUE[*]} event_location # ${VENUE[*]} event_location
# ${LOCALTION[*]} event_location # ${LOCALTION[*]} event_location
if [ "${LOCATION[LONGITUDE]}" != "" ] || [ "${VENUE[TITLE]}" != "" ]; then if [ -n "${LOCATION[LONGITUDE]}" ] || [ -n "${VENUE[TITLE]}" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
for key in "${!BASHBOT_EVENT_LOCATION[@]}" for key in "${!BASHBOT_EVENT_LOCATION[@]}"
do do
@ -537,7 +549,7 @@ process_message() {
# in reply to message from # in reply to message from
REPLYTO=( ) REPLYTO=( )
REPLYTO[UID]="${UPD["result",${num},"message","reply_to_message","from","id"]}" REPLYTO[UID]="${UPD["result",${num},"message","reply_to_message","from","id"]}"
if [ "${REPLYTO[UID]}" != "" ]; then if [ -n "${REPLYTO[UID]}" ]; then
REPLYTO[0]="$(JsonDecode "${UPD["result",${num},"message","reply_to_message","text"]}")" REPLYTO[0]="$(JsonDecode "${UPD["result",${num},"message","reply_to_message","text"]}")"
REPLYTO[ID]="${UPD["result",${num},"message","reply_to_message","message_id"]}" REPLYTO[ID]="${UPD["result",${num},"message","reply_to_message","message_id"]}"
REPLYTO[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","reply_to_message","from","first_name"]}")" REPLYTO[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","reply_to_message","from","first_name"]}")"
@ -548,7 +560,7 @@ process_message() {
# forwarded message from # forwarded message from
FORWARD=( ) FORWARD=( )
FORWARD[UID]="${UPD["result",${num},"message","forward_from","id"]}" FORWARD[UID]="${UPD["result",${num},"message","forward_from","id"]}"
if [ "${FORWARD[UID]}" != "" ]; then if [ -n "${FORWARD[UID]}" ]; then
FORWARD[ID]="${MESSAGE[ID]}" # same as message ID FORWARD[ID]="${MESSAGE[ID]}" # same as message ID
FORWARD[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","first_name"]}")" FORWARD[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","first_name"]}")"
FORWARD[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","last_name"]}")" FORWARD[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","last_name"]}")"
@ -571,7 +583,7 @@ process_message() {
# Contact # Contact
CONTACT=( ) CONTACT=( )
CONTACT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","first_name"]}")" CONTACT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","first_name"]}")"
if [ "${CONTACT[FIRST_NAME]}" != "" ]; then if [ -n "${CONTACT[FIRST_NAME]}" ]; then
CONTACT[USER_ID]="$(JsonDecode "${UPD["result",${num},"message","contact","user_id"]}")" CONTACT[USER_ID]="$(JsonDecode "${UPD["result",${num},"message","contact","user_id"]}")"
CONTACT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","last_name"]}")" CONTACT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","last_name"]}")"
CONTACT[NUMBER]="${UPD["result",${num},"message","contact","phone_number"]}" CONTACT[NUMBER]="${UPD["result",${num},"message","contact","phone_number"]}"
@ -581,7 +593,7 @@ process_message() {
# vunue # vunue
VENUE=( ) VENUE=( )
VENUE[TITLE]="$(JsonDecode "${UPD["result",${num},"message","venue","title"]}")" VENUE[TITLE]="$(JsonDecode "${UPD["result",${num},"message","venue","title"]}")"
if [ "${VENUE[TITLE]}" != "" ]; then if [ -n "${VENUE[TITLE]}" ]; then
VENUE[ADDRESS]="$(JsonDecode "${UPD["result",${num},"message","venue","address"]}")" VENUE[ADDRESS]="$(JsonDecode "${UPD["result",${num},"message","venue","address"]}")"
VENUE[LONGITUDE]="${UPD["result",${num},"message","venue","location","longitude"]}" VENUE[LONGITUDE]="${UPD["result",${num},"message","venue","location","longitude"]}"
VENUE[LATITUDE]="${UPD["result",${num},"message","venue","location","latitude"]}" VENUE[LATITUDE]="${UPD["result",${num},"message","venue","location","latitude"]}"
@ -598,7 +610,7 @@ process_message() {
# service messages # service messages
SERVICE=( ); NEWMEMBER=( ) SERVICE=( ); NEWMEMBER=( )
SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}" SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}"
if [ "${SERVICE[NEWMEMBER]}" != "" ]; then if [ -n "${SERVICE[NEWMEMBER]}" ]; then
NEWMEMBER[ID]="${SERVICE[NEWMEMBER]}" NEWMEMBER[ID]="${SERVICE[NEWMEMBER]}"
NEWMEMBER[FIRSTNAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}" NEWMEMBER[FIRSTNAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}"
NEWMEMBER[LASTNAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}" NEWMEMBER[LASTNAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}"
@ -633,7 +645,7 @@ start_bot() {
local addsleep="100" local addsleep="100"
local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))" local maxsleep="$(( ${BASHBOT_SLEEP:-5000} + 100 ))"
[[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log" [[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log"
[ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" [ -n "${DEBUG}" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\""
[[ "${DEBUG}" = "xdebug"* ]] && set -x [[ "${DEBUG}" = "xdebug"* ]] && set -x
#cleaup old pipes and empty logfiles #cleaup old pipes and empty logfiles
find "${DATADIR}" -type p -delete find "${DATADIR}" -type p -delete
@ -688,14 +700,14 @@ bot_init() {
[[ "${UID}" -eq "0" ]] && RUNUSER="nobody" [[ "${UID}" -eq "0" ]] && RUNUSER="nobody"
echo -n "Enter User to run basbot [$RUNUSER]: " echo -n "Enter User to run basbot [$RUNUSER]: "
read -r TOUSER read -r TOUSER
[ "$TOUSER" = "" ] && TOUSER="$RUNUSER" [ -z "$TOUSER" ] && TOUSER="$RUNUSER"
if ! id "$TOUSER" &>/dev/null; then if ! id "$TOUSER" &>/dev/null; then
echo -e "${RED}User \"$TOUSER\" not found!${NC}" echo -e "${RED}User \"$TOUSER\" not found!${NC}"
exit 3 exit 3
else else
# shellcheck disable=SC2009 # shellcheck disable=SC2009
oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )" oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )"
[ "${oldbot}" != "" ] && \ [ -n "${oldbot}" ] && \
echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}" echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}"
echo "Adjusting user \"${TOUSER}\" files and permissions ..." echo "Adjusting user \"${TOUSER}\" files and permissions ..."
[ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc"
@ -727,7 +739,7 @@ fi
if [ "${SOURCE}" != "yes" ] && [ "$1" != "init" ] && [ "$1" != "help" ]; then if [ "${SOURCE}" != "yes" ] && [ "$1" != "init" ] && [ "$1" != "help" ]; then
ME="$(getBotName)" ME="$(getBotName)"
if [ "$ME" = "" ]; then if [ -z "$ME" ]; then
echo -e "${RED}ERROR: Can't connect to Telegram! Your TOKEN is invalid or you are blocked by ${URL%/*} ...${NC}" echo -e "${RED}ERROR: Can't connect to Telegram! Your TOKEN is invalid or you are blocked by ${URL%/*} ...${NC}"
case "$1" in case "$1" in
"" | "stop" | "kill"* | "suspendb"* ) # warn, but do not exit "" | "stop" | "kill"* | "suspendb"* ) # warn, but do not exit
@ -746,10 +758,10 @@ if [ "${SOURCE}" != "yes" ]; then
# internal options only for use from bashbot and developers # internal options only for use from bashbot and developers
case "$1" in case "$1" in
"outproc") # forward output from interactive and jobs to chat "outproc") # forward output from interactive and jobs to chat
[ "$3" = "" ] && echo "No file to read from" && exit 3 [ -z "$3" ] && echo "No file to read from" && exit 3
[ "$2" = "" ] && echo "No chat to send to" && exit 3 [ -z "$2" ] && echo "No chat to send to" && exit 3
while read -r line ;do while read -r line ;do
[ "$line" != "" ] && send_message "$2" "$line" [ -n "$line" ] && send_message "$2" "$line"
done done
rm -f -r "${DATADIR:-.}/$3" rm -f -r "${DATADIR:-.}/$3"
[ -s "${DATADIR:-.}/$3.log" ] || rm -f "${DATADIR:-.}/$3.log" [ -s "${DATADIR:-.}/$3.log" ] || rm -f "${DATADIR:-.}/$3.log"
@ -772,11 +784,11 @@ if [ "${SOURCE}" != "yes" ]; then
BOTPID="$(proclist "${SESSION}")" BOTPID="$(proclist "${SESSION}")"
case "$1" in case "$1" in
"count") "count") # !!!!! DEPRECATED !!!!!
echo "A total of $(wc -l <"${COUNTFILE}") users used me." echo "A total of $(wc -l <"${COUNTFILE}") users used me."
exit exit
;; ;;
"broadcast") "broadcast") # !!!!! DEPRECATED !!!!!
NUMCOUNT="$(wc -l <"${COUNTFILE}")" NUMCOUNT="$(wc -l <"${COUNTFILE}")"
echo "Sending the broadcast $* to $NUMCOUNT users." echo "Sending the broadcast $* to $NUMCOUNT users."
[ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5" [ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5"
@ -784,7 +796,7 @@ if [ "${SOURCE}" != "yes" ]; then
while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}"
;; ;;
"status") "status")
if [ "${BOTPID}" != "" ]; then if [ -n "${BOTPID}" ]; then
echo -e "${GREEN}Bot is running.${NC}" echo -e "${GREEN}Bot is running.${NC}"
exit exit
else else
@ -795,10 +807,10 @@ if [ "${SOURCE}" != "yes" ]; then
"start") "start")
# shellcheck disable=SC2086 # shellcheck disable=SC2086
[ "${BOTPID}" != "" ] && kill ${BOTPID} [ -n "${BOTPID}" ] && kill ${BOTPID}
nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null & nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null &
echo "Session Name: ${SESSION}" echo "Session Name: ${SESSION}"
if [ "$(proclist "${SESSION}")" != "" ]; then if [ -n "$(proclist "${SESSION}")" ]; then
echo -e "${GREEN}Bot started successfully.${NC}" echo -e "${GREEN}Bot started successfully.${NC}"
else else
echo -e "${RED}An error occurred while starting the bot.${NC}" echo -e "${RED}An error occurred while starting the bot.${NC}"
@ -806,7 +818,7 @@ if [ "${SOURCE}" != "yes" ]; then
fi fi
;; ;;
"kill"|"stop") "kill"|"stop")
if [ "${BOTPID}" != "" ]; then if [ -n "${BOTPID}" ]; then
# shellcheck disable=SC2086 # shellcheck disable=SC2086
if kill ${BOTPID}; then if kill ${BOTPID}; then
echo -e "${GREEN}OK. Bot stopped successfully.${NC}" echo -e "${GREEN}OK. Bot stopped successfully.${NC}"

View File

@ -15,7 +15,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# #
# adjust your language setting here, e.g.when run from other user or cron. # adjust your language setting here, e.g.when run from other user or cron.

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# #
# source from commands.sh to use the aliases # source from commands.sh to use the aliases

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# source from commands.sh to use the inline functions # source from commands.sh to use the inline functions
@ -36,15 +36,15 @@ inline_query_compose(){
JSON='{"type":"article","id":"'$ID'","input_message_content": {"message_text":"'$4'"} '$(title2Json "$3" "" "$5" "$6" "$7")'}' JSON='{"type":"article","id":"'$ID'","input_message_content": {"message_text":"'$4'"} '$(title2Json "$3" "" "$5" "$6" "$7")'}'
;; ;;
"photo") # photo ID photoURL (thumbURL title description caption) "photo") # photo ID photoURL (thumbURL title description caption)
[ "$4" = "" ] && tumb="$3" [ -z "$4" ] && tumb="$3"
JSON='{"type":"photo","id":"'$ID'","photo_url":"'$3'","thumb_url":"'$4${tumb}'"'$(title2Json "$5" "$7" "$6" "$7" "$8")'}' JSON='{"type":"photo","id":"'$ID'","photo_url":"'$3'","thumb_url":"'$4${tumb}'"'$(title2Json "$5" "$7" "$6" "$7" "$8")'}'
;; ;;
"gif") # gif ID photoURL (thumbURL title caption) "gif") # gif ID photoURL (thumbURL title caption)
[ "$4" = "" ] && tumb="$3" [ -z "$4" ] && tumb="$3"
JSON='{"type":"gif","id":"'$ID'","gif_url":"'$3'", "thumb_url":"'$4${tumb}'"'$(title2Json "$5" "$6" "$7" "$8" "$9")'}' JSON='{"type":"gif","id":"'$ID'","gif_url":"'$3'", "thumb_url":"'$4${tumb}'"'$(title2Json "$5" "$6" "$7" "$8" "$9")'}'
;; ;;
"mpeg4_gif") # mpeg4_gif ID mpegURL (thumbURL title caption) "mpeg4_gif") # mpeg4_gif ID mpegURL (thumbURL title caption)
[ "$4" != "" ] && tumb='","thumb_url":"'$4'"' [ -n "$4" ] && tumb='","thumb_url":"'$4'"'
JSON='{"type":"mpeg4_gif","id":"'$ID'","mpeg4_url":"'$3'"'${tumb}$(title2Json "$5" "$6" "" "$7" "$8")'}' JSON='{"type":"mpeg4_gif","id":"'$ID'","mpeg4_url":"'$3'"'${tumb}$(title2Json "$5" "$6" "" "$7" "$8")'}'
;; ;;
"video") # video ID videoURL mime thumbURL title (caption) "video") # video ID videoURL mime thumbURL title (caption)
@ -63,13 +63,13 @@ inline_query_compose(){
JSON='{"type":"location","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'"}' JSON='{"type":"location","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'"}'
;; ;;
"venue") # venue ID lat long title (adress forsquare) "venue") # venue ID lat long title (adress forsquare)
[ "$6" = "" ] && addr="$5" [ -z "$6" ] && addr="$5"
[ "$7" != "" ] && fours=',"foursquare_id":"'$7'"' [ -n "$7" ] && fours=',"foursquare_id":"'$7'"'
JSON='{"type":"venue","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'","address":"'$6${addr}'"'${fours}'}' JSON='{"type":"venue","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'","address":"'$6${addr}'"'${fours}'}'
;; ;;
"contact") # contact ID phone first (last thumb) "contact") # contact ID phone first (last thumb)
[ "$5" != "" ] && last=',"last_name":"'$5'"' [ -n "$5" ] && last=',"last_name":"'$5'"'
[ "$6" != "" ] && tumb='","thumb_url":"'$6'"' [ -n "$6" ] && tumb='","thumb_url":"'$6'"'
JSON='{"type":"contact","id":"'$ID'","phone_number":"'$3'","first_name":"'$4'"'${last}'"}' JSON='{"type":"contact","id":"'$ID'","phone_number":"'$3'","first_name":"'$4'"'${last}'"}'
;; ;;
# title2Json title caption description markup inlinekeyboard # title2Json title caption description markup inlinekeyboard

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# source from commands.sh if you want ro use interactive or background jobs # source from commands.sh if you want ro use interactive or background jobs
@ -50,7 +50,7 @@ start_back() {
# $2 program # $2 program
# $3 prefix # $3 prefix
start_proc() { start_proc() {
[ "$2" = "" ] && return [ -z "$2" ] && return
[ -x "${2%% *}" ] || return 1 [ -x "${2%% *}" ] || return 1
local fifo; fifo="${DATADIR:-.}/$(procname "$1" "$3")" local fifo; fifo="${DATADIR:-.}/$(procname "$1" "$3")"
kill_proc "$1" "$3" kill_proc "$1" "$3"
@ -69,7 +69,7 @@ check_back() {
# $1 chatid # $1 chatid
# $2 prefix # $2 prefix
check_proc() { check_proc() {
[ "$(proclist "$(procname "$1" "$2")")" != "" ] [ -n "$(proclist "$(procname "$1" "$2")")" ]
# shellcheck disable=SC2034 # shellcheck disable=SC2034
res=$?; return $? res=$?; return $?
} }
@ -90,7 +90,7 @@ kill_proc() {
prid="$(proclist "${fifo}")" prid="$(proclist "${fifo}")"
fifo="${DATADIR:-.}/${fifo}" fifo="${DATADIR:-.}/${fifo}"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
[ "${prid}" != "" ] && kill ${prid} [ -n "${prid}" ] && kill ${prid}
[ -s "${fifo}.log" ] || rm -f "${fifo}.log" [ -s "${fifo}.log" ] || rm -f "${fifo}.log"
[ -p "${fifo}" ] && rm -f "${fifo}"; [ -p "${fifo}" ] && rm -f "${fifo}";
} }

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# source once magic, function named like file # source once magic, function named like file
eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
@ -58,10 +58,10 @@ user_is_botadmin() {
user_is_allowed() { user_is_allowed() {
local acl="$1" local acl="$1"
[ "$1" = "" ] && return 1 [ -z "$1" ] && return 1
grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0 grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0
[ "$2" != "" ] && acl="${acl}:$2" [ -n "$2" ] && acl="${acl}:$2"
grep -F -xq "${acl}:*" <"${BOTACL}" && return 0 grep -F -xq "${acl}:*" <"${BOTACL}" && return 0
[ "$3" != "" ] && acl="${acl}:$3" [ -n "$3" ] && acl="${acl}:$3"
grep -F -xq "${acl}" <"${BOTACL}" grep -F -xq "${acl}" <"${BOTACL}"
} }

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# #
# source from commands.sh to use jsonDB functions # source from commands.sh to use jsonDB functions
# #
@ -20,7 +20,7 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
# $2 filename, must be relative to BASHBOT_ETC, and not contain '..' # $2 filename, must be relative to BASHBOT_ETC, and not contain '..'
jssh_readDB() { jssh_readDB() {
local DB; DB="$(jssh_checkDB "$2")" local DB; DB="$(jssh_checkDB "$2")"
[ "${DB}" = "" ] && return 1 [ -z "${DB}" ] && return 1
[ ! -f "${DB}" ] && return 2 [ ! -f "${DB}" ] && return 2
Json2Array "$1" <"${DB}" Json2Array "$1" <"${DB}"
} }
@ -31,7 +31,7 @@ jssh_readDB() {
# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
jssh_writeDB() { jssh_writeDB() {
local DB; DB="$(jssh_checkDB "$2")" local DB; DB="$(jssh_checkDB "$2")"
[ "${DB}" = "" ] && return 1 [ -z "${DB}" ] && return 1
[ ! -f "${DB}" ] && return 2 [ ! -f "${DB}" ] && return 2
Array2Json "$1" >"${DB}" Array2Json "$1" >"${DB}"
} }
@ -41,10 +41,10 @@ jssh_writeDB() {
# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
jssh_updateDB() { jssh_updateDB() {
declare -n ARRAY="$1" declare -n ARRAY="$1"
[ "${ARRAY[*]}" = "" ] && return 1 [ -z "${ARRAY[*]}" ] && return 1
declare -A oldARR newARR declare -A oldARR newARR
jssh_readDB "oldARR" "$2" || return "$?" jssh_readDB "oldARR" "$2" || return "$?"
if [ "${oldARR[*]}" = "" ]; then if [ -z "${oldARR[*]}" ]; then
# no old content # no old content
jssh_writeDB "$1" "$2" jssh_writeDB "$1" "$2"
else else
@ -68,7 +68,7 @@ jssh_insertDB() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
local key="$1" value="$2" local key="$1" value="$2"
local DB; DB="$(jssh_checkDB "$3")" local DB; DB="$(jssh_checkDB "$3")"
[ "${DB}" = "" ] && return 1 [ -z "${DB}" ] && return 1
[ ! -f "${DB}" ] && return 2 [ ! -f "${DB}" ] && return 2
# its append, but last one counts, its a simple DB ... # its append, but last one counts, its a simple DB ...
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}"
@ -90,7 +90,7 @@ jssh_getDB() {
# $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
jssh_newDB() { jssh_newDB() {
local DB; DB="$(jssh_checkDB "$1")" local DB; DB="$(jssh_checkDB "$1")"
[ "${DB}" = "" ] && return 1 [ -z "${DB}" ] && return 1
[ -f "${DB}" ] && return 2 # already exist, do not zero out [ -f "${DB}" ] && return 2 # already exist, do not zero out
printf '\n' >"${DB}" printf '\n' >"${DB}"
} }
@ -98,7 +98,7 @@ jssh_newDB() {
# $1 filename, check filename, it must be relative to BASHBOT_ETC, and not contain '..' # $1 filename, check filename, it must be relative to BASHBOT_ETC, and not contain '..'
# returns real path to DB file if everything is ok # returns real path to DB file if everything is ok
jssh_checkDB(){ jssh_checkDB(){
[ "$1" = "" ] && return 1 [ -z "$1" ] && return 1
local DB="${BASHBOT_ETC:-.}/$1.jssh" local DB="${BASHBOT_ETC:-.}/$1.jssh"
[[ "$1" = "${BASHBOT_ETC:-.}"* ]] && DB="$1.jssh" [[ "$1" = "${BASHBOT_ETC:-.}"* ]] && DB="$1.jssh"
[[ "$1" = *'..'* ]] && return 2 [[ "$1" = *'..'* ]] && return 2

View File

@ -5,7 +5,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.94-pre-1-g4aa7561 #### $$VERSION$$ v0.94-pre-2-gc0a633f
# source once magic, function named like file # source once magic, function named like file
eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
@ -67,19 +67,19 @@ sendEmpty() {
} }
send_keyboard() { send_keyboard() {
if [[ "$3" != *'['* ]]; then old_send_keyboard "${@}"; return; fi if [[ "$3" != *'['* ]]; then old_send_keyboard "${@}"; return; fi
local text; text='"text":"'$(JsonEscape "${2}")'"'; [ "${2}" = "" ] && text='"text":"'"${ISEMPTY}"'"' local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"${ISEMPTY}"'"'
local one_time=', "one_time_keyboard":true' && [ "$4" != "" ] && one_time="" local one_time=', "one_time_keyboard":true' && [ -n "$4" ] && one_time=""
sendEmpty "${1}" "${text}"', "reply_markup": {"keyboard": [ '"${3}"' ] '"${one_time}"'}' "$MSG_URL" sendEmpty "${1}" "${text}"', "reply_markup": {"keyboard": [ '"${3}"' ] '"${one_time}"'}' "$MSG_URL"
# '"text":"$2", "reply_markup": {"keyboard": [ ${3} ], "one_time_keyboard": true}' # '"text":"$2", "reply_markup": {"keyboard": [ ${3} ], "one_time_keyboard": true}'
} }
remove_keyboard() { remove_keyboard() {
local text; text='"text":"'$(JsonEscape "${2}")'"'; [ "${2}" = "" ] && text='"text":"'"${ISEMPTY}"'"' local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"${ISEMPTY}"'"'
sendEmpty "${1}" "${text}"', "reply_markup": {"remove_keyboard":true}' "$MSG_URL" sendEmpty "${1}" "${text}"', "reply_markup": {"remove_keyboard":true}' "$MSG_URL"
#JSON='"text":"$2", "reply_markup": {"remove_keyboard":true}' #JSON='"text":"$2", "reply_markup": {"remove_keyboard":true}'
} }
send_inline_keyboard() { send_inline_keyboard() {
local text; text='"text":"'$(JsonEscape "${2}")'"'; [ "${2}" = "" ] && text='"text":"'"${ISEMPTY}"'"' local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"${ISEMPTY}"'"'
sendEmpty "${1}" "${text}"', "reply_markup": {"inline_keyboard": [ '"${3}"' ]}' "$MSG_URL" sendEmpty "${1}" "${text}"', "reply_markup": {"inline_keyboard": [ '"${3}"' ]}' "$MSG_URL"
# JSON='"text":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]}' # JSON='"text":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]}'
} }
@ -93,7 +93,7 @@ UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}"
# for now this can only send local files with curl! # for now this can only send local files with curl!
# extend to allow send files by URL or telegram ID # extend to allow send files by URL or telegram ID
send_file() { send_file() {
[ "$2" = "" ] && return [ -z "$2" ] && return
[[ "$2" = "http"* ]] && return # currently we do not support URL [[ "$2" = "http"* ]] && return # currently we do not support URL
upload_file "${@}" upload_file "${@}"
} }
@ -150,25 +150,25 @@ upload_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 [ -z "$2" ] && return
sendJson "${1}" '"action": "'"${2}"'"' "$ACTION_URL" sendJson "${1}" '"action": "'"${2}"'"' "$ACTION_URL"
} }
send_location() { send_location() {
[ "$3" = "" ] && return [ -z "$3" ] && return
sendJson "${1}" '"latitude": '"${2}"', "longitude": '"${3}"'' "$LOCATION_URL" sendJson "${1}" '"latitude": '"${2}"', "longitude": '"${3}"'' "$LOCATION_URL"
} }
send_venue() { send_venue() {
local add="" local add=""
[ "$5" = "" ] && return [ -z "$5" ] && return
[ "$6" != "" ] && add=', "foursquare_id": '"$6"'' [ -n "$6" ] && add=', "foursquare_id": '"$6"''
sendJson "${1}" '"latitude": '"${2}"', "longitude": '"${3}"', "address": "'"${5}"'", "title": "'"${4}"'"'"${add}" "$VENUE_URL" sendJson "${1}" '"latitude": '"${2}"', "longitude": '"${3}"', "address": "'"${5}"'", "title": "'"${4}"'"'"${add}" "$VENUE_URL"
} }
forward_message() { forward_message() {
[ "$3" = "" ] && return [ -z "$3" ] && return
sendJson "${1}" '"from_chat_id": '"${2}"', "message_id": '"${3}"'' "$FORWARD_URL" sendJson "${1}" '"from_chat_id": '"${2}"', "message_id": '"${3}"'' "$FORWARD_URL"
} }
forward() { # backward compatibility forward() { # backward compatibility
@ -176,7 +176,7 @@ forward() { # backward compatibility
} }
send_message() { send_message() {
[ "$2" = "" ] && return [ -z "$2" ] && return
local text keyboard btext burl no_keyboard file lat long title address sent local text keyboard btext burl no_keyboard file lat long title address sent
text="$(sed <<< "${2}" 's/ mykeyboardend.*//;s/ *my[kfltab][a-z]\{2,13\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')" text="$(sed <<< "${2}" 's/ mykeyboardend.*//;s/ *my[kfltab][a-z]\{2,13\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')"
text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')"
@ -191,27 +191,27 @@ send_message() {
title="$(sed <<< "${2}" '/mytitlestartshere /!d;s/.*mytitlestartshere //;s/ *my[kfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" title="$(sed <<< "${2}" '/mytitlestartshere /!d;s/.*mytitlestartshere //;s/ *my[kfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
address="$(sed <<< "${2}" '/myaddressstartshere /!d;s/.*myaddressstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" address="$(sed <<< "${2}" '/myaddressstartshere /!d;s/.*myaddressstartshere //;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"
} }
if [ "$no_keyboard" != "" ]; then if [ -n "$no_keyboard" ]; then
remove_keyboard "$1" "$text" remove_keyboard "$1" "$text"
sent=y sent=y
fi fi
if [ "$keyboard" != "" ]; then if [ -n "$keyboard" ]; then
if [[ "$keyboard" != *"["* ]]; then # pre 0.60 style if [[ "$keyboard" != *"["* ]]; then # pre 0.60 style
keyboard="[ ${keyboard//\" \"/\" \] , \[ \"} ]" keyboard="[ ${keyboard//\" \"/\" \] , \[ \"} ]"
fi fi
send_keyboard "$1" "$text" "$keyboard" send_keyboard "$1" "$text" "$keyboard"
sent=y sent=y
fi fi
if [ "$btext" != "" ] && [ "$burl" != "" ]; then if [ -n "$btext" ] && [ -n "$burl" ]; then
send_button "$1" "$text" "$btext" "$burl" send_button "$1" "$text" "$btext" "$burl"
sent=y sent=y
fi fi
if [ "$file" != "" ]; then if [ -n "$file" ]; then
send_file "$1" "$file" "$text" send_file "$1" "$file" "$text"
sent=y sent=y
fi fi
if [ "$lat" != "" ] && [ "$long" != "" ]; then if [ -n "$lat" ] && [ -n "$long" ]; then
if [ "$address" != "" ] && [ "$title" != "" ]; then if [ -n "$address" ] && [ -n "$title" ]; then
send_venue "$1" "$lat" "$long" "$title" "$address" send_venue "$1" "$lat" "$long" "$title" "$address"
else else
send_location "$1" "$lat" "$long" send_location "$1" "$lat" "$long"