From dd19f0f34e145397472b48427506e120bbc9c145 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 5 Jan 2021 22:01:32 +0100 Subject: [PATCH] modules: send_file support for URL --- bashbot.sh | 10 +++++----- bin/send_file.sh | 5 +++-- modules/sendMessage.sh | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 95978b3..25d6b61 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.25-dev-14-g2fe6d4b +#### $$VERSION$$ v1.25-dev-15-g4582efd ################################################################## # emmbeded system may claim bash but it is not @@ -487,13 +487,13 @@ if detect_curl ; then sendUpload() { [ "$#" -lt 4 ] && return if [ -n "$5" ]; then - [ -n "${BASHBOTDEBUG}" ] &&\ - log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5" - # shellcheck disable=SC2086 + [ -n "${BASHBOTDEBUG}" ] &&\ + log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5" + # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -b -n 2>/dev/null )" else - # shellcheck disable=SC2086 + # shellcheck disable=SC2086 res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\ -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -b -n 2>/dev/null )" fi diff --git a/bin/send_file.sh b/bin/send_file.sh index 958dd31..585ddf4 100755 --- a/bin/send_file.sh +++ b/bin/send_file.sh @@ -21,7 +21,7 @@ # AUTHOR: KayM (gnadelwartz), kay@rrr.de # CREATED: 25.12.2020 20:24 # -#### $$VERSION$$ v1.25-dev-14-g2fe6d4b +#### $$VERSION$$ v1.25-dev-15-g4582efd #=============================================================================== #### @@ -54,7 +54,8 @@ else fi FILE="$2" -[[ "$2" != "/"* ]] && FILE="${PWD}/$2" +# convert to absolute path if not start with / or http:// +[[ ! ( "$2" == "/"* || "$2" =~ ^https*:// ) ]] && FILE="${PWD}/$2" # send message in selected format "${SEND}" "${CHAT}" "${FILE}" "$3" diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index a140c38..b120023 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -6,7 +6,7 @@ # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # # shellcheck disable=SC1117 -#### $$VERSION$$ v1.25-dev-14-g2fe6d4b +#### $$VERSION$$ v1.25-dev-15-g4582efd # will be automatically sourced from bashbot @@ -205,19 +205,28 @@ send_file() { fi } +# supports http and local file +# $1 chat, $2 file, $3 caption, $4 extension (optional) upload_file(){ - local CUR_URL WHAT STATUS text=$3 file="$2" - # file access checks ... - [[ "${file}" = *'..'* ]] && return 1 # no directory traversal - [[ "${file}" = '.'* ]] && return 1 # no hidden or relative files - if [[ "${file}" = '/'* ]] ; then - [[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX + local CUR_URL WHAT STATUS media text file="$2" ext="$4" + text="$(JsonEscape "$3")" + if [[ "${file}" =~ ^https*:// ]]; then + media="URL" else - file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR + # we have a file, check file location ... + media="FILE" + [[ "${file}" = *'..'* ]] && return 1 # no directory traversal + [[ "${file}" = '.'* ]] && return 1 # no hidden or relative files + if [[ "${file}" = '/'* ]] ; then + [[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX + else + file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR + fi + [ ! -r "${file}" ] && return 3 # and file must exits of course fi - [ ! -r "${file}" ] && return 3 # and file must exits of course - case "${file##*.}" in + [ -z "${ext}" ] && ext="${file##*.}" + case "${ext}" in mp3|flac) CUR_URL="${AUDIO_URL}" WHAT="audio" @@ -250,8 +259,19 @@ upload_file(){ STATUS="upload_document" ;; esac + + # prepare to send FILE / URL send_action "$1" "${STATUS}" - sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}" + # select method to use + case "${media}" in + FILE) # send local file ... + sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}";; + + URL) # send URL, should also work for file_id ... + # e.g. '"photo":"https://dealz.rrr.de/assets/images/rbofd-1.gif","caption":"some text"' + sendJson "$1" '"'"${WHAT}"'":"'"${file}"'","caption":"'"${text//\\n/$'\n'}"'"' "${CUR_URL}" + esac + return 0 } # 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