2010-10-27 13:27:00 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
|
|
|
|
class RosterItem(object):
|
|
|
|
|
2010-10-27 17:36:23 +00:00
|
|
|
"""
|
|
|
|
A RosterItem is a single entry in a roster node, and tracks
|
|
|
|
the subscription state and user annotations of a single JID.
|
|
|
|
|
|
|
|
Roster items may use an external datastore to persist roster data
|
|
|
|
across sessions. Client applications will not need to use this
|
|
|
|
functionality, but is intended for components that do not have their
|
|
|
|
roster persisted automatically by the XMPP server.
|
|
|
|
|
|
|
|
Roster items provide many methods for handling incoming presence
|
|
|
|
stanzas that ensure that response stanzas are sent according to
|
|
|
|
RFC 3921.
|
|
|
|
|
|
|
|
The external datastore is accessed through a provided interface
|
|
|
|
object which is stored in self.db. The interface object MUST
|
|
|
|
provide two methods: load and save, both of which are responsible
|
|
|
|
for working with a single roster item. A private dictionary,
|
|
|
|
self._db_state, is used to store any metadata needed by the
|
|
|
|
interface, such as the row ID of a roster item, etc.
|
|
|
|
|
|
|
|
Interface for self.db.load:
|
|
|
|
load(owner_jid, jid, db_state):
|
|
|
|
owner_jid -- The JID that owns the roster.
|
|
|
|
jid -- The JID of the roster item.
|
|
|
|
db_state -- A dictionary containing any data saved
|
|
|
|
by the interface object after a save()
|
|
|
|
call. Will typically have the equivalent
|
|
|
|
of a 'row_id' value.
|
|
|
|
|
|
|
|
Interface for self.db.save:
|
|
|
|
save(owner_jid, jid, item_state, db_state):
|
|
|
|
owner_jid -- The JID that owns the roster.
|
|
|
|
jid -- The JID of the roster item.
|
|
|
|
item_state -- A dictionary containing the fields:
|
|
|
|
'from', 'to', 'pending_in', 'pending_out',
|
|
|
|
'whitelisted', 'subscription', 'name',
|
|
|
|
and 'groups'.
|
|
|
|
db_state -- A dictionary provided for persisting
|
|
|
|
datastore specific information. Typically,
|
|
|
|
a value equivalent to 'row_id' will be
|
|
|
|
stored here.
|
|
|
|
|
|
|
|
State Fields:
|
|
|
|
from -- Indicates if a subscription of type 'from'
|
|
|
|
has been authorized.
|
|
|
|
to -- Indicates if a subscription of type 'to' has
|
|
|
|
been authorized.
|
|
|
|
pending_in -- Indicates if a subscription request has been
|
|
|
|
received from this JID and it has not been
|
|
|
|
authorized yet.
|
|
|
|
pending_out -- Indicates if a subscription request has been sent
|
|
|
|
to this JID and it has not been accepted yet.
|
|
|
|
subscription -- Returns one of: 'to', 'from', 'both', or 'none'
|
|
|
|
based on the states of from, to, pending_in,
|
|
|
|
and pending_out. Assignment to this value does
|
|
|
|
not affect the states of the other values.
|
|
|
|
whitelisted -- Indicates if a subscription request from this
|
|
|
|
JID should be automatically accepted.
|
|
|
|
name -- A user supplied alias for the JID.
|
|
|
|
groups -- A list of group names for the JID.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
xmpp -- The main SleekXMPP instance.
|
|
|
|
owner -- The JID that owns the roster.
|
|
|
|
jid -- The JID for the roster item.
|
|
|
|
db -- Optional datastore interface object.
|
|
|
|
last_status -- The last presence sent to this JID.
|
|
|
|
resources -- A dictionary of online resources for this JID.
|
|
|
|
Will contain the fields 'show', 'status',
|
|
|
|
and 'priority'.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
load -- Retrieve the roster item from an
|
|
|
|
external datastore, if one was provided.
|
|
|
|
save -- Save the roster item to an external
|
|
|
|
datastore, if one was provided.
|
|
|
|
remove -- Remove a subscription to the JID and revoke
|
|
|
|
its whitelisted status.
|
|
|
|
subscribe -- Subscribe to the JID.
|
|
|
|
authorize -- Accept a subscription from the JID.
|
|
|
|
unauthorize -- Deny a subscription from the JID.
|
|
|
|
unsubscribe -- Unsubscribe from the JID.
|
|
|
|
send_presence -- Send a directed presence to the JID.
|
|
|
|
send_last_presence -- Resend the last sent presence.
|
|
|
|
handle_available -- Update the JID's resource information.
|
|
|
|
handle_unavailable -- Update the JID's resource information.
|
|
|
|
handle_subscribe -- Handle a subscription request.
|
|
|
|
handle_subscribed -- Handle a notice that a subscription request
|
|
|
|
was authorized by the JID.
|
|
|
|
handle_unsubscribe -- Handle an unsubscribe request.
|
|
|
|
handle_unsubscribed -- Handle a notice that a subscription was
|
|
|
|
removed by the JID.
|
|
|
|
handle_probe -- Handle a presence probe query.
|
|
|
|
"""
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def __init__(self, xmpp, jid, owner=None,
|
2011-06-16 23:03:31 +00:00
|
|
|
state=None, db=None, roster=None):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
|
|
|
Create a new roster item.
|
|
|
|
|
|
|
|
Arguments:
|
2011-06-16 23:03:31 +00:00
|
|
|
xmpp -- The main SleekXMPP instance.
|
|
|
|
jid -- The item's JID.
|
|
|
|
owner -- The roster owner's JID. Defaults
|
|
|
|
so self.xmpp.boundjid.bare.
|
|
|
|
state -- A dictionary of initial state values.
|
|
|
|
db -- An optional interface to an external datastore.
|
|
|
|
roster -- The roster object containing this entry.
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
2010-10-27 03:47:17 +00:00
|
|
|
self.xmpp = xmpp
|
|
|
|
self.jid = jid
|
2010-10-27 19:02:21 +00:00
|
|
|
self.owner = owner or self.xmpp.boundjid.bare
|
2010-10-27 03:47:17 +00:00
|
|
|
self.last_status = None
|
|
|
|
self.resources = {}
|
2011-06-16 23:03:31 +00:00
|
|
|
self.roster = roster
|
2010-10-27 12:09:50 +00:00
|
|
|
self.db = db
|
2010-10-27 03:47:17 +00:00
|
|
|
self._state = state or {
|
|
|
|
'from': False,
|
|
|
|
'to': False,
|
|
|
|
'pending_in': False,
|
|
|
|
'pending_out': False,
|
|
|
|
'whitelisted': False,
|
|
|
|
'subscription': 'none',
|
|
|
|
'name': '',
|
|
|
|
'groups': []}
|
2012-03-07 19:44:07 +00:00
|
|
|
|
2010-10-27 12:09:50 +00:00
|
|
|
self._db_state = {}
|
2010-10-27 03:47:17 +00:00
|
|
|
self.load()
|
|
|
|
|
2012-03-05 19:12:13 +00:00
|
|
|
def set_backend(self, db=None, save=True):
|
2011-02-02 17:05:34 +00:00
|
|
|
"""
|
|
|
|
Set the datastore interface object for the roster item.
|
|
|
|
|
|
|
|
Arguments:
|
2012-03-05 19:12:13 +00:00
|
|
|
db -- The new datastore interface.
|
|
|
|
save -- If True, save the existing state to the new
|
|
|
|
backend datastore. Defaults to True.
|
2011-02-02 17:05:34 +00:00
|
|
|
"""
|
|
|
|
self.db = db
|
2012-03-05 19:12:13 +00:00
|
|
|
if save:
|
|
|
|
self.save()
|
2011-02-02 17:05:34 +00:00
|
|
|
self.load()
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def load(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
|
|
|
Load the item's state information from an external datastore,
|
|
|
|
if one has been provided.
|
|
|
|
"""
|
2010-10-27 12:09:50 +00:00
|
|
|
if self.db:
|
|
|
|
item = self.db.load(self.owner, self.jid,
|
|
|
|
self._db_state)
|
2010-10-27 03:47:17 +00:00
|
|
|
if item:
|
|
|
|
self['name'] = item['name']
|
|
|
|
self['groups'] = item['groups']
|
|
|
|
self['from'] = item['from']
|
|
|
|
self['to'] = item['to']
|
|
|
|
self['whitelisted'] = item['whitelisted']
|
|
|
|
self['pending_out'] = item['pending_out']
|
|
|
|
self['pending_in'] = item['pending_in']
|
|
|
|
self['subscription'] = self._subscription()
|
|
|
|
return self._state
|
|
|
|
return None
|
|
|
|
|
2012-03-07 19:44:07 +00:00
|
|
|
def save(self, remove=False):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
|
|
|
Save the item's state information to an external datastore,
|
|
|
|
if one has been provided.
|
2012-03-07 19:44:07 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
remove -- If True, expunge the item from the datastore.
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
2011-11-28 23:00:35 +00:00
|
|
|
self['subscription'] = self._subscription()
|
2012-03-07 19:44:07 +00:00
|
|
|
if remove:
|
|
|
|
self._state['removed'] = True
|
2010-10-27 12:09:50 +00:00
|
|
|
if self.db:
|
|
|
|
self.db.save(self.owner, self.jid,
|
|
|
|
self._state, self._db_state)
|
2010-10-27 03:47:17 +00:00
|
|
|
|
2012-03-07 19:44:07 +00:00
|
|
|
# Finally, remove the in-memory copy if needed.
|
|
|
|
if remove:
|
|
|
|
del self.xmpp.roster[self.owner][self.jid]
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def __getitem__(self, key):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Return a state field's value."""
|
2010-10-27 03:47:17 +00:00
|
|
|
if key in self._state:
|
|
|
|
if key == 'subscription':
|
|
|
|
return self._subscription()
|
|
|
|
return self._state[key]
|
|
|
|
else:
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""
|
|
|
|
Set the value of a state field.
|
|
|
|
|
|
|
|
For boolean states, the values True, 'true', '1', 'on',
|
|
|
|
and 'yes' are accepted as True; all others are False.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
key -- The state field to modify.
|
|
|
|
value -- The new value of the state field.
|
|
|
|
"""
|
2010-10-27 03:47:17 +00:00
|
|
|
if key in self._state:
|
|
|
|
if key in ['name', 'subscription', 'groups']:
|
|
|
|
self._state[key] = value
|
|
|
|
else:
|
|
|
|
value = str(value).lower()
|
|
|
|
self._state[key] = value in ('true', '1', 'on', 'yes')
|
|
|
|
else:
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def _subscription(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Return the proper subscription type based on current state."""
|
2010-10-27 03:47:17 +00:00
|
|
|
if self['to'] and self['from']:
|
|
|
|
return 'both'
|
|
|
|
elif self['from']:
|
|
|
|
return 'from'
|
|
|
|
elif self['to']:
|
|
|
|
return 'to'
|
|
|
|
else:
|
|
|
|
return 'none'
|
|
|
|
|
|
|
|
def remove(self):
|
2010-10-27 13:27:00 +00:00
|
|
|
"""
|
2010-10-27 19:02:21 +00:00
|
|
|
Remove a JID's whitelisted status and unsubscribe if a
|
|
|
|
subscription exists.
|
2010-10-27 13:27:00 +00:00
|
|
|
"""
|
2010-10-27 03:47:17 +00:00
|
|
|
if self['to']:
|
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
2011-11-06 16:33:03 +00:00
|
|
|
p['type'] = 'unsubscribe'
|
2010-10-27 03:47:17 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
p.send()
|
|
|
|
self['to'] = False
|
|
|
|
self['whitelisted'] = False
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def subscribe(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Send a subscription request to the JID."""
|
2010-10-27 03:47:17 +00:00
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
|
|
|
p['type'] = 'subscribe'
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
self['pending_out'] = True
|
|
|
|
self.save()
|
|
|
|
p.send()
|
|
|
|
|
|
|
|
def authorize(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Authorize a received subscription request from the JID."""
|
2010-10-27 03:47:17 +00:00
|
|
|
self['from'] = True
|
|
|
|
self['pending_in'] = False
|
|
|
|
self.save()
|
|
|
|
self._subscribed()
|
|
|
|
self.send_last_presence()
|
|
|
|
|
|
|
|
def unauthorize(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Deny a received subscription request from the JID."""
|
2010-10-27 03:47:17 +00:00
|
|
|
self['from'] = False
|
|
|
|
self['pending_in'] = False
|
|
|
|
self.save()
|
|
|
|
self._unsubscribed()
|
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
|
|
|
p['type'] = 'unavailable'
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
p.send()
|
|
|
|
|
|
|
|
def _subscribed(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Handle acknowledging a subscription."""
|
2010-10-27 03:47:17 +00:00
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
|
|
|
p['type'] = 'subscribed'
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
p.send()
|
|
|
|
|
|
|
|
def unsubscribe(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Unsubscribe from the JID."""
|
2010-10-27 03:47:17 +00:00
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
|
|
|
p['type'] = 'unsubscribe'
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
self.save()
|
|
|
|
p.send()
|
|
|
|
|
|
|
|
def _unsubscribed(self):
|
2010-10-27 19:02:21 +00:00
|
|
|
"""Handle acknowledging an unsubscribe request."""
|
2010-10-27 03:47:17 +00:00
|
|
|
p = self.xmpp.Presence()
|
|
|
|
p['to'] = self.jid
|
|
|
|
p['type'] = 'unsubscribed'
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
|
|
|
p.send()
|
|
|
|
|
2011-06-16 23:03:31 +00:00
|
|
|
def send_presence(self, ptype=None, pshow=None, pstatus=None,
|
|
|
|
ppriority=None, pnick=None):
|
|
|
|
"""
|
|
|
|
Create, initialize, and send a Presence stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
pshow -- The presence's show value.
|
|
|
|
pstatus -- The presence's status message.
|
|
|
|
ppriority -- This connections' priority.
|
|
|
|
ptype -- The type of presence, such as 'subscribe'.
|
|
|
|
pnick -- Optional nickname of the presence's sender.
|
|
|
|
"""
|
|
|
|
p = self.xmpp.make_presence(pshow=pshow,
|
|
|
|
pstatus=pstatus,
|
|
|
|
ppriority=ppriority,
|
|
|
|
ptype=ptype,
|
|
|
|
pnick=pnick,
|
|
|
|
pto=self.jid)
|
2010-10-27 03:47:17 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
p['from'] = self.owner
|
2011-06-18 21:36:19 +00:00
|
|
|
if p['type'] in p.showtypes or \
|
|
|
|
p['type'] in ['available', 'unavailable']:
|
2011-06-16 23:03:31 +00:00
|
|
|
self.last_status = p
|
2010-10-27 03:47:17 +00:00
|
|
|
p.send()
|
|
|
|
|
2011-06-16 23:03:31 +00:00
|
|
|
if not self.xmpp.sentpresence:
|
|
|
|
self.xmpp.event('sent_presence')
|
|
|
|
self.xmpp.sentpresence = True
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def send_last_presence(self):
|
|
|
|
if self.last_status is None:
|
2011-06-16 23:03:31 +00:00
|
|
|
pres = self.roster.last_status
|
|
|
|
if pres is None:
|
|
|
|
self.send_presence()
|
|
|
|
else:
|
|
|
|
pres['to'] = self.jid
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
pres['from'] = self.owner
|
|
|
|
else:
|
|
|
|
del pres['from']
|
|
|
|
pres.send()
|
2010-10-27 03:47:17 +00:00
|
|
|
else:
|
|
|
|
self.last_status.send()
|
|
|
|
|
|
|
|
def handle_available(self, presence):
|
|
|
|
resource = presence['from'].resource
|
|
|
|
data = {'status': presence['status'],
|
|
|
|
'show': presence['show'],
|
|
|
|
'priority': presence['priority']}
|
2012-04-07 20:23:24 +00:00
|
|
|
got_online = not self.resources
|
2010-10-27 03:47:17 +00:00
|
|
|
if resource not in self.resources:
|
|
|
|
self.resources[resource] = {}
|
2011-11-18 21:57:41 +00:00
|
|
|
old_status = self.resources[resource].get('status', '')
|
2011-11-18 21:37:57 +00:00
|
|
|
old_show = self.resources[resource].get('show', None)
|
2010-10-27 03:47:17 +00:00
|
|
|
self.resources[resource].update(data)
|
2012-04-07 20:23:24 +00:00
|
|
|
if got_online:
|
|
|
|
self.xmpp.event('got_online', presence)
|
2011-11-18 21:57:41 +00:00
|
|
|
if old_show != presence['show'] or old_status != presence['status']:
|
2011-11-18 21:37:57 +00:00
|
|
|
self.xmpp.event('changed_status', presence)
|
2010-10-27 03:47:17 +00:00
|
|
|
|
|
|
|
def handle_unavailable(self, presence):
|
|
|
|
resource = presence['from'].resource
|
|
|
|
if not self.resources:
|
|
|
|
return
|
|
|
|
if resource in self.resources:
|
|
|
|
del self.resources[resource]
|
2011-11-18 21:37:57 +00:00
|
|
|
self.xmpp.event('changed_status', presence)
|
2010-10-27 03:47:17 +00:00
|
|
|
if not self.resources:
|
|
|
|
self.xmpp.event('got_offline', presence)
|
|
|
|
|
|
|
|
def handle_subscribe(self, presence):
|
|
|
|
"""
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| EXISTING STATE | DELIVER? | NEW STATE |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| "None" | yes | "None + Pending In" |
|
|
|
|
| "None + Pending Out" | yes | "None + Pending Out/In" |
|
|
|
|
| "None + Pending In" | no | no state change |
|
|
|
|
| "None + Pending Out/In" | no | no state change |
|
|
|
|
| "To" | yes | "To + Pending In" |
|
|
|
|
| "To + Pending In" | no | no state change |
|
|
|
|
| "From" | no * | no state change |
|
|
|
|
| "From + Pending Out" | no * | no state change |
|
|
|
|
| "Both" | no * | no state change |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
"""
|
2011-04-14 23:27:27 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
if not self['from'] and not self['pending_in']:
|
|
|
|
self['pending_in'] = True
|
|
|
|
self.xmpp.event('roster_subscription_request', presence)
|
|
|
|
elif self['from']:
|
|
|
|
self._subscribed()
|
|
|
|
self.save()
|
|
|
|
else:
|
|
|
|
#server shouldn't send an invalid subscription request
|
2010-10-27 03:47:17 +00:00
|
|
|
self.xmpp.event('roster_subscription_request', presence)
|
|
|
|
|
|
|
|
def handle_subscribed(self, presence):
|
|
|
|
"""
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| EXISTING STATE | DELIVER? | NEW STATE |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| "None" | no | no state change |
|
|
|
|
| "None + Pending Out" | yes | "To" |
|
|
|
|
| "None + Pending In" | no | no state change |
|
|
|
|
| "None + Pending Out/In" | yes | "To + Pending In" |
|
|
|
|
| "To" | no | no state change |
|
|
|
|
| "To + Pending In" | no | no state change |
|
|
|
|
| "From" | no | no state change |
|
|
|
|
| "From + Pending Out" | yes | "Both" |
|
|
|
|
| "Both" | no | no state change |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
"""
|
2011-04-14 23:27:27 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
if not self['to'] and self['pending_out']:
|
|
|
|
self['pending_out'] = False
|
|
|
|
self['to'] = True
|
|
|
|
self.xmpp.event('roster_subscription_authorized', presence)
|
|
|
|
self.save()
|
|
|
|
else:
|
2010-10-27 03:47:17 +00:00
|
|
|
self.xmpp.event('roster_subscription_authorized', presence)
|
|
|
|
|
|
|
|
def handle_unsubscribe(self, presence):
|
|
|
|
"""
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| EXISTING STATE | DELIVER? | NEW STATE |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| "None" | no | no state change |
|
|
|
|
| "None + Pending Out" | no | no state change |
|
|
|
|
| "None + Pending In" | yes * | "None" |
|
|
|
|
| "None + Pending Out/In" | yes * | "None + Pending Out" |
|
|
|
|
| "To" | no | no state change |
|
|
|
|
| "To + Pending In" | yes * | "To" |
|
|
|
|
| "From" | yes * | "None" |
|
|
|
|
| "From + Pending Out" | yes * | "None + Pending Out |
|
|
|
|
| "Both" | yes * | "To" |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
"""
|
2011-04-14 23:27:27 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
if not self['from'] and self['pending_in']:
|
|
|
|
self['pending_in'] = False
|
|
|
|
self._unsubscribed()
|
|
|
|
elif self['from']:
|
|
|
|
self['from'] = False
|
|
|
|
self._unsubscribed()
|
|
|
|
self.xmpp.event('roster_subscription_remove', presence)
|
|
|
|
self.save()
|
|
|
|
else:
|
2010-10-27 03:47:17 +00:00
|
|
|
self.xmpp.event('roster_subscription_remove', presence)
|
|
|
|
|
|
|
|
def handle_unsubscribed(self, presence):
|
|
|
|
"""
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| EXISTING STATE | DELIVER? | NEW STATE |
|
|
|
|
+------------------------------------------------------------------+
|
|
|
|
| "None" | no | no state change |
|
|
|
|
| "None + Pending Out" | yes | "None" |
|
|
|
|
| "None + Pending In" | no | no state change |
|
|
|
|
| "None + Pending Out/In" | yes | "None + Pending In" |
|
|
|
|
| "To" | yes | "None" |
|
|
|
|
| "To + Pending In" | yes | "None + Pending In" |
|
|
|
|
| "From" | no | no state change |
|
|
|
|
| "From + Pending Out" | yes | "From" |
|
|
|
|
| "Both" | yes | "From" |
|
|
|
|
+------------------------------------------------------------------
|
|
|
|
"""
|
2011-04-14 23:27:27 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
if not self['to'] and self['pending_out']:
|
|
|
|
self['pending_out'] = False
|
|
|
|
elif self['to'] and not self['pending_out']:
|
|
|
|
self['to'] = False
|
|
|
|
self.xmpp.event('roster_subscription_removed', presence)
|
|
|
|
self.save()
|
|
|
|
else:
|
2010-10-27 03:47:17 +00:00
|
|
|
self.xmpp.event('roster_subscription_removed', presence)
|
|
|
|
|
|
|
|
def handle_probe(self, presence):
|
|
|
|
if self['to']:
|
|
|
|
self.send_last_presence()
|
|
|
|
if self['pending_out']:
|
|
|
|
self.subscribe()
|
|
|
|
if not self['to']:
|
|
|
|
self._unsubscribed()
|
2011-02-14 21:24:49 +00:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Forgot current resource presence information as part of
|
|
|
|
a roster reset request.
|
|
|
|
"""
|
|
|
|
self.resources = {}
|
2012-01-11 04:03:22 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self._state)
|