diff --git a/src/c2s.py b/src/c2s.py index d14c77e..2c2b3d3 100644 --- a/src/c2s.py +++ b/src/c2s.py @@ -10,26 +10,26 @@ client = Blueprint("c2s", __name__) @client.route("/_matrix/client/v3/account/password", methods=["POST"]) -@client.route('/_matrix/client/v3/user//account_data/', methods=['GET', 'PUT']) -@client.route('/_matrix/client/r0/user//account_data/', methods=['GET', 'PUT']) -@client.route('/_matrix/client/v3/sendToDevice//', methods=["PUT"]) -@client.route('/_matrix/media/v3/upload//', methods=["PUT"]) -@client.route('/_matrix/client/v3/thirdparty/protocols') -@client.route('/_matrix/client/r0/thirdparty/protocols') -@client.route('/_matrix/client/v3/delete_devices', methods=['POST']) -@client.route('/_matrix/client/r0/delete_devices', methods=['POST']) -@client.route('/_matrix/client/v3/logout/all', methods=['POST']) -@client.route('/_matrix/client/v3/logout', methods=['POST']) -@client.route('/_matrix/client/v3/rooms//invite', methods=['POST']) -@client.route('/_matrix/client/v3/rooms//leave', methods=['POST']) -@client.route('/_matrix/client/r0/rooms//leave', methods=['POST']) -@client.route('/_matrix/client/v3/rooms//read_markers', methods=['POST']) -@client.route('/_matrix/client/r0/rooms//read_markers', methods=['POST']) -@client.route('/_matrix/client/v3/keys/device_signing/upload', methods=['POST']) -@client.route("/_matrix/client/v3/rooms//receipt//", methods=['POST']) +@client.route("/_matrix/client/v3/user//account_data/", methods=["GET", "PUT"]) +@client.route("/_matrix/client/r0/user//account_data/", methods=["GET", "PUT"]) +@client.route("/_matrix/client/v3/sendToDevice//", methods=["PUT"]) +@client.route("/_matrix/media/v3/upload//", methods=["PUT"]) +@client.route("/_matrix/client/v3/thirdparty/protocols") +@client.route("/_matrix/client/r0/thirdparty/protocols") +@client.route("/_matrix/client/v3/delete_devices", methods=["POST"]) +@client.route("/_matrix/client/r0/delete_devices", methods=["POST"]) +@client.route("/_matrix/client/v3/logout/all", methods=["POST"]) +@client.route("/_matrix/client/v3/logout", methods=["POST"]) +@client.route("/_matrix/client/v3/rooms//invite", methods=["POST"]) +@client.route("/_matrix/client/v3/rooms//leave", methods=["POST"]) +@client.route("/_matrix/client/r0/rooms//leave", methods=["POST"]) +@client.route("/_matrix/client/v3/rooms//read_markers", methods=["POST"]) +@client.route("/_matrix/client/r0/rooms//read_markers", methods=["POST"]) +@client.route("/_matrix/client/v3/keys/device_signing/upload", methods=["POST"]) +@client.route("/_matrix/client/v3/rooms//receipt//", methods=["POST"]) @client.route("/_matrix/client/v3/users//report", methods=["POST"]) -@client.route('/_matrix/client/v3/voip/turnServer') -@client.route('/_matrix/client/r0/voip/turnServer') +@client.route("/_matrix/client/v3/voip/turnServer") +@client.route("/_matrix/client/r0/voip/turnServer") @client.route("/_matrix/client/v3/rooms//report/") @client.route("/_matrix/client/v3/rooms//report") @client.route("/_matrix/client/v3/users//report") @@ -37,7 +37,7 @@ async def empty_response(**kwargs): return jsonify({}) -@client.route('/_matrix/client/versions') +@client.route("/_matrix/client/versions") async def spec_versions(): return jsonify({ "versions": ( @@ -97,8 +97,8 @@ async def lock(user): return jsonify({"locked": True}) -@client.route('/_matrix/client/v3/rooms//members') -@client.route('/_matrix/client/r0/rooms//members') +@client.route("/_matrix/client/v3/rooms//members") +@client.route("/_matrix/client/r0/rooms//members") async def room_member_count(roomId): return jsonify({ "chunk": [{ @@ -126,15 +126,15 @@ async def whoami(): }) -@client.route('/_matrix/client/v3/register', methods=['POST']) -@client.route('/_matrix/client/v1/register', methods=['POST']) -@client.route('/_matrix/client/r0/register', methods=['POST']) +@client.route("/_matrix/client/v3/register", methods=["POST"]) +@client.route("/_matrix/client/v1/register", methods=["POST"]) +@client.route("/_matrix/client/r0/register", methods=["POST"]) async def register(): if users_can_register: try: data = request.get_json() - if data and 'auth' in data: + if data and "auth" in data: return jsonify({ "user_id": f"@vona:{server_name}", "home_server": f"{server_name}", @@ -157,10 +157,10 @@ async def register(): }), 403 -@client.route('/_matrix/client/r0/login', methods=['GET', 'POST']) -@client.route('/_matrix/client/v3/login', methods=['GET', 'POST']) +@client.route("/_matrix/client/r0/login", methods=["GET", "POST"]) +@client.route("/_matrix/client/v3/login", methods=["GET", "POST"]) async def login(): - if request.method == 'GET': + if request.method == "GET": return jsonify({ "flows": [ {"type": "m.login.password"}, @@ -178,19 +178,19 @@ async def login(): "user_id": f"@vona:{server_name}" }) -@client.route('/_matrix/client/v3/account/password/email/requestToken', methods=['POST']) +@client.route("/_matrix/client/v3/account/password/email/requestToken", methods=["POST"]) async def pswd_reset(): return jsonify({"errcode":"M_THREEPID_NOT_FOUND","error":"Email not found"}), 400 -@client.route('/_matrix/client/v3/keys/upload', methods=['POST']) +@client.route("/_matrix/client/v3/keys/upload", methods=["POST"]) async def key_upload(): return jsonify({"one_time_key_counts":{"signed_curve25519":50}}) -@client.route("/_matrix/client/v3/room_keys/version", methods=['POST', 'GET']) -@client.route("/_matrix/client/unstable/room_keys/version", methods=['POST', 'GET']) +@client.route("/_matrix/client/v3/room_keys/version", methods=["POST", "GET"]) +@client.route("/_matrix/client/unstable/room_keys/version", methods=["POST", "GET"]) async def room_keys(): - if request.method == 'POST': + if request.method == "POST": return jsonify({"version": str(the_funny_number)}) return jsonify({ @@ -259,25 +259,25 @@ async def capabilities(): }) -@client.route('/_matrix/client/r0/pushrules/') -@client.route('/_matrix/client/v3/pushrules/') +@client.route("/_matrix/client/r0/pushrules/") +@client.route("/_matrix/client/v3/pushrules/") async def pushrules(): # TODO: Actually implement this return jsonify({}) -@client.route('/_matrix/client/v3/user//filter/') -@client.route('/_matrix/client/r0/user//filter/') -@client.route('/_matrix/client/v3/user//filter', methods=['POST']) -@client.route('/_matrix/client/r0/user//filter', methods=['POST']) +@client.route("/_matrix/client/v3/user//filter/") +@client.route("/_matrix/client/r0/user//filter/") +@client.route("/_matrix/client/v3/user//filter", methods=["POST"]) +@client.route("/_matrix/client/r0/user//filter", methods=["POST"]) async def filter(**kwargs): return jsonify({"filter_id": "vvvooonnnaaa"}) -@client.route('/_matrix/client/v3/join/', methods=['POST']) -@client.route('/_matrix/client/r0/join/', methods=['POST']) -@client.route('/_matrix/client/v3/rooms//join', methods=['POST']) -@client.route('/_matrix/client/v3/knock/', methods=['POST']) +@client.route("/_matrix/client/v3/join/", methods=["POST"]) +@client.route("/_matrix/client/r0/join/", methods=["POST"]) +@client.route("/_matrix/client/v3/rooms//join", methods=["POST"]) +@client.route("/_matrix/client/v3/knock/", methods=["POST"]) async def join(room): return jsonify({"room_id": room}) @@ -340,9 +340,9 @@ async def sync(): room_state.append(room_name) wait_time = 0 - if 'timeout' in request.args: + if "timeout" in request.args: try: - wait_time = int(request.args.get('timeout')) / 1000 + wait_time = int(request.args.get("timeout")) / 1000 except: pass await asyncio.sleep(wait_time) @@ -375,13 +375,13 @@ async def sync(): }) -@client.route('/_matrix/client/v3/rooms//send//', methods=['POST', 'PUT']) -@client.route('/_matrix/client/r0/rooms//send//', methods=['POST', 'PUT']) +@client.route("/_matrix/client/v3/rooms//send//", methods=["POST", "PUT"]) +@client.route("/_matrix/client/r0/rooms//send//", methods=["POST", "PUT"]) async def send_message(room, eventType, txnId): return jsonify({"event_id": globals.make_event_id()}), 200 -@client.route('/_matrix/client/v3/user_directory/search', methods=['POST']) +@client.route("/_matrix/client/v3/user_directory/search", methods=["POST"]) async def user_directory(): return jsonify({ "limited": False, @@ -393,8 +393,8 @@ async def user_directory(): }) -@client.route('/_matrix/client/v3/devices') -@client.route('/_matrix/client/r0/devices') +@client.route("/_matrix/client/v3/devices") +@client.route("/_matrix/client/r0/devices") async def devices(): return jsonify({ "devices": [{ @@ -406,8 +406,8 @@ async def devices(): }) -@client.route('/_matrix/client/v3/devices/', methods=['GET', 'PUT', 'DELETE']) -@client.route('/_matrix/client/r0/devices/', methods=['GET', 'PUT', 'DELETE']) +@client.route("/_matrix/client/v3/devices/", methods=["GET", "PUT", "DELETE"]) +@client.route("/_matrix/client/r0/devices/", methods=["GET", "PUT", "DELETE"]) async def get_device(device): if request.method == "GET": return jsonify({ @@ -420,7 +420,7 @@ async def get_device(device): return jsonify({}) -@client.route("/_matrix/client/v3/refresh", methods=['POST']) +@client.route("/_matrix/client/v3/refresh", methods=["POST"]) async def refresh(): return jsonify({ "access_token": "vona", @@ -429,21 +429,21 @@ async def refresh(): }) -@client.route('/_matrix/client/unstable/im.nheko.summary/rooms//summary') -@client.route('/_matrix/client/unstable/im.nheko.summary/summary/') -@client.route('/_matrix/client/v1/room_summary/') +@client.route("/_matrix/client/unstable/im.nheko.summary/rooms//summary") +@client.route("/_matrix/client/unstable/im.nheko.summary/summary/") +@client.route("/_matrix/client/v1/room_summary/") def unstable_room_summary(roomId): - room = room_dir_room['chunk'][0] + room = room_dir_room["chunk"][0] return jsonify({ - "room_id": room['room_id'], - "avatar_url": room['avatar_url'], - "guest_can_join": room['guest_can_join'], - "name": room['name'], - "num_joined_members": room['num_joined_members'], - "topic": room['topic'], - "world_readable": room['world_readable'], - "join_rule": room['join_rule'], - "room_type": room['room_type'], + "room_id": room["room_id"], + "avatar_url": room["avatar_url"], + "guest_can_join": room["guest_can_join"], + "name": room["name"], + "num_joined_members": room["num_joined_members"], + "topic": room["topic"], + "world_readable": room["world_readable"], + "join_rule": room["join_rule"], + "room_type": room["room_type"], "membership": "join", "room_version": 2, }) @@ -454,7 +454,7 @@ def unstable_room_summary(roomId): async def room_query(room): if request.method == "GET": return jsonify({ - "room_id": room_dir_room['chunk'][0]['room_id'], + "room_id": room_dir_room["chunk"][0]["room_id"], "servers": [server_name] }) @@ -470,7 +470,7 @@ async def room_aliases(room): @client.route("/_matrix/client/v3/directory/list/room/", methods=["GET", "PUT"]) -@client.route('/_matrix/client/r0/directory/list/room/', methods=["GET", "PUT"]) +@client.route("/_matrix/client/r0/directory/list/room/", methods=["GET", "PUT"]) async def room_visibility(room): return jsonify({"visibility": "public"}) @@ -515,25 +515,25 @@ async def search(): }) -@client.route('/_matrix/media/v1/thumbnail//') -@client.route('/_matrix/client/v1/media/thumbnail//') -@client.route('/_matrix/media/r0/thumbnail//') -@client.route('/_matrix/media/v3/thumbnail//') -@client.route('/_matrix/media/v3/download///') -@client.route('/_matrix/client/v1/media/download//') -@client.route('/_matrix/media/v3/download//') -@client.route('/_matrix/media/r0/download//') +@client.route("/_matrix/media/v1/thumbnail//") +@client.route("/_matrix/client/v1/media/thumbnail//") +@client.route("/_matrix/media/r0/thumbnail//") +@client.route("/_matrix/media/v3/thumbnail//") +@client.route("/_matrix/media/v3/download///") +@client.route("/_matrix/client/v1/media/download//") +@client.route("/_matrix/media/v3/download//") +@client.route("/_matrix/media/r0/download//") async def media(**kwargs): return send_file(cat) -@client.route('/_matrix/client/v3/register/available') -@client.route('/_matrix/client/r0/register/available') +@client.route("/_matrix/client/v3/register/available") +@client.route("/_matrix/client/r0/register/available") async def username_available(): return jsonify({"available": True}) -@client.route('/_matrix/media/v3/preview_url') +@client.route("/_matrix/media/v3/preview_url") async def url_preview(): return jsonify({ "matrix:image:size": 102400, @@ -545,32 +545,32 @@ async def url_preview(): "og:title": "cool cat" }) -@client.route('/_matrix/client/v1/media/preview_url') +@client.route("/_matrix/client/v1/media/preview_url") async def media_preview(): response = send_file(cat) - response.headers['Content-Disposition'] = f'inline; filename="cat.jpg"' - response.headers['Content-Type'] = 'image/jpg' + response.headers["Content-Disposition"] = f'inline; filename="cat.jpg"' + response.headers["Content-Type"] = "image/jpg" return response -@client.route('/_matrix/media/v3/upload', methods=['POST']) -@client.route('/_matrix/media/r0/upload', methods=['POST']) -@client.route('/_matrix/media/v1/create', methods=['POST']) +@client.route("/_matrix/media/v3/upload", methods=["POST"]) +@client.route("/_matrix/media/r0/upload", methods=["POST"]) +@client.route("/_matrix/media/v1/create", methods=["POST"]) async def upload_media(): return jsonify({"content_uri": f"mxc://{server_name}/cat"}) -@client.route('/_matrix/media/v3/config') +@client.route("/_matrix/media/v3/config") async def media_config(): return jsonify({"m.upload.size": the_funny_number * 69420}) -@client.route('/_matrix/client/v3/profile//', methods=['GET', 'PUT', 'DELETE']) -@client.route('/_matrix/client/r0/profile//', methods=['GET', 'PUT', 'DELETE']) +@client.route("/_matrix/client/v3/profile//", methods=["GET", "PUT", "DELETE"]) +@client.route("/_matrix/client/r0/profile//", methods=["GET", "PUT", "DELETE"]) async def profile_keys(userId, key): - if request.method == 'GET': - if key == 'avatar_url': + if request.method == "GET": + if key == "avatar_url": return jsonify({"avatar_url": f"mxc://{server_name}/cat"}) - elif key == 'displayname': + elif key == "displayname": return jsonify({"displayname": "Vona"}) return jsonify({ @@ -580,8 +580,8 @@ async def profile_keys(userId, key): return jsonify({}) -@client.route('/_matrix/client/v3/profile/') -@client.route('/_matrix/client/r0/profile/') +@client.route("/_matrix/client/v3/profile/") +@client.route("/_matrix/client/r0/profile/") async def user_profile(userId): return jsonify({ "avatar_url": f"mxc://{server_name}/cat", @@ -589,8 +589,8 @@ async def user_profile(userId): }) -@client.route('/_matrix/client/v3/rooms//messages') -@client.route('/_matrix/client/r0/rooms//messages') +@client.route("/_matrix/client/v3/rooms//messages") +@client.route("/_matrix/client/r0/rooms//messages") async def room_messages(roomId): return jsonify({ "chunk": [{ @@ -610,8 +610,8 @@ async def room_messages(roomId): "start": f"{os.urandom(16).hex()}" }) -@client.route('/_matrix/client/v3/keys/query', methods=['POST']) -@client.route('/_matrix/client/r0/keys/query', methods=['POST']) +@client.route("/_matrix/client/v3/keys/query", methods=["POST"]) +@client.route("/_matrix/client/r0/keys/query", methods=["POST"]) async def query_keys(): # Should be replaced eventually return jsonify({}) @@ -643,6 +643,6 @@ async def presence(user): }) -@client.route('/_matrix/client/r0/publicRooms', methods=["GET", "POST"]) +@client.route("/_matrix/client/r0/publicRooms", methods=["GET", "POST"]) async def room_directory(): return jsonify(room_dir_room) diff --git a/src/config-example.py b/src/config-example.py index 203d9a3..b5902c7 100644 --- a/src/config-example.py +++ b/src/config-example.py @@ -1,5 +1,5 @@ # Cat picture to use. Must be a JPEG (most camera photos are JPEG already anyway). -cat = '../cat.jpg' +cat = "../cat.jpg" # Your server name. server_name = "example.org" @@ -41,7 +41,7 @@ support = { port = 5000 # The address to listen on. -addr = '127.0.0.1' +addr = "127.0.0.1" # Whether or not to enable registration. users_can_register = False diff --git a/src/custom.py b/src/custom.py index 82e888c..19fd2b5 100644 --- a/src/custom.py +++ b/src/custom.py @@ -1,6 +1,6 @@ from config import server_name, the_funny_number, room_dir_room from flask import Blueprint, jsonify, request, Response -from globals import vona_version, make_event_id +import globals import base64 import re import os @@ -16,20 +16,20 @@ custom = Blueprint("custom", __name__) # files eventually. -@custom.route('/_synapse/admin/v1/suspend/', methods=["PUT"]) -@custom.route('/_synapse/admin/v1/deactivate/', methods=['POST']) -@custom.route('/_synapse/admin/v1/reset_password/', methods=['POST']) -@custom.route('/_synapse/admin/v1/users//admin', methods=["PUT"]) -@custom.route('/_synapse/admin/v2/users//delete_devices', methods=['POST']) -@custom.route('/_synapse/admin/v1/users//shadow_ban', methods=['DELETE', 'POST']) -@custom.route('/_synapse/admin/v1/users//override_ratelimit', methods=['GET', 'POST', 'DELETE']) -@custom.route('/_synapse/admin/v1/media/protect/', methods=['POST']) -@custom.route('/_synapse/admin/v1/media/unprotect/', methods=['POST']) -@custom.route('/_synapse/admin/v1/media/quarantine//', methods=['POST']) -@custom.route('/_synapse/admin/v1/media/unquarantine//', methods=['POST']) -@custom.route('/_dendrite/admin/purgeRoom/', methods=['POST']) -@custom.route('/_dendrite/admin/refreshDevices/', methods=['POST']) -@custom.route('/_dendrite/admin/fulltext/reindex') +@custom.route("/_synapse/admin/v1/suspend/", methods=["PUT"]) +@custom.route("/_synapse/admin/v1/deactivate/", methods=["POST"]) +@custom.route("/_synapse/admin/v1/reset_password/", methods=["POST"]) +@custom.route("/_synapse/admin/v1/users//admin", methods=["PUT"]) +@custom.route("/_synapse/admin/v2/users//delete_devices", methods=["POST"]) +@custom.route("/_synapse/admin/v1/users//shadow_ban", methods=["DELETE", "POST"]) +@custom.route("/_synapse/admin/v1/users//override_ratelimit", methods=["GET", "POST", "DELETE"]) +@custom.route("/_synapse/admin/v1/media/protect/", methods=["POST"]) +@custom.route("/_synapse/admin/v1/media/unprotect/", methods=["POST"]) +@custom.route("/_synapse/admin/v1/media/quarantine//", methods=["POST"]) +@custom.route("/_synapse/admin/v1/media/unquarantine//", methods=["POST"]) +@custom.route("/_dendrite/admin/purgeRoom/", methods=["POST"]) +@custom.route("/_dendrite/admin/refreshDevices/", methods=["POST"]) +@custom.route("/_dendrite/admin/fulltext/reindex") @custom.route("/_synapse/admin/v1/federation/destinations//reset_connection", methods=["POST"]) @custom.route("/_synapse/admin/v1/rooms/") @custom.route("/_synapse/admin/v1/rooms//timestamp_to_event") @@ -40,11 +40,11 @@ async def empty_response(**kwargs): # Synapse -@custom.route('/_synapse/admin/v1/server_version') +@custom.route("/_synapse/admin/v1/server_version") def synapse_version(): - return jsonify({'server_version': vona_version}) + return jsonify({"server_version": globals.vona_version}) -@custom.route('/_synapse/admin/v2/users') +@custom.route("/_synapse/admin/v2/users") def synapse_user_list(): return jsonify({ "users": [ @@ -65,14 +65,14 @@ def synapse_user_list(): "total": 1 }) -@custom.route('/_synapse/admin/v2/users/', methods=['GET', 'PUT']) +@custom.route("/_synapse/admin/v2/users/", methods=["GET", "PUT"]) def synapse_user_info(user_id): - if request.method == 'GET': + if request.method == "GET": return jsonify({"name":f"@vona:{server_name}","displayname":"Vona","threepids":[],"avatar_url":f"mxc://{server_name}/cat","is_guest":0,"admin":0,"deactivated":0,"erased":False,"shadow_banned":0,"creation_ts":the_funny_number,"last_seen_ts":the_funny_number,"appservice_id":the_funny_number,"consent_server_notice_sent":the_funny_number,"consent_version":the_funny_number,"consent_ts":the_funny_number,"external_ids":[],"user_type":"vona","locked":False,"suspended":False}) return jsonify({}), 201 -@custom.route('/_synapse/admin/v1/whois/') +@custom.route("/_synapse/admin/v1/whois/") async def synapse_whois(user_id): return jsonify({ "user_id": f"@vona:{server_name}", @@ -82,185 +82,204 @@ async def synapse_whois(user_id): "connections": [{ "ip":f"127.0.0.1", "last_seen":the_funny_number, - "user_agent":f"Vona/{vona_version}" + "user_agent":f"Vona/{globals.vona_version}" }] }] } } }) -@custom.route('/_synapse/admin/v1/users//joined_rooms') +@custom.route("/_synapse/admin/v1/users//joined_rooms") def synapse_user_joined_rooms(user_id): - return jsonify({"joined_rooms":[room_dir_room['chunk'][0]['room_id']],"total":1}) + return jsonify({"joined_rooms":[room_dir_room["chunk"][0]["room_id"]],"total":1}) -@custom.route('/_synapse/admin/v1/users//sent_invite_count') +@custom.route("/_synapse/admin/v1/users//sent_invite_count") async def synapse_invite_count(user_id): return jsonify({"invite_count": the_funny_number}) -@custom.route('/_synapse/admin/v1/users//accountdata') +@custom.route("/_synapse/admin/v1/users//accountdata") def synapse_account_data(user_id): return jsonify({"account_data":{"global":{}}}) -@custom.route('/_synapse/admin/v1/users//media', methods=['GET', 'DELETE']) +@custom.route("/_synapse/admin/v1/users//media", methods=["GET", "DELETE"]) def synapse_account_media(user_id): - if request.method == 'GET': + if request.method == "GET": return jsonify({"media": [{"created_ts":the_funny_number,"last_access_ts":the_funny_number,"media_id":"cat","media_length":the_funny_number,"media_type":"image/jpeg","quarantined_by":"null","safe_from_quarantine":False,"upload_name":"cat.jpg"}], "total": the_funny_number}) return jsonify({"deleted_media": ["cat"], "total": the_funny_number}) -@custom.route('/_synapse/admin/v1/users//login', methods=['POST']) +@custom.route("/_synapse/admin/v1/users//login", methods=["POST"]) def synapse_account_login(user_id): return jsonify({"access_token": "vona"}) -@custom.route('/_synapse/admin/v1/users//_allow_cross_signing_replacement_without_uia', methods=['POST']) +@custom.route("/_synapse/admin/v1/users//_allow_cross_signing_replacement_without_uia", methods=["POST"]) def synapse_stupid_mas_bullshit(user_id): return jsonify({"updatable_without_uia_before_ms": the_funny_number}) -@custom.route('/_synapse/admin/v2/users//devices', methods=['GET', 'POST']) +@custom.route("/_synapse/admin/v2/users//devices", methods=["GET", "POST"]) def synapse_device_list(user_id): - if request.method == 'GET': + if request.method == "GET": return jsonify({ "devices": [{ "device_id": "VVOONNAA", "display_name": "Vona", "last_seen_ip": "127.0.0.1", "last_seen_ts": the_funny_number, - "last_seen_user_agent": f"Vona/{vona_version}" + "last_seen_user_agent": f"Vona/{globals.vona_version}" }], "total": 1 }) return jsonify({}) -@custom.route('/_synapse/admin/v2/users//devices/', methods=["GET", "PUT", "DELETE"]) +@custom.route("/_synapse/admin/v2/users//devices/", methods=["GET", "PUT", "DELETE"]) async def synapse_device_info(user_id, device_id): - if request.method == 'GET': + if request.method == "GET": return jsonify({ "device_id": "VVOONNAA", "display_name": "Vona", "last_seen_ip": "127.0.0.1", "last_seen_ts": the_funny_number, - "last_seen_user_agent": f"Vona/{vona_version}" + "last_seen_user_agent": f"Vona/{globals.vona_version}" }) return jsonify({}) -@custom.route('/_synapse/admin/v1/users//pushers') +@custom.route("/_synapse/admin/v1/users//pushers") async def synapse_pushers(user_id): return jsonify({"pushers": [], "total": the_funny_number}) -@custom.route('/_synapse/admin/v1/username_available') +@custom.route("/_synapse/admin/v1/username_available") async def synapse_username_available(): return jsonify({"available": True}) -@custom.route('/_synapse/admin/v1/threepid//users/') -@custom.route('/_synapse/admin/v1/auth_providers//users/') +@custom.route("/_synapse/admin/v1/threepid//users/") +@custom.route("/_synapse/admin/v1/auth_providers//users/") async def synapse_threepid(p, a): return jsonify({"user_id": f"@vona:{server_name}"}) -@custom.route('/_synapse/admin/v1//redact') +@custom.route("/_synapse/admin/v1//redact") def synapse_redact(user_id): return jsonify({"redact_id": os.urandom(16).hex()}) -@custom.route('/_synapse/admin/v1/user/redact_status/') +@custom.route("/_synapse/admin/v1/user/redact_status/") async def synapse_redact_status(redact_id): return jsonify({"status":"active","failed_redactions":[]}) -@custom.route('/_synapse/admin/v1/experimental_features/', methods=['GET', 'PUT']) +@custom.route("/_synapse/admin/v1/experimental_features/", methods=["GET", "PUT"]) async def synapse_experimental_features(user_id): return jsonify({"features": {}}) -@custom.route('/_synapse/admin/v1/register', methods=['GET', 'POST']) +@custom.route("/_synapse/admin/v1/register", methods=["GET", "POST"]) async def synapse_register(): - if request.method == 'GET': + if request.method == "GET": return jsonify({"nonce": os.urandom(16).hex()}) return jsonify({"access_token": "vona"}) -@custom.route('/_synapse/admin/v1/join/', methods=['POST']) +@custom.route("/_synapse/admin/v1/join/", methods=["POST"]) async def synapse_membership_manipulation(roomId): - return jsonify({"room_id": room_dir_room['chunk'][0]['room_id']}) + return jsonify({"room_id": room_dir_room["chunk"][0]["room_id"]}) -@custom.route('/_synapse/admin/v1/account_validity/validity', methods=['POST']) +@custom.route("/_synapse/admin/v1/account_validity/validity", methods=["POST"]) async def synapse_account_validity(): return jsonify({"expiration_ts": the_funny_number}) -@custom.route('/_synapse/admin/v1/send_server_notice', methods=['POST']) -@custom.route('/_synapse/admin/v1/send_server_notice/', methods=["PUT"]) +@custom.route("/_synapse/admin/v1/send_server_notice", methods=["POST"]) +@custom.route("/_synapse/admin/v1/send_server_notice/", methods=["PUT"]) async def synapse_server_notice(**kwargs): - return jsonify({"event_id": make_event_id()}) + return jsonify({"event_id": globals.make_event_id()}) -@custom.route('/_synapse/admin/v1/purge_history//', methods=['POST']) -@custom.route('/_synapse/admin/v1/purge_history/', methods=['POST']) +@custom.route("/_synapse/admin/v1/purge_history//", methods=["POST"]) +@custom.route("/_synapse/admin/v1/purge_history/", methods=["POST"]) async def synapse_purge_event(**kwargs): return jsonify({"purge_id": os.urandom(16).hex()}) -@custom.route('/_synapse/admin/v1/purge_history_status/') +@custom.route("/_synapse/admin/v1/purge_history_status/") def synapse_purge_status(purge_id): return jsonify({"status":"active"}) -@custom.route('/_synapse/admin/v1/room//media') +@custom.route("/_synapse/admin/v1/room//media") def synapse_room_media(room_id): return jsonify({"local": [f"mxc://{server_name}/cat"], "remote": []}) -@custom.route('/_synapse/admin/v1/room//media/quarantine', methods=['POST']) +@custom.route("/_synapse/admin/v1/room//media/quarantine", methods=["POST"]) def synapse_quarantine_room_media(room_id): return jsonify({"num_quarantined": the_funny_number}) -@custom.route('/_synapse/admin/v1/media//delete', methods=['POST']) -@custom.route('/_synapse/admin/v1/media//', methods=['DELETE']) -@custom.route('/_synapse/admin/v1/media/delete', methods=['POST']) +@custom.route("/_synapse/admin/v1/media//delete", methods=["POST"]) +@custom.route("/_synapse/admin/v1/media//", methods=["DELETE"]) +@custom.route("/_synapse/admin/v1/media/delete", methods=["POST"]) async def synapse_delete_media_from_server(**kwargs): return jsonify({"deleted_media": ["cat"], "total": the_funny_number}) -@custom.route('/_synapse/admin/v1/purge_media_cache', methods=['POST']) +@custom.route("/_synapse/admin/v1/purge_media_cache", methods=["POST"]) async def synapse_delete_remote_media(): return jsonify({"deleted": the_funny_number}) -@custom.route('/_synapse/admin/v1/statistics/users/media') +@custom.route("/_synapse/admin/v1/statistics/users/media") async def synapse_media_stats(): return jsonify({"users":[{"displayname":"Vona","media_count":the_funny_number,"media_length":the_funny_number,"user_id":f"@vona:{server_name}"}],"total":the_funny_number}) -@custom.route('/_synapse/admin/v1/statistics/database/rooms') +@custom.route("/_synapse/admin/v1/statistics/database/rooms") async def synapse_room_stats(): return jsonify({ "rooms": [{ - "room_id": room_dir_room['chunk'][0]['room_id'], + "room_id": room_dir_room["chunk"][0]["room_id"], "estimated_size": the_funny_number * 420 }] }) -@custom.route('/_synapse/admin/v1/background_updates/enabled', methods=['POST', 'GET']) -@custom.route('/_synapse/admin/v1/background_updates/status') +@custom.route("/_synapse/admin/v1/background_updates/enabled", methods=["POST", "GET"]) +@custom.route("/_synapse/admin/v1/background_updates/status") async def synapse_change_bg_update(): return jsonify({"enabled":False}) # No documentation on what Synapse actually returns for this API, so a blank dict for now. -@custom.route('/_synapse/admin/v1/background_updates/start_job', methods=['POST']) +@custom.route("/_synapse/admin/v1/background_updates/start_job", methods=["POST"]) async def synapse_bg_update_start_job(): return jsonify({}) -@custom.route('/_synapse/admin/v1/event_reports') +@custom.route("/_synapse/admin/v1/event_reports") async def synapse_event_reports(): return jsonify({ "event_reports": [{ - "event_id": make_event_id(), + "event_id": globals.make_event_id(), "id": the_funny_number, "reason": "", "score": the_funny_number, "received_ts": the_funny_number, - "room_id": room_dir_room['chunk'][0]['room_id'], - "name": room_dir_room['chunk'][0]['name'], + "room_id": room_dir_room["chunk"][0]["room_id"], + "name": room_dir_room["chunk"][0]["name"], "sender": f"@vona:{server_name}", "user_id": f"@vona:{server_name}" }], "total": the_funny_number }) -@custom.route('/_synapse/admin/v1/event_reports/', methods=['GET', 'DELETE']) +@custom.route("/_synapse/admin/v1/event_reports/", methods=["GET", "DELETE"]) async def synapse_interact_with_reported_event(report_id): - if request.method == 'GET': - return jsonify({"event_id": make_event_id(),"event_json": {"auth_events": [],"content": {"msgtype": "m.text","body": "Number 15: Burger King Foot Lettuce.\nThe last thing you'd want in your Burger King burger is someones foot fungus, but as it turns out, that might be what you get. A 4channer uploaded a photo, anonymously to the site showcasing his feet in a plastic bin of lettuce with the statement \"This is the lettuce you eat at Burger King.\". Admittedly, he had shoes on, but thats even worse. The post went live at 11:38 PM on July 16 and a mere 20 minutes later the Burger King in question was alerted to the rogue employee. At least, I hope hes rogue. How did it happen? Well, the BK employee hadn't removed the EXIF data from the uploaded photo, which suggested that the culprit was somewhere in Mayfield Heights, Ohio. This was at 11:47. 3 minutes later, at 11:50, the Burger King branch was posted with wishes of happy unemployment. 5 minutes later, the news station was contacted by another 4channer, and 3 minutes later at 11:58 a link was posted: BK's tell us about us online forum. The foot photo, otherwise known as Exhibit A, was attached. Cleveland Seen Magazine contacted the BK in question and the next day when questioned, the breakfast shift manager said \"Oh, I know who that is, hes getting fired\". Mystery solved, by 4chan. Now we can go back to eating our fast food in peace.","format": "org.matrix.custom.html","formatted_body": "Number 15: Burger King Foot Lettuce.
The last thing you'd want in your Burger King burger is someones foot fungus, but as it turns out, that might be what you get. A 4channer uploaded a photo, anonymously to the site showcasing his feet in a plastic bin of lettuce with the statement "This is the lettuce you eat at Burger King.". Admittedly, he had shoes on, but thats even worse. The post went live at 11:38 PM on July 16 and a mere 20 minutes later the Burger King in question was alerted to the rogue employee. At least, I hope hes rogue. How did it happen? Well, the BK employee hadn't removed the EXIF data from the uploaded photo, which suggested that the culprit was somewhere in Mayfield Heights, Ohio. This was at 11:47. 3 minutes later, at 11:50, the Burger King branch was posted with wishes of happy unemployment. 5 minutes later, the news station was contacted by another 4channer, and 3 minutes later at 11:58 a link was posted: BK's tell us about us online forum. The foot photo, otherwise known as Exhibit A, was attached. Cleveland Seen Magazine contacted the BK in question and the next day when questioned, the breakfast shift manager said "Oh, I know who that is, hes getting fired". Mystery solved, by 4chan. Now we can go back to eating our fast food in peace."},"depth": the_funny_number,"hashes": {"sha256": "todo: replace with something proper"},"origin": server_name,"origin_server_ts": the_funny_number,"prev_events": [make_event_id()],"prev_state": [],"room_id": room_dir_room['chunk'][0]['room_id'],"sender": f"@vona:{server_name}","signatures": {server_name: {"ed25519:a_JaEG": base64.b64encode(os.urandom(32)).decode('utf-8')[:87]}}},"type": "m.room.message","unsigned": {"age_ts": the_funny_number},"id": the_funny_number,"reason": "vona","score": the_funny_number,"received_ts": the_funny_number,"room_id": room_dir_room['chunk'][0]['room_id'],"name": room_dir_room['chunk'][0]['name'],"sender": f"@vona:{server_name}","user_id": f"@vona:{server_name}"}) + if request.method == "GET": + return jsonify({ + "event_id": globals.make_event_id(), + "event_json": globals.hash_and_sign_event({ + "auth_events": [], + "content": { + "msgtype": "m.text", + "body": "Number 15: Burger King Foot Lettuce.\nThe last thing you'd want in your Burger King burger is someones foot fungus, but as it turns out, that might be what you get. A 4channer uploaded a photo, anonymously to the site showcasing his feet in a plastic bin of lettuce with the statement \"This is the lettuce you eat at Burger King.\". Admittedly, he had shoes on, but thats even worse. The post went live at 11:38 PM on July 16 and a mere 20 minutes later the Burger King in question was alerted to the rogue employee. At least, I hope hes rogue. How did it happen? Well, the BK employee hadn't removed the EXIF data from the uploaded photo, which suggested that the culprit was somewhere in Mayfield Heights, Ohio. This was at 11:47. 3 minutes later, at 11:50, the Burger King branch was posted with wishes of happy unemployment. 5 minutes later, the news station was contacted by another 4channer, and 3 minutes later at 11:58 a link was posted: BK's tell us about us online forum. The foot photo, otherwise known as Exhibit A, was attached. Cleveland Seen Magazine contacted the BK in question and the next day when questioned, the breakfast shift manager said \"Oh, I know who that is, hes getting fired\". Mystery solved, by 4chan. Now we can go back to eating our fast food in peace.", + "format": "org.matrix.custom.html", + "formatted_body": "Number 15: Burger King Foot Lettuce.
The last thing you'd want in your Burger King burger is someones foot fungus, but as it turns out, that might be what you get. A 4channer uploaded a photo, anonymously to the site showcasing his feet in a plastic bin of lettuce with the statement "This is the lettuce you eat at Burger King.". Admittedly, he had shoes on, but thats even worse. The post went live at 11:38 PM on July 16 and a mere 20 minutes later the Burger King in question was alerted to the rogue employee. At least, I hope hes rogue. How did it happen? Well, the BK employee hadn't removed the EXIF data from the uploaded photo, which suggested that the culprit was somewhere in Mayfield Heights, Ohio. This was at 11:47. 3 minutes later, at 11:50, the Burger King branch was posted with wishes of happy unemployment. 5 minutes later, the news station was contacted by another 4channer, and 3 minutes later at 11:58 a link was posted: BK's tell us about us online forum. The foot photo, otherwise known as Exhibit A, was attached. Cleveland Seen Magazine contacted the BK in question and the next day when questioned, the breakfast shift manager said "Oh, I know who that is, hes getting fired". Mystery solved, by 4chan. Now we can go back to eating our fast food in peace." + }, + "depth": the_funny_number, + "origin": server_name, + "origin_server_ts": the_funny_number, + "prev_events": [globals.make_event_id()], + "prev_state": [], + "room_id": room_dir_room["chunk"][0]["room_id"], + "sender": f"@vona:{server_name}", + "type": "m.room.message" + }) + }) return jsonify({}) @@ -383,7 +402,7 @@ async def synapse_forward_extremities(room): return jsonify({ "count": 1, "results": [{ - "event_id": make_event_id(), + "event_id": globals.make_event_id(), "state_group": the_funny_number, "depth": the_funny_number, "received_ts": the_funny_number @@ -391,27 +410,30 @@ async def synapse_forward_extremities(room): }) # Dendrite - https://element-hq.github.io/dendrite/administration/adminapi -@custom.route('/_dendrite/admin/evacuateUser/', methods=['POST']) +@custom.route("/_dendrite/admin/evacuateUser/", methods=["POST"]) async def dendrite_evacuate_user(userId): - return jsonify({"affected": [room_dir_room['chunk'][0]['room_id']]}) + return jsonify({"affected": [room_dir_room["chunk"][0]["room_id"]]}) -@custom.route('/_dendrite/admin/evacuateRoom/', methods=['POST']) +@custom.route("/_dendrite/admin/evacuateRoom/", methods=["POST"]) async def dendrite_evacuate_room(roomId): return jsonify({"affected": [f"@vona:{server_name}"]}) -@custom.route('/_dendrite/admin/resetPassword/', methods=['POST']) +@custom.route("/_dendrite/admin/resetPassword/", methods=["POST"]) async def dendrite_reset_pswd(userId): return jsonify({"password_updated": True}) # Conduwuit/Tuwunel/Continuwuity -@custom.route('/_continuwuity/local_user_count') -@custom.route('/_conduwuit/local_user_count') -@custom.route('/_tuwunel/local_user_count') +@custom.route("/_continuwuity/local_user_count") +@custom.route("/_conduwuit/local_user_count") +@custom.route("/_tuwunel/local_user_count") async def conduwuit_user_count(): return jsonify({"count": 1}) -@custom.route('/_continuwuity/server_version') -@custom.route('/_conduwuit/server_version') -@custom.route('/_tuwunel/server_version') +@custom.route("/_continuwuity/server_version") +@custom.route("/_conduwuit/server_version") +@custom.route("/_tuwunel/server_version") async def conduwuit_server_version(): - return jsonify({"name":"Vona","version":vona_version}) + return jsonify({ + "name": "Vona", + "version":globals.vona_version + }) diff --git a/src/globals.py b/src/globals.py index faef058..917f490 100644 --- a/src/globals.py +++ b/src/globals.py @@ -160,7 +160,7 @@ def make_auth_header(destination, method, path, content=None) -> str: def redact_event(event): # Returns a redacted event as per - # the algorithm for v1 rooms. + # the algorithm for v1/v2 rooms. allowed_keys = [ "event_id", diff --git a/src/identity.py b/src/identity.py index e8ab23b..46b7a9d 100644 --- a/src/identity.py +++ b/src/identity.py @@ -8,7 +8,7 @@ identity = Blueprint("identity", __name__) # I'm pretty sure only Element uses this, # but oh well. -# https://spec.matrix.org/v1.16/identity-service-api/#api-version-check +# https://spec.matrix.org/latest/identity-service-api/#api-version-check @identity.route("/_matrix/identity/versions") async def versions(): # Stolen from the vector.im identity server @@ -27,70 +27,70 @@ async def versions(): }) -# https://spec.matrix.org/v1.16/identity-service-api/#authentication +# https://spec.matrix.org/latest/identity-service-api/#authentication @identity.route("/_matrix/identity/v2/account") async def account_info(): return jsonify({"user_id": f"@vona:{server_name}"}) -@identity.route("/_matrix/identity/v2/account/logout", methods=['POST']) +@identity.route("/_matrix/identity/v2/account/logout", methods=["POST"]) async def logout(): return jsonify({}) -@identity.route("/_matrix/identity/v2/account/register", methods=['POST']) +@identity.route("/_matrix/identity/v2/account/register", methods=["POST"]) async def register(): return jsonify({"token":"vona"}) -# https://spec.matrix.org/v1.16/identity-service-api/#terms-of-service -@identity.route('/_matrix/identity/v2/terms', methods=['GET', 'POST']) +# https://spec.matrix.org/latest/identity-service-api/#terms-of-service +@identity.route("/_matrix/identity/v2/terms", methods=["GET", "POST"]) async def policies(): - if request.method == 'GET': + if request.method == "GET": return jsonify({"policies":{}}) return jsonify({}) -@identity.route('/_matrix/identity/v2') +@identity.route("/_matrix/identity/v2") async def status(): return jsonify({}) -@identity.route('/_matrix/identity/v2/pubkey/ephemeral/isvalid') -@identity.route('/_matrix/identity/v2/pubkey/isvalid') +@identity.route("/_matrix/identity/v2/pubkey/ephemeral/isvalid") +@identity.route("/_matrix/identity/v2/pubkey/isvalid") async def pubkey_validity(): return jsonify({"valid": True}) -@identity.route('/_matrix/identity/v2/pubkey/') +@identity.route("/_matrix/identity/v2/pubkey/") async def get_key(key): return jsonify({ "errcode": "M_NOT_FOUND", "error": "The public key was not found" }), 404 -@identity.route('/_matrix/identity/v2/hash_details') +@identity.route("/_matrix/identity/v2/hash_details") async def hash_details(): return jsonify({"algorithms":["none","sha256"],"lookup_pepper": "vona"}) -@identity.route('/_matrix/identity/v2/lookup', methods=['POST']) +@identity.route("/_matrix/identity/v2/lookup", methods=["POST"]) async def lookup(): req = request.json if "addresses" in req: - return jsonify({"mappings": {req['addresses'][0]: f"@vona:{server_name}"}}) + return jsonify({"mappings": {req["addresses"][0]: f"@vona:{server_name}"}}) else: return jsonify({"errcode": "M_INVALID_PEPPER","error": "Invalid pepper"}) -@identity.route('/_matrix/identity/v2/validate/email/requestToken', methods=['POST']) -@identity.route('/_matrix/identity/v2/validate/msisdn/requestToken', methods=['POST']) +@identity.route("/_matrix/identity/v2/validate/email/requestToken", methods=["POST"]) +@identity.route("/_matrix/identity/v2/validate/msisdn/requestToken", methods=["POST"]) async def request_validation_token(): return jsonify({"sid": str(the_funny_number)}) -@identity.route('/_matrix/identity/v2/validate/email/submitToken', methods=['GET', 'POST']) -@identity.route('/_matrix/identity/v2/validate/msisdn/submitToken', methods=['GET', 'POST']) +@identity.route("/_matrix/identity/v2/validate/email/submitToken", methods=["GET", "POST"]) +@identity.route("/_matrix/identity/v2/validate/msisdn/submitToken", methods=["GET", "POST"]) async def submit_validation_token(): return jsonify({"success": True}) -@identity.route('/_matrix/identity/v2/3pid/bind', methods=['POST']) +@identity.route("/_matrix/identity/v2/3pid/bind", methods=["POST"]) async def threepid_bind(): if "mxid" in request.get_json(): mxid = request.get_json()["mxid"] @@ -106,11 +106,11 @@ async def threepid_bind(): "ts": int(time.time() * 1000) })) -@identity.route('/_matrix/identity/v2/3pid/unbind', methods=['POST']) +@identity.route("/_matrix/identity/v2/3pid/unbind", methods=["POST"]) async def threepid_unbind(): return jsonify({}) -@identity.route('/_matrix/identity/v2/3pid/getValidated3pid') +@identity.route("/_matrix/identity/v2/3pid/getValidated3pid") async def threepid_validated(): # Please email abuse@matrix.org return jsonify({ @@ -120,8 +120,8 @@ async def threepid_validated(): }) -# https://spec.matrix.org/v1.16/identity-service-api/#invitation-storage -@identity.route('/_matrix/identity/v2/store-invite', methods=['POST']) +# https://spec.matrix.org/latest/identity-service-api/#invitation-storage +@identity.route("/_matrix/identity/v2/store-invite", methods=["POST"]) async def invite(): return jsonify({ "display_name": "Vona", @@ -139,10 +139,10 @@ async def invite(): }) -# https://spec.matrix.org/v1.16/identity-service-api/#ephemeral-invitation-signing -@identity.route('/_matrix/identity/v2/sign-ed25519', methods=['POST']) +# https://spec.matrix.org/latest/identity-service-api/#ephemeral-invitation-signing +@identity.route("/_matrix/identity/v2/sign-ed25519", methods=["POST"]) async def invite_signing(): - required_keys = {'mxid', 'private_key', 'token'} + required_keys = {"mxid", "private_key", "token"} d = data.get_json() if set(d.keys()) == required_keys: diff --git a/src/main.py b/src/main.py index 4575976..d978f3d 100644 --- a/src/main.py +++ b/src/main.py @@ -34,19 +34,18 @@ async def handle_logging(response): # Discard logs for OPTIONS return response + origin = "unknown" + try: if "Authorization" in request.headers: if request.headers["Authorization"].split()[0] == "X-Matrix": origin = request.headers["Authorization"].split('origin="')[1].split('"')[0] - else: origin = "client" - else: - origin = "unknown" except: - origin = "unknown" + pass - print(f"[{origin}] [{request.remote_addr}] [{datetime.now().strftime('%d/%b/%Y:%H:%M:%S')}] {request.method} {request.full_path} {response.status_code}") + print(f'[{origin}] [{request.remote_addr}] [{datetime.now().strftime("%d/%b/%Y:%H:%M:%S")}] {request.method} {request.full_path} {response.status_code}') response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" response.headers["Access-Control-Allow-Origin"] = "*" @@ -63,7 +62,7 @@ async def root(): @app.route("/_matrix/static/") async def matrix_static(): - return f'Vona {globals.vona_version} is running

It works! Vona {globals.vona_version} is running

Your Vona server is listening on this port and is ready for messages.

To use this server you\'ll need a Matrix client.

Welcome to the Matrix universe :)


matrix.org

' + return f'Vona {globals.vona_version} is running

It works! Vona {globals.vona_version} is running

Your Vona server is listening on this port and is ready for messages.

To use this server you\"ll need a Matrix client.

Welcome to the Matrix universe :)


matrix.org

' # Error handlers diff --git a/src/s2s.py b/src/s2s.py index 09bad27..aaf2046 100644 --- a/src/s2s.py +++ b/src/s2s.py @@ -246,32 +246,32 @@ async def keys(): } })) -@server.route('/_matrix/federation/v1/query/directory') +@server.route("/_matrix/federation/v1/query/directory") async def room_query(): return jsonify({ "room_id": globals.make_event_id().replace("$", "!"), "servers": [server_name] }) -@server.route('/_matrix/federation/v1/media/download/') +@server.route("/_matrix/federation/v1/media/download/") async def download_media(media_id): # Auth media requires this to be # multipart despite not even using # it for anything. Minor annoyance. - with open(cat, 'rb') as img_file: + with open(cat, "rb") as img_file: image_data = img_file.read() - boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW' + boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW" response_body = ( - f'--{boundary}\r\n' - f'Content-Type: application/json\r\n\r\n' - f'{{}}\r\n' - f'--{boundary}\r\n' - f'Content-Type: image/jpeg\r\n' + f"--{boundary}\r\n" + f"Content-Type: application/json\r\n\r\n" + f"{{}}\r\n" + f"--{boundary}\r\n" + f"Content-Type: image/jpeg\r\n" f'Content-Disposition: attachment; filename="cat.jpg"\r\n\r\n' - ).encode() + image_data + f'\r\n--{boundary}--\r\n'.encode() + ).encode() + image_data + f"\r\n--{boundary}--\r\n".encode() - response = Response(response_body, content_type=f'multipart/mixed; boundary={boundary}') + response = Response(response_body, content_type=f"multipart/mixed; boundary={boundary}") response.status_code = 200 return response @@ -353,13 +353,13 @@ async def make_join(roomId, userId): "room_version": "2" }) -@server.route('/_matrix/federation/v1/publicRooms', methods=['POST', 'GET']) +@server.route("/_matrix/federation/v1/publicRooms", methods=["POST", "GET"]) async def room_directory(): return jsonify(room_dir_room) -# https://spec.matrix.org/v1.16/server-server-api/#transactions -@server.route('/_matrix/federation/v1/send/', methods=["PUT"]) +# https://spec.matrix.org/latest/server-server-api/#transactions +@server.route("/_matrix/federation/v1/send/", methods=["PUT"]) async def receive_txn(txnId): # We will need to implement a way to store every # event we need if we want to send events in the @@ -372,10 +372,10 @@ async def receive_txn(txnId): # - m.room.power_levels # - m.room.third_party_invite # - # As per https://spec.matrix.org/v1.16/rooms/v2/#authorization-rules + # As per https://spec.matrix.org/latest/rooms/v2/#authorization-rules - data = request.data.decode('utf-8') + data = request.data.decode("utf-8") parsed_data = json.loads(data) response = {"pdus": {}} @@ -386,23 +386,27 @@ async def receive_txn(txnId): return jsonify(response) -@server.route('/_matrix/federation/v1/query/profile') + +@server.route("/_matrix/federation/v1/query/profile") async def user_profile(): - field = request.args.get('field') + field = request.args.get("field") if field: - if field == 'avatar_url': + if field == "avatar_url": return jsonify({"avatar_url":f"mxc://{server_name}/cat"}) - elif field == 'displayname': + elif field == "displayname": return jsonify({"displayname":"Vona"}) - return jsonify({"errcode": "M_NOT_FOUND","error": "The requested profile key does not exist."}), 404 + return jsonify({ + "errcode": "M_NOT_FOUND", + "error": "The requested profile key does not exist." + }), 404 return jsonify({"avatar_url": f"mxc://{server_name}/cat","displayname": "Vona"}) -# https://spec.matrix.org/v1.16/server-server-api/#device-management -@server.route(f'/_matrix/federation/v1/user/devices/@/:{server_name}') -@server.route('/_matrix/federation/v1/user/devices/') +# https://spec.matrix.org/latest/server-server-api/#device-management +@server.route(f"/_matrix/federation/v1/user/devices/@/:{server_name}") +@server.route("/_matrix/federation/v1/user/devices/") async def user_devices(user): return jsonify({ "devices": [], @@ -411,7 +415,7 @@ async def user_devices(user): }) -@server.route('/_matrix/federation/v1/user/keys/query', methods=['POST']) +@server.route("/_matrix/federation/v1/user/keys/query", methods=["POST"]) async def user_keys(): try: users = request.json["device_keys"] @@ -424,12 +428,12 @@ async def user_keys(): return jsonify({"device_keys": users}) -@server.route('/_matrix/federation/v2/invite//', methods=["PUT"]) +@server.route("/_matrix/federation/v2/invite//", methods=["PUT"]) async def invite_user_v2(room, txnId): return invite_user(request.data) -@server.route('/_matrix/federation/v1/invite//', methods=["PUT"]) +@server.route("/_matrix/federation/v1/invite//", methods=["PUT"]) async def invite_user_v1(room, txnId): return [200, invite_user(request.data)] @@ -470,46 +474,46 @@ def invite_user(data): }), 403 -@server.route('/_matrix/federation/v1/hierarchy/') +@server.route("/_matrix/federation/v1/hierarchy/") def space_hierachy(roomId): - room = room_dir_room['chunk'][0] + room = room_dir_room["chunk"][0] return jsonify({ "children": [{ - "avatar_url": room['avatar_url'], + "avatar_url": room["avatar_url"], "children_state": [{ "content": {"via": [server_name]}, "origin_server_ts": the_funny_number, "sender": f"@vona:{server_name}", - "state_key": room['room_id'], + "state_key": room["room_id"], "type": "m.space.child" }], - "guest_can_join": room['guest_can_join'], - "join_rule": room['join_rule'], - "name": room['name'], - "num_joined_members": room['num_joined_members'], - "room_id": room['room_id'], - "room_type": room['room_type'], - "topic": room['topic'], - "world_readable": room['world_readable'] + "guest_can_join": room["guest_can_join"], + "join_rule": room["join_rule"], + "name": room["name"], + "num_joined_members": room["num_joined_members"], + "room_id": room["room_id"], + "room_type": room["room_type"], + "topic": room["topic"], + "world_readable": room["world_readable"] }], "inaccessible_children": [], "room": { "allowed_room_ids": [], - "avatar_url": room['avatar_url'], + "avatar_url": room["avatar_url"], "children_state": [{ "content": {"via": [server_name]}, "origin_server_ts": the_funny_number, "sender": f"@vona:{server_name}", - "state_key": room['room_id'], + "state_key": room["room_id"], "type": "m.space.child" }], - "guest_can_join": room['guest_can_join'], - "join_rule": room['join_rule'], - "name": room['name'], - "num_joined_members": room['num_joined_members'], - "room_id": room['room_id'], - "room_type": room['room_type'], - "topic": room['topic'], - "world_readable": room['world_readable'] + "guest_can_join": room["guest_can_join"], + "join_rule": room["join_rule"], + "name": room["name"], + "num_joined_members": room["num_joined_members"], + "room_id": room["room_id"], + "room_type": room["room_type"], + "topic": room["topic"], + "world_readable": room["world_readable"] } })