annotate sat/plugins/plugin_xep_0353.py @ 4044:3900626bc100

plugin XEP-0166: refactoring, and various improvments: - add models for transport and applications handlers and linked data - split models into separate file - some type hints - some documentation comments - add actions to prepare confirmation, useful to do initial parsing of all contents - application arg/kwargs and some transport data can be initialised during Jingle `initiate` call, this is notably useful when a call is made with transport data (this is the call for A/V calls where codecs and ICE candidate can be specified when starting a call) - session data can be specified during Jingle `initiate` call - new `store_in_session` argument in `_parse_elements`, which can be used to avoid race-condition when a context element (<decription> or <transport>) is being parsed for an action while an other action happens (like `transport-info`) - don't sed `sid` in `transport_elt` during a `transport-info` action anymore in `build_action`: this is specific to Jingle File Transfer and has been moved there rel 419
author Goffi <goffi@goffi.org>
date Mon, 15 May 2023 16:23:11 +0200
parents 877145b4ba01
children c23cad65ae99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # SàT plugin for Jingle Message Initiation (XEP-0353)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3405
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from zope.interface import implementer
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from twisted.internet import defer
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.internet import reactor
3652
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
22 from twisted.words.protocols.jabber import xmlstream, jid, error
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from wokkel import disco, iwokkel
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.i18n import _, D_
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.constants import Const as C
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core import exceptions
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.log import getLogger
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.tools import utils
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.tools import xml_tools
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 log = getLogger(__name__)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 NS_JINGLE_MESSAGE = "urn:xmpp:jingle-message:0"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 PLUGIN_INFO = {
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_NAME: "Jingle Message Initiation",
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_IMPORT_NAME: "XEP-0353",
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_TYPE: "XEP",
3651
867a15f05476 plugin XEP-0353: fix plugin modes
Goffi <goffi@goffi.org>
parents: 3517
diff changeset
41 C.PI_MODES: [C.PLUG_MODE_CLIENT],
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_PROTOCOLS: ["XEP-0353"],
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_DEPENDENCIES: ["XEP-0166"],
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_MAIN: "XEP_0353",
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_HANDLER: "yes",
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_DESCRIPTION: _("""Implementation of Jingle Message Initiation"""),
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 }
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
3517
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
50 class XEP_0353:
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def __init__(self, host):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 log.info(_("plugin {name} initialization").format(name=PLUGIN_INFO[C.PI_NAME]))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
55 host.register_namespace("jingle-message", NS_JINGLE_MESSAGE)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self._j = host.plugins["XEP-0166"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
57 host.trigger.add("XEP-0166_initiate", self._on_initiate_trigger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
58 host.trigger.add("messageReceived", self._on_message_received)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
60 def get_handler(self, client):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return Handler()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
63 def profile_connecting(self, client):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 # mapping from session id to deferred used to wait for destinee answer
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 client._xep_0353_pending_sessions = {}
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
67 def build_message_data(self, client, peer_jid, verb, session_id):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 mess_data = {
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 'from': client.jid,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 'to': peer_jid,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 'uid': '',
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 'message': {},
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 'type': C.MESS_TYPE_CHAT,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 'subject': {},
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 'extra': {}
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 }
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
77 client.generate_message_xml(mess_data)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 verb_elt = mess_data["xml"].addElement((NS_JINGLE_MESSAGE, verb))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 verb_elt["id"] = session_id
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return mess_data
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
82 async def _on_initiate_trigger(self, client, session, contents):
3517
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
83 # FIXME: check that at least one resource of the peer_jid can handle the feature
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 peer_jid = session['peer_jid']
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if peer_jid.resource:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
3652
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
88 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
89 infos = await self.host.memory.disco.get_infos(client, peer_jid)
3652
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
90 except error.StanzaError as e:
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
91 if e.condition == "service-unavailable":
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
92 categories = {}
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
93 else:
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
94 raise e
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
95 else:
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
96 categories = {c for c, __ in infos.identities}
3517
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
97 if "component" in categories:
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
98 # we don't use message initiation with components
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
99 return True
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
100
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 # if the contact is not in our roster, we need to send a directed presence
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 # according to XEP-0353 §3.1
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
106 mess_data = self.build_message_data(client, peer_jid, "propose", session['id'])
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 for content in contents:
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
108 content_data = self._j.get_content_data(
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 content)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 try:
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
111 jingle_description_elt = (
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
112 content_data.application.handler.jingle_description_elt
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
113 )
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 except AttributeError:
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
115 log.debug(
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
116 "no jingle_description_elt set for "
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
117 f"{content_data.application.handler}"
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
118 )
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 description_elt = domish.Element((content["app_ns"], "description"))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
121 description_elt = await utils.as_deferred(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
122 jingle_description_elt,
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
123 client, session, content_data.content_name, *content_data.app_args,
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
124 **content_data.app_kwargs
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 mess_data["xml"].propose.addChild(description_elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 response_d = defer.Deferred()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 # we wait for 2 min before cancelling the session init
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
129 # response_d.addTimeout(2*60, reactor)
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
130 # FIXME: let's application decide timeout?
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
131 response_d.addTimeout(2, reactor)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 client._xep_0353_pending_sessions[session['id']] = response_d
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
133 await client.send_message_data(mess_data)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 accepting_jid = await response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 except defer.TimeoutError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 log.warning(_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 "Message initiation with {peer_jid} timed out"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 ).format(peer_jid=peer_jid))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 session["peer_jid"] = accepting_jid
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 del client._xep_0353_pending_sessions[session['id']]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
145 async def _on_message_received(self, client, message_elt, post_treat):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 for elt in message_elt.elements():
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if elt.uri == NS_JINGLE_MESSAGE:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if elt.name == "propose":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
149 return await self._handle_propose(client, message_elt, elt)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 elif elt.name == "retract":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
151 return self._handle_retract(client, message_elt, elt)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 elif elt.name == "proceed":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
153 return self._handle_proceed(client, message_elt, elt)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 elif elt.name == "accept":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
155 return self._handle_accept(client, message_elt, elt)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 elif elt.name == "reject":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
157 return self._handle_accept(client, message_elt, elt)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 log.warning(f"invalid element: {elt.toXml}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
163 async def _handle_propose(self, client, message_elt, elt):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 peer_jid = jid.JID(message_elt["from"])
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 app_ns = elt.description.uri
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 try:
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
169 application = self._j.get_application(app_ns)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 human_name = getattr(application.handler, "human_name", application.name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 except (exceptions.NotFound, AttributeError):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 if app_ns.startswith("urn:xmpp:jingle:apps:"):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 human_name = app_ns[21:].split(":", 1)[0].replace('-', ' ').title()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 splitted_ns = app_ns.split(':')
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 if len(splitted_ns) > 1:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 human_name = splitted_ns[-2].replace('- ', ' ').title()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 human_name = app_ns
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 confirm_msg = D_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 "Somebody not in your contact list ({peer_jid}) wants to do a "
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 '"{human_name}" session with you, this would leak your presence and '
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 "possibly you IP (internet localisation), do you accept?"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 ).format(peer_jid=peer_jid, human_name=human_name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 confirm_title = D_("Invitation from an unknown contact")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
187 accept = await xml_tools.defer_confirm(
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 self.host, confirm_msg, confirm_title, profile=client.profile,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 action_extra={
4042
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
190 "type": C.META_TYPE_NOT_IN_ROSTER_LEAK,
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
191 "session_id": session_id,
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
192 "from_jid": peer_jid.full(),
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 }
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 if not accept:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
196 mess_data = self.build_message_data(
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 client, client.jid.userhostJID(), "reject", session_id)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
198 await client.send_message_data(mess_data)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 # we don't sent anything to sender, to avoid leaking presence
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 session_id = elt["id"]
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
204 # FIXME: accept is not used anymore in new specification, check it and remove it
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
205 mess_data = self.build_message_data(
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 client, client.jid.userhostJID(), "accept", session_id)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
207 await client.send_message_data(mess_data)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
208 mess_data = self.build_message_data(
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 client, peer_jid, "proceed", session_id)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
210 await client.send_message_data(mess_data)
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
213 def _handle_retract(self, client, message_elt, proceed_elt):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 log.warning("retract is not implemented yet")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
217 def _handle_proceed(self, client, message_elt, proceed_elt):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 session_id = proceed_elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 log.warning(f"invalid proceed element in message_elt: {message_elt}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 response_d = client._xep_0353_pending_sessions[session_id]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 log.warning(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 _("no pending session found with id {session_id}, did it timed out?")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 .format(session_id=session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 response_d.callback(jid.JID(message_elt["from"]))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
235 def _handle_accept(self, client, message_elt, accept_elt):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3652
diff changeset
238 def _handle_reject(self, client, message_elt, accept_elt):
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
241
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 @implementer(iwokkel.IDisco)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 class Handler(xmlstream.XMPPHandler):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 return [disco.DiscoFeature(NS_JINGLE_MESSAGE)]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
247
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 return []