You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.9 KiB
Bash

#!/bin/bash
#'
#This Bash script takes a json array of form [{"room_id": "!asdX:domain.com", "room_name": "nameX"}, {"room_id": "!asdY:domain.com", "room_name": "nameY"}] and iterates over them with the delete room api which is exclusive to Synapse.
#This array is supplied as a file parameter and created with another script (see filter_account_data_rooms_for_pattern.sh)
#
#See: https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api
#
#'
set -e
domain=$1
login_file=$2
input_file=$3
output_folder=$4
if [ -z "$domain" -o ! -e "$input_file" -o ! -e "$login_file" ]; then
echo "Domain: $domain, login file: $login_file, input file: $input_file, output folder: $output_folder"
echo "Usage: $0 http(s)://<DOMAIN> <LOGIN_FILE> <INPUT_FILE> [OUTPUT_FOLDER]"
exit 1
fi
if [ ! -z "$output_folder" -a ! -d "$output_folder" ]; then
echo "The given output folder ($output_folder) is not a valid directory! Creating it ..."
mkdir $output_folder
fi
#echo "Querying homeserver for new login data ..."
#echo curl -XPOST -d "{\"type\":\"m.login.password\", \"user\":\"$user\", \"password\":\"$password\"}" "$domain/_matrix/client/r0/login" -o "$output_file"
#curl --globoff -XGET -H "Authorization: Bearer $token" "$domain/_matrix/client/r0/sync?filter={\"room\":{\"timeline\":{\"limit\":1}}}"
ROOM_COUNT=$(jq '. | length' "$input_file")
echo "Found $ROOM_COUNT rooms to delete in the input file. Example: "
echo $(jq '.[0]' "$input_file")
echo
echo "Other Example: "
echo $(jq '.[-1]' "$input_file")
echo
read -p "Is this looking correct? (y/n) " reply
if [ "$reply" != "y" -a "$reply" != "Y" ]; then
echo "Aborting ..."
exit 0
fi
token=$(jq -r '.access_token' "$login_file")
for ((i = 0; i < $ROOM_COUNT; i++ ));do
room_json=$(jq ".[$i]" "$input_file")
room_name=$(echo $room_json | jq -r '.room_name')
room_id=$(echo $room_json | jq -r '.room_id')
room_id_enc=$(echo $room_json | jq -r '.room_id' | sed 's/!/%21/g' | sed 's/:/%3A/g') # room id has to be URL encoded (bootleg version)
echo "Currently purging room $room_name with ID: $room_id ..."
if [ -d "$output_folder" ]; then
output_file="$output_folder/$room_id_enc-delete_result.txt"
fi
if [ ! -z "$output_file" ]; then
echo curl --globoff -XDELETE -H "Authorization: Bearer $token" -d "{\"block\": false, \"purge\": true}" "$domain/_synapse/admin/v2/rooms/$room_id_enc" -o "$output_file"
curl --globoff -XDELETE -H "Authorization: Bearer $token" -d "{\"block\": false, \"purge\": true}" "$domain/_synapse/admin/v2/rooms/$room_id_enc" -o "$output_file"
else
echo curl --globoff -XDELETE -H "Authorization: Bearer $token" -d "{\"block\": false, \"purge\": true}" "$domain/_synapse/admin/v2/rooms/$room_id_enc"
curl --globoff -XDELETE -H "Authorization: Bearer $token" -d "{\"block\": false, \"purge\": true}" "$domain/_synapse/admin/v2/rooms/$room_id_enc"
fi
echo "Sleeping for 5 seconds ..."
sleep 5
done