summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/stanzabase.py
blob: 233e91f62ac8c7905e23776f4beb99510b1ff151 (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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
# -*- coding: utf-8 -*-
"""
    sleekxmpp.xmlstream.stanzabase
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module implements a wrapper layer for XML objects
    that allows them to be treated like dictionaries.

    Part of SleekXMPP: The Sleek XMPP Library

    :copyright: (c) 2011 Nathanael C. Fritz
    :license: MIT, see LICENSE for more details
"""

from __future__ import with_statement, unicode_literals

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

from sleekxmpp.xmlstream import JID
from sleekxmpp.xmlstream.tostring import tostring
from sleekxmpp.thirdparty import OrderedDict


log = logging.getLogger(__name__)


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


XML_NS = 'http://www.w3.org/XML/1998/namespace'


def register_stanza_plugin(stanza, plugin, iterable=False, overrides=False):
    """
    Associate a stanza object as a plugin for another stanza.

    >>> from sleekxmpp.xmlstream import register_stanza_plugin
    >>> register_stanza_plugin(Iq, CustomStanza)

    Plugin stanzas marked as iterable will be included in the list of
    substanzas for the parent, using ``parent['substanzas']``. If the
    attribute ``plugin_multi_attrib`` was defined for the plugin, then
    the substanza set can be filtered to only instances of the plugin
    class. For example, given a plugin class ``Foo`` with
    ``plugin_multi_attrib = 'foos'`` then::

        parent['foos']

    would return a collection of all ``Foo`` substanzas.

    :param class stanza: The class of the parent stanza.
    :param class plugin: The class of the plugin stanza.
    :param bool iterable: Indicates if the plugin stanza should be
                          included in the parent stanza's iterable
                          ``'substanzas'`` interface results.
    :param bool overrides: Indicates if the plugin should be allowed
                           to override the interface handlers for
                           the parent stanza, based on the plugin's
                           ``overrides`` field.

    .. versionadded:: 1.0-Beta1
        Made ``register_stanza_plugin`` the default name. The prior
        ``registerStanzaPlugin`` function name remains as an alias.
    """
    tag = "{%s}%s" % (plugin.namespace, plugin.name)

    # Prevent weird memory reference gotchas by ensuring
    # that the parent stanza class has its own set of
    # plugin info maps and is not using the mappings from
    # an ancestor class (like ElementBase).
    plugin_info = ('plugin_attrib_map', 'plugin_tag_map',
                   'plugin_iterables', 'plugin_overrides')
    for attr in plugin_info:
        info = getattr(stanza, attr)
        setattr(stanza, attr, info.copy())

    stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
    stanza.plugin_tag_map[tag] = plugin

    if iterable:
        stanza.plugin_iterables.add(plugin)
        if plugin.plugin_multi_attrib:
            multiplugin = multifactory(plugin, plugin.plugin_multi_attrib)
            register_stanza_plugin(stanza, multiplugin)
    if overrides:
        for interface in plugin.overrides:
            stanza.plugin_overrides[interface] = plugin.plugin_attrib


# To maintain backwards compatibility for now, preserve the camel case name.
registerStanzaPlugin = register_stanza_plugin


def multifactory(stanza, plugin_attrib):
    """
    Returns a ElementBase class for handling reoccuring child stanzas
    """

    def plugin_filter(self):
        return lambda x: isinstance(x, self._multistanza)

    def plugin_lang_filter(self, lang):
        return lambda x: isinstance(x, self._multistanza) and \
                         x['lang'] == lang

    class Multi(ElementBase):
        """
        Template class for multifactory
        """
        def setup(self, xml=None):
            self.xml = ET.Element('')

    def get_multi(self, lang=None):
        parent = self.parent()
        if not lang or lang == '*':
            res = filter(plugin_filter(self), parent)
        else:
            res = filter(plugin_filter(self, lang), parent)
        return list(res)

    def set_multi(self, val, lang=None):
        parent = self.parent()
        del_multi = getattr(self, 'del_%s' % plugin_attrib)
        del_multi(lang)
        for sub in val:
            parent.append(sub)

    def del_multi(self, lang=None):
        parent = self.parent()
        if not lang or lang == '*':
            res = filter(plugin_filter(self), parent)
        else:
            res = filter(plugin_filter(self, lang), parent)
        res = list(res)
        if not res:
            del parent.plugins[(plugin_attrib, None)]
            parent.loaded_plugins.remove(plugin_attrib)
            try:
                parent.xml.remove(self.xml)
            except:
                pass
        else:
            for stanza in list(res):
                parent.iterables.remove(stanza)
                parent.xml.remove(stanza.xml)

    Multi.is_extension = True
    Multi.plugin_attrib = plugin_attrib
    Multi._multistanza = stanza
    Multi.interfaces = set([plugin_attrib])
    Multi.lang_interfaces = set([plugin_attrib])
    setattr(Multi, "get_%s" % plugin_attrib, get_multi)
    setattr(Multi, "set_%s" % plugin_attrib, set_multi)
    setattr(Multi, "del_%s" % plugin_attrib, del_multi)
    return Multi


def fix_ns(xpath, split=False, propagate_ns=True, default_ns=''):
    """Apply the stanza's namespace to elements in an XPath expression.

    :param string xpath: The XPath expression to fix with namespaces.
    :param bool split: Indicates if the fixed XPath should be left as a
                       list of element names with namespaces. Defaults to
                       False, which returns a flat string path.
    :param bool propagate_ns: Overrides propagating parent element
                              namespaces to child elements. Useful if
                              you wish to simply split an XPath that has
                              non-specified namespaces, and child and
                              parent namespaces are known not to always
                              match. Defaults to True.
    """
    fixed = []
    # Split the XPath into a series of blocks, where a block
    # is started by an element with a namespace.
    ns_blocks = xpath.split('{')
    for ns_block in ns_blocks:
        if '}' in ns_block:
            # Apply the found namespace to following elements
            # that do not have namespaces.
            namespace = ns_block.split('}')[0]
            elements = ns_block.split('}')[1].split('/')
        else:
            # Apply the stanza's namespace to the following
            # elements since no namespace was provided.
            namespace = default_ns
            elements = ns_block.split('/')

        for element in elements:
            if element:
                # Skip empty entry artifacts from splitting.
                if propagate_ns:
                    tag = '{%s}%s' % (namespace, element)
                else:
                    tag = element
                fixed.append(tag)
    if split:
        return fixed
    return '/'.join(fixed)


class ElementBase(object):

    """
    The core of SleekXMPP's stanza XML manipulation and handling is provided
    by ElementBase. ElementBase wraps XML cElementTree objects and enables
    access to the XML contents through dictionary syntax, similar in style
    to the Ruby XMPP library Blather's stanza implementation.

    Stanzas are defined by their name, namespace, and interfaces. For
    example, a simplistic Message stanza could be defined as::

        >>> class Message(ElementBase):
        ...     name = "message"
        ...     namespace = "jabber:client"
        ...     interfaces = set(('to', 'from', 'type', 'body'))
        ...     sub_interfaces = set(('body',))

    The resulting Message stanza's contents may be accessed as so::

        >>> message['to'] = "user@example.com"
        >>> message['body'] = "Hi!"
        >>> message['body']
        "Hi!"
        >>> del message['body']
        >>> message['body']
        ""

    The interface values map to either custom access methods, stanza
    XML attributes, or (if the interface is also in sub_interfaces) the
    text contents of a stanza's subelement.

    Custom access methods may be created by adding methods of the
    form "getInterface", "setInterface", or "delInterface", where
    "Interface" is the titlecase version of the interface name.

    Stanzas may be extended through the use of plugins. A plugin
    is simply a stanza that has a plugin_attrib value. For example::

        >>> class MessagePlugin(ElementBase):
        ...     name = "custom_plugin"
        ...     namespace = "custom"
        ...     interfaces = set(('useful_thing', 'custom'))
        ...     plugin_attrib = "custom"

    The plugin stanza class must be associated with its intended
    container stanza by using register_stanza_plugin as so::

        >>> register_stanza_plugin(Message, MessagePlugin)

    The plugin may then be accessed as if it were built-in to the parent
    stanza::

        >>> message['custom']['useful_thing'] = 'foo'

    If a plugin provides an interface that is the same as the plugin's
    plugin_attrib value, then the plugin's interface may be assigned
    directly from the parent stanza, as shown below, but retrieving
    information will require all interfaces to be used, as so::

        >>> # Same as using message['custom']['custom']
        >>> message['custom'] = 'bar'
        >>> # Must use all interfaces
        >>> message['custom']['custom']
        'bar'

    If the plugin sets :attr:`is_extension` to ``True``, then both setting
    and getting an interface value that is the same as the plugin's
    plugin_attrib value will work, as so::

        >>> message['custom'] = 'bar'  # Using is_extension=True
        >>> message['custom']
        'bar'


    :param xml: Initialize the stanza object with an existing XML object.
    :param parent: Optionally specify a parent stanza object will
                   contain this substanza.
    """

    #: The XML tag name of the element, not including any namespace
    #: prefixes. For example, an :class:`ElementBase` object for
    #: ``<message />`` would use ``name = 'message'``.
    name = 'stanza'

    #: The XML namespace for the element. Given ``<foo xmlns="bar" />``,
    #: then ``namespace = "bar"`` should be used. The default namespace
    #: is ``jabber:client`` since this is being used in an XMPP library.
    namespace = 'jabber:client'

    #: For :class:`ElementBase` subclasses which are intended to be used
    #: as plugins, the ``plugin_attrib`` value defines the plugin name.
    #: Plugins may be accessed by using the ``plugin_attrib`` value as
    #: the interface. An example using ``plugin_attrib = 'foo'``::
    #:
    #:     register_stanza_plugin(Message, FooPlugin)
    #:     msg = Message()
    #:     msg['foo']['an_interface_from_the_foo_plugin']
    plugin_attrib = 'plugin'

    #: For :class:`ElementBase` subclasses that are intended to be an
    #: iterable group of items, the ``plugin_multi_attrib`` value defines
    #: an interface for the parent stanza which returns the entire group
    #: of matching substanzas. So the following are equivalent::
    #:
    #:     # Given stanza class Foo, with plugin_multi_attrib = 'foos'
    #:     parent['foos']
    #:     filter(isinstance(item, Foo), parent['substanzas'])
    plugin_multi_attrib = ''

    #: The set of keys that the stanza provides for accessing and
    #: manipulating the underlying XML object. This set may be augmented
    #: with the :attr:`plugin_attrib` value of any registered
    #: stanza plugins.
    interfaces = set(('type', 'to', 'from', 'id', 'payload'))

    #: A subset of :attr:`interfaces` which maps interfaces to direct
    #: subelements of the underlying XML object. Using this set, the text
    #: of these subelements may be set, retrieved, or removed without
    #: needing to define custom methods.
    sub_interfaces = set()

    #: A subset of :attr:`interfaces` which maps the presence of
    #: subelements to boolean values. Using this set allows for quickly
    #: checking for the existence of empty subelements like ``<required />``.
    #:
    #: .. versionadded:: 1.1
    bool_interfaces = set()

    #: .. versionadded:: 1.1.2
    lang_interfaces = set()

    #: In some cases you may wish to override the behaviour of one of the
    #: parent stanza's interfaces. The ``overrides`` list specifies the
    #: interface name and access method to be overridden. For example,
    #: to override setting the parent's ``'condition'`` interface you
    #: would use::
    #:
    #:     overrides = ['set_condition']
    #:
    #: Getting and deleting the ``'condition'`` interface would not
    #: be affected.
    #:
    #: .. versionadded:: 1.0-Beta5
    overrides = []

    #: If you need to add a new interface to an existing stanza, you
    #: can create a plugin and set ``is_extension = True``. Be sure
    #: to set the :attr:`plugin_attrib` value to the desired interface
    #: name, and that it is the only interface listed in
    #: :attr:`interfaces`. Requests for the new interface from the
    #: parent stanza will be passed to the plugin directly.
    #:
    #: .. versionadded:: 1.0-Beta5
    is_extension = False

    #: A map of interface operations to the overriding functions.
    #: For example, after overriding the ``set`` operation for
    #: the interface ``body``, :attr:`plugin_overrides` would be::
    #:
    #:     {'set_body': <some function>}
    #:
    #: .. versionadded: 1.0-Beta5
    plugin_overrides = {}

    #: A mapping of the :attr:`plugin_attrib` values of registered
    #: plugins to their respective classes.
    plugin_attrib_map = {}

    #: A mapping of root element tag names (in ``'{namespace}elementname'``
    #: format) to the plugin classes responsible for them.
    plugin_tag_map = {}

    #: The set of stanza classes that can be iterated over using
    #: the 'substanzas' interface. Classes are added to this set
    #: when registering a plugin with ``iterable=True``::
    #:
    #:     register_stanza_plugin(DiscoInfo, DiscoItem, iterable=True)
    #:
    #: .. versionadded:: 1.0-Beta5
    plugin_iterables = set()

    #: A deprecated version of :attr:`plugin_iterables` that remains
    #: for backward compatibility. It required a parent stanza to
    #: know beforehand what stanza classes would be iterable::
    #:
    #:     class DiscoItem(ElementBase):
    #:         ...
    #:
    #:     class DiscoInfo(ElementBase):
    #:         subitem = (DiscoItem, )
    #:         ...
    #:
    #: .. deprecated:: 1.0-Beta5
    subitem = set()

    #: The default XML namespace: ``http://www.w3.org/XML/1998/namespace``.
    xml_ns = XML_NS

    def __init__(self, xml=None, parent=None):
        self._index = 0

        #: The underlying XML object for the stanza. It is a standard
        #: :class:`xml.etree.cElementTree` object.
        self.xml = xml

        #: An ordered dictionary of plugin stanzas, mapped by their
        #: :attr:`plugin_attrib` value.
        self.plugins = OrderedDict()
        self.loaded_plugins = set()

        #: A list of child stanzas whose class is included in
        #: :attr:`plugin_iterables`.
        self.iterables = []

        #: The name of the tag for the stanza's root element. It is the
        #: same as calling :meth:`tag_name()` and is formatted as
        #: ``'{namespace}elementname'``.
        self.tag = self.tag_name()

        #: A :class:`weakref.weakref` to the parent stanza, if there is one.
        #: If not, then :attr:`parent` is ``None``.
        self.parent = None
        if parent is not None:
            if not isinstance(parent, weakref.ReferenceType):
                self.parent = weakref.ref(parent)
            else:
                self.parent = parent

        if self.subitem is not None:
            for sub in self.subitem:
                self.plugin_iterables.add(sub)

        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:
            if child.tag in self.plugin_tag_map:
                plugin_class = self.plugin_tag_map[child.tag]
                self.init_plugin(plugin_class.plugin_attrib,
                                 existing_xml=child,
                                 reuse=False)

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

        Will return ``True`` if XML was generated according to the stanza's
        definition instead of building a stanza object from an existing
        XML object.

        :param xml: An existing XML object to use for the stanza's content
                    instead of generating new XML.
        """
        if self.xml is None:
            self.xml = xml

        last_xml = self.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, lang=None):
        """Enable and initialize a stanza plugin.

        Alias for :meth:`init_plugin`.

        :param string attrib: The :attr:`plugin_attrib` value of the
                              plugin to enable.
        """
        return self.init_plugin(attrib, lang)

    def _get_plugin(self, name, lang=None):
        if lang is None:
            lang = self.get_lang()

        if name not in self.plugin_attrib_map:
            return None

        plugin_class = self.plugin_attrib_map[name]

        if plugin_class.is_extension:
            if (name, None) in self.plugins:
                return self.plugins[(name, None)]
            else:
                return self.init_plugin(name, lang)
        else:
            if (name, lang) in self.plugins:
                return self.plugins[(name, lang)]
            else:
                return self.init_plugin(name, lang)

    def init_plugin(self, attrib, lang=None, existing_xml=None, reuse=True):
        """Enable and initialize a stanza plugin.

        :param string attrib: The :attr:`plugin_attrib` value of the
                              plugin to enable.
        """
        default_lang = self.get_lang()
        if not lang:
            lang = default_lang

        plugin_class = self.plugin_attrib_map[attrib]

        if plugin_class.is_extension and (attrib, None) in self.plugins:
            return self.plugins[(attrib, None)]
        if reuse and (attrib, lang) in self.plugins:
            return self.plugins[(attrib, lang)]

        if existing_xml is None:
            existing_xml = self.xml.find(plugin_class.tag_name())

        if existing_xml is not None:
            if existing_xml.attrib.get('{%s}lang' % XML_NS, default_lang) != lang:
                existing_xml = None

        plugin = plugin_class(parent=self, xml=existing_xml)

        if plugin.is_extension:
            self.plugins[(attrib, None)] = plugin
        else:
            if lang != default_lang:
                plugin['lang'] = lang
            self.plugins[(attrib, lang)] = plugin

        if plugin_class in self.plugin_iterables:
            self.iterables.append(plugin)
            if plugin_class.plugin_multi_attrib:
                self.init_plugin(plugin_class.plugin_multi_attrib)

        self.loaded_plugins.add(attrib)

        return plugin

    def _get_stanza_values(self):
        """Return A JSON/dictionary version of the XML content
        exposed through the stanza's interfaces::

            >>> msg = Message()
            >>> msg.values
            {'body': '', 'from': , 'mucnick': '', 'mucroom': '',
            'to': , 'type': 'normal', 'id': '', 'subject': ''}

        Likewise, assigning to :attr:`values` will change the XML
        content::

            >>> msg = Message()
            >>> msg.values = {'body': 'Hi!', 'to': 'user@example.com'}
            >>> msg
            '<message to="user@example.com"><body>Hi!</body></message>'

        .. versionadded:: 1.0-Beta1
        """
        values = {}
        values['lang'] = self['lang']
        for interface in self.interfaces:
            values[interface] = self[interface]
            if interface in self.lang_interfaces:
                values['%s|*' % interface] = self['%s|*' % interface]
        for plugin, stanza in self.plugins.items():
            lang = stanza['lang']
            if lang:
                values['%s|%s' % (plugin[0], lang)] = stanza.values
            else:
                values[plugin[0]] = stanza.values
        if self.iterables:
            iterables = []
            for stanza in self.iterables:
                iterables.append(stanza.values)
                iterables[-1]['__childtag__'] = stanza.tag
            values['substanzas'] = iterables
        return values

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

        Stanza plugin values may be set using nested dictionaries.

        :param values: A dictionary mapping stanza interface with values.
                       Plugin interfaces may accept a nested dictionary that
                       will be used recursively.

        .. versionadded:: 1.0-Beta1
        """
        iterable_interfaces = [p.plugin_attrib for \
                                    p in self.plugin_iterables]

        for interface, value in values.items():
            full_interface = interface
            interface_lang = ('%s|' % interface).split('|')
            interface = interface_lang[0]
            lang = interface_lang[1] or self.get_lang()

            if interface == 'substanzas':
                # Remove existing substanzas
                for stanza in self.iterables:
                    self.xml.remove(stanza.xml)
                self.iterables = []

                # Add new substanzas
                for subdict in value:
                    if '__childtag__' in subdict:
                        for subclass in self.plugin_iterables:
                            child_tag = "{%s}%s" % (subclass.namespace,
                                                    subclass.name)
                            if subdict['__childtag__'] == child_tag:
                                sub = subclass(parent=self)
                                sub.values = subdict
                                self.iterables.append(sub)
                                break
            elif interface == 'lang':
                self[interface] = value
            elif interface in self.interfaces:
                self[full_interface] = value
            elif interface in self.plugin_attrib_map:
                if interface not in iterable_interfaces:
                    plugin = self._get_plugin(interface, lang)
                    if plugin:
                        plugin.values = value
        return self

    def __getitem__(self, attrib):
        """Return the value of a stanza interface using dict-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 ``get_attrib``
        method (or ``get_foo`` 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 (``'substanzas'``)
            2. The result of calling the ``get_foo`` override handler.
            3. The result of calling ``get_foo``.
            4. The result of calling ``getFoo``.
            5. The contents of the ``foo`` subelement, if ``foo`` is listed
               in :attr:`sub_interfaces`.
            6. True or False depending on the existence of a ``foo``
               subelement and ``foo`` is in :attr:`bool_interfaces`.
            7. The value of the ``foo`` attribute of the XML object.
            8. The plugin named ``'foo'``
            9. An empty string.

        :param string attrib: The name of the requested stanza interface.
        """
        full_attrib = attrib
        attrib_lang = ('%s|' % attrib).split('|')
        attrib = attrib_lang[0]
        lang = attrib_lang[1] or ''

        kwargs = {}
        if lang and attrib in self.lang_interfaces:
            kwargs['lang'] = lang

        if attrib == 'substanzas':
            return self.iterables
        elif attrib in self.interfaces or attrib == 'lang':
            get_method = "get_%s" % attrib.lower()
            get_method2 = "get%s" % attrib.title()

            if self.plugin_overrides:
                name = self.plugin_overrides.get(get_method, None)
                if name:
                    plugin = self._get_plugin(name, lang)
                    if plugin:
                        handler = getattr(plugin, get_method, None)
                        if handler:
                            return handler(**kwargs)

            if hasattr(self, get_method):
                return getattr(self, get_method)(**kwargs)
            elif hasattr(self, get_method2):
                return getattr(self, get_method2)(**kwargs)
            else:
                if attrib in self.sub_interfaces:
                    return self._get_sub_text(attrib, lang=lang)
                elif attrib in self.bool_interfaces:
                    elem = self.xml.find('{%s}%s' % (self.namespace, attrib))
                    return elem is not None
                else:
                    return self._get_attr(attrib)
        elif attrib in self.plugin_attrib_map:
            plugin = self._get_plugin(attrib, lang)
            if plugin and plugin.is_extension:
                return plugin[full_attrib]
            return plugin
        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 ``set_attrib``
        method (or ``set_foo`` 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 the ``set_foo`` override handler, if it exists.
            3. Call ``set_foo``, if it exists.
            4. Call ``setFoo``, if it exists.
            5. Set the text of a ``foo`` element, if ``'foo'`` is
               in :attr:`sub_interfaces`.
            6. Add or remove an empty subelement ``foo``
               if ``foo`` is in :attr:`bool_interfaces`.
            7. Set the value of a top level XML attribute named ``foo``.
            8. Attempt to pass the value to a plugin named ``'foo'`` using
               the plugin's ``'foo'`` interface.
            9. Do nothing.

        :param string attrib: The name of the stanza interface to modify.
        :param value: The new value of the stanza interface.
        """
        full_attrib = attrib
        attrib_lang = ('%s|' % attrib).split('|')
        attrib = attrib_lang[0]
        lang = attrib_lang[1] or ''

        kwargs = {}
        if lang and attrib in self.lang_interfaces:
            kwargs['lang'] = lang

        if attrib in self.interfaces or attrib == 'lang':
            if value is not None:
                set_method = "set_%s" % attrib.lower()
                set_method2 = "set%s" % attrib.title()

                if self.plugin_overrides:
                    name = self.plugin_overrides.get(set_method, None)
                    if name:
                        plugin = self._get_plugin(name, lang)
                        if plugin:
                            handler = getattr(plugin, set_method, None)
                            if handler:
                                return handler(value, **kwargs)

                if hasattr(self, set_method):
                    getattr(self, set_method)(value, **kwargs)
                elif hasattr(self, set_method2):
                    getattr(self, set_method2)(value, **kwargs)
                else:
                    if attrib in self.sub_interfaces:
                        if lang == '*':
                            return self._set_all_sub_text(attrib,
                                                          value,
                                                          lang='*')
                        return self._set_sub_text(attrib, text=value,
                                                          lang=lang)
                    elif attrib in self.bool_interfaces:
                        if value:
                            return self._set_sub_text(attrib, '',
                                    keep=True,
                                    lang=lang)
                        else:
                            return self._set_sub_text(attrib, '',
                                    keep=False,
                                    lang=lang)
                    else:
                        self._set_attr(attrib, value)
            else:
                self.__delitem__(attrib)
        elif attrib in self.plugin_attrib_map:
            plugin = self._get_plugin(attrib, lang)
            if plugin:
                plugin[full_attrib] = value
        return self

    def __delitem__(self, attrib):
        """Delete the value of a stanza interface using dict-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 ``del_attrib``
        method (or ``del_foo`` where the interface is named ``'foo'``, etc).

        The effect of deleting a stanza interface value named ``foo`` will be
        one of:

            1. Call ``del_foo`` override handler, if it exists.
            2. Call ``del_foo``, if it exists.
            3. Call ``delFoo``, if it exists.
            4. Delete ``foo`` element, if ``'foo'`` is in
               :attr:`sub_interfaces`.
            5. Remove ``foo`` element if ``'foo'`` is in
               :attr:`bool_interfaces`.
            6. Delete top level XML attribute named ``foo``.
            7. Remove the ``foo`` plugin, if it was loaded.
            8. Do nothing.

        :param attrib: The name of the affected stanza interface.
        """
        full_attrib = attrib
        attrib_lang = ('%s|' % attrib).split('|')
        attrib = attrib_lang[0]
        lang = attrib_lang[1] or ''

        kwargs = {}
        if lang and attrib in self.lang_interfaces:
            kwargs['lang'] = lang

        if attrib in self.interfaces or attrib == 'lang':
            del_method = "del_%s" % attrib.lower()
            del_method2 = "del%s" % attrib.title()

            if self.plugin_overrides:
                name = self.plugin_overrides.get(del_method, None)
                if name:
                    plugin = self._get_plugin(attrib, lang)
                    if plugin:
                        handler = getattr(plugin, del_method, None)
                        if handler:
                            return handler(**kwargs)

            if hasattr(self, del_method):
                getattr(self, del_method)(**kwargs)
            elif hasattr(self, del_method2):
                getattr(self, del_method2)(**kwargs)
            else:
                if attrib in self.sub_interfaces:
                    return self._del_sub(attrib, lang=lang)
                elif attrib in self.bool_interfaces:
                    return self._del_sub(attrib, lang=lang)
                else:
                    self._del_attr(attrib)
        elif attrib in self.plugin_attrib_map:
            plugin = self._get_plugin(attrib, lang)
            if not plugin:
                return self
            if plugin.is_extension:
                del plugin[full_attrib]
                del self.plugins[(attrib, None)]
            else:
                del self.plugins[(attrib, lang)]
            self.loaded_plugins.remove(attrib)
            try:
                self.xml.remove(plugin.xml)
            except:
                pass
        return self

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

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

        :param name: The name of the attribute.
        :param 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 _del_attr(self, name):
        """Remove a top level attribute of the XML object.

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

    def _get_attr(self, name, default=''):
        """Return the value of a top level attribute of the 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.

        :param name: The name of the attribute.
        :param 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 _get_sub_text(self, name, default='', lang=None):
        """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.

        :param name: The name or XPath expression of the element.
        :param default: Optional default to return if the element does
                        not exists. An empty string is returned otherwise.
        """
        name = self._fix_ns(name)
        if lang == '*':
            return self._get_all_sub_text(name, default, None)

        default_lang = self.get_lang()
        if not lang:
            lang = default_lang

        stanzas = self.xml.findall(name)
        if not stanzas:
            return default
        for stanza in stanzas:
            if stanza.attrib.get('{%s}lang' % XML_NS, default_lang) == lang:
                if stanza.text is None:
                    return default
                return stanza.text
        return default

    def _get_all_sub_text(self, name, default='', lang=None):
        name = self._fix_ns(name)

        default_lang = self.get_lang()
        results = OrderedDict()
        stanzas = self.xml.findall(name)
        if stanzas:
            for stanza in stanzas:
                stanza_lang = stanza.attrib.get('{%s}lang' % XML_NS,
                                                default_lang)
                if not lang or lang == '*' or stanza_lang == lang:
                    results[stanza_lang] = stanza.text
        return results

    def _set_sub_text(self, name, text=None, keep=False, lang=None):
        """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.

        :param name: The name or XPath expression of the element.
        :param 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.
        :param keep: Indicates if the element should be kept if its text is
                     removed. Defaults to False.
        """
        default_lang = self.get_lang()
        if lang is None:
            lang = default_lang

        if not text and not keep:
            return self._del_sub(name, lang=lang)

        path = self._fix_ns(name, split=True)
        name = path[-1]
        parent = self.xml

        # The first goal is to find the parent of the subelement, or, if
        # we can't find that, the closest grandparent element.
        missing_path = []
        search_order = path[:-1]
        while search_order:
            parent = self.xml.find('/'.join(search_order))
            ename = search_order.pop()
            if parent is not None:
                break
            else:
                missing_path.append(ename)
        missing_path.reverse()

        # Find all existing elements that match the desired
        # element path (there may be multiples due to different
        # languages values).
        if parent is not None:
            elements = self.xml.findall('/'.join(path))
        else:
            parent = self.xml
            elements = []

        # Insert the remaining grandparent elements that don't exist yet.
        for ename in missing_path:
            element = ET.Element(ename)
            parent.append(element)
            parent = element

        # Re-use an existing element with the proper language, if one exists.
        for element in elements:
            elang = element.attrib.get('{%s}lang' % XML_NS, default_lang)
            if not lang and elang == default_lang or lang and lang == elang:
                element.text = text
                return element

        # No useable element exists, so create a new one.
        element = ET.Element(name)
        element.text = text
        if lang and lang != default_lang:
            element.attrib['{%s}lang' % XML_NS] = lang
        parent.append(element)
        return element

    def _set_all_sub_text(self, name, values, keep=False, lang=None):
        self._del_sub(name, lang)
        for value_lang, value in values.items():
            if not lang or lang == '*' or value_lang == lang:
                self._set_sub_text(name, text=value,
                                         keep=keep,
                                         lang=value_lang)

    def _del_sub(self, name, all=False, lang=None):
        """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.

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

        default_lang = self.get_lang()
        if not lang:
            lang = default_lang

        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 list(element):
                        # Only delete the originally requested elements, and
                        # any parent elements that have become empty.
                        elem_lang = element.attrib.get('{%s}lang' % XML_NS,
                                                       default_lang)
                        if lang == '*' or elem_lang == lang:
                            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

    def match(self, xpath):
        """Compare a stanza object with an XPath-like expression.

        If the XPath matches the contents of the stanza object, the match
        is successful.

        The XPath expression may include checks for stanza attributes.
        For example::

            'presence@show=xa@priority=2/status'

        Would match a presence stanza whose show value is set to ``'xa'``,
        has a priority value of ``'2'``, and has a status element.

        :param string xpath: The XPath expression to check against. It
                             may be either a string or a list of element
                             names with attribute checks.
        """
        if not isinstance(xpath, list):
            xpath = self._fix_ns(xpath, split=True, propagate_ns=False)

        # Extract the tag name and attribute checks for the first XPath node.
        components = xpath[0].split('@')
        tag = components[0]
        attributes = components[1:]

        if tag not in (self.name, "{%s}%s" % (self.namespace, self.name)) and \
            tag not in self.loaded_plugins and tag not in self.plugin_attrib:
            # The requested tag is not in this stanza, so no match.
            return False

        # Check the rest of the XPath against any substanzas.
        matched_substanzas = False
        for substanza in self.iterables:
            if xpath[1:] == []:
                break
            matched_substanzas = substanza.match(xpath[1:])
            if matched_substanzas:
                break

        # Check attribute values.
        for attribute in attributes:
            name, value = attribute.split('=')
            if self[name] != value:
                return False

        # Check sub interfaces.
        if len(xpath) > 1:
            next_tag = xpath[1]
            if next_tag in self.sub_interfaces and self[next_tag]:
                return True

        # Attempt to continue matching the XPath using the stanza's plugins.
        if not matched_substanzas and len(xpath) > 1:
            # Convert {namespace}tag@attribs to just tag
            next_tag = xpath[1].split('@')[0].split('}')[-1]
            langs = [name[1] for name in self.plugins if name[0] == next_tag]
            for lang in langs:
                plugin = self._get_plugin(next_tag, lang)
                if plugin and plugin.match(xpath[1:]):
                    return True
            return False

        # Everything matched.
        return True

    def find(self, xpath):
        """Find an XML object in this stanza given an XPath expression.

        Exposes ElementTree interface for backwards compatibility.

        .. note::

            Matching on attribute values is not supported in Python 2.6
            or Python 3.1

        :param string xpath: An XPath expression matching a single
                             desired element.
        """
        return self.xml.find(xpath)

    def findall(self, xpath):
        """Find multiple XML objects in this stanza given an XPath expression.

        Exposes ElementTree interface for backwards compatibility.

        .. note::

            Matching on attribute values is not supported in Python 2.6
            or Python 3.1.

        :param string xpath: An XPath expression matching multiple
                             desired elements.
        """
        return self.xml.findall(xpath)

    def get(self, key, default=None):
        """Return the value of a stanza interface.

        If the found value is None or an empty string, return the supplied
        default value.

        Allows stanza objects to be used like dictionaries.

        :param string key: The name of the stanza interface to check.
        :param default: Value to return if the stanza interface has a value
                        of ``None`` or ``""``. Will default to returning None.
        """
        value = self[key]
        if value is None or value == '':
            return default
        return value

    def keys(self):
        """Return the names of all stanza interfaces provided by the
        stanza object.

        Allows stanza objects to be used like dictionaries.
        """
        out = []
        out += [x for x in self.interfaces]
        out += [x for x in self.loaded_plugins]
        out.append('lang')
        if self.iterables:
            out.append('substanzas')
        return out

    def append(self, item):
        """Append either an XML object or a substanza to this stanza object.

        If a substanza object is appended, it will be added to the list
        of iterable stanzas.

        Allows stanza objects to be used like lists.

        :param item: Either an XML object or a stanza object to add to
                     this stanza's contents.
        """
        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)
        if item.__class__ in self.plugin_iterables:
            if item.__class__.plugin_multi_attrib:
                self.init_plugin(item.__class__.plugin_multi_attrib)
        return self

    def appendxml(self, xml):
        """Append an XML object to the stanza's XML.

        The added XML will not be included in the list of
        iterable substanzas.

        :param XML xml: The XML object to add to the stanza.
        """
        self.xml.append(xml)
        return self

    def pop(self, index=0):
        """Remove and return the last substanza in the list of
        iterable substanzas.

        Allows stanza objects to be used like lists.

        :param int index: The index of the substanza to remove.
        """
        substanza = self.iterables.pop(index)
        self.xml.remove(substanza.xml)
        return substanza

    def next(self):
        """Return the next iterable substanza."""
        return self.__next__()

    def clear(self):
        """Remove all XML element contents and plugins.

        Any attribute values will be preserved.
        """
        for child in list(self.xml):
            self.xml.remove(child)

        for plugin in list(self.plugins.keys()):
            del self.plugins[plugin]
        return self

    @classmethod
    def tag_name(cls):
        """Return the namespaced name of the stanza's root element.

        The format for the tag name is::

            '{namespace}elementname'

        For example, for the stanza ``<foo xmlns="bar" />``,
        ``stanza.tag_name()`` would return ``"{bar}foo"``.
        """
        return "{%s}%s" % (cls.namespace, cls.name)

    def get_lang(self, lang=None):
        result = self.xml.attrib.get('{%s}lang' % XML_NS, '')
        if not result and self.parent and self.parent():
            return self.parent()['lang']
        return result

    def set_lang(self, lang):
        self.del_lang()
        attr = '{%s}lang' % XML_NS
        if lang:
            self.xml.attrib[attr] = lang

    def del_lang(self):
        attr = '{%s}lang' % XML_NS
        if attr in self.xml.attrib:
            del self.xml.attrib[attr]

    @property
    def attrib(self):
        """Return the stanza object itself.

        Older implementations of stanza objects used XML objects directly,
        requiring the use of ``.attrib`` to access attribute values.

        Use of the dictionary syntax with the stanza object itself for
        accessing stanza interfaces is preferred.

        .. deprecated:: 1.0
        """
        return self

    def _fix_ns(self, xpath, split=False, propagate_ns=True):
        return fix_ns(xpath, split=split,
                             propagate_ns=propagate_ns,
                             default_ns=self.namespace)

    def __eq__(self, other):
        """Compare the stanza object with another to test for equality.

        Stanzas are equal if their interfaces return the same values,
        and if they are both instances of ElementBase.

        :param ElementBase other: The stanza object to compare against.
        """
        if not isinstance(other, ElementBase):
            return False

        # Check that this stanza is a superset of the other stanza.
        values = self.values
        for key in other.keys():
            if key not in values or values[key] != other[key]:
                return False

        # Check that the other stanza is a superset of this stanza.
        values = other.values
        for key in self.keys():
            if key not in values or values[key] != self[key]:
                return False

        # Both stanzas are supersets of each other, therefore they
        # must be equal.
        return True

    def __ne__(self, other):
        """Compare the stanza object with another to test for inequality.

        Stanzas are not equal if their interfaces return different values,
        or if they are not both instances of ElementBase.

        :param ElementBase other: The stanza object to compare against.
        """
        return not self.__eq__(other)

    def __bool__(self):
        """Stanza objects should be treated as True in boolean contexts.

        Python 3.x version.
        """
        return True

    def __nonzero__(self):
        """Stanza objects should be treated as True in boolean contexts.

        Python 2.x version.
        """
        return True

    def __len__(self):
        """Return the number of iterable substanzas in this stanza."""
        return len(self.iterables)

    def __iter__(self):
        """Return an iterator object for the stanza's substanzas.

        The iterator is the stanza object itself. Attempting to use two
        iterators on the same stanza at the same time is discouraged.
        """
        self._index = 0
        return self

    def __next__(self):
        """Return the next iterable substanza."""
        self._index += 1
        if self._index > len(self.iterables):
            self._index = 0
            raise StopIteration
        return self.iterables[self._index - 1]

    def __copy__(self):
        """Return a copy of the stanza object that does not share the same
        underlying XML object.
        """
        return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)

    def __str__(self, top_level_ns=True):
        """Return a string serialization of the underlying XML object.

        .. seealso:: :ref:`tostring`

        :param bool top_level_ns: Display the top-most namespace.
                                  Defaults to True.
        """
        return tostring(self.xml, xmlns='',
                        top_level=True)

    def __repr__(self):
        """Use the stanza's serialized XML as its representation."""
        return self.__str__()


class StanzaBase(ElementBase):

    """
    StanzaBase provides the foundation for all other stanza objects used
    by SleekXMPP, and defines a basic set of interfaces common to nearly
    all stanzas. These interfaces are the ``'id'``, ``'type'``, ``'to'``,
    and ``'from'`` attributes. An additional interface, ``'payload'``, is
    available to access the XML contents of the stanza. Most stanza objects
    will provided more specific interfaces, however.

    **Stanza Interfaces:**

        :id: An optional id value that can be used to associate stanzas
        :to: A JID object representing the recipient's JID.
        :from: A JID object representing the sender's JID.
               with their replies.
        :type: The type of stanza, typically will be ``'normal'``,
               ``'error'``, ``'get'``, or ``'set'``, etc.
        :payload: The XML contents of the stanza.

    :param XMLStream stream: Optional :class:`sleekxmpp.xmlstream.XMLStream`
                             object responsible for sending this stanza.
    :param XML xml: Optional XML contents to initialize stanza values.
    :param string stype: Optional stanza type value.
    :param sto: Optional string or :class:`sleekxmpp.xmlstream.JID`
                object of the recipient's JID.
    :param sfrom: Optional string or :class:`sleekxmpp.xmlstream.JID`
                  object of the sender's JID.
    :param string sid: Optional ID value for the stanza.
    :param parent: Optionally specify a parent stanza object will
                   contain this substanza.
    """

    #: The default XMPP client namespace
    namespace = 'jabber:client'

    #: There is a small set of attributes which apply to all XMPP stanzas:
    #: the stanza type, the to and from JIDs, the stanza ID, and, especially
    #: in the case of an Iq stanza, a payload.
    interfaces = set(('type', 'to', 'from', 'id', 'payload'))

    #: A basic set of allowed values for the ``'type'`` interface.
    types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))

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

    def set_type(self, value):
        """Set the stanza's ``'type'`` attribute.

        Only type values contained in :attr:`types` are accepted.

        :param string value: One of the values contained in :attr:`types`
        """
        if value in self.types:
            self.xml.attrib['type'] = value
        return self

    def get_to(self):
        """Return the value of the stanza's ``'to'`` attribute."""
        return JID(self._get_attr('to'))

    def set_to(self, value):
        """Set the ``'to'`` attribute of the stanza.

        :param value: A string or :class:`sleekxmpp.xmlstream.JID` object
               representing the recipient's JID.
        """
        return self._set_attr('to', str(value))

    def get_from(self):
        """Return the value of the stanza's ``'from'`` attribute."""
        return JID(self._get_attr('from'))

    def set_from(self, value):
        """Set the 'from' attribute of the stanza.

        Arguments:
            from -- A string or JID object representing the sender's JID.
        """
        return self._set_attr('from', str(value))

    def get_payload(self):
        """Return a list of XML objects contained in the stanza."""
        return list(self.xml)

    def set_payload(self, value):
        """Add XML content to the stanza.

        :param value: Either an XML or a stanza object, or a list
                      of XML or stanza objects.
        """
        if not isinstance(value, list):
            value = [value]
        for val in value:
            self.append(val)
        return self

    def del_payload(self):
        """Remove the XML contents of the stanza."""
        self.clear()
        return self

    def reply(self, clear=True):
        """Prepare the stanza for sending a reply.

        Swaps the ``'from'`` and ``'to'`` attributes.

        If ``clear=True``, then also remove the stanza's
        contents to make room for the reply content.

        For client streams, the ``'from'`` attribute is removed.

        :param bool clear: Indicates if the stanza's contents should be
                           removed. Defaults to ``True``.
        """
        # 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']
        if clear:
            self.clear()
        return self

    def error(self):
        """Set the stanza's type to ``'error'``."""
        self['type'] = 'error'
        return self

    def unhandled(self):
        """Called if no handlers have been registered to process this stanza.

        Meant to be overridden.
        """
        pass

    def exception(self, e):
        """Handle exceptions raised during stanza processing.

        Meant to be overridden.
        """
        log.exception('Error handling {%s}%s stanza', self.namespace,
                                                      self.name)

    def send(self, now=False):
        """Queue the stanza to be sent on the XML stream.

        :param bool now: Indicates if the queue should be skipped and the
                         stanza sent immediately. Useful for stream
                         initialization. Defaults to ``False``.
        """
        self.stream.send(self, now=now)

    def __copy__(self):
        """Return a copy of the stanza object that does not share the
        same underlying XML object, but does share the same XML stream.
        """
        return self.__class__(xml=copy.deepcopy(self.xml),
                              stream=self.stream)

    def __str__(self, top_level_ns=False):
        """Serialize the stanza's XML to a string.

        :param bool top_level_ns: Display the top-most namespace.
                                  Defaults to ``False``.
        """
        xmlns = self.stream.default_ns if self.stream else ''
        return tostring(self.xml, xmlns=xmlns,
                        stream=self.stream,
                        top_level=(self.stream is None))


#: A JSON/dictionary version of the XML content exposed through
#: the stanza interfaces::
#:
#:     >>> msg = Message()
#:     >>> msg.values
#:    {'body': '', 'from': , 'mucnick': '', 'mucroom': '',
#:     'to': , 'type': 'normal', 'id': '', 'subject': ''}
#:
#: Likewise, assigning to the :attr:`values` will change the XML
#: content::
#:
#:     >>> msg = Message()
#:     >>> msg.values = {'body': 'Hi!', 'to': 'user@example.com'}
#:     >>> msg
#:     '<message to="user@example.com"><body>Hi!</body></message>'
#:
#: Child stanzas are exposed as nested dictionaries.
ElementBase.values = property(ElementBase._get_stanza_values,
                              ElementBase._set_stanza_values)


# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
ElementBase.initPlugin = ElementBase.init_plugin
ElementBase._getAttr = ElementBase._get_attr
ElementBase._setAttr = ElementBase._set_attr
ElementBase._delAttr = ElementBase._del_attr
ElementBase._getSubText = ElementBase._get_sub_text
ElementBase._setSubText = ElementBase._set_sub_text
ElementBase._delSub = ElementBase._del_sub
ElementBase.getStanzaValues = ElementBase._get_stanza_values
ElementBase.setStanzaValues = ElementBase._set_stanza_values

StanzaBase.setType = StanzaBase.set_type
StanzaBase.getTo = StanzaBase.get_to
StanzaBase.setTo = StanzaBase.set_to
StanzaBase.getFrom = StanzaBase.get_from
StanzaBase.setFrom = StanzaBase.set_from
StanzaBase.getPayload = StanzaBase.get_payload
StanzaBase.setPayload = StanzaBase.set_payload
StanzaBase.delPayload = StanzaBase.del_payload