Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0095.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 xep-0095 | 4 # SAT plugin for managing xep-0095 |
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 |
23 | 23 |
24 log = getLogger(__name__) | 24 log = getLogger(__name__) |
25 from sat.core import exceptions | 25 from sat.core import exceptions |
26 from twisted.words.protocols.jabber import xmlstream | 26 from twisted.words.protocols.jabber import xmlstream |
27 from twisted.words.protocols.jabber import error | 27 from twisted.words.protocols.jabber import error |
28 from zope.interface import implements | 28 from zope.interface import implementer |
29 from wokkel import disco | 29 from wokkel import disco |
30 from wokkel import iwokkel | 30 from wokkel import iwokkel |
31 import uuid | 31 import uuid |
32 | 32 |
33 | 33 |
69 def unregisterSIProfile(self, si_profile): | 69 def unregisterSIProfile(self, si_profile): |
70 try: | 70 try: |
71 del self.si_profiles[si_profile] | 71 del self.si_profiles[si_profile] |
72 except KeyError: | 72 except KeyError: |
73 log.error( | 73 log.error( |
74 u"Trying to unregister SI profile [{}] which was not registered".format( | 74 "Trying to unregister SI profile [{}] which was not registered".format( |
75 si_profile | 75 si_profile |
76 ) | 76 ) |
77 ) | 77 ) |
78 | 78 |
79 def streamInit(self, iq_elt, client): | 79 def streamInit(self, iq_elt, client): |
81 | 81 |
82 @param iq_elt: IQ element | 82 @param iq_elt: IQ element |
83 """ | 83 """ |
84 log.info(_("XEP-0095 Stream initiation")) | 84 log.info(_("XEP-0095 Stream initiation")) |
85 iq_elt.handled = True | 85 iq_elt.handled = True |
86 si_elt = iq_elt.elements(NS_SI, "si").next() | 86 si_elt = next(iq_elt.elements(NS_SI, "si")) |
87 si_id = si_elt["id"] | 87 si_id = si_elt["id"] |
88 si_mime_type = iq_elt.getAttribute("mime-type", "application/octet-stream") | 88 si_mime_type = iq_elt.getAttribute("mime-type", "application/octet-stream") |
89 si_profile = si_elt["profile"] | 89 si_profile = si_elt["profile"] |
90 si_profile_key = ( | 90 si_profile_key = ( |
91 si_profile[len(SI_PROFILE_HEADER) :] | 91 si_profile[len(SI_PROFILE_HEADER) :] |
134 si_elt.addChild(elt) | 134 si_elt.addChild(elt) |
135 client.send(result_elt) | 135 client.send(result_elt) |
136 | 136 |
137 def _parseOfferResult(self, iq_elt): | 137 def _parseOfferResult(self, iq_elt): |
138 try: | 138 try: |
139 si_elt = iq_elt.elements(NS_SI, "si").next() | 139 si_elt = next(iq_elt.elements(NS_SI, "si")) |
140 except StopIteration: | 140 except StopIteration: |
141 log.warning(u"No <si/> element found in result while expected") | 141 log.warning("No <si/> element found in result while expected") |
142 raise exceptions.DataError | 142 raise exceptions.DataError |
143 return (iq_elt, si_elt) | 143 return (iq_elt, si_elt) |
144 | 144 |
145 def proposeStream( | 145 def proposeStream( |
146 self, | 146 self, |
163 - (D(domish_elt, domish_elt): offer deferred which returl a tuple | 163 - (D(domish_elt, domish_elt): offer deferred which returl a tuple |
164 with iq_elt and si_elt | 164 with iq_elt and si_elt |
165 """ | 165 """ |
166 offer = client.IQ() | 166 offer = client.IQ() |
167 sid = str(uuid.uuid4()) | 167 sid = str(uuid.uuid4()) |
168 log.debug(_(u"Stream Session ID: %s") % offer["id"]) | 168 log.debug(_("Stream Session ID: %s") % offer["id"]) |
169 | 169 |
170 offer["from"] = client.jid.full() | 170 offer["from"] = client.jid.full() |
171 offer["to"] = to_jid.full() | 171 offer["to"] = to_jid.full() |
172 si = offer.addElement("si", NS_SI) | 172 si = offer.addElement("si", NS_SI) |
173 si["id"] = sid | 173 si["id"] = sid |
180 offer_d = offer.send() | 180 offer_d = offer.send() |
181 offer_d.addCallback(self._parseOfferResult) | 181 offer_d.addCallback(self._parseOfferResult) |
182 return sid, offer_d | 182 return sid, offer_d |
183 | 183 |
184 | 184 |
185 @implementer(iwokkel.IDisco) | |
185 class XEP_0095_handler(xmlstream.XMPPHandler): | 186 class XEP_0095_handler(xmlstream.XMPPHandler): |
186 implements(iwokkel.IDisco) | |
187 | 187 |
188 def __init__(self, plugin_parent): | 188 def __init__(self, plugin_parent): |
189 self.plugin_parent = plugin_parent | 189 self.plugin_parent = plugin_parent |
190 self.host = plugin_parent.host | 190 self.host = plugin_parent.host |
191 | 191 |
195 ) | 195 ) |
196 | 196 |
197 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | 197 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
198 return [disco.DiscoFeature(NS_SI)] + [ | 198 return [disco.DiscoFeature(NS_SI)] + [ |
199 disco.DiscoFeature( | 199 disco.DiscoFeature( |
200 u"http://jabber.org/protocol/si/profile/{}".format(profile_name) | 200 "http://jabber.org/protocol/si/profile/{}".format(profile_name) |
201 ) | 201 ) |
202 for profile_name in self.plugin_parent.si_profiles | 202 for profile_name in self.plugin_parent.si_profiles |
203 ] | 203 ] |
204 | 204 |
205 def getDiscoItems(self, requestor, target, nodeIdentifier=""): | 205 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |