diff --git a/scripts/systemd/semif-gateway.service.in b/scripts/systemd/semif-gateway.service.in index 8ab3cd4..ffa4e4e 100644 --- a/scripts/systemd/semif-gateway.service.in +++ b/scripts/systemd/semif-gateway.service.in @@ -7,6 +7,7 @@ Wants=network-online.target semif-simplex.service Type=simple WorkingDirectory=@REPO@ Environment=HF_HOME=@REPO@/.runtime/hf +Environment=PYTHONUNBUFFERED=1 ExecStart=@REPO@/.runtime/venv/bin/python -m semif_agent.cli gateway Restart=always RestartSec=5 diff --git a/semif_agent/gateway/simplex.py b/semif_agent/gateway/simplex.py index 58bfe17..6b15130 100644 --- a/semif_agent/gateway/simplex.py +++ b/semif_agent/gateway/simplex.py @@ -161,14 +161,33 @@ class SimplexAdapter(GatewayAdapter): if direction and "Snd" in direction: return None # own echo content = chat_item.get("content") or {} - if content.get("type") != "text": + # simplex-chat v7 tags the content union by constructor: a received text + # message is {"type": "rcvMsgContent", "msgContent": {"type": "text", + # "text": ...}}. The daemon's DB/Aeson encoding nests under the + # constructor key instead ({"rcvMsgContent": {"msgContent": ...}}), so + # accept both shapes. + if content.get("type") == "rcvMsgContent": + msg_content = content.get("msgContent") or {} + elif isinstance(content.get("rcvMsgContent"), dict): + msg_content = content["rcvMsgContent"].get("msgContent") or {} + else: return None - text = str(content.get("text", "")).strip() + if msg_content.get("type") != "text": + return None + text = str(msg_content.get("text", "")).strip() if not text: return None contact = chat_info.get("contact") or {} contact_id = str(contact.get("contactId") or chat_info.get("chatId") or "") - display_name = contact.get("displayName") or contact.get("localDisplayName") + profile = contact.get("profile") or {} + # v7's Contact has `displayName: null`; the real peer name lives in + # `profile.displayName`, and `localDisplayName` is auto-suffixed on + # collisions (e.g. a second contact becomes `pepper_1`). + display_name = ( + profile.get("displayName") + or contact.get("displayName") + or contact.get("localDisplayName") + ) if not contact_id: return None if not self.is_authorized(contact_id, display_name): diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 175b2f4..7f3f030 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -66,15 +66,26 @@ def test_check_requirements_needs_url(): # ---- adapter: parsing ---- def direct_item(text="hello", contact_id="4", display="alice", direction="directRcv", kind="text"): + # v7 wire shape: the Contact's `displayName` is null and the peer name lives + # in `profile.displayName`; `localDisplayName` is auto-suffixed on collision. + # Received text is tagged `rcvMsgContent` with the message under `msgContent`. return { "chatInfo": { "type": "direct", "chatId": contact_id, - "contact": {"contactId": contact_id, "displayName": display}, + "contact": { + "contactId": contact_id, + "localDisplayName": display, + "displayName": None, + "profile": {"displayName": display}, + }, }, "chatItem": { "chatDir": {"type": direction}, - "content": {"type": kind, "text": text}, + "content": { + "type": "rcvMsgContent", + "msgContent": {"type": kind, "text": text}, + }, }, } @@ -103,6 +114,54 @@ def test_parse_filters_unauthorized_contact(): assert adapter._parse_chat_item(direct_item(contact_id="4", display="alice")) is None +def test_parse_real_v7_payload_with_suffixed_local_name(): + # Captured live from simplex-chat v7 (`/_get chat @3`): the text is nested + # under content.msgContent; Contact.displayName is null, the peer name is in + # profile.displayName, and a collision-suffixed localDisplayName must not + # defeat the allowlist. + adapter = SimplexAdapter({"allowed_users": ["pepper"]}) + item = { + "chatInfo": { + "type": "direct", + "chatId": 6, + "contact": { + "contactId": 6, + "localDisplayName": "pepper_1", + "displayName": None, + "profile": {"displayName": "pepper"}, + }, + }, + "chatItem": { + "chatDir": {"type": "directRcv"}, + "content": { + "type": "rcvMsgContent", + "msgContent": { + "type": "text", + "text": "what is the next thing on my calendar?", + }, + }, + }, + } + message = adapter._parse_chat_item(item) + assert message is not None + assert message.text == "what is the next thing on my calendar?" + assert message.contact_id == "6" + assert message.display_name == "pepper" + + +def test_parse_accepts_aeson_nested_content_shape(): + # The daemon's DB/Aeson encoding nests under the constructor key instead of + # tagging with `type`; the parser tolerates both. + adapter = SimplexAdapter({"allow_all_users": True}) + item = direct_item() + item["chatItem"]["content"] = { + "rcvMsgContent": {"msgContent": {"type": "text", "text": "hi"}} + } + message = adapter._parse_chat_item(item) + assert message is not None + assert message.text == "hi" + + # ---- adapter: send command ---- def test_send_command_is_structured_not_shortcut():