From e15aef6b6bb2d2676c7ecf8fe62a61098acb26a5 Mon Sep 17 00:00:00 2001 From: cynic Date: Sun, 8 Dec 2024 12:27:33 -0600 Subject: [PATCH] Add mass_unban.py --- mass_unban.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 mass_unban.py diff --git a/mass_unban.py b/mass_unban.py new file mode 100644 index 0000000..85dcb5a --- /dev/null +++ b/mass_unban.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +import requests +import json + +# define room, server domain, and authorization token +ROOM_ID = "!string@domain.tld" +DOMAIN = "matrix.domain.tld" +AUTH_TOKEN = 'syt_tokentokentoken' + + +# unban query url +URL = f"https://{DOMAIN}/_matrix/client/r0/rooms/{ROOM_ID}/members?membership=ban" + +def mass_unban(matching_keys): + for i in matching_keys: + print("Unbanning id " + i + " ...") + headers = { "Authorization": f"Bearer {AUTH_TOKEN}" } + # unban the given id + response_unban = requests.get(URL, headers=headers) + # check if the request was successful + if response.status_code == 200: + print("Id unbanned successfully.\n") + else: + print(f"Error: {response.status_code} - {response.text}") + +# prompt the user for a keyword and make the query +keyword = input("Please enter keyword for search (for example the domain): ") +print("") +headers = { "Authorization": f"Bearer {AUTH_TOKEN}" } +response = requests.get(URL, headers=headers) + +# check if the request was successful +if response.status_code == 200: + # parse the JSON response + data = response.json() + # extract and filter the state keys based on the keyword + matching_keys = [member['state_key'] for member in data.get('chunk', []) if keyword in member['state_key']] + # print the matching state keys + for key in matching_keys: + print(key) + # confirm action with the user + print("\nThe users above are about to be unbanned...") + answer = input("Continue? ") + if answer.lower() in ["y","yes"]: + # trigger mass unban on the filtered list + mass_unban(matching_keys) + elif answer.lower() in ["n","no"]: + print("Thank you.") + quit() +else: + print(f"Error: {response.status_code} - {response.text}") + quit(1)