Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0071.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
112 @param data: data send by message_received trigger through post_treat deferred | 112 @param data: data send by message_received trigger through post_treat deferred |
113 @param message_elt: whole <message> stanza | 113 @param message_elt: whole <message> stanza |
114 @param body_elts: XHTML-IM body elements found | 114 @param body_elts: XHTML-IM body elements found |
115 @return: the data with the extra parameter updated | 115 @return: the data with the extra parameter updated |
116 """ | 116 """ |
117 | |
117 # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message | 118 # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message |
118 def converted(xhtml, lang): | 119 def converted(xhtml, lang): |
119 if lang: | 120 if lang: |
120 data["extra"]["xhtml_{}".format(lang)] = xhtml | 121 data["extra"]["xhtml_{}".format(lang)] = xhtml |
121 else: | 122 else: |
154 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT) | 155 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT) |
155 d.addCallback(self._fill_body_text, data, lang) | 156 d.addCallback(self._fill_body_text, data, lang) |
156 defers.append(d) | 157 defers.append(d) |
157 | 158 |
158 def _send_message_add_rich(self, data, client): | 159 def _send_message_add_rich(self, data, client): |
159 """ Construct XHTML-IM node and add it XML element | 160 """Construct XHTML-IM node and add it XML element |
160 | 161 |
161 @param data: message data as sended by sendMessage callback | 162 @param data: message data as sended by sendMessage callback |
162 """ | 163 """ |
163 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists | 164 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists |
164 # but both can't exist at the same time | 165 # but both can't exist at the same time |
195 d_list = defer.DeferredList(defers) | 196 d_list = defer.DeferredList(defers) |
196 d_list.addCallback(lambda __: data) | 197 d_list.addCallback(lambda __: data) |
197 return d_list | 198 return d_list |
198 | 199 |
199 def message_received_trigger(self, client, message, post_treat): | 200 def message_received_trigger(self, client, message, post_treat): |
200 """ Check presence of XHTML-IM in message | 201 """Check presence of XHTML-IM in message""" |
201 """ | |
202 try: | 202 try: |
203 html_elt = next(message.elements(NS_XHTML_IM, "html")) | 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 |
208 body_elts = html_elt.elements(NS_XHTML, "body") | 208 body_elts = html_elt.elements(NS_XHTML, "body") |
209 post_treat.addCallback(self._message_post_treat, message, body_elts, client) | 209 post_treat.addCallback(self._message_post_treat, message, body_elts, client) |
210 return True | 210 return True |
211 | 211 |
212 def send_message_trigger(self, client, data, pre_xml_treatments, post_xml_treatments): | 212 def send_message_trigger(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"].items(): | 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 |
229 data["xhtml"] = xhtml | 229 data["xhtml"] = xhtml |
230 post_xml_treatments.addCallback(self._send_message_add_rich, client) | 230 post_xml_treatments.addCallback(self._send_message_add_rich, client) |
231 return True | 231 return True |
232 | 232 |
233 def _purge_style(self, styles_raw): | 233 def _purge_style(self, styles_raw): |
234 """ Remove unauthorised styles according to the XEP-0071 | 234 """Remove unauthorised styles according to the XEP-0071 |
235 @param styles_raw: raw styles (value of the style attribute) | 235 @param styles_raw: raw styles (value of the style attribute) |
236 """ | 236 """ |
237 purged = [] | 237 purged = [] |
238 | 238 |
239 styles = [style.strip().split(":") for style in styles_raw.split(";")] | 239 styles = [style.strip().split(":") for style in styles_raw.split(";")] |
248 purged.append((name, value.strip())) | 248 purged.append((name, value.strip())) |
249 | 249 |
250 return "; ".join(["%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 """ |
256 # TODO: more clever tag replacement (replace forbidden tags with equivalents when possible) | 256 # TODO: more clever tag replacement (replace forbidden tags with equivalents when possible) |
257 | 257 |
258 parser = html.HTMLParser(remove_comments=True, encoding="utf-8") | 258 parser = html.HTMLParser(remove_comments=True, encoding="utf-8") |
273 else: | 273 else: |
274 # we remove unallowed attributes | 274 # we remove unallowed attributes |
275 attrib = elem.attrib | 275 attrib = elem.attrib |
276 att_to_remove = set(attrib).difference(allowed[elem.tag]) | 276 att_to_remove = set(attrib).difference(allowed[elem.tag]) |
277 for att in att_to_remove: | 277 for att in att_to_remove: |
278 del (attrib[att]) | 278 del attrib[att] |
279 if "style" in attrib: | 279 if "style" in attrib: |
280 attrib["style"] = self._purge_style(attrib["style"]) | 280 attrib["style"] = self._purge_style(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: |