fabianonline / matrix.sh

Send messages via matrix chat protocol right from your command line
125 stars 23 forks source link

Sending a single line string appends "\n" to it #15

Closed efelon closed 1 year ago

efelon commented 2 years ago

This is only a "cosmetics" report.

I'm on

I can send text messages without problems, but when I'm sending a file like this:

/opt/matrix_cli/matrix.sh --identifier='system.internal' --room='!***********:domain.cc' --file="/my/diff.txt"

The raw data of the message show

"content": {
    "body": "diff.txt\n",
    "msgtype": "m.file",
    "filename": "diff.txt\n",
    "url": "mxc://domain.cc/********************"

and when trying to download the file, it will be saved as diff.txt_. Content of the file is fine.

Let me add that I'm so happy about your script. It also did work right away.

efelon commented 2 years ago

The culprit adding the \n is the escape() function on line 105. More specific the "-s" option for jq.

I tested a little without the -s and it looked good to me.

escape() {
  jq -R . <<<"$1"
}

From the man page of jq I see that this (-s and -R) is used to handle multi-line input, useful for message input indeed. But for filename, IDENTIFIER or displayname it should not be there.

One solution could be to test if the input to escape has multiple lines:

escape() {
  local multil=
  [ $(echo "$1" | wc -l) -gt 1 ] && multil="-s"
  jq $multil -R . <<<"$1"
}
efelon commented 1 year ago

Fixed with: https://github.com/fabianonline/matrix.sh/commit/38ecb51d9b16b90dfcc967df8ba8fab4f375fdf5