From 7475e38388ca347bb6d1052df57184ced2403412 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Wed, 23 Aug 2023 18:36:40 +0800 Subject: [PATCH] add example of remote media purging, create new function for sending server notices (untested). --- docs/server_functions.md | 20 +++++++++++-- moderation_tool.py | 10 +++++-- server_commands.py | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/docs/server_functions.md b/docs/server_functions.md index c13d12e..e143664 100644 --- a/docs/server_functions.md +++ b/docs/server_functions.md @@ -45,8 +45,24 @@ Process Flow: Example: ```bash -$ date --date '149 days ago' +%s -$ curl -X POST --header "Authorization: Bearer ACCESS_TOKEN" '... Matrix Synapse purge endpoint ...' +$ python3 moderation_tool.py + +101 + +Enter the number of days to purge from: 30 + +Enter the number of days to purge too: -2 +{"deleted":0} +{"deleted":0} +{"deleted":3} +{"deleted":360} +{"deleted":469} +... +{"deleted":1020} +{"deleted":2440} +{"deleted":0} +{"deleted":0} +Done! :) ``` 102) **Prepare Database for Copying Events of Multiple Rooms** diff --git a/moderation_tool.py b/moderation_tool.py index 634c5a8..70605f5 100755 --- a/moderation_tool.py +++ b/moderation_tool.py @@ -80,9 +80,10 @@ while pass_token == False: print("103) Show last 10 reported events.\t\t\t\t#### Report Generation ####") print("104) Get all reported events.\t\t\t\t\t150) Generate user report.") print("105) Get details of a reported event.\t\t\t\t151) Lookup homeserver admin contact details.") - print("\t\t\t\t\t\t\t\t152) Send a test email (to yourself).") - print("#### rdlist - General ####\t\t\t\t\t153) Send a test Matrix message (to yourself).") - print("120) Block all rooms with specific rdlist tags.\t\t\t154) Send test incident reports (to yourself).") + print("106) Send a server notice.\t\t\t\t\t152) Send a test email (to yourself).") + print("\t\t\t\t\t\t\t\t\t153) Send a test Matrix message (to yourself).") + print("\n#### rdlist - General ####\t\t\t\t\t154) Send test incident reports (to yourself).") + print("120) Block all rooms with specific rdlist tags.") print("121) Get rdlist tags for a room.") print("\n#### rdlist - Recommended Tags ####") print("For rdlist rooms with recommended tags, the following actions are available:") @@ -212,6 +213,9 @@ while pass_token == False: elif menu_input == "105": report_details = server_commands.get_event_report_details() print(json.dumps(report_details, indent=4, sort_keys=True)) + elif menu_input == "106": + server_commands.send_server_notice() + print("\nServer notice sent.\n") elif menu_input == "120": rdlist_commands.block_all_rooms_with_rdlist_tags(False,'','','') elif menu_input == "121": diff --git a/server_commands.py b/server_commands.py index 0c89b4c..4a848db 100644 --- a/server_commands.py +++ b/server_commands.py @@ -203,3 +203,64 @@ def get_event_report_details(preset_report_id=''): else: print(f"Error fetching event report details: {response.status_code}, {response.text}") return None + +def send_server_notice(preset_user_id='', preset_message='', txnId=None, event_type="m.room.message", state_key=None): + """ + Sends a server notice to a given user. + + Args: + - user_id (str): The Matrix ID of the user to send the notice to, e.g. "@target_user:server_name". + - message (str): The message to be sent as a notice. + - txnId (str, optional): A unique transaction ID. If provided, retransmissions with the same txnId will be ignored. + - event_type (str, optional): The type of event. Defaults to "m.room.message". + - state_key (str, optional): Setting this will result in a state event being sent. + + Returns: + - dict: A dictionary containing the response from the server. + """ + + # Take user_id from user if not provided + if preset_user_id == '': + user_id = input("\nEnter the user_id of the user you would like to send the server notice to: ") + elif preset_user_id != '': + user_id = preset_user_id + + # Take message from user if not provided + if preset_message == '': + message = input("\nEnter the message you would like to send to the user: ") + elif preset_message != '': + message = preset_message + + # Construct the URL based on whether a txnId is provided + if txnId: + url = f"https://{hardcoded_variables.homeserver_url}/_synapse/admin/v1/send_server_notice/{txnId}" + else: + url = f"https://{hardcoded_variables.homeserver_url}/_synapse/admin/v1/send_server_notice" + + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {hardcoded_variables.access_token}" + } + + # Construct the request body + data = { + "user_id": user_id, + "content": { + "msgtype": "m.text", + "body": message + } + } + + if event_type: + data["type"] = event_type + if state_key: + data["state_key"] = state_key + + # Send the request + response = requests.put(url, headers=headers, json=data) if txnId else requests.post(url, headers=headers, json=data) + + if response.status_code == 200: + return response.json() + else: + print(f"Error sending server notice: {response.status_code}, {response.text}") + return None \ No newline at end of file