diff options
Diffstat (limited to 'sleekxmpp/stanza')
-rw-r--r-- | sleekxmpp/stanza/error.py | 2 | ||||
-rw-r--r-- | sleekxmpp/stanza/iq.py | 1 | ||||
-rw-r--r-- | sleekxmpp/stanza/message.py | 3 | ||||
-rw-r--r-- | sleekxmpp/stanza/nick.py | 65 | ||||
-rw-r--r-- | sleekxmpp/stanza/presence.py | 3 | ||||
-rw-r--r-- | sleekxmpp/stanza/rootstanza.py | 2 | ||||
-rw-r--r-- | sleekxmpp/stanza/roster.py | 102 | ||||
-rw-r--r-- | sleekxmpp/stanza/stream_error.py | 3 | ||||
-rw-r--r-- | sleekxmpp/stanza/stream_features.py | 3 |
9 files changed, 81 insertions, 103 deletions
diff --git a/sleekxmpp/stanza/error.py b/sleekxmpp/stanza/error.py index d985f729..825287ad 100644 --- a/sleekxmpp/stanza/error.py +++ b/sleekxmpp/stanza/error.py @@ -6,7 +6,7 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin +from sleekxmpp.xmlstream import ElementBase, ET class Error(ElementBase): diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py index f05dad17..47d51b04 100644 --- a/sleekxmpp/stanza/iq.py +++ b/sleekxmpp/stanza/iq.py @@ -6,7 +6,6 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.stanza import Error from sleekxmpp.stanza.rootstanza import RootStanza from sleekxmpp.xmlstream import StanzaBase, ET from sleekxmpp.xmlstream.handler import Waiter, Callback diff --git a/sleekxmpp/stanza/message.py b/sleekxmpp/stanza/message.py index 19d4d9e2..407802bd 100644 --- a/sleekxmpp/stanza/message.py +++ b/sleekxmpp/stanza/message.py @@ -6,9 +6,8 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.stanza import Error from sleekxmpp.stanza.rootstanza import RootStanza -from sleekxmpp.xmlstream import StanzaBase, ET +from sleekxmpp.xmlstream import StanzaBase class Message(RootStanza): diff --git a/sleekxmpp/stanza/nick.py b/sleekxmpp/stanza/nick.py index 1e23d34f..0e9a5c2b 100644 --- a/sleekxmpp/stanza/nick.py +++ b/sleekxmpp/stanza/nick.py @@ -6,67 +6,12 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.stanza import Message, Presence -from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin - - -class Nick(ElementBase): - - """ - XEP-0172: User Nickname allows the addition of a <nick> element - in several stanza types, including <message> and <presence> stanzas. - - The nickname contained in a <nick> should be the global, friendly or - informal name chosen by the owner of a bare JID. The <nick> element - may be included when establishing communications with new entities, - such as normal XMPP users or MUC services. - - The nickname contained in a <nick> element will not necessarily be - the same as the nickname used in a MUC. - - Example stanzas: - <message to="user@example.com"> - <nick xmlns="http://jabber.org/nick/nick">The User</nick> - <body>...</body> - </message> - - <presence to="otheruser@example.com" type="subscribe"> - <nick xmlns="http://jabber.org/nick/nick">The User</nick> - </presence> - - Stanza Interface: - nick -- A global, friendly or informal name chosen by a user. - - Methods: - setup -- Overrides ElementBase.setup. - get_nick -- Return the nickname in the <nick> element. - set_nick -- Add a <nick> element with the given nickname. - del_nick -- Remove the <nick> element. - """ - - namespace = 'http://jabber.org/protocol/nick' - name = 'nick' - plugin_attrib = name - interfaces = set(('nick',)) - - def set_nick(self, nick): - """ - Add a <nick> element with the given nickname. - - Arguments: - nick -- A human readable, informal name. - """ - self.xml.text = nick - - def get_nick(self): - """Return the nickname in the <nick> element.""" - return self.xml.text - - def del_nick(self): - """Remove the <nick> element.""" - if self.parent is not None: - self.parent().xml.remove(self.xml) +# The nickname stanza has been moved to its own plugin, but the existing +# references are kept for backwards compatibility. +from sleekxmpp.stanza import Message, Presence +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.plugins.xep_0172 import UserNick as Nick register_stanza_plugin(Message, Nick) register_stanza_plugin(Presence, Nick) diff --git a/sleekxmpp/stanza/presence.py b/sleekxmpp/stanza/presence.py index c8706233..f2dd0968 100644 --- a/sleekxmpp/stanza/presence.py +++ b/sleekxmpp/stanza/presence.py @@ -6,9 +6,8 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.stanza import Error from sleekxmpp.stanza.rootstanza import RootStanza -from sleekxmpp.xmlstream import StanzaBase, ET +from sleekxmpp.xmlstream import StanzaBase class Presence(RootStanza): diff --git a/sleekxmpp/stanza/rootstanza.py b/sleekxmpp/stanza/rootstanza.py index 2ac47d8b..bb756acb 100644 --- a/sleekxmpp/stanza/rootstanza.py +++ b/sleekxmpp/stanza/rootstanza.py @@ -7,8 +7,6 @@ """ import logging -import traceback -import sys from sleekxmpp.exceptions import XMPPError, IqError, IqTimeout from sleekxmpp.stanza import Error diff --git a/sleekxmpp/stanza/roster.py b/sleekxmpp/stanza/roster.py index 3fcdbebc..4788ba72 100644 --- a/sleekxmpp/stanza/roster.py +++ b/sleekxmpp/stanza/roster.py @@ -36,7 +36,30 @@ class Roster(ElementBase): namespace = 'jabber:iq:roster' name = 'query' plugin_attrib = 'roster' - interfaces = set(('items',)) + interfaces = set(('items', 'ver')) + + def get_ver(self): + """ + Ensure handling an empty ver attribute propery. + + The ver attribute is special in that the presence of the + attribute with an empty value is important for boostrapping + roster versioning. + """ + return self.xml.attrib.get('ver', None) + + def set_ver(self, ver): + """ + Ensure handling an empty ver attribute propery. + + The ver attribute is special in that the presence of the + attribute with an empty value is important for boostrapping + roster versioning. + """ + if ver is not None: + self.xml.attrib['ver'] = ver + else: + del self.xml.attrib['ver'] def set_items(self, items): """ @@ -55,20 +78,10 @@ class Roster(ElementBase): """ self.del_items() 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]: - 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) + item = RosterItem() + item.values = items[jid] + item['jid'] = jid + self.append(item) return self def get_items(self): @@ -83,31 +96,58 @@ class Roster(ElementBase): been assigned. """ items = {} - itemsxml = self.xml.findall('{jabber:iq:roster}item') - if itemsxml is not None: - for itemxml in itemsxml: - item = {} - item['name'] = itemxml.get('name', '') - item['subscription'] = itemxml.get('subscription', '') - 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 + for item in self['substanzas']: + if isinstance(item, RosterItem): + items[item['jid']] = item.values + # Remove extra JID reference to keep everything + # backward compatible + del items[item['jid']]['jid'] return items def del_items(self): """ Remove all <item> elements from the roster stanza. """ - for child in self.xml.getchildren(): - self.xml.remove(child) + for item in self['substanzas']: + 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_jid(self): + return JID(self._get_attr('jid', '')) + + def set_jid(self, jid): + self._set_attr('jid', str(jid)) + + 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(Roster, RosterItem, iterable=True) # To comply with PEP8, method names now use underscores. # Deprecated method names are re-mapped for backwards compatibility. diff --git a/sleekxmpp/stanza/stream_error.py b/sleekxmpp/stanza/stream_error.py index cf59a7fa..5a6dac96 100644 --- a/sleekxmpp/stanza/stream_error.py +++ b/sleekxmpp/stanza/stream_error.py @@ -7,8 +7,7 @@ """ from sleekxmpp.stanza.error import Error -from sleekxmpp.xmlstream import StanzaBase, ElementBase, ET -from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.xmlstream import StanzaBase class StreamError(Error, StanzaBase): diff --git a/sleekxmpp/stanza/stream_features.py b/sleekxmpp/stanza/stream_features.py index b800011f..9993c84a 100644 --- a/sleekxmpp/stanza/stream_features.py +++ b/sleekxmpp/stanza/stream_features.py @@ -6,8 +6,7 @@ See the file LICENSE for copying permission. """ -from sleekxmpp.xmlstream import ElementBase, StanzaBase, ET -from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.xmlstream import StanzaBase class StreamFeatures(StanzaBase): |