From 990d8aa232c0a65e7f044e27bb8a62950ff70c0a Mon Sep 17 00:00:00 2001 From: creme Date: Wed, 12 Feb 2025 15:15:21 +0100 Subject: [PATCH] update useful aliases --- usr/local/bin/README.md | 58 ++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/usr/local/bin/README.md b/usr/local/bin/README.md index 3c82185..fbe78ca 100644 --- a/usr/local/bin/README.md +++ b/usr/local/bin/README.md @@ -1,43 +1,75 @@ **some useful aliases:** +``` +source "$HOME/.token" # contains $token +server='server.tld' +``` + get a list of all rooms: ```bash matrix-show_rooms() { - curl -s --header "Authorization: Bearer $token" -X GET 'https://matrix.envs.net/_synapse/admin/v1/rooms?order_by=state_events&limit=1000000' | jq -Mr . > ~/rooms.txt && nano ~/rooms.txt + curl -s -H "Authorization: Bearer $token" -X GET 'https://'"$server"'/_synapse/admin/v1/rooms?order_by=state_events&limit=1000000' | jq -Mr . > ~/rooms.txt && nano ~/rooms.txt } ``` get a list of all users: ```bash matrix-show_users() { - curl -s --header "Authorization: Bearer $token" -X GET 'https://matrix.envs.net/_synapse/admin/v2/users?from=0&limit=100000&guests=false' | jq -Mr . > ~/users.txt && nano ~/users.txt + curl -s -H "Authorization: Bearer $token" -X GET 'https://'"$server"'/_synapse/admin/v2/users?from=0&limit=100000&guests=false' | jq -Mr . > ~/users.txt && nano ~/users.txt } ``` deactivate a list of user_ids: ```bash matrix-deactivate-users() { - while read i; do - printf '%s\n' "$i"; \ - curl -s --header "Authorization: Bearer $token" -X POST 'https://matrix.envs.net/_synapse/admin/v1/deactivate/'"$i" -d '{"erase": true}'; printf '\n'; done < ~/deactivate_users.txt + while read i; do + printf '%s\n' "$i"; \ + curl -s -H "Authorization: Bearer $token" -X POST 'https://'"$server"'/_synapse/admin/v1/deactivate/'"$i" -d '{"erase": true}'; printf '\n' + done < ~/deactivate_users.txt + echo > ~/deactivate_users.txt } ``` -purge and block a list of room_ids: +remove all rooms without local members: +```bash +matrix-remove-empty-rooms() { + TOPURGE=$(curl -s -H "Authorization: Bearer $token" -X GET \ + 'https://'"$server"'/_synapse/admin/v1/rooms?limit=1000000' | jq -Mr '.rooms[] | select(.joined_local_members == 0) | .room_id') + + for i in $TOPURGE; do + printf 'processing room %s ..\n' "$i" + curl -s -w "\nResponse code: %{response_code}\n" -H "Authorization: Bearer $token" -H "Content-Type: application/json" -X DELETE -d '{}' \ + 'https://'"$server"'/_synapse/admin/v1/rooms/'"$i"'' + done +} +``` + +purge and block a list of room_ids and also deactivate all local users in this rooms: ```bash matrix-purge-rooms() { - while read i; do - printf '%s\n' "$i"; \ - curl -s --header "Authorization: Bearer $token" -X DELETE 'https://matrix.envs.net/_synapse/admin/v1/rooms/'"$i" -d '{"purge": true, "block": true}'; printf '\n'; done < ~/purge_rooms.txt + echo > ~/blocked_members.txt + while read i; do + printf '%s\n' "$i" + curl -s -H "Authorization: Bearer $token" -X GET 'https://'"$server"'/_synapse/admin/v1/rooms/'"$i"'/members' | jq -Mr .members[] >> ~/blocked_members.txt + curl -s -H "Authorization: Bearer $token" -X DELETE 'https://'"$server"'/_synapse/admin/v1/rooms/'"$i" -d '{"purge": true, "block": true}' + printf '\n' + done < ~/purge_rooms.txt + sed -i -e '/^$/d' ~/blocked_members.txt + sort -u -o ~/blocked_members.txt{,} + for i in $(grep "$server" ~/blocked_members.txt); do + printf '%s\n' "$i" + curl -s -H "Authorization: Bearer $token" -X POST 'https://'"$server"'/_synapse/admin/v1/deactivate/'"$i" -d '{"erase": true}' + done + sed -i '/'"$server"'/d' ~/blocked_members.txt } ``` get a list of all blocked room_ids: ```bash matrix-get-blocked_rooms() { - sudo -iu postgres psql -d matrix -c "SELECT room_id FROM blocked_rooms" > ~/blocked_rooms.tmp - sed -i -e 's/ //' -e '1,2d' ~/blocked_rooms.tmp - head -n -2 /root/blocked_rooms.tmp > ~/blocked_rooms.txt - rm ~/blocked_rooms.tmp + sudo -iu postgres psql -d matrix -c "SELECT room_id FROM blocked_rooms" > ~/blocked_rooms.tmp + sed -i -e 's/ //' -e '1,2d' ~/blocked_rooms.tmp + head -n -2 /root/blocked_rooms.tmp > ~/blocked_rooms.txt + rm ~/blocked_rooms.tmp } ```