annotate sat/plugins/plugin_xep_0353.py @ 3788:12317ba98d99

cli: new `blocking` commands: commands to list blocked entities, block or unblock one or more entities rel 367
author Goffi <goffi@goffi.org>
date Fri, 27 May 2022 12:15:04 +0200
parents 6e34307319c0
children 524856bd7b19
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 host.registerNamespace("jingle-message", NS_JINGLE_MESSAGE)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self._j = host.plugins["XEP-0166"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 host.trigger.add("XEP-0166_initiate", self._onInitiateTrigger)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 host.trigger.add("messageReceived", self._onMessageReceived)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def getHandler(self, client):
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def profileConnecting(self, client):
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def buildMessageData(self, client, peer_jid, verb, session_id):
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 }
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 client.generateMessageXML(mess_data)
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 async def _onInitiateTrigger(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:
6e34307319c0 plugin XEP-0353: fix jingle initiation on disco "Service Unavailable" error:
Goffi <goffi@goffi.org>
parents: 3651
diff changeset
89 infos = await self.host.memory.disco.getInfos(client, peer_jid)
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 mess_data = self.buildMessageData(client, peer_jid, "propose", session['id'])
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 for content in contents:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 application, app_args, app_kwargs, content_name = self._j.getContentData(
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:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 jingleDescriptionElt = application.handler.jingleDescriptionElt
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 except AttributeError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 log.debug(f"no jingleDescriptionElt set for {application.handler}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 description_elt = domish.Element((content["app_ns"], "description"))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 description_elt = await utils.asDeferred(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 jingleDescriptionElt,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 client, session, content_name, *app_args, **app_kwargs
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 mess_data["xml"].propose.addChild(description_elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 response_d = defer.Deferred()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 # we wait for 2 min before cancelling the session init
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 response_d.addTimeout(2*60, reactor)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 client._xep_0353_pending_sessions[session['id']] = response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 accepting_jid = await response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 except defer.TimeoutError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 log.warning(_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 "Message initiation with {peer_jid} timed out"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 ).format(peer_jid=peer_jid))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 session["peer_jid"] = accepting_jid
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 del client._xep_0353_pending_sessions[session['id']]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 async def _onMessageReceived(self, client, message_elt, post_treat):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 for elt in message_elt.elements():
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if elt.uri == NS_JINGLE_MESSAGE:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if elt.name == "propose":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 return await self._handlePropose(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 elif elt.name == "retract":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return self._handleRetract(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 elif elt.name == "proceed":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 return self._handleProceed(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 elif elt.name == "accept":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 elif elt.name == "reject":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 log.warning(f"invalid element: {elt.toXml}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 async def _handlePropose(self, client, message_elt, elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 peer_jid = jid.JID(message_elt["from"])
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 app_ns = elt.description.uri
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 application = self._j.getApplication(app_ns)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 human_name = getattr(application.handler, "human_name", application.name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 except (exceptions.NotFound, AttributeError):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if app_ns.startswith("urn:xmpp:jingle:apps:"):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 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
166 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 splitted_ns = app_ns.split(':')
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if len(splitted_ns) > 1:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 human_name = splitted_ns[-2].replace('- ', ' ').title()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 human_name = app_ns
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 confirm_msg = D_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 "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
175 '"{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
176 "possibly you IP (internet localisation), do you accept?"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 ).format(peer_jid=peer_jid, human_name=human_name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 confirm_title = D_("Invitation from an unknown contact")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 accept = await xml_tools.deferConfirm(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 self.host, confirm_msg, confirm_title, profile=client.profile,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 action_extra={
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 "meta_type": C.META_TYPE_NOT_IN_ROSTER_LEAK,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 "meta_session_id": session_id,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 "meta_from_jid": peer_jid.full(),
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 }
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 if not accept:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 client, client.jid.userhostJID(), "reject", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 # 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
192 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 client, client.jid.userhostJID(), "accept", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 client, peer_jid, "proceed", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 def _handleRetract(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 log.warning("retract is not implemented yet")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 def _handleProceed(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 session_id = proceed_elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 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
213 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 response_d = client._xep_0353_pending_sessions[session_id]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 log.warning(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 _("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
219 .format(session_id=session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 response_d.callback(jid.JID(message_elt["from"]))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 def _handleAccept(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 def _handleReject(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 pass
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 @implementer(iwokkel.IDisco)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 class Handler(xmlstream.XMPPHandler):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return [disco.DiscoFeature(NS_JINGLE_MESSAGE)]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 return []