Files
waving-hands/deploy/waving-hands-say.sh
T

19 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# A word from the keeper in a table's talk, signed as the keeper, whether or
# not the host lets the gallery talk: the answer to a call that came too late,
# or anything else the table should hear from the keeper. Runs ON the droplet,
# through the duel server's keeper door.
# waving-hands-say.sh ROOM "text"
set -euo pipefail
ROOM="${1:?usage: waving-hands-say.sh ROOM \"text\"}"
TEXT="${2:?usage: waving-hands-say.sh ROOM \"text\"}"
DOOR="http://127.0.0.1:8788/keeper/rooms/${ROOM^^}"
[ -s /etc/waving-hands/keeper.key ] || { echo "No keeper's key at /etc/waving-hands/keeper.key; a deploy makes it." >&2; exit 1; }
# The key goes as a header read from a file descriptor, never on a command line another user could see.
KEY="@/dev/fd/3"
exec 3< <(printf 'x-keeper-key: %s\n' "$(cat /etc/waving-hands/keeper.key)")
BODY=$(TEXT="$TEXT" python3 -c 'import json, os; print(json.dumps({"text": os.environ["TEXT"]}))')
CODE=$(curl -s -o /tmp/waving-hands-say.$$ -w '%{http_code}' -X POST -H "$KEY" -H 'content-type: application/json' -d "$BODY" "$DOOR/say")
if [ "$CODE" = 204 ]; then echo "said at ${ROOM^^}"; else cat /tmp/waving-hands-say.$$ >&2; echo >&2; rm -f /tmp/waving-hands-say.$$; exit 1; fi
rm -f /tmp/waving-hands-say.$$