Add mass_unban.py

This commit is contained in:
cynic 2024-12-08 12:27:33 -06:00
parent fd543007fc
commit e15aef6b6b

52
mass_unban.py Normal file
View File

@ -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)