changeset 4053:dd39e60ca2aa

plugin XEP-0166: set `jingle_elt`, `desc_elt` and `transport_elt`: those values are set to corresponding domish.Element in session for session init/confirmation/accept workflow.
author Goffi <goffi@goffi.org>
date Mon, 29 May 2023 13:32:28 +0200
parents 2ced30f6d5de
children 4c8bf67bfbeb
files sat/plugins/plugin_xep_0166/__init__.py
diffstat 1 files changed, 25 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0166/__init__.py	Mon May 29 13:32:19 2023 +0200
+++ b/sat/plugins/plugin_xep_0166/__init__.py	Mon May 29 13:32:28 2023 +0200
@@ -251,6 +251,7 @@
         @param request(domsih.Element): jingle request
         @param client: %(doc_client)s
         """
+        del session["jingle_elt"]
         log.warning(f"Error while processing jingle request [{client.profile}]")
         if isinstance(failure_.value, defer.FirstError):
             failure_ = failure_.value.subFailure.value
@@ -510,6 +511,7 @@
             client, session, XEP_0166.A_SESSION_INITIATE
         )
         jingle_elt["initiator"] = initiator.full()
+        session["jingle_elt"] = jingle_elt
 
         session_contents = session["contents"]
 
@@ -527,11 +529,13 @@
                 )
 
             # we build the session data for this content
+            application_data = {}
+            transport_data = content_data.transport_data
             session_content = {
                 "application": content_data.application,
-                "application_data": {},
+                "application_data": application_data,
                 "transport": transport,
-                "transport_data": content_data.transport_data,
+                "transport_data": transport_data,
                 "creator": XEP_0166.ROLE_INITIATOR,
                 "senders": content.get("senders", "both"),
             }
@@ -551,7 +555,7 @@
                 pass
 
             # then the description element
-            desc_elt = await utils.as_deferred(
+            application_data["desc_elt"] = desc_elt = await utils.as_deferred(
                 content_data.application.handler.jingle_session_init,
                 client, session, content_data.content_name,
                 *content_data.app_args, **content_data.app_kwargs
@@ -559,7 +563,7 @@
             content_elt.addChild(desc_elt)
 
             # and the transport one
-            transport_elt = await utils.as_deferred(
+            transport_data["transport_elt"] = transport_elt = await utils.as_deferred(
                 transport.handler.jingle_session_init,
                 client, session, content_data.content_name,
             )
@@ -570,6 +574,13 @@
             client, session, iq_elt, jingle_elt
         ):
             return sid
+
+        # processing is done, we can remove elements
+        for content_data in session_contents.values():
+            del content_data["application_data"]["desc_elt"]
+            del content_data["transport_data"]["transport_elt"]
+        del session["jingle_elt"]
+
         if encrypted:
             for content in session["contents"].values():
                 if "encryption" not in content:
@@ -742,7 +753,8 @@
     ) -> Dict[str, dict]:
         """Parse contents elements and fill contents_dict accordingly
 
-        after the parsing, contents_dict will containt handlers, "desc_elt" and "transport_elt"
+        after the parsing, contents_dict will containt handlers, "desc_elt" and
+        "transport_elt"
         @param jingle_elt: parent <jingle> element, containing one or more <content>
         @param session: session data
         @param request: the whole request
@@ -979,6 +991,8 @@
         client.send(xmlstream.toResponse(request, "result"))
 
 
+        assert "jingle_elt" not in session
+        session["jingle_elt"] = jingle_elt
         if not await self.host.trigger.async_point(
             "XEP-0166_on_session_initiate",
             client, session, request, jingle_elt
@@ -1017,6 +1031,7 @@
         @param jingle_elt(domish.Element): jingle data of this session
         @param client: %(doc_client)s
         """
+        del session["jingle_elt"]
         confirmed = all(confirm_results)
         if not confirmed:
             return self.terminate(client, XEP_0166.REASON_DECLINE, session)
@@ -1025,6 +1040,7 @@
             client, session, XEP_0166.A_SESSION_ACCEPT
         )
         jingle_elt["responder"] = session['local_jid'].full()
+        session["jingle_elt"] = jingle_elt
 
         # contents
 
@@ -1076,6 +1092,7 @@
                 elements=False,
             )
         )
+        d_list.addCallback(lambda __: session.pop("jingle_elt"))
         d_list.addCallback(lambda __: iq_elt.send())
 
         def change_state(__, session):
@@ -1092,7 +1109,7 @@
 
     def on_session_terminate(self, client, request, jingle_elt, session):
         # TODO: check reason, display a message to user if needed
-        log.debug("Jingle Session {} terminated".format(session["id"]))
+        log.debug(f"Jingle Session {session['id']} terminated")
         try:
             reason_elt = next(jingle_elt.elements(NS_JINGLE, "reason"))
         except StopIteration:
@@ -1135,6 +1152,7 @@
         client.send(xmlstream.toResponse(request, "result"))
         # and change the state
         session["state"] = STATE_ACTIVE
+        session["jingle_elt"] = jingle_elt
 
         await defer.DeferredList(self._call_plugins(
             client,
@@ -1154,6 +1172,7 @@
                 client, XEP_0166.A_START, session, app_method_name=None, elements=False
             )
         )
+        negociate_dlist.addCallback(lambda __: session.pop("jingle_elt"))
 
     def _on_session_cb(self, result, client, request, jingle_elt, session):
         client.send(xmlstream.toResponse(request, "result"))