summaryrefslogtreecommitdiff
path: root/sleekxmpp/stanza/roster.py
blob: 21aaa1617f1678c0edf6556962fa01ca28322e40 (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
from .. xmlstream.stanzabase import ElementBase, ET, JID

class Roster(ElementBase):
	namespace = 'jabber:iq:roster'
	name = 'query'
	plugin_attrib = 'roster'
	interfaces = set(('items',))

	def setItems(self, items):
		self.delItems()
		for jid in items:
			ijid = str(jid)
			item = ET.Element('{jabber:iq:roster}item', {'jid': ijid})
			if 'subscription' in items[jid]:
				item.attrib['subscription'] = items[jid]['subscription']
			if 'name' in items[jid]:
				item.attrib['name'] = items[jid]['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
	
	def getItems(self):
		items = {}
		itemsxml = self.xml.findall('{jabber:iq:roster}item')
		if itemsxml is not None:
			item = {}
			for itemxml in itemsxml:
				item['name'] = itemxml.get('name', '')
				item['subscription'] = itemxml.get('subscription', '')
				item['groups'] = []
				groupsxml = itemxml.findall('{jabber:iq:roster}group')
				if groupsxml is not None:
					for groupxml in groupsxml:
						item['groups'].append(groupxml.text)
				items[JID(itemxml.get('jid'))] = item
		return items
	
	def delItems(self):
		for child in self.xml.getchildren():
			self.xml.remove(child)