#! /bin/sh
#
# notify-by-matrix
# Copyright (C) 2021 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#

set -e

# Set our Matrix-related vars here
MX_TOKEN="{{ nagios_matrix_token }}"
MX_SERVER="{{ nagios_matrix_server }}"
MX_ROOM="{{ nagios_matrix_room }}"

# Get a TXN to prefix this particular message with
MX_TXN="$(date "+%s")$(( RANDOM % 9999 ))"

# Read a message from STDIN
# NOTE: This is dangerous and stupid and unsanitized
read message
while read line; do
	message="${message}\n${line}"
done

# Push it to the channel
curl -X PUT \
	--header 'Content-Type: application/json' \
	--header 'Accept: application/json' \
	-d "{
		\"msgtype\": \"m.text\",
		\"body\": \"$message\"
	}" \
	"$MX_SERVER/_matrix/client/unstable/rooms/$MX_ROOM/send/m.room.message/$MX_TXN?access_token=$MX_TOKEN"