Rename to slixmpp
[slixmpp.git] / slixmpp / plugins / xep_0059 / stanza.py
blobe2701af41e18f4eba9191499f975b3a99ab86d70
1 """
2 Slixmpp: The Slick XMPP Library
3 Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson
4 This file is part of Slixmpp.
6 See the file LICENSE for copying permission.
7 """
9 from slixmpp.xmlstream import ElementBase, ET
10 from slixmpp.plugins.xep_0030.stanza.items import DiscoItems
13 class Set(ElementBase):
15 """
16 XEP-0059 (Result Set Managment) can be used to manage the
17 results of queries. For example, limiting the number of items
18 per response or starting at certain positions.
20 Example set stanzas:
21 <iq type="get">
22 <query xmlns="http://jabber.org/protocol/disco#items">
23 <set xmlns="http://jabber.org/protocol/rsm">
24 <max>2</max>
25 </set>
26 </query>
27 </iq>
29 <iq type="result">
30 <query xmlns="http://jabber.org/protocol/disco#items">
31 <item jid="conference.example.com" />
32 <item jid="pubsub.example.com" />
33 <set xmlns="http://jabber.org/protocol/rsm">
34 <first>conference.example.com</first>
35 <last>pubsub.example.com</last>
36 </set>
37 </query>
38 </iq>
40 Stanza Interface:
41 first_index -- The index attribute of <first>
42 after -- The id defining from which item to start
43 before -- The id defining from which item to
44 start when browsing backwards
45 max -- Max amount per response
46 first -- Id for the first item in the response
47 last -- Id for the last item in the response
48 index -- Used to set an index to start from
49 count -- The number of remote items available
51 Methods:
52 set_first_index -- Sets the index attribute for <first> and
53 creates the element if it doesn't exist
54 get_first_index -- Returns the value of the index
55 attribute for <first>
56 del_first_index -- Removes the index attribute for <first>
57 but keeps the element
58 set_before -- Sets the value of <before>, if the value is True
59 then the element will be created without a value
60 get_before -- Returns the value of <before>, if it is
61 empty it will return True
63 """
64 namespace = 'http://jabber.org/protocol/rsm'
65 name = 'set'
66 plugin_attrib = 'rsm'
67 sub_interfaces = set(('first', 'after', 'before', 'count',
68 'index', 'last', 'max'))
69 interfaces = set(('first_index', 'first', 'after', 'before',
70 'count', 'index', 'last', 'max'))
72 def set_first_index(self, val):
73 fi = self.find("{%s}first" % (self.namespace))
74 if fi is not None:
75 if val:
76 fi.attrib['index'] = val
77 elif 'index' in fi.attrib:
78 del fi.attrib['index']
79 elif val:
80 fi = ET.Element("{%s}first" % (self.namespace))
81 fi.attrib['index'] = val
82 self.xml.append(fi)
84 def get_first_index(self):
85 fi = self.find("{%s}first" % (self.namespace))
86 if fi is not None:
87 return fi.attrib.get('index', '')
89 def del_first_index(self):
90 fi = self.xml.find("{%s}first" % (self.namespace))
91 if fi is not None:
92 del fi.attrib['index']
94 def set_before(self, val):
95 b = self.xml.find("{%s}before" % (self.namespace))
96 if b is None and val is True:
97 self._set_sub_text('{%s}before' % self.namespace, '', True)
98 else:
99 self._set_sub_text('{%s}before' % self.namespace, val)
101 def get_before(self):
102 b = self.xml.find("{%s}before" % (self.namespace))
103 if b is not None and not b.text:
104 return True
105 elif b is not None:
106 return b.text
107 else:
108 return None