comparison sat/plugins/plugin_exp_jingle_stream.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin for managing pipes (experimental) 4 # SAT plugin for managing pipes (experimental)
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
49 C.PI_MAIN: "JingleStream", 49 C.PI_MAIN: "JingleStream",
50 C.PI_HANDLER: "no", 50 C.PI_HANDLER: "no",
51 C.PI_DESCRIPTION: _("""Jingle Stream plugin"""), 51 C.PI_DESCRIPTION: _("""Jingle Stream plugin"""),
52 } 52 }
53 53
54 CONFIRM = D_(u"{peer} wants to send you a stream, do you accept ?") 54 CONFIRM = D_("{peer} wants to send you a stream, do you accept ?")
55 CONFIRM_TITLE = D_(u"Stream Request") 55 CONFIRM_TITLE = D_("Stream Request")
56 56
57 57
58 class StreamProtocol(protocol.Protocol): 58 class StreamProtocol(protocol.Protocol):
59 def __init__(self): 59 def __init__(self):
60 self.pause = False 60 self.pause = False
117 self.client_conn.setPause(True) 117 self.client_conn.setPause(True)
118 118
119 def startStream(self, consumer): 119 def startStream(self, consumer):
120 if self.consumer is not None: 120 if self.consumer is not None:
121 raise exceptions.InternalError( 121 raise exceptions.InternalError(
122 _(u"stream can't be used with multiple consumers") 122 _("stream can't be used with multiple consumers")
123 ) 123 )
124 assert self.deferred is None 124 assert self.deferred is None
125 self.consumer = consumer 125 self.consumer = consumer
126 consumer.registerProducer(self, True) 126 consumer.registerProducer(self, True)
127 self.deferred = defer.Deferred() 127 self.deferred = defer.Deferred()
164 164
165 def write(self, data): 165 def write(self, data):
166 try: 166 try:
167 self.client_conn.sendData(data) 167 self.client_conn.sendData(data)
168 except AttributeError: 168 except AttributeError:
169 log.warning(_(u"No client connected, can't send data")) 169 log.warning(_("No client connected, can't send data"))
170 170
171 def writeToConsumer(self, data): 171 def writeToConsumer(self, data):
172 self.consumer.write(data) 172 self.consumer.write(data)
173 173
174 174
184 "streamOut", 184 "streamOut",
185 ".plugin", 185 ".plugin",
186 in_sign="ss", 186 in_sign="ss",
187 out_sign="s", 187 out_sign="s",
188 method=self._streamOut, 188 method=self._streamOut,
189 async=True, 189 async_=True,
190 ) 190 )
191 191
192 # jingle callbacks 192 # jingle callbacks
193 193
194 def _streamOut(self, to_jid_s, profile_key): 194 def _streamOut(self, to_jid_s, profile_key):
225 "senders": self._j.ROLE_INITIATOR, 225 "senders": self._j.ROLE_INITIATOR,
226 "app_kwargs": {"stream_object": factory}, 226 "app_kwargs": {"stream_object": factory},
227 } 227 }
228 ], 228 ],
229 ) 229 )
230 defer.returnValue(unicode(port)) 230 defer.returnValue(str(port))
231 231
232 def jingleSessionInit(self, client, session, content_name, stream_object): 232 def jingleSessionInit(self, client, session, content_name, stream_object):
233 content_data = session["contents"][content_name] 233 content_data = session["contents"][content_name]
234 application_data = content_data["application_data"] 234 application_data = content_data["application_data"]
235 assert "stream_object" not in application_data 235 assert "stream_object" not in application_data
243 content_data = session["contents"][content_name] 243 content_data = session["contents"][content_name]
244 if content_data["senders"] not in ( 244 if content_data["senders"] not in (
245 self._j.ROLE_INITIATOR, 245 self._j.ROLE_INITIATOR,
246 self._j.ROLE_RESPONDER, 246 self._j.ROLE_RESPONDER,
247 ): 247 ):
248 log.warning(u"Bad sender, assuming initiator") 248 log.warning("Bad sender, assuming initiator")
249 content_data["senders"] = self._j.ROLE_INITIATOR 249 content_data["senders"] = self._j.ROLE_INITIATOR
250 250
251 confirm_data = yield xml_tools.deferDialog( 251 confirm_data = yield xml_tools.deferDialog(
252 self.host, 252 self.host,
253 _(CONFIRM).format(peer=session["peer_jid"].full()), 253 _(CONFIRM).format(peer=session["peer_jid"].full()),
264 if not C.bool(confirm_data["answer"]): 264 if not C.bool(confirm_data["answer"]):
265 defer.returnValue(False) 265 defer.returnValue(False)
266 try: 266 try:
267 port = int(confirm_data["port"]) 267 port = int(confirm_data["port"])
268 except (ValueError, KeyError): 268 except (ValueError, KeyError):
269 raise exceptions.DataError(_(u"given port is invalid")) 269 raise exceptions.DataError(_("given port is invalid"))
270 endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", port) 270 endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", port)
271 factory = StreamFactory() 271 factory = StreamFactory()
272 yield endpoint.connect(factory) 272 yield endpoint.connect(factory)
273 content_data["stream_object"] = factory 273 content_data["stream_object"] = factory
274 finished_d = content_data["finished_d"] = defer.Deferred() 274 finished_d = content_data["finished_d"] = defer.Deferred()
286 content_data["stream_object"] = application_data["stream_object"] 286 content_data["stream_object"] = application_data["stream_object"]
287 finished_d = content_data["finished_d"] = defer.Deferred() 287 finished_d = content_data["finished_d"] = defer.Deferred()
288 args = [client, session, content_name, content_data] 288 args = [client, session, content_name, content_data]
289 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args) 289 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
290 else: 290 else:
291 log.warning(u"FIXME: unmanaged action {}".format(action)) 291 log.warning("FIXME: unmanaged action {}".format(action))
292 return desc_elt 292 return desc_elt
293 293
294 def _finishedCb(self, __, client, session, content_name, content_data): 294 def _finishedCb(self, __, client, session, content_name, content_data):
295 log.info(u"Pipe transfer completed") 295 log.info("Pipe transfer completed")
296 self._j.contentTerminate(client, session, content_name) 296 self._j.contentTerminate(client, session, content_name)
297 content_data["stream_object"].stopStream() 297 content_data["stream_object"].stopStream()
298 298
299 def _finishedEb(self, failure, client, session, content_name, content_data): 299 def _finishedEb(self, failure, client, session, content_name, content_data):
300 log.warning(u"Error while streaming pipe: {}".format(failure)) 300 log.warning("Error while streaming pipe: {}".format(failure))
301 self._j.contentTerminate( 301 self._j.contentTerminate(
302 client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT 302 client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT
303 ) 303 )
304 content_data["stream_object"].stopStream() 304 content_data["stream_object"].stopStream()