From 81739a456de2d21b68981acb6a06e4cb3f697240 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Fri, 28 Jul 2023 10:36:58 +0800 Subject: [PATCH] add shadow ban function. ask if admin wants to shadow ban users before mass rdlist shutdowns. move example room/user lists into seperate folder. --- .../example_create_users_list.txt | 0 .../example_room_list.json | 0 .../example_room_list.txt | 0 .../example_users_list.txt | 0 moderation_tool.py | 7 +++++-- rdlist_commands.py | 7 +++++++ user_commands.py | 20 +++++++++++++++++++ 7 files changed, 32 insertions(+), 2 deletions(-) rename example_create_users_list.txt => examples/example_create_users_list.txt (100%) rename example_room_list.json => examples/example_room_list.json (100%) rename example_room_list.txt => examples/example_room_list.txt (100%) rename example_users_list.txt => examples/example_users_list.txt (100%) diff --git a/example_create_users_list.txt b/examples/example_create_users_list.txt similarity index 100% rename from example_create_users_list.txt rename to examples/example_create_users_list.txt diff --git a/example_room_list.json b/examples/example_room_list.json similarity index 100% rename from example_room_list.json rename to examples/example_room_list.json diff --git a/example_room_list.txt b/examples/example_room_list.txt similarity index 100% rename from example_room_list.txt rename to examples/example_room_list.txt diff --git a/example_users_list.txt b/examples/example_users_list.txt similarity index 100% rename from example_users_list.txt rename to examples/example_users_list.txt diff --git a/moderation_tool.py b/moderation_tool.py index 3f72723..9d44f2f 100755 --- a/moderation_tool.py +++ b/moderation_tool.py @@ -54,10 +54,10 @@ while pass_token == False: print("17) Set rate limit of a user account.\t\t66) Purge the event history of multiple rooms to a specific timestamp.") print("18) Delete rate limit of a user account.\t67) Get blocked status for room.") print("19) Check if user account exists.\t\t68) Block a room.") - print("\t\t\t\t\t\t69) Unblock a room.") + print("20) Shadow ban a user.\t\t\t\t69) Unblock a room.") print("\n#### Server Commands ####\t\t\t\t\t#### Report Generation ####") print("100) Delete and block a specific media.\t\t\t\t150) Generate user report.") - print("101) Purge remote media repository up to a certain date.\t\t151) Decrypt user report .zip file.") + print("101) Purge remote media repository up to a certain date.\t151) Decrypt user report .zip file.") print("102) Prepare database for copying events of multiple rooms.\t152) Lookup homeserver admin contact email.") print("\t\t\t\t\t\t\t\t153) Send a test email.") print("#### rdlist ####\t\t\t\t\t\t154) Send test incident reports to yourself.") @@ -116,6 +116,9 @@ while pass_token == False: print("\nUser account exists.\n") elif user_account_exists == False: print("\nUser account does not exist.\n") + elif menu_input == "20": + shadow_ban_dict = user_commands.shadow_ban_account('') + print(json.dumps(shadow_ban_dict, indent=4, sort_keys=True)) elif menu_input == "50": room_details_dict = room_commands.get_room_details('') print(json.dumps(room_details_dict, indent=4, sort_keys=True)) diff --git a/rdlist_commands.py b/rdlist_commands.py index 572e6b9..1c6bb88 100644 --- a/rdlist_commands.py +++ b/rdlist_commands.py @@ -238,6 +238,13 @@ def block_all_rooms_with_rdlist_tags(rdlist_use_recommended,preset_user_ID,prese num_rooms_purged = 0 if shutdown_confirmation.lower() in ['y', 'yes', 'Y', 'Yes']: + # Ask the user if they wish to shadow ban all local users in these rooms + shadow_ban_confirmation = input("\nDo you want to also shadow ban all your local users in these rooms before performing these shutdowns? (This is recommended as it prevents them from alerting others about these mass shutdown.) y/n? ") + # Perform shadow bans if admin confirms + if shadow_ban_confirmation in ['y', 'yes', 'Y', 'Yes']: + for user in all_local_users: + print(f"\nShadow banning user: {user}") + user_commands.shadow_ban_account(user) for room_id in all_room_ids: blocked_status = room_commands.get_block_status(room_id) #print(f"\nroom_details_dict: {room_details_dict}") diff --git a/user_commands.py b/user_commands.py index 504329a..0dc39a3 100644 --- a/user_commands.py +++ b/user_commands.py @@ -505,3 +505,23 @@ def check_user_account_exists(preset_username): # Example: # $ curl -X GET /_synapse/admin/v1/username_available?username=dogpoo&access_token=ACCESS_TOKEN + +def shadow_ban_account(preset_username): + if preset_username == '': + username = input("\nPlease enter the username you wish to shadow ban: ") + elif preset_username != '': + username = preset_username + username = parse_username(username) + + url = f"https://{hardcoded_variables.homeserver_url}/_synapse/admin/v1/users/@{username}:{hardcoded_variables.base_url}/shadow_ban" + url += f"?access_token={hardcoded_variables.access_token}" + + response = requests.post(url, verify=True) + + if response.status_code != 200: + print(f"Error shadow banning account: {response.status_code}, {response.text}\n") + + return json.loads(response.text) + +# Example: +# curl -XPOST -H "Content-Type: application/json" 'https://matrix.perthchat.org/_synapse/admin/v1/users/@dogpoo:perthchat.org/shadow_ban?access_token=ACCESS_TOKEN'