comparison sat/plugins/plugin_xep_0071.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 85d3240a400f
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 Publish-Subscribe (xep-0071) 4 # SAT plugin for Publish-Subscribe (xep-0071)
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
25 log = getLogger(__name__) 25 log = getLogger(__name__)
26 from sat.tools.common import data_format 26 from sat.tools.common import data_format
27 27
28 from twisted.internet import defer 28 from twisted.internet import defer
29 from wokkel import disco, iwokkel 29 from wokkel import disco, iwokkel
30 from zope.interface import implements 30 from zope.interface import implementer
31 31
32 # from lxml import etree 32 # from lxml import etree
33 try: 33 try:
34 from lxml import html 34 from lxml import html
35 except ImportError: 35 except ImportError:
36 raise exceptions.MissingModule( 36 raise exceptions.MissingModule(
37 u"Missing module lxml, please download/install it from http://lxml.de/" 37 "Missing module lxml, please download/install it from http://lxml.de/"
38 ) 38 )
39 try: 39 try:
40 from twisted.words.protocols.xmlstream import XMPPHandler 40 from twisted.words.protocols.xmlstream import XMPPHandler
41 except ImportError: 41 except ImportError:
42 from wokkel.subprotocols import XMPPHandler 42 from wokkel.subprotocols import XMPPHandler
174 data["extra"]["xhtml"] = xhtml_im 174 data["extra"]["xhtml"] = xhtml_im
175 body_elt.addRawXml(xhtml_im) 175 body_elt.addRawXml(xhtml_im)
176 176
177 syntax = self._s.getCurrentSyntax(client.profile) 177 syntax = self._s.getCurrentSyntax(client.profile)
178 defers = [] 178 defers = []
179 if u"xhtml" in data["extra"]: 179 if "xhtml" in data["extra"]:
180 # we have directly XHTML 180 # we have directly XHTML
181 for lang, xhtml in data_format.getSubDict("xhtml", data["extra"]): 181 for lang, xhtml in data_format.getSubDict("xhtml", data["extra"]):
182 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers) 182 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers)
183 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM) 183 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
184 d.addCallback(syntax_converted, lang) 184 d.addCallback(syntax_converted, lang)
185 defers.append(d) 185 defers.append(d)
186 elif u"rich" in data["extra"]: 186 elif "rich" in data["extra"]:
187 # we have rich syntax to convert 187 # we have rich syntax to convert
188 for lang, rich_data in data_format.getSubDict("rich", data["extra"]): 188 for lang, rich_data in data_format.getSubDict("rich", data["extra"]):
189 self._check_body_text(data, lang, rich_data, syntax, defers) 189 self._check_body_text(data, lang, rich_data, syntax, defers)
190 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM) 190 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM)
191 d.addCallback(syntax_converted, lang) 191 d.addCallback(syntax_converted, lang)
192 defers.append(d) 192 defers.append(d)
193 else: 193 else:
194 exceptions.InternalError(u"xhtml or rich should be present at this point") 194 exceptions.InternalError("xhtml or rich should be present at this point")
195 d_list = defer.DeferredList(defers) 195 d_list = defer.DeferredList(defers)
196 d_list.addCallback(lambda __: data) 196 d_list.addCallback(lambda __: data)
197 return d_list 197 return d_list
198 198
199 def messageReceivedTrigger(self, client, message, post_treat): 199 def messageReceivedTrigger(self, client, message, post_treat):
200 """ Check presence of XHTML-IM in message 200 """ Check presence of XHTML-IM in message
201 """ 201 """
202 try: 202 try:
203 html_elt = message.elements(NS_XHTML_IM, "html").next() 203 html_elt = next(message.elements(NS_XHTML_IM, "html"))
204 except StopIteration: 204 except StopIteration:
205 # No XHTML-IM 205 # No XHTML-IM
206 pass 206 pass
207 else: 207 else:
208 body_elts = html_elt.elements(NS_XHTML, "body") 208 body_elts = html_elt.elements(NS_XHTML, "body")
211 211
212 def sendMessageTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): 212 def sendMessageTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
213 """ Check presence of rich text in extra """ 213 """ Check presence of rich text in extra """
214 rich = {} 214 rich = {}
215 xhtml = {} 215 xhtml = {}
216 for key, value in data["extra"].iteritems(): 216 for key, value in data["extra"].items():
217 if key.startswith("rich"): 217 if key.startswith("rich"):
218 rich[key[5:]] = value 218 rich[key[5:]] = value
219 elif key.startswith("xhtml"): 219 elif key.startswith("xhtml"):
220 xhtml[key[6:]] = value 220 xhtml[key[6:]] = value
221 if rich and xhtml: 221 if rich and xhtml:
222 raise exceptions.DataError( 222 raise exceptions.DataError(
223 _(u"Can't have XHTML and rich content at the same time") 223 _("Can't have XHTML and rich content at the same time")
224 ) 224 )
225 if rich or xhtml: 225 if rich or xhtml:
226 if rich: 226 if rich:
227 data["rich"] = rich 227 data["rich"] = rich
228 else: 228 else:
245 name = name.strip() 245 name = name.strip()
246 if name not in styles_allowed: 246 if name not in styles_allowed:
247 continue 247 continue
248 purged.append((name, value.strip())) 248 purged.append((name, value.strip()))
249 249
250 return u"; ".join([u"%s: %s" % data for data in purged]) 250 return "; ".join(["%s: %s" % data for data in purged])
251 251
252 def XHTML2XHTML_IM(self, xhtml): 252 def XHTML2XHTML_IM(self, xhtml):
253 """ Convert XHTML document to XHTML_IM subset 253 """ Convert XHTML document to XHTML_IM subset
254 @param xhtml: raw xhtml to convert 254 @param xhtml: raw xhtml to convert
255 """ 255 """
263 body_elt = html.Element("body") 263 body_elt = html.Element("body")
264 body_elt.append(root) 264 body_elt.append(root)
265 else: 265 else:
266 body_elt.attrib.clear() 266 body_elt.attrib.clear()
267 267
268 allowed_tags = allowed.keys() 268 allowed_tags = list(allowed.keys())
269 to_strip = [] 269 to_strip = []
270 for elem in body_elt.iter(): 270 for elem in body_elt.iter():
271 if elem.tag not in allowed_tags: 271 if elem.tag not in allowed_tags:
272 to_strip.append(elem) 272 to_strip.append(elem)
273 else: 273 else:
280 attrib["style"] = self._purgeStyle(attrib["style"]) 280 attrib["style"] = self._purgeStyle(attrib["style"])
281 281
282 for elem in to_strip: 282 for elem in to_strip:
283 if elem.tag in blacklist: 283 if elem.tag in blacklist:
284 # we need to remove the element and all descendants 284 # we need to remove the element and all descendants
285 log.debug(u"removing black listed tag: %s" % (elem.tag)) 285 log.debug("removing black listed tag: %s" % (elem.tag))
286 elem.drop_tree() 286 elem.drop_tree()
287 else: 287 else:
288 elem.drop_tag() 288 elem.drop_tag()
289 if len(body_elt) != 1: 289 if len(body_elt) != 1:
290 root_elt = body_elt 290 root_elt = body_elt
293 root_elt = body_elt[0] 293 root_elt = body_elt[0]
294 294
295 return html.tostring(root_elt, encoding="unicode", method="xml") 295 return html.tostring(root_elt, encoding="unicode", method="xml")
296 296
297 297
298 @implementer(iwokkel.IDisco)
298 class XEP_0071_handler(XMPPHandler): 299 class XEP_0071_handler(XMPPHandler):
299 implements(iwokkel.IDisco)
300 300
301 def __init__(self, plugin_parent): 301 def __init__(self, plugin_parent):
302 self.plugin_parent = plugin_parent 302 self.plugin_parent = plugin_parent
303 self.host = plugin_parent.host 303 self.host = plugin_parent.host
304 304