summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/stanzabase.py
blob: 22853a9d05a11afc6b38dd729fde23e56903f201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  Nathanael C. Fritz
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import copy
import logging
import sys
import weakref
from xml.etree import cElementTree as ET

from sleekxmpp.xmlstream import JID
from sleekxmpp.xmlstream.tostring import tostring


# Used to check if an argument is an XML object.
XML_TYPE = type(ET.Element('xml'))


def registerStanzaPlugin(stanza, plugin):
    """
    Associate a stanza object as a plugin for another stanza.

    Arguments:
        stanza -- The class of the parent stanza.
        plugin -- The class of the plugin stanza.
    """
    tag = "{%s}%s" % (plugin.namespace, plugin.name)
    stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
    stanza.plugin_tag_map[tag] = plugin


class ElementBase(object):
    name = 'stanza'
    plugin_attrib = 'plugin'
    namespace = 'jabber:client'
    interfaces = set(('type', 'to', 'from', 'id', 'payload'))
    types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
    sub_interfaces = tuple()
    plugin_attrib_map = {}
    plugin_tag_map = {}
    subitem = None

    def __init__(self, xml=None, parent=None):
        """
        Create a new stanza object.

        Arguments:
            xml    -- Initialize the stanza with optional existing XML.
            parent -- Optional stanza object that contains this stanza.
        """
        self.xml = xml
        self.plugins = {}
        self.iterables = []
        self.idx = 0
        if parent is None:
            self.parent = None
        else:
            self.parent = weakref.ref(parent)

        if self.setup(xml):
            # If we generated our own XML, then everything is ready.
            return

        # Initialize values using provided XML
        for child in self.xml.getchildren():
            if child.tag in self.plugin_tag_map:
                plugin = self.plugin_tag_map[child.tag]
                self.plugins[plugin.plugin_attrib] = plugin(child, self)
            if self.subitem is not None:
                for sub in self.subitem:
                    if child.tag == "{%s}%s" % (sub.namespace, sub.name):
                        self.iterables.append(sub(child, self))
                        break

    def setup(self, xml=None):
        """
        Initialize the stanza's XML contents.

        Will return True if XML was generated according to the stanza's
        definition.

        Arguments:
            xml -- Optional XML object to use for the stanza's content
                   instead of generating XML.
        """
        if self.xml is None:
            self.xml = xml

        if self.xml is None:
            # Generate XML from the stanza definition
            for ename in self.name.split('/'):
                new = ET.Element("{%s}%s" % (self.namespace, ename))
                if self.xml is None:
                    self.xml = new
                else:
                    last_xml.append(new)
                last_xml = new
            if self.parent is not None:
                self.parent().xml.append(self.xml)

            # We had to generate XML
            return True
        else:
            # We did not generate XML
            return False

    def enable(self, attrib):
        """
        Enable and initialize a stanza plugin.

        Alias for initPlugin.

        Arguments:
            attrib -- The stanza interface for the plugin.
        """
        return self.initPlugin(attrib)

    def initPlugin(self, attrib):
        """
        Enable and initialize a stanza plugin.

        Arguments:
            attrib -- The stanza interface for the plugin.
        """
        if attrib not in self.plugins:
            plugin_class = self.plugin_attrib_map[attrib]
            self.plugins[attrib] = plugin_class(parent=self)
        return self

    def getStanzaValues(self):
        """
        Return a dictionary of the stanza's interface values.

        Stanza plugin values are included as nested dictionaries.
        """
        values = {}
        for interface in self.interfaces:
            values[interface] = self[interface]
        for plugin, stanza in self.plugins.items():
            values[plugin] = stanza.getStanzaValues()
        if self.iterables:
            iterables = []
            for stanza in self.iterables:
                iterables.append(stanza.getStanzaValues())
                iterables[-1].update({
                    '__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)
                    })
            values['substanzas'] = iterables
        return values

    def setStanzaValues(self, values):
        """
        Set multiple stanza interface values using a dictionary.

        Stanza plugin values may be set using nested dictionaries.

        Arguments:
            values -- A dictionary mapping stanza interface with values.
                      Plugin interfaces may accept a nested dictionary that
                      will be used recursively.
        """
        for interface, value in values.items():
            if interface == 'substanzas':
                for subdict in value:
                    if '__childtag__' in subdict:
                        for subclass in self.subitem:
                            child_tag = "{%s}%s" % (subclass.namespace,
                                                    subclass.name)
                            if subdict['__childtag__'] == child_tag:
                                sub = subclass(parent=self)
                                sub.setStanzaValues(subdict)
                                self.iterables.append(sub)
                                break
            elif interface in self.interfaces:
                self[interface] = value
            elif interface in self.plugin_attrib_map:
                if interface not in self.plugins:
                    self.initPlugin(interface)
                self.plugins[interface].setStanzaValues(value)
        return self

    def __getitem__(self, attrib):
        """
        Return the value of a stanza interface using dictionary-like syntax.

        Example:
            >>> msg['body']
            'Message contents'

        Stanza interfaces are typically mapped directly to the underlying XML
        object, but can be overridden by the presence of a getAttrib method
        (or getFoo where the interface is named foo, etc).

        The search order for interface value retrieval for an interface
        named 'foo' is:
            1. The list of substanzas.
            2. The result of calling getFoo.
            3. The contents of the foo subelement, if foo is a sub interface.
            4. The value of the foo attribute of the XML object.
            5. The plugin named 'foo'
            6. An empty string.

        Arguments:
            attrib -- The name of the requested stanza interface.
        """
        if attrib == 'substanzas':
            return self.iterables
        elif attrib in self.interfaces:
            get_method = "get%s" % attrib.title()
            if hasattr(self, get_method):
                return getattr(self, get_method)()
            else:
                if attrib in self.sub_interfaces:
                    return self._getSubText(attrib)
                else:
                    return self._getAttr(attrib)
        elif attrib in self.plugin_attrib_map:
            if attrib not in self.plugins:
                self.initPlugin(attrib)
            return self.plugins[attrib]
        else:
            return ''

    def __setitem__(self, attrib, value):
        """
        Set the value of a stanza interface using dictionary-like syntax.

        Example:
            >>> msg['body'] = "Hi!"
            >>> msg['body']
            'Hi!'

        Stanza interfaces are typically mapped directly to the underlying XML
        object, but can be overridden by the presence of a setAttrib method
        (or setFoo where the interface is named foo, etc).

        The effect of interface value assignment for an interface
        named 'foo' will be one of:
            1. Delete the interface's contents if the value is None.
            2. Call setFoo, if it exists.
            3. Set the text of a foo element, if foo is in sub_interfaces.
            4. Set the value of a top level XML attribute name foo.
            5. Attempt to pass value to a plugin named foo using the plugin's
               foo interface.
            6. Do nothing.

        Arguments:
            attrib -- The name of the stanza interface to modify.
            value  -- The new value of the stanza interface.
        """
        if attrib in self.interfaces:
            if value is not None:
                set_method = "set%s" % attrib.title()
                if hasattr(self, set_method):
                    getattr(self, set_method)(value,)
                else:
                    if attrib in self.sub_interfaces:
                        return self._setSubText(attrib, text=value)
                    else:
                        self._setAttr(attrib, value)
            else:
                self.__delitem__(attrib)
        elif attrib in self.plugin_attrib_map:
            if attrib not in self.plugins:
                self.initPlugin(attrib)
            self.plugins[attrib][attrib] = value
        return self

    def __delitem__(self, attrib):
        """
        Delete the value of a stanza interface using dictionary-like syntax.

        Example:
            >>> msg['body'] = "Hi!"
            >>> msg['body']
            'Hi!'
            >>> del msg['body']
            >>> msg['body']
            ''

        Stanza interfaces are typically mapped directly to the underlyig XML
        object, but can be overridden by the presence of a delAttrib method
        (or delFoo where the interface is named foo, etc).

        The effect of deleting a stanza interface value named foo will be
        one of:
            1. Call delFoo, if it exists.
            2. Delete foo element, if foo is in sub_interfaces.
            3. Delete top level XML attribute named foo.
            4. Remove the foo plugin, if it was loaded.
            5. Do nothing.

        Arguments:
            attrib -- The name of the affected stanza interface.
        """
        if attrib in self.interfaces:
            del_method = "del%s" % attrib.title()
            if hasattr(self, del_method):
                getattr(self, del_method)()
            else:
                if attrib in self.sub_interfaces:
                    return self._delSub(attrib)
                else:
                    self._delAttr(attrib)
        elif attrib in self.plugin_attrib_map:
            if attrib in self.plugins:
                del self.plugins[attrib]
        return self

    def _setAttr(self, name, value):
        """
        Set the value of a top level attribute of the underlying XML object.

        If the new value is None or an empty string, then the attribute will
        be removed.

        Arguments:
            name  -- The name of the attribute.
            value -- The new value of the attribute, or None or '' to
                     remove it.
        """
        if value is None or value == '':
            self.__delitem__(name)
        else:
            self.xml.attrib[name] = value

    def _delAttr(self, name):
        """
        Remove a top level attribute of the underlying XML object.

        Arguments:
            name -- The name of the attribute.
        """
        if name in self.xml.attrib:
            del self.xml.attrib[name]

    def _getAttr(self, name, default=''):
        """
        Return the value of a top level attribute of the underlying
        XML object.

        In case the attribute has not been set, a default value can be
        returned instead. An empty string is returned if no other default
        is supplied.

        Arguments:
            name    -- The name of the attribute.
            default -- Optional value to return if the attribute has not
                       been set. An empty string is returned otherwise.
        """
        return self.xml.attrib.get(name, default)

    def _getSubText(self, name, default=''):
        """
        Return the text contents of a sub element.

        In case the element does not exist, or it has no textual content,
        a default value can be returned instead. An empty string is returned 
        if no other default is supplied.

        Arguments:
            name    -- The name or XPath expression of the element.
            default -- Optional default to return if the element does
                       not exists. An empty string is returned otherwise.
        """
        name = self._fix_ns(name)
        stanza = self.xml.find(name)
        if stanza is None or stanza.text is None:
            return default
        else:
            return stanza.text

    def _setSubText(self, name, text=None, keep=False):
        """
        Set the text contents of a sub element.

        In case the element does not exist, a element will be created,
        and its text contents will be set.

        If the text is set to an empty string, or None, then the 
        element will be removed, unless keep is set to True.

        Arguments:
            name -- The name or XPath expression of the element.
            text -- The new textual content of the element. If the text
                    is an empty string or None, the element will be removed
                    unless the parameter keep is True.
            keep -- Indicates if the element should be kept if its text is
                    removed. Defaults to False.
        """
        name = self._fix_ns(name)
        element = self.xml.find(name)

        if not text and not keep:
            return self.__delitem__(name)

        if element is None:
            # We need to add the element. If the provided name was
            # an XPath expression, some of the intermediate elements
            # may already exist. If so, we want to use those instead
            # of generating new elements.
            last_xml = self.xml
            walked = []
            for ename in name.split('/'):
                walked.append(ename)
                element = self.xml.find("/".join(walked))
                if element is None:
                    element = ET.Element(ename)
                    last_xml.append(element)
                last_xml = element
            element = last_xml

        element.text = text
        return element 

    def _delSub(self, name, all=False):
        """
        Remove sub elements that match the given name or XPath.

        If the element is in a path, then any parent elements that become
        empty after deleting the element may also be deleted if requested
        by setting all=True.

        Arguments:
            name -- The name or XPath expression for the element(s) to remove.
            all  -- If True, remove all empty elements in the path to the 
                    deleted element. Defaults to False.
        """
        name = self._fix_ns(name)
        path = name.split("/")
        original_target = path[-1]

        for level, _ in enumerate(path):
            # Generate the paths to the target elements and their parent.
            element_path = "/".join(path[:len(path) - level])
            parent_path = "/".join(path[:len(path) - level - 1])

            elements = self.xml.findall(element_path)
            parent = self.xml.find(parent_path)

            if elements:
                if parent is None:
                    parent = self.xml
                for element in elements:
                    if element.tag == original_target or not element.getchildren():
                        # Only delete the originally requested elements, and any
                        # parent elements that have become empty.
                        parent.remove(element)
            if not all:
                # If we don't want to delete elements up the tree, stop
                # after deleting the first level of elements.
                return
        
    @property
    def attrib(self): #backwards compatibility
            return self

    def __iter__(self):
            self.idx = 0
            return self

    def __bool__(self): #python 3.x
            return True
    
    def __nonzero__(self): #python 2.x
            return True

    def __next__(self):
            self.idx += 1
            if self.idx > len(self.iterables):
                    self.idx = 0
                    raise StopIteration
            return self.iterables[self.idx - 1]

    def next(self):
            return self.__next__()

    def __len__(self):
            return len(self.iterables)

    def append(self, item):
            if not isinstance(item, ElementBase):
                    if type(item) == XML_TYPE:
                            return self.appendxml(item)
                    else:
                            raise TypeError
            self.xml.append(item.xml)
            self.iterables.append(item)
            return self

    def pop(self, idx=0):
            aff = self.iterables.pop(idx)
            self.xml.remove(aff.xml)
            return aff

    def get(self, key, defaultvalue=None):
            value = self[key]
            if value is None or value == '':
                    return defaultvalue
            return value

    def keys(self):
            out = []
            out += [x for x in self.interfaces]
            out += [x for x in self.plugins]
            if self.iterables:
                    out.append('substanzas')
            return tuple(out)

    def match(self, matchstring):
            if isinstance(matchstring, str):
                    nodes = matchstring.split('/')
            else:
                    nodes = matchstring
            tagargs = nodes[0].split('@')
            if tagargs[0] not in (self.plugins, self.plugin_attrib): return False
            founditerable = False
            for iterable in self.iterables:
                    if nodes[1:] == []:
                            break
                    founditerable = iterable.match(nodes[1:])
                    if founditerable: break;
            for evals in tagargs[1:]:
                    x,y = evals.split('=')
                    if self[x] != y: return False
            if not founditerable and len(nodes) > 1:
                    next = nodes[1].split('@')[0]
                    if next in self.plugins:
                            return self.plugins[next].match(nodes[1:])
                    else:
                            return False
            return True

    def find(self, xpath): # for backwards compatiblity, expose elementtree interface
            return self.xml.find(xpath)

    def findall(self, xpath):
            return self.xml.findall(xpath)

    def __eq__(self, other):
            if not isinstance(other, ElementBase):
                    return False
            values = self.getStanzaValues()
            for key in other:
                    if key not in values or values[key] != other[key]:
                            return False
            return True

    def appendxml(self, xml):
            self.xml.append(xml)
            return self

    def __copy__(self):
            return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)

    def __str__(self):
            return tostring(self.xml, xmlns='', stanza_ns=self.namespace)

    def __repr__(self):
            return self.__str__()

    def _fix_ns(self, xpath):
        """
        Apply the stanza's namespace to elements in an XPath expression.

        Arguments:
            xpath -- The XPath expression to fix with namespaces.
        """

        def fix_ns(name):
            """Apply namespace to an element if needed."""
            if "}" in name:
                return name
            return "{%s}%s" % (self.namespace, name)

        return "/".join(map(fix_ns, xpath.split("/")))
        

#def __del__(self): #prevents garbage collection of reference cycle
#       if self.parent is not None:
#               self.parent.xml.remove(self.xml)

class StanzaBase(ElementBase):
        name = 'stanza'
        namespace = 'jabber:client'
        interfaces = set(('type', 'to', 'from', 'id', 'payload'))
        types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
        sub_interfaces = tuple()

        def __init__(self, stream=None, xml=None, stype=None, sto=None, sfrom=None, sid=None):
                self.stream = stream
                if stream is not None:
                        self.namespace = stream.default_ns
                ElementBase.__init__(self, xml)
                if stype is not None:
                        self['type'] = stype
                if sto is not None:
                        self['to'] = sto
                if sfrom is not None:
                        self['from'] = sfrom
                self.tag = "{%s}%s" % (self.namespace, self.name)

        def setType(self, value):
                if value in self.types:
                        self.xml.attrib['type'] = value
                return self

        def getPayload(self):
                return self.xml.getchildren()

        def setPayload(self, value):
                self.xml.append(value)
                return self

        def delPayload(self):
                self.clear()
                return self

        def clear(self):
                for child in self.xml.getchildren():
                        self.xml.remove(child)
                for plugin in list(self.plugins.keys()):
                        del self.plugins[plugin]
                return self

        def reply(self):
                # if it's a component, use from
                if self.stream and hasattr(self.stream, "is_component") and self.stream.is_component:
                        self['from'], self['to'] = self['to'], self['from']
                else:
                        self['to'] = self['from']
                        del self['from']
                self.clear()
                return self

        def error(self):
                self['type'] = 'error'
                return self

        def getTo(self):
                return JID(self._getAttr('to'))

        def setTo(self, value):
                return self._setAttr('to', str(value))

        def getFrom(self):
                return JID(self._getAttr('from'))

        def setFrom(self, value):
                return self._setAttr('from', str(value))

        def unhandled(self):
                pass

        def exception(self, e):
                logging.exception('Error handling {%s}%s stanza' % (self.namespace, self.name))

        def send(self):
                self.stream.sendRaw(self.__str__())

        def __copy__(self):
                return self.__class__(xml=copy.deepcopy(self.xml), stream=self.stream)

        def __str__(self):
                return tostring(self.xml, xmlns='', stanza_ns=self.namespace, stream=self.stream)