2010-10-06 18:20:32 +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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import base64
|
|
|
|
import sys
|
|
|
|
import hashlib
|
|
|
|
import random
|
2010-10-21 02:18:27 +00:00
|
|
|
import threading
|
2010-10-06 18:20:32 +00:00
|
|
|
|
2011-06-30 22:40:22 +00:00
|
|
|
import sleekxmpp
|
2010-10-06 18:20:32 +00:00
|
|
|
from sleekxmpp import plugins
|
|
|
|
from sleekxmpp import stanza
|
2011-06-30 22:40:22 +00:00
|
|
|
from sleekxmpp import features
|
2010-10-06 18:20:32 +00:00
|
|
|
from sleekxmpp.basexmpp import BaseXMPP
|
2011-01-28 05:49:37 +00:00
|
|
|
from sleekxmpp.stanza import *
|
2011-06-28 18:06:44 +00:00
|
|
|
from sleekxmpp.stanza.stream import tls, sasl
|
2010-10-06 18:20:32 +00:00
|
|
|
from sleekxmpp.xmlstream import XMLStream, RestartStream
|
2011-01-28 05:49:37 +00:00
|
|
|
from sleekxmpp.xmlstream import StanzaBase, ET, register_stanza_plugin
|
2010-10-06 18:20:32 +00:00
|
|
|
from sleekxmpp.xmlstream.matcher import *
|
|
|
|
from sleekxmpp.xmlstream.handler import *
|
|
|
|
|
|
|
|
# Flag indicating if DNS SRV records are available for use.
|
|
|
|
SRV_SUPPORT = True
|
|
|
|
try:
|
|
|
|
import dns.resolver
|
|
|
|
except:
|
|
|
|
SRV_SUPPORT = False
|
|
|
|
|
2010-10-06 18:31:33 +00:00
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-10-06 18:20:32 +00:00
|
|
|
class ClientXMPP(BaseXMPP):
|
|
|
|
|
|
|
|
"""
|
|
|
|
SleekXMPP's client class.
|
|
|
|
|
|
|
|
Use only for good, not for evil.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
connect -- Overrides XMLStream.connect.
|
|
|
|
del_roster_item -- Delete a roster item.
|
|
|
|
get_roster -- Retrieve the roster from the server.
|
|
|
|
register_feature -- Register a stream feature.
|
|
|
|
update_roster -- Update a roster item.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, jid, password, ssl=False, plugin_config={},
|
|
|
|
plugin_whitelist=[], escape_quotes=True):
|
|
|
|
"""
|
|
|
|
Create a new SleekXMPP client.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID of the XMPP user account.
|
|
|
|
password -- The password for the XMPP user account.
|
|
|
|
ssl -- Deprecated.
|
|
|
|
plugin_config -- A dictionary of plugin configurations.
|
|
|
|
plugin_whitelist -- A list of approved plugins that will be loaded
|
|
|
|
when calling register_plugins.
|
|
|
|
escape_quotes -- Deprecated.
|
|
|
|
"""
|
|
|
|
BaseXMPP.__init__(self, 'jabber:client')
|
|
|
|
|
|
|
|
self.set_jid(jid)
|
|
|
|
self.password = password
|
|
|
|
self.escape_quotes = escape_quotes
|
|
|
|
self.plugin_config = plugin_config
|
|
|
|
self.plugin_whitelist = plugin_whitelist
|
|
|
|
self.srv_support = SRV_SUPPORT
|
|
|
|
|
|
|
|
self.stream_header = "<stream:stream to='%s' %s %s version='1.0'>" % (
|
2010-10-16 19:09:40 +00:00
|
|
|
self.boundjid.host,
|
2010-10-06 18:20:32 +00:00
|
|
|
"xmlns:stream='%s'" % self.stream_ns,
|
|
|
|
"xmlns='%s'" % self.default_ns)
|
|
|
|
self.stream_footer = "</stream:stream>"
|
|
|
|
|
|
|
|
self.features = []
|
2011-01-28 05:49:37 +00:00
|
|
|
self._stream_feature_handlers = {}
|
|
|
|
self._stream_feature_order = []
|
|
|
|
self._sasl_mechanism_handlers = {}
|
|
|
|
self._sasl_mechanism_priorities = []
|
2010-10-06 18:20:32 +00:00
|
|
|
|
|
|
|
#TODO: Use stream state here
|
|
|
|
self.authenticated = False
|
|
|
|
self.sessionstarted = False
|
|
|
|
self.bound = False
|
|
|
|
self.bindfail = False
|
2011-01-28 05:49:37 +00:00
|
|
|
|
|
|
|
self.add_event_handler('connected', self._handle_connected)
|
|
|
|
|
|
|
|
self.register_stanza(StreamFeatures)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
2010-10-06 22:45:11 +00:00
|
|
|
self.register_handler(
|
2010-10-06 18:20:32 +00:00
|
|
|
Callback('Stream Features',
|
|
|
|
MatchXPath('{%s}features' % self.stream_ns),
|
|
|
|
self._handle_stream_features))
|
2010-10-06 22:45:11 +00:00
|
|
|
self.register_handler(
|
2010-10-06 18:20:32 +00:00
|
|
|
Callback('Roster Update',
|
|
|
|
MatchXPath('{%s}iq/{%s}query' % (
|
|
|
|
self.default_ns,
|
|
|
|
'jabber:iq:roster')),
|
|
|
|
self._handle_roster))
|
2011-06-30 22:40:22 +00:00
|
|
|
|
|
|
|
# Setup default stream features
|
|
|
|
self.register_plugin('feature_starttls')
|
|
|
|
self.register_plugin('feature_mechanisms')
|
|
|
|
self.register_plugin('feature_bind')
|
|
|
|
self.register_plugin('feature_session')
|
|
|
|
|
|
|
|
# Setup default SASL mechanisms
|
|
|
|
self.register_plugin('sasl_plain',
|
|
|
|
{'priority': 1})
|
|
|
|
self.register_plugin('sasl_anonymous',
|
|
|
|
{'priority': 0})
|
2010-10-14 01:15:21 +00:00
|
|
|
|
2011-03-22 15:56:55 +00:00
|
|
|
def connect(self, address=tuple(), reattempt=True, use_tls=True):
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
|
|
|
Connect to the XMPP server.
|
|
|
|
|
|
|
|
When no address is given, a SRV lookup for the server will
|
|
|
|
be attempted. If that fails, the server user in the JID
|
|
|
|
will be used.
|
|
|
|
|
|
|
|
Arguments:
|
2011-01-07 21:41:31 +00:00
|
|
|
address -- A tuple containing the server's host and port.
|
|
|
|
reattempt -- If True, reattempt the connection if an
|
2011-03-22 15:59:27 +00:00
|
|
|
error occurs. Defaults to True.
|
|
|
|
use_tls -- Indicates if TLS should be used for the
|
|
|
|
connection. Defaults to True.
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
2010-10-21 02:18:27 +00:00
|
|
|
self.session_started_event.clear()
|
2010-10-06 18:20:32 +00:00
|
|
|
if not address or len(address) < 2:
|
|
|
|
if not self.srv_support:
|
2010-11-06 05:28:59 +00:00
|
|
|
log.debug("Did not supply (address, port) to connect" + \
|
2010-10-06 18:20:32 +00:00
|
|
|
" to and no SRV support is installed" + \
|
|
|
|
" (http://www.dnspython.org)." + \
|
|
|
|
" Continuing to attempt connection, using" + \
|
|
|
|
" server hostname from JID.")
|
|
|
|
else:
|
2010-11-06 05:28:59 +00:00
|
|
|
log.debug("Since no address is supplied," + \
|
2010-10-06 18:20:32 +00:00
|
|
|
"attempting SRV lookup.")
|
|
|
|
try:
|
2011-01-22 17:47:22 +00:00
|
|
|
xmpp_srv = "_xmpp-client._tcp.%s" % self.boundjid.host
|
2010-10-06 18:20:32 +00:00
|
|
|
answers = dns.resolver.query(xmpp_srv, dns.rdatatype.SRV)
|
2010-11-08 01:14:17 +00:00
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.debug("No appropriate SRV record found." + \
|
2010-10-06 18:20:32 +00:00
|
|
|
" Using JID server name.")
|
2011-01-19 20:23:00 +00:00
|
|
|
except (dns.exception.Timeout,):
|
|
|
|
log.debug("DNS resolution timed out.")
|
2010-10-06 18:20:32 +00:00
|
|
|
else:
|
|
|
|
# Pick a random server, weighted by priority.
|
|
|
|
|
|
|
|
addresses = {}
|
|
|
|
intmax = 0
|
|
|
|
for answer in answers:
|
|
|
|
intmax += answer.priority
|
|
|
|
addresses[intmax] = (answer.target.to_text()[:-1],
|
|
|
|
answer.port)
|
2010-10-14 17:58:07 +00:00
|
|
|
#python3 returns a generator for dictionary keys
|
|
|
|
priorities = [x for x in addresses.keys()]
|
2010-10-06 18:20:32 +00:00
|
|
|
priorities.sort()
|
|
|
|
|
|
|
|
picked = random.randint(0, intmax)
|
|
|
|
for priority in priorities:
|
|
|
|
if picked <= priority:
|
|
|
|
address = addresses[priority]
|
|
|
|
break
|
|
|
|
|
|
|
|
if not address:
|
|
|
|
# If all else fails, use the server from the JID.
|
2010-10-14 23:27:44 +00:00
|
|
|
address = (self.boundjid.host, 5222)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
2011-01-07 21:41:31 +00:00
|
|
|
return XMLStream.connect(self, address[0], address[1],
|
2011-03-22 15:56:55 +00:00
|
|
|
use_tls=use_tls, reattempt=reattempt)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
2011-01-28 05:49:37 +00:00
|
|
|
def register_feature(self, name, handler, restart=False, order=5000):
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
|
|
|
Register a stream feature.
|
|
|
|
|
|
|
|
Arguments:
|
2011-01-28 05:49:37 +00:00
|
|
|
name -- The name of the stream feature.
|
|
|
|
handler -- The function to execute if the feature is received.
|
|
|
|
restart -- Indicates if feature processing should halt with
|
2010-10-06 18:20:32 +00:00
|
|
|
this feature. Defaults to False.
|
2011-01-28 05:49:37 +00:00
|
|
|
order -- The relative ordering in which the feature should
|
|
|
|
be negotiated. Lower values will be attempted
|
|
|
|
earlier when available.
|
|
|
|
"""
|
|
|
|
self._stream_feature_handlers[name] = (handler, restart)
|
|
|
|
self._stream_feature_order.append((order, name))
|
|
|
|
self._stream_feature_order.sort()
|
|
|
|
|
|
|
|
def register_sasl_mechanism(self, name, handler, priority=0):
|
|
|
|
"""
|
|
|
|
Register a handler for a SASL authentication mechanism.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the mechanism (all caps)
|
|
|
|
handler -- The function that will perform the
|
|
|
|
authentication. The function must
|
|
|
|
return True if it is able to carry
|
|
|
|
out the authentication, False if
|
|
|
|
a required condition is not met.
|
|
|
|
priority -- An integer value indicating the
|
|
|
|
preferred ordering for the mechanism.
|
|
|
|
High values will be attempted first.
|
|
|
|
"""
|
2011-06-30 22:40:22 +00:00
|
|
|
self['feature_mechanisms'].register_mechanism(name, handler, priority)
|
2011-01-28 05:49:37 +00:00
|
|
|
|
|
|
|
def remove_sasl_mechanism(self, name):
|
|
|
|
"""
|
|
|
|
Remove support for a given SASL authentication mechanism.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the mechanism to remove (all caps)
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
2011-06-30 22:40:22 +00:00
|
|
|
self['feature_mechanisms'].remove_mechanism(name)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
def update_roster(self, jid, name=None, subscription=None, groups=[],
|
|
|
|
block=True, timeout=None, callback=None):
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
|
|
|
Add or change a roster item.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID of the entry to modify.
|
|
|
|
name -- The user's nickname for this JID.
|
|
|
|
subscription -- The subscription status. May be one of
|
|
|
|
'to', 'from', 'both', or 'none'. If set
|
|
|
|
to 'remove', the entry will be deleted.
|
|
|
|
groups -- The roster groups that contain this item.
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
block -- Specify if the roster request will block
|
|
|
|
until a response is received, or a timeout
|
|
|
|
occurs. Defaults to True.
|
|
|
|
timeout -- The length of time (in seconds) to wait
|
|
|
|
for a response before continuing if blocking
|
|
|
|
is used. Defaults to self.response_timeout.
|
|
|
|
callback -- Optional reference to a stream handler function.
|
|
|
|
Will be executed when the roster is received.
|
|
|
|
Implies block=False.
|
2010-10-06 18:20:32 +00:00
|
|
|
"""
|
2011-01-28 05:49:37 +00:00
|
|
|
iq = self.Iq()
|
|
|
|
iq['type'] = 'set'
|
2010-10-06 18:20:32 +00:00
|
|
|
iq['roster']['items'] = {jid: {'name': name,
|
|
|
|
'subscription': subscription,
|
|
|
|
'groups': groups}}
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
response = iq.send(block, timeout, callback)
|
2011-05-20 17:46:12 +00:00
|
|
|
if response in [False, None] or not isinstance(response, Iq):
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
return response
|
2010-10-24 16:08:59 +00:00
|
|
|
return response['type'] == 'result'
|
2010-10-06 18:20:32 +00:00
|
|
|
|
|
|
|
def del_roster_item(self, jid):
|
|
|
|
"""
|
|
|
|
Remove an item from the roster by setting its subscription
|
|
|
|
status to 'remove'.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID of the item to remove.
|
|
|
|
"""
|
|
|
|
return self.update_roster(jid, subscription='remove')
|
|
|
|
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
def get_roster(self, block=True, timeout=None, callback=None):
|
|
|
|
"""
|
|
|
|
Request the roster from the server.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
block -- Specify if the roster request will block until a
|
|
|
|
response is received, or a timeout occurs.
|
|
|
|
Defaults to True.
|
|
|
|
timeout -- The length of time (in seconds) to wait for a response
|
|
|
|
before continuing if blocking is used.
|
|
|
|
Defaults to self.response_timeout.
|
|
|
|
callback -- Optional reference to a stream handler function. Will
|
|
|
|
be executed when the roster is received.
|
|
|
|
Implies block=False.
|
|
|
|
"""
|
2011-01-28 05:49:37 +00:00
|
|
|
iq = self.Iq()
|
|
|
|
iq['type'] = 'get'
|
|
|
|
iq.enable('roster')
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
response = iq.send(block, timeout, callback)
|
|
|
|
|
|
|
|
if response == False:
|
|
|
|
self.event('roster_timeout')
|
|
|
|
|
|
|
|
if response in [False, None] or not isinstance(response, Iq):
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return self._handle_roster(response, request=True)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
2011-01-28 05:49:37 +00:00
|
|
|
def _handle_connected(self, event=None):
|
|
|
|
#TODO: Use stream state here
|
|
|
|
self.authenticated = False
|
|
|
|
self.sessionstarted = False
|
|
|
|
self.bound = False
|
|
|
|
self.bindfail = False
|
|
|
|
self.features = []
|
|
|
|
|
|
|
|
def session_timeout():
|
|
|
|
if not self.session_started_event.isSet():
|
|
|
|
log.debug("Session start has taken more than 15 seconds")
|
|
|
|
self.disconnect(reconnect=self.auto_reconnect)
|
|
|
|
|
|
|
|
self.schedule("session timeout checker", 15, session_timeout)
|
|
|
|
|
2010-10-06 18:20:32 +00:00
|
|
|
def _handle_stream_features(self, features):
|
|
|
|
"""
|
|
|
|
Process the received stream features.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
features -- The features stanza.
|
|
|
|
"""
|
2011-01-28 05:49:37 +00:00
|
|
|
for order, name in self._stream_feature_order:
|
|
|
|
if name in features['features']:
|
|
|
|
handler, restart = self._stream_feature_handlers[name]
|
|
|
|
if handler(features) and restart:
|
|
|
|
# Don't continue if the feature requires
|
|
|
|
# restarting the XML stream.
|
|
|
|
return True
|
|
|
|
|
2010-10-06 18:20:32 +00:00
|
|
|
def _handle_roster(self, iq, request=False):
|
|
|
|
"""
|
|
|
|
Update the roster after receiving a roster stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- The roster stanza.
|
|
|
|
request -- Indicates if this stanza is a response
|
|
|
|
to a request for the roster.
|
|
|
|
"""
|
|
|
|
if iq['type'] == 'set' or (iq['type'] == 'result' and request):
|
|
|
|
for jid in iq['roster']['items']:
|
|
|
|
if not jid in self.roster:
|
|
|
|
self.roster[jid] = {'groups': [],
|
|
|
|
'name': '',
|
|
|
|
'subscription': 'none',
|
|
|
|
'presence': {},
|
|
|
|
'in_roster': True}
|
|
|
|
self.roster[jid].update(iq['roster']['items'][jid])
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
self.event('roster_received', iq)
|
2010-10-06 18:20:32 +00:00
|
|
|
|
|
|
|
self.event("roster_update", iq)
|
|
|
|
if iq['type'] == 'set':
|
|
|
|
iq.reply()
|
|
|
|
iq.enable('roster')
|
|
|
|
iq.send()
|
Resolve timeout errors for get_roster.
See issue #89
Using get_roster will now return the same types of values as
Iq.send. If a timeout occurs, then the event 'roster_timeout'
will be fired. A successful call to get_roster will also
raise the 'roster_received' event.
To ensure that the get_roster call was successful, here
is a pattern to follow:
def __init__(self, ...):
...
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('roster_timeout', self.roster_timeout)
self.add_event_handler('roster_received', self.roster_received)
def session_start(self, e):
self.send_presence()
self.get_roster()
def roster_timeout(self, e):
# Optionally increase the timeout period
self.get_roster(timeout=self.response_timeout * 2)
def roster_received(self, iq):
# Do stuff, roster has been initialized.
...
2011-05-20 16:56:00 +00:00
|
|
|
return True
|
2011-02-14 18:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
ClientXMPP.updateRoster = ClientXMPP.update_roster
|
|
|
|
ClientXMPP.delRosterItem = ClientXMPP.del_roster_item
|
|
|
|
ClientXMPP.getRoster = ClientXMPP.get_roster
|
|
|
|
ClientXMPP.registerFeature = ClientXMPP.register_feature
|