The first snippet

This commit is contained in:
Éibhear Ó hAnluain 2023-11-28 12:58:39 +00:00
commit 729e223f25
Signed by: eibhear
GPG key ID: D124FE9CF2177106
2 changed files with 138 additions and 0 deletions

9
README.org Normal file
View file

@ -0,0 +1,9 @@
* Éibhear's public snippets and code samples
Here you'll see some snippets and samples of code that I find
useful. You're welcome to take a copy and do what you want with
anything you find in this repository.
Consider what you find here, therefore, to be effectively in the
Public Domain.

129
busy-on-matrix.sh Normal file
View file

@ -0,0 +1,129 @@
#!/bin/bash
## A simple script that will update a specific set of Matrix rooms
## with a simple notice message and a custom event depending on
## whether you're busy or not
## Where the below says "url-encoded" it means special characters
## (e.g. "!") are in the url-encoded form (i.e. "%21")
# Change the display name, based on the state we want to be in.
BUSY_STATE="I am busy right now"
FREE_STATE="I am available now"
TARGET_STATE=""
# Room to test for what the current state is.
TEST_ROOM="<url-encoded matrix room>"
MATRIX_SERVER="<matrix server where your log in (e.g. https://matrix.org)>"
ACCESS_TOKEN="<an access token for one of your sessions>"
# My ID
ME="<your url-encoded matrix ID (e.g. %40eibhear%3Amatrix.org)"
# An associative array. It's structured this way so that I can quickly
# look to see what each room ID is.
declare -A ROOM_LIST
## The ROOM_LIST is a bash array of room identifiers, each in the
## format of [<label>]=<url-encoded matrix room id>. The label is just
## to help you manage this list. The list below is just an example.
ROOM_LIST=([MatrixHQ]=%21OGEhHVWSdvArJzumhm:matrix.org
[SynapseAdmins]=%21ehXvUhWNASUkSLvAGP:matrix.org
[myTestRoom]=${TEST_ROOM})
usage () {
echo "Usage: $(basename ${0}) [ -h | [ -d [ -d ] ] | -q | -b | -f | -t ]"
echo
echo "Alert a set of matrix rooms that I am either busy and would like not"
echo "to be disturbed, or that I am now free."
echo
echo "Where ..."
echo " -h: Print out this help and exit."
echo " -d: Debug output (a second -d means extra debug output)."
echo " -q: Query the current status."
echo " -b: Declare that I am busy regardless of the current status."
echo " -f: Declare that I am free regardless of the current status."
echo " -t: I'm running a test, don't spam everyone."
echo
}
# Processing command-line options.
export DEBUG=N
export QUERY_ONLY=N
while getopts "dqbfth" opt; do
case ${opt} in
d) # We want DEBUG output
if [ "${DEBUG}" = "N" ]; then
DEBUG=Y
else
set -x
fi
# if the command-line includes " -d -d" or "-dd" bash -x is used.
;;
q) # We're just querying
QUERY_ONLY=Y
;;
b) # Set to busy regardless
TARGET_STATE=${BUSY_STATE}
;;
f) # Set to free regardless
TARGET_STATE=${FREE_STATE}
;;
t) # We're testing something, so we set the ROOM_LIST to just that
# one test room.
unset ROOM_LIST
ROOM_LIST=([myTestRoom]=${TEST_ROOM})
;;
h) # Help!
usage
exit 0
;;
*) echo "Oops"
exit 1
;;
esac
done
## org.gibiris.availability is a custom event, as I don't know what
## the deal is with a formal status/availablility event
# Learn what the curret display name is.
export ACTUAL_STATE=$(curl -s -X GET "${MATRIX_SERVER}/_matrix/client/v3/user/${ME}/rooms/${TEST_ROOM}/account_data/org.gibiris.availability?access_token=${ACCESS_TOKEN}" -H "Accept: application/json" | jq .availability)
echo "Current state: ${ACTUAL_STATE}"
# If we're only checkin', we're done.
if [ "${QUERY_ONLY}" == "Y" ]; then
exit 0
fi
# If TARGET_STATE isn't set at this point, we're togglin'
if [ -z "${TARGET_STATE}" ]; then
if [ "${ACTUAL_STATE}" == "\"${FREE_STATE}\"" ]; then
echo "You are supposedly free currently."
TARGET_STATE="${BUSY_STATE}"
elif [ "${ACTUAL_STATE}" == "\"${BUSY_STATE}\"" ]; then
echo "You are supposedly busy at the moment."
TARGET_STATE="${FREE_STATE}"
else
echo "We don't know what state you are in"
exit 1
fi
fi
echo "Setting availability to \"${TARGET_STATE}\"."
export MATRIX_TXN_ID="$(date '+%s%N')"
# Apply the changes
for room in ${ROOM_LIST[@]}
do
echo ${room}
curl -X PUT "${MATRIX_SERVER}/_matrix/client/v3/user/${ME}/rooms/${room}/account_data/org.gibiris.availability?access_token=${ACCESS_TOKEN}" -H "Accept: application/json" -H "Content-Type: application/json" -d "{ \"availability\": \"${TARGET_STATE}\" }"
curl -X PUT "${MATRIX_SERVER}/_matrix/client/r0/rooms/${room}/send/m.room.message/${MATRIX_TXN_ID}?access_token=${ACCESS_TOKEN}" -H 'Accept: application/json' -H 'Content-Type: application/json' -d "{ \"body\": \"${TARGET_STATE}\", \"msgtype\": \"m.notice\" }"
echo; echo
sleep 2
done