From 7c112122e8f50debf108c70398f5428124ec141e Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Wed, 1 May 2019 11:46:57 +0200 Subject: [PATCH] add tests for ENV, fix some small nitpits --- .gitignore | 1 + README.html | 2 +- README.md | 2 +- README.txt | 2 +- bashbot.rc | 2 +- bashbot.sh | 6 +- commands.sh | 2 +- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 10 +-- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-dist.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 | 28 +++++-- 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.cron | 2 +- examples/calc.sh | 2 +- examples/notify.sh | 2 +- examples/question.sh | 2 +- examples/send-system-status/botacl | 1 + examples/send-system-status/mycommands.sh | 69 +++++++--------- modules/aliases.sh | 2 +- modules/background.sh | 2 +- modules/inline.sh | 2 +- mycommands.sh | 2 +- test/ADD-test-new.sh | 2 +- test/ALL-tests.inc.sh | 5 +- test/a-commit-test.sh | 2 +- test/c-init-test.sh | 2 +- test/d-JSON.sh-test.sh | 6 +- test/d-JSON.sh-test/JSON26783.log.in | 2 + test/d-JSON.sh-test/JSON30458.log.in | 2 + test/d-JSON.sh-test/JSON32034.log.in | 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 | 78 +++++++++++++++++++ test/e-env-test/botacl | 1 + test/e-env-test/botadmin | 1 + test/e-env-test/token | 1 + 54 files changed, 193 insertions(+), 98 deletions(-) create mode 100644 test/d-JSON.sh-test/JSON26783.log.in create mode 100644 test/d-JSON.sh-test/JSON30458.log.in create mode 100644 test/d-JSON.sh-test/JSON32034.log.in create mode 100755 test/e-env-test.sh create mode 100644 test/e-env-test/botacl create mode 100644 test/e-env-test/botadmin create mode 100644 test/e-env-test/token diff --git a/.gitignore b/.gitignore index b977922..1fb2e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ *.save *.log /JSON.sh/ +/data-bot-bash/ /tmp-bot-bash/ /dist/ diff --git a/README.html b/README.html index dcdd24f..52ab189 100644 --- a/README.html +++ b/README.html @@ -97,6 +97,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.7-rc1-0-g8279bdb

+


VERSION
v0.70-rc1-0-g8883cc9

diff --git a/README.md b/README.md index d4a5fac..3b6eb68 100644 --- a/README.md +++ b/README.md @@ -93,4 +93,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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/README.txt b/README.txt index d354c82..c341e68 100644 --- a/README.txt +++ b/README.txt @@ -130,4 +130,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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/bashbot.rc b/bashbot.rc index 1cf26be..1dc6470 100755 --- a/bashbot.rc +++ b/bashbot.rc @@ -1,7 +1,7 @@ #!/bin/sh # description: Start or stop telegram-bash-bot # -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # shellcheck disable=SC2009 # shellcheck disable=SC2181 diff --git a/bashbot.sh b/bashbot.sh index 4a090ff..4a29081 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # # Exit Codes: # - 0 sucess (hopefully) @@ -70,7 +70,7 @@ JSONSHFILE="${BASHBOT_JSONSH:-${RUNDIR}/JSON.sh/JSON.sh}" if [ ! -f "${JSONSHFILE}" ]; then echo "Seems to be first run, Downloading ${JSONSHFILE}..." - mkdir "JSON.sh" 2>/dev/null; + [[ "${JSONSHFILE}" = "${RUNDIR}/JSON.sh/JSON.sh" ]] && mkdir "JSON.sh" 2>/dev/null; curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" chmod +x "${JSONSHFILE}" fi @@ -587,7 +587,7 @@ bot_init() { exit 3 else echo "Adjusting user \"${TOUSER}\" files and permissions ..." - sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' bashbot.rc + [ -w "bashbot.rc" ] && sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' "bashbot.rc" chown -R "$TOUSER" . ./* chmod 711 . chmod -R a-w ./* diff --git a/commands.sh b/commands.sh index fff8090..18254b9 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/dev/all-tests.sh b/dev/all-tests.sh index 3a5d570..381c114 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 3339fbc..c0909b4 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 cc4ccd4..5b99dfb 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 ############ # NOTE: you MUST run install-hooks.sh again when updating this file! @@ -42,12 +42,12 @@ else exit 1 fi -REMOTEVER="$(git ls-remote -t --refs 2>/dev/null | tail -1 | sed 's/.*\/v//')" +REMOTEVER="$(git ls-remote -t --refs 2>/dev/null | tail -1 | sed -e 's/.*\/v//' -e 's/-.*//')" VERSION="$(git describe --tags | sed -e 's/-.*//' -e 's/v//')" # LOCAL version must greater than latest REMOTE release version -if (( $(echo "${VERSION} > ${REMOTEVER}" | bc -l) )); then +if (( $(echo "${VERSION} >= ${REMOTEVER}" | bc -l) )); then # update version in bashbot files on push set +f [ -f "${LASTPUSH}" ] && LASTFILES="$(find ./* -newer "${LASTPUSH}")" @@ -57,8 +57,8 @@ if (( $(echo "${VERSION} > ${REMOTEVER}" | bc -l) )); then dev/version.sh ${LASTFILES} 2>/dev/null || exit 1 echo " OK" else - echo "Error: local version ${VERSION} must be greater than latest release version." - echo "use \"git tag ...\" to create a local version greater than ${REMOTEVER}" + echo "Error: local version ${VERSION} must be greater or equal to release version ${REMOTEVER}." + echo "use \"git tag ...\" to create a new local version" exit 1 fi diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index da5e5bc..90e2126 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 ############ # 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 45f7467..aa8d98f 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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-dist.sh b/dev/make-dist.sh index 4112b4d..b82146c 100755 --- a/dev/make-dist.sh +++ b/dev/make-dist.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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 faa64f8..6b5af58 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,3 +1,3 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 bashbot.rc diff --git a/dev/version.sh b/dev/version.sh index a1b4dd6..a4aafa2 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/doc/0_install.md b/doc/0_install.md index 437f3fd..ebd9a3f 100644 --- a/doc/0_install.md +++ b/doc/0_install.md @@ -63,5 +63,5 @@ The old format is supported for backward compatibility, but may fail for corner #### [Next Create Bot](1_firstbot.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index d891e68..a38e0db 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/2_usage.md b/doc/2_usage.md index 011d224..c32a791 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -167,5 +167,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index 4480f4c..eca6c45 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -177,5 +177,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Getting started](2_usage.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/4_expert.md b/doc/4_expert.md index abc9c95..4dfd314 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/5_practice.md b/doc/5_practice.md index 9e8a868..f163c41 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/6_reference.md b/doc/6_reference.md index 8ce949b..0ad3855 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -489,5 +489,5 @@ Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/7_develop.md b/doc/7_develop.md index 57723d8..2b6e822 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -92,5 +92,5 @@ fi #### [Prev Function Reference](6_reference.md) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/doc/8_custom.md b/doc/8_custom.md index da4611a..6477cd8 100644 --- a/doc/8_custom.md +++ b/doc/8_custom.md @@ -117,5 +117,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms. #### [Prev Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/examples/README.md b/examples/README.md index 1917f03..a68985a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,13 +9,12 @@ An example crontab is provided in ```examples/bashbot.cron```, see [Expert use]( ### Interactive chats Two examples for interactive scripts are provided as **calc.sh** and **question.sh**, see [Advanced use](../doc/3_advanced.md#Interactive-Chats) -### Background jobs +### Background scripts Background jobs are an easy way to provide sceduled messages or alerts if something happens. - **notify.sh** is a simple example on how to send a message every x seonds, e.g. current time. -**background-scripts** contains a more concrete example on how to start and stop different scripts plus some example backgound scripts. +**background-scripts** contains a more useful example on how to start and stop different scripts plus some example backgound scripts. ``` mycommands.sh - /run_xxx and /kill-xxx wil start any script named run_xxx.sh @@ -28,16 +27,31 @@ Background jobs are an easy way to provide sceduled messages or alerts if someth **Note:** Output of system commands often contains newlines, each newline results in a telegram message, the function 'send_telegram' in mycommands.sh avoids this by converting each newline to ' mynewlinestartshere ' before output the string. -**system-status** contains an example with commands showing system status. This example is adapted from https://github.com/RG72/telegram-bot-bash +### Ssystem Status + +**send-system-status** contains an example for commands showing status of different subsystems. This example is adapted from + https://github.com/RG72/telegram-bot-bash to current bashbot commands, but not fully tested. This will show how easy you can +convert existing bots. ``` - mycommands.sh - sommands to show system status + mycommands.sh - commands to show system status botacl - controls who can show system status + +*Availiable commands*: + /se *sensors* + /smb *smbstatus* + /free *memory status* + /md *raid status* + /lvm *lvm status* + /lvsd *Datailed lvm status* + /df *disk space* + /ifconfig *ifconfig output* + /smart *sda* _smart status for sda drive_ ``` -### Use bashbot from external scripts +### External scripts **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 diff --git a/examples/background-scripts/run_diskusage.sh b/examples/background-scripts/run_diskusage.sh index b4217a0..3963bb9 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 467ede3..d5eff1e 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 08de54b..12a0ff3 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 9f563ae..c12f52f 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment diff --git a/examples/bashbot.cron b/examples/bashbot.cron index dc89324..6be077e 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 SHELL=/bin/sh diff --git a/examples/calc.sh b/examples/calc.sh index 6826000..1aa0429 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 9f563ae..c12f52f 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 faa0e72..fd5bc76 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 40a251d..fd5b863 100644 --- a/examples/send-system-status/botacl +++ b/examples/send-system-status/botacl @@ -1,6 +1,7 @@ # file: botacl # a user not listed here, will return false from 'user_is_allowed' # +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # Format: # user:ressource:chat diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh index 2444f7d..8a380e2 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 @@ -20,65 +20,56 @@ bashbot_info='This bot allows you to request status of your system. To begin using the bot, try with the /help command. ' bashbot_help='*Availiable commands*: -/s *sensors* -/ss *smbstatus* +/se *sensors* +/smb *smbstatus* /free *memory status* /md *raid status* -/lvs *lvm status* +/lvm *lvm status* /lvsd *Datailed lvm status* /df *disk space* /ifconfig *ifconfig output* /smart *-d sda* _smart status for sda drive_ ' -IDMESSAGEPRE="Your chat identifier is" # your additional bahsbot commands # NOTE: command can have @botname attached, you must add * in case tests... mycommands() { - if user_is_allowed "${USER[ID]}" "systemstatus"; then - case "$MESSAGE" in - '/md'|'raid_status') msg="$(cat /proc/mdstat)";; - '/ss'|'smbstatus') - msg="" - smbstatus >/tmp/smbstatus - send_doc "$TARGET" "/tmp/smbstatus" - prevActiveTime=$curTime - ;; - '/s'|'sensors'|'/sensors') msg="$(sensors | sed -r 's/\s|\)+//g' | sed -r 's/\(high=|\(min=/\//' | sed -r 's/\,crit=|\,max=/\//')";; - '/free') msg="$(free -h)";; - '/pvs') msg="$(pvs)";; - '/ifconfig') msg="$(ifconfig)";; - '/vgs') msg="$(vgs)";; - '/lvs') msg="$(lvs | sed -r 's/\s+/\n/g')";; - '/lvsd') msg="$(lvs -a -o +devices | sed -r 's/\s+/\n/g')";; - '/smart'|'/smartctl') - if [ "$OPTARG" == "" ]; then - msg="example \`/smart sda\`" - else - drive="$(echo "$OPTARG" | cut -c 1-3)" - echo "smartctl -a /dev/$drive" - msg="$(smartctl -a "/dev/$drive")" - fi + [[ "$MESSAGE" = '/'* ]] || return + set +f + # shellcheck disable=SC2206 + local arg=( $MESSAGE ) + set -f + local cmd="${arg[0]}" + local msg="" + + if user_is_botadmin || user_is_allowed "${USER[ID]}" "systemstatus"; then + case "$cmd" in + '/md'*) msg="$(cat /proc/mdstat)";; + '/smb'*) msg="$(smbstatus)" ;; + '/se'*) msg="$(sensors | sed -r 's/\s|\)+//g' | sed -r 's/\(high=|\(min=/\//' | sed -r 's/\,crit=|\,max=/\//')";; + '/free'*) msg="$(free -h)";; + '/pvs'*) msg="$(pvs)";; + '/ifc'*) msg="$(ifconfig)";; + '/vgs'*) msg="$(vgs)";; + '/lvm'*) msg="$(lvs | sed -r 's/\s+/\n/g')";; + '/lvsd'*) msg="$(lvs -a -o +devices | sed -r 's/\s+/\n/g')";; + '/smart'*) + [ "${arg[0]}" == "" ] && msg="example \`/smart sda\`" && return + drive="$(echo "${arg[0]}" | cut -c 1-3)" + echo "smartctl -a /dev/$drive" + msg="$(smartctl -a "/dev/$drive")" ;; '/df') msg="$(df -h | sed -r 's/^/\n/' | sed -r 's/\s+/\n/g')";; esac if [ "$msg" != "" ]; then - send_telegram "${CHAT[ID]}" "$msg" + send_normal_message "${CHAT[ID]}" "$msg" fi else - send_normal_message "Sorry, you are not allowed to use this bot!" + send_normal_message "${USER[ID]}" "Sorry, you are not allowed to use this bot!" fi } # place your processing functions here -# converts newlines for telegram and send as one message -# $1 chat -# $2 message -send_telegram() { - # output to telegram - send_message "${1}" "$(sed <<< "${2}" -e ':a;N;$!ba;s/\n/ mynewlinestartshere /g')" -} # 2>>"$0.log" - diff --git a/modules/aliases.sh b/modules/aliases.sh index 473db4c..c578f9c 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # # source from commands.sh to use the aliases diff --git a/modules/background.sh b/modules/background.sh index fba9649..3b81f28 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # source from commands.sh if you want ro use interactive or background jobs diff --git a/modules/inline.sh b/modules/inline.sh index f994b35..d3afd4d 100644 --- a/modules/inline.sh +++ b/modules/inline.sh @@ -5,6 +5,6 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # source from commands.sh to use the inline functions diff --git a/mycommands.sh b/mycommands.sh index 148bb60..4aa5ff5 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # # shellcheck disable=SC2154 # shellcheck disable=SC2034 diff --git a/test/ADD-test-new.sh b/test/ADD-test-new.sh index 9f9305d..b8b522b 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 63b6cca..7e7046a 100644 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME @@ -11,11 +11,12 @@ export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME TESTNAME="${REFDIR//-/ }" # common filenames -export TOKENFILE ACLFILE COUNTFILE ADMINFILE +export TOKENFILE ACLFILE COUNTFILE ADMINFILE DATADIR TOKENFILE="token" ACLFILE="botacl" COUNTFILE="count" ADMINFILE="botadmin" + DATADIR="data-bot-bash" # SUCCESS NOSUCCES export SUCCESS NOSUCCESS diff --git a/test/a-commit-test.sh b/test/a-commit-test.sh index 3f20fb1..9682712 100755 --- a/test/a-commit-test.sh +++ b/test/a-commit-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 ../dev/hooks/pre-commit.sh diff --git a/test/c-init-test.sh b/test/c-init-test.sh index be914f2..a1f5c98 100755 --- a/test/c-init-test.sh +++ b/test/c-init-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 a3b35fa..192ac8e 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -20,14 +20,14 @@ do [ "${i}" = "1" ] && echo " ... JSON.sh -s -b -n" [ "${i}" = "2" ] && echo " ... JSON.sh" set +f - for jsonfile in ${REFDIR}/*.log + for jsonfile in ${REFDIR}/*.in do set -f [ "${i}" = "1" ] && "${JSON}" -s -b -n <"${jsonfile}" >"${jsonfile}.out-${i}" [ "${i}" = "2" ] && "${JSON}" <"${jsonfile}" >"${jsonfile}.out-${i}" # output processed input - diff -c "${jsonfile}.result-${i}" "${jsonfile}.out-${i}" || exit 1 + diff -c "${jsonfile%.in}.result-${i}" "${jsonfile}.out-${i}" || exit 1 done echo "${SUCCESS}" done diff --git a/test/d-JSON.sh-test/JSON26783.log.in b/test/d-JSON.sh-test/JSON26783.log.in new file mode 100644 index 0000000..0edc2c4 --- /dev/null +++ b/test/d-JSON.sh-test/JSON26783.log.in @@ -0,0 +1,2 @@ +{"ok":true,"result":[{"update_id":146860898, +"message":{"message_id":6620,"from":{"id":123456789,"is_bot":false,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","language_code":"de"},"chat":{"id":123456789,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","type":"private"},"date":1555934213,"location":{"latitude":49.630443,"longitude":8.361698},"venue":{"location":{"latitude":49.630443,"longitude":8.361698},"title":"Vannini","address":"K\u00e4mmererstr. 3","foursquare_id":"4bf94ec05ec320a115f889d3"}}}]} diff --git a/test/d-JSON.sh-test/JSON30458.log.in b/test/d-JSON.sh-test/JSON30458.log.in new file mode 100644 index 0000000..eda2b1e --- /dev/null +++ b/test/d-JSON.sh-test/JSON30458.log.in @@ -0,0 +1,2 @@ +{"ok":true,"result":[{"update_id":146860896, +"message":{"message_id":6618,"from":{"id":123456789,"is_bot":false,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","language_code":"de"},"chat":{"id":123456789,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","type":"private"},"date":1555934195,"voice":{"duration":1,"mime_type":"audio/ogg","file_id":"AwADAgADKQMAAh638UnbhHGzIMozZgI","file_size":3963}}}]} diff --git a/test/d-JSON.sh-test/JSON32034.log.in b/test/d-JSON.sh-test/JSON32034.log.in new file mode 100644 index 0000000..7253f15 --- /dev/null +++ b/test/d-JSON.sh-test/JSON32034.log.in @@ -0,0 +1,2 @@ +{"ok":true,"result":[{"update_id":146860897, +"message":{"message_id":6619,"from":{"id":123456789,"is_bot":false,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","language_code":"de"},"chat":{"id":586928566,"first_name":"Kay","last_name":"M","username":"Gnadelwartz","type":"private"},"date":1555934204,"contact":{"phone_number":"222222","first_name":"ADAC Pannenhilfe","vcard":"BEGIN:VCARD\nVERSION:2.1\nN:Pannenhilfe;ADAC;;;\nFN:ADAC Pannenhilfe\nTEL;CELL;PREF:+49179222222\nTEL;X-Mobil:222222\nEND:VCARD"}}}]} diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index 0a89905..8e680f2 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 7eda80d..f71e93c 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 dafd32b..473543a 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.7-rc1-0-g8279bdb +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 # 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 new file mode 100755 index 0000000..8482449 --- /dev/null +++ b/test/e-env-test.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +#### $$VERSION$$ v0.70-rc1-0-g8883cc9 + +# include common functions and definitions +# shellcheck source=test/ALL-tests.inc.sh +source "./ALL-tests.inc.sh" + +set -e + +#cd "${TESTDIR}" || exit 1 + +# source bashbot.sh function, uncomment if you want to test functions +# shellcheck source=./bashbot.sh +# source "\/bashbot.sh" source + +# start writing your tests here ... +# test setting of env variables to different locations + +export BASHBOT_ETC="${TESTDIR}/env/etc/bashbot" +export BASHBOT_VAR="${TESTDIR}/env/var/bashbot" +export BASHBOT_JSONSH="${TESTDIR}/env/local/bin/JSON.sh" +BASHBOT_BIN="${TESTDIR}/env/local/bin" + +# create dirs +mkdir -p "${BASHBOT_ETC}" || exit 1 +mkdir -p "${BASHBOT_VAR}" || exit 1 +mkdir -p "${BASHBOT_BIN}" || exit 1 + +# cp bashbot files to new localtions +set +f +# shellcheck disable=SC2086 +cp ${TESTDIR}/*commands.sh "${BASHBOT_ETC}" || exit 1 +set -f +cp "${TESTDIR}/bashbot.sh" "${BASHBOT_BIN}" || exit 1 + +TESTTOKEN="bashbottestscript" +TESTFILES="${TOKENFILE} ${ACLFILE} ${ADMINFILE}" + + +echo "Check first run in ENVIRONMENT ..." + +# run bashbot first time with init +"${BASHBOT_BIN}/bashbot.sh" init >"${LOGFILE}" <>"${LOGFILE}" + if ! diff -q "${BASHBOT_ETC}/${file}" "${REFDIR}/${file}" >>"${LOGFILE}"; then echo "${NOSUCCESS} Fail diff ${file}!"; FAIL="1"; fi + +done + +echo " ... BASHBOT_ETC seems to work!" +echo "${SUCCESS}" diff --git a/test/e-env-test/botacl b/test/e-env-test/botacl new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/e-env-test/botacl @@ -0,0 +1 @@ + diff --git a/test/e-env-test/botadmin b/test/e-env-test/botadmin new file mode 100644 index 0000000..a1e2647 --- /dev/null +++ b/test/e-env-test/botadmin @@ -0,0 +1 @@ +? diff --git a/test/e-env-test/token b/test/e-env-test/token new file mode 100644 index 0000000..60557b1 --- /dev/null +++ b/test/e-env-test/token @@ -0,0 +1 @@ +bashbottestscript