diff options
Diffstat (limited to 'sleekxmpp/plugins/xep_0030/stanza')
-rw-r--r-- | sleekxmpp/plugins/xep_0030/stanza/info.py | 36 | ||||
-rw-r--r-- | sleekxmpp/plugins/xep_0030/stanza/items.py | 45 |
2 files changed, 55 insertions, 26 deletions
diff --git a/sleekxmpp/plugins/xep_0030/stanza/info.py b/sleekxmpp/plugins/xep_0030/stanza/info.py index 6764acbb..25d1d07f 100644 --- a/sleekxmpp/plugins/xep_0030/stanza/info.py +++ b/sleekxmpp/plugins/xep_0030/stanza/info.py @@ -146,7 +146,7 @@ class DiscoInfo(ElementBase): return True return False - def get_identities(self, lang=None): + def get_identities(self, lang=None, dedupe=True): """ Return a set of all identities in tuple form as so: (category, type, lang, name) @@ -155,17 +155,25 @@ class DiscoInfo(ElementBase): that language. Arguments: - lang -- Optional, standard xml:lang value. + lang -- Optional, standard xml:lang value. + dedupe -- If True, de-duplicate identities, otherwise + return a list of all identities. """ - identities = set() + if dedupe: + identities = set() + else: + identities = [] for id_xml in self.findall('{%s}identity' % self.namespace): xml_lang = id_xml.attrib.get('{%s}lang' % self.xml_ns, None) if lang is None or xml_lang == lang: - identities.add(( - id_xml.attrib['category'], - id_xml.attrib['type'], - id_xml.attrib.get('{%s}lang' % self.xml_ns, None), - id_xml.attrib.get('name', None))) + id = (id_xml.attrib['category'], + id_xml.attrib['type'], + id_xml.attrib.get('{%s}lang' % self.xml_ns, None), + id_xml.attrib.get('name', None)) + if dedupe: + identities.add(id) + else: + identities.append(id) return identities def set_identities(self, identities, lang=None): @@ -237,11 +245,17 @@ class DiscoInfo(ElementBase): return True return False - def get_features(self): + def get_features(self, dedupe=True): """Return the set of all supported features.""" - features = set() + if dedupe: + features = set() + else: + features = [] for feature_xml in self.findall('{%s}feature' % self.namespace): - features.add(feature_xml.attrib['var']) + if dedupe: + features.add(feature_xml.attrib['var']) + else: + features.append(feature_xml.attrib['var']) return features def set_features(self, features): diff --git a/sleekxmpp/plugins/xep_0030/stanza/items.py b/sleekxmpp/plugins/xep_0030/stanza/items.py index a1fb819c..512f2336 100644 --- a/sleekxmpp/plugins/xep_0030/stanza/items.py +++ b/sleekxmpp/plugins/xep_0030/stanza/items.py @@ -6,7 +6,7 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.xmlstream import ElementBase, ET +from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin class DiscoItems(ElementBase): @@ -78,13 +78,11 @@ class DiscoItems(ElementBase): """ if (jid, node) not in self._items: self._items.add((jid, node)) - item_xml = ET.Element('{%s}item' % self.namespace) - item_xml.attrib['jid'] = jid - if name: - item_xml.attrib['name'] = name - if node: - item_xml.attrib['node'] = node - self.xml.append(item_xml) + item = DiscoItem(parent=self) + item['jid'] = jid + item['node'] = node + item['name'] = name + self.iterables.append(item) return True return False @@ -108,11 +106,9 @@ class DiscoItems(ElementBase): def get_items(self): """Return all items.""" items = set() - for item_xml in self.findall('{%s}item' % self.namespace): - item = (item_xml.attrib['jid'], - item_xml.attrib.get('node'), - item_xml.attrib.get('name')) - items.add(item) + for item in self['substanzas']: + if isinstance(item, DiscoItem): + items.add((item['jid'], item['node'], item['name'])) return items def set_items(self, items): @@ -132,5 +128,24 @@ class DiscoItems(ElementBase): def del_items(self): """Remove all items.""" self._items = set() - for item_xml in self.findall('{%s}item' % self.namespace): - self.xml.remove(item_xml) + for item in self['substanzas']: + if isinstance(item, DiscoItem): + self.xml.remove(item.xml) + + +class DiscoItem(ElementBase): + name = 'item' + namespace = 'http://jabber.org/protocol/disco#items' + plugin_attrib = name + interfaces = set(('jid', 'node', 'name')) + + def get_node(self): + """Return the item's node name or ``None``.""" + return self._get_attr('node', None) + + def get_name(self): + """Return the item's human readable name, or ``None``.""" + return self._get_attr('name', None) + + +register_stanza_plugin(DiscoItems, DiscoItem, iterable=True) |