Skip to content

Commit aee7bae

Browse files
authored
Merge branch 'develop' into master
2 parents 622a394 + 3f848ac commit aee7bae

35 files changed

+750
-270
lines changed

README.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,6 @@ <h3>Blocked by telegram?</h3>
392392
<p>@Gnadelwartz</p>
393393
<h2>That's it all guys!</h2>
394394
<p>If you feel that there's something missing or if you found a bug, feel free to submit a pull request!</p>
395-
<h4>$$VERSION$$ v1.40-dev-34-g1440d56</h4>
395+
<h4>$$VERSION$$ v1.41-0-gad1b91f</h4>
396396
</body>
397397
</html>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,4 @@ See `mycommnds.sh.dist` for an example.
241241

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

244-
#### $$VERSION$$ v1.40-0-gf9dab50
244+
#### $$VERSION$$ v1.41-0-gad1b91f

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,5 @@ That's it all guys!
318318
If you feel that there's something missing or if you found a bug, feel free to submit a
319319
pull request!
320320

321-
$$VERSION$$ v1.40-dev-34-g1440d56
321+
$$VERSION$$ v1.41-0-gad1b91f
322322

bashbot.sh

Lines changed: 94 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb
3030
# 8 - curl/wget missing
3131
# 10 - not bash!
3232
#
33-
#### $$VERSION$$ v1.40-0-gf9dab50
33+
#### $$VERSION$$ v1.45-dev-26-g82a57a7
3434
##################################################################
3535

3636
# are we running in a terminal?
@@ -99,11 +99,15 @@ getConfigKey() {
9999
[[ "$1" =~ ^[-${azAZo9},._]+$ ]] || return 3
100100
[ -r "${BOTCONFIG}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' "${BOTCONFIG}.jssh" | tail -n 1
101101
}
102-
# escape / remove text characters for json strings, eg. " -> \"
103-
# $1 string
104-
# output escaped string
102+
# escape characters in json strings for telegram
103+
# $1 string, output escaped string
105104
JsonEscape(){
106-
sed 's/\([-"`´,§$%&/(){}#@!?*.\t]\)/\\\1/g' <<< "$1"
105+
sed -E -e 's/\r//g' -e 's/([-"`´,§$%&/(){}#@!?*.\t])/\\\1/g' <<< "${1//$'\n'/\\n}"
106+
}
107+
# clean \ from escaped json string
108+
# $1 string, output cleaned string
109+
cleanEscaped(){ # remove " all \ but \n\u \n or \r
110+
sed -E -e 's/\\"/+/g' -e 's/\\([^nu])/\1/g' -e 's/(\r|\n)//g' <<<"$1"
107111
}
108112
# check if $1 seems a valid token
109113
# return true if token seems to be valid
@@ -157,7 +161,7 @@ debug_checks(){ {
157161
[ -z "$(getConfigKey "botadmin")" ] && printf "%(%c)T: %s\n" -1 "Bot admin is missing! =========="
158162
# call user defined debug_checks if exists
159163
_exec_if_function my_debug_checks "$(_date)" "${where}" "$*"
160-
} >>"${DEBUGLOG}"
164+
} 2>/dev/null >>"${DEBUGLOG}"
161165
}
162166

163167
# some Linux distributions (e.g. Manjaro) doesn't seem to have C locale activated by default
@@ -174,10 +178,10 @@ RUNDIR="$(dirname "$0")"
174178
MODULEDIR="${SCRIPTDIR}/modules"
175179

176180
# adjust stuff for source, use return from source without source
177-
alias exit_source='exit'
181+
exit_source() { exit "$1"; }
178182
if [[ "${SCRIPT}" != "${REALME}" || "$1" == "source" ]]; then
179183
SOURCE="yes"
180-
[ -z "$1" ] && alias exit_source='printf "Exit from source ...\n";return'
184+
[ -z "$1" ] && exit_source() { printf "Exit from source ...\n"; return "$1"; }
181185
fi
182186

183187
# emmbeded system may claim bash but it is not
@@ -271,6 +275,7 @@ if [ -z "${BOTTOKEN}" ]; then
271275
[ -z "${admin}" ] && admin='?'
272276
printf '["botadmin"]\t"%s"\n' "${admin}" >> "${BOTCONFIG}.jssh"
273277
fi
278+
274279
# setup botacl file
275280
if [ ! -f "${BOTACL}" ]; then
276281
printf "${GREY}Create initial ${BOTACL} file.${NN}"
@@ -279,15 +284,14 @@ if [ -z "${BOTTOKEN}" ]; then
279284
# check data dir file
280285
if [ ! -w "${DATADIR}" ]; then
281286
printf "${RED}ERROR: ${DATADIR} does not exist or is not writeable!.${NN}"
282-
exit_source 2
287+
[ "$1" != "init" ] && exit_source 2 # skip on init
283288
fi
284289
# setup count file
285290
if [ ! -f "${COUNTFILE}.jssh" ]; then
286291
printf '["counted_user_chat_id"]\t"num_messages_seen"\n' >> "${COUNTFILE}.jssh"
287292
elif [ ! -w "${COUNTFILE}.jssh" ]; then
288-
printf "${RED}ERROR: Can't write to ${COUNTFILE}!.${NN}"
293+
printf "${RED}WARNING: Can't write to ${COUNTFILE}!.${NN}"
289294
ls -l "${COUNTFILE}.jssh"
290-
exit_source 2
291295
fi
292296
# setup blocked file
293297
if [ ! -f "${BLOCKEDFILE}.jssh" ]; then
@@ -342,6 +346,7 @@ fi
342346
BASHBOT_RETRY="" # retry by default
343347

344348
URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}"
349+
FILEURL="${URL%%/bot*}/file/bot${BOTTOKEN}"
345350
ME_URL=${URL}'/getMe'
346351

347352
#################
@@ -388,16 +393,6 @@ if ! _is_function jssh_newDB; then
388393
exit_source 6
389394
fi
390395

391-
# $1 URL, $2 filename in DATADIR
392-
# outputs final filename
393-
download() {
394-
local empty="no.file" file="${2:-${empty}}"
395-
if [[ "${file}" = *"/"* ]] || [[ "${file}" = "."* ]]; then file="${empty}"; fi
396-
while [ -f "${DATADIR:-.}/${file}" ] ; do file="${RANDOM}-${file}"; done
397-
getJson "$1" >"${DATADIR:-.}/${file}" || return
398-
printf '%s\n' "${DATADIR:-.}/${file}"
399-
}
400-
401396
# $1 postfix, e.g. chatid
402397
# $2 prefix, back- or startbot-
403398
procname(){
@@ -425,19 +420,56 @@ killallproc() {
425420
debug_checks "end killallproc" "$1"
426421
}
427422

428-
429-
# $ chat $2 msg_id $3 nolog
430-
declare -xr DELETE_URL=${URL}'/deleteMessage'
431-
delete_message() {
432-
[ -z "$3" ] && log_update "Delete Message CHAT=$1 MSG_ID=$2"
433-
sendJson "$1" '"message_id": '"$2"'' "${DELETE_URL}"
434-
}
435-
436-
# get download url for file id, $1 file_id
423+
# URL path for file id, $1 file_id
424+
# use download_file "path" to download file
437425
get_file() {
438426
[ -z "$1" ] && return
439427
sendJson "" '"file_id": "'"$1"'"' "${URL}/getFile"
440-
printf "%s\n" "${URL}/${UPD["result,file_path"]}"
428+
printf "%s\n" "${UPD["result,file_path"]}"
429+
}
430+
# download file to DATADIR
431+
# $1 URL path, $2 proposed filename (may modified/ignored)
432+
# outputs final filename
433+
# keep old function name for backward compatibility
434+
alias download="download_file"
435+
download_file() {
436+
local url="$1" file="${2:-$1}"
437+
# old mode if full URL is given
438+
if [[ "${1}" =~ ^https*:// ]]; then
439+
# random filename if not given for http
440+
if [ -z "$2" ]; then
441+
: "$(mktemp -u -p . "XXXXXXXXXX" 2>/dev/null)"
442+
file="download-${_#./}"
443+
fi
444+
else
445+
# prefix https://api.telegram...
446+
url="${FILEURL}/${url}"
447+
fi
448+
# filename: replace "/" with "-", use mktemp if exist
449+
file="${DATADIR:-.}/${file//\//-}"
450+
[ -f "${file}" ] && file="$(mktemp -p "${DATADIR:-.}" "XXXXX-${file##*/}" )"
451+
getJson "${url}" >"${file}" || return
452+
# output absolute file path
453+
printf "%s\n" "$(cd "${file%/*}" >/dev/null 2>&1 && pwd)/${file##*/}"
454+
}
455+
# notify mycommands about errors while sending
456+
# $1 calling function $2 error $3 chat $4 user $5 error message $6 ... remaining args to calling function
457+
# calls function based on error: bashbotError{function} basbotError{error}
458+
# if no specific function exist try to call bashbotProcessError
459+
processError(){
460+
local func="$1" err="$2"
461+
[[ "${err}" != "4"* ]] && return 1
462+
# check for bashbotError${func} provided in mycommands
463+
# shellcheck disable=SC2082
464+
if _is_function "bashbotError_${func}"; then
465+
"bashbotError_${func}" "$@"
466+
# check for bashbotError${err} provided in mycommands
467+
elif _is_function "bashbotError_${err}"; then
468+
"bashbotError_${err}" "$@"
469+
# noting found, try bashbotProcessError
470+
else
471+
_exec_if_function bashbotProcessError "$@"
472+
fi
441473
}
442474

443475
# iconv used to filter out broken utf characters, if not installed fake it
@@ -461,7 +493,7 @@ sendJson(){
461493
if [ -n "${BASHBOTDEBUG}" ] ; then
462494
log_update "sendJson (${DETECTED_CURL}) CHAT=${chat#*:} JSON=${2:0:100} URL=${3##*/}"
463495
# mask " and \ , remove newline from json
464-
log_message "DEBUG sendJson ==========\n$("${JSONSHFILE}" -b -n <<<"$(sed -E -e 's/\\"/+/g' -e 's/\\/\\\\/g' -e 's/(\r|\n)//g' <<<"${json}")" 2>&1)"
496+
log_message "DEBUG sendJson ==========\n$("${JSONSHFILE}" -b -n <<<"$(cleanEscaped "${json}")" 2>&1)"
465497
fi
466498
# chat id not a number
467499
if [[ "${chat}" == *"NAN\"," ]]; then
@@ -477,6 +509,36 @@ sendJson(){
477509
[ -n "${BASHBOT_EVENT_SEND[*]}" ] && event_send "send" "${@}" &
478510
}
479511

512+
UPLOADDIR="${BASHBOT_UPLOAD:-${DATADIR}/upload}"
513+
514+
# $1 chat $2 file, $3 calling function
515+
# return final file name or empty string on error
516+
checkUploadFile() {
517+
local err file="$2"
518+
[[ "${file}" = *'..'* || "${file}" = '.'* ]] && err=1 # no directory traversal
519+
if [[ "${file}" = '/'* ]] ; then
520+
[[ ! "${file}" =~ ${FILE_REGEX} ]] && err=2 # absolute must match REGEX
521+
else
522+
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # others must be in UPLOADDIR
523+
fi
524+
[ ! -r "${file}" ] && err=3 # and file must exits of course
525+
# file path error, generate error response
526+
if [ -n "${err}" ]; then
527+
BOTSENT=(); BOTSENT[OK]="false"
528+
case "${err}" in
529+
1) BOTSENT[ERROR]="Path to file $2 contains to much '../' or starts with '.'";;
530+
2) BOTSENT[ERROR]="Path to file $2 does not match regex: ${FILE_REGEX} ";;
531+
3) if [[ "$2" == "/"* ]];then
532+
BOTSENT[ERROR]="File not found: $2"
533+
else
534+
BOTSENT[ERROR]="File not found: ${UPLOADDIR}/$2"
535+
fi;;
536+
esac
537+
[ -n "${BASHBOTDEBUG}" ] && log_debug "$3: CHAT=$1 FILE=$2 MSG=${BOTSENT[DESCRIPTION]}"
538+
return 1
539+
fi
540+
}
541+
480542

481543
#
482544
# curl / wget specific functions

bin/any_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ USAGE='any_command.sh [-h|--help] [--force|--reference] bot_command args ...'
2121
# AUTHOR: KayM (gnadelwartz), [email protected]
2222
# CREATED: 30.01.2021 10:24
2323
#
24-
#### $$VERSION$$ v1.40-0-gf9dab50
24+
#### $$VERSION$$ v1.45-dev-7-ga9ed559
2525
#===============================================================================
2626

2727
####

bin/bashbot_init.inc.sh

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
#
66
# USAGE: source bashbot_init.inc.sh
77
#
8-
# DESCRIPTION: extend / overwrite bashbot initialisation
8+
# DESCRIPTION: extend / overwrite bashbot initialisation
99
#
1010
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
1111
# AUTHOR: KayM (gnadelwartz), [email protected]
1212
# CREATED: 27.01.2021 13:42
1313
#
14-
#### $$VERSION$$ v1.40-0-gf9dab50
14+
#### $$VERSION$$ v1.45-dev-3-g429c230
1515
#===============================================================================
1616
# shellcheck disable=SC2059
1717

@@ -54,17 +54,23 @@ bot_init() {
5454
[ -r "${addons}" ] && source "${addons}" "init" "${DEBUG}"
5555
done
5656
printf "Done.\n"
57-
# ask for bashbot user
58-
# shellcheck disable=SC2153
59-
runuser="${RUNUSER}"; [ "${UID}" = "0" ] && runuser="nobody"
57+
# guess bashbot from botconfig.jssh owner:group
58+
[ -f "${BOTCONFIG}.jssh" ] && runuser="$(stat -c '%U' "${BOTCONFIG}.jssh"):$(stat -c '%G' "${BOTCONFIG}.jssh")"
59+
# empty or ":" use user running init, nobody for root
60+
if [ "${#runuser}" -lt 3 ]; then
61+
# shellcheck disable=SC2153
62+
runuser="${RUNUSER}"
63+
[ "${UID}" = "0" ] && runuser="nobody"
64+
fi
6065
printf "Enter User to run bashbot [${runuser}]: "
6166
read -r chown
62-
[ -z "${chown}" ] && chown="${runuser}"; touser="${chown%:*}"
67+
[ -z "${chown}" ] && chown="${runuser}"
68+
touser="${chown%:*}"
6369
# check user ...
6470
if ! id "${touser}" &>/dev/null; then
6571
printf "${RED}User \"${touser}\" does not exist!${NN}"
6672
exit 3
67-
elif [[ "${UID}" != "0" && "${touser}" != "${runuser}" ]]; then
73+
elif [ "${UID}" != "0" ]; then
6874
# different user but not root ...
6975
printf "${ORANGE}You are not root, adjusting permissions may fail. Try \"sudo ./bashbot.sh init\"${NN}Press <CTRL+C> to stop or <Enter> to continue..." 1>&2
7076
[ -n "${INTERACTIVE}" ] && read -r runuser
@@ -98,13 +104,13 @@ bot_init() {
98104
fi
99105
# adjust permissions
100106
printf "Adjusting files and permissions for user \"${touser}\" ...\n"
107+
chown -Rf "${chown}" . ./*
101108
chmod 711 .
102109
chmod -R o-w ./*
103110
chmod -R u+w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" logs "${LOGDIR}/"*.log 2>/dev/null
104111
chmod -R o-r,o-w "${COUNTFILE}"* "${BLOCKEDFILE}"* "${DATADIR}" "${BOTACL}" 2>/dev/null
105112
# jsshDB must writeable by owner
106113
find . -name '*.jssh*' -exec chmod u+w \{\} +
107-
chown -Rf "${chown}" . ./*
108114
printf "Done.\n"
109115
# adjust values in bashbot.rc
110116
if [ -w "bashbot.rc" ]; then

bin/process_update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [<file]'
1515
# AUTHOR: KayM (gnadelwartz), [email protected]
1616
# CREATED: 30.01.2021 19:14
1717
#
18-
#### $$VERSION$$ v1.40-0-gf9dab50
18+
#### $$VERSION$$ v1.45-dev-30-g8efbfca
1919
#===============================================================================
2020

2121
####

bin/send_dice.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC1090,SC2034
3+
#===============================================================================
4+
#
5+
# FILE: bin/send_dice.sh
6+
#
7+
USAGE='send_dice.sh [-h|--help] "CHAT[ID]" "emoji" [debug]'
8+
#
9+
# DESCRIPTION: send an animated emoji (dice) to given chat
10+
#
11+
# OPTIONS: CHAT[ID] - ID number of CHAT or BOTADMIN to send to yourself
12+
# emoji - must be one of: “🎲”, “🎯”, “🏀”, “⚽”, or “🎰”
13+
# ":game_die:" ":dart:" ":basketball:" ":soccer:" :slot_machine:"
14+
#
15+
# -h - display short help
16+
# --help - this help
17+
#
18+
# Set BASHBOT_HOME to your installation directory
19+
#
20+
# LICENSE: WTFPLv2 http://www.wtfpl.net/txt/copying/
21+
# AUTHOR: KayM (gnadelwartz), [email protected]
22+
# CREATED: 07.02.2021 18:45
23+
#
24+
#### $$VERSION$$ v1.45-dev-8-g069570e
25+
#===============================================================================
26+
27+
####
28+
# parse args
29+
SEND="send_dice"
30+
31+
# set bashbot environment
32+
source "${0%/*}/bashbot_env.inc.sh" "${3:-debug}" # $5 debug
33+
print_help "$1"
34+
35+
####
36+
# ready, do stuff here -----
37+
if [ "$1" == "BOTADMIN" ]; then
38+
CHAT="${BOT_ADMIN}"
39+
else
40+
CHAT="$1"
41+
fi
42+
43+
# send message in selected format
44+
"${SEND}" "${CHAT}" "$2"
45+
46+
# output send message result
47+
print_result

commands.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# This file is public domain in the USA and all free countries.
1616
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
1717
#
18-
#### $$VERSION$$ v1.40-0-gf9dab50
18+
#### $$VERSION$$ v1.45-dev-9-g62b6b61
1919
#
2020

2121
# bashbot locale defaults to c.UTF-8, adjust locale in mycommands.sh if needed
@@ -42,10 +42,12 @@ bashbot_help='
4242
*• /start*: _Start bot and get this message_.
4343
*• /help*: _Get this message_.
4444
*• /info*: _Get shorter info message about this bot_.
45-
*• /question*: _Start interactive chat (mycommands.dist)_.
46-
*• /cancel*: _Cancel any currently running interactive chat_.
4745
*• /kickme*: _You will be autokicked from the group_.
4846
*• /leavechat*: _The bot will leave the group with this command _.
47+
Additional commands from mycommands.dist ...
48+
*• /game*: _throw a die_.
49+
*• /question*: _Start interactive chat_.
50+
*• /cancel*: _Cancel any currently running interactive chat_.
4951
Written by Drew (@topkecleon) and KayM (@gnadelwartz).
5052
Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash)
5153
'

dev/all-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# Description: run all tests, exit after failed test
77
#
8-
#### $$VERSION$$ v1.40-0-gf9dab50
8+
#### $$VERSION$$ v1.45-dev-21-ge67e43d
99
#############################################################
1010

1111
#shellcheck disable=SC1090

0 commit comments

Comments
 (0)