#!/bin/bash
#'
#This Bash script takes a json array of form [{"user_id": "@asdX:domain.com", "displayname": "nameX"}, {"user_id": "@asdY:domain.com", "displayname": null}] and iterates over them with the delete user api which is exclusive to Synapse.
#This array is supplied as a file parameter and created with another script (see filter_users_by_*.sh)
#
#See: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#deactivate-account
#
#'
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
USER_COUNT = $( jq '. | length' " $input_file " )
echo " Found $USER_COUNT users 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 < $USER_COUNT ; i++ ) ) ; do
user_json = $( jq " .[ $i ] " " $input_file " )
user_displayname = $( echo $user_json | jq -r '.displayname' )
user_id = $( echo $user_json | jq -r '.user_id' )
user_id_enc = $( echo $user_json | jq -r '.user_id' | sed 's/@/%40/g' | sed 's/:/%3A/g' ) # user id has to be URL encoded (bootleg version)
echo " Currently purging user $user_displayname with ID: $user_id ... "
if [ -d " $output_folder " ] ; then
output_file = " $output_folder / $user_id_enc -delete_result.txt "
fi
if [ ! -z " $output_file " ] ; then
echo curl --globoff -XPOST -H "Authorization: Bearer [TOKEN]" -d "{\"erase\": true}" " $domain /_synapse/admin/v1/deactivate/ $user_id_enc " -o " $output_file "
curl --globoff -XPOST -H " Authorization: Bearer $token " -d "{\"erase\": true}" " $domain /_synapse/admin/v1/deactivate/ $user_id_enc " -o " $output_file "
else
echo curl --globoff -XPOST -H "Authorization: Bearer [TOKEN]" -d "{\"erase\": true}" " $domain /_synapse/admin/v1/deactivate/ $user_id_enc "
curl --globoff -XPOST -H " Authorization: Bearer $token " -d "{\"erase\": true}" " $domain /_synapse/admin/v1/deactivate/ $user_id_enc "
fi
echo "Sleeping for 5 seconds ..."
sleep 5
done