From 8affc94813ce6da779159346729b516c3c5c0237 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Jun 2020 21:18:32 +0200 Subject: [PATCH 01/76] start 0.98 dev, create botconfig db --- bashbot.sh | 138 ++++++++++++++++++++++++++++------------------ modules/jsonDB.sh | 2 +- 2 files changed, 85 insertions(+), 55 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5c7f090..ec9664f 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-0-ga8fe178 # # Exit Codes: # - 0 sucess (hopefully) @@ -69,6 +69,12 @@ Array2Json() { printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" done } +getConfigKey() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + declare -A confARR + Json2Array "confARR" <"${BOTDATABASE}.jssh" + printf '%s' "${confARR["$1"]}" +} # get location and name of bashbot.sh SCRIPT="$0" @@ -108,6 +114,7 @@ if [ ! -w "." ]; then fi # Setup and check environment if BOTTOKEN is NOT set +BOTDATABASE="${BASHBOT_ETC:-.}/botconfig" TOKENFILE="${BASHBOT_ETC:-.}/token" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" BOTACL="${BASHBOT_ETC:-.}/botacl" @@ -119,13 +126,22 @@ LOGDIR="${RUNDIR:-.}/logs" if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then LOGDIR="${RUNDIR:-.}" fi +DEBUGLOG="${LOGDIR}/DEBUG.log" ERRORLOG="${LOGDIR}/ERROR.log" +UPDATELOG="${LOGDIR}/UPDATE.log" # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then - # BOTTOKEN empty read from file - if [ ! -f "${TOKENFILE}" ]; then - if [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then + # DATABASE does not exist, create + [ ! -f "${BOTDATABASE}.jssh" ] && + printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTDATABASE}.jssh" + # BOTTOKEN empty read ask user + if ! grep -qs '\["bottoken"\]' "${BOTDATABASE}.jssh" ; then + # convert old token + if [ -r "${TOKENFILE}" ]; then + printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh" + # no old token avalible ask user + elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" exit 2 else @@ -133,12 +149,9 @@ if [ -z "${BOTTOKEN}" ]; then echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r BOTTOKEN - printf '%s\n' "${BOTTOKEN}" > "${TOKENFILE}" + printf '["bottoken"]\t"%s"\n' "${BOTTOKEN}" >> "${BOTDATABASE}.jssh" fi fi - # read BOTTOKEN from file and removen everyting from first newline to end - BOTTOKEN="$(< "${TOKENFILE}")" - BOTTOKEN="${BOTTOKEN%%$'\n'*}" # setup botadmin file if [ ! -f "${BOTADMIN}" ]; then @@ -186,10 +199,9 @@ if [ -z "${BOTTOKEN}" ]; then fi fi -# do we have BSD sed -if ! sed '1ia' /dev/null; then - echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" -fi +# read BOTTOKEN from bot database if not set +[ -z "${BOTTOKEN}" ] && BOTTOKEN="$(getConfigKey "bottoken")" + # BOTTOKEN format checks if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then echo -e "${ORANGE}Warning, your bottoken may incorrect. it should have the following format:${NC}" @@ -213,6 +225,9 @@ ME_URL=$URL'/getMe' UPD_URL=$URL'/getUpdates?offset=' GETFILE_URL=$URL'/getFile' +################# +# BASHBOT COMMON functions + declare -rx SCRIPT SCRIPTDIR MODULEDIR RUNDIR ADDONDIR TOKENFILE BOTADMIN BOTACL DATADIR COUNTFILE declare -rx BOTTOKEN URL ME_URL UPD_URL GETFILE_URL @@ -245,14 +260,17 @@ done # BASHBOT INTERNAL functions # +# do we have BSD sed +if ! sed '1ia' /dev/null; then + echo -e "${ORANGE}Warning: You may run on a BSD style system without gnu utils ...${NC}" +fi #jsonDB is now mandatory if ! _is_function jssh_newDB ; then echo -e "${RED}ERROR: Mandatory module jsonDB is missing or not readable!" exit 6 fi -################# -# BASHBOT COMMON functions + # $1 URL, $2 filename in DATADIR # outputs final filename download() { @@ -328,11 +346,11 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then if [ -n "$5" ]; then # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ - -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )" + -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" else # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ - -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" + -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" fi sendJsonResult "${res}" "sendUpload (curl)" "$@" } @@ -514,15 +532,13 @@ process_client() { # process per message type if [ -z "${iQUERY[ID]}" ]; then - MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" process_message "${num}" "${debug}" - [[ -n "${debug}" ]] && printf "%s: update received FROM=%s CHAT=%s CMD=%s\n"\ - "$(date)" "${USER[USERNAME]:0:20} (${USER[ID]})"\ - "${CHAT[USERNAME]:0:20}${CHAT[TITLE]:0:30} (${CHAT[ID]})" "${MESSAGE%%[ \?]*}" + printf "%s: update received FROM=%s CHAT=%s CMD=%s\n" "$(date)" "${USER[USERNAME]:0:20} (${USER[ID]})"\ + "${CHAT[USERNAME]:0:20}${CHAT[TITLE]:0:30} (${CHAT[ID]})" "${MESSAGE:0:30}" >>"${UPDATELOG}" else process_inline "${num}" "${debug}" - [[ -n "${debug}" ]] && printf "%s: iQuery received FROM=%s iQUERY=%s\n"\ - "$(date)" "${iQUERY[USERNAME]} (${iQUERY[USER_ID]})" "${iQUERY[0]}" + printf "%s: iQuery received FROM=%s iQUERY=%s\n" "$(date)"\ + "${iQUERY[USERNAME]:0:20} (${iQUERY[USER_ID]})" "${iQUERY[0]}" >>"${UPDATELOG}" fi ##### # process inline and message events @@ -673,6 +689,7 @@ process_inline() { process_message() { local num="$1" # Message + MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}" # Chat ID is now parsed when update isreceived @@ -791,8 +808,8 @@ start_bot() { local stepsleep="${BASHBOT_SLEEP_STEP:-100}" local maxsleep="${BASHBOT_SLEEP:-5000}" # redirect to Debug.log - [[ "${DEBUG}" == *"debug" ]] && exec &>>"${LOGDIR}/DEBUG.log" - [ -n "${DEBUG}" ] && printf "%s: Start BASHBOT in Mode \"%s\" ==========\n" "$(date)" "${DEBUG}" + printf "%s: Start BASHBOT updates in Mode \"%s\" ==========\n" "$(date)" "${DEBUG}" >>"${DEBUGLOG}" + [[ "${DEBUG}" == *"debug" ]] && exec &>>"${DEBUGLOG}" [[ "${DEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles find "${DATADIR}" -type p -delete @@ -826,7 +843,7 @@ start_bot() { # did we ge an responsn0r if [ -n "${UPDATE}" ]; then # we got something, do processing - [ "${OFFSET}" = "-999" ] && [ "${nextsleep}" -gt "${maxsleep}" ] &&\ + [ "${OFFSET}" = "-999" ] && [ "${nextsleep}" -gt "$((maxsleep*2))" ] &&\ printf "%s: Recovered from timeout/broken/no connection, continue with telegram updates\n"\ "$(date)" >>"${ERRORLOG}" # escape bash $ expansion bug @@ -908,18 +925,6 @@ if [ ! -f "${JSONSHFILE}" ]; then chmod +x "${JSONSHFILE}" fi -if [ "${SOURCE}" != "yes" ] && [ "$1" != "init" ] && [ "$1" != "help" ]; then - ME="$(getBotName)" - if [ -z "$ME" ]; then - echo -e "${RED}ERROR: Can't connect to Telegram! Your TOKEN is invalid or you are blocked by ${URL%/*} ...${NC}" - case "$1" in - "" | "stop" | "kill"* | "suspendb"* ) # warn, but do not exit - echo -e "${RED}Ignored to continue for $1 ... ${NC}";; - *) exit 1;; - esac - fi -fi - # source the script with source as param to use functions in other scripts # do not execute if read from other scripts @@ -928,33 +933,52 @@ if [ "${SOURCE}" != "yes" ]; then ############## # internal options only for use from bashbot and developers case "$1" in - "outproc") # forward output from interactive and jobs to chat - [ -z "$3" ] && echo "No file to read from" && exit 3 + # all these commands need the botname and a working connection + "botname"|"outproc"|"start"*|"stop"*|"kill"*|"resume"*|"suspend"*) + ME="$(getBotName)" + if [ -n "${ME}" ]; then + # ok we have a connection an got botname, save it + echo -e "${GREY}Bottoken is valid ...${NC}" + jssh_updateKeyDB "botname" "${ME}" "${BOTDATABASE}" + rm -f "${BOTDATABASE}.jssh.flock" + else + echo -e "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NC}" + ME="$(getConfigKey "botname")" + if [ -z "$ME" ]; then + echo -e "${RED}ERROR: No cached botname, can't continue! ...${NC}" + exit 1 + fi + fi +set +x + printf "Bot Name: %s\n" "${ME}" + SESSION="${ME:-_bot}-startbot" + BOTPID="$(proclist "${SESSION}")" + [ "$1" = "botname" ] && exit + ;& + # used to send output of backgrond and interactive to chats + "outproc") # $2 chat_id $3 identifier of job, internal use only! + [ -z "$3" ] && echo "No job identifier" && exit 3 [ -z "$2" ] && echo "No chat to send to" && exit 3 + # read until terminated while read -r line ;do [ -n "$line" ] && send_message "$2" "$line" done + # cleanup datadir, keep logfile if not empty rm -f -r "${DATADIR:-.}/$3" [ -s "${DATADIR:-.}/$3.log" ] || rm -f "${DATADIR:-.}/$3.log" exit ;; + # finally starts the read update loop, internal use only1 "startbot" ) start_bot "$2" exit ;; - "init") # adjust users and permissions + # run after every update to update files and adjust permissions + "init") bot_init "$2" exit ;; - esac - - - ############### - # "official" arguments as shown to users - SESSION="${ME:-unknown}-startbot" - BOTPID="$(proclist "${SESSION}")" - - case "$1" in + # print usage sats "stats"|'count') declare -A STATS jssh_readDB_async "STATS" "${COUNTFILE}" @@ -970,6 +994,7 @@ if [ "${SOURCE}" != "yes" ]; then echo "A total of ${MESSAGES} messages from ${USERS} users are processed." exit ;; + # sedn message to all users 'broadcast') declare -A SENDALL shift @@ -988,6 +1013,7 @@ if [ "${SOURCE}" != "yes" ]; then echo -e "\nMessage \"$*\" sent to ${USERS} users." exit ;; + # does what is says "status") if [ -n "${BOTPID}" ]; then echo -e "${GREEN}Bot is running.${NC}" @@ -998,11 +1024,13 @@ if [ "${SOURCE}" != "yes" ]; then fi ;; + # start bot as background jod and check if bot is running "start") # shellcheck disable=SC2086 [ -n "${BOTPID}" ] && kill ${BOTPID} nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null & - echo "Session Name: ${SESSION}" + printf "Session Name: %s\n" "${SESSION}" + sleep 1 if [ -n "$(proclist "${SESSION}")" ]; then echo -e "${GREEN}Bot started successfully.${NC}" else @@ -1010,7 +1038,8 @@ if [ "${SOURCE}" != "yes" ]; then exit 5 fi ;; - "kill"|"stop") + # does what it says + "stop") if [ -n "${BOTPID}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then @@ -1022,7 +1051,8 @@ if [ "${SOURCE}" != "yes" ]; then fi exit ;; - "resumeb"* | "killb"* | "suspendb"*) + # suspend, resume or kill backgrund jobs + "suspendb"*|"resumeb"*|"killb"*) _is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; } job_control "$1" ;; @@ -1031,8 +1061,8 @@ if [ "${SOURCE}" != "yes" ]; then exit ;; *) - echo -e "${RED}${REALME}: BAD REQUEST${NC}" - echo -e "${ORANGE}Available arguments: ${GREY}start, stop, kill, status, stats, broadcast, help, suspendback, resumeback, killback${NC}" + echo -e "${RED}${REALME##*/}: unknown command${NC}" + echo -e "${ORANGE}Available commands: ${GREY}start, stop, status, stats, broadcast, help, suspendback, resumeback, killback${NC}" exit 4 ;; esac diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 924a58b..eed6c90 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-1-g15e6c7b +#### $$VERSION$$ v0.98-dev-0-ga8fe178 # # source from commands.sh to use jsonDB functions # From eb4fa6840bc83a24554f56dff13403fde487a220 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Jun 2020 21:24:54 +0200 Subject: [PATCH 02/76] add new file to gitignore --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d8956d3..9575134 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,11 @@ /.github/ /count* /token* -/bocked* +/blocked* +/botconf* +/botacl* +/botown* +ˆ.jssh *.save *.log *.swp From a901cc78d456dcebad2d9dff59922f86c9e4e88a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 12 Jun 2020 23:41:47 +0200 Subject: [PATCH 03/76] first working version with jssh botconfig database --- bashbot.rc | 4 ++-- bashbot.sh | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bashbot.rc b/bashbot.rc index f09e946..eeb2ef2 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-2-geb4fa68 # shellcheck disable=SC2009 # shellcheck disable=SC2181 @@ -45,7 +45,7 @@ case "$1" in RETVAL=$? ;; 'stop') - $runcmd "$start kill" + $runcmd "$start stop" RETVAL=$? ;; 'status') diff --git a/bashbot.sh b/bashbot.sh index ec9664f..f07b411 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-0-ga8fe178 +#### $$VERSION$$ v0.98-dev-2-geb4fa68 # # Exit Codes: # - 0 sucess (hopefully) @@ -534,7 +534,8 @@ process_client() { if [ -z "${iQUERY[ID]}" ]; then process_message "${num}" "${debug}" printf "%s: update received FROM=%s CHAT=%s CMD=%s\n" "$(date)" "${USER[USERNAME]:0:20} (${USER[ID]})"\ - "${CHAT[USERNAME]:0:20}${CHAT[TITLE]:0:30} (${CHAT[ID]})" "${MESSAGE:0:30}" >>"${UPDATELOG}" + "${CHAT[USERNAME]:0:20}${CHAT[TITLE]:0:30} (${CHAT[ID]})"\ + "${MESSAGE:0:30}${CAPTION:0:30}${URLS[*]:0:30}" >>"${UPDATELOG}" else process_inline "${num}" "${debug}" printf "%s: iQuery received FROM=%s iQUERY=%s\n" "$(date)"\ @@ -934,7 +935,7 @@ if [ "${SOURCE}" != "yes" ]; then # internal options only for use from bashbot and developers case "$1" in # all these commands need the botname and a working connection - "botname"|"outproc"|"start"*|"stop"*|"kill"*|"resume"*|"suspend"*) + "botname"|"outproc"|"start"*|"stop"*|"kill"*|"resume"*|"suspend"*|"status") ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection an got botname, save it @@ -949,12 +950,11 @@ if [ "${SOURCE}" != "yes" ]; then exit 1 fi fi -set +x printf "Bot Name: %s\n" "${ME}" SESSION="${ME:-_bot}-startbot" BOTPID="$(proclist "${SESSION}")" [ "$1" = "botname" ] && exit - ;& + ;;& # used to send output of backgrond and interactive to chats "outproc") # $2 chat_id $3 identifier of job, internal use only! [ -z "$3" ] && echo "No job identifier" && exit 3 @@ -979,7 +979,8 @@ set +x exit ;; # print usage sats - "stats"|'count') + "count") echo -e "${RED}Command ${GREY}count${RED} is deprecated, use ${GREY}stats{$RED}instead.${NC}";& + "stats") declare -A STATS jssh_readDB_async "STATS" "${COUNTFILE}" for MSG in ${!STATS[*]} @@ -1016,10 +1017,10 @@ set +x # does what is says "status") if [ -n "${BOTPID}" ]; then - echo -e "${GREEN}Bot is running.${NC}" + echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}" exit else - echo -e "${ORANGE}Bot not running.${NC}" + echo -e "${ORANGE}Bot not running with UID ${RUNUSER}.${NC}" exit 5 fi ;; @@ -1039,6 +1040,7 @@ set +x fi ;; # does what it says + "kill") echo -e "${RED}Command ${GREY}kill${RED} is deprecated, use ${GREY}stop{$RED}instead.${NC}";& "stop") if [ -n "${BOTPID}" ]; then # shellcheck disable=SC2086 @@ -1048,6 +1050,8 @@ set +x echo -e "${RED}An error occured while stopping bot.${NC}" exit 5 fi + else + echo -e "${ORANGE}Bot not running with UID ${RUNUSER}.${NC}" fi exit ;; From f32833ccf9251a134acf103a3bef56fe9b9bf265 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 09:30:30 +0200 Subject: [PATCH 04/76] output bot validation result on tty only --- bashbot.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f07b411..714930c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-2-geb4fa68 +#### $$VERSION$$ v0.98-dev-3-ga901cc7 # # Exit Codes: # - 0 sucess (hopefully) @@ -939,7 +939,7 @@ if [ "${SOURCE}" != "yes" ]; then ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection an got botname, save it - echo -e "${GREY}Bottoken is valid ...${NC}" + [ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}" jssh_updateKeyDB "botname" "${ME}" "${BOTDATABASE}" rm -f "${BOTDATABASE}.jssh.flock" else @@ -950,7 +950,7 @@ if [ "${SOURCE}" != "yes" ]; then exit 1 fi fi - printf "Bot Name: %s\n" "${ME}" + [ -n "${CLEAR}" ] && printf "Bot Name: %s\n" "${ME}" SESSION="${ME:-_bot}-startbot" BOTPID="$(proclist "${SESSION}")" [ "$1" = "botname" ] && exit From 5baab14cd2d171456bf8eb6dc7ecd11a8bb2b77f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 11:05:28 +0200 Subject: [PATCH 05/76] fix newmwber first_name last_name --- bashbot.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 714930c..d759141 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-3-ga901cc7 +#### $$VERSION$$ v0.98-dev-4-gf32833c # # Exit Codes: # - 0 sucess (hopefully) @@ -698,10 +698,11 @@ process_message() { CHAT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","last_name"]}")" CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")" CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")" + # set real name as username if empty + : "${CHAT[USERNAME]:=${CHAT[FIRST_NAME]} ${CHAT[LAST_NAME]}}" CHAT[TITLE]="$(JsonDecode "${UPD["result",${num},"message","chat","title"]}")" CHAT[TYPE]="$(JsonDecode "${UPD["result",${num},"message","chat","type"]}")" CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}" - CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="${CHAT[ALL_ADMIN]}" # backward compatibility # user ID is now parsed when update isreceived #USER[ID]="${UPD["result",${num},"message","from","id"]}" @@ -775,10 +776,11 @@ process_message() { SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}" if [ -n "${SERVICE[NEWMEMBER]}" ]; then NEWMEMBER[ID]="${SERVICE[NEWMEMBER]}" - NEWMEMBER[FIRSTNAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}" - NEWMEMBER[LASTNAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}" + NEWMEMBER[FIRST_NAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}" + NEWMEMBER[LAST_NAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}" NEWMEMBER[USERNAME]="${UPD["result",${num},"message","new_chat_member","username"]}" NEWMEMBER[ISBOT]="${UPD["result",${num},"message","new_chat_member","is_bot"]}" + MESSAGE[0]="/new_chat_member ${NEWMEMBER[USERNAME]:=${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]}}" fi SERVICE[LEFTMEMBER]="${UPD["result",${num},"message","left_chat_member","id"]}" SERVICE[NEWTILE]="${UPD["result",${num},"message","new_chat_title"]}" From e0470aadc0f92b23e2735c37d5bb4f7b5ca75ef3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 17:56:43 +0200 Subject: [PATCH 06/76] move Json2Arru and Array2Json to jsshDB module --- bashbot.sh | 24 +++--------------------- modules/jsonDB.sh | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d759141..5f09f36 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-4-gf32833c +#### $$VERSION$$ v0.98-dev-5-g5baab14 # # Exit Codes: # - 0 sucess (hopefully) @@ -53,27 +53,9 @@ _round_float() { local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0" LC_ALL=C printf "%.${digit}f" "${1}" } -# read JSON.sh style data and asssign to an ARRAY -# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling -Json2Array() { - # shellcheck source=./commands.sh - [ -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 -# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling -Array2Json() { - local key - declare -n ARRAY="$1" - for key in "${!ARRAY[@]}" - do - printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" - done -} getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - declare -A confARR - Json2Array "confARR" <"${BOTDATABASE}.jssh" - printf '%s' "${confARR["$1"]}" + sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } # get location and name of bashbot.sh @@ -136,7 +118,7 @@ if [ -z "${BOTTOKEN}" ]; then [ ! -f "${BOTDATABASE}.jssh" ] && printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTDATABASE}.jssh" # BOTTOKEN empty read ask user - if ! grep -qs '\["bottoken"\]' "${BOTDATABASE}.jssh" ; then + if [ -z "$(getConfigKey "bottoken")" ]; then # convert old token if [ -r "${TOKENFILE}" ]; then printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index eed6c90..e486ebf 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-0-ga8fe178 +#### $$VERSION$$ v0.98-dev-5-g5baab14 # # source from commands.sh to use jsonDB functions # @@ -323,4 +323,22 @@ jssh_clearDB_async() { printf '' >"${DB}" } +# read JSON.sh style data and asssign to an ARRAY +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Json2Array() { + # shellcheck source=./commands.sh + [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) +} +# get Config Key from jssh file without jsshDB +# output ARRAY as JSON.sh style data +# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling +Array2Json() { + [ -z "$1" ] && return 1 + local key + declare -n ARRAY="$1" + for key in "${!ARRAY[@]}" + do + printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}" + done +} From ca0509556acf30cfc7020c9aa9de02fd5d017033 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 18:40:34 +0200 Subject: [PATCH 07/76] fix/optimize getKey functions --- bashbot.sh | 4 ++-- modules/jsonDB.sh | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5f09f36..6a91124 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-5-g5baab14 +#### $$VERSION$$ v0.98-dev-6-ge0470aa # # Exit Codes: # - 0 sucess (hopefully) @@ -55,7 +55,7 @@ _round_float() { } getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 + [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } # get location and name of bashbot.sh diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index e486ebf..15e64af 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-5-g5baab14 +#### $$VERSION$$ v0.98-dev-6-ge0470aa # # source from commands.sh to use jsonDB functions # @@ -124,12 +124,10 @@ if _exists flock; then jssh_getKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 - Json2Array "oldARR" <"${DB}" + [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 } 200>"${DB}${BASHBOT_LOCKNAME}" - printf '%s' "${oldARR["$1"]}" } @@ -288,9 +286,7 @@ jssh_deleteKeyDB_async() { jssh_getKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR - Json2Array "oldARR" <"${DB}" - printf '%s' "${oldARR["$1"]}" + [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 } jssh_countKeyDB_async() { @@ -326,7 +322,7 @@ jssh_clearDB_async() { # read JSON.sh style data and asssign to an ARRAY # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling Json2Array() { - # shellcheck source=./commands.sh + # shellcheck disable=SC1091,SC1090 [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) } # get Config Key from jssh file without jsshDB From cdc6dd323dcf8f20dae54c7a55c7d9a60af4021c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 19:57:24 +0200 Subject: [PATCH 08/76] add LEFTMEMBER values, doc values --- bashbot.sh | 10 +++++++++- doc/2_usage.md | 11 ++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 6a91124..c168e43 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-6-ge0470aa +#### $$VERSION$$ v0.98-dev-7-gca05095 # # Exit Codes: # - 0 sucess (hopefully) @@ -765,6 +765,14 @@ process_message() { MESSAGE[0]="/new_chat_member ${NEWMEMBER[USERNAME]:=${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]}}" fi SERVICE[LEFTMEMBER]="${UPD["result",${num},"message","left_chat_member","id"]}" + if [ -n "${SERVICE[LEFTMEMBER]}" ]; then + LEFTMEBER[ID]="${SERVICE[LEFTMEBER]}" + LEFTMEBER[FIRST_NAME]="${UPD["result",${num},"message","left_chat_member","first_name"]}" + LEFTMEBER[LAST_NAME]="${UPD["result",${num},"message","left_chat_member","last_name"]}" + LEFTMEBER[USERNAME]="${UPD["result",${num},"message","left_chat_member","username"]}" + LEFTMEBER[ISBOT]="${UPD["result",${num},"message","left_chat_member","is_bot"]}" + MESSAGE[0]="/left_chat_member ${LEFTMEMBER[USERNAME]:=${LEFTMEMBER[FIRST_NAME]} ${LEFTMEMBER[LAST_NAME]}}" + fi SERVICE[NEWTILE]="${UPD["result",${num},"message","new_chat_title"]}" SERVICE[NEWPHOTO]="${UPD["result",${num},"message","new_chat_photo"]}" SERVICE[PINNED]="${UPD["result",${num},"message","pinned_message"]}" diff --git a/doc/2_usage.md b/doc/2_usage.md index 7b099c8..0bba689 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -155,11 +155,16 @@ Evertime a Message is received, you can read incoming data using the following v * ```${SERVICE}```: set to "yes" when a service message is recived. * ```${SERVICE[NEWMEMBER]```: New user's id * ```${NEWMEMBER[ID]```: New user's id - * ```${NEWMEMBER[FIRSTNAME]```: New user's first name - * ```${NEWMEMBER[LASTNAME]```: New user's last name + * ```${NEWMEMBER[FIRST_NAME]```: New user's first name + * ```${NEWMEMBER[LAST_NAME]```: New user's last name * ```${NEWMEMBER[USERNAME]```: New user's username * ```${NEWMEMBER[ISBOT]```: New user is a bot * ```${SERVICE[LEFTMEMBER]```: Id of user left + * ```${LEFTMEBER[ID]```: New user's id + * ```${LEFTMEBER[FIRST_NAME]```: New user's first name + * ```${LEFTMEBER[LAST_NAME]```: New user's last name + * ```${LEFTMEBER[USERNAME]```: New user's username + * ```${LEFTMEBER[ISBOT]```: New user is a bot * ```${SERVICE[NEWTILE]```: Text of new title * ```${SERVICE[NEWPHOTO]```: New Chat Picture array * ```${SERVICE[PINNED]```: Pinned Message structure @@ -256,5 +261,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-7-gca05095 From e0b9f6f8701db504373d7c9ef67ce1d8181f075c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 21:39:52 +0200 Subject: [PATCH 09/76] create setconfigKey, clean botconfig on update startup --- bashbot.sh | 28 ++++++++++++++++++---------- modules/chatMember.sh | 6 +++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index c168e43..4eae041 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-7-gca05095 +#### $$VERSION$$ v0.98-dev-8-gcdc6dd3 # # Exit Codes: # - 0 sucess (hopefully) @@ -53,6 +53,10 @@ _round_float() { local digit="${2}"; [[ "${2}" =~ ^[0-9]+$ ]] || digit="0" LC_ALL=C printf "%.${digit}f" "${1}" } +setConfigKey() { + [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" +} getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 @@ -121,7 +125,7 @@ if [ -z "${BOTTOKEN}" ]; then if [ -z "$(getConfigKey "bottoken")" ]; then # convert old token if [ -r "${TOKENFILE}" ]; then - printf '["bottoken"]\t"%s"\n' "$(< "${TOKENFILE}")" >>"${BOTDATABASE}.jssh" + token="$(< "${TOKENFILE}")" # no old token avalible ask user elif [ -z "${CLEAR}" ] && [ "$1" != "init" ]; then echo "Running headless, set BOTTOKEN or run ${SCRIPT} init first!" @@ -130,25 +134,27 @@ if [ -z "${BOTTOKEN}" ]; then ${CLEAR} echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" - read -r BOTTOKEN - printf '["bottoken"]\t"%s"\n' "${BOTTOKEN}" >> "${BOTDATABASE}.jssh" + read -r token fi + [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTDATABASE}.jssh" fi # setup botadmin file - if [ ! -f "${BOTADMIN}" ]; then - if [ -z "${CLEAR}" ]; then + if [ -z "$(getConfigKey "botadmin")" ]; then + # convert old token + if [ -r "${BOTADMIN}" ]; then + admin="$(< "${BOTADMIN}")" + elif [ -z "${CLEAR}" ]; then echo "Running headless, set botadmin to AUTO MODE!" - printf '%s\n' '?' > "${BOTADMIN}" else ${CLEAR} echo -e "${RED}BOTADMIN MISSING.${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}" read -r admin - [ -z "${admin}" ] && admin='?' - printf '%s\n' "${admin}" > "${BOTADMIN}" fi + [ -z "${admin}" ] && admin='?' + printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTDATABASE}.jssh" fi # setup botacl file if [ ! -f "${BOTACL}" ]; then @@ -825,6 +831,8 @@ start_bot() { # cleanup countfile on startup jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" + jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTDATABASE}" + [ -f "${BOTDATABASE}.jssh.flock" ] && rm -f "${BOTDATABASE}.jssh.flock" ########## # bot is ready, start processing updates ... @@ -998,7 +1006,7 @@ if [ "${SOURCE}" != "yes" ]; then [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue (( USERS++ )) if [ -n "$*" ]; then - send_markdown_message "${MSG}" "$*" + send_message "${MSG}" "$*" echo -e ".\c" sleep 0.1 fi diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 0915133..3335518 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-8-gcdc6dd3 # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" @@ -50,11 +50,11 @@ user_is_admin() { } user_is_botadmin() { - local admin; admin="$(head -n 1 "${BOTADMIN}")" + local admin; admin="$(getConfigKey "botadmin")" [ "${admin}" = "${1}" ] && return 0 [ "${admin}" = "${2}" ] && return 0 [[ "${admin}" = "@*" ]] && [[ "${admin}" = "${2}" ]] && return 0 - if [ "${admin}" = "?" ]; then printf '%s\n' "${1:-?}" >"${BOTADMIN}"; return 0; fi + if [ "${admin}" = "?" ]; then setConfigKey "${1:-?}"; return 0; fi return 1 } From 74caa49ab106fbb15f5584ebb74bd7a42cc247ad Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 09:10:43 +0200 Subject: [PATCH 10/76] startup message, optimize file_id --- bashbot.sh | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 4eae041..a8e410c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-8-gcdc6dd3 +#### $$VERSION$$ v0.98-dev-9-ge0b9f6f # # Exit Codes: # - 0 sucess (hopefully) @@ -687,7 +687,7 @@ process_message() { CHAT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","chat","first_name"]}")" CHAT[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","chat","username"]}")" # set real name as username if empty - : "${CHAT[USERNAME]:=${CHAT[FIRST_NAME]} ${CHAT[LAST_NAME]}}" + [ -z "${CHAT[USERNAME]}" ] && CHAT[USERNAME]="${CHAT[FIRST_NAME]} ${CHAT[LAST_NAME]}" CHAT[TITLE]="$(JsonDecode "${UPD["result",${num},"message","chat","title"]}")" CHAT[TYPE]="$(JsonDecode "${UPD["result",${num},"message","chat","type"]}")" CHAT[ALL_ADMIN]="${UPD["result",${num},"message","chat","all_members_are_administrators"]}" @@ -719,19 +719,15 @@ process_message() { FORWARD[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","username"]}")" fi - # Audio - URLS[AUDIO]="$(get_file "${UPD["result",${num},"message","audio","file_id"]}")" - # Document - URLS[DOCUMENT]="$(get_file "${UPD["result",${num},"message","document","file_id"]}")" - # Photo - URLS[PHOTO]="$(get_file "${UPD["result",${num},"message","photo",0,"file_id"]}")" - # Sticker - URLS[STICKER]="$(get_file "${UPD["result",${num},"message","sticker","file_id"]}")" - # Video - URLS[VIDEO]="$(get_file "${UPD["result",${num},"message","video","file_id"]}")" - # Voice - URLS[VOICE]="$(get_file "${UPD["result",${num},"message","voice","file_id"]}")" - + # get file URL from telegram + if grep -qs -e '\["result",'"${num}"',"message",".*,"file_id"\]' <<<"${UPDATE}"; then + URLS[AUDIO]="$(get_file "${UPD["result",${num},"message","audio","file_id"]}")" + URLS[DOCUMENT]="$(get_file "${UPD["result",${num},"message","document","file_id"]}")" + URLS[PHOTO]="$(get_file "${UPD["result",${num},"message","photo",0,"file_id"]}")" + URLS[STICKER]="$(get_file "${UPD["result",${num},"message","sticker","file_id"]}")" + URLS[VIDEO]="$(get_file "${UPD["result",${num},"message","video","file_id"]}")" + URLS[VOICE]="$(get_file "${UPD["result",${num},"message","voice","file_id"]}")" + fi # Contact CONTACT=( ) CONTACT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","first_name"]}")" @@ -800,16 +796,18 @@ process_message() { ######################### # main get updates loop, should never terminate start_bot() { - local DEBUG="$1" - local OFFSET=0 + local DEBUG OFFSET=0 # adaptive sleep deafults local nextsleep="100" : local stepsleep="${BASHBOT_SLEEP_STEP:-100}" local maxsleep="${BASHBOT_SLEEP:-5000}" + # startup message + DEBUG="$(date):Start BASHBOT updates in Mode \"${1:-normal}\" ==========" + printf "%s\n" "${DEBUG}" >>"${UPDATELOG}" + printf "%s\n" "${DEBUG}"; DEBUG="${1}" # redirect to Debug.log - printf "%s: Start BASHBOT updates in Mode \"%s\" ==========\n" "$(date)" "${DEBUG}" >>"${DEBUGLOG}" [[ "${DEBUG}" == *"debug" ]] && exec &>>"${DEBUGLOG}" - [[ "${DEBUG}" == "xdebug"* ]] && set -x + [[ "${DEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles find "${DATADIR}" -type p -delete find "${DATADIR}" -size 0 -name "*.log" -delete From 656389182f069520c0670b109a4d07cfb7f0720c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 13:50:44 +0200 Subject: [PATCH 11/76] doc log files --- README.html | 64 +++++++++++++++++++++++++++++--------------------- README.md | 18 +++++++++++++- README.txt | 21 ++++++++++++++++- doc/2_usage.md | 4 ++-- 4 files changed, 76 insertions(+), 31 deletions(-) diff --git a/README.html b/README.html index 502abca..1afdc83 100644 --- a/README.html +++ b/README.html @@ -175,6 +175,16 @@ his is bashbot, the Telegram bot written entirely in bash. It features background tasks and interactive chats, and can serve as an interface for CLI programs.

For more Information on how to install, customize and use your new bot, read the Documentation

+

Log files

+

Since version 0.96 bashbot log commands recieved (updates) and connection errors. If you start bashbot in debug mode bash stdout, stderr and all recieved telegram message updates are logged also.

+

To enable debug mode start bashbot with debug as third argument: bashbot start debug

+
├── logs 
+│   ├── ERROR.log        # connection errors from / to telegram API
+│   ├── UPDATE.log       # summary whats received from your bot
+│   │
+│   ├── DEBUG.log        # stdout/stderr of you bot (debug mode enabled)
+│   └── MESSAGE.log      # full text of all message updates received (debug mode enabled)
+

Security Considerations

Running a Telegram Bot means it is connected to the public and you never know whats send to your Bot.

@@ -184,18 +194,18 @@ It features background tasks and interactive chats, and can serve as an interfac

A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

use printf whenever possible

If you're writing a script and it is taking external input (from the user as arguments, or file names from the file system...), you shouldn't use echo to display it. Use printf whenever possible

- +

Do not use #!/usr/bin/env bash

We stay with /bin/bash shebang, because it's more save from security perspective.

Using a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.

@@ -225,27 +235,27 @@ It features background tasks and interactive chats, and can serve as an interfac

Can I send messages from CLI and scripts?

Of course, you can send messages from CLI and scripts, simply install bashbot as described here, send the messsage '/start' to set yourself as botadmin and stop the bot with ./bashbot.sh kill.

Run the following commands in your bash shell or script while you are in the installation directory:

- +

For more information see Expert Use

Why do I get "EXPECTED value GOT EOF" on start?

May be your IP is blocked by telegram. You can test this by running curl or wget manually:

- +

This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a socks or tor proxy on your server look for the BASHBOT_CURL_ARGS lines in 'mycommands.sh' as example.

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ v0.96-0-g3871ca9

+

$$VERSION$$ v0.98-dev-10-g74caa49

diff --git a/README.md b/README.md index 207d974..2ba8736 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,22 @@ It features background tasks and interactive chats, and can serve as an interfac ``` For more Information on how to install, customize and use your new bot, read the [Documentation](#Documentation) +### Log files + +Since version 0.96 bashbot log commands recieved (updates) and connection errors. If you start bashbot in debug mode +bash stdout, stderr and all recieved telegram message updates are logged also. + +To enable debug mode start bashbot with debug as third argument: `bashbot start debug` + +``` +├── logs +│   ├── ERROR.log # connection errors from / to telegram API +│   ├── UPDATE.log # summary whats received from your bot +│   │ +│   ├── DEBUG.log # stdout/stderr of you bot (debug mode enabled) +│   └── MESSAGE.log # full text of all message updates received (debug mode enabled) +``` + ---- ## Security Considerations @@ -219,4 +235,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-10-g74caa49 diff --git a/README.txt b/README.txt index b5f2360..0ac45cc 100644 --- a/README.txt +++ b/README.txt @@ -131,6 +131,25 @@ interface for CLI programs. For more Information on how to install, customize and use your new bot, read the [Documentation](#Documentation) +### Log files + +Since version 0.96 bashbot log commands recieved (updates) and connection +errors. If you start bashbot in debug mode +bash stdout, stderr and all recieved telegram message updates are logged also. + +To enable debug mode start bashbot with debug as third argument: `bashbot start +debug` + +``` +├── logs +│   ├── ERROR.log # connection errors from / to telegram API +│   ├── UPDATE.log # summary whats received from your bot +│   │ +│   ├── DEBUG.log # stdout/stderr of you bot (debug mode enabled) +│   └── MESSAGE.log # full text of all message updates received (debug +mode enabled) +``` + ---- ## Security Considerations @@ -305,4 +324,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-10-g74caa49 diff --git a/doc/2_usage.md b/doc/2_usage.md index 0bba689..e8fdb9d 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -32,7 +32,7 @@ Have FUN! ├── scripts # place your bashbot interactive and background scripts here │   └── interactive.sh.clean # interactive script template for new scripts │ -├── logs # here you'll find ERROR, DEBUG and MESSAGE.log +├── logs # here you'll find ERROR, UPDSTE, DEBUG and MESSAGE.log │ ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off @@ -261,5 +261,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-7-gca05095 +#### $$VERSION$$ v0.98-dev-10-g74caa49 From de811c173a3fd6fafa9864405b610e2a1ceb0b29 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 13:57:36 +0200 Subject: [PATCH 12/76] fix order of debug start message --- bashbot.sh | 6 +++--- doc/2_usage.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index a8e410c..227f99a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-9-ge0b9f6f +#### $$VERSION$$ v0.98-dev-11-g6563891 # # Exit Codes: # - 0 sucess (hopefully) @@ -804,9 +804,9 @@ start_bot() { # startup message DEBUG="$(date):Start BASHBOT updates in Mode \"${1:-normal}\" ==========" printf "%s\n" "${DEBUG}" >>"${UPDATELOG}" - printf "%s\n" "${DEBUG}"; DEBUG="${1}" # redirect to Debug.log - [[ "${DEBUG}" == *"debug" ]] && exec &>>"${DEBUGLOG}" + [[ "${1}" == *"debug" ]] && exec &>>"${DEBUGLOG}" + printf "%s\n" "${DEBUG}"; DEBUG="${1}" [[ "${DEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles find "${DATADIR}" -type p -delete diff --git a/doc/2_usage.md b/doc/2_usage.md index e8fdb9d..11b3e10 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -32,7 +32,7 @@ Have FUN! ├── scripts # place your bashbot interactive and background scripts here │   └── interactive.sh.clean # interactive script template for new scripts │ -├── logs # here you'll find ERROR, UPDSTE, DEBUG and MESSAGE.log +├── logs # here you'll find ERROR, UPDATE, DEBUG and MESSAGE.log │ ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off @@ -261,5 +261,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-10-g74caa49 +#### $$VERSION$$ v0.98-dev-11-g6563891 From 2281943940fb7b0b594b73991c0886ce4de19040 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 17:00:41 +0200 Subject: [PATCH 13/76] optimze countKeyDB_async --- modules/jsonDB.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 15e64af..941d838 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-6-ge0470aa +#### $$VERSION$$ v0.98-dev-12-gde811c1 # # source from commands.sh to use jsonDB functions # @@ -143,7 +143,7 @@ if _exists flock; then # start atomic delete here, exclusive max wait 5 { flock -e -w 5 200 Json2Array "oldARR" <"${DB}" - if [ "$3" != "" ]; then + if [ -n "$3" ]; then (( oldARR["$1"]+="$3" )); Array2Json "oldARR" >"${DB}" else @@ -291,14 +291,19 @@ jssh_getKeyDB_async() { jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB COUNT="1"; DB="$(jssh_checkDB "$2")" - [ "$3" != "" ] && COUNT="$3" + local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR - # start atomic delete here, exclusive max wait 10s + # start atomic delete here, exclusive max wait 5 Json2Array "oldARR" <"${DB}" - (( oldARR["$1"]+=COUNT )); - Array2Json "oldARR" >"${DB}" -} + if [ -n "$3" ]; then + (( oldARR["$1"]+="$3" )); + Array2Json "oldARR" >"${DB}" + else + # it's append, but last one counts, its a simple DB ... + (( oldARR["$1"]++ )); + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + fi + } # updatie key/value in place to jsshDB # $1 key name, can onyl contain -a-zA-Z0-9,._ From cc69310a7e759ef05ce734194019d1452dc6fff1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 17:39:18 +0200 Subject: [PATCH 14/76] countKey optimze fast path --- modules/jsonDB.sh | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 941d838..7b505a7 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-12-gde811c1 +#### $$VERSION$$ v0.98-dev-13-g2281943 # # source from commands.sh to use jsonDB functions # @@ -138,18 +138,18 @@ if _exists flock; then # side effect: if $3 is not given, we add to end of file to be as fast as possible jssh_countKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR + local VAL DB; DB="$(jssh_checkDB "$2")" # start atomic delete here, exclusive max wait 5 { flock -e -w 5 200 - Json2Array "oldARR" <"${DB}" if [ -n "$3" ]; then + declare -A oldARR + Json2Array "oldARR" <"${DB}" (( oldARR["$1"]+="$3" )); Array2Json "oldARR" >"${DB}" - else + elif [ -r "${DB}" ]; then # it's append, but last one counts, its a simple DB ... - (( oldARR["$1"]++ )); - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + VAL="$(sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1)" + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "$((++VAL))" >>"${DB}" fi } 200>"${DB}${BASHBOT_LOCKNAME}" } @@ -291,17 +291,17 @@ jssh_getKeyDB_async() { jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR + local VAL DB; DB="$(jssh_checkDB "$2")" # start atomic delete here, exclusive max wait 5 - Json2Array "oldARR" <"${DB}" if [ -n "$3" ]; then + declare -A oldARR + Json2Array "oldARR" <"${DB}" (( oldARR["$1"]+="$3" )); Array2Json "oldARR" >"${DB}" - else + elif [ -r "${DB}" ]; then # it's append, but last one counts, its a simple DB ... - (( oldARR["$1"]++ )); - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + VAL="$(sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1)" + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "$((++VAL))" >>"${DB}" fi } From bc443311834d28e375927aaf301f6c5c71250f6d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 18:22:11 +0200 Subject: [PATCH 15/76] allow block chats also, optimize blockfile reading --- bashbot.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 227f99a..d4449de 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-11-g6563891 +#### $$VERSION$$ v0.98-dev-14-gcc69310 # # Exit Codes: # - 0 sucess (hopefully) @@ -515,8 +515,8 @@ process_client() { [[ -n "${debug}" ]] && printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log" # check for uers / groups to ignore - [ -n "${USER[ID]}" ] && [[ " ${!BASHBOT_BLOCKED[*]} " == *" ${USER[ID]} "* ]] && return - jssh_readDB_async "BASHBOT_BLOCKED" "${BLOCKEDFILE}" + [[ -n "$(jssh_getKeyDB_async "${USER[ID]}" "${BLOCKEDFILE}")" || + -n "$(jssh_getKeyDB_async "${CHAT[ID]}" "${BLOCKEDFILE}")" ]] && return # process per message type if [ -z "${iQUERY[ID]}" ]; then @@ -802,7 +802,7 @@ start_bot() { local stepsleep="${BASHBOT_SLEEP_STEP:-100}" local maxsleep="${BASHBOT_SLEEP:-5000}" # startup message - DEBUG="$(date):Start BASHBOT updates in Mode \"${1:-normal}\" ==========" + DEBUG="$(date): Start BASHBOT updates in Mode \"${1:-normal}\" ==========" printf "%s\n" "${DEBUG}" >>"${UPDATELOG}" # redirect to Debug.log [[ "${1}" == *"debug" ]] && exec &>>"${DEBUGLOG}" @@ -1018,7 +1018,7 @@ if [ "${SOURCE}" != "yes" ]; then echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}" exit else - echo -e "${ORANGE}Bot not running with UID ${RUNUSER}.${NC}" + echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" exit 5 fi ;; @@ -1049,7 +1049,7 @@ if [ "${SOURCE}" != "yes" ]; then exit 5 fi else - echo -e "${ORANGE}Bot not running with UID ${RUNUSER}.${NC}" + echo -e "${ORANGE}No Bot running with UID ${RUNUSER}.${NC}" fi exit ;; From 2af9d76ea7bbf24c4f3315cbfba31bc860855436 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 20:36:14 +0200 Subject: [PATCH 16/76] jssh_updateArray update array only if file has changed --- modules/jsonDB.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 7b505a7..6696d45 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-13-g2281943 +#### $$VERSION$$ v0.98-dev-15-gbc44331 # # source from commands.sh to use jsonDB functions # @@ -212,7 +212,7 @@ jssh_newDB() { # $1 filename, check filename, it must be relative to BASHBOT_VAR, and not contain '..' # returns real path to DB file if everything is ok -jssh_checkDB_sync() { jssh_checkDB "$@"; } +jssh_checkDB_async() { jssh_checkDB "$@"; } jssh_checkDB(){ local DB [ -z "$1" ] && return 1 @@ -225,6 +225,18 @@ jssh_checkDB(){ printf '%s' "${DB}" } +# updates Array if DB file has changed since last call +# $1 name of array to update +# $2 database +# $3 id used to identify caller +jssh_updateArray_asyn() { jssh_updateArray "$@"; } +function jssh_updateArray() { + local DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 + [ "${DB}" -nt "${}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB "${1}" "${2}" +} + ###################### # implementations as non atomic functions From dda5b6d10ef244b0052163303fb9e72fa2872efe Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 20:44:11 +0200 Subject: [PATCH 17/76] make jsonDB.sh independent from bashbot --- modules/jsonDB.sh | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 6696d45..315bd7e 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-15-gbc44331 +#### $$VERSION$$ v0.98-dev-16-g2af9d76 # # source from commands.sh to use jsonDB functions # @@ -22,9 +22,11 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" # tinybox # lockfile filename.flock is persistent and will be testet with flock for active lock (file open) -export BASHBOT_LOCKNAME=".flock" +export JSSH_LOCKNAME=".flock" + +# use flock if command exist +if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then -if _exists flock; then ############### # we have flock # use flock for atomic operations @@ -37,7 +39,7 @@ if _exists flock; then [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # shared lock, many processes can read, max wait 1s - { flock -s -w 1 200; Json2Array "$1" <"${DB}"; } 200>"${DB}${BASHBOT_LOCKNAME}" + { flock -s -w 1 200; Json2Array "$1" <"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" } # write ARRAY content to a file in JSON.sh format @@ -49,7 +51,7 @@ if _exists flock; then [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # exclusive lock, no other process can read or write, maximum wait to get lock is 10s - { flock -e -w 10 200; Array2Json "$1" >"${DB}"; } 200>"${DB}${BASHBOT_LOCKNAME}" + { flock -e -w 10 200; Array2Json "$1" >"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" } # update/write ARRAY content in file without deleting keys not in ARRAY @@ -79,7 +81,7 @@ if _exists flock; then done Array2Json "oldARR" >"${DB}" fi - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } # insert, update, apped key/value to jsshDB @@ -98,7 +100,7 @@ if _exists flock; then { flock -e -w 2 200 # it's append, but last one counts, its a simple DB ... printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } @@ -114,7 +116,7 @@ if _exists flock; then Json2Array "oldARR" <"${DB}" unset oldARR["$1"] Array2Json "oldARR" >"${DB}" - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } # get key/value from jsshDB @@ -127,7 +129,7 @@ if _exists flock; then # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } @@ -151,7 +153,7 @@ if _exists flock; then VAL="$(sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1)" printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "$((++VAL))" >>"${DB}" fi - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } # update key/value in place to jsshDB @@ -173,7 +175,7 @@ if _exists flock; then [ -z "${DB}" ] && return 1 { flock -e -w 10 200 printf '' >"${DB}" - } 200>"${DB}${BASHBOT_LOCKNAME}" + } 200>"${DB}${JSSH_LOCKNAME}" } else From 852ab9de16909a304b46dc95d058408c5ae3222f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 20:56:46 +0200 Subject: [PATCH 18/76] adjust modules coment --- modules/aliases.sh | 4 ++-- modules/answerInline.sh | 4 ++-- modules/background.sh | 4 ++-- modules/chatMember.sh | 6 +++--- modules/jsonDB.sh | 5 ++++- modules/sendMessage.sh | 4 +++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/aliases.sh b/modules/aliases.sh index d0795b3..19d8702 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -5,9 +5,9 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d # -# source from commands.sh to use the aliases +# will be automatically sourced from bashbot # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/answerInline.sh b/modules/answerInline.sh index 8a20057..e4f6ec9 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.sh @@ -5,9 +5,9 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d -# source from commands.sh to use the inline functions +# will be automatically sourced from bashbot # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/background.sh b/modules/background.sh index 622d114..7bb6a5e 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,9 +5,9 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d -# source from commands.sh if you want ro use interactive or background jobs +# will be automatically sourced from bashbot # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 3335518..789457a 100644 --- a/modules/chatMember.sh +++ b/modules/chatMember.sh @@ -5,13 +5,13 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-8-gcdc6dd3 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d + +# will be automatically sourced from bashbot # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" -# source from commands.sh to use the member functions - LEAVE_URL=$URL'/leaveChat' KICK_URL=$URL'/kickChatMember' UNBAN_URL=$URL'/unbanChatMember' diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 315bd7e..0df7f5b 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,13 +5,16 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-16-g2af9d76 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d # # source from commands.sh to use jsonDB functions # # jsonDB provides simple functions to read and store bash Arrays # from to file in JSON.sh output format, its a simple key/value storage. +# will be automatically sourced from bashbot +# but can be used independed from bashbot also +# e.g. to create scrupts to manage jssh files # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 8474d87..a506c26 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,9 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-17-gdda5b6d + +# will be automatically sourced from bashbot # source once magic, function named like file eval "$(basename "${BASH_SOURCE[0]}")(){ :; }" From 4e043c5717df657f70fb36a0188960264044b72f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 14 Jun 2020 21:26:45 +0200 Subject: [PATCH 19/76] fix updateArray, new flock use method --- modules/jsonDB.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 0df7f5b..ccb489a 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-17-gdda5b6d +#### $$VERSION$$ v0.98-dev-18-g852ab9d # # source from commands.sh to use jsonDB functions # @@ -234,12 +234,15 @@ jssh_checkDB(){ # $1 name of array to update # $2 database # $3 id used to identify caller -jssh_updateArray_asyn() { jssh_updateArray "$@"; } -function jssh_updateArray() { +jssh_updateArray() { + local DB; DB="$(jssh_checkDB "$2")" + { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" +} +function jssh_updateArray_async() { local DB; DB="$(jssh_checkDB "$2")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 - [ "${DB}" -nt "${}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB "${1}" "${2}" + [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" } From 3e4e90443c0fb1f25746926aa5761fef8d3f73cc Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 08:39:52 +0200 Subject: [PATCH 20/76] inform admin about successful start --- bashbot.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d4449de..96e4ef2 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-14-gcc69310 +#### $$VERSION$$ v0.98-dev-19-g4e043c5 # # Exit Codes: # - 0 sucess (hopefully) @@ -796,7 +796,7 @@ process_message() { ######################### # main get updates loop, should never terminate start_bot() { - local DEBUG OFFSET=0 + local ADMIN DEBUG OFFSET=0 # adaptive sleep deafults local nextsleep="100" : local stepsleep="${BASHBOT_SLEEP_STEP:-100}" @@ -831,7 +831,9 @@ start_bot() { [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTDATABASE}" [ -f "${BOTDATABASE}.jssh.flock" ] && rm -f "${BOTDATABASE}.jssh.flock" - + # inform botadmin about start + ADMIN="$(getConfigKey "botadmin")" + [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." & ########## # bot is ready, start processing updates ... while true; do From 24a7eaa8ddbc81d238ef98465d30e24e145027e6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 09:09:08 +0200 Subject: [PATCH 21/76] cache botconfig, optimze updateArray --- bashbot.sh | 9 +++++++-- modules/jsonDB.sh | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 96e4ef2..2078e9d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-19-g4e043c5 +#### $$VERSION$$ v0.98-dev-20-g3e4e904 # # Exit Codes: # - 0 sucess (hopefully) @@ -41,7 +41,8 @@ _exists() { # execute function if exists _exec_if_function() { - [ "$(LC_ALL=C type -t "${1}")" != "function" ] || "$@" + [ "$(LC_ALL=C type -t "${1}")" != "function" ] && return 1 + "$@" } # returns true if function exist _is_function() { @@ -57,11 +58,15 @@ setConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" } +declare -A BASHBOTCONFIG getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + # cache if aupdateArray exists, else read from file + _exec_if_function jssh_updateArray_async "BASHBOTCONFIG" "${BOTDATABASE}" && printf '%s' "${BASHBOTCONFIG[${1}]}" && return [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } + # get location and name of bashbot.sh SCRIPT="$0" REALME="${BASH_SOURCE[0]}" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index ccb489a..e8e3b57 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-18-g852ab9d +#### $$VERSION$$ v0.98-dev-20-g3e4e904 # # source from commands.sh to use jsonDB functions # @@ -235,7 +235,9 @@ jssh_checkDB(){ # $2 database # $3 id used to identify caller jssh_updateArray() { - local DB; DB="$(jssh_checkDB "$2")" + local DB="${2}.jssh" # name check in async + [ -z "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } function jssh_updateArray_async() { From c94328003468a890a8c267ab973ef4c7206001c8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 10:42:36 +0200 Subject: [PATCH 22/76] cache blocked users with updateArray --- bashbot.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 2078e9d..3de69e7 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-20-g3e4e904 +#### $$VERSION$$ v0.98-dev-21-g24a7eaa # # Exit Codes: # - 0 sucess (hopefully) @@ -520,8 +520,8 @@ process_client() { [[ -n "${debug}" ]] && printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log" # check for uers / groups to ignore - [[ -n "$(jssh_getKeyDB_async "${USER[ID]}" "${BLOCKEDFILE}")" || - -n "$(jssh_getKeyDB_async "${CHAT[ID]}" "${BLOCKEDFILE}")" ]] && return + jssh_updateArray_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" + [ -n "${USER[ID]}" ] && [[ -n "${BASHBOTBLOCKED[${USER[ID]}]}" || -n "${BASHBOTBLOCKED[${CHAT[ID]}]}" ]] && return # process per message type if [ -z "${iQUERY[ID]}" ]; then @@ -800,6 +800,7 @@ process_message() { ######################### # main get updates loop, should never terminate +declare -A BASHBOTBLOCKED start_bot() { local ADMIN DEBUG OFFSET=0 # adaptive sleep deafults @@ -836,6 +837,7 @@ start_bot() { [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTDATABASE}" [ -f "${BOTDATABASE}.jssh.flock" ] && rm -f "${BOTDATABASE}.jssh.flock" + jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start ADMIN="$(getConfigKey "botadmin")" [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." & From 6439d435c256774b52ff95cf0b511a018811f9f0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 11:23:43 +0200 Subject: [PATCH 23/76] jssh no more double implementation of complex functions --- modules/jsonDB.sh | 109 +++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 68 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index e8e3b57..3edfc59 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-20-g3e4e904 +#### $$VERSION$$ v0.98-dev-22-gc943280 # # source from commands.sh to use jsonDB functions # @@ -60,31 +60,13 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then # update/write ARRAY content in file without deleting keys not in ARRAY # $1 ARRAY name, must be delared with "declare -A ARRAY" upfront # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + # complex slow, warpper async jssh_updateDB() { # for atomic update we cant use read/writeDB - local DB; DB="$(jssh_checkDB "$2")" - [ -z "${DB}" ] && return 1 + [ -z "${2}" ] && return 1 + local DB="${2}.jssh" # check in async [ ! -f "${DB}" ] && return 2 - declare -n ARRAY="$1" - [ -z "${ARRAY[*]}" ] && return 1 - declare -A oldARR - - # start atomic update here, exclusive max wait 10s - { flock -e -w 10 200 - Json2Array "oldARR" <"${DB}" - if [ -z "${oldARR[*]}" ]; then - # no old content - Array2Json "$1" >"${DB}" - else - # merge arrays - local key - for key in "${!ARRAY[@]}" - do - oldARR["${key}"]="${ARRAY["${key}"]}" - done - Array2Json "oldARR" >"${DB}" - fi - } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; jssh_updateDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } # insert, update, apped key/value to jsshDB @@ -95,14 +77,13 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then # renamed to be more consistent jssh_insertKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local key="$1" value="$2" local DB; DB="$(jssh_checkDB "$3")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # start atomic update here, exclusive max wait 2, it's append, not overwrite { flock -e -w 2 200 # it's append, but last one counts, its a simple DB ... - printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${DB}" } 200>"${DB}${JSSH_LOCKNAME}" } @@ -110,16 +91,13 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then # delete key/value from jsshDB # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' + # medium complex slow, wrapper async jssh_deleteKeyDB() { + [ -z "${2}" ] && return 1 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR + local DB="${2}.jssh" # start atomic delete here, exclusive max wait 10s - { flock -e -w 10 200 - Json2Array "oldARR" <"${DB}" - unset oldARR["$1"] - Array2Json "oldARR" >"${DB}" - } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; jssh_deleteKey_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } # get key/value from jsshDB @@ -141,22 +119,13 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $3 optional count, value added to counter, add 1 if empty # side effect: if $3 is not given, we add to end of file to be as fast as possible + # complex, wrapper to async jssh_countKeyDB() { + [ -z "${2}" ] && return 1 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local VAL DB; DB="$(jssh_checkDB "$2")" + local DB="${2}.jssh" # start atomic delete here, exclusive max wait 5 - { flock -e -w 5 200 - if [ -n "$3" ]; then - declare -A oldARR - Json2Array "oldARR" <"${DB}" - (( oldARR["$1"]+="$3" )); - Array2Json "oldARR" >"${DB}" - elif [ -r "${DB}" ]; then - # it's append, but last one counts, its a simple DB ... - VAL="$(sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1)" - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "$((++VAL))" >>"${DB}" - fi - } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 5 200; jssh_countKeyDB "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } # update key/value in place to jsshDB @@ -176,14 +145,24 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then jssh_clearDB() { local DB; DB="$(jssh_checkDB "$1")" [ -z "${DB}" ] && return 1 - { flock -e -w 10 200 - printf '' >"${DB}" - } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; printf '' >"${DB}"; } 200>"${DB}${JSSH_LOCKNAME}" } + # updates Array if DB file has changed since last call + # $1 name of array to update + # $2 database + # $3 id used to identify caller + # medium comlex, wrapper async + jssh_updateArray() { + [ -z "${2}" ] && return 1 + local DB="${2}.jssh" # name check in async + [ ! -f "${DB}" ] && return 2 + { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + } + else ######### - # we have no flock, use "old" not atomic functions + # we have no flock, use non atomic functions alias jssh_readDB=ssh_readDB_async alias jssh_writeDB=jssh_writeDB_async alias jssh_updateDB=jssh_updateDB_async @@ -194,6 +173,7 @@ else alias jssh_countKeyDB=jssh_countKeyDB_async alias jssh_updateKeyDB=jssh_updateKeyDB_async alias jssh_clearDB=jssh_clearDB_async + alias jssh_updateArray=updateArray_async fi ############## @@ -230,23 +210,6 @@ jssh_checkDB(){ printf '%s' "${DB}" } -# updates Array if DB file has changed since last call -# $1 name of array to update -# $2 database -# $3 id used to identify caller -jssh_updateArray() { - local DB="${2}.jssh" # name check in async - [ -z "${DB}" ] && return 1 - [ ! -f "${DB}" ] && return 2 - { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" -} -function jssh_updateArray_async() { - local DB; DB="$(jssh_checkDB "$2")" - [ -z "${DB}" ] && return 1 - [ ! -f "${DB}" ] && return 2 - [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" -} - ###################### # implementations as non atomic functions @@ -287,12 +250,11 @@ jssh_updateDB_async() { jssh_insertDB_async() { jssh_insertKeyDB "$@"; } jssh_insertKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local key="$1" value="$2" local DB; DB="$(jssh_checkDB "$3")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 # its append, but last one counts, its a simple DB ... - printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${value//\"/\\\"}" >>"${DB}" + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${DB}" } @@ -346,6 +308,17 @@ jssh_clearDB_async() { printf '' >"${DB}" } +function jssh_updateArray_async() { + local DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 + [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" +} + +############## +# these 2 fuctions does all key/value store "magic" +# and convert from/to bash array + # read JSON.sh style data and asssign to an ARRAY # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling Json2Array() { From 4990f70bceacdb75e646496873106ff004d5d903 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 14:16:14 +0200 Subject: [PATCH 24/76] escape tab in JSON send to telegram --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 3de69e7..1092e5a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-21-g24a7eaa +#### $$VERSION$$ v0.98-dev-23-g6439d43 # # Exit Codes: # - 0 sucess (hopefully) @@ -456,7 +456,7 @@ sendJsonResult(){ # $1 string # output escaped string JsonEscape(){ - sed 's/\([-"`´,§$%&/(){}#@!?*.]\)/\\\1/g' <<< "$1" + sed 's/\([-"`´,§$%&/(){}#@!?*.\t]\)/\\\1/g' <<< "$1" } # convert common telegram entities to JSON From 4b1875777971c7e315be757e695ae4eb2b657ad5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 14:22:28 +0200 Subject: [PATCH 25/76] remove JsonEscape for markup type, not needed --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 1092e5a..97cf431 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-23-g6439d43 +#### $$VERSION$$ v0.98-dev-24-g4990f70 # # Exit Codes: # - 0 sucess (hopefully) @@ -466,7 +466,7 @@ title2Json(){ [ -n "$1" ] && title=',"title":"'$(JsonEscape "$1")'"' [ -n "$2" ] && caption=',"caption":"'$(JsonEscape "$2")'"' [ -n "$3" ] && desc=',"description":"'$(JsonEscape "$3")'"' - [ -n "$4" ] && markup=',"parse_mode":"'$(JsonEscape "$4")'"' + [ -n "$4" ] && markup=',"parse_mode":"'"$4"'"' [ -n "$5" ] && keyboard=',"reply_markup":"'$(JsonEscape "$5")'"' printf '%s\n' "${title}${caption}${desc}${markup}${keyboard}" } From 8991cc98f09cfd9b0fb68e8945c7b53c0c92725a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 19:44:46 +0200 Subject: [PATCH 26/76] optimize update processing, fix LEFTMEMBER --- bashbot.sh | 37 ++++++++++++++++++++----------------- doc/2_usage.md | 12 ++++++------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 97cf431..c16c0eb 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-24-g4990f70 +#### $$VERSION$$ v0.98-dev-25-g4b18757 # # Exit Codes: # - 0 sucess (hopefully) @@ -702,11 +702,13 @@ process_message() { USER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","first_name"]}")" USER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","from","last_name"]}")" USER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","from","username"]}")" + # set real name as username if empty + [ -z "${USER[USERNAME]}" ] && USER[USERNAME]="${USER[FIRST_NAME]} ${USER[LAST_NAME]}" # in reply to message from REPLYTO=( ) - REPLYTO[UID]="${UPD["result",${num},"message","reply_to_message","from","id"]}" - if [ -n "${REPLYTO[UID]}" ]; then + if grep -qs -e '\["result",'"${num}"',"message","reply_to_message"' <<<"${UPDATE}"; then + REPLYTO[UID]="${UPD["result",${num},"message","reply_to_message","from","id"]}" REPLYTO[0]="$(JsonDecode "${UPD["result",${num},"message","reply_to_message","text"]}")" 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"]}")" @@ -716,8 +718,8 @@ process_message() { # forwarded message from FORWARD=( ) - FORWARD[UID]="${UPD["result",${num},"message","forward_from","id"]}" - if [ -n "${FORWARD[UID]}" ]; then + if grep -qs -e '\["result",'"${num}"',"message","forward_from"' <<<"${UPDATE}"; then + FORWARD[UID]="${UPD["result",${num},"message","forward_from","id"]}" FORWARD[ID]="${MESSAGE[ID]}" # same as message ID FORWARD[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","first_name"]}")" FORWARD[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","forward_from","last_name"]}")" @@ -725,6 +727,7 @@ process_message() { fi # get file URL from telegram + URLS=() if grep -qs -e '\["result",'"${num}"',"message",".*,"file_id"\]' <<<"${UPDATE}"; then URLS[AUDIO]="$(get_file "${UPD["result",${num},"message","audio","file_id"]}")" URLS[DOCUMENT]="$(get_file "${UPD["result",${num},"message","document","file_id"]}")" @@ -735,8 +738,8 @@ process_message() { fi # Contact CONTACT=( ) - CONTACT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","first_name"]}")" - if [ -n "${CONTACT[FIRST_NAME]}" ]; then + if grep -qs -e '\["result",'"${num}"',"message","contact"' <<<"${UPDATE}"; then + CONTACT[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","first_name"]}")" CONTACT[USER_ID]="$(JsonDecode "${UPD["result",${num},"message","contact","user_id"]}")" CONTACT[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","contact","last_name"]}")" CONTACT[NUMBER]="${UPD["result",${num},"message","contact","phone_number"]}" @@ -745,8 +748,8 @@ process_message() { # vunue VENUE=( ) - VENUE[TITLE]="$(JsonDecode "${UPD["result",${num},"message","venue","title"]}")" - if [ -n "${VENUE[TITLE]}" ]; then + if grep -qs -e '\["result",'"${num}"',"message","contact"' <<<"${UPDATE}"; then + VENUE[TITLE]="$(JsonDecode "${UPD["result",${num},"message","venue","title"]}")" VENUE[ADDRESS]="$(JsonDecode "${UPD["result",${num},"message","venue","address"]}")" VENUE[LONGITUDE]="${UPD["result",${num},"message","venue","location","longitude"]}" VENUE[LATITUDE]="${UPD["result",${num},"message","venue","location","latitude"]}" @@ -762,8 +765,8 @@ process_message() { # service messages SERVICE=( ); NEWMEMBER=( ) - SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}" - if [ -n "${SERVICE[NEWMEMBER]}" ]; then + if grep -qs -e '\["result",'"${num}"',"message","new_chat_member' <<<"${UPDATE}"; then + SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}" NEWMEMBER[ID]="${SERVICE[NEWMEMBER]}" NEWMEMBER[FIRST_NAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}" NEWMEMBER[LAST_NAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}" @@ -771,13 +774,13 @@ process_message() { NEWMEMBER[ISBOT]="${UPD["result",${num},"message","new_chat_member","is_bot"]}" MESSAGE[0]="/new_chat_member ${NEWMEMBER[USERNAME]:=${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]}}" fi - SERVICE[LEFTMEMBER]="${UPD["result",${num},"message","left_chat_member","id"]}" - if [ -n "${SERVICE[LEFTMEMBER]}" ]; then - LEFTMEBER[ID]="${SERVICE[LEFTMEBER]}" - LEFTMEBER[FIRST_NAME]="${UPD["result",${num},"message","left_chat_member","first_name"]}" - LEFTMEBER[LAST_NAME]="${UPD["result",${num},"message","left_chat_member","last_name"]}" + if grep -qs -e '\["result",'"${num}"',"message","left_chat_member' <<<"${UPDATE}"; then + SERVICE[LEFTMEMBER]="${UPD["result",${num},"message","left_chat_member","id"]}" + LEFTMEMBER[ID]="${SERVICE[LEFTMEBER]}" + LEFTMEMBER[FIRST_NAME]="${UPD["result",${num},"message","left_chat_member","first_name"]}" + LEFTMEMBER[LAST_NAME]="${UPD["result",${num},"message","left_chat_member","last_name"]}" LEFTMEBER[USERNAME]="${UPD["result",${num},"message","left_chat_member","username"]}" - LEFTMEBER[ISBOT]="${UPD["result",${num},"message","left_chat_member","is_bot"]}" + LEFTMEMBER[ISBOT]="${UPD["result",${num},"message","left_chat_member","is_bot"]}" MESSAGE[0]="/left_chat_member ${LEFTMEMBER[USERNAME]:=${LEFTMEMBER[FIRST_NAME]} ${LEFTMEMBER[LAST_NAME]}}" fi SERVICE[NEWTILE]="${UPD["result",${num},"message","new_chat_title"]}" diff --git a/doc/2_usage.md b/doc/2_usage.md index 11b3e10..cac8f84 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -160,11 +160,11 @@ Evertime a Message is received, you can read incoming data using the following v * ```${NEWMEMBER[USERNAME]```: New user's username * ```${NEWMEMBER[ISBOT]```: New user is a bot * ```${SERVICE[LEFTMEMBER]```: Id of user left - * ```${LEFTMEBER[ID]```: New user's id - * ```${LEFTMEBER[FIRST_NAME]```: New user's first name - * ```${LEFTMEBER[LAST_NAME]```: New user's last name - * ```${LEFTMEBER[USERNAME]```: New user's username - * ```${LEFTMEBER[ISBOT]```: New user is a bot + * ```${LEFTMEMBER[ID]```: New user's id + * ```${LEFTMEMBER[FIRST_NAME]```: New user's first name + * ```${LEFTMEMBER[LAST_NAME]```: New user's last name + * ```${LEFTMEMBER[USERNAME]```: New user's username + * ```${LEFTMEMBER[ISBOT]```: New user is a bot * ```${SERVICE[NEWTILE]```: Text of new title * ```${SERVICE[NEWPHOTO]```: New Chat Picture array * ```${SERVICE[PINNED]```: Pinned Message structure @@ -261,5 +261,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-11-g6563891 +#### $$VERSION$$ v0.98-dev-25-g4b18757 From b33132ea5ecf7dd90db38255dacb2f894a8ac4f5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 20:07:43 +0200 Subject: [PATCH 27/76] inform dot admin about stop, resume, restart --- bashbot.sh | 5 ++++- modules/background.sh | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index c16c0eb..f2fcd54 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-25-g4b18757 +#### $$VERSION$$ v0.98-dev-26-g8991cc9 # # Exit Codes: # - 0 sucess (hopefully) @@ -1055,6 +1055,9 @@ if [ "${SOURCE}" != "yes" ]; then if [ -n "${BOTPID}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then + # inform botadmin about stop + ADMIN="$(getConfigKey "botadmin")" + [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") stopped ..." & echo -e "${GREEN}OK. Bot stopped successfully.${NC}" else echo -e "${RED}An error occured while stopping bot.${NC}" diff --git a/modules/background.sh b/modules/background.sh index 7bb6a5e..d00c617 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-17-gdda5b6d +#### $$VERSION$$ v0.98-dev-26-g8991cc9 # will be automatically sourced from bashbot @@ -119,7 +119,7 @@ inproc() { # suspendb* # resumeb* job_control() { - local content proc CHAT job fifo killall="" + local ADMIN content proc CHAT job fifo killall="" for FILE in "${DATADIR:-.}/"*-back.cmd; do [ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break content="$(< "${FILE}")" @@ -132,16 +132,25 @@ job_control() { "resumeb"*|"backgr"*) printf "Restart Job: %s %s\n" "${proc}" " ${fifo}" restart_back "${CHAT}" "${proc}" "${job}" + # inform botadmin about stop + ADMIN="$(getConfigKey "botadmin")" + [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") restart background jobs ..." & ;; "suspendb"*) printf "Suspend Job: %s %s\n" "${proc}" " ${fifo}" kill_proc "${CHAT}" "${job}" + # inform botadmin about stop + ADMIN="$(getConfigKey "botadmin")" + [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") suspend background jobs ..." & killall="y" ;; "killb"*) printf "Kill Job: %s %s\n" "${proc}" " ${fifo}" kill_proc "${CHAT}" "${job}" rm -f "${FILE}" # remove job + # inform botadmin about stop + ADMIN="$(getConfigKey "botadmin")" + [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") kill background jobs ..." & killall="y" ;; esac From 70e7ee4bc195c3cba03831df6e6c16b67e9c6a29 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Jun 2020 07:03:47 +0200 Subject: [PATCH 28/76] only one message on suspend/resume jobs --- modules/background.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index d00c617..6bf092d 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-26-g8991cc9 +#### $$VERSION$$ v0.98-dev-27-gb33132e # will be automatically sourced from bashbot @@ -119,7 +119,9 @@ inproc() { # suspendb* # resumeb* job_control() { - local ADMIN content proc CHAT job fifo killall="" + local BOT ADM content proc CHAT job fifo killall="" + BOT="$(getConfigKey "botname")" + ADM="$(getConfigKey "botadmin")" for FILE in "${DATADIR:-.}/"*-back.cmd; do [ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break content="$(< "${FILE}")" @@ -133,15 +135,13 @@ job_control() { printf "Restart Job: %s %s\n" "${proc}" " ${fifo}" restart_back "${CHAT}" "${proc}" "${job}" # inform botadmin about stop - ADMIN="$(getConfigKey "botadmin")" - [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") restart background jobs ..." & + [ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} restart background jobs ..." & ;; "suspendb"*) printf "Suspend Job: %s %s\n" "${proc}" " ${fifo}" kill_proc "${CHAT}" "${job}" # inform botadmin about stop - ADMIN="$(getConfigKey "botadmin")" - [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") suspend background jobs ..." & + [ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} suspend background jobs ..." & killall="y" ;; "killb"*) @@ -149,11 +149,12 @@ job_control() { kill_proc "${CHAT}" "${job}" rm -f "${FILE}" # remove job # inform botadmin about stop - ADMIN="$(getConfigKey "botadmin")" - [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") kill background jobs ..." & + [ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} kill background jobs ..." & killall="y" ;; esac + # send message only onnfirst job + ADM="" done # kill all requestet. kill ALL background jobs, even not listed in data-bot-bash [ "${killall}" = "y" ] && killallproc "back-" From 5dcbccd236f8a9a9f9a3e7c28f2725cf27ec5eda Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Jun 2020 08:31:32 +0200 Subject: [PATCH 29/76] simplify updateArray --- modules/jsonDB.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 3edfc59..d166e93 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-22-gc943280 +#### $$VERSION$$ v0.98-dev-28-g70e7ee4 # # source from commands.sh to use jsonDB functions # @@ -157,7 +157,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [ -z "${2}" ] && return 1 local DB="${2}.jssh" # name check in async [ ! -f "${DB}" ] && return 2 - { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB "${1}" "${2}" } else From 413779dede2becdddc1f0c4fb695fbba57b182a2 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Jun 2020 08:39:54 +0200 Subject: [PATCH 30/76] fix deleteKeyDB after conversion --- modules/jsonDB.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index d166e93..636ca3d 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-28-g70e7ee4 +#### $$VERSION$$ v0.98-dev-29-g5dcbccd # # source from commands.sh to use jsonDB functions # @@ -97,7 +97,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB="${2}.jssh" # start atomic delete here, exclusive max wait 10s - { flock -e -w 10 200; jssh_deleteKey_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 10 200; jssh_deleteKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } # get key/value from jsshDB From 7694df5eccdecadd9ba78b3d061a7c01057d9969 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 16 Jun 2020 23:05:52 +0200 Subject: [PATCH 31/76] detect edited message --- bashbot.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f2fcd54..b510b67 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-26-g8991cc9 +#### $$VERSION$$ v0.98-dev-30-g413779d # # Exit Codes: # - 0 sucess (hopefully) @@ -516,6 +516,8 @@ process_client() { iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" CHAT[ID]="${UPD["result",${num},"message","chat","id"]}" USER[ID]="${UPD["result",${num},"message","from","id"]}" + [ -z "${CHAT[ID]}" ] && CHAT[ID]="${UPD["result",${num},"edited_message","chat","id"]}" + [ -z "${USER[ID]}" ] && USER[ID]="${UPD["result",${num},"edited_message","from","id"]}" # log message on debug [[ -n "${debug}" ]] && printf "\n%s: New Message ==========\n%s\n" "$(date)" "$UPDATE" >>"${LOGDIR}/MESSAGE.log" @@ -525,6 +527,12 @@ process_client() { # process per message type if [ -z "${iQUERY[ID]}" ]; then + if grep -qs -e '\["result",'"${num}"',"edited_message"' <<<"${UPDATE}"; then + # edited message + UPDATE="${UPDATE//,${num},\"edited_message\",/,${num},\"message\",}" + Json2Array 'UPD' <<<"${UPDATE}" + MESSAGE[0]="/edited_message " + fi process_message "${num}" "${debug}" printf "%s: update received FROM=%s CHAT=%s CMD=%s\n" "$(date)" "${USER[USERNAME]:0:20} (${USER[ID]})"\ "${CHAT[USERNAME]:0:20}${CHAT[TITLE]:0:30} (${CHAT[ID]})"\ @@ -683,7 +691,7 @@ process_inline() { process_message() { local num="$1" # Message - MESSAGE[0]="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" + MESSAGE[0]+="$(JsonDecode "${UPD["result",${num},"message","text"]}" | sed 's#\\/#/#g')" MESSAGE[ID]="${UPD["result",${num},"message","message_id"]}" # Chat ID is now parsed when update isreceived From 6e3655fc2bab97920003076f2a575e01f6ec0511 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Jun 2020 05:44:01 +0200 Subject: [PATCH 32/76] fix startup message --- bashbot.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index b510b67..9ff1012 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-30-g413779d +#### $$VERSION$$ v0.98-dev-31-g7694df5 # # Exit Codes: # - 0 sucess (hopefully) @@ -851,7 +851,7 @@ start_bot() { jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start ADMIN="$(getConfigKey "botadmin")" - [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." & + [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") started ..." & ########## # bot is ready, start processing updates ... while true; do @@ -1065,7 +1065,7 @@ if [ "${SOURCE}" != "yes" ]; then if kill ${BOTPID}; then # inform botadmin about stop ADMIN="$(getConfigKey "botadmin")" - [ "${ADMIN}" -gt 4 ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") stopped ..." & + [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") stopped ..." & echo -e "${GREEN}OK. Bot stopped successfully.${NC}" else echo -e "${RED}An error occured while stopping bot.${NC}" From 18f0ace928c042a0ad61b068e80c3645a9717c7c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Jun 2020 08:38:44 +0200 Subject: [PATCH 33/76] finalize service message processing --- bashbot.sh | 32 ++++++++++++++++++-------------- doc/2_usage.md | 50 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 9ff1012..bf01654 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-31-g7694df5 +#### $$VERSION$$ v0.98-dev-32-g6e3655f # # Exit Codes: # - 0 sucess (hopefully) @@ -756,7 +756,7 @@ process_message() { # vunue VENUE=( ) - if grep -qs -e '\["result",'"${num}"',"message","contact"' <<<"${UPDATE}"; then + if grep -qs -e '\["result",'"${num}"',"message","venue"' <<<"${UPDATE}"; then VENUE[TITLE]="$(JsonDecode "${UPD["result",${num},"message","venue","title"]}")" VENUE[ADDRESS]="$(JsonDecode "${UPD["result",${num},"message","venue","address"]}")" VENUE[LONGITUDE]="${UPD["result",${num},"message","venue","location","longitude"]}" @@ -772,31 +772,35 @@ process_message() { LOCATION[LATITUDE]="${UPD["result",${num},"message","location","latitude"]}" # service messages - SERVICE=( ); NEWMEMBER=( ) + SERVICE=( ); NEWMEMBER=( ); LEFTMEMBER=( ) if grep -qs -e '\["result",'"${num}"',"message","new_chat_member' <<<"${UPDATE}"; then SERVICE[NEWMEMBER]="${UPD["result",${num},"message","new_chat_member","id"]}" NEWMEMBER[ID]="${SERVICE[NEWMEMBER]}" - NEWMEMBER[FIRST_NAME]="${UPD["result",${num},"message","new_chat_member","first_name"]}" - NEWMEMBER[LAST_NAME]="${UPD["result",${num},"message","new_chat_member","last_name"]}" - NEWMEMBER[USERNAME]="${UPD["result",${num},"message","new_chat_member","username"]}" + NEWMEMBER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","new_chat_member","first_name"]}")" + NEWMEMBER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","new_chat_member","last_name"]}")" + NEWMEMBER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","new_chat_member","username"]}")" NEWMEMBER[ISBOT]="${UPD["result",${num},"message","new_chat_member","is_bot"]}" MESSAGE[0]="/new_chat_member ${NEWMEMBER[USERNAME]:=${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]}}" fi if grep -qs -e '\["result",'"${num}"',"message","left_chat_member' <<<"${UPDATE}"; then SERVICE[LEFTMEMBER]="${UPD["result",${num},"message","left_chat_member","id"]}" LEFTMEMBER[ID]="${SERVICE[LEFTMEBER]}" - LEFTMEMBER[FIRST_NAME]="${UPD["result",${num},"message","left_chat_member","first_name"]}" - LEFTMEMBER[LAST_NAME]="${UPD["result",${num},"message","left_chat_member","last_name"]}" - LEFTMEBER[USERNAME]="${UPD["result",${num},"message","left_chat_member","username"]}" + LEFTMEMBER[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"message","left_chat_member","first_name"]}")" + LEFTMEMBER[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"message","left_chat_member","last_name"]}")" + LEFTMEBER[USERNAME]="$(JsonDecode "${UPD["result",${num},"message","left_chat_member","username"]}")" LEFTMEMBER[ISBOT]="${UPD["result",${num},"message","left_chat_member","is_bot"]}" MESSAGE[0]="/left_chat_member ${LEFTMEMBER[USERNAME]:=${LEFTMEMBER[FIRST_NAME]} ${LEFTMEMBER[LAST_NAME]}}" fi - SERVICE[NEWTILE]="${UPD["result",${num},"message","new_chat_title"]}" - SERVICE[NEWPHOTO]="${UPD["result",${num},"message","new_chat_photo"]}" - SERVICE[PINNED]="${UPD["result",${num},"message","pinned_message"]}" + if grep -qs -e '\["result",'"${num}"',"message","\(new_chat_[tp]\)\|\(pinned_message\)' <<<"${UPDATE}"; then + SERVICE[NEWTITLE]="$(JsonDecode "${UPD["result",${num},"message","new_chat_title"]}")" + [ -n "${SERVICE[NEWTITLE]}" ] && MESSAGE[0]="/new_chat_title ${SERVICE[NEWTITLE]}" + SERVICE[NEWPHOTO]="$(get_file "${UPD["result",${num},"message","new_chat_photo",0,"file_id"]}")" + [ -n "${SERVICE[NEWPHOTO]}" ] && MESSAGE[0]="/new_chat_photo ${SERVICE[NEWPHOTO]}" + SERVICE[PINNED]="$(JsonDecode "${UPD["result",${num},"message","pinned_message"]}")" + [ -n "${SERVICE[PINNED]}" ] && MESSAGE[0]="/new_pinned_message ${SERVICE[PINNED]}" + fi # set SSERVICE to yes if a service message was received - [[ "${SERVICE[*]}" =~ ^[[:blank:]]+$ ]] || SERVICE[0]="yes" - + [[ "${SERVICE[*]}" =~ ^[[:blank:]]*$ ]] || SERVICE[0]="yes" # split message in command and args CMD=( ) diff --git a/doc/2_usage.md b/doc/2_usage.md index cac8f84..9e37108 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -100,10 +100,10 @@ Evertime a Message is received, you can read incoming data using the following v ### Regular Messages -* ```${MESSAGE}```: Current message +These Variables are always present in regular messages: + +* ```${MESSAGE}```: Current message text * ```${MESSAGE[ID]}```: ID of current message -* ```$CAPTION```: Captions -* ```$REPLYTO```: Original message wich was replied to * ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message. * ```${USER[ID]}```: User id * ```${USER[FIRST_NAME]}```: User's first name @@ -117,6 +117,10 @@ Evertime a Message is received, you can read incoming data using the following v * ```${CHAT[TITLE]}```: Title * ```${CHAT[TYPE]}```: Type * ```${CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]}```: All members are administrators (true if true) + +The following variables are set if the message contains optional parts: + +* ```$REPLYTO```: Original message wich was replied to * ```$REPLYTO```: This array contains the First name, last name, username and user id of the ORIGINAL sender of the message REPLIED to. * ```${REPLYTO[ID]}```: ID of message wich was replied to * ```${REPLYTO[UID]}```: Original user's id @@ -129,6 +133,7 @@ Evertime a Message is received, you can read incoming data using the following v * ```${FORWARD[FIRST_NAME]}```: Original user's first name * ```${FORWARD[LAST_NAME]}```: Original user's' last name * ```${FORWARD[USERNAME]}```: Original user's username +* ```$CAPTION```: Picture, Audio, Video, File Captions * ```$URLS```: This array contains documents, audio files, voice recordings and stickers as URL. * ```${URLS[AUDIO]}```: Audio files * ```${URLS[VIDEO]}```: Videos @@ -151,30 +156,45 @@ Evertime a Message is received, you can read incoming data using the following v * ```${VENUE[LONGITUDE]}```: Longitude * ```${VENUE[LATITUDE]}```: Latitude * ```${VENUE[FOURSQUARE]}```: Fouresquare ID + + +### Service Messages + +Service Messages are regular messages not itended for end users, instead they signal special events to the +client, e.g. new users. + +If a service message is recieved bashbot sets MESSAGE to the service message type as a command, +e.g. if a new user joins a chat MESSAGE is set to "/new_chat_user". + * ```$SERVICE```: This array contains info abbout recived service messages. - * ```${SERVICE}```: set to "yes" when a service message is recived. + * ```${SERVICE}```: "yes" if service message is received * ```${SERVICE[NEWMEMBER]```: New user's id + * ```${MESSAGE}```: /new_chat_member * ```${NEWMEMBER[ID]```: New user's id * ```${NEWMEMBER[FIRST_NAME]```: New user's first name * ```${NEWMEMBER[LAST_NAME]```: New user's last name * ```${NEWMEMBER[USERNAME]```: New user's username * ```${NEWMEMBER[ISBOT]```: New user is a bot * ```${SERVICE[LEFTMEMBER]```: Id of user left - * ```${LEFTMEMBER[ID]```: New user's id - * ```${LEFTMEMBER[FIRST_NAME]```: New user's first name - * ```${LEFTMEMBER[LAST_NAME]```: New user's last name - * ```${LEFTMEMBER[USERNAME]```: New user's username - * ```${LEFTMEMBER[ISBOT]```: New user is a bot - * ```${SERVICE[NEWTILE]```: Text of new title - * ```${SERVICE[NEWPHOTO]```: New Chat Picture array + * ```${MESSAGE}```: /left_chat_member + * ```${LEFTMEMBER[ID]```: Left user's id + * ```${LEFTMEMBER[FIRST_NAME]```: Left user's first name + * ```${LEFTMEMBER[LAST_NAME]```: Left user's last name + * ```${LEFTMEMBER[USERNAME]```: Left user's username + * ```${LEFTMEMBER[ISBOT]```: Left user is a bot + * ```${SERVICE[NEWTITLE]```: Text of new title + * ```${MESSAGE}```: /new_chat_title + * ```${SERVICE[NEWPHOTO]```: New Chat Picture URL + * ```${MESSAGE}```: /new_chat_picture * ```${SERVICE[PINNED]```: Pinned Message structure + * ```${MESSAGE}```: /new_pinned_message +### Inline query messages - -### Inline queries -Evertime a Message is received, you can read incoming data using the following variables: +Inline query messages are small, non regular messages used for interaction with the user, +they contain the following varibles only: * ```${iQUERY}```: Current inline query * ```$iQUERY```: This array contains the ID, First name, last name, username and user id of the sender of the current inline query. @@ -261,5 +281,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-25-g4b18757 +#### $$VERSION$$ v0.98-dev-32-g6e3655f From 91ced1fa35d0b231a3129055f12c4218e7a228af Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Jun 2020 18:36:05 +0200 Subject: [PATCH 34/76] add /edited_message example to mycommands.sh --- mycommands.sh | 8 +++++++- mycommands.sh.clean | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/mycommands.sh b/mycommands.sh index 120c204..0231663 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -8,7 +8,7 @@ # #### if you start to develop your own bot, use the clean version of this file: # #### mycommands.clean # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-33-g18f0ace # # uncomment the following lines to overwrite info and help messages @@ -99,6 +99,12 @@ else send_markdown_message "${CHAT[ID]}" "*${NOTBOTADMIN}*"; return 1 fi ;; + # will we process edited messages also? + '/edited_message'*) + return 1 # no + # but if we do, remove /edited_message + MESSAGE="${MESSAGE#/* }" + ;; esac case "${MESSAGE}" in diff --git a/mycommands.sh.clean b/mycommands.sh.clean index 6fe948e..2d95105 100644 --- a/mycommands.sh.clean +++ b/mycommands.sh.clean @@ -4,7 +4,7 @@ # files: mycommands.sh.clean # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-33-g18f0ace # ########## @@ -72,7 +72,6 @@ else # a service Message was received # add your own stuff here if [ -n "${SERVICE}" ]; then - # example: delete every service message if [ "${SILENCER}" = "yes" ]; then delete_message "${CHAT[ID]}" "${MESSAGE[ID]}" From ea2751370617440d4af1fa20a4e033393a924645 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Jun 2020 19:02:49 +0200 Subject: [PATCH 35/76] should we be more BSD comaptible? sorry ... --- doc/0_install.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/0_install.md b/doc/0_install.md index 6376b15..4e10114 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -51,6 +51,12 @@ see e.g. [Install Bash on Mac](http://macappstore.org/bash/) **On BSD and MacOS** I recommend to install gnu coreutils and include them in front of your PATH environment variable before running bashbot, e.g. the gnu versions of sed, grep, find ... +I considered to make bashbot BSD sed compatible, but much of the bashbot "magic" relies on +(gnu) sed features, e.g. alternate (|), nonprintable (\n\t\<) or repeat (?+) pattern, not supported by BSD sed. +BSD/MacOS sed compatibility will result in a rewrite of all grep/sed commands with an uncertain outcome, +see [BSD/MacOS vs. GNU sed](https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification) +to get an impression how different they are. + In adition you must adjust the shebang line of the scripts ```bashbot.sh``` and ```json.sh``` to point to to the correct bash or use the example script: ```examples/bash2env *.sh */*.sh``` @@ -101,5 +107,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-34-g91ced1f From 2dbe63b79e96171e8e0952f51c3804d34dd10ad3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 17 Jun 2020 19:10:42 +0200 Subject: [PATCH 36/76] quote exaples --- doc/0_install.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/0_install.md b/doc/0_install.md index 4e10114..b0eece0 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -52,7 +52,7 @@ see e.g. [Install Bash on Mac](http://macappstore.org/bash/) environment variable before running bashbot, e.g. the gnu versions of sed, grep, find ... I considered to make bashbot BSD sed compatible, but much of the bashbot "magic" relies on -(gnu) sed features, e.g. alternate (|), nonprintable (\n\t\<) or repeat (?+) pattern, not supported by BSD sed. +(gnu) sed features, e.g. alternation ```|```, non printables ```\n\t\<```) or repeat ```?+``` pattern, not supported by BSD sed. BSD/MacOS sed compatibility will result in a rewrite of all grep/sed commands with an uncertain outcome, see [BSD/MacOS vs. GNU sed](https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification) to get an impression how different they are. @@ -107,5 +107,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.98-dev-34-g91ced1f +#### $$VERSION$$ v0.98-dev-35-gea27513 From 3cd8dc91ae1cd57ea58bccfbe688cd17c8bfe2f0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 11:48:24 +0200 Subject: [PATCH 37/76] readDB on empty array given to updateArray --- modules/jsonDB.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 636ca3d..e01ea75 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-29-g5dcbccd +#### $$VERSION$$ v0.98-dev-36-g2dbe63b # # source from commands.sh to use jsonDB functions # @@ -312,7 +312,7 @@ function jssh_updateArray_async() { local DB; DB="$(jssh_checkDB "$2")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 - [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" + [[ -z "${1[*]}" || "${DB}" -nt "${DB}.last${3}" ]] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" } ############## From 215667b3ef521962cde6b7db6350966fe9afb0e0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 11:51:19 +0200 Subject: [PATCH 38/76] direct read is faster than caching botconfig with updateArray --- bashbot.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index bf01654..3478d7d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-32-g6e3655f +#### $$VERSION$$ v0.98-dev-37-g3cd8dc9 # # Exit Codes: # - 0 sucess (hopefully) @@ -58,11 +58,8 @@ setConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" } -declare -A BASHBOTCONFIG getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - # cache if aupdateArray exists, else read from file - _exec_if_function jssh_updateArray_async "BASHBOTCONFIG" "${BOTDATABASE}" && printf '%s' "${BASHBOTCONFIG[${1}]}" && return [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } From 35d5e05ea91cb07c2450bc70d5878f7551b3831d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 12:36:34 +0200 Subject: [PATCH 39/76] log deleteMessage --- bashbot.sh | 4 +++- doc/0_install.md | 4 ++-- modules/sendMessage.sh | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 3478d7d..e91779a 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-37-g3cd8dc9 +#### $$VERSION$$ v0.98-dev-38-g215667b # # Exit Codes: # - 0 sucess (hopefully) @@ -298,8 +298,10 @@ killallproc() { } +# $ chat $2 mesgid $3 nolog declare -xr DELETE_URL=$URL'/deleteMessage' delete_message() { + [ -z "$3" ] && printf "%s: Delete Message CHAT=%s MSG_ID=%s\n" "$(date)" "${1}" "${2}" >>"${UPDATELOG}" sendJson "${1}" '"message_id": '"${2}"'' "${DELETE_URL}" } diff --git a/doc/0_install.md b/doc/0_install.md index b0eece0..c27ab9e 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -52,7 +52,7 @@ see e.g. [Install Bash on Mac](http://macappstore.org/bash/) environment variable before running bashbot, e.g. the gnu versions of sed, grep, find ... I considered to make bashbot BSD sed compatible, but much of the bashbot "magic" relies on -(gnu) sed features, e.g. alternation ```|```, non printables ```\n\t\<```) or repeat ```?+``` pattern, not supported by BSD sed. +(gnu) sed features, e.g. alternation ```|```, non printables ```\n\t\<``` or repeat ```?+``` pattern, not supported by BSD sed. BSD/MacOS sed compatibility will result in a rewrite of all grep/sed commands with an uncertain outcome, see [BSD/MacOS vs. GNU sed](https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification) to get an impression how different they are. @@ -107,5 +107,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.98-dev-35-gea27513 +#### $$VERSION$$ v0.98-dev-38-g215667b diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index a506c26..17c6cba 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-17-gdda5b6d +#### $$VERSION$$ v0.98-dev-38-g215667b # will be automatically sourced from bashbot @@ -75,7 +75,7 @@ old_send_keyboard() { ISEMPTY="ThisTextIsEmptyAndWillBeDeleted" sendEmpty() { sendJson "${@}" - [[ "${2}" = *"${ISEMPTY}"* ]] && delete_message "${1}" "${BOTSENT[ID]}" + [[ "${2}" = *"${ISEMPTY}"* ]] && delete_message "${1}" "${BOTSENT[ID]}" "nolog" } send_keyboard() { if [[ "$3" != *'['* ]]; then old_send_keyboard "${@}"; return; fi From 344167e259f4e5dfec1ba4b01135ea78b91fe2c7 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 12:39:07 +0200 Subject: [PATCH 40/76] log interactvice/backgound start kill --- bashbot.sh | 2 +- doc/0_install.md | 2 +- modules/background.sh | 9 ++++++--- modules/sendMessage.sh | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index e91779a..c189081 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-38-g215667b +#### $$VERSION$$ v0.98-dev-39-g35d5e05 # # Exit Codes: # - 0 sucess (hopefully) diff --git a/doc/0_install.md b/doc/0_install.md index c27ab9e..9187dfe 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -107,5 +107,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.98-dev-38-g215667b +#### $$VERSION$$ v0.98-dev-39-g35d5e05 diff --git a/modules/background.sh b/modules/background.sh index 6bf092d..6076f39 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-27-gb33132e +#### $$VERSION$$ v0.98-dev-39-g35d5e05 # will be automatically sourced from bashbot @@ -41,8 +41,9 @@ killproc() { # $3 jobname # $4 $5 parameters start_back() { - #local fifo; fifo="${DATADIR:-.}/$(procname "$1")" - printf '%s\n' "$1:$3:$2" >"${DATADIR:-.}/$(procname "$1")$3-back.cmd" + local cmdfile; cmdfile="${DATADIR:-.}/$(procname "$1")$3-back.cmd" + printf "%s: Start background job CHAT=%s JOB=%s CMD=%s\n" "$(date)" "${1}" "${cmdfile##*/}" "${2} ${4} ${5}" >>"${UPDATELOG}" + printf '%s\n' "$1:$3:$2" >"${cmdfile}" restart_back "$@" } restart_back() { @@ -59,6 +60,7 @@ start_proc() { [ -z "$2" ] && return [ -x "${2%% *}" ] || return 1 local fifo; fifo="${DATADIR:-.}/$(procname "$1")" + printf "%s: Start interacitve script CHAT=%s FIFO=%s CMD=%s\n" "$(date)" "${1}" "${fifo##*/}" "${2} ${3} ${4}" >>"${UPDATELOG}" kill_proc "$1" mkfifo "${fifo}" nohup bash -c "{ $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" @@ -95,6 +97,7 @@ kill_proc() { fifo="$(procname "$1" "$2")" prid="$(proclist "${fifo}")" fifo="${DATADIR:-.}/${fifo}" + printf "%s: Stop interacitve / background CHAT=%s FIFO/JOB=%s\n" "$(date)" "${1}" "${fifo##*/}" >>"${UPDATELOG}" # shellcheck disable=SC2086 [ -n "${prid}" ] && kill ${prid} [ -s "${fifo}.log" ] || rm -f "${fifo}.log" diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 17c6cba..5144b2f 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-38-g215667b +#### $$VERSION$$ v0.98-dev-39-g35d5e05 # will be automatically sourced from bashbot From d221fb698e74794e5712e9bb9f7b23bf1ee156ca Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 13:11:19 +0200 Subject: [PATCH 41/76] global BASHBOTDEBUG, log send/get Json to debuglog --- bashbot.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index c189081..8562bbd 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-39-g35d5e05 +#### $$VERSION$$ v0.98-dev-40-g344167e # # Exit Codes: # - 0 sucess (hopefully) @@ -319,6 +319,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ + [ -n "${BASHBOTDEBUG}" ] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" # shellcheck disable=SC2086 "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } @@ -326,6 +327,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then sendJson(){ local chat=""; [ -n "${1}" ] && chat='"chat_id":'"${1}"',' + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (curl) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2}" "${3##*/}" # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}"\ -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ @@ -336,6 +338,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then sendUpload() { [ "$#" -lt 4 ] && return if [ -n "$5" ]; then + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendUpload CHAT=%s WHAT=%s FILE=%s CAPT=%s\n" "$(date)" "${1}" "${2}" "${3}" "${4}" # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" @@ -349,6 +352,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then else # simple curl or wget call outputs result to stdout getJson(){ + [ -n "${BASHBOTDEBUG}" ] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" # shellcheck disable=SC2086 wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1" } @@ -356,6 +360,7 @@ else sendJson(){ local chat=""; [ -n "${1}" ] && chat='"chat_id":'"${1}"',' + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (wget) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2}" "${3##*/}" # 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)"'}' \ --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" @@ -815,33 +820,34 @@ process_message() { ######################### # main get updates loop, should never terminate declare -A BASHBOTBLOCKED +export BASHBOTDEBUG start_bot() { - local ADMIN DEBUG OFFSET=0 + local ADMIN OFFSET=0 # adaptive sleep deafults local nextsleep="100" : local stepsleep="${BASHBOT_SLEEP_STEP:-100}" local maxsleep="${BASHBOT_SLEEP:-5000}" # startup message - DEBUG="$(date): Start BASHBOT updates in Mode \"${1:-normal}\" ==========" - printf "%s\n" "${DEBUG}" >>"${UPDATELOG}" + BASHBOTDEBUG="$(date): Start BASHBOT updates in Mode \"${1:-normal}\" ==========" + printf "%s\n" "${BASHBOTDEBUG}" >>"${UPDATELOG}" # redirect to Debug.log [[ "${1}" == *"debug" ]] && exec &>>"${DEBUGLOG}" - printf "%s\n" "${DEBUG}"; DEBUG="${1}" - [[ "${DEBUG}" == "xdebug"* ]] && set -x + printf "%s\n" "${BASHBOTDEBUG}"; BASHBOTDEBUG="${1}" + [[ "${BASHBOTDEBUG}" == "xdebug"* ]] && set -x #cleaup old pipes and empty logfiles find "${DATADIR}" -type p -delete find "${DATADIR}" -size 0 -name "*.log" -delete # load addons on startup for addons in "${ADDONDIR:-.}"/*.sh ; do # shellcheck source=./modules/aliases.sh - [ -r "${addons}" ] && source "${addons}" "startbot" "${DEBUG}" + [ -r "${addons}" ] && source "${addons}" "startbot" "${BASHBOTDEBUG}" done # shellcheck source=./commands.sh source "${COMMANDS}" "startbot" # start timer events if [ -n "${BASHBOT_START_TIMER}" ] ; then # shellcheck disable=SC2064 - trap "event_timer $DEBUG" ALRM + trap "event_timer $BASHBOTDEBUG" ALRM start_timer & # shellcheck disable=SC2064 trap "kill -9 $!; exit" EXIT INT HUP TERM QUIT @@ -877,7 +883,7 @@ start_bot() { if [ "$OFFSET" != "1" ]; then nextsleep="100" - process_updates "${DEBUG}" + process_updates "${BASHBOTDEBUG}" fi else # ups, something bad happend, wait maxsleep*10 From eea1de6cbdc2fd4f6135e2e3326442fb7650435e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 13:19:30 +0200 Subject: [PATCH 42/76] global BASHBOTDEBUG, do not log getting updates --- bashbot.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 8562bbd..ae44af5 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-40-g344167e +#### $$VERSION$$ v0.98-dev-41-gd221fb6 # # Exit Codes: # - 0 sucess (hopefully) @@ -319,7 +319,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ - [ -n "${BASHBOTDEBUG}" ] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" + [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" # shellcheck disable=SC2086 "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } @@ -352,7 +352,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then else # simple curl or wget call outputs result to stdout getJson(){ - [ -n "${BASHBOTDEBUG}" ] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" + [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" # shellcheck disable=SC2086 wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1" } @@ -867,7 +867,7 @@ start_bot() { # adaptive sleep in ms rounded to next 0.1 s sleep "$(_round_float "${nextsleep}e-3" "1")" # get next update - UPDATE="$(getJson "$UPD_URL$OFFSET" 2>/dev/null | "${JSONSHFILE}" -s -b -n 2>/dev/null | iconv -f utf-8 -t utf-8 -c)" + UPDATE="$(getJson "$UPD_URL$OFFSET" "nolog" 2>/dev/null | "${JSONSHFILE}" -s -b -n 2>/dev/null | iconv -f utf-8 -t utf-8 -c)" # did we ge an responsn0r if [ -n "${UPDATE}" ]; then # we got something, do processing From 8b3e92d2331fffe132db45bebee886044ef5641d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 14:00:38 +0200 Subject: [PATCH 43/76] explain why $ in user imput can be dangeruous --- README.html | 5 +++-- README.md | 10 +++++++--- README.txt | 16 ++++++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.html b/README.html index 1afdc83..5a66ad9 100644 --- a/README.html +++ b/README.html @@ -190,7 +190,8 @@ It features background tasks and interactive chats, and can serve as an interfac

Running a Telegram Bot means it is connected to the public and you never know whats send to your Bot.

Bash scripts in general are not designed to be bullet proof, so consider this Bot as a proof of concept. Bash programmers often struggle with 'quoting hell' and globbing, see Implications of wrong quoting

Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands.

-

Note: Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version! One of the most powerful features of unix shells like bash is variable and command substitution, this can lead to RCE and information disclosing bugs if you do not escape '$' porperly, see Issue #125

+

Note: Until v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, pls update if you use an older version! see Issue #125

+

One of the most powerful features of unix shells like bash is variable and command substitution using ${} and $(), but as they are expanded in doble quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot even bash does much to avoid this. So it's more secure to escape or remove '$' in input from user, files or network.

A powerful tool to improve your scripts is shellcheck. You can use it online or install shellcheck locally. Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.

use printf whenever possible

If you're writing a script and it is taking external input (from the user as arguments, or file names from the file system...), you shouldn't use echo to display it. Use printf whenever possible

@@ -256,6 +257,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ v0.98-dev-10-g74caa49

+

$$VERSION$$ v0.98-dev-42-geea1de6

diff --git a/README.md b/README.md index 2ba8736..398bbab 100644 --- a/README.md +++ b/README.md @@ -127,8 +127,12 @@ Bash scripts in general are not designed to be bullet proof, so consider this Bo Whenever you are processing input from from untrusted sources (messages, files, network) you must be as carefull as possible, e.g. set IFS appropriate, disable globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. -**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code execution bug, pls update if you use an older version! -One of the most powerful features of unix shells like bash is variable and command substitution, this can lead to RCE and information disclosing bugs if you do not escape '$' porperly, see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash had a remote code execution (RCE) bug, pls update if you use an older version! +see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) + +One of the most powerful features of unix shells like bash is variable and command substitution using ```${}``` and ```$()```, +but as they are expanded in doble quotes, this can lead to RCE and information disclosing bugs in complex scripts like bashbot +even bash does much to avoid this. So it's more secure to escape or remove '$' in input from user, files or network. A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck locally](https://github.com/koalaman/shellcheck#installing). Shellcheck is used extensive in bashbot development to enshure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a [test suite](doc/7_develop.md) to check if important functionality is working as expected. @@ -235,4 +239,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-10-g74caa49 +#### $$VERSION$$ v0.98-dev-42-geea1de6 diff --git a/README.txt b/README.txt index 0ac45cc..cade7d3 100644 --- a/README.txt +++ b/README.txt @@ -168,12 +168,16 @@ globbing (set -f) and quote everthing. In addition delete unused scripts and examples from your Bot, e.g. scripts 'notify', 'calc', 'question', and disable all not used commands. -**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash has a remote code -execution bug, pls update if you use an older version! +**Note:** Until v0.941 (mai/22/2020) telegram-bot-bash had a remote code +execution (RCE) bug, pls update if you use an older version! +see [Issue #125](https://github.com/topkecleon/telegram-bot-bash/issues/125) + One of the most powerful features of unix shells like bash is variable and -command substitution, this can lead to RCE and information disclosing bugs if -you do not escape '$' porperly, see [Issue -#125](https://github.com/topkecleon/telegram-bot-bash/issues/125) +command substitution using ```${}``` and ```$()```, +but as they are expanded in doble quotes, this can lead to RCE and information +disclosing bugs in complex scripts like bashbot +even bash does much to avoid this. So it's more secure to escape or remove '$' +in input from user, files or network. A powerful tool to improve your scripts is ```shellcheck```. You can [use it online](https://www.shellcheck.net/) or [install shellcheck @@ -324,4 +328,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-10-g74caa49 +#### $$VERSION$$ v0.98-dev-42-geea1de6 From 02715e4128eb151bc9bb4aea0a8e7e3f1d08506d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 14:06:36 +0200 Subject: [PATCH 44/76] rename UPDATE.log to BASHBOT.log --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ae44af5..ca2fb6c 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-41-gd221fb6 +#### $$VERSION$$ v0.98-dev-43-g8b3e92d # # Exit Codes: # - 0 sucess (hopefully) @@ -116,7 +116,7 @@ if [ ! -d "${LOGDIR}" ] || [ ! -w "${LOGDIR}" ]; then fi DEBUGLOG="${LOGDIR}/DEBUG.log" ERRORLOG="${LOGDIR}/ERROR.log" -UPDATELOG="${LOGDIR}/UPDATE.log" +UPDATELOG="${LOGDIR}/BASHBOT.log" # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then From 30eedcffb10f496be551c9728cb51a7df6a3038e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 14:33:13 +0200 Subject: [PATCH 45/76] fix updateArray check for empty array --- modules/jsonDB.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index e01ea75..cf4207b 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-36-g2dbe63b +#### $$VERSION$$ v0.98-dev-44-g02715e4 # # source from commands.sh to use jsonDB functions # @@ -157,7 +157,8 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [ -z "${2}" ] && return 1 local DB="${2}.jssh" # name check in async [ ! -f "${DB}" ] && return 2 - [ "${DB}" -nt "${DB}.last${3}" ] && touch "${DB}.last${3}" && jssh_readDB "${1}" "${2}" + declare -n ARRAY="$1" + [[ -z "${ARRAY[*]}" || "${DB}" -nt "${DB}.last${3}" ]] && touch "${DB}.last${3}" && jssh_readDB "${1}" "${2}" } else @@ -312,7 +313,8 @@ function jssh_updateArray_async() { local DB; DB="$(jssh_checkDB "$2")" [ -z "${DB}" ] && return 1 [ ! -f "${DB}" ] && return 2 - [[ -z "${1[*]}" || "${DB}" -nt "${DB}.last${3}" ]] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" + declare -n ARRAY="$1" + [[ -z "${ARRAY[*]}" || "${DB}" -nt "${DB}.last${3}" ]] && touch "${DB}.last${3}" && jssh_readDB_async "${1}" "${2}" } ############## From 67f2dcd4985f84e8dc372c0189236f1fc3c60e9e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 15:38:05 +0200 Subject: [PATCH 46/76] finalise get/send Json log on debug --- bashbot.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ca2fb6c..e8904cf 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-43-g8b3e92d +#### $$VERSION$$ v0.98-dev-45-g30eedcf # # Exit Codes: # - 0 sucess (hopefully) @@ -319,7 +319,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" + [[ -n "${BASHBOTDEBUG}" && -n "${3}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } @@ -327,7 +327,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then sendJson(){ local chat=""; [ -n "${1}" ] && chat='"chat_id":'"${1}"',' - [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (curl) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2}" "${3##*/}" + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (curl) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2:0:100}" "${3##*/}" 1>&2 # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}"\ -d '{'"${chat} $(iconv -f utf-8 -t utf-8 -c <<<$2)"'}' -X POST "${3}" \ @@ -338,7 +338,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then sendUpload() { [ "$#" -lt 4 ] && return if [ -n "$5" ]; then - [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendUpload CHAT=%s WHAT=%s FILE=%s CAPT=%s\n" "$(date)" "${1}" "${2}" "${3}" "${4}" + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendUpload CHAT=%s WHAT=%s FILE=%s CAPT=%s\n" "$(date)" "${1}" "${2}" "${3}" "${4}" 1>&2 # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" @@ -352,7 +352,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then else # simple curl or wget call outputs result to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" + [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1" } @@ -360,7 +360,7 @@ else sendJson(){ local chat=""; [ -n "${1}" ] && chat='"chat_id":'"${1}"',' - [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (wget) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2}" "${3##*/}" + [ -n "${BASHBOTDEBUG}" ] && printf "%s: sendJson (wget) CHAT=%s JSON=%s URL=%s\n" "$(date)" "${1}" "${2:0:100}" "${3##*/}" 1>&2 # 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)"'}' \ --header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n 2>/dev/null )" @@ -399,6 +399,7 @@ sendJsonRetry(){ # $1 result $2 function $3 .. $n original arguments, $3 is Chat_id sendJsonResult(){ BOTSENT=( ) + [ -n "${BASHBOTDEBUG}" ] && printf "\n%s: New Result ==========\n%s\n" "$(date)" "$1" >>"${LOGDIR}/MESSAGE.log" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" if [ "${BOTSENT[OK]}" = "true" ]; then BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "${1}")" From ba614b71fd14e197fc0cff1d36c181cbb5b6df59 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 16:04:39 +0200 Subject: [PATCH 47/76] fix nolog on updates --- bashbot.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index e8904cf..94b361f 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-45-g30eedcf +#### $$VERSION$$ v0.98-dev-46-g67f2dcd # # Exit Codes: # - 0 sucess (hopefully) @@ -319,7 +319,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -n "${3}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" 1>&2 + [[ -n "${BASHBOTDEBUG}" && -z "${2}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } @@ -352,7 +352,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then else # simple curl or wget call outputs result to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" 1>&2 + [[ -n "${BASHBOTDEBUG}" && -z "${2}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1" } From 9adc62d6ecb25b7ac86e857e4d9b947335f17ef5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 18 Jun 2020 16:20:42 +0200 Subject: [PATCH 48/76] update doc for loggin --- README.html | 8 ++++---- README.md | 10 +++++----- README.txt | 14 +++++++------- doc/2_usage.md | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.html b/README.html index 5a66ad9..6862f6d 100644 --- a/README.html +++ b/README.html @@ -176,14 +176,14 @@ It features background tasks and interactive chats, and can serve as an interfac

For more Information on how to install, customize and use your new bot, read the Documentation

Log files

-

Since version 0.96 bashbot log commands recieved (updates) and connection errors. If you start bashbot in debug mode bash stdout, stderr and all recieved telegram message updates are logged also.

+

Since version 0.96 bashbot log commands recieved/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also.

To enable debug mode start bashbot with debug as third argument: bashbot start debug

├── logs 
+│   ├── BASHBOT.log      # log what your bot is doing ...
 │   ├── ERROR.log        # connection errors from / to telegram API
-│   ├── UPDATE.log       # summary whats received from your bot
 │   │
 │   ├── DEBUG.log        # stdout/stderr of you bot (debug mode enabled)
-│   └── MESSAGE.log      # full text of all message updates received (debug mode enabled)
+│   └── MESSAGE.log      # full text of all message send/received (debug mode enabled)
 

Security Considerations

@@ -257,6 +257,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it!

If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

-

$$VERSION$$ v0.98-dev-42-geea1de6

+

$$VERSION$$ v0.98-dev-47-gba614b7

diff --git a/README.md b/README.md index 398bbab..8a7569d 100644 --- a/README.md +++ b/README.md @@ -104,18 +104,18 @@ For more Information on how to install, customize and use your new bot, read the ### Log files -Since version 0.96 bashbot log commands recieved (updates) and connection errors. If you start bashbot in debug mode -bash stdout, stderr and all recieved telegram message updates are logged also. +Since version 0.96 bashbot log commands recieved/send and connection errors. If you start bashbot in debug mode +bash stdout, stderr and all send/received telegram message are logged also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` ``` ├── logs +│   ├── BASHBOT.log # log what your bot is doing ... │   ├── ERROR.log # connection errors from / to telegram API -│   ├── UPDATE.log # summary whats received from your bot │   │ │   ├── DEBUG.log # stdout/stderr of you bot (debug mode enabled) -│   └── MESSAGE.log # full text of all message updates received (debug mode enabled) +│   └── MESSAGE.log # full text of all message send/received (debug mode enabled) ``` ---- @@ -239,4 +239,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-42-geea1de6 +#### $$VERSION$$ v0.98-dev-47-gba614b7 diff --git a/README.txt b/README.txt index cade7d3..8b24b76 100644 --- a/README.txt +++ b/README.txt @@ -133,21 +133,21 @@ the [Documentation](#Documentation) ### Log files -Since version 0.96 bashbot log commands recieved (updates) and connection -errors. If you start bashbot in debug mode -bash stdout, stderr and all recieved telegram message updates are logged also. +Since version 0.96 bashbot log commands recieved/send and connection errors. If +you start bashbot in debug mode +bash stdout, stderr and all send/received telegram message are logged also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` ``` ├── logs +│   ├── BASHBOT.log # log what your bot is doing ... │   ├── ERROR.log # connection errors from / to telegram API -│   ├── UPDATE.log # summary whats received from your bot │   │ │   ├── DEBUG.log # stdout/stderr of you bot (debug mode enabled) -│   └── MESSAGE.log # full text of all message updates received (debug -mode enabled) +│   └── MESSAGE.log # full text of all message send/received (debug mode +enabled) ``` ---- @@ -328,4 +328,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-42-geea1de6 +#### $$VERSION$$ v0.98-dev-47-gba614b7 diff --git a/doc/2_usage.md b/doc/2_usage.md index 9e37108..067a5cd 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -32,7 +32,7 @@ Have FUN! ├── scripts # place your bashbot interactive and background scripts here │   └── interactive.sh.clean # interactive script template for new scripts │ -├── logs # here you'll find ERROR, UPDATE, DEBUG and MESSAGE.log +├── logs # here you'll find BASHBOT, ERROR, DEBUG and MESSAGE.log │ ├── modules # optional functions, sourced by commands.sh │   ├── aliases.sh # to disable modules rename them xxx.sh.off @@ -281,5 +281,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-32-g6e3655f +#### $$VERSION$$ v0.98-dev-47-gba614b7 From 5dcc7ef3f64b320d77c7a755b59da1e5886ee750 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 11:13:36 +0200 Subject: [PATCH 49/76] jsonDB check for empty filename always --- modules/jsonDB.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index cf4207b..411474b 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-44-g02715e4 +#### $$VERSION$$ v0.98-dev-48-g9adc62d # # source from commands.sh to use jsonDB functions # @@ -106,6 +106,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then alias jssh_getDB=jssh_getKeyDB jssh_getKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + [ -z "${DB}" ] && return 1 local DB; DB="$(jssh_checkDB "$2")" # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 @@ -125,7 +126,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB="${2}.jssh" # start atomic delete here, exclusive max wait 5 - { flock -e -w 5 200; jssh_countKeyDB "$@"; } 200>"${DB}${JSSH_LOCKNAME}" + { flock -e -w 5 200; jssh_countKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } # update key/value in place to jsshDB @@ -135,6 +136,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then #no own locking, so async is the same as updatekeyDB jssh_updateKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + [ -z "${3}" ] && return 1 declare -A updARR # shellcheck disable=SC2034 updARR["$1"]="$2" @@ -230,6 +232,7 @@ jssh_writeDB_async() { } jssh_updateDB_async() { + [ -z "${2}" ] && return 1 declare -n ARRAY="$1" [ -z "${ARRAY[*]}" ] && return 1 declare -A oldARR @@ -262,6 +265,7 @@ jssh_insertKeyDB_async() { jssh_deleteKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 declare -A oldARR Json2Array "oldARR" <"${DB}" unset oldARR["$1"] @@ -271,12 +275,14 @@ jssh_deleteKeyDB_async() { jssh_getKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 } jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local VAL DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 # start atomic delete here, exclusive max wait 5 if [ -n "$3" ]; then declare -A oldARR @@ -297,6 +303,7 @@ jssh_countKeyDB_async() { #no own locking, so async is the same as updatekeyDB jssh_updateKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + [ -z "${3}" ] && return 1 declare -A updARR # shellcheck disable=SC2034 updARR["$1"]="$2" From fa5be1eafba4168ac7c24245ded13a43c2854c53 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 12:49:18 +0200 Subject: [PATCH 50/76] rename BOTDATABASE to BOTCONFIG --- bashbot.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 94b361f..36f34e2 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-46-g67f2dcd +#### $$VERSION$$ v0.98-dev-49-g5dcc7ef # # Exit Codes: # - 0 sucess (hopefully) @@ -56,11 +56,11 @@ _round_float() { } setConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh" } getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 + [ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1 } @@ -102,7 +102,7 @@ if [ ! -w "." ]; then fi # Setup and check environment if BOTTOKEN is NOT set -BOTDATABASE="${BASHBOT_ETC:-.}/botconfig" +BOTCONFIG="${BASHBOT_ETC:-.}/botconfig" TOKENFILE="${BASHBOT_ETC:-.}/token" BOTADMIN="${BASHBOT_ETC:-.}/botadmin" BOTACL="${BASHBOT_ETC:-.}/botacl" @@ -121,8 +121,8 @@ UPDATELOG="${LOGDIR}/BASHBOT.log" # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then # DATABASE does not exist, create - [ ! -f "${BOTDATABASE}.jssh" ] && - printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTDATABASE}.jssh" + [ ! -f "${BOTCONFIG}.jssh" ] && + printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" # BOTTOKEN empty read ask user if [ -z "$(getConfigKey "bottoken")" ]; then # convert old token @@ -138,7 +138,7 @@ if [ -z "${BOTTOKEN}" ]; then echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token fi - [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTDATABASE}.jssh" + [ -n "${token}" ] && printf '["bottoken"]\t"%s"\n' "${token}" >> "${BOTCONFIG}.jssh" fi # setup botadmin file @@ -156,7 +156,7 @@ if [ -z "${BOTTOKEN}" ]; then read -r admin fi [ -z "${admin}" ] && admin='?' - printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTDATABASE}.jssh" + printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTCONFIG}.jssh" fi # setup botacl file if [ ! -f "${BOTACL}" ]; then @@ -856,8 +856,8 @@ start_bot() { # cleanup countfile on startup jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" - jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTDATABASE}" - [ -f "${BOTDATABASE}.jssh.flock" ] && rm -f "${BOTDATABASE}.jssh.flock" + jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTCONFIG}" + [ -f "${BOTCONFIG}.jssh.flock" ] && rm -f "${BOTCONFIG}.jssh.flock" jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start ADMIN="$(getConfigKey "botadmin")" @@ -968,8 +968,8 @@ if [ "${SOURCE}" != "yes" ]; then if [ -n "${ME}" ]; then # ok we have a connection an got botname, save it [ -n "${CLEAR}" ] && echo -e "${GREY}Bottoken is valid ...${NC}" - jssh_updateKeyDB "botname" "${ME}" "${BOTDATABASE}" - rm -f "${BOTDATABASE}.jssh.flock" + jssh_updateKeyDB "botname" "${ME}" "${BOTCONFIG}" + rm -f "${BOTCONFIG}.jssh.flock" else echo -e "${GREY}Info: Can't get Botname from Telegram, try cached one ...${NC}" ME="$(getConfigKey "botname")" From cf536c9ca3fe24770acc9bc39c680236c3dc5f1f Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 12:51:36 +0200 Subject: [PATCH 51/76] make help browseable if w3mmor lynx exists --- bashbot.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 36f34e2..d020135 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-49-g5dcc7ef +#### $$VERSION$$ v0.98-dev-50-gfa5be1e # # Exit Codes: # - 0 sucess (hopefully) @@ -1092,7 +1092,13 @@ if [ "${SOURCE}" != "yes" ]; then job_control "$1" ;; "help") - less "README.txt" + HELP="README" + if [ -n "${CLEAR}" ];then + _exists w3m && w3m "$HELP.html" && exit + _exists lynx && lynx "$HELP.html" && exit + _exists less && less "$HELP.txt" && exit + fi + cat "$HELP.txt" exit ;; *) From 75024a17261abecdec611b200abbb580938a7afe Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 12:54:05 +0200 Subject: [PATCH 52/76] rearrange get botname, allow recover from broken botconfig --- bashbot.sh | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index d020135..e21ce31 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-50-gfa5be1e +#### $$VERSION$$ v0.98-dev-51-gcf536c9 # # Exit Codes: # - 0 sucess (hopefully) @@ -21,6 +21,7 @@ # - 4 unkown command # - 5 cannot connect to telegram bot # - 6 mandatory module not found +# - 6 can't get bottoken # shellcheck disable=SC2140,SC2031,SC2120,SC1091 # are we runnig in a terminal? @@ -120,7 +121,7 @@ UPDATELOG="${LOGDIR}/BASHBOT.log" # we assume everthing is already set up correctly if we have TOKEN if [ -z "${BOTTOKEN}" ]; then - # DATABASE does not exist, create + # BOTCONFIG does not exist, create [ ! -f "${BOTCONFIG}.jssh" ] && printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" # BOTTOKEN empty read ask user @@ -143,7 +144,7 @@ if [ -z "${BOTTOKEN}" ]; then # setup botadmin file if [ -z "$(getConfigKey "botadmin")" ]; then - # convert old token + # convert old admin if [ -r "${BOTADMIN}" ]; then admin="$(< "${BOTADMIN}")" elif [ -z "${CLEAR}" ]; then @@ -190,11 +191,24 @@ if [ -z "${BOTTOKEN}" ]; then fi # read BOTTOKEN from bot database if not set -[ -z "${BOTTOKEN}" ] && BOTTOKEN="$(getConfigKey "bottoken")" +if [ -z "${BOTTOKEN}" ]; then + BOTTOKEN="$(getConfigKey "bottoken")" + if [[ -z "${BOTTOKEN}" && "${1}" != "help" ]]; then + echo -e "${ORANGE}Warning: can't get bot token, try to recover working config.${NC}" + if [ -r "${BOTCONFIG}.jssh.ok" ]; then + cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh" + BOTTOKEN="$(getConfigKey "bottoken")" + else + echo -e "${RED}Error: Missing bot token! remove ${BOTCONFIG}.jssh and run \"bashbot.sh init\" may fix it.${NC}" + exit 7 + fi + fi +fi + # BOTTOKEN format checks if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then - echo -e "${ORANGE}Warning, your bottoken may incorrect. it should have the following format:${NC}" + echo -e "${ORANGE}Warning: your bottoken may incorrect. it should have the following format:${NC}" echo -e "${GREY}123456789${RED}:${GREY}Aa-Zz_0Aa-Zz_1Aa-Zz_2Aa-Zz_3Aa-Zz_4${ORANGE} => ${NC}\c" echo -e "${GREY}8-10 digits${RED}:${GREY}35 alnum characters + '_-'${NC}" echo -e "${ORANGE}Your current token is: '${GREY}^$(cat -ve <<<"${BOTTOKEN//:/${RED}:${GREY}}")${ORANGE}'${NC}" @@ -319,7 +333,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then [ -z "${BASHBOT_CURL}" ] && BASHBOT_CURL="curl" # simple curl or wget call, output to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -z "${2}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" 1>&2 + [[ -n "${BASHBOTDEBUG}" && -n "${3}" ]] && printf "%s: getJson (curl) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 "${BASHBOT_CURL}" -sL -k ${BASHBOT_CURL_ARGS} -m "${TIMEOUT}" "$1" } @@ -352,7 +366,7 @@ if [ -z "${BASHBOT_WGET}" ] && _exists curl ; then else # simple curl or wget call outputs result to stdout getJson(){ - [[ -n "${BASHBOTDEBUG}" && -z "${2}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" 1>&2 + [[ -n "${BASHBOTDEBUG}" && -z "${3}" ]] && printf "%s: getJson (wget) URL=%s\n" "$(date)" "${1##*/}" 1>&2 # shellcheck disable=SC2086 wget --no-check-certificate -t 2 -T "${TIMEOUT}" ${BASHBOT_WGET_ARGS} -qO - "$1" } @@ -856,7 +870,7 @@ start_bot() { # cleanup countfile on startup jssh_deleteKeyDB "CLEAN_COUNTER_DATABASE_ON_STARTUP" "${COUNTFILE}" [ -f "${COUNTFILE}.jssh.flock" ] && rm -f "${COUNTFILE}.jssh.flock" - jssh_deleteKeyDB "CLEAN_BOT_DATABASE_ON_STARTUP" "${BOTCONFIG}" + jssh_deleteKeyDB "CLEAN_BOT_BOTCONFIG_ON_STARTUP" "${BOTCONFIG}" [ -f "${BOTCONFIG}.jssh.flock" ] && rm -f "${BOTCONFIG}.jssh.flock" jssh_readDB_async "BASHBOTBLOCKED" "${BLOCKEDFILE}" # inform botadmin about start @@ -932,7 +946,7 @@ bot_init() { chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTADMIN}" "${LOGDIR}/"*.log 2>/dev/null chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null # jsshDB must writeable by owner - find . -name '*.jssh' -exec chmod u+w \{\} + + find . -name '*.jssh*' -exec chmod u+w \{\} + fi # show result ls -l @@ -962,8 +976,8 @@ if [ "${SOURCE}" != "yes" ]; then ############## # internal options only for use from bashbot and developers case "$1" in - # all these commands need the botname and a working connection - "botname"|"outproc"|"start"*|"stop"*|"kill"*|"resume"*|"suspend"*|"status") + # update botname botname when starting only + "botname"|"start"*|"resume"*) ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection an got botname, save it @@ -979,14 +993,13 @@ if [ "${SOURCE}" != "yes" ]; then fi fi [ -n "${CLEAR}" ] && printf "Bot Name: %s\n" "${ME}" - SESSION="${ME:-_bot}-startbot" - BOTPID="$(proclist "${SESSION}")" [ "$1" = "botname" ] && exit ;;& # used to send output of backgrond and interactive to chats "outproc") # $2 chat_id $3 identifier of job, internal use only! [ -z "$3" ] && echo "No job identifier" && exit 3 [ -z "$2" ] && echo "No chat to send to" && exit 3 + ME="$(getConfigKey "botname")" # read until terminated while read -r line ;do [ -n "$line" ] && send_message "$2" "$line" @@ -1009,6 +1022,7 @@ if [ "${SOURCE}" != "yes" ]; then # print usage sats "count") echo -e "${RED}Command ${GREY}count${RED} is deprecated, use ${GREY}stats{$RED}instead.${NC}";& "stats") + ME="$(getConfigKey "botname")" declare -A STATS jssh_readDB_async "STATS" "${COUNTFILE}" for MSG in ${!STATS[*]} @@ -1025,10 +1039,11 @@ if [ "${SOURCE}" != "yes" ]; then ;; # sedn message to all users 'broadcast') + ME="$(getConfigKey "botname")" declare -A SENDALL shift jssh_readDB_async "SENDALL" "${COUNTFILE}" - echo -e "Sending broadcast message to all users \c" + echo -e "Sending broadcast message to all users of ${ME} \c" for MSG in ${!SENDALL[*]} do [[ ! "${MSG}" =~ ^[0-9-]*$ ]] && continue @@ -1044,6 +1059,9 @@ if [ "${SOURCE}" != "yes" ]; then ;; # does what is says "status") + ME="$(getConfigKey "botname")" + SESSION="${ME:-_bot}-startbot" + BOTPID="$(proclist "${SESSION}")" if [ -n "${BOTPID}" ]; then echo -e "${GREEN}Bot is running with UID ${RUNUSER}.${NC}" exit @@ -1055,6 +1073,9 @@ if [ "${SOURCE}" != "yes" ]; then # start bot as background jod and check if bot is running "start") + # shellcheck disable=SC2086 + SESSION="${ME:-_bot}-startbot" + BOTPID="$(proclist "${SESSION}")" # shellcheck disable=SC2086 [ -n "${BOTPID}" ] && kill ${BOTPID} nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null & @@ -1070,12 +1091,15 @@ if [ "${SOURCE}" != "yes" ]; then # does what it says "kill") echo -e "${RED}Command ${GREY}kill${RED} is deprecated, use ${GREY}stop{$RED}instead.${NC}";& "stop") + ME="$(getConfigKey "botname")" + SESSION="${ME:-_bot}-startbot" + BOTPID="$(proclist "${SESSION}")" if [ -n "${BOTPID}" ]; then # shellcheck disable=SC2086 if kill ${BOTPID}; then # inform botadmin about stop ADMIN="$(getConfigKey "botadmin")" - [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(getConfigKey "botname") stopped ..." & + [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(ME) stopped ..." & echo -e "${GREEN}OK. Bot stopped successfully.${NC}" else echo -e "${RED}An error occured while stopping bot.${NC}" From 11b5aab9b97182bc73f7bf53ada67d11ce9d3416 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 12:56:09 +0200 Subject: [PATCH 53/76] do not create botconfig on help --- bashbot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index e21ce31..75613e0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-51-gcf536c9 +#### $$VERSION$$ v0.98-dev-52-g75024a1 # # Exit Codes: # - 0 sucess (hopefully) @@ -120,7 +120,7 @@ ERRORLOG="${LOGDIR}/ERROR.log" UPDATELOG="${LOGDIR}/BASHBOT.log" # we assume everthing is already set up correctly if we have TOKEN -if [ -z "${BOTTOKEN}" ]; then +if [[ -z "${BOTTOKEN}" && "${1}" != "help" ]]; then # BOTCONFIG does not exist, create [ ! -f "${BOTCONFIG}.jssh" ] && printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" From f1d71a0771413a5491432c63b44c17a1ddbe4c59 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 13:15:50 +0200 Subject: [PATCH 54/76] process help at beginning --- .github/ISSUE_TEMPLATE/bug_report.md | 33 ----------------------- .github/ISSUE_TEMPLATE/feature_request.md | 20 -------------- .github/ISSUE_TEMPLATE/others.md | 17 ------------ bashbot.sh | 27 ++++++++++--------- 4 files changed, 14 insertions(+), 83 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 .github/ISSUE_TEMPLATE/feature_request.md delete mode 100644 .github/ISSUE_TEMPLATE/others.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index cba24b6..0000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve -title: '' -labels: '' -assignees: '' - ---- - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Enter text / Run command '....' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** -If applicable, add screenshots to help explain your problem. - -**Bashbot (please complete the following information):** - - Version [ grep 'VERSION' bashbot.sh ] - - OS: [ uname -a] - - Shell [ bash --version] - - -**Additional context** -Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index bbcbbe7..0000000 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: '' -assignees: '' - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/others.md b/.github/ISSUE_TEMPLATE/others.md deleted file mode 100644 index 92c89a9..0000000 --- a/.github/ISSUE_TEMPLATE/others.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -name: Others -about: Anything else not a Bug or Feature -title: '' -labels: '' -assignees: '' - ---- - -**Is your issue is related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe your idea or complaint** -A clear and concise description of what you want to talk about. - -**Additional context** -Add any other context or screenshots about your idea or complaint here. diff --git a/bashbot.sh b/bashbot.sh index 75613e0..18b5c78 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-52-g75024a1 +#### $$VERSION$$ v0.98-dev-53-g11b5aab # # Exit Codes: # - 0 sucess (hopefully) @@ -64,6 +64,17 @@ getConfigKey() { [ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1 } +if [ "$1" = "help" ]; then + HELP="README" + if [ -n "${CLEAR}" ];then + _exists w3m && w3m "$HELP.html" && exit + _exists lynx && lynx "$HELP.html" && exit + _exists less && less "$HELP.txt" && exit + fi + cat "$HELP.txt" + exit +fi + # get location and name of bashbot.sh SCRIPT="$0" @@ -120,7 +131,7 @@ ERRORLOG="${LOGDIR}/ERROR.log" UPDATELOG="${LOGDIR}/BASHBOT.log" # we assume everthing is already set up correctly if we have TOKEN -if [[ -z "${BOTTOKEN}" && "${1}" != "help" ]]; then +if [ -z "${BOTTOKEN}" ]; then # BOTCONFIG does not exist, create [ ! -f "${BOTCONFIG}.jssh" ] && printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" @@ -193,7 +204,7 @@ fi # read BOTTOKEN from bot database if not set if [ -z "${BOTTOKEN}" ]; then BOTTOKEN="$(getConfigKey "bottoken")" - if [[ -z "${BOTTOKEN}" && "${1}" != "help" ]]; then + if [ -z "${BOTTOKEN}" ]; then echo -e "${ORANGE}Warning: can't get bot token, try to recover working config.${NC}" if [ -r "${BOTCONFIG}.jssh.ok" ]; then cp "${BOTCONFIG}.jssh.ok" "${BOTCONFIG}.jssh" @@ -1115,16 +1126,6 @@ if [ "${SOURCE}" != "yes" ]; then _is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; } job_control "$1" ;; - "help") - HELP="README" - if [ -n "${CLEAR}" ];then - _exists w3m && w3m "$HELP.html" && exit - _exists lynx && lynx "$HELP.html" && exit - _exists less && less "$HELP.txt" && exit - fi - cat "$HELP.txt" - exit - ;; *) echo -e "${RED}${REALME##*/}: unknown command${NC}" echo -e "${ORANGE}Available commands: ${GREY}start, stop, status, stats, broadcast, help, suspendback, resumeback, killback${NC}" From 1cba3aaf53c9c834b9c2ebf272e1ee641c5882f5 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 13:41:29 +0200 Subject: [PATCH 55/76] check for no arg at beginning --- bashbot.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 18b5c78..9e2217d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-53-g11b5aab +#### $$VERSION$$ v0.98-dev-54-gf1d71a0 # # Exit Codes: # - 0 sucess (hopefully) @@ -64,6 +64,8 @@ getConfigKey() { [ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1 } +BOTCOMMANDS="start, stop, status, help, init, stats, broadcast, suspendback, resumeback, killback" +[ -z "$1" ] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit if [ "$1" = "help" ]; then HELP="README" if [ -n "${CLEAR}" ];then @@ -1128,7 +1130,7 @@ if [ "${SOURCE}" != "yes" ]; then ;; *) echo -e "${RED}${REALME##*/}: unknown command${NC}" - echo -e "${ORANGE}Available commands: ${GREY}start, stop, status, stats, broadcast, help, suspendback, resumeback, killback${NC}" + echo -e "${RED}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit exit 4 ;; esac From f119c447cec56d114ab8a0c4e915e166d57a0815 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 17:39:53 +0200 Subject: [PATCH 56/76] fix 2 message typo --- bashbot.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 9e2217d..5f0a76e 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-54-gf1d71a0 +#### $$VERSION$$ v0.98-dev-55-g1cba3aa # # Exit Codes: # - 0 sucess (hopefully) @@ -418,7 +418,7 @@ sendJsonRetry(){ return ;; esac - [ "${BOTSENT[OK]}" = "true" ] && printf "%s: Retry OK: %s %s %s\n" "$(date)" "${retry}" "${1}" "${2:0:60}" + [ "${BOTSENT[OK]}" = "true" ] && printf "%s: Retry OK: %s %s %s\n" "$(date)" "${retry}" "${1}" "${2:0:60}" } >>"${ERRORLOG}" # process sendJson result @@ -1112,7 +1112,7 @@ if [ "${SOURCE}" != "yes" ]; then if kill ${BOTPID}; then # inform botadmin about stop ADMIN="$(getConfigKey "botadmin")" - [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot $(ME) stopped ..." & + [ -n "${ADMIN}" ] && send_normal_message "${ADMIN}" "Bot ${ME} stopped ..." & echo -e "${GREEN}OK. Bot stopped successfully.${NC}" else echo -e "${RED}An error occured while stopping bot.${NC}" From 4d8c2054a6bdc765ade75d0cf66f9a33f04dc00e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 18:47:18 +0200 Subject: [PATCH 57/76] fix detecting sourceing --- bashbot.sh | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5f0a76e..ab095ea 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-55-g1cba3aa +#### $$VERSION$$ v0.98-dev-56-gf119c44 # # Exit Codes: # - 0 sucess (hopefully) @@ -64,20 +64,6 @@ getConfigKey() { [ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTCONFIG}.jssh" | tail -n 1 } -BOTCOMMANDS="start, stop, status, help, init, stats, broadcast, suspendback, resumeback, killback" -[ -z "$1" ] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit -if [ "$1" = "help" ]; then - HELP="README" - if [ -n "${CLEAR}" ];then - _exists w3m && w3m "$HELP.html" && exit - _exists lynx && lynx "$HELP.html" && exit - _exists less && less "$HELP.txt" && exit - fi - cat "$HELP.txt" - exit -fi - - # get location and name of bashbot.sh SCRIPT="$0" REALME="${BASH_SOURCE[0]}" @@ -91,6 +77,19 @@ if [ "${SCRIPT}" != "${REALME}" ] || [ "$1" = "source" ]; then SOURCE="yes" fi +BOTCOMMANDS="start, stop, status, help, init, stats, broadcast, suspendback, resumeback, killback" +[[ -z "$1" && -z "${SOURCE}" ]] && echo -e "${ORANGE}Available commands: ${GREY}${BOTCOMMANDS}${NC}" && exit +if [ "$1" = "help" ]; then + HELP="README" + if [ -n "${CLEAR}" ];then + _exists w3m && w3m "$HELP.html" && exit + _exists lynx && lynx "$HELP.html" && exit + _exists less && less "$HELP.txt" && exit + fi + cat "$HELP.txt" + exit +fi + if [ -n "$BASHBOT_HOME" ]; then SCRIPTDIR="$BASHBOT_HOME" else @@ -103,7 +102,7 @@ ADDONDIR="${BASHBOT_ETC:-.}/addons" RUNUSER="${USER}" # USER is overwritten by bashbot array # OK everthing setup, lest start -if [ "${SOURCE}" != "yes" ] && [ -z "$BASHBOT_HOME" ] && ! cd "${RUNDIR}" ; then +if [[ -z "${SOURCE}" && -z "$BASHBOT_HOME" ]] && ! cd "${RUNDIR}" ; then echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}" exit 1 else @@ -256,7 +255,7 @@ export res CAPTION ################## # read commamds file if we are not sourced COMMANDS="${BASHBOT_ETC:-.}/commands.sh" -if [ "${SOURCE}" != "yes" ]; then +if [ -z "${SOURCE}" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" ls -l "${COMMANDS}" @@ -396,7 +395,7 @@ else sendUpload() { printf "%s: %s\n" "$(date)" "Sorry, wget does not support file upload\n" >>"${ERRORLOG}" BOTSENT[OK]="false" - [ "${SOURCE}" != "yes" ] && [ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "upload" "$@" & + [[ -z "${SOURCE}" && -n "${BASHBOT_EVENT_SEND[*]}" ]] && event_send "upload" "$@" & } fi @@ -984,7 +983,7 @@ fi # source the script with source as param to use functions in other scripts # do not execute if read from other scripts -if [ "${SOURCE}" != "yes" ]; then +if [ -z "${SOURCE}" ]; then ############## # internal options only for use from bashbot and developers From e4d13fd92d903364b1de4a527952a1eded604616 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 18:50:55 +0200 Subject: [PATCH 58/76] convert newlines to STRING \n when send to telegra --- modules/sendMessage.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 5144b2f..d15f7c8 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-39-g35d5e05 +#### $$VERSION$$ v0.98-dev-57-g4d8c205 # will be automatically sourced from bashbot @@ -191,8 +191,8 @@ send_message() { [ -z "$2" ] && return 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')" - # shellcheck disable=SC2001 - text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" + text="${text//*([[:space:]])mynewlinestartshere*([[:space:]])/ \\n}" + text="${text//$'\n'/\\n}" [ "$3" != "safe" ] && { no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" @@ -239,10 +239,10 @@ send_message() { send_text() { case "$2" in - html_parse_mode*) + '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}" ;; *) From 21f29a496fa9f28b6918847005ef843bae0b687b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 20:57:59 +0200 Subject: [PATCH 59/76] send_messages split messages >4096 at last newline --- modules/sendMessage.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index d15f7c8..33824ef 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-57-g4d8c205 +#### $$VERSION$$ v0.98-dev-58-ge4d13fd # will be automatically sourced from bashbot @@ -27,10 +27,19 @@ ACTION_URL=$URL'/sendChatAction' FORWARD_URL=$URL'/forwardMessage' send_normal_message() { - local text; text="$(JsonEscape "${2}")" + local len text; text="$(JsonEscape "${2}")" + text="${text//$'\n'/\\n}" until [ -z "${text}" ]; do - sendJson "${1}" '"text":"'"${text:0:4096}"'"' "${MSG_URL}" - text="${text:4096}" + if [ "${#text}" -le 4096 ]; then\ + sendJson "${1}" '"text":"'"${text}"'"' "${MSG_URL}" + unset text + else + len=4095 + [ "${text:4095:2}" != "\n" ] &&\ + len="${text:0:4096}" && len="${len%\\n*}" && len="${#len}" + sendJson "${1}" '"text":"'"${text:0:${len}}"'"' "${MSG_URL}" + text="${text:$((len+2))}" + fi done } From fa6ad3e6a8ef2148db064196a082b18124b60295 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 22:43:52 +0200 Subject: [PATCH 60/76] fix getKeyDB --- modules/jsonDB.sh | 4 ++-- modules/sendMessage.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index 411474b..b7f6421 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-48-g9adc62d +#### $$VERSION$$ v0.98-dev-59-g21f29a4 # # source from commands.sh to use jsonDB functions # @@ -106,8 +106,8 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then alias jssh_getDB=jssh_getKeyDB jssh_getKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - [ -z "${DB}" ] && return 1 local DB; DB="$(jssh_checkDB "$2")" + [ -z "${DB}" ] && return 1 # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 33824ef..cb51eaa 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-58-ge4d13fd +#### $$VERSION$$ v0.98-dev-59-g21f29a4 # will be automatically sourced from bashbot @@ -30,7 +30,7 @@ send_normal_message() { local len text; text="$(JsonEscape "${2}")" text="${text//$'\n'/\\n}" until [ -z "${text}" ]; do - if [ "${#text}" -le 4096 ]; then\ + if [ "${#text}" -le 4096 ]; then sendJson "${1}" '"text":"'"${text}"'"' "${MSG_URL}" unset text else From 30a72eb853ac18832e8f13360ea43afe30b83957 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 19 Jun 2020 23:26:52 +0200 Subject: [PATCH 61/76] examples for action on user and or chat, e.g. logging --- mycommands.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mycommands.sh b/mycommands.sh index 0231663..02b1d93 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -8,7 +8,7 @@ # #### if you start to develop your own bot, use the clean version of this file: # #### mycommands.clean # -#### $$VERSION$$ v0.98-dev-33-g18f0ace +#### $$VERSION$$ v0.98-dev-60-gfa6ad3e # # uncomment the following lines to overwrite info and help messages @@ -82,6 +82,19 @@ else fi fi + # exmaple for actions based on chat or sender + case "${USER[ID]}+${CHAT[ID]}" in + 'USERID+'*) # do something for all messages from USER + printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${USER[ID]}.log" + ;;& + *'+CHATID') # do something for all messages from CHAT + printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${CHAT[ID]}.log" + ;;& + 'USERID+CHATID') # do something only for messages form USER in CHAT + printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${CHAT[ID]}+${USER[ID]}.log" + ;;& + esac + # pre-check admin only commands case "${MESSAGE}" in # must be private, group admin, or botadmin From 8f03835ccc423d3ffde2eb5d0427727d53e81a9c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Jun 2020 10:16:12 +0200 Subject: [PATCH 62/76] harmonize background log messages --- modules/background.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index 6076f39..0157726 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-39-g35d5e05 +#### $$VERSION$$ v0.98-dev-61-g30a72eb # will be automatically sourced from bashbot @@ -42,13 +42,13 @@ killproc() { # $4 $5 parameters start_back() { local cmdfile; cmdfile="${DATADIR:-.}/$(procname "$1")$3-back.cmd" - printf "%s: Start background job CHAT=%s JOB=%s CMD=%s\n" "$(date)" "${1}" "${cmdfile##*/}" "${2} ${4} ${5}" >>"${UPDATELOG}" printf '%s\n' "$1:$3:$2" >"${cmdfile}" restart_back "$@" } restart_back() { local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-$3-")" - kill_proc "$1" "back-$3-" + printf "%s: Start background job CHAT=%s JOB=%s CMD=%s\n" "$(date)" "${1}" "${fifo##*/}" "${2} ${4} ${5}" >>"${UPDATELOG}" + check_back "$1" "$3" && kill_proc "$1" "back-$3-" nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & } @@ -60,8 +60,8 @@ start_proc() { [ -z "$2" ] && return [ -x "${2%% *}" ] || return 1 local fifo; fifo="${DATADIR:-.}/$(procname "$1")" - printf "%s: Start interacitve script CHAT=%s FIFO=%s CMD=%s\n" "$(date)" "${1}" "${fifo##*/}" "${2} ${3} ${4}" >>"${UPDATELOG}" - kill_proc "$1" + printf "%s: Start interacitve script CHAT=%s JOB=%s CMD=%s\n" "$(date)" "${1}" "${fifo##*/}" "${2} ${3} ${4}" >>"${UPDATELOG}" + check_proc "$1" && kill_proc "$1" mkfifo "${fifo}" nohup bash -c "{ $2 \"$4\" \"$5\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & @@ -97,7 +97,7 @@ kill_proc() { fifo="$(procname "$1" "$2")" prid="$(proclist "${fifo}")" fifo="${DATADIR:-.}/${fifo}" - printf "%s: Stop interacitve / background CHAT=%s FIFO/JOB=%s\n" "$(date)" "${1}" "${fifo##*/}" >>"${UPDATELOG}" + printf "%s: Stop interacitve / background CHAT=%s JOB=%s\n" "$(date)" "${1}" "${fifo##*/}" >>"${UPDATELOG}" # shellcheck disable=SC2086 [ -n "${prid}" ] && kill ${prid} [ -s "${fifo}.log" ] || rm -f "${fifo}.log" From 0b8c0476dbde1c074126541be3efe34921588684 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Jun 2020 13:19:45 +0200 Subject: [PATCH 63/76] fix replacement of \n --- modules/sendMessage.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index cb51eaa..5f6fc4c 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-59-g21f29a4 +#### $$VERSION$$ v0.98-dev-62-g8f03835 # will be automatically sourced from bashbot @@ -32,7 +32,7 @@ send_normal_message() { until [ -z "${text}" ]; do if [ "${#text}" -le 4096 ]; then sendJson "${1}" '"text":"'"${text}"'"' "${MSG_URL}" - unset text + break else len=4095 [ "${text:4095:2}" != "\n" ] &&\ @@ -200,7 +200,8 @@ send_message() { [ -z "$2" ] && return 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="${text//*([[:space:]])mynewlinestartshere*([[:space:]])/ \\n}" + #shellcheck disable=SC2001 + text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\\n/g')" text="${text//$'\n'/\\n}" [ "$3" != "safe" ] && { no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" From cac1ea44c22aee614ab9a6c4cf1bfbf9c6b6e2ee Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Jun 2020 17:30:03 +0200 Subject: [PATCH 64/76] fix make-standalone, add html doc to it --- dev/make-distribution.sh | 16 ++++++++++------ dev/make-html.sh | 13 +++++++++++++ dev/make-standalone.sh | 15 ++++++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100755 dev/make-html.sh diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index cb87687..3dc9f72 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-63-g0b8c047 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -33,10 +33,13 @@ done # create dir for distribution and copy files mkdir -p "${DISTDIR}" 2>/dev/null # shellcheck disable=SC2086 +echo "Copy files" +# shellcheck disable=SC2086 cp -r ${DISTFILES} "${DISTDIR}" cd "${DISTDIR}" || exit 1 # do not overwrite on update +echo "Create .dist files" for file in mycommands.sh bashbot.rc addons/*.sh do [ "${file}" = "addons/*.sh" ] && continue @@ -44,6 +47,7 @@ do done # dwonload JSON.sh +echo "Inject JSON.sh" JSONSHFILE="JSON.sh/JSON.sh" if [ ! -f "${JSONSHFILE}" ]; then mkdir "JSON.sh" 2>/dev/null @@ -52,17 +56,17 @@ if [ ! -f "${JSONSHFILE}" ]; then fi # make html doc -mkdir html 2>/dev/null -cp README.html html/index.html -find doc -iname "*.md" -type f -exec sh -c 'pandoc -s -f commonmark -M "title=Bashobot Documentation - ${0%.md}.html" "${0}" -o "./html/$(basename ${0%.md}.html)"' {} \; -find examples -iname "*.md" -type f -exec sh -c 'pandoc -s -f commonmark -M "title=Bashobot Documentation - ${0%.md}.html" "${0}" -o "${0%.md}.html"' {} \; -find README.html html examples -iname "*.html" -type f -exec sh -c 'sed -i -E "s/href=\"(\.\.\/)*doc\//href=\"\1html\//g;s/href=\"(.*).md(#.*)*\"/href=\"\1.html\"/g" ${0}' {} \; +echo "Create html doc" +#shellcheck disable=SC1090 +source "$GIT_DIR/../dev/make-html.sh" # create archive cd .. || exit 1 +echo "Create dist archives" zip -rq "${DISTNAME}-${VERSION}.zip" "${DISTNAME}" tar -czf "${DISTNAME}-${VERSION}.tar.gz" "${DISTNAME}" +echo "Done!" # shellcheck disable=SC2086 ls -ld ${DISTNAME}-${VERSION}.* diff --git a/dev/make-html.sh b/dev/make-html.sh new file mode 100755 index 0000000..795a1fd --- /dev/null +++ b/dev/make-html.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# magic to ensure that we're always inside the root of our application, +# no matter from which directory we'll run script +if [ ! -f README.html ]; then + echo "This script must run where README.html is!" && exit 1 +fi + +# make html doc +mkdir html 2>/dev/null +cp README.html html/index.html +find doc -iname "*.md" -type f -exec sh -c 'pandoc -s -f commonmark -M "title=Bashobot Documentation - ${0%.md}.html" "${0}" -o "./html/$(basename ${0%.md}.html)"' {} \; +find examples -iname "*.md" -type f -exec sh -c 'pandoc -s -f commonmark -M "title=Bashobot Documentation - ${0%.md}.html" "${0}" -o "${0%.md}.html"' {} \; +find README.html html examples -iname "*.html" -type f -exec sh -c 'sed -i -E "s/href=\"(\.\.\/)*doc\//href=\"\1html\//g;s/href=\"(.*).md(#.*)*\"/href=\"\1.html\"/g" ${0}' {} \; diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index d979cf3..b1b29c3 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -5,7 +5,7 @@ # If you your bot is finished you can use make-standalone.sh to create the # the old all-in-one bashbot: bashbot.sh and commands.sh only! # -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-63-g0b8c047 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -18,7 +18,7 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE/${DISTNAME}" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules LICENSE README.txt token count botacl botadmin" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules LICENSE README.* doc botacl botconfig.jssh" # run pre_commit on files dev/hooks/pre-commit.sh @@ -38,7 +38,7 @@ echo " ... create unified commands.sh" { # first head of commands.sh - sed -n '0,/^if / p' commands.sh | head -n -2 | grep -v 'mycommands.sh' + sed -n '0,/^if / p' commands.sh | head -n -2 # then mycommands from first non comment line on printf '\n##############################\n# my commands starts here ...\n' @@ -46,7 +46,7 @@ echo " ... create unified commands.sh" # last tail of commands.sh printf '\n##############################\n# default commands starts here ...\n' - sed -n '/\/mycommands.sh"/,$ p' commands.sh | tail -n +2 + sed -n '/source .*\/mycommands.sh"/,$ p' commands.sh | tail -n +2 } >>$$commands.sh @@ -79,10 +79,15 @@ sed -E -e '/(shellcheck)|(#!\/bin\/bash)/! s/^[[:space:]]*#.*//' -e 's/^[[:space sed -E -e '/(shellcheck)|(#!\/bin\/bash)/! s/^[[:space:]]*#.*//' -e 's/^[[:space:]]*//' -e '/^$/d' commands.sh > commands.sh.min chmod +x bashbot.sh.min +# make html doc +echo "Create html doc" +#shellcheck disable=SC1090 +source "$GIT_DIR/../dev/make-html.sh" + echo "Done!" cd .. || exit 1 echo -e "\\nStandalone bashbot files are now availible in \"${DISTDIR}\":\\n" -ls -l "${DISTDIR}"* +ls -l "${DISTDIR}" From 25cc9a50159ed9b0845b74660ca5b16ad57fb5bb Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Jun 2020 17:40:07 +0200 Subject: [PATCH 65/76] add scripts logs to dist --- dev/make-distribution.sh | 4 ++-- dev/make-standalone.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 3dc9f72..a6257d5 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ v0.98-dev-63-g0b8c047 +#### $$VERSION$$ v0.98-dev-64-gcac1ea4 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -17,7 +17,7 @@ VERSION="$(git describe --tags | sed -e 's/-[0-9].*//' -e 's/v//')" DISTNAME="telegram-bot-bash" DISTDIR="./DIST/${DISTNAME}" -DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean doc examples modules addons LICENSE README.md README.txt README.html" +DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh mycommands.sh.clean doc examples logs scripts modules addons LICENSE README.md README.txt README.html" # run tests first! diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index b1b29c3..3fa9f32 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -5,7 +5,7 @@ # If you your bot is finished you can use make-standalone.sh to create the # the old all-in-one bashbot: bashbot.sh and commands.sh only! # -#### $$VERSION$$ v0.98-dev-63-g0b8c047 +#### $$VERSION$$ v0.98-dev-64-gcac1ea4 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -18,7 +18,7 @@ fi #DISTNAME="telegram-bot-bash" DISTDIR="./STANDALONE/${DISTNAME}" -DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules LICENSE README.* doc botacl botconfig.jssh" +DISTFILES="bashbot.sh bashbot.rc commands.sh mycommands.sh dev/obfuscate.sh modules scripts logs LICENSE README.* doc botacl botconfig.jssh" # run pre_commit on files dev/hooks/pre-commit.sh From d52ea8c24094e30dde21bad02cf896b1140d6705 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 20 Jun 2020 20:12:36 +0200 Subject: [PATCH 66/76] avoid unintended file overwrite --- bashbot.sh | 15 ++++++++++----- modules/jsonDB.sh | 8 ++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ab095ea..61ffb88 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-56-gf119c44 +#### $$VERSION$$ v0.98-dev-65-g25cc9a5 # # Exit Codes: # - 0 sucess (hopefully) @@ -57,6 +57,7 @@ _round_float() { } setConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + [ -z "${BOTCONFIG}" ] && return 1 printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTCONFIG}.jssh" } getConfigKey() { @@ -135,7 +136,7 @@ UPDATELOG="${LOGDIR}/BASHBOT.log" if [ -z "${BOTTOKEN}" ]; then # BOTCONFIG does not exist, create [ ! -f "${BOTCONFIG}.jssh" ] && - printf '["bot_config_key"]\t"config_key_value"\n' >"${BOTCONFIG}.jssh" + printf '["bot_config_key"]\t"config_key_value"\n' >>"${BOTCONFIG}.jssh" # BOTTOKEN empty read ask user if [ -z "$(getConfigKey "bottoken")" ]; then # convert old token @@ -186,7 +187,7 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup count file if [ ! -f "${COUNTFILE}.jssh" ]; then - printf '["counted_user_chat_id"]\t"num_messages_seen"\n' > "${COUNTFILE}.jssh" + printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh" # convert old file on creation if [ -r "${COUNTFILE}" ];then sed 's/COUNT/\[\"/;s/$/\"\]\t\"1\"/' < "${COUNTFILE}" >> "${COUNTFILE}.jssh" @@ -198,7 +199,7 @@ if [ -z "${BOTTOKEN}" ]; then fi # setup blocked file if [ ! -f "${BLOCKEDFILE}.jssh" ]; then - printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >"${BLOCKEDFILE}.jssh" + printf '["blocked_user_or_chat_id"]\t"name and reason"\n' >>"${BLOCKEDFILE}.jssh" fi fi @@ -424,6 +425,7 @@ sendJsonRetry(){ # stdout is written to ERROR.log # $1 result $2 function $3 .. $n original arguments, $3 is Chat_id sendJsonResult(){ + local offset=0 BOTSENT=( ) [ -n "${BASHBOTDEBUG}" ] && printf "\n%s: New Result ==========\n%s\n" "$(date)" "$1" >>"${LOGDIR}/MESSAGE.log" BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "${1}")" @@ -439,12 +441,15 @@ sendJsonResult(){ BOTSENT[DESCRIPTION]="$(JsonGetString '"description"' <<< "${1}")" BOTSENT[RETRY]="$(JsonGetValue '"parameters","retry_after"' <<< "${1}")" else + BOTSENT[OK]="false" BOTSENT[ERROR]="999" BOTSENT[DESCRIPTION]="Send to telegram not possible, timeout/broken/no connection" fi # log error + [[ "${BOTSENT[ERROR]}" = "400" && "${BOTSENT[DESCRIPTION]}" == *"starting at byte offset"* ]] &&\ + offset="${BOTSENT[DESCRIPTION]%* }" printf "%s: RESULT=%s FUNC=%s CHAT[ID]=%s ERROR=%s DESC=%s ACTION=%s\n" "$(date)"\ - "${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" "${4:0:60}" + "${BOTSENT[OK]}" "${2}" "${3}" "${BOTSENT[ERROR]}" "${BOTSENT[DESCRIPTION]}" "${4:${offset}:100}" # warm path, do not retry on error, also if we use wegt [ -n "${BASHBOT_RETRY}${BASHBOT_WGET}" ] && return diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index b7f6421..7bace5c 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-59-g21f29a4 +#### $$VERSION$$ v0.98-dev-65-g25cc9a5 # # source from commands.sh to use jsonDB functions # @@ -206,11 +206,11 @@ jssh_checkDB(){ [ -z "$1" ] && return 1 [[ "$1" = *'..'* ]] && return 2 if [[ "$1" == "${BASHBOT_VAR:-.}"* ]] || [[ "$1" == "${BASHBOT_DATA:-.}"* ]]; then - DB="$1.jssh" + DB="${1}.jssh" else - DB="${BASHBOT_VAR:-.}/$1.jssh" + DB="${BASHBOT_VAR:-.}/${1}.jssh" fi - printf '%s' "${DB}" + [ "${DB}" != ".jssh" ] && printf '%s' "${DB}" } From 4fe969766b18cb52b7ca9b83a74330529eccc614 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 22 Jun 2020 14:10:36 +0200 Subject: [PATCH 67/76] better handling of restart jobs --- bashbot.sh | 5 +++-- modules/background.sh | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 61ffb88..fd85ca0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-65-g25cc9a5 +#### $$VERSION$$ v0.98-dev-66-gd52ea8c # # Exit Codes: # - 0 sucess (hopefully) @@ -994,7 +994,7 @@ if [ -z "${SOURCE}" ]; then # internal options only for use from bashbot and developers case "$1" in # update botname botname when starting only - "botname"|"start"*|"resume"*) + "botname"|"start"*) ME="$(getBotName)" if [ -n "${ME}" ]; then # ok we have a connection an got botname, save it @@ -1130,6 +1130,7 @@ if [ -z "${SOURCE}" ]; then # suspend, resume or kill backgrund jobs "suspendb"*|"resumeb"*|"killb"*) _is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; } + ME="$(getConfigKey "botname")" job_control "$1" ;; *) diff --git a/modules/background.sh b/modules/background.sh index 0157726..2172fbc 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-61-g30a72eb +#### $$VERSION$$ v0.98-dev-66-gd52ea8c # will be automatically sourced from bashbot @@ -50,6 +50,7 @@ restart_back() { printf "%s: Start background job CHAT=%s JOB=%s CMD=%s\n" "$(date)" "${1}" "${fifo##*/}" "${2} ${4} ${5}" >>"${UPDATELOG}" check_back "$1" "$3" && kill_proc "$1" "back-$3-" nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"; }" &>>"${fifo}.log" & + sleep 0.5 # give bg job some time to init } From 80a6dbbe9fd6170e767ae317ec6e32791524fe3a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 22 Jun 2020 18:38:15 +0200 Subject: [PATCH 68/76] more save split to array --- bashbot.sh | 11 ++--------- modules/background.sh | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index fd85ca0..00b6123 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-66-gd52ea8c +#### $$VERSION$$ v0.98-dev-67-g4fe9697 # # Exit Codes: # - 0 sucess (hopefully) @@ -839,14 +839,7 @@ process_message() { [[ "${SERVICE[*]}" =~ ^[[:blank:]]*$ ]] || SERVICE[0]="yes" # split message in command and args - CMD=( ) - if [[ "${MESSAGE[0]}" == "/"* ]]; then - set -f; unset IFS - # shellcheck disable=SC2206 - CMD=( ${MESSAGE[0]} ) - CMD[0]="${CMD[0]%%@*}" - set +f - fi + [ "${MESSAGE[0]:0:1}" = "/" ] && read -r CMD <<<"${MESSAGE[0]}" && CMD[0]="${CMD[0]%%@*}" } ######################### diff --git a/modules/background.sh b/modules/background.sh index 2172fbc..cf0d6c6 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-66-gd52ea8c +#### $$VERSION$$ v0.98-dev-67-g4fe9697 # will be automatically sourced from bashbot From afa8d874e3c4f26738e814f97d01a94fb118bfb3 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 22 Jun 2020 23:51:07 +0200 Subject: [PATCH 69/76] telegram removes keyboard if we remove text --- modules/sendMessage.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 5f6fc4c..2530b8a 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-62-g8f03835 +#### $$VERSION$$ v0.98-dev-68-g80a6dbb # will be automatically sourced from bashbot @@ -88,9 +88,9 @@ sendEmpty() { } send_keyboard() { if [[ "$3" != *'['* ]]; then old_send_keyboard "${@}"; return; fi - local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"${ISEMPTY}"'"' + local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"Keyboard:"'"' local one_time=', "one_time_keyboard":true' && [ -n "$4" ] && one_time="" - sendEmpty "${1}" "${text}"', "reply_markup": {"keyboard": [ '"${3}"' ] '"${one_time}"'}' "$MSG_URL" + sendJson "${1}" "${text}"', "reply_markup": {"keyboard": [ '"${3}"' ] '"${one_time}"'}' "$MSG_URL" # '"text":"$2", "reply_markup": {"keyboard": [ ${3} ], "one_time_keyboard": true}' } @@ -100,8 +100,8 @@ remove_keyboard() { #JSON='"text":"$2", "reply_markup": {"remove_keyboard":true}' } send_inline_keyboard() { - local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"${ISEMPTY}"'"' - sendEmpty "${1}" "${text}"', "reply_markup": {"inline_keyboard": [ '"${3}"' ]}' "$MSG_URL" + local text; text='"text":"'$(JsonEscape "${2}")'"'; [ -z "${2}" ] && text='"text":"'"Keyboard:"'"' + sendJson "${1}" "${text}"', "reply_markup": {"inline_keyboard": [ '"${3}"' ]}' "$MSG_URL" # JSON='"text":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]}' } send_button() { From 694ee6185d015392ab70ed50c09d1d50c2985303 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 23 Jun 2020 16:11:45 +0200 Subject: [PATCH 70/76] use vim spellcheck for MD --- README.html | 28 +++++++++++------------ README.md | 30 ++++++++++++------------- README.txt | 41 +++++++++++++++++---------------- doc/2_usage.md | 8 +++---- doc/3_advanced.md | 6 ++--- doc/4_expert.md | 12 +++++----- doc/5_practice.md | 10 ++++----- doc/6_reference.md | 56 +++++++++++++++++++++++----------------------- doc/7_develop.md | 6 ++--- 9 files changed, 98 insertions(+), 99 deletions(-) diff --git a/README.html b/README.html index 6862f6d..2f7f285 100644 --- a/README.html +++ b/README.html @@ -91,9 +91,9 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel

Released to the public domain wherever applicable. Elsewhere, consider it released under the WTFPLv2.

Prerequisites

Uses JSON.sh, but no more TMUX.

-

Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. More concret on the common commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes

+

Even bashbot is written in bash, it depends on commands typically available in a Unix/Linux Environment. More concret on the common commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes

Note for MacOS and BSD Users: As bashbot heavily uses modern bash and (gnu) grep/sed features, bashbot will not run without installing additional software, see Install Bashbot

-

Bashbot Documentation and Downloads are availible on www.github.com

+

Bashbot Documentation and Downloads are available on www.github.com

Documentation

  • Introdution to Telegram Bots
  • @@ -108,7 +108,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
  • Getting Started
    • Managing your Bot
    • -
    • Recieve data
    • +
    • Receive data
    • Send messages
    • Send files, locations, keyboards
  • @@ -124,7 +124,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
    • Handling UTF-8 character sets
    • Run as other user or system service
    • -
    • Scedule bashbot from Cron
    • +
    • Schedule bashbot from Cron
    • Use from CLI and Scripts
    • Customize Bashbot Environment
    @@ -132,7 +132,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
    • Customize mycommands.sh
    • Overwrite/disable commands
    • -
    • Seperate logic from commands
    • +
    • Separate logic from commands
    • Test your Bot with shellcheck
  • Function Reference @@ -143,17 +143,17 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
  • jsshDB Bashbot key-value storage
  • Background and Interactive Jobs
-
  • Deveoper Notes +
  • Devloper Notes
    • Debug bashbot
    • Modules, addons, events
    • Setup your environment
    • -
    • Bashbot testsuite
    • +
    • Bashbot test suite
  • Examples Dir
  • Your really first bashbot in a nutshell

    -

    To install and run bashbot you need acess to a linux/unix command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-(

    +

    To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix/bsd like command line you should stop reading here :-(

    In addition you need a Telegram client and a mobile phone to register an account. If you don't want to register for Telegram you should stop reading here ;-)

    After you're registered to Telegram send a message to @botfather, create a new Telegram Bot token and write it down. You need the token to install the bot.

    Now open a linux/unix/bsd terminal and check if bash is installed: which bash && echo "bash installed!". If you get an error message bash is not installed.

    @@ -176,7 +176,7 @@ It features background tasks and interactive chats, and can serve as an interfac

    For more Information on how to install, customize and use your new bot, read the Documentation

    Log files

    -

    Since version 0.96 bashbot log commands recieved/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also.

    +

    Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also.

    To enable debug mode start bashbot with debug as third argument: bashbot start debug

    ├── logs 
     │   ├── BASHBOT.log      # log what your bot is doing ...
    @@ -225,7 +225,7 @@ It features background tasks and interactive chats, and can serve as an interfac
     

    Why Bash and not the much better xyz?

    Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side:

      -
    • bashbot will run everywhere where bash is availible, from ebedded linux to mainframe
    • +
    • bashbot will run everywhere where bash is available, from ebedded linux to mainframe
    • easy to integrate with other shell script, e.g. for sending system message / health status
    • no need to install or learn a new programming language, library or framework
    • no database, not event driven, not OO ...
    • @@ -246,17 +246,17 @@ It features background tasks and interactive chats, and can serve as an interfac # send me output of a system command send_message "$(<"$BOTADMIN")" "$(df -h)"

    For more information see Expert Use

    -

    Why do I get "EXPECTED value GOT EOF" on start?

    -

    May be your IP is blocked by telegram. You can test this by running curl or wget manually:

    +

    Blocked by telegram?

    +

    This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a tor proxy on your server you may uncomment the BASHBOT_CURL_ARGS line in 'mycommands.sh'

    +

    You can test if younare blockeds by running curl or wget manually:

    -

    This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a socks or tor proxy on your server look for the BASHBOT_CURL_ARGS lines in 'mycommands.sh' as example.

    @Gnadelwartz

    That's it!

    If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

    -

    $$VERSION$$ v0.98-dev-47-gba614b7

    +

    $$VERSION$$ v0.98-dev-69-gafa8d87

    diff --git a/README.md b/README.md index 8a7569d..b30097f 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/cop ## Prerequisites Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. -Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. +Even bashbot is written in bash, it depends on commands typically available in a Unix/Linux Environment. More concret on the common commands provided by recent versions of [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands) *Note for MacOS and BSD Users:* As bashbot heavily uses modern bash and (gnu) grep/sed features, bashbot will not run without installing additional software, see [Install Bashbot](doc/0_install.md) -Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are availible on www.github.com +Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are available on www.github.com ## Documentation * [Introdution to Telegram Bots](https://core.telegram.org/bots) @@ -29,7 +29,7 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do * [Get Bottoken from Botfather](doc/1_firstbot.md) * [Getting Started](doc/2_usage.md) * Managing your Bot - * Recieve data + * Receive data * Send messages * Send files, locations, keyboards * [Advanced Features](doc/3_advanced.md) @@ -41,13 +41,13 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do * [Expert Use](doc/4_expert.md) * Handling UTF-8 character sets * Run as other user or system service - * Scedule bashbot from Cron + * Schedule bashbot from Cron * Use from CLI and Scripts * Customize Bashbot Environment * [Best Practices](doc/5_practice.md) * Customize mycommands.sh * Overwrite/disable commands - * Seperate logic from commands + * Separate logic from commands * Test your Bot with shellcheck * [Function Reference](doc/6_reference.md) * Sending Messages, Files, Keyboards @@ -55,16 +55,16 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do * Inline Queries * jsshDB Bashbot key-value storage * Background and Interactive Jobs -* [Deveoper Notes](doc/7_develop.md) +* [Devloper Notes](doc/7_develop.md) * Debug bashbot * Modules, addons, events * Setup your environment - * Bashbot testsuite + * Bashbot test suite * [Examples Dir](examples/README.md) ### Your really first bashbot in a nutshell -To install and run bashbot you need acess to a linux/unix command line. If you don't know how to get accces to a linux/unix/bsd like command line you should stop reading here :-( +To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix/bsd like command line you should stop reading here :-( In addition you need a [Telegram client](https://telegram.org) and a mobile phone to [register an account](https://telegramguide.com/create-a-telegram-account/). If you don't want to register for Telegram you should stop reading here ;-) @@ -104,7 +104,7 @@ For more Information on how to install, customize and use your new bot, read the ### Log files -Since version 0.96 bashbot log commands recieved/send and connection errors. If you start bashbot in debug mode +Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also. To enable debug mode start bashbot with debug as third argument: `bashbot start debug` @@ -190,7 +190,7 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w ### Why Bash and not the much better xyz? Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side: -- bashbot will run everywhere where bash is availible, from ebedded linux to mainframe +- bashbot will run everywhere where bash is available, from ebedded linux to mainframe - easy to integrate with other shell script, e.g. for sending system message / health status - no need to install or learn a new programming language, library or framework - no database, not event driven, not OO ... @@ -221,8 +221,10 @@ send_message "$(<"$BOTADMIN")" "$(df -h)" For more information see [Expert Use](doc/8_custom.md) -### Why do I get "EXPECTED value GOT EOF" on start? -May be your IP is blocked by telegram. You can test this by running curl or wget manually: +### Blocked by telegram? +This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in 'mycommands.sh' + +You can test if younare blockeds by running curl or wget manually: ```bash curl -m 10 https://api.telegram.org/bot #curl: (28) Connection timed out after 10001 milliseconds @@ -230,8 +232,6 @@ curl -m 10 https://api.telegram.org/bot wget -t 1 -T 10 https://api.telegram.org/bot #Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out. ``` -This may happen if to many wrong requests are sent to api.telegram.org, e.g. using a wrong token or not existing API calls. If you have a fixed IP you can ask telegram service to unblock your ip or change your IP. If you are running a socks or tor proxy on your server look for the ```BASHBOT_CURL_ARGS``` lines in 'mycommands.sh' as example. - @Gnadelwartz @@ -239,4 +239,4 @@ This may happen if to many wrong requests are sent to api.telegram.org, e.g. usi If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-47-gba614b7 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/README.txt b/README.txt index 8b24b76..1835449 100644 --- a/README.txt +++ b/README.txt @@ -15,7 +15,7 @@ Elsewhere, consider it released under the ## Prerequisites Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. -Even bashbot is written in bash, it depends on commands typically availible in +Even bashbot is written in bash, it depends on commands typically available in a Unix/Linux Environment. More concret on the common commands provided by recent versions of [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), @@ -30,7 +30,7 @@ see [Install Bashbot](doc/0_install.md) Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Downloads](https://github.com/topkecleon/telegram-bot-bash/releases) are -availible on www.github.com +available on www.github.com ## Documentation * [Introdution to Telegram Bots](https://core.telegram.org/bots) @@ -42,7 +42,7 @@ availible on www.github.com * [Get Bottoken from Botfather](doc/1_firstbot.md) * [Getting Started](doc/2_usage.md) * Managing your Bot - * Recieve data + * Receive data * Send messages * Send files, locations, keyboards * [Advanced Features](doc/3_advanced.md) @@ -54,13 +54,13 @@ availible on www.github.com * [Expert Use](doc/4_expert.md) * Handling UTF-8 character sets * Run as other user or system service - * Scedule bashbot from Cron + * Schedule bashbot from Cron * Use from CLI and Scripts * Customize Bashbot Environment * [Best Practices](doc/5_practice.md) * Customize mycommands.sh * Overwrite/disable commands - * Seperate logic from commands + * Separate logic from commands * Test your Bot with shellcheck * [Function Reference](doc/6_reference.md) * Sending Messages, Files, Keyboards @@ -68,17 +68,17 @@ availible on www.github.com * Inline Queries * jsshDB Bashbot key-value storage * Background and Interactive Jobs -* [Deveoper Notes](doc/7_develop.md) +* [Devloper Notes](doc/7_develop.md) * Debug bashbot * Modules, addons, events * Setup your environment - * Bashbot testsuite + * Bashbot test suite * [Examples Dir](examples/README.md) ### Your really first bashbot in a nutshell -To install and run bashbot you need acess to a linux/unix command line. If you -don't know how to get accces to a linux/unix/bsd like command line you should +To install and run bashbot you need access to a linux/unix command line. If you +don't know how to get access to a linux/unix/bsd like command line you should stop reading here :-( In addition you need a [Telegram client](https://telegram.org) and a mobile @@ -133,7 +133,7 @@ the [Documentation](#Documentation) ### Log files -Since version 0.96 bashbot log commands recieved/send and connection errors. If +Since version 0.96 bashbot log commands received/send and connection errors. If you start bashbot in debug mode bash stdout, stderr and all send/received telegram message are logged also. @@ -262,7 +262,7 @@ execution bug, pls update if you use an older version! Well, thats a damn good question ... may be because I'm an Unix/Linux admin from stone age. Nevertheless there are more reasons from my side: -- bashbot will run everywhere where bash is availible, from ebedded linux to +- bashbot will run everywhere where bash is available, from ebedded linux to mainframe - easy to integrate with other shell script, e.g. for sending system message / health status @@ -303,9 +303,14 @@ send_message "$(<"$BOTADMIN")" "$(df -h)" For more information see [Expert Use](doc/8_custom.md) -### Why do I get "EXPECTED value GOT EOF" on start? -May be your IP is blocked by telegram. You can test this by running curl or -wget manually: +### Blocked by telegram? +This may happen if to many wrong requests are sent to api.telegram.org, e.g. +using a wrong token or not existing API calls. If you have a fixed IP you can +ask telegram service to unblock your ip or change your IP. If you are running a +tor proxy on your server you may uncomment the ```BASHBOT_CURL_ARGS``` line in +'mycommands.sh' + +You can test if younare blockeds by running curl or wget manually: ```bash curl -m 10 https://api.telegram.org/bot #curl: (28) Connection timed out after 10001 milliseconds @@ -314,12 +319,6 @@ wget -t 1 -T 10 https://api.telegram.org/bot #Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out. ``` -This may happen if to many wrong requests are sent to api.telegram.org, e.g. -using a wrong token or not existing API calls. If you have a fixed IP you can -ask telegram service to unblock your ip or change your IP. If you are running a -socks or tor proxy on your server look for the ```BASHBOT_CURL_ARGS``` lines -in 'mycommands.sh' as example. - @Gnadelwartz @@ -328,4 +327,4 @@ in 'mycommands.sh' as example. If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.98-dev-47-gba614b7 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/2_usage.md b/doc/2_usage.md index 067a5cd..92d1043 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -1,5 +1,5 @@ #### [Home](../README.md) -## Gettting Started +## Getting Started The Bots standard commands are in the commands dispatcher ```commands.sh```, Do not edit this file! Add your commands and functions to ```mycommands.sh```. In 'mycommands.sh.dist' you find examples how to add own commands and overwrite existing ones. See [Best practices](5_practice.md) for more information. @@ -95,7 +95,7 @@ To send a broadcast to all of users that ever used the bot run the following com ---- -## Recieve data +## Receive data Evertime a Message is received, you can read incoming data using the following variables: ### Regular Messages @@ -163,7 +163,7 @@ The following variables are set if the message contains optional parts: Service Messages are regular messages not itended for end users, instead they signal special events to the client, e.g. new users. -If a service message is recieved bashbot sets MESSAGE to the service message type as a command, +If a service message is received bashbot sets MESSAGE to the service message type as a command, e.g. if a new user joins a chat MESSAGE is set to "/new_chat_user". * ```$SERVICE```: This array contains info abbout recived service messages. @@ -281,5 +281,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.98-dev-47-gba614b7 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index deafadc..7fa3ddc 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -41,7 +41,7 @@ In addition you can check individual capabilities of users as you must define in 987654321:start:98979695 # * are only allowed on the right hand side and not for user! -# the following exaples are NOT valid! +# the following examples are NOT valid! *:*:* *:start:* *:*:98979695 @@ -226,7 +226,7 @@ massive aoumnt of messages. By default bashbot detects if a message is not sent e.g. resend on throttling. In addtion every send error is logged in logs/ERROR.log -#### Trasmission results +#### Transmission results On every message send to telegram (transmission) the results are provided in bash variables, like its done when a new message is received. @@ -289,5 +289,5 @@ Note: If you disable automatic retry, se above, you disable also connection prob #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/4_expert.md b/doc/4_expert.md index 11caee9..ac93a8f 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -50,7 +50,7 @@ Most complete support for decoding of multibyte characters can only be provided ### Run as other user or system service -Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or sceduled from cron. This is onyl recommended for experiend linux users. +Bashbot is desingned to run manually by the user who installed it. Nevertheless it's possible to run it by an other user-ID, as a system service or scheduled from cron. This is onyl recommended for experiend linux users. Setup the environment for the user you want to run bashbot and enter desired username, e.g. nobody : ```bash @@ -95,7 +95,7 @@ To change back the environment to your user-ID run ```sudo ./bashbot.sh init``` To use bashbot as a system servive include a working ```bashbot.rc``` in your init system (systemd, /etc/init.d). -### Scedule bashbot from Cron +### Schedule bashbot from Cron An example crontab is provided in ```examples/bashbot.cron```. - If you are running bashbot with your user-ID, copy the examples lines to your crontab and remove username ```nobody```. @@ -147,7 +147,7 @@ BOTTOKEN # default: content of ${TOKENFILE} URL # telegram api URL - default: https://api.telegram.org/bot${BOTTOKEN}" ``` -#### Interacctive use +#### Interactive use For testing your setup or sending messages yoursel you can use bashbot functions from bash command line: ```bash # are we running bash? @@ -206,7 +206,7 @@ This section describe how you can customize bashbot to your needs by setting env #### Change file locations In standard setup bashbot is self containing, this means you can place 'telegram-bot-bash' any location -and run it from there. All files - programm, config, data etc - will reside in 'telegram-bot-bash'. +and run it from there. All files - program, config, data etc - will reside in 'telegram-bot-bash'. If you want to have other locations for config, data etc, define and export the following environment variables. **Note: all specified directories and files must exist or running 'bashbot.sh' will fail.** @@ -338,7 +338,7 @@ for every poll until the maximum of BASHBOT_SLEEP ms. ``` -#### Testet configs as of v0.90 release +#### Tested configs as of v0.90 release **Note: Environment variables are not stored, you must setup them before every call to bashbot.sh, e.g. from a script.** ##### simple Unix like config, for one bot. bashbot is installed in '/usr/local/telegram-bot-bash' @@ -382,5 +382,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Advanced Use](3_advanced.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/5_practice.md b/doc/5_practice.md index 0731e75..f0e8b46 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -7,7 +7,7 @@ If you are new to Bot development read [Bots: An introduction for developers](ht In addition you should know about [BotFather, the one bot to rule them all](https://core.telegram.org/bots#3-how-do-i-create-a-bot). It will help you create new bots and change settings for existing ones. [Commands known by Botfather](https://core.telegram.org/bots#generating-an-authorization-token) -If you dont't have a github account, it may time to [sepup a free account now](https://github.com/pricing) +If you don't have a github account, it may time to [setup a free account now](https://github.com/pricing) ### Add commands to mycommands.sh only To ease updates never change ```bashbot.sh```, instead your commands and functions must go to ```mycommands.sh``` . Insert your Bot commands in the ```case ... esac``` block of the 'mycommands()' function: @@ -67,12 +67,12 @@ In case you want to add some processing to the global bashbot command add ```ret ``` -### Seperate logic from commands +### Separate logic from commands -If a command need more than 2-3 lines of code, you should use a function to seperate logic from command. Place your functions in ```mycommands.sh``` and call the from your command. Example: +If a command need more than 2-3 lines of code, you should use a function to separate logic from command. Place your functions in ```mycommands.sh``` and call the from your command. Example: ```bash # file: mycommands.sh -# your additional bahsbot commands +# your additional bashbot commands mycommands() { @@ -152,5 +152,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/6_reference.md b/doc/6_reference.md index b5c7825..0515210 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -126,7 +126,7 @@ send_file "${CHAT[ID]}" "https://www.domain,com/something.gif" "Something" ---- ##### send_keyboard -Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Example Keybord Array definitions: +Note: since version 0.6 send_keyboard was changed to use native "JSON Array" notation as used from Telegram. Example Keyboard Array definitions: - yes no in two rows: - OLD format: 'yes' 'no' (two strings) @@ -134,7 +134,7 @@ Note: since version 0.6 send_keyboard was changed to use native "JSON Array" not - new layouts made easy with NEW format: - Yes No in one row: '[ "yes" , "no" ]' - Yes No plus Maybe in 2.row: '[ "yes" , "no" ] , [ "maybe" ]' - - numpad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' + - number pad style keyboard: '[ "1" , "2" , "3" ] , [ "4" , "5" , "6" ] , [ "7" , "8" , "9" ] , [ "0" ]' *usage:* send_keyboard "chat-id" "message" "keyboard" @@ -175,7 +175,7 @@ This allows to place multiple inline buttons in a row. The inline buttons must s ```[ {"text":"text1", "url":"url1"}, ... {"text":"textN", "url":"urlN"} ]``` -Each button consists of a pair of text and URL values, sourrounded by '{ }', multiple buttons are seperated by '**,**' and everthing is wrapped in '[ ]'. +Each button consists of a pair of text and URL values, sourrounded by '{ }', multiple buttons are separated by '**,**' and everthing is wrapped in '[ ]'. *usage:* send_inline_keyboard "chat-id" "message" "[ {"text":"text", "url":"url"} ...]" @@ -204,7 +204,7 @@ If your Bot is a chat admin he can kick and ban a user. *alias:* _kick_user "${USER[ID]}" ##### unban_chat_member -If your Bot is a chat admine can unban a kicked user. +If your Bot is a chat admin can unban a kicked user. *usage:* unban_chat_member "${CHAT[ID]}" "${USER[ID]}" @@ -295,7 +295,7 @@ It send back only one response to an inline query. ##### answer_inline_multi -anser_inline_multi allows you to send back a list of responses. responses must be seperated by ','. +anwser_inline_multi allows you to send back a list of responses. Responses must be separated by ','. *usage:* answer_inline_multi "${iQUERY[ID]}" "res, res, ... res" @@ -351,7 +351,7 @@ see [InlineQueryResult for more information](https://core.telegram.org/bots/api# ### Background and Interactive jobs -Background functions and interactive jobs extends the bot functionality to not only react to user input. You can start scripts for interative +Background functions and interactive jobs extends the bot functionality to not only react to user input. You can start scripts for interactive chats and send messages based on time or other external events. ##### start_proc @@ -402,7 +402,7 @@ fi ---- ##### start_back -Starts a script as a background job and attaches a jobname to it. All output from a background job is sent to the associated chat. +Starts a script as a background job and attaches a job name to it. All output from a background job is sent to the associated chat. In contrast to interactive chats, background jobs do not receive user input and can run forever. In addition you can suspend and restart running jobs, e.g. after reboot. @@ -453,7 +453,7 @@ fi ##### send_interactive Form version 0.80 on forward_message is used to forward messages to interactive job. It replaces the old 'inproc' commands used for TMUX. -Usually message is automatically forwarded in 'commands.sh', but you can forward messages wihle processing also or send your own messages. +Usually a message is automatically forwarded in 'commands.sh', but you can forward messages while processing also or send your own messages. *usage:* send_interactive "${CHAT[ID]}" "message" @@ -463,17 +463,17 @@ Usually message is automatically forwarded in 'commands.sh', but you can forwar ### jsshDB Since output generated by JSON.sh is so handy to use in bash, we use the format for a simple keys/value file store. -The file extions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA.. +The file extentions is '.jssh' and for security reasons location of jssh files is restricted to BASHBOT_ETC and BASHBOT_DATA.. #### fast and slow operations jsshDB files are simple text files and if you append a new Key/value pairs to the end of the file it overwrites -an existing key/value pair. We use this behaivor for "fast" file operations. +an existing key/value pair. We use this behavior for "fast" file operations. -"fast funtions" add a new key/value pair to the end of the file without deleting an existing one, this is fast but over (long) -time the file grows infinitly. +"fast functions" add a new key/value pair to the end of the file without deleting an existing one, this is fast but over (long) +time the file grows infinity. -"slow funtions" in contrast modify the key/value pairs in place and write the whole file back, +"slow functions" in contrast modify the key/value pairs in place and write the whole file back, this is slower but clean up the file. All previously added key/value pairs are replaced and only the last one is written back to the file. @@ -499,7 +499,7 @@ A jssh fileDB consists of two files which must reside inside BASHBOT_ETC or BAS Path names containing `..` or not located in BASHBOT_ETC or BASHBOT_DATA are refused by jsshDB functions with an error. -Since version 0.94 jsshDB functions support file locking with flock. write/update operations are serialised with flock to wait until +Since version 0.94 jsshDB functions support file locking with flock. Write/update operations are serialised with flock to wait until previous operations are finished, see "man flock" for information. To avoid deadlocks we use a timeout of 10s for write and 5s for read operations. Every jssh_*DB function exist as jssj_*DB_async also. @@ -592,7 +592,7 @@ cat "${DATADIR:-}/myvalues.jssh" ``` ##### jssh_printDB -Print content of an ARRAY to STDOUT. ARRAY name must be delared with "declare -A ARRAY" before calling printDB.. +Print content of an ARRAY to STDOUT. ARRAY name must be declared with "declare -A ARRAY" before calling printDB.. *usage:* jssh_printDB "ARRAY" @@ -614,7 +614,7 @@ jssh_printDB READVALUES ``` ##### jssh_updateDB -Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be delared with "declare -A ARRAY" before calling updateDB. +Update/Add content of an ARRAY into a jsshDB file. ARRAY name must be declared with "declare -A ARRAY" before calling updateDB. "DB" file MUST exist or nothing is written. Note: Existing content not in ARRAY is kept in file. @@ -649,14 +649,14 @@ cat "$DBfile" ``` ##### jssh_readDB -Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be delared with "declare -A ARRAY" upfront, +Read content of a file in JSON.sh format into given ARRAY. ARRAY name must be declared with "declare -A ARRAY" upfront, *usage:* jssh_readDB "ARRAY" "filename" *usage:* jssh_readDB_async "ARRAY" "filename" -Note: readDB uses concurrent / shared locking from flock so multiple proceses can read from file, as long no process is writing. -Maximum timeour for reading is 1s to not block readers. +Note: readDB uses concurrent / shared locking from flock so multiple processes can read from file, as long no process is writing. +Maximum timeout for reading is 1s to not block readers. *example:* ```bash @@ -720,7 +720,7 @@ jssh_insertKeyDB "newkey" "an other value" "${DATADIR:-.}/myvalues" ``` ##### jssh_deleteKeyDB -Deleted a key=value pair froma jsshDB file, key name is only allowed to contain '-a-zA-Z0-9,._' +Deleted a key=value pair from a jsshDB file, key name is only allowed to contain '-a-zA-Z0-9,._' *usage:* jssh_deleteKeyDB "key" "filename" @@ -754,7 +754,7 @@ https://linuxconfig.org/how-to-use-arrays-in-bash-script ---- -### Aliases - shortcuts for often used funtions +### Aliases - shortcuts for often used functions Aliases are handy shortcuts for using in 'mycommands.sh', they avoid error prone typing of "${CHAT[ID]}" "${USER[ID]}" as much as possible. Do not use them in bashbot.sh, modules and addons. @@ -855,7 +855,7 @@ Do not use them in bashbot.sh, modules and addons. ##### download Download the fiven URL ans returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a -random number. filename is not allowed to contain '/' or '..'. +random number. Filename is not allowed to contain '/' or '..'. *usage:* download URL filename @@ -986,7 +986,7 @@ Reads JSON fro STDIN and Outputs found Value to STDOUT ##### Json2Array -Read JSON.sh style data from STDIN and asssign to given ARRAY +Read JSON.sh style data from STDIN and assign to given ARRAY ARRAY name must be declared with "declare -A ARRAY" before calling *usage:* Json2Array "ARRAY" @@ -1005,13 +1005,13 @@ Output ARRAY as JSON.sh style data to STDOUT ---- ##### process_client -Every Message sent to your Bot is processd by this function. It parse the send JSON and assign the found Values to bash variables. +Every Message sent to your Bot is processed by this function. It parse the send JSON and assign the found Values to bash variables. ##### process_updates -If new updates are availible, this functions gets the JSON from Telegram and dispatch it. +If new updates are available, this functions gets the JSON from Telegram and dispatch it. ##### process_inline -Every Inline Message sent to your Bot is processd by this function. It parse the send JSON and assign the found Values to bash variables. +Every Inline Message sent to your Bot is processed by this function. It parse the send JSON and assign the found Values to bash variables. ##### start_timer Start the the every minute timer ... @@ -1028,12 +1028,12 @@ Dispatcher for BASHBOT_EVENT_MESSAGE and related ---- ##### getBotName -The name of your bot is availible as bash variable "$ME", there is no need to call this function if Bot is running. +The name of your bot is available as bash variable "$ME", there is no need to call this function if Bot is running. *usage:* ME="$(getBotName)" #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 diff --git a/doc/7_develop.md b/doc/7_develop.md index a3324f3..e3559f5 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -283,7 +283,7 @@ For a shell script running as a service it's important to be paranoid about quot To run shellcheck for a single script run ```shellcheck -x script.sh```, to check all schripts run ```dev/hooks/pre-commit.sh```. -### bashbot tests +### bashbot test suite Starting with version 0.70 bashbot has a test suite. To start testsuite run ```dev/all-tests.sh```. all-tests.sh will return 'SUCCESS' only if all tests pass. #### enabling / disabling tests @@ -302,7 +302,7 @@ A temporary test environment is created when 'ALL-tests.sh' starts and deleted a The file ```ALL-tests.inc.sh``` must be included from all tests and provide the test environment as shell variables: ```bash -# Test Evironment +# Test Environment TESTME="$(basename "$0")" DIRME="$(pwd)" TESTDIR="$1" @@ -347,5 +347,5 @@ fi #### [Prev Function Reference](6_reference.md) -#### $$VERSION$$ v0.96-0-g3871ca9 +#### $$VERSION$$ v0.98-dev-69-gafa8d87 From d2a578a75788bdd244523ddcb0d6a236fcfb7986 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 23 Jun 2020 16:35:50 +0200 Subject: [PATCH 71/76] codespell is much better --- README.html | 22 ++++---- README.md | 22 ++++---- README.txt | 26 +++++----- addons/antiFlood.sh | 4 +- addons/example.sh | 12 ++--- bashbot.rc | 4 +- bashbot.sh | 50 +++++++++---------- commands.sh | 8 +-- db.json | 2 +- dev/make-standalone.sh | 4 +- doc/0_install.md | 10 ++-- doc/2_usage.md | 20 ++++---- doc/3_advanced.md | 26 +++++----- doc/4_expert.md | 28 +++++------ doc/5_practice.md | 6 +-- doc/6_reference.md | 46 ++++++++--------- doc/7_develop.md | 42 ++++++++-------- examples/README.md | 8 +-- examples/background-scripts/mycommands.sh | 2 +- examples/background-scripts/run_diskusage.sh | 4 +- .../background-scripts/run_filecontent.sh | 6 +-- examples/background-scripts/run_filename.sh | 4 +- examples/background-scripts/run_notify.sh | 4 +- examples/bashbot-multi.sh | 10 ++-- examples/bashbot.cron | 4 +- examples/calc.sh | 4 +- examples/notify.sh | 4 +- examples/question.sh | 4 +- examples/send-system-status/botacl | 6 +-- examples/send-system-status/mycommands.sh | 4 +- modules/answerInline.sh | 6 +-- modules/background.sh | 6 +-- modules/jsonDB.sh | 34 ++++++------- modules/sendMessage.sh | 4 +- mycommands.sh | 18 +++---- mycommands.sh.clean | 12 ++--- scripts/interactive.sh.clean | 4 +- test/ADD-test-new.sh | 4 +- test/c-init-test.sh | 4 +- test/d-process_inline-test.sh | 6 +-- test/e-env-test.sh | 6 +-- 41 files changed, 250 insertions(+), 250 deletions(-) diff --git a/README.html b/README.html index 2f7f285..90f2a8a 100644 --- a/README.html +++ b/README.html @@ -143,7 +143,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
  • jsshDB Bashbot key-value storage
  • Background and Interactive Jobs
  • -
  • Devloper Notes +
  • Developer Notes