Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0261.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/plugins/plugin_xep_0261.py@524856bd7b19 |
children | e11b13418ba6 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT plugin for Jingle (XEP-0261) | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from libervia.backend.core.i18n import _ | |
21 from libervia.backend.core.constants import Const as C | |
22 from libervia.backend.core.log import getLogger | |
23 | |
24 log = getLogger(__name__) | |
25 from wokkel import disco, iwokkel | |
26 from zope.interface import implementer | |
27 from twisted.words.xish import domish | |
28 import uuid | |
29 | |
30 try: | |
31 from twisted.words.protocols.xmlstream import XMPPHandler | |
32 except ImportError: | |
33 from wokkel.subprotocols import XMPPHandler | |
34 | |
35 | |
36 NS_JINGLE_IBB = "urn:xmpp:jingle:transports:ibb:1" | |
37 | |
38 PLUGIN_INFO = { | |
39 C.PI_NAME: "Jingle In-Band Bytestreams", | |
40 C.PI_IMPORT_NAME: "XEP-0261", | |
41 C.PI_TYPE: "XEP", | |
42 C.PI_MODES: C.PLUG_MODE_BOTH, | |
43 C.PI_PROTOCOLS: ["XEP-0261"], | |
44 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0047"], | |
45 C.PI_MAIN: "XEP_0261", | |
46 C.PI_HANDLER: "yes", | |
47 C.PI_DESCRIPTION: _("""Implementation of Jingle In-Band Bytestreams"""), | |
48 } | |
49 | |
50 | |
51 class XEP_0261(object): | |
52 NAMESPACE = NS_JINGLE_IBB # used by XEP-0260 plugin for transport-replace | |
53 | |
54 def __init__(self, host): | |
55 log.info(_("plugin Jingle In-Band Bytestreams")) | |
56 self.host = host | |
57 self._j = host.plugins["XEP-0166"] # shortcut to access jingle | |
58 self._ibb = host.plugins["XEP-0047"] # and in-band bytestream | |
59 self._j.register_transport( | |
60 NS_JINGLE_IBB, self._j.TRANSPORT_STREAMING, self, -10000 | |
61 ) # must be the lowest priority | |
62 | |
63 def get_handler(self, client): | |
64 return XEP_0261_handler() | |
65 | |
66 def jingle_session_init(self, client, session, content_name): | |
67 transport_elt = domish.Element((NS_JINGLE_IBB, "transport")) | |
68 content_data = session["contents"][content_name] | |
69 transport_data = content_data["transport_data"] | |
70 transport_data["block_size"] = self._ibb.BLOCK_SIZE | |
71 transport_elt["block-size"] = str(transport_data["block_size"]) | |
72 transport_elt["sid"] = transport_data["sid"] = str(uuid.uuid4()) | |
73 return transport_elt | |
74 | |
75 def jingle_handler(self, client, action, session, content_name, transport_elt): | |
76 content_data = session["contents"][content_name] | |
77 transport_data = content_data["transport_data"] | |
78 if action in ( | |
79 self._j.A_SESSION_ACCEPT, | |
80 self._j.A_ACCEPTED_ACK, | |
81 self._j.A_TRANSPORT_ACCEPT, | |
82 ): | |
83 pass | |
84 elif action in (self._j.A_SESSION_INITIATE, self._j.A_TRANSPORT_REPLACE): | |
85 transport_data["sid"] = transport_elt["sid"] | |
86 elif action in (self._j.A_START, self._j.A_PREPARE_RESPONDER): | |
87 local_jid = session["local_jid"] | |
88 peer_jid = session["peer_jid"] | |
89 sid = transport_data["sid"] | |
90 stream_object = content_data["stream_object"] | |
91 if action == self._j.A_START: | |
92 block_size = transport_data["block_size"] | |
93 d = self._ibb.start_stream( | |
94 client, stream_object, local_jid, peer_jid, sid, block_size | |
95 ) | |
96 d.chainDeferred(content_data["finished_d"]) | |
97 else: | |
98 d = self._ibb.create_session( | |
99 client, stream_object, local_jid, peer_jid, sid) | |
100 d.chainDeferred(content_data["finished_d"]) | |
101 else: | |
102 log.warning("FIXME: unmanaged action {}".format(action)) | |
103 return transport_elt | |
104 | |
105 | |
106 @implementer(iwokkel.IDisco) | |
107 class XEP_0261_handler(XMPPHandler): | |
108 | |
109 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | |
110 return [disco.DiscoFeature(NS_JINGLE_IBB)] | |
111 | |
112 def getDiscoItems(self, requestor, target, nodeIdentifier=""): | |
113 return [] |