From 917faecdcbf07b63173b34b708cff5bdf6476d25 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 19 Jul 2012 23:54:18 -0700 Subject: [PATCH] Fix issue of roster data being split across multiple rosters. Resolved by always normalizing JIDs to bare form, regardless of if they are JID objects or strings. Also simplified related code to prefer use of JID objects instead of strings so they don't need to be parsed multiple times. --- sleekxmpp/basexmpp.py | 52 +++++++++---------------- sleekxmpp/componentxmpp.py | 8 ++-- sleekxmpp/features/feature_bind/bind.py | 2 +- sleekxmpp/roster/multi.py | 14 ++++--- sleekxmpp/roster/single.py | 14 +++++-- sleekxmpp/xmlstream/jid.py | 3 ++ 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index da5b3e41..d81be016 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -91,13 +91,13 @@ class BaseXMPP(XMLStream): #: owner JIDs, as in the case for components. For clients #: which only have a single JID, see :attr:`client_roster`. self.roster = roster.Roster(self) - self.roster.add(self.boundjid.bare) + self.roster.add(self.boundjid) #: The single roster for the bound JID. This is the #: equivalent of:: #: #: self.roster[self.boundjid.bare] - self.client_roster = self.roster[self.boundjid.bare] + self.client_roster = self.roster[self.boundjid] #: The distinction between clients and components can be #: important, primarily for choosing how to handle the @@ -691,17 +691,13 @@ class BaseXMPP(XMLStream): msg['to'] = self.boundjid self.event('message', msg) - def _handle_available(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_available(presence) + def _handle_available(self, pres): + self.roster[pres['to']][pres['from']].handle_available(pres) - def _handle_unavailable(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_unavailable(presence) + def _handle_unavailable(self, pres): + self.roster[pres['to']][pres['from']].handle_unavailable(pres) - def _handle_new_subscription(self, stanza): + def _handle_new_subscription(self, pres): """Attempt to automatically handle subscription requests. Subscriptions will be approved if the request is from @@ -713,8 +709,8 @@ class BaseXMPP(XMLStream): If a subscription is accepted, a request for a mutual subscription will be sent if :attr:`auto_subscribe` is ``True``. """ - roster = self.roster[stanza['to'].bare] - item = self.roster[stanza['to'].bare][stanza['from'].bare] + roster = self.roster[pres['to']] + item = self.roster[pres['to']][pres['from']] if item['whitelisted']: item.authorize() elif roster.auto_authorize: @@ -724,30 +720,20 @@ class BaseXMPP(XMLStream): elif roster.auto_authorize == False: item.unauthorize() - def _handle_removed_subscription(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].unauthorize() + def _handle_removed_subscription(self, pres): + self.roster[pres['to']][pres['from']].handle_unauthorize(pres) - def _handle_subscribe(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_subscribe(presence) + def _handle_subscribe(self, pres): + self.roster[pres['to']][pres['from']].handle_subscribe(pres) - def _handle_subscribed(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_subscribed(presence) + def _handle_subscribed(self, pres): + self.roster[pres['to']][pres['from']].handle_subscribed(pres) - def _handle_unsubscribe(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_unsubscribe(presence) + def _handle_unsubscribe(self, pres): + self.roster[pres['to']][pres['from']].handle_unsubscribe(pres) - def _handle_unsubscribed(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_unsubscribed(presence) + def _handle_unsubscribed(self, pres): + self.roster[pres['to']][pres['from']].handle_unsubscribed(pres) def _handle_presence(self, presence): """Process incoming presence stanzas. diff --git a/sleekxmpp/componentxmpp.py b/sleekxmpp/componentxmpp.py index 20748b69..265ecd42 100644 --- a/sleekxmpp/componentxmpp.py +++ b/sleekxmpp/componentxmpp.py @@ -158,10 +158,8 @@ class ComponentXMPP(BaseXMPP): """ self.session_bind_event.set() self.session_started_event.set() - self.event("session_bind", self.xmpp.boundjid.full, direct=True) + self.event("session_bind", self.xmpp.boundjid, direct=True) self.event("session_start") - def _handle_probe(self, presence): - pto = presence['to'].bare - pfrom = presence['from'].bare - self.roster[pto][pfrom].handle_probe(presence) + def _handle_probe(self, pres): + self.roster[pres['to']][pres['from']].handle_probe(pres) diff --git a/sleekxmpp/features/feature_bind/bind.py b/sleekxmpp/features/feature_bind/bind.py index 2253d5ae..0584b308 100644 --- a/sleekxmpp/features/feature_bind/bind.py +++ b/sleekxmpp/features/feature_bind/bind.py @@ -50,7 +50,7 @@ class FeatureBind(BasePlugin): self.xmpp.set_jid(response['bind']['jid']) self.xmpp.bound = True - self.xmpp.event('session_bind', self.xmpp.boundjid.full, direct=True) + self.xmpp.event('session_bind', self.xmpp.boundjid, direct=True) self.xmpp.session_bind_event.set() self.xmpp.features.add('bind') diff --git a/sleekxmpp/roster/multi.py b/sleekxmpp/roster/multi.py index 9a04aebb..5d070ec8 100644 --- a/sleekxmpp/roster/multi.py +++ b/sleekxmpp/roster/multi.py @@ -94,10 +94,12 @@ class Roster(object): Arguments: key -- Return the roster for this JID. """ - if isinstance(key, JID): - key = key.bare if key is None: - key = self.xmpp.boundjid.bare + key = self.xmpp.boundjid + if not isinstance(key, JID): + key = JID(key) + key = key.bare + if key not in self._rosters: self.add(key) self._rosters[key].auto_authorize = self.auto_authorize @@ -119,8 +121,10 @@ class Roster(object): Arguments: node -- The JID for the new roster node. """ - if isinstance(node, JID): - node = node.bare + if not isinstance(node, JID): + node = JID(node) + + node = node.bare if node not in self._rosters: self._rosters[node] = RosterNode(self.xmpp, node, self.db) diff --git a/sleekxmpp/roster/single.py b/sleekxmpp/roster/single.py index f8c9c781..f080ae8a 100644 --- a/sleekxmpp/roster/single.py +++ b/sleekxmpp/roster/single.py @@ -89,8 +89,11 @@ class RosterNode(object): A new item entry will be created if one does not already exist. """ - if isinstance(key, JID): - key = key.bare + if key is None: + key = JID('') + if not isinstance(key, JID): + key = JID(key) + key = key.bare if key not in self._jids: self.add(key, save=True) return self._jids[key] @@ -101,8 +104,11 @@ class RosterNode(object): To remove an item from the server, use the remove() method. """ - if isinstance(key, JID): - key = key.bare + if key is None: + key = JID('') + if not isinstance(key, JID): + key = JID(key) + key = key.bare if key in self._jids: del self._jids[key] diff --git a/sleekxmpp/xmlstream/jid.py b/sleekxmpp/xmlstream/jid.py index 281bf4ee..1582164a 100644 --- a/sleekxmpp/xmlstream/jid.py +++ b/sleekxmpp/xmlstream/jid.py @@ -143,3 +143,6 @@ class JID(object): def __hash__(self): """Hash a JID based on the string version of its full JID.""" return hash(self.full) + + def __copy__(self): + return JID(self.jid)