Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits:
-
05e74605
by Philipp Hörist at 2019-11-03T20:59:27Z
4 changed files:
Changes:
| ... | ... | @@ -19,8 +19,10 @@ import logging |
| 19 | 19 |
|
| 20 | 20 |
from nbxmpp.protocol import NodeProcessed
|
| 21 | 21 |
from nbxmpp.protocol import NS_DATA
|
| 22 |
+from nbxmpp.protocol import NS_XHTML
|
|
| 22 | 23 |
from nbxmpp.structs import StanzaHandler
|
| 23 | 24 |
from nbxmpp.structs import StanzaIDData
|
| 25 |
+from nbxmpp.structs import XHTMLData
|
|
| 24 | 26 |
from nbxmpp.util import error_factory
|
| 25 | 27 |
from nbxmpp.const import MessageType
|
| 26 | 28 |
|
| ... | ... | @@ -79,7 +81,17 @@ class BaseMessage: |
| 79 | 81 |
forms = stanza.getTags('x', namespace=NS_DATA)
|
| 80 | 82 |
if forms:
|
| 81 | 83 |
properties.forms = forms
|
| 82 |
- properties.xhtml = stanza.getXHTML()
|
|
| 84 |
+ |
|
| 85 |
+ xhtml = stanza.getXHTML()
|
|
| 86 |
+ if xhtml is None:
|
|
| 87 |
+ return
|
|
| 88 |
+ |
|
| 89 |
+ if xhtml.getTag('body', namespace=NS_XHTML) is None:
|
|
| 90 |
+ log.warning('xhtml without body found')
|
|
| 91 |
+ log.warning(stanza)
|
|
| 92 |
+ return
|
|
| 93 |
+ |
|
| 94 |
+ properties.xhtml = XHTMLData(xhtml)
|
|
| 83 | 95 |
|
| 84 | 96 |
@staticmethod
|
| 85 | 97 |
def _parse_type(stanza):
|
| ... | ... | @@ -22,6 +22,7 @@ from nbxmpp.protocol import NS_FORWARD |
| 22 | 22 |
from nbxmpp.protocol import NS_MUC_USER
|
| 23 | 23 |
from nbxmpp.protocol import NS_MAM_1
|
| 24 | 24 |
from nbxmpp.protocol import NS_MAM_2
|
| 25 |
+from nbxmpp.protocol import NS_XHTML
|
|
| 25 | 26 |
from nbxmpp.protocol import NodeProcessed
|
| 26 | 27 |
from nbxmpp.protocol import InvalidFrom
|
| 27 | 28 |
from nbxmpp.protocol import InvalidStanza
|
| ... | ... | @@ -118,3 +119,16 @@ def unwrap_mam(stanza, own_jid): |
| 118 | 119 |
archive=stanza.getFrom(),
|
| 119 | 120 |
namespace=result.getNamespace(),
|
| 120 | 121 |
timestamp=delay_timestamp)
|
| 122 |
+ |
|
| 123 |
+ |
|
| 124 |
+def build_xhtml_body(xhtml, xmllang=None):
|
|
| 125 |
+ try:
|
|
| 126 |
+ if xmllang is not None:
|
|
| 127 |
+ body = '<body xmlns="%s" xml:lang="%s">%s</body>' % (
|
|
| 128 |
+ NS_XHTML, xmllang, xhtml)
|
|
| 129 |
+ else:
|
|
| 130 |
+ body = '<body xmlns="%s">%s</body>' % (NS_XHTML, xhtml)
|
|
| 131 |
+ except Exception as error:
|
|
| 132 |
+ log.error('Error while building xhtml node: %s', error)
|
|
| 133 |
+ return None
|
|
| 134 |
+ return body
|
| ... | ... | @@ -26,7 +26,6 @@ from base64 import b64encode |
| 26 | 26 |
from precis_i18n import get_profile
|
| 27 | 27 |
from nbxmpp.stringprepare import nameprep
|
| 28 | 28 |
from nbxmpp.simplexml import Node
|
| 29 |
-from nbxmpp.simplexml import NodeBuilder
|
|
| 30 | 29 |
|
| 31 | 30 |
def ascii_upper(s):
|
| 32 | 31 |
return s.upper()
|
| ... | ... | @@ -1232,7 +1231,7 @@ class Message(Protocol): |
| 1232 | 1231 |
payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
|
| 1233 | 1232 |
if body:
|
| 1234 | 1233 |
self.setBody(body)
|
| 1235 |
- if xhtml:
|
|
| 1234 |
+ if xhtml is not None:
|
|
| 1236 | 1235 |
self.setXHTML(xhtml)
|
| 1237 | 1236 |
if subject is not None:
|
| 1238 | 1237 |
self.setSubject(subject)
|
| ... | ... | @@ -1243,22 +1242,8 @@ class Message(Protocol): |
| 1243 | 1242 |
"""
|
| 1244 | 1243 |
return self.getTagData('body')
|
| 1245 | 1244 |
|
| 1246 |
- def getXHTML(self, xmllang=None):
|
|
| 1247 |
- """
|
|
| 1248 |
- Return serialized xhtml-im element text of the message
|
|
| 1249 |
- |
|
| 1250 |
- TODO: Returning a DOM could make rendering faster.
|
|
| 1251 |
- """
|
|
| 1252 |
- xhtml = self.getTag('html', namespace=NS_XHTML_IM)
|
|
| 1253 |
- if xhtml:
|
|
| 1254 |
- if xmllang:
|
|
| 1255 |
- body = xhtml.getTag('body',
|
|
| 1256 |
- namespace=NS_XHTML,
|
|
| 1257 |
- attrs={'xml:lang': xmllang})
|
|
| 1258 |
- else:
|
|
| 1259 |
- body = xhtml.getTag('body', namespace=NS_XHTML)
|
|
| 1260 |
- return str(body)
|
|
| 1261 |
- return None
|
|
| 1245 |
+ def getXHTML(self):
|
|
| 1246 |
+ return self.getTag('html', namespace=NS_XHTML_IM)
|
|
| 1262 | 1247 |
|
| 1263 | 1248 |
def getSubject(self):
|
| 1264 | 1249 |
"""
|
| ... | ... | @@ -1293,25 +1278,20 @@ class Message(Protocol): |
| 1293 | 1278 |
Set the text of the message"""
|
| 1294 | 1279 |
self.setTagData('body', val)
|
| 1295 | 1280 |
|
| 1296 |
- def setXHTML(self, val, xmllang=None):
|
|
| 1297 |
- """
|
|
| 1298 |
- Sets the xhtml text of the message (XEP-0071). The parameter is the
|
|
| 1299 |
- "inner html" to the body.
|
|
| 1300 |
- """
|
|
| 1301 |
- try:
|
|
| 1302 |
- if xmllang:
|
|
| 1303 |
- dom = NodeBuilder('<body xmlns="%s" xml:lang="%s">%s</body>' \
|
|
| 1304 |
- % (NS_XHTML, xmllang, val)).getDom()
|
|
| 1281 |
+ def setXHTML(self, body, add=False):
|
|
| 1282 |
+ if isinstance(body, str):
|
|
| 1283 |
+ body = Node(node=body)
|
|
| 1284 |
+ if add:
|
|
| 1285 |
+ xhtml = self.getTag('html', namespace=NS_XHTML_IM)
|
|
| 1286 |
+ if xhtml is not None:
|
|
| 1287 |
+ xhtml.addChild(node=body)
|
|
| 1305 | 1288 |
else:
|
| 1306 |
- dom = NodeBuilder('<body xmlns="%s">%s</body>' % (NS_XHTML,
|
|
| 1307 |
- val), 0).getDom()
|
|
| 1308 |
- if self.getTag('html'):
|
|
| 1309 |
- self.getTag('html').addChild(node=dom)
|
|
| 1310 |
- else:
|
|
| 1311 |
- self.setTag('html', namespace=NS_XHTML_IM).addChild(node=dom)
|
|
| 1312 |
- except Exception as e:
|
|
| 1313 |
- print("Error" + str(e))
|
|
| 1314 |
- # FIXME: log. we could not set xhtml (parse error, whatever)
|
|
| 1289 |
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
|
|
| 1290 |
+ else:
|
|
| 1291 |
+ xhtml_nodes = self.getTags('html', namespace=NS_XHTML_IM)
|
|
| 1292 |
+ for xhtml in xhtml_nodes:
|
|
| 1293 |
+ self.delChild(xhtml)
|
|
| 1294 |
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
|
|
| 1315 | 1295 |
|
| 1316 | 1296 |
def setSubject(self, val):
|
| 1317 | 1297 |
"""
|
| ... | ... | @@ -26,6 +26,7 @@ from nbxmpp.protocol import NS_MAM_2 |
| 26 | 26 |
from nbxmpp.protocol import NS_MUC
|
| 27 | 27 |
from nbxmpp.protocol import NS_MUC_INFO
|
| 28 | 28 |
from nbxmpp.protocol import NS_CLIENT
|
| 29 |
+from nbxmpp.protocol import NS_XHTML
|
|
| 29 | 30 |
from nbxmpp.protocol import Protocol
|
| 30 | 31 |
from nbxmpp.const import MessageType
|
| 31 | 32 |
from nbxmpp.const import AvatarState
|
| ... | ... | @@ -831,3 +832,26 @@ class PresenceProperties: |
| 831 | 832 |
return self.muc_user.role
|
| 832 | 833 |
except Exception:
|
| 833 | 834 |
return None
|
| 835 |
+ |
|
| 836 |
+ |
|
| 837 |
+class XHTMLData:
|
|
| 838 |
+ def __init__(self, xhtml):
|
|
| 839 |
+ self._bodys = {}
|
|
| 840 |
+ for body in xhtml.getTags('body', namespace=NS_XHTML):
|
|
| 841 |
+ lang = body.getXmlLang()
|
|
| 842 |
+ self._bodys[lang] = body
|
|
| 843 |
+ |
|
| 844 |
+ def get_body(self, pref_lang=None):
|
|
| 845 |
+ if pref_lang is not None:
|
|
| 846 |
+ body = self._bodys.get(pref_lang)
|
|
| 847 |
+ if body is not None:
|
|
| 848 |
+ return str(body)
|
|
| 849 |
+ |
|
| 850 |
+ body = self._bodys.get('en')
|
|
| 851 |
+ if body is not None:
|
|
| 852 |
+ return str(body)
|
|
| 853 |
+ |
|
| 854 |
+ body = self._bodys.get(None)
|
|
| 855 |
+ if body is not None:
|
|
| 856 |
+ return str(body)
|
|
| 857 |
+ return str(self._bodys.popitem()[1])
|