Update Roster stanza to use RosterItem substanzas.

get_roster() now returns the Iq result stanza instead of True (stanzas
also evaluate to True).
This commit is contained in:
Lance Stout 2012-01-12 17:21:43 -08:00
parent c0074f95b1
commit e8b2dd6698
2 changed files with 43 additions and 32 deletions

View file

@ -244,7 +244,8 @@ class ClientXMPP(BaseXMPP):
response = iq.send(block, timeout, callback) response = iq.send(block, timeout, callback)
if block: if block:
return self._handle_roster(response, request=True) self._handle_roster(response, request=True)
return response
def _handle_connected(self, event=None): def _handle_connected(self, event=None):
#TODO: Use stream state here #TODO: Use stream state here
@ -291,7 +292,6 @@ class ClientXMPP(BaseXMPP):
iq.reply() iq.reply()
iq.enable('roster') iq.enable('roster')
iq.send() iq.send()
return True
def _handle_session_bind(self, jid): def _handle_session_bind(self, jid):
"""Set the client roster to the JID set by the server. """Set the client roster to the JID set by the server.

View file

@ -55,20 +55,10 @@ class Roster(ElementBase):
""" """
self.del_items() self.del_items()
for jid in items: for jid in items:
ijid = str(jid) item = RosterItem()
item = ET.Element('{jabber:iq:roster}item', {'jid': ijid}) item.values = items[jid]
if 'subscription' in items[jid]: item['jid'] = jid
item.attrib['subscription'] = items[jid]['subscription'] self.append(item)
if 'name' in items[jid]:
name = items[jid]['name']
if name is not None:
item.attrib['name'] = name
if 'groups' in items[jid]:
for group in items[jid]['groups']:
groupxml = ET.Element('{jabber:iq:roster}group')
groupxml.text = group
item.append(groupxml)
self.xml.append(item)
return self return self
def get_items(self): def get_items(self):
@ -83,31 +73,52 @@ class Roster(ElementBase):
been assigned. been assigned.
""" """
items = {} items = {}
itemsxml = self.xml.findall('{jabber:iq:roster}item') for item in self['substanzas']:
if itemsxml is not None: if isinstance(item, RosterItem):
for itemxml in itemsxml: items[item['jid']] = item.values
item = {} # Remove extra JID reference to keep everything
item['name'] = itemxml.get('name', '') # backward compatible
item['subscription'] = itemxml.get('subscription', '') del items[item['jid']]['jid']
item['ask'] = itemxml.get('ask', '')
item['approved'] = itemxml.get('approved', '')
item['groups'] = []
groupsxml = itemxml.findall('{jabber:iq:roster}group')
if groupsxml is not None:
for groupxml in groupsxml:
item['groups'].append(groupxml.text)
items[itemxml.get('jid')] = item
return items return items
def del_items(self): def del_items(self):
""" """
Remove all <item> elements from the roster stanza. Remove all <item> elements from the roster stanza.
""" """
for child in self.xml.getchildren(): for item in self['substanzas']:
self.xml.remove(child) if isinstance(item, RosterItem):
self.xml.remove(item.xml)
class RosterItem(ElementBase):
namespace = 'jabber:iq:roster'
name = 'item'
plugin_attrib = 'item'
interfaces = set(('jid', 'name', 'subscription', 'ask',
'approved', 'groups'))
def get_groups(self):
groups = []
for group in self.xml.findall('{%s}group' % self.namespace):
groups.append(group.text)
return groups
def set_groups(self, values):
self.del_groups()
for group in values:
group_xml = ET.Element('{%s}group' % self.namespace)
group_xml.text = group
self.xml.append(group_xml)
def del_groups(self):
for group in self.xml.findall('{%s}group' % self.namespace):
self.xmp.remove(group)
register_stanza_plugin(Iq, Roster) register_stanza_plugin(Iq, Roster)
register_stanza_plugin(Roster, RosterItem, iterable=True)
# To comply with PEP8, method names now use underscores. # To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility. # Deprecated method names are re-mapped for backwards compatibility.