comparison sat/plugins/plugin_xep_0071.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children c23cad65ae99
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
92 92
93 def __init__(self, host): 93 def __init__(self, host):
94 log.info(_("XHTML-IM plugin initialization")) 94 log.info(_("XHTML-IM plugin initialization"))
95 self.host = host 95 self.host = host
96 self._s = self.host.plugins["TEXT_SYNTAXES"] 96 self._s = self.host.plugins["TEXT_SYNTAXES"]
97 self._s.addSyntax( 97 self._s.add_syntax(
98 self.SYNTAX_XHTML_IM, 98 self.SYNTAX_XHTML_IM,
99 lambda xhtml: xhtml, 99 lambda xhtml: xhtml,
100 self.XHTML2XHTML_IM, 100 self.XHTML2XHTML_IM,
101 [self._s.OPT_HIDDEN], 101 [self._s.OPT_HIDDEN],
102 ) 102 )
103 host.trigger.add("messageReceived", self.messageReceivedTrigger) 103 host.trigger.add("messageReceived", self.message_received_trigger)
104 host.trigger.add("sendMessage", self.sendMessageTrigger) 104 host.trigger.add("sendMessage", self.send_message_trigger)
105 105
106 def getHandler(self, client): 106 def get_handler(self, client):
107 return XEP_0071_handler(self) 107 return XEP_0071_handler(self)
108 108
109 def _messagePostTreat(self, data, message_elt, body_elts, client): 109 def _message_post_treat(self, data, message_elt, body_elts, client):
110 """Callback which manage the post treatment of the message in case of XHTML-IM found 110 """Callback which manage the post treatment of the message in case of XHTML-IM found
111 111
112 @param data: data send by messageReceived trigger through post_treat deferred 112 @param data: data send by messageReceived 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
153 if not (lang or "") in data["message"]: 153 if not (lang or "") in data["message"]:
154 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT) 154 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT)
155 d.addCallback(self._fill_body_text, data, lang) 155 d.addCallback(self._fill_body_text, data, lang)
156 defers.append(d) 156 defers.append(d)
157 157
158 def _sendMessageAddRich(self, data, client): 158 def _send_message_add_rich(self, data, client):
159 """ Construct XHTML-IM node and add it XML element 159 """ Construct XHTML-IM node and add it XML element
160 160
161 @param data: message data as sended by sendMessage callback 161 @param data: message data as sended by sendMessage callback
162 """ 162 """
163 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists 163 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists
172 data["extra"]["xhtml_{}".format(lang)] = xhtml_im 172 data["extra"]["xhtml_{}".format(lang)] = xhtml_im
173 else: 173 else:
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.get_current_syntax(client.profile)
178 defers = [] 178 defers = []
179 if "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.get_sub_dict("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 "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.get_sub_dict("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("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 message_received_trigger(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 = 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
207 else: 207 else:
208 body_elts = html_elt.elements(NS_XHTML, "body") 208 body_elts = html_elt.elements(NS_XHTML, "body")
209 post_treat.addCallback(self._messagePostTreat, 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 sendMessageTrigger(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"):
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:
229 data["xhtml"] = xhtml 229 data["xhtml"] = xhtml
230 post_xml_treatments.addCallback(self._sendMessageAddRich, client) 230 post_xml_treatments.addCallback(self._send_message_add_rich, client)
231 return True 231 return True
232 232
233 def _purgeStyle(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
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._purgeStyle(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:
284 # we need to remove the element and all descendants 284 # we need to remove the element and all descendants
285 log.debug("removing black listed tag: %s" % (elem.tag)) 285 log.debug("removing black listed tag: %s" % (elem.tag))