#!/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}/" def mass_unban(matching_keys): for i in matching_keys: print("Unbanning id " + i + " ...") headers = { "Authorization": f"Bearer {AUTH_TOKEN}" } # unban the given id data = {"user_id": i} unban_url = URL + 'unban' response_unban = requests.post(unban_url, headers=headers, json=data) # 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("To search users for mass-unbanning, please enter a keyword (for example a domain): ") print("") headers = { "Authorization": f"Bearer {AUTH_TOKEN}" } banned_url = URL + "members?membership=ban" response = requests.get(banned_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)