Philipp Hörist pushed to branch master at gajim / python-nbxmpp

Commits:

4 changed files:

Changes:

  • nbxmpp/modules/message.py
    ... ... @@ -19,10 +19,8 @@ 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
    
    23 22
     from nbxmpp.structs import StanzaHandler
    
    24 23
     from nbxmpp.structs import StanzaIDData
    
    25
    -from nbxmpp.structs import XHTMLData
    
    26 24
     from nbxmpp.util import error_factory
    
    27 25
     from nbxmpp.const import MessageType
    
    28 26
     
    
    ... ... @@ -81,17 +79,7 @@ class BaseMessage:
    81 79
             forms = stanza.getTags('x', namespace=NS_DATA)
    
    82 80
             if forms:
    
    83 81
                 properties.forms = forms
    
    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)
    
    82
    +        properties.xhtml = stanza.getXHTML()
    
    95 83
     
    
    96 84
         @staticmethod
    
    97 85
         def _parse_type(stanza):
    

  • nbxmpp/modules/misc.py
    ... ... @@ -22,7 +22,6 @@ 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
    
    26 25
     from nbxmpp.protocol import NodeProcessed
    
    27 26
     from nbxmpp.protocol import InvalidFrom
    
    28 27
     from nbxmpp.protocol import InvalidStanza
    
    ... ... @@ -119,16 +118,3 @@ def unwrap_mam(stanza, own_jid):
    119 118
                                 archive=stanza.getFrom(),
    
    120 119
                                 namespace=result.getNamespace(),
    
    121 120
                                 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

  • nbxmpp/protocol.py
    ... ... @@ -26,6 +26,7 @@ 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
    
    29 30
     
    
    30 31
     def ascii_upper(s):
    
    31 32
         return s.upper()
    
    ... ... @@ -1231,7 +1232,7 @@ class Message(Protocol):
    1231 1232
                     payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
    
    1232 1233
             if body:
    
    1233 1234
                 self.setBody(body)
    
    1234
    -        if xhtml is not None:
    
    1235
    +        if xhtml:
    
    1235 1236
                 self.setXHTML(xhtml)
    
    1236 1237
             if subject is not None:
    
    1237 1238
                 self.setSubject(subject)
    
    ... ... @@ -1242,8 +1243,22 @@ class Message(Protocol):
    1242 1243
             """
    
    1243 1244
             return self.getTagData('body')
    
    1244 1245
     
    
    1245
    -    def getXHTML(self):
    
    1246
    -        return self.getTag('html', namespace=NS_XHTML_IM)
    
    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
    
    1247 1262
     
    
    1248 1263
         def getSubject(self):
    
    1249 1264
             """
    
    ... ... @@ -1278,20 +1293,25 @@ class Message(Protocol):
    1278 1293
             Set the text of the message"""
    
    1279 1294
             self.setTagData('body', val)
    
    1280 1295
     
    
    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)
    
    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()
    
    1288 1305
                 else:
    
    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)
    
    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)
    
    1295 1315
     
    
    1296 1316
         def setSubject(self, val):
    
    1297 1317
             """
    

  • nbxmpp/structs.py
    ... ... @@ -26,7 +26,6 @@ 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
    
    30 29
     from nbxmpp.protocol import Protocol
    
    31 30
     from nbxmpp.const import MessageType
    
    32 31
     from nbxmpp.const import AvatarState
    
    ... ... @@ -832,26 +831,3 @@ class PresenceProperties:
    832 831
                 return self.muc_user.role
    
    833 832
             except Exception:
    
    834 833
                 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])