From 5a689d22de1b4de1e77a25f3d9adc63af5468105 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Fri, 5 Feb 2021 14:46:11 +0100 Subject: [PATCH] JsonEscape: handle \r \n also --- bashbot.sh | 4 ++-- modules/sendMessage.sh | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 588619b..48b7ca4 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb # 8 - curl/wget missing # 10 - not bash! # -#### $$VERSION$$ v1.41-dev-0-gd15b4f5 +#### $$VERSION$$ v1.41-dev-1-g58fb001 ################################################################## # are we running in a terminal? @@ -102,7 +102,7 @@ getConfigKey() { # escape characters in json strings for telegram # $1 string, output escaped string JsonEscape(){ - sed 's/\([-"`´,§$%&/(){}#@!?*.\t]\)/\\\1/g' <<< "$1" + sed -E -e 's/\r//g' -e 's/([-"`´,§$%&/(){}#@!?*.\t])/\\\1/g' <<< "${1//$'\n'/\\n}" } # clean \ from escaped json string # $1 string, output cleaned string diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index 4185948..2d23e8c 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.40-0-gf9dab50 +#### $$VERSION$$ v1.41-dev-1-g58fb001 # will be automatically sourced from bashbot @@ -25,7 +25,6 @@ EDIT_URL=${URL}'/editMessageText' # $1 CHAT $2 message send_normal_message() { local len text; text="$(JsonEscape "$2")" - text="${text//$'\n'/\\n}" until [ -z "${text}" ]; do if [ "${#text}" -le 4096 ]; then sendJson "$1" '"text":"'"${text}"'"' "${MSG_URL}" @@ -85,7 +84,6 @@ edit_message_caption() { # $1 CHAT $2 message $3 action $4 URL _format_message_url(){ local text; text="$(JsonEscape "$2")" - text="${text//$'\n'/\\n}" [ "${#text}" -ge 4096 ] && log_error "Warning: html/markdown message longer than 4096 characters, message is rejected if formatting crosses 4096 border." until [ -z "${text}" ]; do sendJson "$1" '"text":"'"${text:0:4096}"'"'"$3"'' "$4" @@ -97,7 +95,6 @@ _format_message_url(){ # $1 CHAT $2 message $3 action $4 URL _markdownv2_message_url() { local text; text="$(JsonEscape "$2")" - text="${text//$'\n'/\\n}" [ "${#text}" -ge 4096 ] && log_error "Warning: markdownv2 message longer than 4096 characters, message is rejected if formatting crosses 4096 border." # markdown v2 needs additional double escaping! text="$(sed -E -e 's|([_|~`>+=#{}()!.-])|\\\1|g' <<< "${text}")"