summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0059/stanza.py
blob: e1942ee4d0539589bb8f74595756416b5e3b0608 (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
"""
    Slixmpp: The Slick XMPP Library
    Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson
    This file is part of Slixmpp.

    See the file LICENSE for copying permission.
"""

from slixmpp.xmlstream import ElementBase, ET
from slixmpp.plugins.xep_0030.stanza.items import DiscoItems


class Set(ElementBase):

    """
    XEP-0059 (Result Set Management) can be used to manage the
    results of queries. For example, limiting the number of items
    per response or starting at certain positions.

    Example set stanzas:
    ::

        <iq type="get">
          <query xmlns="http://jabber.org/protocol/disco#items">
            <set xmlns="http://jabber.org/protocol/rsm">
              <max>2</max>
            </set>
          </query>
        </iq>

        <iq type="result">
          <query xmlns="http://jabber.org/protocol/disco#items">
            <item jid="conference.example.com" />
            <item jid="pubsub.example.com" />
            <set xmlns="http://jabber.org/protocol/rsm">
              <first>conference.example.com</first>
              <last>pubsub.example.com</last>
            </set>
          </query>
        </iq>

    Stanza Interface:
    ::

        first_index -- The index attribute of <first>
        after       -- The id defining from which item to start
        before      -- The id defining from which item to
                       start when browsing backwards
        max         -- Max amount per response
        first       -- Id for the first item in the response
        last        -- Id for the last item in the response
        index       -- Used to set an index to start from
        count       -- The number of remote items available


    """
    namespace = 'http://jabber.org/protocol/rsm'
    name = 'set'
    plugin_attrib = 'rsm'
    sub_interfaces = {'first', 'after', 'before', 'count',
                      'index', 'last', 'max'}
    interfaces = {'first_index', 'first', 'after', 'before',
                  'count', 'index', 'last', 'max'}

    def set_first_index(self, val):
        """
        Sets the index attribute for <first> and
        creates the element if it doesn't exist
        """
        fi = self.xml.find("{%s}first" % (self.namespace))
        if fi is not None:
            if val:
                fi.attrib['index'] = val
            elif 'index' in fi.attrib:
                del fi.attrib['index']
        elif val:
            fi = ET.Element("{%s}first" % (self.namespace))
            fi.attrib['index'] = val
            self.xml.append(fi)

    def get_first_index(self):
        """
        Returns the value of the index attribute for <first>
        """
        fi = self.xml.find("{%s}first" % (self.namespace))
        if fi is not None:
            return fi.attrib.get('index', '')

    def del_first_index(self):
        """
        Removes the index attribute for <first> but keeps the element
        """
        fi = self.xml.find("{%s}first" % (self.namespace))
        if fi is not None:
            del fi.attrib['index']

    def set_before(self, val):
        """
        Sets the value of <before>, if the value is True
        then the element will be created without a value
        """
        b = self.xml.find("{%s}before" % (self.namespace))
        if b is None and val is True:
            self._set_sub_text('{%s}before' % self.namespace, '', True)
        else:
            self._set_sub_text('{%s}before' % self.namespace, val)

    def get_before(self):
        """
        Returns the value of <before>, if it is
        empty it will return True
        """
        b = self.xml.find("{%s}before" % (self.namespace))
        if b is not None and not b.text:
            return True
        elif b is not None:
            return b.text
        else:
            return None