annotate sat/plugins/plugin_xep_0353.py @ 3521:62f490eff51c

jp (pubsub, blog): `rename` commands implementation
author Goffi <goffi@goffi.org>
date Sat, 01 May 2021 18:39:34 +0200
parents 8508fab9bcc2
children 867a15f05476
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import xmlstream, jid
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",
3517
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
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
3517
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
88 infos = await self.host.memory.disco.getInfos(client, peer_jid)
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
89 categories = {c for c, __ in infos.identities}
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
90 if "component" in categories:
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
91 # 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
92 return True
8508fab9bcc2 plugin XEP-0353: don't use Jingle Message Initiation for components:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
93
3405
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 # 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
96 # according to XEP-0353 §3.1
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 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
100 for content in contents:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 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
102 content)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 jingleDescriptionElt = application.handler.jingleDescriptionElt
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 except AttributeError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 log.debug(f"no jingleDescriptionElt set for {application.handler}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 description_elt = domish.Element((content["app_ns"], "description"))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 description_elt = await utils.asDeferred(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 jingleDescriptionElt,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 client, session, content_name, *app_args, **app_kwargs
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 mess_data["xml"].propose.addChild(description_elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 response_d = defer.Deferred()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 # 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
116 response_d.addTimeout(2*60, reactor)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 client._xep_0353_pending_sessions[session['id']] = response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 accepting_jid = await response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 except defer.TimeoutError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 log.warning(_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "Message initiation with {peer_jid} timed out"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 ).format(peer_jid=peer_jid))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 session["peer_jid"] = accepting_jid
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 del client._xep_0353_pending_sessions[session['id']]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 async def _onMessageReceived(self, client, message_elt, post_treat):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 for elt in message_elt.elements():
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 if elt.uri == NS_JINGLE_MESSAGE:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 if elt.name == "propose":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 return await self._handlePropose(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 elif elt.name == "retract":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return self._handleRetract(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 elif elt.name == "proceed":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return self._handleProceed(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 elif elt.name == "accept":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 elif elt.name == "reject":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 log.warning(f"invalid element: {elt.toXml}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 async def _handlePropose(self, client, message_elt, elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 peer_jid = jid.JID(message_elt["from"])
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 app_ns = elt.description.uri
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 application = self._j.getApplication(app_ns)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 human_name = getattr(application.handler, "human_name", application.name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 except (exceptions.NotFound, AttributeError):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if app_ns.startswith("urn:xmpp:jingle:apps:"):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 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
159 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 splitted_ns = app_ns.split(':')
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 if len(splitted_ns) > 1:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 human_name = splitted_ns[-2].replace('- ', ' ').title()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 human_name = app_ns
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 confirm_msg = D_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 "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
168 '"{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
169 "possibly you IP (internet localisation), do you accept?"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 ).format(peer_jid=peer_jid, human_name=human_name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 confirm_title = D_("Invitation from an unknown contact")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 accept = await xml_tools.deferConfirm(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.host, confirm_msg, confirm_title, profile=client.profile,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 action_extra={
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 "meta_type": C.META_TYPE_NOT_IN_ROSTER_LEAK,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 "meta_session_id": session_id,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 "meta_from_jid": peer_jid.full(),
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 }
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 if not accept:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 client, client.jid.userhostJID(), "reject", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 # 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
185 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 client, client.jid.userhostJID(), "accept", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 client, peer_jid, "proceed", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def _handleRetract(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 log.warning("retract is not implemented yet")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 def _handleProceed(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 session_id = proceed_elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 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
206 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 response_d = client._xep_0353_pending_sessions[session_id]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 log.warning(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 _("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
212 .format(session_id=session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 response_d.callback(jid.JID(message_elt["from"]))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 def _handleAccept(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 def _handleReject(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
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 @implementer(iwokkel.IDisco)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 class Handler(xmlstream.XMPPHandler):
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 getDiscoInfo(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 return [disco.DiscoFeature(NS_JINGLE_MESSAGE)]
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 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 return []