JsonEscape: handle \r \n also

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-02-05 14:46:11 +01:00
parent 58fb001d1e
commit 5a689d22de
2 changed files with 3 additions and 6 deletions

View File

@ -30,7 +30,7 @@ BOTCOMMANDS="-h help init start stop status suspendback resumeback killb
# 8 - curl/wget missing # 8 - curl/wget missing
# 10 - not bash! # 10 - not bash!
# #
#### $$VERSION$$ v1.41-dev-0-gd15b4f5 #### $$VERSION$$ v1.41-dev-1-g58fb001
################################################################## ##################################################################
# are we running in a terminal? # are we running in a terminal?
@ -102,7 +102,7 @@ getConfigKey() {
# escape characters in json strings for telegram # escape characters in json strings for telegram
# $1 string, output escaped string # $1 string, output escaped string
JsonEscape(){ 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 # clean \ from escaped json string
# $1 string, output cleaned string # $1 string, output cleaned string

View File

@ -6,7 +6,7 @@
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
# shellcheck disable=SC1117 # shellcheck disable=SC1117
#### $$VERSION$$ v1.40-0-gf9dab50 #### $$VERSION$$ v1.41-dev-1-g58fb001
# will be automatically sourced from bashbot # will be automatically sourced from bashbot
@ -25,7 +25,6 @@ EDIT_URL=${URL}'/editMessageText'
# $1 CHAT $2 message # $1 CHAT $2 message
send_normal_message() { send_normal_message() {
local len text; text="$(JsonEscape "$2")" local len text; text="$(JsonEscape "$2")"
text="${text//$'\n'/\\n}"
until [ -z "${text}" ]; do until [ -z "${text}" ]; do
if [ "${#text}" -le 4096 ]; then if [ "${#text}" -le 4096 ]; then
sendJson "$1" '"text":"'"${text}"'"' "${MSG_URL}" sendJson "$1" '"text":"'"${text}"'"' "${MSG_URL}"
@ -85,7 +84,6 @@ edit_message_caption() {
# $1 CHAT $2 message $3 action $4 URL # $1 CHAT $2 message $3 action $4 URL
_format_message_url(){ _format_message_url(){
local text; text="$(JsonEscape "$2")" 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." [ "${#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 until [ -z "${text}" ]; do
sendJson "$1" '"text":"'"${text:0:4096}"'"'"$3"'' "$4" sendJson "$1" '"text":"'"${text:0:4096}"'"'"$3"'' "$4"
@ -97,7 +95,6 @@ _format_message_url(){
# $1 CHAT $2 message $3 action $4 URL # $1 CHAT $2 message $3 action $4 URL
_markdownv2_message_url() { _markdownv2_message_url() {
local text; text="$(JsonEscape "$2")" 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." [ "${#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! # markdown v2 needs additional double escaping!
text="$(sed -E -e 's|([_|~`>+=#{}()!.-])|\\\1|g' <<< "${text}")" text="$(sed -E -e 's|([_|~`>+=#{}()!.-])|\\\1|g' <<< "${text}")"