annotate sat/plugins/plugin_xep_0353.py @ 3405:ecdb3728749e

plugin XEP-0353: Jingle Message Initiation implementation: This plugin uses the new `XEP-0166_initiate` trigger to initiate a Jingle session with messages if the peer jid has no resource specified. On reception, if the sender is not in our roster, a confirmation is requested to user to avoid leaking presence and IP. If user refuses the session for somebody not in roster, nothing is sent at all (the request is just ignored).
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:15 +0100
parents
children be6d91572633
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)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
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",
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_MODES: C.PLUG_MODE_BOTH,
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 class XEP_0353(object):
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):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 peer_jid = session['peer_jid']
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if peer_jid.resource:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 # 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
89 # according to XEP-0353 §3.1
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 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
93 for content in contents:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 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
95 content)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 jingleDescriptionElt = application.handler.jingleDescriptionElt
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 except AttributeError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 log.debug(f"no jingleDescriptionElt set for {application.handler}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 description_elt = domish.Element((content["app_ns"], "description"))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 description_elt = await utils.asDeferred(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 jingleDescriptionElt,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 client, session, content_name, *app_args, **app_kwargs
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["xml"].propose.addChild(description_elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 response_d = defer.Deferred()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 # 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
109 response_d.addTimeout(2*60, reactor)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 client._xep_0353_pending_sessions[session['id']] = response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 accepting_jid = await response_d
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 except defer.TimeoutError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 log.warning(_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 "Message initiation with {peer_jid} timed out"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 ).format(peer_jid=peer_jid))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 session["peer_jid"] = accepting_jid
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 del client._xep_0353_pending_sessions[session['id']]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 async def _onMessageReceived(self, client, message_elt, post_treat):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 for elt in message_elt.elements():
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if elt.uri == NS_JINGLE_MESSAGE:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 if elt.name == "propose":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 return await self._handlePropose(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 elif elt.name == "retract":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return self._handleRetract(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 elif elt.name == "proceed":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return self._handleProceed(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 elif elt.name == "accept":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 elif elt.name == "reject":
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return self._handleAccept(client, message_elt, elt)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 log.warning(f"invalid element: {elt.toXml}")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 async def _handlePropose(self, client, message_elt, elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 peer_jid = jid.JID(message_elt["from"])
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if peer_jid.userhostJID() not in client.roster:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 app_ns = elt.description.uri
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 application = self._j.getApplication(app_ns)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 human_name = getattr(application.handler, "human_name", application.name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 except (exceptions.NotFound, AttributeError):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if app_ns.startswith("urn:xmpp:jingle:apps:"):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 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
152 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 splitted_ns = app_ns.split(':')
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 if len(splitted_ns) > 1:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 human_name = splitted_ns[-2].replace('- ', ' ').title()
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 human_name = app_ns
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 confirm_msg = D_(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 "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
161 '"{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
162 "possibly you IP (internet localisation), do you accept?"
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 ).format(peer_jid=peer_jid, human_name=human_name)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 confirm_title = D_("Invitation from an unknown contact")
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 accept = await xml_tools.deferConfirm(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 self.host, confirm_msg, confirm_title, profile=client.profile,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 action_extra={
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 "meta_type": C.META_TYPE_NOT_IN_ROSTER_LEAK,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 "meta_session_id": session_id,
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 "meta_from_jid": peer_jid.full(),
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 }
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 if not accept:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 client, client.jid.userhostJID(), "reject", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 # 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
178 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 else:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 await client.presence.available(peer_jid)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 session_id = elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 client, client.jid.userhostJID(), "accept", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 mess_data = self.buildMessageData(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 client, peer_jid, "proceed", session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 await client.sendMessageData(mess_data)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 def _handleRetract(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 log.warning("retract is not implemented yet")
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
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def _handleProceed(self, client, message_elt, proceed_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 session_id = proceed_elt["id"]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 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
199 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 try:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 response_d = client._xep_0353_pending_sessions[session_id]
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 except KeyError:
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 log.warning(
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 _("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
205 .format(session_id=session_id)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 )
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return True
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 response_d.callback(jid.JID(message_elt["from"]))
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 return False
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 def _handleAccept(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 def _handleReject(self, client, message_elt, accept_elt):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 pass
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217
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 @implementer(iwokkel.IDisco)
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 class Handler(xmlstream.XMPPHandler):
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 getDiscoInfo(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 return [disco.DiscoFeature(NS_JINGLE_MESSAGE)]
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 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
ecdb3728749e plugin XEP-0353: Jingle Message Initiation implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return []