diff options
author | Maxime “pep” Buquet <pep@bouah.net> | 2020-05-20 00:37:46 +0200 |
---|---|---|
committer | Maxime “pep” Buquet <pep@bouah.net> | 2020-05-20 00:37:46 +0200 |
commit | b62f0e90c1b4865a1d14dd757b7d57fbf0a946cd (patch) | |
tree | d76100aeeddecdead092759450b89c660ffa9e1d | |
parent | 9ace053992f12af62680a0b3f20f3291150e8935 (diff) | |
download | slixmpp-b62f0e90c1b4865a1d14dd757b7d57fbf0a946cd.tar.gz slixmpp-b62f0e90c1b4865a1d14dd757b7d57fbf0a946cd.tar.bz2 slixmpp-b62f0e90c1b4865a1d14dd757b7d57fbf0a946cd.tar.xz slixmpp-b62f0e90c1b4865a1d14dd757b7d57fbf0a946cd.zip |
xep_0045: don't create empty item
Only create an item if an attribute is set. Don't create it when reading
if it wasn't already present.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
-rw-r--r-- | slixmpp/plugins/xep_0045.py | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/slixmpp/plugins/xep_0045.py b/slixmpp/plugins/xep_0045.py index 30769b5c..dfbb3b58 100644 --- a/slixmpp/plugins/xep_0045.py +++ b/slixmpp/plugins/xep_0045.py @@ -29,61 +29,61 @@ class MUCPresence(ElementBase): affiliations = {'', } roles = {'', } - def get_xml_item(self): + def get_item_attr(self, attr, default): + item = self.xml.find('{http://jabber.org/protocol/muc#user}item') + if item is None: + return default + return item.get(attr) + + def set_item_attr(self, attr, value): item = self.xml.find('{http://jabber.org/protocol/muc#user}item') if item is None: item = ET.Element('{http://jabber.org/protocol/muc#user}item') self.xml.append(item) + item.attrib[attr] = value return item + def del_item_attr(self, attr): + item = self.xml.find('{http://jabber.org/protocol/muc#user}item') + if item is not None and attr in item.attrib: + del item.attrib[attr] + def get_affiliation(self): - #TODO if no affilation, set it to the default and return default - item = self.get_xml_item() - return item.get('affiliation', '') + return self.get_item_attr('affiliation', '') def set_affiliation(self, value): - item = self.get_xml_item() - #TODO check for valid affiliation - item.attrib['affiliation'] = value + self.set_item_attr('affiliation', value) return self def del_affiliation(self): - item = self.get_xml_item() - #TODO set default affiliation - if 'affiliation' in item.attrib: del item.attrib['affiliation'] + # TODO: set default affiliation + self.del_item_attr('affiliation') return self def get_jid(self): - item = self.get_xml_item() - return JID(item.get('jid', '')) + return JID(self.get_item_attr('jid', '')) def set_jid(self, value): - item = self.get_xml_item() if not isinstance(value, str): value = str(value) - item.attrib['jid'] = value + self.set_item_attr('jid', value) return self def del_jid(self): - item = self.get_xml_item() - if 'jid' in item.attrib: del item.attrib['jid'] + self.del_item_attr('jid') return self def get_role(self): - item = self.get_xml_item() - #TODO get default role, set default role if none - return item.get('role', '') + return self.get_item_attr('role', '') def set_role(self, value): - item = self.get_xml_item() - #TODO check for valid role - item.attrib['role'] = value + # TODO: check for valid role + self.set_item_attr('role', value) return self def del_role(self): - item = self.get_xml_item() - #TODO set default role - if 'role' in item.attrib: del item.attrib['role'] + # TODO: set default role + self.del_item_attr('role') return self def get_nick(self): |