annotate src/plugins/plugin_xep_0071.py @ 1988:3f0d22565684

primitivus (chat): fixed a crash when page-up was pressed on room contacts list
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:12 +0200
parents a2bc5089c2eb
children 3626b2813158
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1813
diff changeset
1 #!/usr/bin/env python2
668
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)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1750
diff changeset
5 # Copyright (C) 2009-2016 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 _
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
21 from sat.core.constants import Const as C
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
22 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
23 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
24 log = getLogger(__name__)
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
26 from twisted.internet import defer
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
27 from wokkel import disco, iwokkel
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from zope.interface import implements
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 # 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
30 try:
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
31 from lxml import html
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
32 except ImportError:
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
33 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
34 try:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from twisted.words.protocols.xmlstream import XMPPHandler
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except ImportError:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from wokkel.subprotocols import XMPPHandler
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im'
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 NS_XHTML = 'http://www.w3.org/1999/xhtml'
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "name": "XHTML-IM Plugin",
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "import_name": "XEP-0071",
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "type": "XEP",
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "protocols": ["XEP-0071"],
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "dependencies": ["TEXT-SYNTAXES"],
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "main": "XEP_0071",
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "handler": "yes",
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "description": _("""Implementation of XHTML-IM""")
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
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 allowed = {
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "a": set(["href", "style", "type"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "blockquote": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "body": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "br": set([]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "cite": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "em": set([]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 "img": set(["alt", "height", "src", "style", "width"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "li": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 "ol": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 "p": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 "span": set(["style"]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 "strong": set([]),
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "ul": set(["style"]),
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
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 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
70
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 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
72
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 class XEP_0071(object):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 SYNTAX_XHTML_IM = "XHTML-IM"
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
78 log.info(_("XHTML-IM plugin initialization"))
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.host = host
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
80 self._s = self.host.plugins["TEXT-SYNTAXES"]
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
81 self._s.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self._s.OPT_HIDDEN])
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
83 host.trigger.add("messageSend", self.messageSendTrigger)
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def getHandler(self, profile):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return XEP_0071_handler(self)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
88 def _messagePostTreat(self, data, body_elts):
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
89 """Callback which manage the post treatment of the message in case of XHTML-IM found
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @param data: data send by MessageReceived trigger through post_treat deferred
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
91 @param body_elts: XHTML-IM body elements found
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @return: the data with the extra parameter updated
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
94 # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
95 def converted(xhtml, lang):
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
96 if lang:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
97 data['extra']['xhtml_{}'.format(lang)] = xhtml
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
98 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
99 data['extra']['xhtml'] = xhtml
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
101 defers = []
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
102 for body_elt in body_elts:
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
103 lang = body_elt.getAttribute((C.NS_XML, 'lang'), '')
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
104 d = self._s.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
105 d.addCallback(converted, lang)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
106 defers.append(d)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
107
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
108 d_list = defer.DeferredList(defers)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
109 d_list.addCallback(lambda dummy: data)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
110 return d_list
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
111
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
112 def _messageSendAddRich(self, data, client):
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
113 """ Construct XHTML-IM node and add it XML element
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
114
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
115 @param data: message data as sended by messageSend callback
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
116 """
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
117 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
118 # but both can't exist at the same time
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
119 message_elt = data['xml']
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
120 html_elt = message_elt.addElement((NS_XHTML_IM, 'html'))
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
121
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
122 def syntax_converted(xhtml_im, lang):
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
123 body_elt = html_elt.addElement((NS_XHTML, 'body'))
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
124 if lang:
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
125 body_elt[(C.NS_XML, 'lang')] = lang
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
126 data['extra']['xhtml_{}'.format(lang)] = xhtml_im
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
127 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
128 data['extra']['xhtml'] = xhtml_im
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
129 body_elt.addRawXml(xhtml_im)
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
130
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
131 syntax = self._s.getCurrentSyntax(client.profile)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
132 defers = []
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
133 try:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
134 rich = data['extra']['rich']
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
135 except KeyError:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
136 # we have directly XHTML
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
137 for lang, xhtml in data['extra']['xhtml'].iteritems():
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
138 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
139 d.addCallback(syntax_converted, lang)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
140 defers.append(d)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
141 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
142 # we have rich syntax to convert
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
143 for lang, rich_data in rich.iteritems():
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
144 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
145 d.addCallback(syntax_converted, lang)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
146 defers.append(d)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
147 d_list = defer.DeferredList(defers)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
148 d_list.addCallback(lambda dummy: data)
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
149 return d_list
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
150
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
151 def messageReceivedTrigger(self, client, message, post_treat):
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """ Check presence of XHTML-IM in message
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 try:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 html_elt = message.elements(NS_XHTML_IM, 'html').next()
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 except StopIteration:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 # No XHTML-IM
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 pass
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
159 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
160 body_elts = html_elt.elements(NS_XHTML, 'body')
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
161 post_treat.addCallback(self._messagePostTreat, body_elts)
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 return True
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
164 def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
165 """ Check presence of rich text in extra
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
166 """
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
167 rich = {}
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
168 xhtml = {}
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
169 for key, value in data['extra'].iteritems():
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
170 if key.startswith('rich'):
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
171 rich[key[5:]] = value
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
172 elif key.startswith('xhtml'):
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
173 xhtml[key[6:]] = value
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
174 if rich and xhtml:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
175 raise exceptions.DataError(_(u"Can't have XHTML and rich content at the same time"))
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
176 if rich or xhtml:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
177 if rich:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
178 data['rich'] = rich
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
179 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
180 data['xhtml'] = xhtml
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
181 post_xml_treatments.addCallback(self._messageSendAddRich, client)
702
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
182 return True
a25db3fe3959 plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents: 701
diff changeset
183
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 def _purgeStyle(self, styles_raw):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 """ Remove unauthorised styles according to the XEP-0071
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 @param styles_raw: raw styles (value of the style attribute)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 purged = []
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 styles = [style.strip().split(':') for style in styles_raw.split(';')]
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 for style_tuple in styles:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if len(style_tuple) != 2:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 continue
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 name, value = style_tuple
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 name = name.strip()
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 if name not in styles_allowed:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 continue
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 purged.append((name, value.strip()))
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 return u'; '.join([u"%s: %s" % data for data in purged])
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 def XHTML2XHTML_IM(self, xhtml):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 """ Convert XHTML document to XHTML_IM subset
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 @param xhtml: raw xhtml to convert
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 """
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
207 # 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
208
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 parser = html.HTMLParser(remove_comments=True, encoding='utf-8')
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 root = html.fromstring(xhtml, parser=parser)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 body_elt = root.find('body')
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 if body_elt is None:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213 # 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
214 body_elt = html.Element('body')
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 body_elt.append(root)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 else:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 body_elt.attrib.clear()
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 allowed_tags = allowed.keys()
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 to_strip = []
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 for elem in body_elt.iter():
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 if elem.tag not in allowed_tags:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 to_strip.append(elem)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 else:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 # we remove unallowed attributes
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 attrib = elem.attrib
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 att_to_remove = set(attrib).difference(allowed[elem.tag])
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 for att in att_to_remove:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 del(attrib[att])
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 if "style" in attrib:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231 attrib["style"] = self._purgeStyle(attrib["style"])
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 for elem in to_strip:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 if elem.tag in blacklist:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 #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
236 log.debug(u"removing black listed tag: %s" % (elem.tag))
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 elem.drop_tree()
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 else:
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 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
240 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
241 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
242 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
243 else:
98b2400e17d6 plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents: 668
diff changeset
244 root_elt = body_elt[0]
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245
701
98b2400e17d6 plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents: 668
diff changeset
246 return html.tostring(root_elt, encoding='unicode', method='xml')
668
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248 class XEP_0071_handler(XMPPHandler):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 implements(iwokkel.IDisco)
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 def __init__(self, plugin_parent):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 self.plugin_parent = plugin_parent
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253 self.host = plugin_parent.host
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
256 return [disco.DiscoFeature(NS_XHTML_IM)]
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
257
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
7bb50096d225 plugin XEP_0071: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259 return []