Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0071.py @ 3836:e183f1fbfa8d
doc (cli/pubsub): add doc for ``reference`` subcommand:
rel 369
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Jul 2022 16:15:09 +0200 |
parents | be6d91572633 |
children | 524856bd7b19 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
668 | 3 |
4 # SAT plugin for Publish-Subscribe (xep-0071) | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
668 | 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 | |
771 | 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 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
24 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
25 log = getLogger(__name__) |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
26 from sat.tools.common import data_format |
668 | 27 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
28 from twisted.internet import defer |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
29 from wokkel import disco, iwokkel |
3028 | 30 from zope.interface import implementer |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
31 |
668 | 32 # 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
|
33 try: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
34 from lxml import html |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
35 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 raise exceptions.MissingModule( |
3028 | 37 "Missing module lxml, please download/install it from http://lxml.de/" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 ) |
668 | 39 try: |
40 from twisted.words.protocols.xmlstream import XMPPHandler | |
41 except ImportError: | |
42 from wokkel.subprotocols import XMPPHandler | |
43 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 NS_XHTML_IM = "http://jabber.org/protocol/xhtml-im" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 NS_XHTML = "http://www.w3.org/1999/xhtml" |
668 | 46 |
47 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
48 C.PI_NAME: "XHTML-IM Plugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
49 C.PI_IMPORT_NAME: "XEP-0071", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
50 C.PI_TYPE: "XEP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
51 C.PI_PROTOCOLS: ["XEP-0071"], |
2780
85d3240a400f
plugin text syntaxes: changed import name to TEXT_SYNTAX (better with underscore for autocompletion)
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
52 C.PI_DEPENDENCIES: ["TEXT_SYNTAXES"], |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
53 C.PI_MAIN: "XEP_0071", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
54 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 C.PI_DESCRIPTION: _("""Implementation of XHTML-IM"""), |
668 | 56 } |
57 | |
58 allowed = { | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 "a": set(["href", "style", "type"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 "blockquote": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 "body": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 "br": set([]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 "cite": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 "em": set([]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 "img": set(["alt", "height", "src", "style", "width"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 "li": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 "ol": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 "p": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 "span": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 "strong": set([]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 "ul": set(["style"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 } |
668 | 73 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 styles_allowed = [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 "background-color", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 "color", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 "font-family", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 "font-size", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 "font-style", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 "font-weight", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 "margin-left", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 "margin-right", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 "text-align", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 "text-decoration", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 ] |
668 | 86 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 blacklist = ["script"] # tag that we have to kill (we don't keep content) |
668 | 88 |
89 | |
90 class XEP_0071(object): | |
91 SYNTAX_XHTML_IM = "XHTML-IM" | |
92 | |
93 def __init__(self, host): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
922
diff
changeset
|
94 log.info(_("XHTML-IM plugin initialization")) |
668 | 95 self.host = host |
2780
85d3240a400f
plugin text syntaxes: changed import name to TEXT_SYNTAX (better with underscore for autocompletion)
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
96 self._s = self.host.plugins["TEXT_SYNTAXES"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 self._s.addSyntax( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 self.SYNTAX_XHTML_IM, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 lambda xhtml: xhtml, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 self.XHTML2XHTML_IM, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 [self._s.OPT_HIDDEN], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 ) |
3172
dcebc585c29f
core: renamed "MessageReceived" trigger to "messageReceived" for consistency.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
103 host.trigger.add("messageReceived", self.messageReceivedTrigger) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
104 host.trigger.add("sendMessage", self.sendMessageTrigger) |
668 | 105 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
106 def getHandler(self, client): |
668 | 107 return XEP_0071_handler(self) |
108 | |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
109 def _messagePostTreat(self, data, message_elt, body_elts, client): |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
110 """Callback which manage the post treatment of the message in case of XHTML-IM found |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
111 |
3172
dcebc585c29f
core: renamed "MessageReceived" trigger to "messageReceived" for consistency.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
112 @param data: data send by messageReceived trigger through post_treat deferred |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
113 @param message_elt: whole <message> stanza |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
114 @param body_elts: XHTML-IM body elements found |
668 | 115 @return: the data with the extra parameter updated |
116 """ | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
117 # 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
|
118 def converted(xhtml, lang): |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
119 if lang: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 data["extra"]["xhtml_{}".format(lang)] = xhtml |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
121 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 data["extra"]["xhtml"] = xhtml |
668 | 123 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
124 defers = [] |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
125 for body_elt in body_elts: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 lang = body_elt.getAttribute((C.NS_XML, "lang"), "") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 treat_d = defer.succeed(None) # deferred used for treatments |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 if self.host.trigger.point( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 "xhtml_post_treat", client, message_elt, body_elt, lang, treat_d |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 ): |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
131 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 treat_d.addCallback( |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
133 lambda __: self._s.convert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
134 body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
135 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 ) |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
137 treat_d.addCallback(converted, lang) |
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
138 defers.append(treat_d) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
139 |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
140 d_list = defer.DeferredList(defers) |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
141 d_list.addCallback(lambda __: data) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
142 return d_list |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
143 |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
144 def _fill_body_text(self, text, data, lang): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 data["message"][lang or ""] = text |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 message_elt = data["xml"] |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
147 body_elt = message_elt.addElement("body", content=text) |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
148 if lang: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 body_elt[(C.NS_XML, "lang")] = lang |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
150 |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
151 def _check_body_text(self, data, lang, markup, syntax, defers): |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
152 """check if simple text message exists, and fill if needed""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
153 if not (lang or "") in data["message"]: |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
154 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT) |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
155 d.addCallback(self._fill_body_text, data, lang) |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
156 defers.append(d) |
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
157 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
158 def _sendMessageAddRich(self, data, client): |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
159 """ 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
|
160 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
161 @param data: message data as sended by sendMessage callback |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
162 """ |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
163 # 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
|
164 # but both can't exist at the same time |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 message_elt = data["xml"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 html_elt = message_elt.addElement((NS_XHTML_IM, "html")) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
167 |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
168 def syntax_converted(xhtml_im, lang): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 body_elt = html_elt.addElement((NS_XHTML, "body")) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
170 if lang: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 body_elt[(C.NS_XML, "lang")] = lang |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 data["extra"]["xhtml_{}".format(lang)] = xhtml_im |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
173 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 data["extra"]["xhtml"] = xhtml_im |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
175 body_elt.addRawXml(xhtml_im) |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
176 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
177 syntax = self._s.getCurrentSyntax(client.profile) |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
178 defers = [] |
3028 | 179 if "xhtml" in data["extra"]: |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
180 # we have directly XHTML |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 for lang, xhtml in data_format.getSubDict("xhtml", data["extra"]): |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
182 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
183 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
|
184 d.addCallback(syntax_converted, lang) |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
185 defers.append(d) |
3028 | 186 elif "rich" in data["extra"]: |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
187 # we have rich syntax to convert |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
188 for lang, rich_data in data_format.getSubDict("rich", data["extra"]): |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
189 self._check_body_text(data, lang, rich_data, syntax, defers) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
190 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
|
191 d.addCallback(syntax_converted, lang) |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
192 defers.append(d) |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
193 else: |
3028 | 194 exceptions.InternalError("xhtml or rich should be present at this point") |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
195 d_list = defer.DeferredList(defers) |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
196 d_list.addCallback(lambda __: data) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
197 return d_list |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
198 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
199 def messageReceivedTrigger(self, client, message, post_treat): |
668 | 200 """ Check presence of XHTML-IM in message |
201 """ | |
202 try: | |
3028 | 203 html_elt = next(message.elements(NS_XHTML_IM, "html")) |
668 | 204 except StopIteration: |
205 # No XHTML-IM | |
206 pass | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
207 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 body_elts = html_elt.elements(NS_XHTML, "body") |
2110
2d633b3c923d
plugin XEP-0231: Bits of Binary first draft:
Goffi <goffi@goffi.org>
parents:
2080
diff
changeset
|
209 post_treat.addCallback(self._messagePostTreat, message, body_elts, client) |
668 | 210 return True |
211 | |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
212 def sendMessageTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): |
2080
3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
213 """ Check presence of rich text in extra """ |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
214 rich = {} |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
215 xhtml = {} |
3028 | 216 for key, value in data["extra"].items(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
217 if key.startswith("rich"): |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
218 rich[key[5:]] = value |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
219 elif key.startswith("xhtml"): |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
220 xhtml[key[6:]] = value |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
221 if rich and xhtml: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
222 raise exceptions.DataError( |
3028 | 223 _("Can't have XHTML and rich content at the same time") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 ) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
225 if rich or xhtml: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
226 if rich: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 data["rich"] = rich |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
228 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
229 data["xhtml"] = xhtml |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2110
diff
changeset
|
230 post_xml_treatments.addCallback(self._sendMessageAddRich, client) |
702
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
231 return True |
a25db3fe3959
plugin XEP-0071: rich messages management for sendMessage
Goffi <goffi@goffi.org>
parents:
701
diff
changeset
|
232 |
668 | 233 def _purgeStyle(self, styles_raw): |
234 """ Remove unauthorised styles according to the XEP-0071 | |
235 @param styles_raw: raw styles (value of the style attribute) | |
236 """ | |
237 purged = [] | |
238 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
239 styles = [style.strip().split(":") for style in styles_raw.split(";")] |
668 | 240 |
241 for style_tuple in styles: | |
242 if len(style_tuple) != 2: | |
243 continue | |
244 name, value = style_tuple | |
245 name = name.strip() | |
246 if name not in styles_allowed: | |
247 continue | |
248 purged.append((name, value.strip())) | |
249 | |
3028 | 250 return "; ".join(["%s: %s" % data for data in purged]) |
668 | 251 |
252 def XHTML2XHTML_IM(self, xhtml): | |
253 """ Convert XHTML document to XHTML_IM subset | |
254 @param xhtml: raw xhtml to convert | |
255 """ | |
256 # TODO: more clever tag replacement (replace forbidden tags with equivalents when possible) | |
257 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 parser = html.HTMLParser(remove_comments=True, encoding="utf-8") |
668 | 259 root = html.fromstring(xhtml, parser=parser) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
260 body_elt = root.find("body") |
668 | 261 if body_elt is None: |
262 # we use the whole XML as body if no body element is found | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
263 body_elt = html.Element("body") |
668 | 264 body_elt.append(root) |
265 else: | |
266 body_elt.attrib.clear() | |
267 | |
3028 | 268 allowed_tags = list(allowed.keys()) |
668 | 269 to_strip = [] |
270 for elem in body_elt.iter(): | |
271 if elem.tag not in allowed_tags: | |
272 to_strip.append(elem) | |
273 else: | |
274 # we remove unallowed attributes | |
275 attrib = elem.attrib | |
276 att_to_remove = set(attrib).difference(allowed[elem.tag]) | |
277 for att in att_to_remove: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 del (attrib[att]) |
668 | 279 if "style" in attrib: |
280 attrib["style"] = self._purgeStyle(attrib["style"]) | |
281 | |
282 for elem in to_strip: | |
283 if elem.tag in blacklist: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 # we need to remove the element and all descendants |
3028 | 285 log.debug("removing black listed tag: %s" % (elem.tag)) |
668 | 286 elem.drop_tree() |
287 else: | |
288 elem.drop_tag() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 if len(body_elt) != 1: |
701
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
290 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
|
291 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
|
292 else: |
98b2400e17d6
plugin XEP-0071: XHTML2XHTML_IM don't return the <body> root tag anymore.
Goffi <goffi@goffi.org>
parents:
668
diff
changeset
|
293 root_elt = body_elt[0] |
668 | 294 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
295 return html.tostring(root_elt, encoding="unicode", method="xml") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
296 |
668 | 297 |
3028 | 298 @implementer(iwokkel.IDisco) |
668 | 299 class XEP_0071_handler(XMPPHandler): |
300 | |
301 def __init__(self, plugin_parent): | |
302 self.plugin_parent = plugin_parent | |
303 self.host = plugin_parent.host | |
304 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
305 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
668 | 306 return [disco.DiscoFeature(NS_XHTML_IM)] |
307 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
308 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
668 | 309 return [] |