From 09d88cdfe227aa9d8f622aed72fd107513fc616a Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 16 Aug 2023 18:33:28 +0200 Subject: [PATCH] plugin XEP-0167, XEP-0353: handle ringing: - add a `jingle_preflight_info` method in the Jingle Application model to handle this kind of information - XEP-0353 is calling `jingle_preflight_info` when a `ringing` message is received - XEP-0167 use this information to send a `ringing` info to frontends --- .../backend/plugins/plugin_xep_0166/models.py | 14 ++++++++++++++ .../plugins/plugin_xep_0167/__init__.py | 18 ++++++++++++++++++ libervia/backend/plugins/plugin_xep_0234.py | 9 +++++++++ libervia/backend/plugins/plugin_xep_0353.py | 17 ++++++++++++++++- 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/libervia/backend/plugins/plugin_xep_0166/models.py b/libervia/backend/plugins/plugin_xep_0166/models.py index b626972f..52cbeaec 100644 --- a/libervia/backend/plugins/plugin_xep_0166/models.py +++ b/libervia/backend/plugins/plugin_xep_0166/models.py @@ -43,6 +43,20 @@ class BaseApplicationHandler(abc.ABC): """ pass + @abc.abstractmethod + async def jingle_preflight_info( + self, + client: SatXMPPEntity, + session: dict, + info_type: str, + info_data: dict|None = None + ) -> None: + """Called when we have new information during preflight + + Notably used by XEP-0353 to signal ringing + """ + pass + @abc.abstractmethod async def jingle_preflight_cancel( self, diff --git a/libervia/backend/plugins/plugin_xep_0167/__init__.py b/libervia/backend/plugins/plugin_xep_0167/__init__.py index e60d716e..ac9fb961 100644 --- a/libervia/backend/plugins/plugin_xep_0167/__init__.py +++ b/libervia/backend/plugins/plugin_xep_0167/__init__.py @@ -387,6 +387,24 @@ class XEP_0167(BaseApplicationHandler): if not accepted: raise exceptions.CancelError("User declined the incoming call.") + async def jingle_preflight_info( + self, + client: SatXMPPEntity, + session: dict, + info_type: str, + info_data: dict|None = None + ) -> None: + if info_type == "ringing": + if not session.get("ringing", False): + self.host.bridge.call_info( + session["id"], "ringing", "", client.profile + ) + # we indicate that the ringing has started, to avoid sending several times + # the signal + session["ringing"] = True + else: + log.warning(f"Unknown preflight info type: {info_type!r}") + async def jingle_preflight_cancel( self, client: SatXMPPEntity, diff --git a/libervia/backend/plugins/plugin_xep_0234.py b/libervia/backend/plugins/plugin_xep_0234.py index 471a1c76..c4dfc146 100644 --- a/libervia/backend/plugins/plugin_xep_0234.py +++ b/libervia/backend/plugins/plugin_xep_0234.py @@ -464,6 +464,15 @@ class XEP_0234(BaseApplicationHandler): session["file_accepted"] = True return accepted + async def jingle_preflight_info( + self, + client: SatXMPPEntity, + session: dict, + info_type: str, + info_data: dict|None = None + ) -> None: + pass + async def jingle_preflight_cancel( self, client: SatXMPPEntity, diff --git a/libervia/backend/plugins/plugin_xep_0353.py b/libervia/backend/plugins/plugin_xep_0353.py index 79cbcd03..daf27419 100644 --- a/libervia/backend/plugins/plugin_xep_0353.py +++ b/libervia/backend/plugins/plugin_xep_0353.py @@ -29,7 +29,6 @@ from libervia.backend.core.constants import Const as C from libervia.backend.core.core_types import SatXMPPEntity from libervia.backend.core.i18n import D_, _ from libervia.backend.core.log import getLogger -from libervia.backend.tools import xml_tools log = getLogger(__name__) @@ -215,6 +214,8 @@ class XEP_0353: return self._handle_accept(client, message_elt, elt) elif elt.name == "reject": return self._handle_reject(client, message_elt, elt) + elif elt.name == "ringing": + return await self._handle_ringing(client, message_elt, elt) else: log.warning(f"invalid element: {elt.toXml}") return True @@ -367,6 +368,20 @@ class XEP_0353: response_d.errback(RejectException(reason, text)) return False + async def _handle_ringing(self, client, message_elt, ringing_elt): + session_id = ringing_elt["id"] + try: + session = self._j.get_session(client, session_id) + except exceptions.NotFound: + log.warning(f"Session {session_id!r} unknown, ignoring ringing.") + return False + for __, content_data in session["contents"].items(): + await content_data["application"].handler.jingle_preflight_info( + client, session, "ringing", None + ) + + return False + @implementer(iwokkel.IDisco) class Handler(xmlstream.XMPPHandler):