annotate src/plugins/plugin_xep_0071.py @ 1760:30efe084471c

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