Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0071.py @ 713:8bd63daecdbf
plugin XEP-0045: fixed incorrect exceptions name
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 17 Nov 2013 17:02:22 +0100 |
parents | a25db3fe3959 |
children | bfabeedbf32e |
rev | line source |
---|---|
668 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Publish-Subscribe (xep-0071) | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from logging import debug, info, error | |
21 | |
22 from wokkel import disco, pubsub, iwokkel | |
23 from zope.interface import implements | |
24 # from lxml import etree | |
25 from lxml import html | |
26 try: | |
27 from twisted.words.protocols.xmlstream import XMPPHandler | |
28 except ImportError: | |
29 from wokkel.subprotocols import XMPPHandler | |
30 | |
31 NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im' | |
32 NS_XHTML = 'http://www.w3.org/1999/xhtml' | |
33 | |
34 PLUGIN_INFO = { | |
35 "name": "XHTML-IM Plugin", | |
36 "import_name": "XEP-0071", | |
37 "type": "XEP", | |
38 "protocols": ["XEP-0071"], | |
39 "dependencies": ["TEXT-SYNTAXES"], | |
40 "main": "XEP_0071", | |
41 "handler": "yes", | |
42 "description": _("""Implementation of XHTML-IM""") | |
43 } | |
44 | |
45 allowed = { | |
46 "a": set(["href", "style", "type"]), | |
47 "blockquote": set(["style"]), | |
48 "body": set(["style"]), | |
49 "br": set([]), | |
50 "cite": set(["style"]), | |
51 "em": set([]), | |
52 "img": set(["alt", "height", "src", "style", "width"]), | |
53 "li": set(["style"]), | |
54 "ol": set(["style"]), | |
55 "p": set(["style"]), | |
56 "span": set(["style"]), | |
57 "strong": set([]), | |
58 "ul": set(["style"]), | |
59 } | |
60 | |
61 styles_allowed = ["background-color", "color", "font-family", "font-size", "font-style", "font-weight", "margin-left", "margin-right", "text-align", "text-decoration"] | |
62 | |
63 blacklist = ['script'] # tag that we have to kill (we don't keep content) | |
64 | |
65 | |
66 class XEP_0071(object): | |
67 SYNTAX_XHTML_IM = "XHTML-IM" | |
68 | |
69 def __init__(self, host): | |
70 info(_("XHTML-IM plugin initialization")) | |
71 self.host = host | |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
72 self.synt_plg = self.host.plugins["TEXT-SYNTAXES"] |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
73 self.synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self.synt_plg.OPT_HIDDEN]) |
668 | 74 host.trigger.add("MessageReceived", self.messageReceivedTrigger) |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
75 host.trigger.add("sendMessage", self.sendMessageTrigger) |
668 | 76 |
77 def getHandler(self, profile): | |
78 return XEP_0071_handler(self) | |
79 | |
80 def _messagePostTreat(self, data, body_elt): | |
81 """ Callback which manage the post treatment of the message in case of XHTML-IM found | |
82 @param data: data send by MessageReceived trigger through post_treat deferred | |
83 @param xhtml_im: XHTML-IM body element found | |
84 @return: the data with the extra parameter updated | |
85 """ | |
86 #TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message | |
87 def converted(xhtml): | |
88 data['extra']['xhtml'] = xhtml | |
89 return data | |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
90 d = self.synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) |
668 | 91 d.addCallback(converted) |
92 return d | |
93 | |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
94 def _sendMessageAddRich(self, mess_data, profile): |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
95 """ Construct XHTML-IM node and add it XML element |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
96 @param mess_data: message data as sended by sendMessage callback |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
97 """ |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
98 def syntax_converted(xhtml_im): |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
99 message_elt = mess_data['xml'] |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
100 html_elt = message_elt.addElement('html', NS_XHTML_IM) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
101 body_elt = html_elt.addElement('body', NS_XHTML) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
102 body_elt.addRawXml(xhtml_im) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
103 mess_data['extra']['xhtml'] = xhtml_im |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
104 return mess_data |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
105 |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
106 rich = mess_data['extra'].pop('rich') |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
107 syntax = self.synt_plg.getCurrentSyntax(profile) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
108 d = self.synt_plg.convert(rich, syntax, self.SYNTAX_XHTML_IM) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
109 d.addCallback(syntax_converted) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
110 return d |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
111 |
668 | 112 def messageReceivedTrigger(self, message, post_treat, profile): |
113 """ Check presence of XHTML-IM in message | |
114 """ | |
115 try: | |
116 html_elt = message.elements(NS_XHTML_IM, 'html').next() | |
117 body_elt = html_elt.elements(NS_XHTML, 'body').next() | |
118 # OK, we have found rich text | |
119 post_treat.addCallback(self._messagePostTreat, body_elt) | |
120 except StopIteration: | |
121 # No XHTML-IM | |
122 pass | |
123 return True | |
124 | |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
125 def sendMessageTrigger(self, mess_data, treatments, profile): |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
126 """ Check presence of rich text in extra |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
127 """ |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
128 try: |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
129 rich = mess_data['extra']['rich'] |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
130 # OK, we have found rich text |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
131 treatments.addCallback(self._sendMessageAddRich, profile) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
132 except KeyError: |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
133 # No rich text found |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
134 pass |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
135 return True |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
136 |
668 | 137 def _purgeStyle(self, styles_raw): |
138 """ Remove unauthorised styles according to the XEP-0071 | |
139 @param styles_raw: raw styles (value of the style attribute) | |
140 """ | |
141 purged = [] | |
142 | |
143 styles = [style.strip().split(':') for style in styles_raw.split(';')] | |
144 | |
145 for style_tuple in styles: | |
146 if len(style_tuple) != 2: | |
147 continue | |
148 name, value = style_tuple | |
149 name = name.strip() | |
150 if name not in styles_allowed: | |
151 continue | |
152 purged.append((name, value.strip())) | |
153 | |
154 return u'; '.join([u"%s: %s" % data for data in purged]) | |
155 | |
156 def XHTML2XHTML_IM(self, xhtml): | |
157 """ Convert XHTML document to XHTML_IM subset | |
158 @param xhtml: raw xhtml to convert | |
159 """ | |
160 # TODO: more clever tag replacement (replace forbidden tags with equivalents when possible) | |
161 | |
162 parser = html.HTMLParser(remove_comments=True, encoding='utf-8') | |
163 root = html.fromstring(xhtml, parser=parser) | |
164 body_elt = root.find('body') | |
165 if body_elt is None: | |
166 # we use the whole XML as body if no body element is found | |
167 body_elt = html.Element('body') | |
168 body_elt.append(root) | |
169 else: | |
170 body_elt.attrib.clear() | |
171 | |
172 allowed_tags = allowed.keys() | |
173 to_strip = [] | |
174 for elem in body_elt.iter(): | |
175 if elem.tag not in allowed_tags: | |
176 to_strip.append(elem) | |
177 else: | |
178 # we remove unallowed attributes | |
179 attrib = elem.attrib | |
180 att_to_remove = set(attrib).difference(allowed[elem.tag]) | |
181 for att in att_to_remove: | |
182 del(attrib[att]) | |
183 if "style" in attrib: | |
184 attrib["style"] = self._purgeStyle(attrib["style"]) | |
185 | |
186 for elem in to_strip: | |
187 if elem.tag in blacklist: | |
188 #we need to remove the element and all descendants | |
189 debug(u"removing black listed tag: %s" % (elem.tag)) | |
190 elem.drop_tree() | |
191 else: | |
192 elem.drop_tag() | |
701
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
193 if len(body_elt) !=1: |
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
194 root_elt = body_elt |
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
195 body_elt.tag = "p" |
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
196 else: |
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
197 root_elt = body_elt[0] |
668 | 198 |
701
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
199 return html.tostring(root_elt, encoding='unicode', method='xml') |
668 | 200 |
201 class XEP_0071_handler(XMPPHandler): | |
202 implements(iwokkel.IDisco) | |
203 | |
204 def __init__(self, plugin_parent): | |
205 self.plugin_parent = plugin_parent | |
206 self.host = plugin_parent.host | |
207 | |
208 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
209 return [disco.DiscoFeature(NS_XHTML_IM)] | |
210 | |
211 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
212 return [] |