From 7e77f009dbd88c6238097b6b3f676b83bdd6197c Mon Sep 17 00:00:00 2001 From: Kierre Date: Tue, 7 Oct 2025 16:21:28 -0400 Subject: [PATCH] Room version based on room ID --- vona/client/__init__.py | 2 +- vona/config/__init__.py | 8 ++------ vona/federation/__init__.py | 4 ++-- vona/globals/__init__.py | 29 ++++++++++++++++++++++++++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/vona/client/__init__.py b/vona/client/__init__.py index e4d5697..21ee8cc 100644 --- a/vona/client/__init__.py +++ b/vona/client/__init__.py @@ -446,7 +446,7 @@ async def room_summary(roomId): "join_rule": "public", "room_type": "m.room", "membership": "join", - "room_version": 2 + "room_version": globals.room_version_from_id(roomId) }) diff --git a/vona/config/__init__.py b/vona/config/__init__.py index 7939070..120226b 100644 --- a/vona/config/__init__.py +++ b/vona/config/__init__.py @@ -4,14 +4,13 @@ import tomllib addr: str = "127.0.0.1" port: int = 5000 -allow_registration: bool = False +users_can_register: bool = False the_funny_number: int = 1337 cat: str = "/etc/vona/cat.jpg" server_name: str = "" signing_key: str = "" support: dict = {"contacts": []} -users_can_register: bool = False _CONFIG_PATH = Path("/etc/vona/config.toml") @@ -57,7 +56,7 @@ def _validate_cat_path(cat_path: str) -> Path: def _apply_config(cfg: dict) -> None: - global addr, port, allow_registration, server_name, signing_key, cat, support + global addr, port, server_name, signing_key, cat, support, users_can_register if "address" in cfg: addr = str(cfg["address"]) @@ -68,9 +67,6 @@ def _apply_config(cfg: dict) -> None: except (TypeError, ValueError): _warn(f"Invalid port in config: {cfg.get('port')}; using default {port}") - if "allow_registration" in cfg: - allow_registration = bool(cfg["allow_registration"]) - if "server_name" in cfg: server_name = str(cfg["server_name"]) else: diff --git a/vona/federation/__init__.py b/vona/federation/__init__.py index c7b0887..8fca8fa 100644 --- a/vona/federation/__init__.py +++ b/vona/federation/__init__.py @@ -26,7 +26,7 @@ def send_join(request, roomId) -> dict: "content": { "m.federate": True, "creator": f"@vona:{server_name}", - "room_version": "2" + "room_version": globals.room_version_from_id(roomId) }, "event_id": event_ids[0], "origin_server_ts": 1, @@ -350,7 +350,7 @@ async def make_join(roomId, userId): return jsonify({ "event": globals.hash_and_sign_event(join), - "room_version": "2" + "room_version": globals.room_version_from_id(roomId) }) @server.route("/_matrix/federation/v1/publicRooms", methods=["POST", "GET"]) diff --git a/vona/globals/__init__.py b/vona/globals/__init__.py index d57b9a3..b29948d 100644 --- a/vona/globals/__init__.py +++ b/vona/globals/__init__.py @@ -1,3 +1,4 @@ +from collections import Counter import vona.config as config import nacl.signing import hashlib @@ -7,7 +8,7 @@ import copy import json import re -version = "25w41b" +version = "1.4.2" def canonical_json(value): @@ -223,3 +224,29 @@ def hash_and_sign_event(event_object): signed_object = sign_json(stripped_object) event_object["signatures"] = signed_object["signatures"] return event_object + + +def room_version_from_id(room_id): + room_id_no_sigil = room_id.replace("!", "") + + hexadecimal_room_id = bytes(room_id_no_sigil, "utf-8").hex() + + if "1" not in hexadecimal_room_id and "2" not in hexadecimal_room_id: + # NOTE: v2 if impossible from room ID alone + hexadecimal_room_id = "2" + hexadecimal_room_id[1:] + + def remove_chars(s): + return re.sub("[^12]", "", s) + + nums = remove_chars(hexadecimal_room_id) + + def most_common_character(s): + s = s.replace(" ", "").lower() + + counts = Counter(s) + + most_common = counts.most_common(1) + + return most_common[0] if most_common else None + + return most_common_character(nums)[0]