From 232b05aa95eb006d552ac6b43c9f088631692d9a Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sat, 26 Dec 2020 00:35:08 +0100 Subject: [PATCH] More calls to admin API which need access token (list user, list rooms, list room members) --- seqs/matrix.cfg.example | 4 ++ seqs/matrix.sh | 131 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 6 deletions(-) diff --git a/seqs/matrix.cfg.example b/seqs/matrix.cfg.example index 7dc6c91..34526f9 100644 --- a/seqs/matrix.cfg.example +++ b/seqs/matrix.cfg.example @@ -1,4 +1,8 @@ #!/bin/bash +# Location of synapse virtual environment MATRIX_HOME="/opt/synapse" +# Corresponds to "server_name" in synapse config MATRIX_DOMAIN="matrix.example.com" +# Provide an admin access token for API calls +MATRIX_ACCESS="" diff --git a/seqs/matrix.sh b/seqs/matrix.sh index 92c8ac2..4e2d09f 100755 --- a/seqs/matrix.sh +++ b/seqs/matrix.sh @@ -182,16 +182,53 @@ step_14() { if [ ! -z $1 ]; then synapseIP="$1" fi - exe curl http://${synapseIP}:8008/_synapse/admin/v1/server_version + + local apiCall="http://${synapseIP}:8008/_synapse/admin/v1/server_version" + # -sS to suppress download progress of curl + exep "curl -sS \"$apiCall\" | python -m json.tool | grep _version" } -step_16_info() { echo "Create new user"; } -step_16_alias() { ALIAS="adduser"; } +step_16_info() { + echo "List all registered users [OPTION] [IP]:8008" + echoinfo "[OPTION]" + echoinfo " -r : Raw json output" +} +step_16_alias() { ALIAS="listuser"; } step_16() { + adminTokenCheck + endReturn -o $? "Admin token needed. Check $SEQ_CONFIG_FILE" + + shift + local synapseIP=localhost + local grepOut=" | grep -E '(\"total\":|\"name\":)'" + + for arg in "$@" ; do + case "$1" in + -r) + grepOut="" + shift + ;; + *) + break + ;; + esac + done + + if [ ! -z $1 ]; then + synapseIP="$1" + fi + + local apiCall="http://${synapseIP}:8008/_synapse/admin/v2/users" + exep "curl -sS --header \"Authorization: Bearer $MATRIX_ACCESS\" \"$apiCall\" | python -m json.tool $grepOut" +} + +step_18_info() { echo "Create new user"; } +step_18_alias() { ALIAS="adduser"; } +step_18() { exe /opt/synapse/env/bin/register_new_matrix_user -c "$MATRIX_HOME/homeserver.yaml" $toolUrlLocal } -step_18_info() { +step_20_info() { shift echo -n "Reset user password" if [ -z $1 ]; then @@ -200,8 +237,8 @@ step_18_info() { echo " for $1" fi } -step_18_alias() { ALIAS="resetpw"; } -step_18() { +step_20_alias() { ALIAS="resetpw"; } +step_20() { shift local user= @@ -222,6 +259,80 @@ step_18() { exep "echo \"$string\" | su postgres -c 'psql -d synapse -f -'" } +step_22_info() { + echo "List all rooms [OPTION] [IP]:8008" + echoinfo "[OPTION]" + echoinfo " -r : Raw json output" +} +step_22_alias() { ALIAS="listrooms"; } +step_22() { + adminTokenCheck + endReturn -o $? "Admin token needed. Check $SEQ_CONFIG_FILE" + + shift + local synapseIP=localhost + local grepOut=" | grep -E '(\"total\":|\"name\":|\"room_id\":)'" + + for arg in "$@" ; do + case "$1" in + -r) + grepOut="" + shift + ;; + *) + break + ;; + esac + done + + if [ ! -z $1 ]; then + synapseIP="$1" + fi + + local apiCall="http://${synapseIP}:8008/_synapse/admin/v1/rooms" + exep "curl -sS --header \"Authorization: Bearer $MATRIX_ACCESS\" \"$apiCall\" | python -m json.tool $grepOut" +} + +step_24_info() { + echo "List all room members [OPTION] [ROOM ID] [IP]:8008" + echoinfo "[OPTION]" + echoinfo " -r : Raw json output" +} +step_24_alias() { ALIAS="listmember"; } +step_24() { + adminTokenCheck + endReturn -o $? "Admin token needed. Check $SEQ_CONFIG_FILE" + + shift + local roomId="" + local synapseIP=localhost + local grepOut=" | grep -E '(\"total\":|\"members\":|\"@)'" + + for arg in "$@" ; do + case "$1" in + -r) + grepOut="" + shift + ;; + *) + break + ;; + esac + done + + if [ ! -z $1 ]; then + roomId="$1" + shift + fi + + if [ ! -z $1 ]; then + synapseIP="$1" + fi + + local apiCall="http://${synapseIP}:8008/_synapse/admin/v1/rooms/$roomId/members" + exep "curl -sS --header \"Authorization: Bearer $MATRIX_ACCESS\" \"$apiCall\" | python -m json.tool $grepOut" +} + step_30_info() { echo "Drop postgres database for $toolName"; } step_30_alias() { ALIAS="dropdb"; } step_30() { @@ -288,5 +399,13 @@ toolScript() { fi } +# End step if no admin access token is configured +adminTokenCheck() { + if [ -z $MATRIX_ACCESS ] ; then + return 1 + fi + return 0 +} + VERSION_SEQREV=11 . /usr/local/bin/sequencer.sh