From bccd064516662130cc4178e14997cfa5f1060930 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 18 May 2019 20:25:18 +0200 Subject: [PATCH 01/20] start dev3 - remove tmux --- README.html | 2 +- README.md | 2 +- README.txt | 2 +- bashbot.rc | 2 +- bashbot.sh | 31 ++---- commands.sh | 4 +- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 2 +- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 2 +- doc/7_develop.md | 2 +- doc/8_custom.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- .../background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 2 +- modules/background.sh | 101 ++++++++++++++---- modules/chatMember.sh | 2 +- modules/sendMessage.sh | 2 +- mycommands.sh | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 53 files changed, 140 insertions(+), 96 deletions(-) diff --git a/README.html b/README.html index 4d7dfc4..ae2c52f 100644 --- a/README.html +++ b/README.html @@ -107,6 +107,6 @@

@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.76-1-ge8a1fd0

+


VERSION
v0.80-dev3-0-g31a5d00

diff --git a/README.md b/README.md index b27c8ae..c4184ec 100644 --- a/README.md +++ b/README.md @@ -107,4 +107,4 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/README.txt b/README.txt index b394f5b..f394c59 100644 --- a/README.txt +++ b/README.txt @@ -155,4 +155,4 @@ health status If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/bashbot.rc b/bashbot.rc index 19d1837..908f863 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 3e7410e..79bc85b 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # # Exit Codes: # - 0 sucess (hopefully) @@ -58,7 +58,6 @@ if [ ! -f "${TOKENFILE}" ]; then echo "Running headless, run ${SCRIPT} init first!" exit 2 else - ${CLEAR} echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token @@ -72,7 +71,6 @@ if [ ! -f "${BOTADMIN}" ]; then echo "Running headless, set botadmin to AUTO MODE!" echo '?' > "${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}" @@ -92,7 +90,6 @@ TMPDIR="${BASHBOT_VAR:-.}/data-bot-bash" if [ ! -d "${TMPDIR}" ]; then mkdir "${TMPDIR}" elif [ ! -w "${TMPDIR}" ]; then - ${CLEAR} echo -e "${RED}ERROR: Can't write to ${TMPDIR}!.${NC}" ls -ld "${TMPDIR}" exit 2 @@ -102,7 +99,6 @@ COUNTFILE="${BASHBOT_VAR:-.}/count" if [ ! -f "${COUNTFILE}" ]; then echo "" >"${COUNTFILE}" elif [ ! -w "${COUNTFILE}" ]; then - ${CLEAR} echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" ls -l "${COUNTFILE}" exit 2 @@ -124,7 +120,6 @@ export res BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE COMMANDS="${BASHBOT_ETC:-.}/commands.sh" if [ "$1" != "source" ]; then if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then - ${CLEAR} echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}" ls -l "${COMMANDS}" exit 3 @@ -429,11 +424,11 @@ if [ "$1" != "source" ]; then "outproc") # forward output from interactive and jobs to chat [ "$3" = "" ] && echo "No file to read from" && exit 3 [ "$2" = "" ] && echo "No chat to send to" && exit 3 - until [ "$line" = "imprettydarnsuredatdisisdaendofdacmd" ];do + while true ;do line="" read -r -t 10 line - [ "$line" != "" ] && [ "$line" != "imprettydarnsuredatdisisdaendofdacmd" ] && send_message "$2" "$line" - done <"${TMPDIR:-.}/$3" + [ "$line" != "" ] && send_message "$2" "$line" + done rm -f -r "${TMPDIR:-.}/$3" exit ;; @@ -470,18 +465,15 @@ if [ "$1" != "source" ]; then while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" ;; "start") - ${CLEAR} tmux kill-session -t "$ME" &>/dev/null tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}" ;; "kill") - ${CLEAR} tmux kill-session -t "$ME" &>/dev/null echo -e "${GREEN}OK. Bot stopped successfully.${NC}" ;; "background" | "resumeback") - ${CLEAR} echo -e "${GREEN}Restart background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then @@ -492,33 +484,30 @@ if [ "$1" != "source" ]; then JOB="${RESTART#*:}" PROG="${JOB#*:}" JOB="${JOB%:*}" - fifo="back-${JOB}-${ME}_${CHAT[ID]}" # compose fifo from jobname, $ME (botname) and CHAT[ID] + fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" echo "restartbackground ${PROG} ${fifo}" - ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null - mkfifo "${TMPDIR:-.}/${fifo}" - tmux new-session -d -s "${fifo}" "${PROG} &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" - tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" + start_back "${CHAT[ID]}" "${PROG}" "${JOB}" fi done ;; "killback" | "suspendback") - ${CLEAR} echo -e "${GREEN}Stopping background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes.${NC}"; break else REMOVE="$(< "${FILE}")" + CHAT[ID]="${RESTART%%:*}" JOB="${REMOVE#*:}" - fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}" + JOB="${JOB%:*}" + fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" echo "killbackground ${fifo}" [ "$1" = "killback" ] && rm -f "${FILE}" # remove job - ( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null + kill_proc "${CHAT[ID]}" "back-${JOB}" fi done ;; "help") - ${CLEAR} less "README.txt" exit ;; diff --git a/commands.sh b/commands.sh index 2654af7..129c378 100644 --- a/commands.sh +++ b/commands.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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # # adjust your language setting here, e.g.when run from other user or cron. @@ -107,7 +107,7 @@ if [ "${1}" != "source" ];then if [ "$res" -eq 0 ] ; then killproc && _message "Command canceled.";else _message "No command is currently running.";fi ;; *) # forward messages to optional dispatcher - _is_function startproc && if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi # interactive running + _is_function forward_interactive && forward_interactive "${CHAT[ID]}" "${MESSAGE}" # interactive running _is_function mycommands && mycommands ;; esac diff --git a/dev/all-tests.sh b/dev/all-tests.sh index bc99493..7739b76 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/git-add.sh b/dev/git-add.sh index aeb99aa..249bd60 100755 --- a/dev/git-add.sh +++ b/dev/git-add.sh @@ -3,7 +3,7 @@ # # works together with git pre-push.sh and ADD all changed files since last push -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index bf19b43..38f5729 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index e1e1610..b50de77 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index d510bf2..7c58fe0 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index cec7784..7f1253a 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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 7fe348f..7d49f07 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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index 0b3c8a0..5bf5b1e 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,3 +1,3 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 bashbot.rc diff --git a/dev/version.sh b/dev/version.sh index 7340d72..c4851f6 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/0_install.md b/doc/0_install.md index 674b542..47b1f7b 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -70,5 +70,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index 1884726..deb4785 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -65,5 +65,5 @@ group. This step is up to you actually. #### [Prev Installation](0_install.md) #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/2_usage.md b/doc/2_usage.md index 28aa312..3f9d780 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -182,5 +182,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 1360d1c..0ae7851 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -180,5 +180,5 @@ See also [answer_inline_multi, answer_inline_compose](6_reference.md#answer_inli #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/4_expert.md b/doc/4_expert.md index adace28..6f7f6c0 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/5_practice.md b/doc/5_practice.md index 9e717e5..fc69778 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -153,5 +153,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/6_reference.md b/doc/6_reference.md index a3359f0..4837f76 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -592,5 +592,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/7_develop.md b/doc/7_develop.md index 55bc350..112bf6b 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -188,5 +188,5 @@ fi #### [Prev Function Reference](6_reference.md) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/doc/8_custom.md b/doc/8_custom.md index 04db87a..ebb2f3b 100644 --- a/doc/8_custom.md +++ b/doc/8_custom.md @@ -131,5 +131,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/examples/README.md b/examples/README.md index 45fa8eb..11426f6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -55,6 +55,6 @@ convert existing bots. **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index 4c756f3..043ae6c 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -4,7 +4,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index fa5ab5c..826c33c 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 3c070fb..86911fa 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index e019204..1f81f75 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -4,7 +4,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot-multi.sh b/examples/bashbot-multi.sh index 61cdde6..293eb78 100755 --- a/examples/bashbot-multi.sh +++ b/examples/bashbot-multi.sh @@ -2,7 +2,7 @@ # file. multibot.sh # description: run multiple telegram bots from one installation # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then echo "Usage: $0 botname command" diff --git a/examples/bashbot.cron b/examples/bashbot.cron index df9b95f..d727ca6 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 4c47dd0..2c79621 100755 --- a/examples/calc.sh +++ b/examples/calc.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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index 976548f..45e2d14 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -4,7 +4,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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index ee74b57..a7ca0c7 100755 --- a/examples/question.sh +++ b/examples/question.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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl index 9f41c1f..2c20ed9 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,7 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # Format: # user:ressource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 64b5180..7c9b946 100644 --- a/examples/send-system-status/mycommands.sh +++ b/examples/send-system-status/mycommands.sh @@ -5,7 +5,7 @@ # to show how you can customize bashbot by only editing mycommands.sh # NOTE: this is not tested, simply copied from original source and reworked! # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/modules/aliases.sh b/modules/aliases.sh index 3ead78f..5781952 100644 --- a/modules/aliases.sh +++ b/modules/aliases.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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # # source from commands.sh to use the aliases diff --git a/modules/answerInline.sh b/modules/answerInline.sh index 3fde398..fcdae5f 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # source from commands.sh to use the inline functions diff --git a/modules/background.sh b/modules/background.sh index d1145d6..c6d3221 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,48 +5,103 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # source from commands.sh if you want ro use interactive or background jobs ###### # interactive and background functions +# old syntax as aliases background() { - echo "${CHAT[ID]}:$2:$1" >"${TMPDIR:-.}/${copname:--}$2-back.cmd" - startproc "$1" "back-$2-" + start_back "${CHAT[ID]}" "$1" "$2" } - startproc() { - killproc "$2" - local fifo="$2${copname}" - mkfifo "${TMPDIR:-.}/${fifo}" - tmux new-session -d -s "${fifo}" "$1 &>${TMPDIR:-.}/${fifo}; echo imprettydarnsuredatdisisdaendofdacmd>${TMPDIR:-.}/${fifo}" - tmux new-session -d -s "sendprocess_${fifo}" "bash $SCRIPT outproc ${CHAT[ID]} ${fifo}" + start_proc "${CHAT[ID]}" "$1" "$2" } - - checkback() { - checkproc "back-$1-" + check_back "${CHAT[ID]}" "$1" +} +checkproc() { + check_proc "${CHAT[ID]}" "$1" +} +killback() { + kill_back "${CHAT[ID]}" "$1" +} +killproc() { + kill_proc "${CHAT[ID]}" "$1" } -checkproc() { - tmux ls | grep -q "$1${copname}" +# internal functions +# $1 chatid +# $2 prefix +fifoname(){ + echo "$2${ME}_$1" +} + +# $1 pipename +listproc() { + # shellcheck disable=SC2009 + ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 +} + +# inline and backgound functions +# $1 chatid +# $2 program +# $3 jobname +start_back() { + local fifo; fifo="$(fifoname "$1")" + echo "$1:$3:$2" >"${TMPDIR:-.}/${fifo}$3-back.cmd" + start_proc "$1" "$2" "back-$3-" +} + + +# $1 chatid +# $2 program +# $3 prefix +start_proc() { + [ "$2" = "" ] && return + kill_proc "$1" "$3" + local fifo; fifo="$(fifoname "$1" "$3")" + mkfifo "${TMPDIR:-.}/${fifo}" + ( $2 <"${TMPDIR:-.}/${fifo}" | "${SCRIPT}" outproc "${1}" "${fifo}"; ) &>>"${TMPDIR:-.}/${fifo}.log" & + disown -a +} + + +# $1 chatid +# $2 jobname +check_back() { + check_proc "$1" "back-$2-" +} + +# $1 chatid +# $2 prefix +check_proc() { + [ "$(listproc "$(fifoname "$1" "$2")")" != "" ] # shellcheck disable=SC2034 res=$?; return $? } -killback() { - killproc "back-$1-" - rm -f "${TMPDIR:-.}/${copname}$1-back.cmd" +# $1 chatid +# $2 jobname +kill_back() { + kill_proc "$1" "back-$2-" + rm -f "${TMPDIR:-.}/$(fifoname "$1")$2-back.cmd" } -killproc() { - local fifo="$1${copname}" - (tmux kill-session -t "${fifo}"; echo imprettydarnsuredatdisisdaendofdacmd>"${TMPDIR:-.}/${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}")2>/dev/null + +# $1 chatid +# $2 prefix +kill_proc() { + local fifo; fifo="$(fifoname "$1" "$2")" + kill -15 "$(listproc "${fifo}")" 2>/dev/null + rm -f -r "${TMPDIR:-.}/${fifo}"; } -inproc() { - tmux send-keys -t "$copname" "${MESSAGE[0]} ${URLS[*]} -" +# $1 chat +# $2 message +forward_interactive() { + local fifo; fifo="$(fifoname "$1")" + [ -p "${fifo}" ] && echo "$2" >"${fifo}" } diff --git a/modules/chatMember.sh b/modules/chatMember.sh index 09148df..d2ed8d9 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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # source from commands.sh to use the member functions diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 59e57e5..cb9999f 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.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # source from commands.sh to use the sendMessage functions diff --git a/mycommands.sh b/mycommands.sh index fb00731..33d8edf 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -2,7 +2,7 @@ # files: mycommands.sh.dist # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # # uncomment the following lines to overwrite info and help messages diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index 70bbf47..d097145 100755 --- a/test/ADD-test-new.sh +++ b/test/ADD-test-new.sh @@ -2,7 +2,7 @@ # # ADD a new test skeleton to test dir, but does not activate test # -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 56e068a..8208922 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 31b7ae6..c0a19e5 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 ../dev/hooks/pre-commit.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh index 7f34def..d50cc87 100644 --- a/test/b-example-test.sh +++ b/test/b-example-test.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # file: b-example-test.sh -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index 1e65619..b440572 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-JSON.sh-test.sh b/test/d-JSON.sh-test.sh index 70be42f..9a45a95 100755 --- a/test/d-JSON.sh-test.sh +++ b/test/d-JSON.sh-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_inline-test.sh b/test/d-process_inline-test.sh index 3c36f23..46a6e30 100755 --- a/test/d-process_inline-test.sh +++ b/test/d-process_inline-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 6b13f25..5efc4ed 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 1f7759b..750be37 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index 7c2b750..901ccbd 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/e-env-test.sh b/test/e-env-test.sh index 5f03074..d9aefdd 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.76-1-ge8a1fd0 +#### $$VERSION$$ v0.80-dev3-0-g31a5d00 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh From a1a823b4b61288d97fdb6908886654c1e6bb56a8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 18 May 2019 21:06:29 +0200 Subject: [PATCH 02/20] fix background execution --- bashbot.sh | 7 +++---- examples/question.sh | 3 ++- modules/background.sh | 21 +++++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 79bc85b..0c6b982 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-1-gbccd064 # # Exit Codes: # - 0 sucess (hopefully) @@ -424,12 +424,11 @@ if [ "$1" != "source" ]; then "outproc") # forward output from interactive and jobs to chat [ "$3" = "" ] && echo "No file to read from" && exit 3 [ "$2" = "" ] && echo "No chat to send to" && exit 3 - while true ;do - line="" - read -r -t 10 line + while read -r -t 10 line ;do [ "$line" != "" ] && send_message "$2" "$line" done rm -f -r "${TMPDIR:-.}/$3" + [ -s "${TMPDIR:-.}/$3.log" ] || rm -f "${TMPDIR:-.}/$3.log" exit ;; "startbot" ) diff --git a/examples/question.sh b/examples/question.sh index a7ca0c7..c6e9176 100755 --- a/examples/question.sh +++ b/examples/question.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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-1-gbccd064 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -26,6 +26,7 @@ until [ "$SUCCESS" = "y" ] ;do case $answer in 'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; 'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; + '') echo "empty answer!" && exit;; *) SUCCESS=n;; esac done diff --git a/modules/background.sh b/modules/background.sh index c6d3221..c2b009f 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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-1-gbccd064 # source from commands.sh if you want ro use interactive or background jobs @@ -50,8 +50,8 @@ listproc() { # $2 program # $3 jobname start_back() { - local fifo; fifo="$(fifoname "$1")" - echo "$1:$3:$2" >"${TMPDIR:-.}/${fifo}$3-back.cmd" + local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")" + echo "$1:$3:$2" >"${fifo}$3-back.cmd" start_proc "$1" "$2" "back-$3-" } @@ -62,9 +62,12 @@ start_back() { start_proc() { [ "$2" = "" ] && return kill_proc "$1" "$3" - local fifo; fifo="$(fifoname "$1" "$3")" - mkfifo "${TMPDIR:-.}/${fifo}" - ( $2 <"${TMPDIR:-.}/${fifo}" | "${SCRIPT}" outproc "${1}" "${fifo}"; ) &>>"${TMPDIR:-.}/${fifo}.log" & + local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1" "$3")" + mkfifo "${fifo}" + { set -f + # shellcheck disable=SC2002 + cat "${fifo}" | $2 | "${SCRIPT}" outproc "${1}" "${fifo}" + } &>>"${fifo}.log" & disown -a } @@ -96,12 +99,14 @@ kill_back() { kill_proc() { local fifo; fifo="$(fifoname "$1" "$2")" kill -15 "$(listproc "${fifo}")" 2>/dev/null - rm -f -r "${TMPDIR:-.}/${fifo}"; + fifo="${TMPDIR:-.}/${fifo}" + [ -s "${fifo}.log" ] || rm -f "${fifo}.log" + [ -p "${fifo}" ] && rm -f "${fifo}"; } # $1 chat # $2 message forward_interactive() { - local fifo; fifo="$(fifoname "$1")" + local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")" [ -p "${fifo}" ] && echo "$2" >"${fifo}" } From a3c0d3165c1e70d71194a0b6f2dff4a161d187c4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 10:52:44 +0200 Subject: [PATCH 03/20] move procname and proclist, replace echo with cat for output to file --- bashbot.sh | 45 ++++++++++++++++++++++++++++--------------- examples/question.sh | 2 +- modules/background.sh | 29 ++++++++-------------------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 0c6b982..a17fbd8 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-1-gbccd064 +#### $$VERSION$$ v0.80-dev3-2-ga1a823b # # Exit Codes: # - 0 sucess (hopefully) @@ -58,6 +58,7 @@ if [ ! -f "${TOKENFILE}" ]; then echo "Running headless, run ${SCRIPT} init first!" exit 2 else + ${CLEAR} echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token @@ -71,6 +72,7 @@ if [ ! -f "${BOTADMIN}" ]; then echo "Running headless, set botadmin to AUTO MODE!" echo '?' > "${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}" @@ -129,6 +131,19 @@ if [ "$1" != "source" ]; then fi +# internal functions +# $1 postfix, e.g. chatid +# $2 prefix, back- or startbot- +procname(){ + echo "$2${ME}_$1" +} + +# $1 proc name +proclist() { + # shellcheck disable=SC2009 + ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 +} + # returns true if command exist _exists() { @@ -228,26 +243,26 @@ JsonGetValue() { ################ # processing of updates starts here process_updates() { - MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')" - for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do - process_client "$1" + local max num debug="$1" + max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')" + for ((num=0; num<=max; num++)); do + process_client "$num" "${debug}" done } process_client() { - iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${PROCESS_NUMBER}"',"inline_query","id"')" + local num="$1" debug="$2" + iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${num}"',"inline_query","id"')" if [ "${iQUERY[ID]}" = "" ]; then - [[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"MESSAGE.log" - process_message "$PROCESS_NUMBER" "$1" + [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"MESSAGE.log" + process_message "${num}" "${debug}" else - [[ "$1" = *"debug"* ]] && echo "$UPDATE" >>"INLINE.log" - [ "$INLINE" != "0" ] && _is_function process_inline && process_inline "$PROCESS_NUMBER" "$1" + [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"INLINE.log" + [ "$INLINE" != "0" ] && _is_function process_inline && process_inline "${num}" "${debug}" fi - # Tmux - copname="$ME"_"${CHAT[ID]}" # shellcheck source=./commands.sh - source "${COMMANDS}" "$1" + source "${COMMANDS}" "${debug}" tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>"${COUNTFILE}" + grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || cat <<< "$tmpcount$'\\n'" >>"${COUNTFILE}" # To get user count execute bash bashbot.sh count } process_inline() { @@ -261,7 +276,7 @@ process_inline() { process_message() { local num="$1" local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" - echo "$UPDATE" >"$TMP" + cat <<< "$UPDATE$'\\n'" >"$TMP" # Message MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')" MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )" @@ -333,7 +348,7 @@ process_message() { # Location LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")" LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")" - NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')" + NAME="$(sed 's/.*\///g' <<< "${URLS[*]}")" rm "$TMP" } diff --git a/examples/question.sh b/examples/question.sh index c6e9176..2ebf99c 100755 --- a/examples/question.sh +++ b/examples/question.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.80-dev3-1-gbccd064 +#### $$VERSION$$ v0.80-dev3-2-ga1a823b # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/modules/background.sh b/modules/background.sh index c2b009f..abddbec 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.80-dev3-1-gbccd064 +#### $$VERSION$$ v0.80-dev3-2-ga1a823b # source from commands.sh if you want ro use interactive or background jobs @@ -32,25 +32,12 @@ killproc() { kill_proc "${CHAT[ID]}" "$1" } -# internal functions -# $1 chatid -# $2 prefix -fifoname(){ - echo "$2${ME}_$1" -} - -# $1 pipename -listproc() { - # shellcheck disable=SC2009 - ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 -} - # inline and backgound functions # $1 chatid # $2 program # $3 jobname start_back() { - local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")" + local fifo; fifo="${TMPDIR:-.}/$(procname "$1")" echo "$1:$3:$2" >"${fifo}$3-back.cmd" start_proc "$1" "$2" "back-$3-" } @@ -62,7 +49,7 @@ start_back() { start_proc() { [ "$2" = "" ] && return kill_proc "$1" "$3" - local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1" "$3")" + local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" mkfifo "${fifo}" { set -f # shellcheck disable=SC2002 @@ -81,7 +68,7 @@ check_back() { # $1 chatid # $2 prefix check_proc() { - [ "$(listproc "$(fifoname "$1" "$2")")" != "" ] + [ "$(proclist "$(procname "$1" "$2")")" != "" ] # shellcheck disable=SC2034 res=$?; return $? } @@ -90,15 +77,15 @@ check_proc() { # $2 jobname kill_back() { kill_proc "$1" "back-$2-" - rm -f "${TMPDIR:-.}/$(fifoname "$1")$2-back.cmd" + rm -f "${TMPDIR:-.}/$(procname "$1")$2-back.cmd" } # $1 chatid # $2 prefix kill_proc() { - local fifo; fifo="$(fifoname "$1" "$2")" - kill -15 "$(listproc "${fifo}")" 2>/dev/null + local fifo; fifo="$(procname "$1" "$2")" + kill -15 "$(proclist "${fifo}")" 2>/dev/null fifo="${TMPDIR:-.}/${fifo}" [ -s "${fifo}.log" ] || rm -f "${fifo}.log" [ -p "${fifo}" ] && rm -f "${fifo}"; @@ -107,6 +94,6 @@ kill_proc() { # $1 chat # $2 message forward_interactive() { - local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")" + local fifo; fifo="${TMPDIR:-.}/$(procname "$1")" [ -p "${fifo}" ] && echo "$2" >"${fifo}" } From e7d2effc2701cbdf5b596f318d55b097ffc34e7b Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 12:54:59 +0200 Subject: [PATCH 04/20] working background jobs, fix old pipes --- bashbot.sh | 5 ++++- modules/background.sh | 17 +++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index a17fbd8..8b151f1 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-2-ga1a823b +#### $$VERSION$$ v0.80-dev3-3-ga3c0d31 # # Exit Codes: # - 0 sucess (hopefully) @@ -364,6 +364,9 @@ start_bot() { [[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log" [ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" [[ "${DEBUG}" = "xdebug"* ]] && set -x + #cleaup old pipes + find "${TMPDIR}" -type p -delete + while true; do UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)" diff --git a/modules/background.sh b/modules/background.sh index abddbec..08e5814 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.80-dev3-2-ga1a823b +#### $$VERSION$$ v0.80-dev3-3-ga3c0d31 # source from commands.sh if you want ro use interactive or background jobs @@ -51,11 +51,9 @@ start_proc() { kill_proc "$1" "$3" local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" mkfifo "${fifo}" - { set -f - # shellcheck disable=SC2002 - cat "${fifo}" | $2 | "${SCRIPT}" outproc "${1}" "${fifo}" - } &>>"${fifo}.log" & - disown -a + nohup bash -c "{ set -f; exec 3>\"${fifo}\" & + cat \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" + }" &>>"${fifo}.log" & } @@ -84,9 +82,12 @@ kill_back() { # $1 chatid # $2 prefix kill_proc() { - local fifo; fifo="$(procname "$1" "$2")" - kill -15 "$(proclist "${fifo}")" 2>/dev/null + local fifo prid + fifo="$(procname "$1" "$2")" + prid="$(proclist "${fifo}")" fifo="${TMPDIR:-.}/${fifo}" + # shellcheck disable=SC2086 + [ "${prid}" != "" ] && kill ${prid} [ -s "${fifo}.log" ] || rm -f "${fifo}.log" [ -p "${fifo}" ] && rm -f "${fifo}"; } From 83623ece2e005d52f6ddff27fa0b552b5d83f35e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 13:59:36 +0200 Subject: [PATCH 05/20] fix interactive and MESSAGE extraction --- bashbot.sh | 10 +++++----- modules/background.sh | 9 ++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 8b151f1..21c3497 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-3-ga3c0d31 +#### $$VERSION$$ v0.80-dev3-4-ge7d2eff # # Exit Codes: # - 0 sucess (hopefully) @@ -253,16 +253,16 @@ process_client() { local num="$1" debug="$2" iQUERY[ID]="$(JsonGetString <<<"${UPDATE}" '"result",'"${num}"',"inline_query","id"')" if [ "${iQUERY[ID]}" = "" ]; then - [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"MESSAGE.log" + [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" process_message "${num}" "${debug}" else - [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE$'\\n'" >>"INLINE.log" + [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"INLINE.log" [ "$INLINE" != "0" ] && _is_function process_inline && process_inline "${num}" "${debug}" fi # shellcheck source=./commands.sh source "${COMMANDS}" "${debug}" tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || cat <<< "$tmpcount$'\\n'" >>"${COUNTFILE}" + grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || cat <<< "$tmpcount" >>"${COUNTFILE}" # To get user count execute bash bashbot.sh count } process_inline() { @@ -276,7 +276,7 @@ process_inline() { process_message() { local num="$1" local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE" - cat <<< "$UPDATE$'\\n'" >"$TMP" + cat <<< "$UPDATE" >"$TMP" # Message MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')" MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )" diff --git a/modules/background.sh b/modules/background.sh index 08e5814..214edf4 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.80-dev3-3-ga3c0d31 +#### $$VERSION$$ v0.80-dev3-4-ge7d2eff # source from commands.sh if you want ro use interactive or background jobs @@ -48,12 +48,11 @@ start_back() { # $3 prefix start_proc() { [ "$2" = "" ] && return - kill_proc "$1" "$3" local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" + kill_proc "$1" "$3" mkfifo "${fifo}" - nohup bash -c "{ set -f; exec 3>\"${fifo}\" & - cat \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" - }" &>>"${fifo}.log" & + nohup bash -c "{ tail -f \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" + rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & } From beb77a4b65001e6eb7f294dfcca164d2e4433640 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 15:03:44 +0200 Subject: [PATCH 06/20] start, kill, resume, suspend now working --- README.html | 4 +-- README.md | 5 ++- README.txt | 5 ++- bashbot.sh | 84 ++++++++++++++++++++++++++++++++---------------- doc/7_develop.md | 4 +-- 5 files changed, 64 insertions(+), 38 deletions(-) diff --git a/README.html b/README.html index ae2c52f..3c3f61e 100644 --- a/README.html +++ b/README.html @@ -18,7 +18,7 @@

Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.

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

Prerequisites

-

Depends on tmux. Uses JSON.sh.

+

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 coreutils, busybox or toybox, see Developer Notes

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

Documentation

@@ -107,6 +107,6 @@

@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.80-dev3-0-g31a5d00

+


VERSION
v0.80-dev3-5-g83623ec

diff --git a/README.md b/README.md index c4184ec..cc2d1f9 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). ## Prerequisites -Depends on [tmux](http://github.com/tmux/tmux). -Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +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. More concret on the common commands provided by [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) @@ -107,4 +106,4 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec diff --git a/README.txt b/README.txt index f394c59..d1459ff 100644 --- a/README.txt +++ b/README.txt @@ -13,8 +13,7 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). ## Prerequisites -Depends on [tmux](http://github.com/tmux/tmux). -Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +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. @@ -155,4 +154,4 @@ health status If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec diff --git a/bashbot.sh b/bashbot.sh index 21c3497..af65f72 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-4-ge7d2eff +#### $$VERSION$$ v0.80-dev3-5-g83623ec # # Exit Codes: # - 0 sucess (hopefully) @@ -392,7 +392,7 @@ bot_init() { [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" #setup bashbot - [[ "$(id -u)" -eq "0" ]] && RUNUSER="nobody" + [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " read -r TOUSER [ "$TOUSER" = "" ] && TOUSER="$RUNUSER" @@ -460,15 +460,14 @@ if [ "$1" != "source" ]; then bot_init exit ;; - "attach") - tmux attach -t "$ME" - exit - ;; esac ############### # "official" arguments as shown to users + SESSION="$ME-startbot" + BOTPID="$(proclist "${SESSION}")" + case "$1" in "count") echo "A total of $(wc -l <"${COUNTFILE}") users used me." @@ -481,46 +480,75 @@ if [ "$1" != "source" ]; then shift while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" ;; + "status") + if [ "${BOTPID}" != "" ]; then + echo -e "${GREEN}Bot is running.${NC}" + exit + else + echo -e "${ORANGE}Bot not running.${NC}" + exit 5 + fi + ;; + "start") - tmux kill-session -t "$ME" &>/dev/null - tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" - echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}" + # shellcheck disable=SC2086 + [ "${BOTPID}" != "" ] && kill ${BOTPID} + nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null & + echo "Session Name: ${SESSION}" + if [ "$(proclist "${SESSION}")" != "" ]; then + echo -e "${GREEN}Bot started successfully.${NC}" + else + echo -e "${RED}An error occurred while starting the bot.${NC}" + exit 5 + fi ;; - "kill") - tmux kill-session -t "$ME" &>/dev/null - echo -e "${GREEN}OK. Bot stopped successfully.${NC}" + "kill"|"stop") + if [ "${BOTPID}" != "" ]; then + # shellcheck disable=SC2086 + if kill ${BOTPID}; then + echo -e "${GREEN}OK. Bot stopped successfully.${NC}" + else + echo -e "${RED}An error occured while stopping bot.${NC}" + exit 5 + fi + fi + exit ;; - "background" | "resumeback") + "backgr"* | "resumeb"*) echo -e "${GREEN}Restart background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes to start.${NC}"; break else - RESTART="$(< "${FILE}")" - CHAT[ID]="${RESTART%%:*}" - JOB="${RESTART#*:}" + CONTENT="$(< "${FILE}")" + CHAT[ID]="${CONTENT%%:*}" + JOB="${CONTENT#*:}" PROG="${JOB#*:}" JOB="${JOB%:*}" - fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" - echo "restartbackground ${PROG} ${fifo}" + fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" + echo "restart background job ${PROG} ${fifo}" start_back "${CHAT[ID]}" "${PROG}" "${JOB}" fi done ;; - "killback" | "suspendback") + "killb"* | "suspendb"*) echo -e "${GREEN}Stopping background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes.${NC}"; break else - REMOVE="$(< "${FILE}")" - CHAT[ID]="${RESTART%%:*}" - JOB="${REMOVE#*:}" + CONTENT="$(< "${FILE}")" + CHAT[ID]="${CONTENT%%:*}" + JOB="${CONTENT#*:}" JOB="${JOB%:*}" - fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" - echo "killbackground ${fifo}" - [ "$1" = "killback" ] && rm -f "${FILE}" # remove job - kill_proc "${CHAT[ID]}" "back-${JOB}" + fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" + if [[ "$1" = "killb"* ]]; then + rm -f "${FILE}" # remove job + echo "kill background job ${fifo}" + else + echo "suspend background job ${fifo}" + fi + kill_proc "${CHAT[ID]}" "back-${JOB}-" fi done ;; @@ -530,13 +558,13 @@ if [ "$1" != "source" ]; then ;; *) echo -e "${RED}${ME}: BAD REQUEST${NC}" - echo -e "${RED}Available arguments: start, kill, count, broadcast, help, suspendback, resumeback, killback${NC}" + echo -e "${RED}Available arguments: start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}" exit 4 ;; esac # warn if root - if [[ "$(id -u)" -eq "0" ]] ; then + if [[ "${UID}" -eq "0" ]] ; then echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}" fi diff --git a/doc/7_develop.md b/doc/7_develop.md index 112bf6b..81eed8e 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -5,7 +5,7 @@ This section is about help and best practices for new bashbot developers. The ma bashbot development is done on github. If you want to provide fixes or new features [fork bashbot on githup](https://help.github.com/en/articles/fork-a-repo) and provide changes as [pull request on github](https://help.github.com/en/articles/creating-a-pull-request). ### Debugging Bashbot -In normal mode of operation all bashbot output is discarded one more correct sent to TMUX console. +In normal mode of operation all bashbot output is discarded. To get these messages (and more) you can start bashbot in the current shell ```./bashbot.sh startbot```. Now you can see all output or erros from bashbot. In addition you can change the change the level of verbosity by adding a third argument after startbot. ``` @@ -188,5 +188,5 @@ fi #### [Prev Function Reference](6_reference.md) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec From 7190c6e330581e3f5958c601a914d87144259367 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 17:31:55 +0200 Subject: [PATCH 07/20] fix outproc exit and show script on ps --- bashbot.sh | 7 +++++-- modules/background.sh | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index af65f72..8f6c2cb 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-5-g83623ec +#### $$VERSION$$ v0.80-dev3-6-gbeb77a4 # # Exit Codes: # - 0 sucess (hopefully) @@ -391,6 +391,9 @@ bot_init() { local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" + # shellcheck disable=SC2009 + oldbot="$(ps -ef | grep startbot | grep -v -e 'grep' -e '\-startbot' )" + [ "${oldbot}" != "" ] && echo -e "${ORANGE}Warning: Old TMUX bot is running! You must kill it manually first:${NC}\\n$${oldbot}" && exit 5 #setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " @@ -442,7 +445,7 @@ if [ "$1" != "source" ]; then "outproc") # forward output from interactive and jobs to chat [ "$3" = "" ] && echo "No file to read from" && exit 3 [ "$2" = "" ] && echo "No chat to send to" && exit 3 - while read -r -t 10 line ;do + while read -r line ;do [ "$line" != "" ] && send_message "$2" "$line" done rm -f -r "${TMPDIR:-.}/$3" diff --git a/modules/background.sh b/modules/background.sh index 214edf4..4cb7fd5 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.80-dev3-4-ge7d2eff +#### $$VERSION$$ v0.80-dev3-6-gbeb77a4 # source from commands.sh if you want ro use interactive or background jobs @@ -51,8 +51,10 @@ start_proc() { local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" kill_proc "$1" "$3" mkfifo "${fifo}" - nohup bash -c "{ tail -f \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\" - rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" & + nohup bash &>>"${fifo}.log" < Date: Sun, 19 May 2019 17:56:24 +0200 Subject: [PATCH 08/20] update doc --- doc/6_reference.md | 60 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/doc/6_reference.md b/doc/6_reference.md index 4837f76..a5be7f1 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -330,20 +330,24 @@ see [InlineQueryResult for more information](https://core.telegram.org/bots/api# ### Background and Interactive jobs You must include ```source modules/background.sh``` in 'commands.sh' to have the following functions availible. -##### startproc +##### start_proc ```startproc``` starts a script, the output of the script is sent to the user or chat, user input will be sent back to the script. see [Advanced Usage](3_advanced.md#Interactive-Chats) -*usage:* startproc "script" +*usage:* start_proc "${CHAT[ID]}" "script" + +*alias:* startproc "${CHAT[ID]}" "script" *example:* ```bash startproc 'examples/calc.sh' ``` -##### checkproc +##### check_proc Return true (0) if an interactive script is running in the chat. -*usage:* checkprog +*usage:* check_prog "${CHAT[ID]}" + +*alias:* checkprog *example:* ```bash @@ -355,10 +359,12 @@ else fi ``` -##### killproc +##### kill_proc Kill the interactive script running in the chat -*usage:* killproc +*usage:* kill_proc "${CHAT[ID]}" + +*alias:* killproc *example:* ```bash @@ -372,22 +378,26 @@ fi ---- -##### background +##### 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. In contrast to interactive chats, background jobs do not recieve user input and can run forever. In addition you can suspend and restart running jobs, e.g. after reboot. -*usage:* background "script" "jobname" +*usage:* start_back "${CHAT[ID]}" "script" "jobname" + +*alias:* background "script" "jobname" *example:* ```bash background "examples/notify.sh" "notify" ``` -##### checkback +##### check_back Return true (0) if an background job is active in the given chat. -*usage:* checkback "jobname" +*usage:* check_back "${CHAT[ID]}" "jobname" + +*alias:* checkback "jobname" *example:* ```bash @@ -400,8 +410,11 @@ else fi ``` -##### killback -*usage:* killback "jobname" +##### kill_back + +*usage:* kill_back "${CHAT[ID]}" "jobname" + +*alias:* killback "jobname" *example:* ```bash @@ -525,6 +538,16 @@ You must include ```source modules/aliases.sh``` in 'commands.sh' to have the f ### Helper functions +##### _exists +Returns true if the given function exist, can be used to check if a module is loaded. + +*usage* _exists command + +*example:* +```bash +_exists "curl" && _message "Command curl is not installed!" +``` + ##### _is_function Returns true if the given function exist, can be used to check if a module is loaded. @@ -535,11 +558,22 @@ Returns true if the given function exist, can be used to check if a module is lo _is_function "background" && _message "you can run background jobs!" ``` + ---- ### Bashbot internal functions These functions are for internal use only and must not used in your bot commands. +##### procname +Returns PrefixBotname-Postfix + +*usage:* procname postfix prefix + +##### proclist +Returns process IDs from ps -ef containing procname + +*usage:* proclist procname + ##### get_file *usage:* url="$(get_file "${CHAT[ID]}" "message")" @@ -592,5 +626,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-7-g7190c6e From b8ae9eca87a91153124d7e84232463f30d223ea9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 10:50:51 +0200 Subject: [PATCH 09/20] 0.80-pre, adjust doc, convert echo with parameters to fprint --- README.html | 2 +- README.md | 2 +- README.txt | 2 +- bashbot.rc | 2 +- bashbot.sh | 26 +++++------ commands.sh | 4 +- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 23 ++++++++-- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 43 ++++++++++--------- doc/7_develop.md | 2 +- doc/8_custom.md | 2 +- examples/README.md | 2 +- examples/background-scripts/run_diskusage.sh | 2 +- .../background-scripts/run_filecontent.sh | 2 +- examples/background-scripts/run_filename.sh | 2 +- examples/background-scripts/run_notify.sh | 2 +- examples/bashbot-multi.sh | 2 +- examples/bashbot.cron | 2 +- examples/calc.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 2 +- examples/send-system-status/mycommands.sh | 2 +- modules/aliases.sh | 2 +- modules/answerInline.sh | 4 +- modules/background.sh | 13 ++++-- modules/chatMember.sh | 4 +- modules/sendMessage.sh | 12 +++--- mycommands.sh | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 2 +- test/a-commit-test.sh | 2 +- test/b-example-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 2 +- test/d-process_inline-test.sh | 2 +- test/d-process_message-test.sh | 2 +- test/d-send_message-test.sh | 2 +- test/d-user_is-test.sh | 2 +- test/e-env-test.sh | 2 +- 53 files changed, 121 insertions(+), 98 deletions(-) diff --git a/README.html b/README.html index 3c3f61e..ec70693 100644 --- a/README.html +++ b/README.html @@ -107,6 +107,6 @@

@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.80-dev3-5-g83623ec

+


VERSION
v0.80-pre-0-gdd7c66d

diff --git a/README.md b/README.md index cc2d1f9..6379fc9 100644 --- a/README.md +++ b/README.md @@ -106,4 +106,4 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-dev3-5-g83623ec +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/README.txt b/README.txt index d1459ff..42ae383 100644 --- a/README.txt +++ b/README.txt @@ -154,4 +154,4 @@ health status If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-dev3-5-g83623ec +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/bashbot.rc b/bashbot.rc index 908f863..56a9647 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 8f6c2cb..43bc6fd 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-dev3-6-gbeb77a4 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # # Exit Codes: # - 0 sucess (hopefully) @@ -62,7 +62,7 @@ if [ ! -f "${TOKENFILE}" ]; then echo -e "${RED}TOKEN MISSING.${NC}" echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}" read -r token - echo "${token}" > "${TOKENFILE}" + printf '%s\n' "${token}" > "${TOKENFILE}" fi fi @@ -70,22 +70,22 @@ BOTADMIN="${BASHBOT_ETC:-.}/botadmin" if [ ! -f "${BOTADMIN}" ]; then if [ "${CLEAR}" = "" ]; then echo "Running headless, set botadmin to AUTO MODE!" - echo '?' > "${BOTADMIN}" + 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 token - echo "${token}" > "${BOTADMIN}" - [ "${token}" = "" ] && echo '?' > "${BOTADMIN}" + read -r admin + printf '%S\n' "${admin}" > "${BOTADMIN}" + [ "${admin}" = "" ] && printf '%s\n' '?' > "${BOTADMIN}" fi fi BOTACL="${BASHBOT_ETC:-.}/botacl" if [ ! -f "${BOTACL}" ]; then echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}" - echo "" >"${BOTACL}" + printf '\n' >"${BOTACL}" fi TMPDIR="${BASHBOT_VAR:-.}/data-bot-bash" @@ -99,7 +99,7 @@ fi COUNTFILE="${BASHBOT_VAR:-.}/count" if [ ! -f "${COUNTFILE}" ]; then - echo "" >"${COUNTFILE}" + printf '\n' >"${COUNTFILE}" elif [ ! -w "${COUNTFILE}" ]; then echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}" ls -l "${COUNTFILE}" @@ -135,7 +135,7 @@ fi # $1 postfix, e.g. chatid # $2 prefix, back- or startbot- procname(){ - echo "$2${ME}_$1" + printf '%s\n' "$2${ME}_$1" } # $1 proc name @@ -262,7 +262,7 @@ process_client() { # shellcheck source=./commands.sh source "${COMMANDS}" "${debug}" tmpcount="COUNT${CHAT[ID]}" - grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || cat <<< "$tmpcount" >>"${COUNTFILE}" + grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}" # To get user count execute bash bashbot.sh count } process_inline() { @@ -329,8 +329,8 @@ process_message() { URLS[VOICE]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","voice","file_id"' <"$TMP")")" # Contact - CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","user_id"' <"$TMP")")" CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","first_name"' <"$TMP")")" + CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","user_id"' <"$TMP")")" CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","last_name"' <"$TMP")")" CONTACT[NUMBER]="$(JsonGetString '"result",'"${num}"',"message","contact","phone_number"' <"$TMP")" CONTACT[VCARD]="$(JsonGetString '"result",'"${num}"',"message","contact","vcard"' <"$TMP")" @@ -393,13 +393,13 @@ bot_init() { [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" # shellcheck disable=SC2009 oldbot="$(ps -ef | grep startbot | grep -v -e 'grep' -e '\-startbot' )" - [ "${oldbot}" != "" ] && echo -e "${ORANGE}Warning: Old TMUX bot is running! You must kill it manually first:${NC}\\n$${oldbot}" && exit 5 + [ "${oldbot}" != "" ] && echo -e "${ORANGE}Warning: Old TMUX bot is running! You must kill it manually first:${NC}\\n${oldbot}" #setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " read -r TOUSER [ "$TOUSER" = "" ] && TOUSER="$RUNUSER" - if ! id "$TOUSER" >/dev/null 2>&1; then + if ! id "$TOUSER" &>/dev/null; then echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 else diff --git a/commands.sh b/commands.sh index 129c378..e619a42 100644 --- a/commands.sh +++ b/commands.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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # # adjust your language setting here, e.g.when run from other user or cron. @@ -107,7 +107,7 @@ if [ "${1}" != "source" ];then if [ "$res" -eq 0 ] ; then killproc && _message "Command canceled.";else _message "No command is currently running.";fi ;; *) # forward messages to optional dispatcher - _is_function forward_interactive && forward_interactive "${CHAT[ID]}" "${MESSAGE}" # interactive running + _is_function send_interactive && send_interactive "${CHAT[ID]}" "${MESSAGE}" _is_function mycommands && mycommands ;; esac diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 7739b76..c3ccaf7 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/git-add.sh b/dev/git-add.sh index 249bd60..33a5926 100755 --- a/dev/git-add.sh +++ b/dev/git-add.sh @@ -3,7 +3,7 @@ # # works together with git pre-push.sh and ADD all changed files since last push -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 38f5729..8fa8eff 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index b50de77..7853247 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index 7c58fe0..ff24815 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 7f1253a..fc65af3 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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 7d49f07..99b92ed 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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index 5bf5b1e..672df7f 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,3 +1,3 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d bashbot.rc diff --git a/dev/version.sh b/dev/version.sh index c4851f6..b5bee19 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/0_install.md b/doc/0_install.md index 47b1f7b..a8740d9 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -35,19 +35,34 @@ As an alternative to download the zip files, you can clone the github repository 3. Extract all files to your existing bashbot dir **Note: all files execpt 'mycommands.sh' and 'commands.sh' may overwritten!** 4. Save your your current 'commands.sh' and run ```cp commands.sh.dist commands.sh``` -5. Run ```sudo ./bashbot.sh init``` to setup your environment after the update +5. Stop all running instances of bashbot at this point latest +6. Run ```sudo ./bashbot.sh init``` to setup your environment after the update If you modified 'commands.sh' re apply all changes to the new 'commands.sh'. To avoid this all your modifications must be done in 'mycommands.sh' only. +Now you can restart your bashbot instances. + ### Notes on Updates -#### Location of tmp / data dir -From version 0.70 on the tmp dir is renamed to 'data-bot-bash' to reflect the fact that not only temporary files are stored. an existing 'tmp-bot-bash' will be automatically renamed after update. +#### removal of TMUX +From version 0.80 on TMUX is no longer needed and the bachsbot command 'attach' is deleted. Old function 'inproc' +is replaced by 'send_interactive'. send_interactive does checks if an interactive job is running internaly. +Pls check if you make use of inproc and remove it including the old checks, e.g. +```bash +if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi +# or +[ checkprog ] && inproc +``` +must be replaced by ```send_interactive "${CHATD[ID]}" "${MESSAGE}"``` +### Do not edit commands.sh From version 0.60 on your commands must be placed in 'mycommands.sh'. If you update from a version with your commands in 'commands.sh' move all your commands and functions to 'mycommands.sh'. +#### Location of var / tmp / data dirs +From version 0.70 on the tmp dir is renamed to 'data-bot-bash' to reflect the fact that not only temporary files are stored. an existing 'tmp-bot-bash' will be automatically renamed after update. + From version 0.50 on the temporary files are no more placed in '/tmp'. instead a dedicated tmp dir is used. #### Changes to send_keyboard in v0.6 @@ -70,5 +85,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index deb4785..48aef6c 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -65,5 +65,5 @@ group. This step is up to you actually. #### [Prev Installation](0_install.md) #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/2_usage.md b/doc/2_usage.md index 3f9d780..e5d2ff0 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -182,5 +182,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 0ae7851..2b7e085 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -180,5 +180,5 @@ See also [answer_inline_multi, answer_inline_compose](6_reference.md#answer_inli #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/4_expert.md b/doc/4_expert.md index 6f7f6c0..bfdbddf 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/5_practice.md b/doc/5_practice.md index fc69778..bc02fd0 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -153,5 +153,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/6_reference.md b/doc/6_reference.md index a5be7f1..636f56c 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -84,6 +84,17 @@ See also [deleteMessage limitations](https://core.telegram.org/bots/api#deleteme ---- +##### send_message +```send_message``` sends any type of message to the given chat. Type of output is steered by keywords within the message. + +The main use case for send_message is to process the output of interactive chats and background jobs. **For regular Bot commands I recommend using of the dedicated send_xxx_message() functions from above.** + +*usage:* send_message "${CHAT[ID]}" "message" + +*example:* - see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) + +---- + ### File, Location, Venue, Keyboard @@ -335,13 +346,14 @@ You must include ```source modules/background.sh``` in 'commands.sh' to have th *usage:* start_proc "${CHAT[ID]}" "script" -*alias:* startproc "${CHAT[ID]}" "script" +*alias:* startproc "script" *example:* ```bash startproc 'examples/calc.sh' ``` + ##### check_proc Return true (0) if an interactive script is running in the chat. @@ -351,8 +363,7 @@ Return true (0) if an interactive script is running in the chat. *example:* ```bash -checkproc -if [ "$res" -gt 0 ] ; then +if ! check_proc "${CHAT[ID]}" ; then startproc "examples/calc.sh" else send_normal_message "${CHAT[ID]}" "Calc already running ..." @@ -368,8 +379,7 @@ Kill the interactive script running in the chat *example:* ```bash -checkprog -if [ "$res" -eq 0 ]; then +if check_proc "${CHAT[ID]}" ; then killproc && send_message "${CHAT[ID]}" "Command canceled." else send_message "${CHAT[ID]}" "Command is not running." @@ -401,8 +411,7 @@ Return true (0) if an background job is active in the given chat. *example:* ```bash -checkback "notify" -if [ "$res" -gt 0 ] ; then +if ! checkback "notify" ; then send_normal_message "${CHAT[ID]}" "Start notify" background "examples/notify.sh" "notify" else @@ -429,16 +438,13 @@ fi ---- -##### send_message -```send_message``` sends any type of message to the given chat. Type of output is steered by keywords within the message. +##### 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. -The main use case for send_message is to process the output of interactive chats and background jobs. **For regular Bot commands I recommend using of the dedicated send_xxx_message() functions from above.** +*usage:* send_interactive "${CHAT[ID]}" "message" -*usage:* send_message "${CHAT[ID]}" "message" - -*example:* - see [Usage](2_usage.md#send_message) and [Advanced Usage](3_advanced.md#Interactive-Chats) - ----- +*replaces:*' incproc ### Aliases - shortcuts for often used funtions You must include ```source modules/aliases.sh``` in 'commands.sh' to have the following functions availible. @@ -565,7 +571,7 @@ _is_function "background" && _message "you can run background jobs!" These functions are for internal use only and must not used in your bot commands. ##### procname -Returns PrefixBotname-Postfix +Returns PrefixBotname_Postfix *usage:* procname postfix prefix @@ -620,11 +626,8 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca *usage:* ME="$(getBotNiname)" -##### inproc -Send Input from Telegram to waiting Interactive Chat. - #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-dev3-7-g7190c6e +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/7_develop.md b/doc/7_develop.md index 81eed8e..785b3b8 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -188,5 +188,5 @@ fi #### [Prev Function Reference](6_reference.md) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.80-dev3-5-g83623ec +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/doc/8_custom.md b/doc/8_custom.md index ebb2f3b..35b87dc 100644 --- a/doc/8_custom.md +++ b/doc/8_custom.md @@ -131,5 +131,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/examples/README.md b/examples/README.md index 11426f6..b2863d5 100644 --- a/examples/README.md +++ b/examples/README.md @@ -55,6 +55,6 @@ convert existing bots. **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index 043ae6c..0fb31ac 100755 --- a/examples/background-scripts/run_diskusage.sh +++ b/examples/background-scripts/run_diskusage.sh @@ -4,7 +4,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index 826c33c..39f39e7 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 86911fa..a9692eb 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,7 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/background-scripts/run_notify.sh b/examples/background-scripts/run_notify.sh index 1f81f75..13d0e6d 100755 --- a/examples/background-scripts/run_notify.sh +++ b/examples/background-scripts/run_notify.sh @@ -4,7 +4,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot-multi.sh b/examples/bashbot-multi.sh index 293eb78..9dd7231 100755 --- a/examples/bashbot-multi.sh +++ b/examples/bashbot-multi.sh @@ -2,7 +2,7 @@ # file. multibot.sh # description: run multiple telegram bots from one installation # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then echo "Usage: $0 botname command" diff --git a/examples/bashbot.cron b/examples/bashbot.cron index d727ca6..dd5c21d 100644 --- a/examples/bashbot.cron +++ b/examples/bashbot.cron @@ -7,7 +7,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 2c79621..f8eadf5 100755 --- a/examples/calc.sh +++ b/examples/calc.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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/notify.sh b/examples/notify.sh index 45e2d14..1f02cfc 100755 --- a/examples/notify.sh +++ b/examples/notify.sh @@ -4,7 +4,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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/question.sh b/examples/question.sh index 2ebf99c..25d20e2 100755 --- a/examples/question.sh +++ b/examples/question.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.80-dev3-2-ga1a823b +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl index 2c20ed9..80d4335 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,7 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # Format: # user:ressource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 7c9b946..755aadc 100644 --- a/examples/send-system-status/mycommands.sh +++ b/examples/send-system-status/mycommands.sh @@ -5,7 +5,7 @@ # to show how you can customize bashbot by only editing mycommands.sh # NOTE: this is not tested, simply copied from original source and reworked! # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/modules/aliases.sh b/modules/aliases.sh index 5781952..859a447 100644 --- a/modules/aliases.sh +++ b/modules/aliases.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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # # source from commands.sh to use the aliases diff --git a/modules/answerInline.sh b/modules/answerInline.sh index fcdae5f..69774d7 100644 --- a/modules/answerInline.sh +++ b/modules/answerInline.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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # source from commands.sh to use the inline functions @@ -97,6 +97,6 @@ inline_query_compose(){ ;; esac - echo "${JSON}" + printf '%s\n' "${JSON}" } diff --git a/modules/background.sh b/modules/background.sh index 4cb7fd5..917ab31 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.80-dev3-6-gbeb77a4 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # source from commands.sh if you want ro use interactive or background jobs @@ -38,7 +38,7 @@ killproc() { # $3 jobname start_back() { local fifo; fifo="${TMPDIR:-.}/$(procname "$1")" - echo "$1:$3:$2" >"${fifo}$3-back.cmd" + printf '%s\n' "$1:$3:$2" >"${fifo}$3-back.cmd" start_proc "$1" "$2" "back-$3-" } @@ -95,7 +95,12 @@ kill_proc() { # $1 chat # $2 message -forward_interactive() { +send_interactive() { local fifo; fifo="${TMPDIR:-.}/$(procname "$1")" - [ -p "${fifo}" ] && echo "$2" >"${fifo}" + [ -p "${fifo}" ] && printf '%s\n' "$2" >"${fifo}" & # not blocking! +} + +# old style but may not work because of local checks +inproc() { + send_interactive "${CHAT[ID]}" "${MESSAGE}" } diff --git a/modules/chatMember.sh b/modules/chatMember.sh index d2ed8d9..e0d4d69 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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # source from commands.sh to use the member functions @@ -48,7 +48,7 @@ user_is_botadmin() { local admin; admin="$(head -n 1 "${BOTADMIN}")" [ "${admin}" = "${1}" ] && return 0 [[ "${admin}" = "@*" ]] && [[ "${admin}" = "${2}" ]] && return 0 - if [ "${admin}" = "?" ]; then echo "${1:-?}" >"${BOTADMIN}"; return 0; fi + if [ "${admin}" = "?" ]; then printf '%s\n' "${1:-?}" >"${BOTADMIN}"; return 0; fi return 1 } diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index cb9999f..d34a5e5 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.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # source from commands.sh to use the sendMessage functions @@ -48,12 +48,12 @@ send_html_message() { old_send_keyboard() { local text='"text":"'"${2}"'"' shift 2 - local keyboard=init - OLDIFS=$IFS - IFS=$(echo -en "\"") + local keyboard="init" + OLDIFS="$IFS" + IFS="\"" for f in "$@" ;do [ "$f" != " " ] && keyboard="$keyboard, [\"$f\"]";done - IFS=$OLDIFS - keyboard=${keyboard/init, /} + IFS="$OLDIFS" + keyboard="${keyboard/init, /}" sendJson "${1}" "${text}"', "reply_markup": {"keyboard": [ '"${keyboard}"' ],"one_time_keyboard": true}' "$MSG_URL" } diff --git a/mycommands.sh b/mycommands.sh index 33d8edf..e1adfed 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -2,7 +2,7 @@ # files: mycommands.sh.dist # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # # uncomment the following lines to overwrite info and help messages diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index d097145..fcf48c6 100755 --- a/test/ADD-test-new.sh +++ b/test/ADD-test-new.sh @@ -2,7 +2,7 @@ # # ADD a new test skeleton to test dir, but does not activate test # -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 8208922..8829e25 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index c0a19e5..385517f 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d ../dev/hooks/pre-commit.sh diff --git a/test/b-example-test.sh b/test/b-example-test.sh index d50cc87..416a247 100644 --- a/test/b-example-test.sh +++ b/test/b-example-test.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # file: b-example-test.sh -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index b440572..1b6b181 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-JSON.sh-test.sh b/test/d-JSON.sh-test.sh index 9a45a95..e4b4e35 100755 --- a/test/d-JSON.sh-test.sh +++ b/test/d-JSON.sh-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_inline-test.sh b/test/d-process_inline-test.sh index 46a6e30..7325efd 100755 --- a/test/d-process_inline-test.sh +++ b/test/d-process_inline-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 5efc4ed..58f339d 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index 750be37..f4938b8 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/d-user_is-test.sh b/test/d-user_is-test.sh index 901ccbd..5a92104 100755 --- a/test/d-user_is-test.sh +++ b/test/d-user_is-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh diff --git a/test/e-env-test.sh b/test/e-env-test.sh index d9aefdd..9b73472 100755 --- a/test/e-env-test.sh +++ b/test/e-env-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-pre-0-gdd7c66d # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh From 9482bd62bf2c312bb119f9958e6643d6ad192309 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 11:09:44 +0200 Subject: [PATCH 10/20] better explanation for not upgraded bot running --- bashbot.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 43bc6fd..f055022 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-1-gb8ae9ec # # Exit Codes: # - 0 sucess (hopefully) @@ -393,7 +393,8 @@ bot_init() { [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" # shellcheck disable=SC2009 oldbot="$(ps -ef | grep startbot | grep -v -e 'grep' -e '\-startbot' )" - [ "${oldbot}" != "" ] && echo -e "${ORANGE}Warning: Old TMUX bot is running! You must kill it manually first:${NC}\\n${oldbot}" + [ "${oldbot}" != "" ] && \ + echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! It is not possible to stop it by this script:${NC}\\n${oldbot}" #setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " From 3c5ffdb506ebe97fbc934ad81e9d25a9e6564b78 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 12:12:11 +0200 Subject: [PATCH 11/20] test if script/job exist and is executable, more cleanup on startup --- bashbot.sh | 5 +++-- modules/background.sh | 3 ++- mycommands.sh | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f055022..af83107 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-1-gb8ae9ec +#### $$VERSION$$ v0.80-pre-2-g9482bd6 # # Exit Codes: # - 0 sucess (hopefully) @@ -364,8 +364,9 @@ start_bot() { [[ "${DEBUG}" = *"debug" ]] && exec &>>"DEBUG.log" [ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\"" [[ "${DEBUG}" = "xdebug"* ]] && set -x - #cleaup old pipes + #cleaup old pipes and empty logfiles find "${TMPDIR}" -type p -delete + find "${TMPDIR}" -size 0 -name "*.log" -delete while true; do UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)" diff --git a/modules/background.sh b/modules/background.sh index 917ab31..d48e9de 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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-2-g9482bd6 # source from commands.sh if you want ro use interactive or background jobs @@ -48,6 +48,7 @@ start_back() { # $3 prefix start_proc() { [ "$2" = "" ] && return + [ -x "${2%% *}" ] || return 1 local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" kill_proc "$1" "$3" mkfifo "${fifo}" diff --git a/mycommands.sh b/mycommands.sh index e1adfed..b5e5ea2 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -2,7 +2,7 @@ # files: mycommands.sh.dist # copy to mycommands.sh and add all your commands and functions here ... # -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-2-g9482bd6 # # uncomment the following lines to overwrite info and help messages @@ -30,7 +30,7 @@ if [ "$1" != "source" ];then '/question'*) # start interactive questions checkproc if [ "$res" -gt 0 ] ; then - startproc "examples/question.sh" + startproc "examples/question.sh" || _message "Can't start question." else send_normal_message "${CHAT[ID]}" "$MESSAGE already running ..." fi @@ -39,7 +39,7 @@ if [ "$1" != "source" ];then '/run_notify'*) # start notify background job myback="notify"; checkback "$myback" if [ "$res" -gt 0 ] ; then - background "examples/notify.sh 60" "$myback" # notify every 60 seconds + background "examples/notify.sh 60" "$myback" || _message "Can't start notify." else send_normal_message "${CHAT[ID]}" "Background command $myback already running ..." fi From d1a3372d3953baedac38628fc59b1be9eba3c5e8 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 15:43:37 +0200 Subject: [PATCH 12/20] better kill handling, add send_file tests --- bashbot.sh | 12 ++++++------ modules/background.sh | 8 +++----- modules/sendMessage.sh | 14 +++++++------- test/d-send_message-test.sh | 15 +++++++++++++-- .../d-send_message-test/d-send_message-test.input | 10 ++++++++++ .../d-send_message-test.result | 15 +++++++++++++++ 6 files changed, 54 insertions(+), 20 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index af83107..4342a0b 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-2-g9482bd6 +#### $$VERSION$$ v0.80-pre-3-g3c5ffdb # # Exit Codes: # - 0 sucess (hopefully) @@ -141,7 +141,7 @@ procname(){ # $1 proc name proclist() { # shellcheck disable=SC2009 - ps -ef | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 + ps -fu "${UID}" | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 } # returns true if command exist @@ -392,10 +392,6 @@ bot_init() { local OLDTMP="${BASHBOT_VAR:-.}/tmp-bot-bash" [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" - # shellcheck disable=SC2009 - oldbot="$(ps -ef | grep startbot | grep -v -e 'grep' -e '\-startbot' )" - [ "${oldbot}" != "" ] && \ - echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! It is not possible to stop it by this script:${NC}\\n${oldbot}" #setup bashbot [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " @@ -405,6 +401,10 @@ bot_init() { echo -e "${RED}User \"$TOUSER\" not found!${NC}" exit 3 else + # shellcheck disable=SC2009 + oldbot="$(ps -fu "$TOUSER" | grep startbot | grep -v -e 'grep' -e '\-startbot' )" + [ "${oldbot}" != "" ] && \ + echo -e "${ORANGE}Warning: At least one not upgraded TMUX bot is running! You must stop it with kill command:${NC}\\n${oldbot}" echo "Adjusting user \"${TOUSER}\" files and permissions ..." [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" chown -R "$TOUSER" . ./* diff --git a/modules/background.sh b/modules/background.sh index d48e9de..e0053ef 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.80-pre-2-g9482bd6 +#### $$VERSION$$ v0.80-pre-3-g3c5ffdb # source from commands.sh if you want ro use interactive or background jobs @@ -52,10 +52,8 @@ start_proc() { local fifo; fifo="${TMPDIR:-.}/$(procname "$1" "$3")" kill_proc "$1" "$3" mkfifo "${fifo}" - nohup bash &>>"${fifo}.log" <>"${fifo}.log" & } diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index d34a5e5..b45547e 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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-3-g3c5ffdb # source from commands.sh to use the sendMessage functions @@ -88,18 +88,18 @@ send_button() { UPLOADDIR="${BASHBOT_UPLOAD:-${TMPDIR}/upload}" send_file() { - [ "$2" = "" ] && return - local file="$2" + [ "$2" = "" ] && return 0 + local CUR_URL WHAT STATUS file="$2" local CAPTION=',"caption":"'$3'"'; [ "$3" = "" ] && CAPTION="" # file access checks ... - [[ "$file" = *'..'* ]] && return # no directory traversal - [[ "$file" = '.'* ]] && return # no hidden or relative files + [[ "$file" = *'..'* ]] && return # no directory traversal + [[ "$file" = '.'* ]] && return # no hidden or relative files if [[ "$file" = '/'* ]] ; then - [[ "$file" =~ $FILE_REGEX ]] || return # absulute must match REGEX + [[ ! "$file" =~ $FILE_REGEX ]] && return # absulute must match REGEX else file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR fi - [ -r "$file" ] || return # and file must exits of course + [ ! -r "$file" ] && return # and file must exits of course local ext="${file##*.}" case $ext in diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index f4938b8..d5705d2 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-3-g3c5ffdb # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -33,10 +33,21 @@ sendJson() { echo -n " Send line ..." +# create dummy files for upload +ALLOW='/tmp/allowed' +FILE_REGEX="$ALLOW/.*" +[ -d "$ALLOW" ] || mkdir "$ALLOW" +touch "$ALLOW/this_is_my.gif" "$ALLOW/this_is_my.doc" +touch "$TMPDIR/this_is_my.gif" "$TMPDIR/this_is_my.doc" + while read -r line ; do echo -n "." + set -x send_message "123456" "$line" >>"${OUTPUTFILE}" -done < "${INPUTFILE}" #2>>"${LOGFILE}" + set +x +done < "${INPUTFILE}" 2>>"${LOGFILE}" +[ -d "$ALLOW" ] && rm -rf "$ALLOW" + echo " done." { diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1; } | cat -v diff --git a/test/d-send_message-test/d-send_message-test.input b/test/d-send_message-test/d-send_message-test.input index 1c17d70..b054070 100644 --- a/test/d-send_message-test/d-send_message-test.input +++ b/test/d-send_message-test/d-send_message-test.input @@ -14,3 +14,13 @@ Text plus vuene will appear in chat mylatstartshere la10 mylongstartshere lo20 m # test for new inline button Text plus keyboard will appear in chat mybtextstartshere Button Text myburlstartshere https://www... STABILO 88/240 Fineliner point 88 mynewlinestartshere mynewlinestartshere [https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] mynewlinestartshere mybtextstartshere Bei Amazon ansehen ... myburlstartshere https://www.amazon.de/dp/B014TN3JYW mytextstartshere second part of text mynewlinestartshere plus newline. + +# test for sendfile +Text plus absolute file will appear in chat myfilelocationstartshere /tmp/allowed/this_is_my.gif +Text plus absolute file will appear in chat myfilelocationstartshere /tmp/allowed/this_is_my.doc +Text plus relative file will appear in chat myfilelocationstartshere this_is_my.gif +Text plus relative file will appear in chat myfilelocationstartshere this_is_my.doc +THIS IS OUTSIDE allowed myfilelocationstartshere /home/user/NOTallowed/this_is_my.dif +THIS DOES NOT EXIST myfilelocationstartshere /tmp/allowed/this_does_not_exist.gif +THIS DOES NOT EXIST myfilelocationstartshere this_does_not_exist.gif + diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index c0dfa66..96b38cb 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -47,3 +47,18 @@ second part of text plus newline.", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen ...", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} URL:https://api.telegram.org/botbashbottestscript/sendMessage +chat:123456 JSON:"text":"# test for sendfile" +URL:https://api.telegram.org/botbashbottestscript/sendMessage + +chat:123456 JSON:"action": "upload_photo" +URL:https://api.telegram.org/botbashbottestscript/sendChatAction + +chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat"" +URL:https://api.telegram.org/botbashbottestscript/sendPhoto + +chat:123456 JSON:"action": "upload_document" +URL:https://api.telegram.org/botbashbottestscript/sendChatAction + +chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat"" +URL:https://api.telegram.org/botbashbottestscript/sendDocument + From e5f7b2da50083618de4ceef3ebd716cdd026d7a2 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 17:26:21 +0200 Subject: [PATCH 13/20] fix send_file, add test for send_file --- bashbot.sh | 16 +++++++++++++++- modules/sendMessage.sh | 15 ++++++++++----- test/d-send_message-test.sh | 6 +++++- .../d-send_message-test.result | 4 ++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 4342a0b..ef59191 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-3-g3c5ffdb +#### $$VERSION$$ v0.80-pre-4-gd1a3372 # # Exit Codes: # - 0 sucess (hopefully) @@ -183,6 +183,16 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" } + #$1 Chat, $2 what , $3 file, $4 URL, $5 caption + sendUpload() { + [ "$#" -lt 4 ] && return + if [ "$5" != "" ]; then + res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )" + else + res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )" + fi + BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" + } else # simple curl or wget call outputs result to stdout getJson(){ @@ -197,6 +207,10 @@ else BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")" BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")" } + sendUpload() { + sendJson "$1" '"text":"Sorry, wget does not support file upload"' "${MSG_URL}" + BOTSENT[OK]="false" + } fi # convert common telegram entities to JSON diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index b45547e..7ad8024 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.80-pre-3-g3c5ffdb +#### $$VERSION$$ v0.80-pre-4-gd1a3372 # source from commands.sh to use the sendMessage functions @@ -87,10 +87,16 @@ send_button() { UPLOADDIR="${BASHBOT_UPLOAD:-${TMPDIR}/upload}" +# for now this can only send local files with curl! +# extend to allow send files by URL or telegram ID send_file() { - [ "$2" = "" ] && return 0 + [ "$2" = "" ] && return + [[ "$2" = "http"* ]] && return # currently we do not support URL + upload_file "${@}" +} + +upload_file(){ local CUR_URL WHAT STATUS file="$2" - local CAPTION=',"caption":"'$3'"'; [ "$3" = "" ] && CAPTION="" # file access checks ... [[ "$file" = *'..'* ]] && return # no directory traversal [[ "$file" = '.'* ]] && return # no hidden or relative files @@ -136,8 +142,7 @@ send_file() { ;; esac send_action "${1}" "$STATUS" - # shellcheck disable=SC2034 - sendJson "${1}" '"'"$WHAT"'":"'"$2"'"'"$CAPTION"'"' "$CUR_URL" + sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "$3" } # typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index d5705d2..989e5d3 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-pre-3-g3c5ffdb +#### $$VERSION$$ v0.80-pre-4-gd1a3372 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -28,6 +28,10 @@ sendEmpty() { sendJson() { printf 'chat:%s\tJSON:%s\nURL:%s\n\n' "${1}" "${2}" "${3}" } +sendUpload() { +#JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat"" + printf 'chat:%s\tJSON:"%s":"%s","caption":"%s"\nURL:%s\n\n' "${1}" "${2}" "${3}" "${5}" "${4}" +} # send text input to send_message diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index 96b38cb..3abd7e0 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -53,12 +53,12 @@ URL:https://api.telegram.org/botbashbottestscript/sendMessage chat:123456 JSON:"action": "upload_photo" URL:https://api.telegram.org/botbashbottestscript/sendChatAction -chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat"" +chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat" URL:https://api.telegram.org/botbashbottestscript/sendPhoto chat:123456 JSON:"action": "upload_document" URL:https://api.telegram.org/botbashbottestscript/sendChatAction -chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat"" +chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat" URL:https://api.telegram.org/botbashbottestscript/sendDocument From d34c22f1562f5e252ee49a1017aed1d69b5ae28d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 18:42:21 +0200 Subject: [PATCH 14/20] function download(), downloads URL to file in data-bot-bash --- bashbot.sh | 16 +++++++++++++++- doc/6_reference.md | 16 +++++++++++++++- modules/aliases.sh | 6 +----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index ef59191..25066f5 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-4-gd1a3372 +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d # # Exit Codes: # - 0 sucess (hopefully) @@ -132,6 +132,20 @@ fi # internal functions +# $1 URL, $2 filename in TMPDIR +# outputs final filename +download() { + local empty="no.file" file="${2:-${empty}}" + if [[ "$file" = *"/"* ]] || [[ "$file" = "."* ]]; then file="${empty}"; fi + while [ -f "${TMPDIR:-.}/${file}" ] ; do file="$RAMDOM-${file}"; done + getJson "$1" >"${TMPDIR:-.}/${file}" || return + printf '%s\n' "${TMPDIR:-.}/${file}" +} + +# easy handling of users: +_is_botadmin() { + user_is_botadmin "${USER[ID]}" +} # $1 postfix, e.g. chatid # $2 prefix, back- or startbot- procname(){ diff --git a/doc/6_reference.md b/doc/6_reference.md index 636f56c..63923b2 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -544,6 +544,20 @@ You must include ```source modules/aliases.sh``` in 'commands.sh' to have the f ### Helper functions +##### 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 '..'. + +*usage:* download URL filename + +*example:* +```bash +file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" +echo "$file" -> ./data-bot-bash/avatar.jpg +file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")" +echo "$file" -> ./data-bot-bash/12345-avatar.jpg +``` + ##### _exists Returns true if the given function exist, can be used to check if a module is loaded. @@ -629,5 +643,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d diff --git a/modules/aliases.sh b/modules/aliases.sh index 859a447..1d18b43 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -5,14 +5,10 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-5-ge5f7b2d # # source from commands.sh to use the aliases -# easy handling of users: -_is_botadmin() { - user_is_botadmin "${USER[ID]}" -} _is_admin() { user_is_admin "${CHAT[ID]}" "${USER[ID]}" } From 300553f8ba3ef2c7304a2e487d7c99f2f965f69a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 19:16:00 +0200 Subject: [PATCH 15/20] move _botadmin back to aliases --- bashbot.sh | 6 +----- modules/aliases.sh | 6 +++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 25066f5..41d448d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -12,7 +12,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.80-pre-5-ge5f7b2d +#### $$VERSION$$ v0.80-pre-6-gd34c22f # # Exit Codes: # - 0 sucess (hopefully) @@ -142,10 +142,6 @@ download() { printf '%s\n' "${TMPDIR:-.}/${file}" } -# easy handling of users: -_is_botadmin() { - user_is_botadmin "${USER[ID]}" -} # $1 postfix, e.g. chatid # $2 prefix, back- or startbot- procname(){ diff --git a/modules/aliases.sh b/modules/aliases.sh index 1d18b43..0c9f1e4 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -5,10 +5,14 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-pre-5-ge5f7b2d +#### $$VERSION$$ v0.80-pre-6-gd34c22f # # source from commands.sh to use the aliases +# easy handling of users: +_is_botadmin() { + user_is_botadmin "${USER[ID]}" +} _is_admin() { user_is_admin "${CHAT[ID]}" "${USER[ID]}" } From f1ebdbb72d08137efc4ff8f215fa801f2270fa7e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 19:49:41 +0200 Subject: [PATCH 16/20] remove tmux (c) and tmux example --- bashbot.sh | 3 +-- doc/5_practice.md | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 41d448d..b6059ac 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -8,11 +8,10 @@ # https://github.com/topkecleon/telegram-bot-bash # Depends on JSON.sh (http://github.com/dominictarr/JSON.sh) (MIT/Apache), -# and on tmux (http://github.com/tmux/tmux) (BSD). # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.80-pre-6-gd34c22f +#### $$VERSION$$ v0.80-pre-7-g300553f # # Exit Codes: # - 0 sucess (hopefully) diff --git a/doc/5_practice.md b/doc/5_practice.md index bc02fd0..f8e9436 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -61,7 +61,7 @@ every line from ```'/command')``` to ```;;```. # ;; *) # forward other messages to optional dispatcher - _is_function startproc && if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi # interactive running + _is_function send_interactive && send_interactive "${CHAT[ID]}" "${MESSAGE}" _is_function mycommands && mycommands ;; esac @@ -153,5 +153,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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-7-g300553f From 8dfdf2e168c7f6aaaa145e91cc0d0fa85fee5b69 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 20:48:49 +0200 Subject: [PATCH 17/20] more save resumeback killback handling --- README.html | 9 +++++---- README.md | 17 ++++++++++------- README.txt | 26 +++++++++++++++----------- bashbot.sh | 6 ++++-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/README.html b/README.html index ec70693..b4ffb7e 100644 --- a/README.html +++ b/README.html @@ -79,9 +79,6 @@
  • Customize bashbot environment
  • Examples
  • -

    You don’t like the many bashbot files?

    -

    At the beginning bashbot was simply the file bashbot.sh I can copy everywhere and run the bot. Now we have ‘commands.sh’, ‘mycommands.sh’, ’modules/*.sh’ and much more.

    -

    Hey no Problem, if you are finished with your cool bot simply run dev/make-standalone.sh to create a stripped down Version containing only ‘bashbot.sh’ and ‘commands.sh’! For more information see Create a stripped down Version of your Bot

    Security Considerations

    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

    @@ -94,6 +91,7 @@

    Your Bot configuration must no be readable from other users. Everyone who can read your Bots token can act as your Bot and has access to all chats your Bot is in!

    Everyone with read access to your Bot files can extract your Bots data. Especially your Bot Token in token must be protected against other users. No one exept you must have write access to the Bot files. The Bot must be restricted to have write access to count and tmp-bot-bash only, all other files must be write protected.

    To set access rights for your bashbot installation to a reasonable default run sudo ./bashbot.sh init after every update or change to your installation directory.

    +

    FAQ

    Is this Bot insecure?

    Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks …

    Why Bash and not the much better xyz?

    @@ -104,9 +102,12 @@
  • no need to install or learn a new programming language, library or framework
  • no database, not event driven, not OO …
  • +

    Can I have the single bashbot.sh file back?

    +

    At the beginning bashbot was simply the file bashbot.sh you can copy everywhere and run the bot. Now we have ‘commands.sh’, ‘mycommands.sh’, ’modules/*.sh’ and much more.

    +

    Hey no Problem, if you are finished with your cool bot run dev/make-standalone.sh to create a stripped down Version of your bot containing only ‘bashbot.sh’ and ‘commands.sh’! For more information see Create a stripped down Version of your Bot

    @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.80-pre-0-gdd7c66d

    +


    VERSION
    v0.80-pre-8-gf1ebdbb

    diff --git a/README.md b/README.md index 6379fc9..221f69a 100644 --- a/README.md +++ b/README.md @@ -59,12 +59,6 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do * [Examples](examples/README.md) -#### You don't like the many bashbot files? -At the beginning bashbot was simply the file ```bashbot.sh``` I can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. - -Hey no Problem, if you are finished with your cool bot simply run ```dev/make-standalone.sh``` to create a stripped down Version containing only -'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped down Version of your Bot](doc/7_develop.md) - ## Security Considerations Running a Telegram Bot means it is connected to the public and you never know whats send to your Bot. @@ -89,6 +83,8 @@ Everyone with read access to your Bot files can extract your Bots data. Especial To set access rights for your bashbot installation to a reasonable default run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +## FAQ + ### Is this Bot insecure? Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible for the bot commands you wrote and you should know about the risks ... @@ -100,10 +96,17 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from - no need to install or learn a new programming language, library or framework - no database, not event driven, not OO ... +#### Can I have the single bashbot.sh file back? +At the beginning bashbot was simply the file ```bashbot.sh``` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. + +Hey no Problem, if you are finished with your cool bot run ```dev/make-standalone.sh``` to create a stripped down Version of your bot containing only +'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped down Version of your Bot](doc/7_develop.md) + + @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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-8-gf1ebdbb diff --git a/README.txt b/README.txt index 42ae383..2ab117c 100644 --- a/README.txt +++ b/README.txt @@ -71,16 +71,6 @@ all](https://core.telegram.org/bots#3-how-do-i-create-a-bot) * [Examples](examples/README.md) -#### You don't like the many bashbot files? -At the beginning bashbot was simply the file ```bashbot.sh``` I can copy -everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', -'modules/*.sh' and much more. - -Hey no Problem, if you are finished with your cool bot simply run -```dev/make-standalone.sh``` to create a stripped down Version containing only -'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped -down Version of your Bot](doc/7_develop.md) - ## Security Considerations Running a Telegram Bot means it is connected to the public and you never know whats send to your Bot. @@ -131,6 +121,8 @@ To set access rights for your bashbot installation to a reasonable default run ```sudo ./bashbot.sh init``` after every update or change to your installation directory. +## FAQ + ### Is this Bot insecure? Bashbot is not more (in)secure as any other Bot written in any other language, we have done our best to make it as secure as possible. But YOU are responsible @@ -147,6 +139,18 @@ health status - no need to install or learn a new programming language, library or framework - no database, not event driven, not OO ... +#### Can I have the single bashbot.sh file back? +At the beginning bashbot was simply the file ```bashbot.sh``` you can copy +everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', +'modules/*.sh' and much more. + +Hey no Problem, if you are finished with your cool bot run +```dev/make-standalone.sh``` to create a stripped down Version of your bot +containing only +'bashbot.sh' and 'commands.sh'! For more information see [Create a stripped +down Version of your Bot](doc/7_develop.md) + + @Gnadelwartz ## That's it! @@ -154,4 +158,4 @@ health status If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-8-gf1ebdbb diff --git a/bashbot.sh b/bashbot.sh index b6059ac..94db6d4 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.80-pre-7-g300553f +#### $$VERSION$$ v0.80-pre-8-gf1ebdbb # # Exit Codes: # - 0 sucess (hopefully) @@ -543,6 +543,7 @@ if [ "$1" != "source" ]; then exit ;; "backgr"* | "resumeb"*) + _is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; } echo -e "${GREEN}Restart background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then @@ -555,11 +556,12 @@ if [ "$1" != "source" ]; then JOB="${JOB%:*}" fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" echo "restart background job ${PROG} ${fifo}" - start_back "${CHAT[ID]}" "${PROG}" "${JOB}" + start_proc "${CHAT[ID]}" "${PROG}" "back-${JOB}-" fi done ;; "killb"* | "suspendb"*) + _is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; } echo -e "${GREEN}Stopping background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then From fd7ca77cc183ecfa47ed720b1ee0c7b1d73660e9 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 21:40:14 +0200 Subject: [PATCH 18/20] move job control to module background and make it more failsave --- bashbot.sh | 44 ++++--------------------------------------- modules/background.sh | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 94db6d4..59ce360 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.80-pre-8-gf1ebdbb +#### $$VERSION$$ v0.80-pre-9-g8dfdf2e # # Exit Codes: # - 0 sucess (hopefully) @@ -542,45 +542,9 @@ if [ "$1" != "source" ]; then fi exit ;; - "backgr"* | "resumeb"*) - _is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; } - echo -e "${GREEN}Restart background processes ...${NC}" - for FILE in "${TMPDIR:-.}/"*-back.cmd; do - if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then - echo -e "${RED}No background processes to start.${NC}"; break - else - CONTENT="$(< "${FILE}")" - CHAT[ID]="${CONTENT%%:*}" - JOB="${CONTENT#*:}" - PROG="${JOB#*:}" - JOB="${JOB%:*}" - fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" - echo "restart background job ${PROG} ${fifo}" - start_proc "${CHAT[ID]}" "${PROG}" "back-${JOB}-" - fi - done - ;; - "killb"* | "suspendb"*) - _is_function start_proc || { echo -e "${RED}Module background not availible.${NC}"; exit 3; } - echo -e "${GREEN}Stopping background processes ...${NC}" - for FILE in "${TMPDIR:-.}/"*-back.cmd; do - if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then - echo -e "${RED}No background processes.${NC}"; break - else - CONTENT="$(< "${FILE}")" - CHAT[ID]="${CONTENT%%:*}" - JOB="${CONTENT#*:}" - JOB="${JOB%:*}" - fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" - if [[ "$1" = "killb"* ]]; then - rm -f "${FILE}" # remove job - echo "kill background job ${fifo}" - else - echo "suspend background job ${fifo}" - fi - kill_proc "${CHAT[ID]}" "back-${JOB}-" - fi - done + "resumeb"* | "killb"* | "suspendb"*) + _is_function job_control || { echo -e "${RED}Module background is not availible!${NC}"; exit 3; } + job_control "$1" ;; "help") less "README.txt" diff --git a/modules/background.sh b/modules/background.sh index e0053ef..7589749 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.80-pre-3-g3c5ffdb +#### $$VERSION$$ v0.80-pre-9-g8dfdf2e # source from commands.sh if you want ro use interactive or background jobs @@ -103,3 +103,44 @@ send_interactive() { inproc() { send_interactive "${CHAT[ID]}" "${MESSAGE}" } + +# start stopp all jobs +# $1 command +# killb* +# suspendb* +# resumeb* +job_control() { + local content proc CHAT job fifo + for FILE in "${TMPDIR:-.}/"*-back.cmd; do + if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then + echo -e "${RED}No background processes.${NC}" + break + else + content="$(< "${FILE}")" + CHAT="${content%%:*}" + job="${content#*:}" + proc="${job#*:}" + job="back-${job%:*}-" + fifo="$(procname "${CHAT}" "${job}")" + case "$1" in + "resumeb"*|"backgr"*) + echo "Restart Job: ${proc} ${fifo}" + start_proc "${CHAT}" "${proc}" "${job}" + ;; + "suspendb"*) + echo "Suspend Job: ${proc} ${fifo}" + kill_proc "${CHAT}" "${proc}" "${job}" + rm -f "${TMPDIR:-.}/${fifo}" + [ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log" + ;; + "killb"*) + echo "Kill Job: ${proc} ${fifo}" + kill_proc "${CHAT}" "${proc}" "${job}" + rm -f "${FILE}" # remove job + rm -f "${TMPDIR:-.}/${fifo}" + [ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log" + ;; + esac + fi + done +} From 8669cfb78543f5458d97c5d8f07e09cec659109c Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 21:48:44 +0200 Subject: [PATCH 19/20] optimize job_control --- modules/background.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/modules/background.sh b/modules/background.sh index 7589749..38f2889 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.80-pre-9-g8dfdf2e +#### $$VERSION$$ v0.80-pre-10-gfd7ca77 # source from commands.sh if you want ro use interactive or background jobs @@ -112,10 +112,7 @@ inproc() { job_control() { local content proc CHAT job fifo for FILE in "${TMPDIR:-.}/"*-back.cmd; do - if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then - echo -e "${RED}No background processes.${NC}" - break - else + [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break content="$(< "${FILE}")" CHAT="${content%%:*}" job="${content#*:}" @@ -130,17 +127,12 @@ job_control() { "suspendb"*) echo "Suspend Job: ${proc} ${fifo}" kill_proc "${CHAT}" "${proc}" "${job}" - rm -f "${TMPDIR:-.}/${fifo}" - [ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log" ;; "killb"*) echo "Kill Job: ${proc} ${fifo}" kill_proc "${CHAT}" "${proc}" "${job}" rm -f "${FILE}" # remove job - rm -f "${TMPDIR:-.}/${fifo}" - [ -s "${TMPDIR:-.}/${fifo}.log" ] || rm -f "${TMPDIR:-.}/${fifo}.log" ;; esac - fi done } From 5b2d0e1e8f6e0f343c09623358a532a3df45720d Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 21 May 2019 10:53:52 +0200 Subject: [PATCH 20/20] improved kill handling, fix job_control --- README.html | 4 ++-- README.md | 4 ++-- README.txt | 4 ++-- bashbot.sh | 21 ++++++++++++++++--- dev/make-distribution.sh | 4 ++-- doc/6_reference.md | 44 +++++++++++++++++++++++++++++++++++++--- modules/background.sh | 12 +++++++---- 7 files changed, 75 insertions(+), 18 deletions(-) diff --git a/README.html b/README.html index b4ffb7e..6736995 100644 --- a/README.html +++ b/README.html @@ -102,12 +102,12 @@
  • no need to install or learn a new programming language, library or framework
  • no database, not event driven, not OO …
  • -

    Can I have the single bashbot.sh file back?

    +

    Can I have the single bashbot.sh file back?

    At the beginning bashbot was simply the file bashbot.sh you can copy everywhere and run the bot. Now we have ‘commands.sh’, ‘mycommands.sh’, ’modules/*.sh’ and much more.

    Hey no Problem, if you are finished with your cool bot run dev/make-standalone.sh to create a stripped down Version of your bot containing only ‘bashbot.sh’ and ‘commands.sh’! For more information see Create a stripped down Version of your Bot

    @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.80-pre-8-gf1ebdbb

    +


    VERSION
    v0.80-pre-11-g8669cfb

    diff --git a/README.md b/README.md index 221f69a..5298fba 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from - no need to install or learn a new programming language, library or framework - no database, not event driven, not OO ... -#### Can I have the single bashbot.sh file back? +### Can I have the single bashbot.sh file back? At the beginning bashbot was simply the file ```bashbot.sh``` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. Hey no Problem, if you are finished with your cool bot run ```dev/make-standalone.sh``` to create a stripped down Version of your bot containing only @@ -109,4 +109,4 @@ Hey no Problem, if you are finished with your cool bot run ```dev/make-standalon If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-pre-8-gf1ebdbb +#### $$VERSION$$ v0.80-pre-11-g8669cfb diff --git a/README.txt b/README.txt index 2ab117c..1a0d429 100644 --- a/README.txt +++ b/README.txt @@ -139,7 +139,7 @@ health status - no need to install or learn a new programming language, library or framework - no database, not event driven, not OO ... -#### Can I have the single bashbot.sh file back? +### Can I have the single bashbot.sh file back? At the beginning bashbot was simply the file ```bashbot.sh``` you can copy everywhere and run the bot. Now we have 'commands.sh', 'mycommands.sh', 'modules/*.sh' and much more. @@ -158,4 +158,4 @@ down Version of your Bot](doc/7_develop.md) If you feel that there's something missing or if you found a bug, feel free to submit a pull request! -#### $$VERSION$$ v0.80-pre-8-gf1ebdbb +#### $$VERSION$$ v0.80-pre-11-g8669cfb diff --git a/bashbot.sh b/bashbot.sh index 59ce360..ffc76a8 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.80-pre-9-g8dfdf2e +#### $$VERSION$$ v0.80-pre-11-g8669cfb # # Exit Codes: # - 0 sucess (hopefully) @@ -147,12 +147,27 @@ procname(){ printf '%s\n' "$2${ME}_$1" } -# $1 proc name +# $1 sting to search for proramm incl. parameters +# retruns a list of PIDs of all current bot proceeses matching $1 proclist() { # shellcheck disable=SC2009 - ps -fu "${UID}" | grep -v grep| grep "$1" | sed 's/\s\+/\t/g' | cut -f 2 + ps -fu "${UID}" | grep -F "$1" | grep -v ' grep'| grep -F "${ME}" | sed 's/\s\+/\t/g' | cut -f 2 } +# $1 sting to search for proramm to kill +killallproc() { + local procid; procid="$(proclist "$1")" + if [ "${procid}" != "" ] ; then + # shellcheck disable=SC2046 + kill $(proclist "$1") + sleep 1 + procid="$(proclist "$1")" + # shellcheck disable=SC2046 + [ "${procid}" != "" ] && kill $(proclist -9 "$1") + fi +} + + # returns true if command exist _exists() { diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index fc65af3..1db1fa2 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.80-pre-0-gdd7c66d +#### $$VERSION$$ v0.80-pre-11-g8669cfb # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -38,7 +38,7 @@ cd "${DISTDIR}" || exit 1 # additional stuff mv "bashbot.rc" "bashbot.rc.dist" -mv "commands.sh" "commands.sh.dist" +# mv "commands.sh" "commands.sh.dist" # will be overwritten from v0.80 on mv "mycommands.sh" "mycommands.sh.dist" JSONSHFILE="JSON.sh/JSON.sh" diff --git a/doc/6_reference.md b/doc/6_reference.md index 63923b2..1d2d8ba 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -589,11 +589,49 @@ Returns PrefixBotname_Postfix *usage:* procname postfix prefix +*example:* +```bash +# returns botname, if already set +procname +# returns unique identifier for everthing related to chat +procname "${CHAT[ID]}" +# returns unique identifier for job, regardless of chat +procname "" "back-jobname-" +# returns unique identifier for a job related to a chat +# e.g. fifo, cmd and logfile name +procname "${CHAT[ID]}" "back-jobname-" +``` + ##### proclist -Returns process IDs from ps -ef containing procname +Returns process IDs of current bot processes containing string 'pattern' in name or argument. -*usage:* proclist procname +*usage:* proclist pattern +*example:* +```bash +# list PIDs of all background processes +proclist "back-" +# list PIDs of all processes of a job +proclist "back-jobname-" +# list PIDs of all processes for a chat +proclist "_${CHAT[ID]}" +# list PIDs of all bot processes +proclist +``` +##### killallproc +kill all current bot processes containing string 'pattern' in name or argument + +*usage:* killallproc pattern + +*example:* +```bash +# kill all background processes +killallproc "back-" +# kill all processes for a chat +killallproc "_${CHAT[ID]}" +# kill all bot processes, including YOURSELF! +killallproc +``` ##### get_file *usage:* url="$(get_file "${CHAT[ID]}" "message")" @@ -643,5 +681,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.80-pre-5-ge5f7b2d +#### $$VERSION$$ v0.80-pre-11-g8669cfb diff --git a/modules/background.sh b/modules/background.sh index 38f2889..cc05810 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.80-pre-10-gfd7ca77 +#### $$VERSION$$ v0.80-pre-11-g8669cfb # source from commands.sh if you want ro use interactive or background jobs @@ -110,7 +110,7 @@ inproc() { # suspendb* # resumeb* job_control() { - local content proc CHAT job fifo + local content proc CHAT job fifo killall="" for FILE in "${TMPDIR:-.}/"*-back.cmd; do [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break content="$(< "${FILE}")" @@ -126,13 +126,17 @@ job_control() { ;; "suspendb"*) echo "Suspend Job: ${proc} ${fifo}" - kill_proc "${CHAT}" "${proc}" "${job}" + kill_proc "${CHAT}" "${job}" + killall="y" ;; "killb"*) echo "Kill Job: ${proc} ${fifo}" - kill_proc "${CHAT}" "${proc}" "${job}" + kill_proc "${CHAT}" "${job}" rm -f "${FILE}" # remove job + killall="y" ;; esac done + # kill all requestet. kill ALL background jobs, even not listed in data-bot-bash + [ "${killall}" = "y" ] && killallproc "back-" }