2009-06-03 22:56:51 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
2010-03-26 21:32:16 +00:00
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
2009-06-03 22:56:51 +00:00
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-01-08 06:03:02 +00:00
|
|
|
from __future__ import with_statement, unicode_literals
|
2010-01-05 21:56:48 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
import sys
|
|
|
|
import copy
|
|
|
|
import logging
|
2010-01-05 21:56:48 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
import sleekxmpp
|
|
|
|
from sleekxmpp import plugins
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-10-27 12:09:50 +00:00
|
|
|
import sleekxmpp.roster as roster
|
2011-01-16 18:22:52 +00:00
|
|
|
from sleekxmpp.stanza import Message, Presence, Iq, Error, StreamError
|
2010-10-02 02:24:19 +00:00
|
|
|
from sleekxmpp.stanza.roster import Roster
|
|
|
|
from sleekxmpp.stanza.nick import Nick
|
|
|
|
from sleekxmpp.stanza.htmlim import HTMLIM
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
from sleekxmpp.xmlstream import XMLStream, JID, tostring
|
2010-10-18 01:38:22 +00:00
|
|
|
from sleekxmpp.xmlstream import ET, register_stanza_plugin
|
2010-10-02 02:24:19 +00:00
|
|
|
from sleekxmpp.xmlstream.matcher import *
|
|
|
|
from sleekxmpp.xmlstream.handler import *
|
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
# In order to make sure that Unicode is handled properly
|
|
|
|
# in Python 2.x, reset the default encoding.
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
reload(sys)
|
|
|
|
sys.setdefaultencoding('utf8')
|
|
|
|
|
|
|
|
|
|
|
|
class BaseXMPP(XMLStream):
|
|
|
|
|
|
|
|
"""
|
|
|
|
The BaseXMPP class adapts the generic XMLStream class for use
|
|
|
|
with XMPP. It also provides a plugin mechanism to easily extend
|
|
|
|
and add support for new XMPP features.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
auto_authorize -- Manage automatically accepting roster
|
|
|
|
subscriptions.
|
|
|
|
auto_subscribe -- Manage automatically requesting mutual
|
|
|
|
subscriptions.
|
|
|
|
is_component -- Indicates if this stream is for an XMPP component.
|
|
|
|
jid -- The XMPP JID for this stream.
|
|
|
|
plugin -- A dictionary of loaded plugins.
|
|
|
|
plugin_config -- A dictionary of plugin configurations.
|
|
|
|
plugin_whitelist -- A list of approved plugins.
|
|
|
|
sentpresence -- Indicates if an initial presence has been sent.
|
|
|
|
roster -- A dictionary containing subscribed JIDs and
|
|
|
|
their presence statuses.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
Iq -- Factory for creating an Iq stanzas.
|
|
|
|
Message -- Factory for creating Message stanzas.
|
|
|
|
Presence -- Factory for creating Presence stanzas.
|
|
|
|
get -- Return a plugin given its name.
|
|
|
|
make_iq -- Create and initialize an Iq stanza.
|
|
|
|
make_iq_error -- Create an Iq stanza of type 'error'.
|
|
|
|
make_iq_get -- Create an Iq stanza of type 'get'.
|
|
|
|
make_iq_query -- Create an Iq stanza with a given query.
|
|
|
|
make_iq_result -- Create an Iq stanza of type 'result'.
|
|
|
|
make_iq_set -- Create an Iq stanza of type 'set'.
|
|
|
|
make_message -- Create and initialize a Message stanza.
|
|
|
|
make_presence -- Create and initialize a Presence stanza.
|
|
|
|
make_query_roster -- Create a roster query.
|
|
|
|
process -- Overrides XMLStream.process.
|
|
|
|
register_plugin -- Load and configure a plugin.
|
|
|
|
register_plugins -- Load and configure multiple plugins.
|
|
|
|
send_message -- Create and send a Message stanza.
|
|
|
|
send_presence -- Create and send a Presence stanza.
|
|
|
|
send_presence_subscribe -- Send a subscription request.
|
|
|
|
"""
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def __init__(self, jid='', default_ns='jabber:client'):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Adapt an XML stream for use with XMPP.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
default_ns -- Ensure that the correct default XML namespace
|
|
|
|
is used during initialization.
|
|
|
|
"""
|
|
|
|
XMLStream.__init__(self)
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
self.default_ns = default_ns
|
2010-10-06 18:03:19 +00:00
|
|
|
self.stream_ns = 'http://etherx.jabber.org/streams'
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
self.boundjid = JID(jid)
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
self.plugin = {}
|
2010-11-18 20:50:45 +00:00
|
|
|
self.plugin_config = {}
|
|
|
|
self.plugin_whitelist = []
|
2010-12-13 19:36:53 +00:00
|
|
|
|
2010-10-27 12:09:50 +00:00
|
|
|
self.roster = roster.Roster(self)
|
|
|
|
self.roster.add(self.boundjid.bare)
|
2011-01-10 21:28:49 +00:00
|
|
|
self.client_roster = self.roster[self.boundjid.bare]
|
2010-10-27 03:47:17 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
self.is_component = False
|
|
|
|
self.auto_authorize = True
|
|
|
|
self.auto_subscribe = True
|
|
|
|
|
|
|
|
self.sentpresence = False
|
|
|
|
|
|
|
|
self.register_handler(
|
|
|
|
Callback('IM',
|
|
|
|
MatchXPath('{%s}message/{%s}body' % (self.default_ns,
|
|
|
|
self.default_ns)),
|
|
|
|
self._handle_message))
|
|
|
|
self.register_handler(
|
|
|
|
Callback('Presence',
|
|
|
|
MatchXPath("{%s}presence" % self.default_ns),
|
|
|
|
self._handle_presence))
|
2011-01-16 18:22:52 +00:00
|
|
|
self.register_handler(
|
|
|
|
Callback('Stream Error',
|
|
|
|
MatchXPath("{%s}error" % self.stream_ns),
|
|
|
|
self._handle_stream_error))
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-10-21 02:33:40 +00:00
|
|
|
self.add_event_handler('disconnected',
|
|
|
|
self._handle_disconnected)
|
2010-11-17 15:13:45 +00:00
|
|
|
self.add_event_handler('presence_available',
|
|
|
|
self._handle_available)
|
|
|
|
self.add_event_handler('presence_dnd',
|
|
|
|
self._handle_available)
|
|
|
|
self.add_event_handler('presence_xa',
|
|
|
|
self._handle_available)
|
|
|
|
self.add_event_handler('presence_chat',
|
|
|
|
self._handle_available)
|
|
|
|
self.add_event_handler('presence_away',
|
|
|
|
self._handle_available)
|
|
|
|
self.add_event_handler('presence_unavailable',
|
|
|
|
self._handle_unavailable)
|
|
|
|
self.add_event_handler('presence_subscribe',
|
|
|
|
self._handle_subscribe)
|
|
|
|
self.add_event_handler('presence_subscribed',
|
|
|
|
self._handle_subscribed)
|
|
|
|
self.add_event_handler('presence_unsubscribe',
|
|
|
|
self._handle_unsubscribe)
|
|
|
|
self.add_event_handler('presence_unsubscribed',
|
|
|
|
self._handle_unsubscribed)
|
|
|
|
self.add_event_handler('presence_probe',
|
|
|
|
self._handle_probe)
|
|
|
|
self.add_event_handler('roster_subscription_request',
|
|
|
|
self._handle_new_subscription)
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
# Set up the XML stream with XMPP's root stanzas.
|
2010-11-17 15:13:45 +00:00
|
|
|
self.register_stanza(Message)
|
|
|
|
self.register_stanza(Iq)
|
|
|
|
self.register_stanza(Presence)
|
2011-01-16 18:22:52 +00:00
|
|
|
self.register_stanza(StreamError)
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
# Initialize a few default stanza plugins.
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(Iq, Roster)
|
|
|
|
register_stanza_plugin(Message, Nick)
|
|
|
|
register_stanza_plugin(Message, HTMLIM)
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
def process(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Ensure that plugin inter-dependencies are handled before starting
|
|
|
|
event processing.
|
|
|
|
|
|
|
|
Overrides XMLStream.process.
|
|
|
|
"""
|
|
|
|
for name in self.plugin:
|
|
|
|
if not self.plugin[name].post_inited:
|
|
|
|
self.plugin[name].post_init()
|
|
|
|
return XMLStream.process(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def register_plugin(self, plugin, pconfig={}, module=None):
|
|
|
|
"""
|
|
|
|
Register and configure a plugin for use in this stream.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
plugin -- The name of the plugin class. Plugin names must
|
|
|
|
be unique.
|
|
|
|
pconfig -- A dictionary of configuration data for the plugin.
|
|
|
|
Defaults to an empty dictionary.
|
|
|
|
module -- Optional refence to the module containing the plugin
|
|
|
|
class if using custom plugins.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Import the given module that contains the plugin.
|
|
|
|
if not module:
|
|
|
|
module = sleekxmpp.plugins
|
|
|
|
module = __import__("%s.%s" % (module.__name__, plugin),
|
|
|
|
globals(), locals(), [plugin])
|
2010-10-17 19:47:24 +00:00
|
|
|
if isinstance(module, str):
|
|
|
|
# We probably want to load a module from outside
|
|
|
|
# the sleekxmpp package, so leave out the globals().
|
|
|
|
module = __import__(module, fromlist=[plugin])
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
# Load the plugin class from the module.
|
|
|
|
self.plugin[plugin] = getattr(module, plugin)(self, pconfig)
|
|
|
|
|
|
|
|
# Let XEP implementing plugins have some extra logging info.
|
|
|
|
xep = ''
|
|
|
|
if hasattr(self.plugin[plugin], 'xep'):
|
|
|
|
xep = "(XEP-%s) " % self.plugin[plugin].xep
|
|
|
|
|
|
|
|
desc = (xep, self.plugin[plugin].description)
|
2010-11-06 05:28:59 +00:00
|
|
|
log.debug("Loaded Plugin %s%s" % desc)
|
2010-10-02 02:24:19 +00:00
|
|
|
except:
|
2010-11-06 05:28:59 +00:00
|
|
|
log.exception("Unable to load plugin: %s", plugin)
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
def register_plugins(self):
|
|
|
|
"""
|
|
|
|
Register and initialize all built-in plugins.
|
|
|
|
|
|
|
|
Optionally, the list of plugins loaded may be limited to those
|
|
|
|
contained in self.plugin_whitelist.
|
|
|
|
|
|
|
|
Plugin configurations stored in self.plugin_config will be used.
|
|
|
|
"""
|
|
|
|
if self.plugin_whitelist:
|
|
|
|
plugin_list = self.plugin_whitelist
|
|
|
|
else:
|
|
|
|
plugin_list = plugins.__all__
|
|
|
|
|
|
|
|
for plugin in plugin_list:
|
|
|
|
if plugin in plugins.__all__:
|
|
|
|
self.register_plugin(plugin,
|
|
|
|
self.plugin_config.get(plugin, {}))
|
|
|
|
else:
|
|
|
|
raise NameError("Plugin %s not in plugins.__all__." % plugin)
|
|
|
|
|
|
|
|
# Resolve plugin inter-dependencies.
|
|
|
|
for plugin in self.plugin:
|
|
|
|
self.plugin[plugin].post_init()
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""
|
|
|
|
Return a plugin given its name, if it has been registered.
|
|
|
|
"""
|
|
|
|
if key in self.plugin:
|
|
|
|
return self.plugin[key]
|
|
|
|
else:
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("""Plugin "%s" is not loaded.""" % key)
|
2010-10-02 02:24:19 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def get(self, key, default):
|
|
|
|
"""
|
|
|
|
Return a plugin given its name, if it has been registered.
|
|
|
|
"""
|
|
|
|
return self.plugin.get(key, default)
|
|
|
|
|
|
|
|
def Message(self, *args, **kwargs):
|
|
|
|
"""Create a Message stanza associated with this stream."""
|
|
|
|
return Message(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def Iq(self, *args, **kwargs):
|
|
|
|
"""Create an Iq stanza associated with this stream."""
|
|
|
|
return Iq(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def Presence(self, *args, **kwargs):
|
|
|
|
"""Create a Presence stanza associated with this stream."""
|
|
|
|
return Presence(self, *args, **kwargs)
|
|
|
|
|
2010-12-28 21:17:08 +00:00
|
|
|
def make_iq(self, id=0, ifrom=None, ito=None, itype=None, iquery=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create a new Iq stanza with a given Id and from JID.
|
|
|
|
|
|
|
|
Arguments:
|
2010-12-29 20:01:50 +00:00
|
|
|
id -- An ideally unique ID value for this stanza thread.
|
|
|
|
Defaults to 0.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
|
|
|
ito -- The destination JID for this stanza.
|
2010-12-28 21:32:28 +00:00
|
|
|
itype -- The Iq's type, one of: get, set, result, or error.
|
|
|
|
iquery -- Optional namespace for adding a query element.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
2010-12-16 20:18:06 +00:00
|
|
|
iq = self.Iq()
|
|
|
|
iq['id'] = str(id)
|
|
|
|
iq['to'] = ito
|
|
|
|
iq['from'] = ifrom
|
|
|
|
iq['type'] = itype
|
2010-12-28 21:17:08 +00:00
|
|
|
iq['query'] = iquery
|
2010-12-16 20:18:06 +00:00
|
|
|
return iq
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-12-16 20:18:06 +00:00
|
|
|
def make_iq_get(self, queryxmlns=None, ito=None, ifrom=None, iq=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create an Iq stanza of type 'get'.
|
|
|
|
|
|
|
|
Optionally, a query element may be added.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
queryxmlns -- The namespace of the query to use.
|
2010-12-16 20:18:06 +00:00
|
|
|
ito -- The destination JID for this stanza.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
|
|
|
iq -- Optionally use an existing stanza instead
|
|
|
|
of generating a new one.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
2010-12-16 20:18:06 +00:00
|
|
|
if not iq:
|
|
|
|
iq = self.Iq()
|
|
|
|
iq['type'] = 'get'
|
|
|
|
iq['query'] = queryxmlns
|
|
|
|
if ito:
|
|
|
|
iq['to'] = ito
|
|
|
|
if ifrom:
|
|
|
|
iq['from'] = ifrom
|
|
|
|
return iq
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-12-16 20:18:06 +00:00
|
|
|
def make_iq_result(self, id=None, ito=None, ifrom=None, iq=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create an Iq stanza of type 'result' with the given ID value.
|
|
|
|
|
|
|
|
Arguments:
|
2010-12-16 20:18:06 +00:00
|
|
|
id -- An ideally unique ID value. May use self.new_id().
|
|
|
|
ito -- The destination JID for this stanza.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
|
|
|
iq -- Optionally use an existing stanza instead
|
|
|
|
of generating a new one.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
2010-12-16 20:18:06 +00:00
|
|
|
if not iq:
|
|
|
|
iq = self.Iq()
|
|
|
|
if id is None:
|
|
|
|
id = self.new_id()
|
|
|
|
iq['id'] = id
|
|
|
|
iq['type'] = 'result'
|
|
|
|
if ito:
|
|
|
|
iq['to'] = ito
|
|
|
|
if ifrom:
|
|
|
|
iq['from'] = ifrom
|
|
|
|
return iq
|
2010-10-02 02:24:19 +00:00
|
|
|
|
2010-12-16 20:18:06 +00:00
|
|
|
def make_iq_set(self, sub=None, ito=None, ifrom=None, iq=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create an Iq stanza of type 'set'.
|
|
|
|
|
|
|
|
Optionally, a substanza may be given to use as the
|
|
|
|
stanza's payload.
|
|
|
|
|
|
|
|
Arguments:
|
2010-12-16 20:18:06 +00:00
|
|
|
sub -- A stanza or XML object to use as the Iq's payload.
|
|
|
|
ito -- The destination JID for this stanza.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
|
|
|
iq -- Optionally use an existing stanza instead
|
|
|
|
of generating a new one.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
2010-12-16 20:18:06 +00:00
|
|
|
if not iq:
|
|
|
|
iq = self.Iq()
|
|
|
|
iq['type'] = 'set'
|
2010-10-02 02:24:19 +00:00
|
|
|
if sub != None:
|
|
|
|
iq.append(sub)
|
2010-12-16 20:18:06 +00:00
|
|
|
if ito:
|
|
|
|
iq['to'] = ito
|
|
|
|
if ifrom:
|
|
|
|
iq['from'] = ifrom
|
2010-10-02 02:24:19 +00:00
|
|
|
return iq
|
|
|
|
|
|
|
|
def make_iq_error(self, id, type='cancel',
|
2010-12-16 20:18:06 +00:00
|
|
|
condition='feature-not-implemented',
|
|
|
|
text=None, ito=None, ifrom=None, iq=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create an Iq stanza of type 'error'.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
id -- An ideally unique ID value. May use self.new_id().
|
|
|
|
type -- The type of the error, such as 'cancel' or 'modify'.
|
|
|
|
Defaults to 'cancel'.
|
|
|
|
condition -- The error condition.
|
|
|
|
Defaults to 'feature-not-implemented'.
|
|
|
|
text -- A message describing the cause of the error.
|
2010-12-16 20:18:06 +00:00
|
|
|
ito -- The destination JID for this stanza.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
|
|
|
iq -- Optionally use an existing stanza instead
|
|
|
|
of generating a new one.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
2010-12-16 20:18:06 +00:00
|
|
|
if not iq:
|
|
|
|
iq = self.Iq()
|
|
|
|
iq['id'] = id
|
|
|
|
iq['error']['type'] = type
|
|
|
|
iq['error']['condition'] = condition
|
|
|
|
iq['error']['text'] = text
|
|
|
|
if ito:
|
|
|
|
iq['to'] = ito
|
|
|
|
if ifrom:
|
|
|
|
iq['from'] = ifrom
|
2010-10-02 02:24:19 +00:00
|
|
|
return iq
|
|
|
|
|
2010-12-16 20:18:06 +00:00
|
|
|
def make_iq_query(self, iq=None, xmlns='', ito=None, ifrom=None):
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
Create or modify an Iq stanza to use the given
|
|
|
|
query namespace.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- Optional Iq stanza to modify. A new
|
|
|
|
stanza is created otherwise.
|
|
|
|
xmlns -- The query's namespace.
|
2010-12-16 20:18:06 +00:00
|
|
|
ito -- The destination JID for this stanza.
|
|
|
|
ifrom -- The from JID to use for this stanza.
|
2010-10-02 02:24:19 +00:00
|
|
|
"""
|
|
|
|
if not iq:
|
|
|
|
iq = self.Iq()
|
|
|
|
iq['query'] = xmlns
|
2010-12-16 20:18:06 +00:00
|
|
|
if ito:
|
|
|
|
iq['to'] = ito
|
|
|
|
if ifrom:
|
|
|
|
iq['from'] = ifrom
|
2010-10-02 02:24:19 +00:00
|
|
|
return iq
|
|
|
|
|
|
|
|
def make_query_roster(self, iq=None):
|
|
|
|
"""
|
|
|
|
Create a roster query element.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- Optional Iq stanza to modify. A new stanza
|
|
|
|
is created otherwise.
|
|
|
|
"""
|
|
|
|
if iq:
|
|
|
|
iq['query'] = 'jabber:iq:roster'
|
|
|
|
return ET.Element("{jabber:iq:roster}query")
|
|
|
|
|
|
|
|
def make_message(self, mto, mbody=None, msubject=None, mtype=None,
|
|
|
|
mhtml=None, mfrom=None, mnick=None):
|
|
|
|
"""
|
|
|
|
Create and initialize a new Message stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
mto -- The recipient of the message.
|
|
|
|
mbody -- The main contents of the message.
|
|
|
|
msubject -- Optional subject for the message.
|
|
|
|
mtype -- The message's type, such as 'chat' or 'groupchat'.
|
|
|
|
mhtml -- Optional HTML body content.
|
|
|
|
mfrom -- The sender of the message. If sending from a client,
|
|
|
|
be aware that some servers require that the full JID
|
|
|
|
of the sender be used.
|
|
|
|
mnick -- Optional nickname of the sender.
|
|
|
|
"""
|
|
|
|
message = self.Message(sto=mto, stype=mtype, sfrom=mfrom)
|
|
|
|
message['body'] = mbody
|
|
|
|
message['subject'] = msubject
|
|
|
|
if mnick is not None:
|
|
|
|
message['nick'] = mnick
|
|
|
|
if mhtml is not None:
|
|
|
|
message['html']['body'] = mhtml
|
|
|
|
return message
|
|
|
|
|
|
|
|
def make_presence(self, pshow=None, pstatus=None, ppriority=None,
|
|
|
|
pto=None, ptype=None, pfrom=None):
|
|
|
|
"""
|
|
|
|
Create and initialize a new Presence stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
pshow -- The presence's show value.
|
|
|
|
pstatus -- The presence's status message.
|
|
|
|
ppriority -- This connections' priority.
|
|
|
|
pto -- The recipient of a directed presence.
|
|
|
|
ptype -- The type of presence, such as 'subscribe'.
|
|
|
|
pfrom -- The sender of the presence.
|
|
|
|
"""
|
|
|
|
presence = self.Presence(stype=ptype, sfrom=pfrom, sto=pto)
|
|
|
|
if pshow is not None:
|
|
|
|
presence['type'] = pshow
|
|
|
|
if pfrom is None:
|
2010-10-16 19:09:40 +00:00
|
|
|
presence['from'] = self.boundjid.full
|
2010-10-02 02:24:19 +00:00
|
|
|
presence['priority'] = ppriority
|
|
|
|
presence['status'] = pstatus
|
|
|
|
return presence
|
|
|
|
|
|
|
|
def send_message(self, mto, mbody, msubject=None, mtype=None,
|
|
|
|
mhtml=None, mfrom=None, mnick=None):
|
|
|
|
"""
|
|
|
|
Create, initialize, and send a Message stanza.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.makeMessage(mto, mbody, msubject, mtype,
|
|
|
|
mhtml, mfrom, mnick).send()
|
|
|
|
|
|
|
|
def send_presence(self, pshow=None, pstatus=None, ppriority=None,
|
|
|
|
pto=None, pfrom=None, ptype=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.
|
|
|
|
pto -- The recipient of a directed presence.
|
|
|
|
ptype -- The type of presence, such as 'subscribe'.
|
|
|
|
pfrom -- The sender of the presence.
|
|
|
|
"""
|
|
|
|
self.makePresence(pshow, pstatus, ppriority, pto,
|
|
|
|
ptype=ptype, pfrom=pfrom).send()
|
|
|
|
# Unexpected errors may occur if
|
|
|
|
if not self.sentpresence:
|
|
|
|
self.event('sent_presence')
|
|
|
|
self.sentpresence = True
|
|
|
|
|
|
|
|
def send_presence_subscription(self, pto, pfrom=None,
|
|
|
|
ptype='subscribe', pnick=None):
|
|
|
|
"""
|
|
|
|
Create, initialize, and send a Presence stanza of type 'subscribe'.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
pto -- The recipient of a directed presence.
|
|
|
|
pfrom -- The sender of the presence.
|
|
|
|
ptype -- The type of presence. Defaults to 'subscribe'.
|
|
|
|
pnick -- Nickname of the presence's sender.
|
|
|
|
"""
|
|
|
|
presence = self.makePresence(ptype=ptype,
|
|
|
|
pfrom=pfrom,
|
|
|
|
pto=self.getjidbare(pto))
|
|
|
|
if pnick:
|
|
|
|
nick = ET.Element('{http://jabber.org/protocol/nick}nick')
|
|
|
|
nick.text = pnick
|
|
|
|
presence.append(nick)
|
|
|
|
presence.send()
|
|
|
|
|
2010-10-14 22:50:54 +00:00
|
|
|
@property
|
|
|
|
def jid(self):
|
|
|
|
"""
|
|
|
|
Attribute accessor for bare jid
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("jid property deprecated. Use boundjid.bare")
|
2010-10-14 22:50:54 +00:00
|
|
|
return self.boundjid.bare
|
|
|
|
|
|
|
|
@jid.setter
|
|
|
|
def jid(self, value):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("jid property deprecated. Use boundjid.bare")
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.bare = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fulljid(self):
|
|
|
|
"""
|
|
|
|
Attribute accessor for full jid
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("fulljid property deprecated. Use boundjid.full")
|
2010-10-14 22:50:54 +00:00
|
|
|
return self.boundjid.full
|
|
|
|
|
|
|
|
@fulljid.setter
|
|
|
|
def fulljid(self, value):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("fulljid property deprecated. Use boundjid.full")
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.full = value
|
2010-10-17 01:15:31 +00:00
|
|
|
|
2010-10-14 22:50:54 +00:00
|
|
|
@property
|
|
|
|
def resource(self):
|
|
|
|
"""
|
|
|
|
Attribute accessor for jid resource
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("resource property deprecated. Use boundjid.resource")
|
2010-10-14 22:50:54 +00:00
|
|
|
return self.boundjid.resource
|
|
|
|
|
|
|
|
@resource.setter
|
|
|
|
def resource(self, value):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("fulljid property deprecated. Use boundjid.full")
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.resource = value
|
2010-10-17 01:15:31 +00:00
|
|
|
|
2010-10-14 22:50:54 +00:00
|
|
|
@property
|
|
|
|
def username(self):
|
|
|
|
"""
|
|
|
|
Attribute accessor for jid usernode
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("username property deprecated. Use boundjid.user")
|
2010-10-14 22:50:54 +00:00
|
|
|
return self.boundjid.user
|
|
|
|
|
|
|
|
@username.setter
|
|
|
|
def username(self, value):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("username property deprecated. Use boundjid.user")
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.user = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def server(self):
|
|
|
|
"""
|
|
|
|
Attribute accessor for jid host
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("server property deprecated. Use boundjid.host")
|
2010-10-14 22:50:54 +00:00
|
|
|
return self.boundjid.server
|
|
|
|
|
|
|
|
@server.setter
|
|
|
|
def server(self, value):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.warning("server property deprecated. Use boundjid.host")
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.server = value
|
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
def set_jid(self, jid):
|
|
|
|
"""Rip a JID apart and claim it as our own."""
|
2010-11-06 05:28:59 +00:00
|
|
|
log.debug("setting jid to %s" % jid)
|
2010-10-14 22:50:54 +00:00
|
|
|
self.boundjid.full = jid
|
2010-10-02 02:24:19 +00:00
|
|
|
|
|
|
|
def getjidresource(self, fulljid):
|
|
|
|
if '/' in fulljid:
|
|
|
|
return fulljid.split('/', 1)[-1]
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def getjidbare(self, fulljid):
|
|
|
|
return fulljid.split('/', 1)[0]
|
|
|
|
|
2010-10-21 02:33:40 +00:00
|
|
|
def _handle_disconnected(self, event):
|
|
|
|
"""When disconnected, reset the roster"""
|
|
|
|
self.roster = {}
|
|
|
|
|
2011-01-16 18:22:52 +00:00
|
|
|
def _handle_stream_error(self, error):
|
|
|
|
self.event('stream_error', error)
|
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
def _handle_message(self, msg):
|
|
|
|
"""Process incoming message stanzas."""
|
|
|
|
self.event('message', msg)
|
|
|
|
|
2010-10-27 03:47:17 +00:00
|
|
|
def _handle_available(self, presence):
|
2010-11-17 15:13:45 +00:00
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_available(presence)
|
2010-10-27 03:47:17 +00:00
|
|
|
|
|
|
|
def _handle_unavailable(self, presence):
|
2010-11-17 15:13:45 +00:00
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_unavailable(presence)
|
2010-10-27 03:47:17 +00:00
|
|
|
|
|
|
|
def _handle_new_subscription(self, stanza):
|
2010-11-17 15:13:45 +00:00
|
|
|
"""
|
|
|
|
Attempt to automatically handle subscription requests.
|
|
|
|
|
|
|
|
Subscriptions will be approved if the request is from
|
|
|
|
a whitelisted JID, of self.auto_authorize is True. They
|
|
|
|
will be rejected if self.auto_authorize is False. Setting
|
|
|
|
self.auto_authorize to None will disable automatic
|
|
|
|
subscription handling (except for whitelisted JIDs).
|
|
|
|
|
|
|
|
If a subscription is accepted, a request for a mutual
|
|
|
|
subscription will be sent if self.auto_subscribe is True.
|
|
|
|
"""
|
2010-10-27 12:09:50 +00:00
|
|
|
roster = self.roster[stanza['to'].bare]
|
|
|
|
item = self.roster[stanza['to'].bare][stanza['from'].bare]
|
2010-10-27 03:47:17 +00:00
|
|
|
if item['whitelisted']:
|
|
|
|
item.authorize()
|
|
|
|
elif roster.auto_authorize:
|
|
|
|
item.authorize()
|
|
|
|
if roster.auto_subscribe:
|
|
|
|
item.subscribe()
|
|
|
|
elif roster.auto_authorize == False:
|
|
|
|
item.unauthorize()
|
|
|
|
|
|
|
|
def _handle_removed_subscription(self, presence):
|
2010-11-17 15:13:45 +00:00
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].unauthorize()
|
|
|
|
|
|
|
|
def _handle_subscribe(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_subscribe(presence)
|
|
|
|
|
|
|
|
def _handle_subscribed(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_subscribed(presence)
|
|
|
|
|
|
|
|
def _handle_unsubscribe(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_unsubscribe(presence)
|
|
|
|
|
|
|
|
def _handle_unsubscribed(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_unsubscribed(presence)
|
|
|
|
|
|
|
|
def _handle_probe(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_probe(presence)
|
2010-10-27 03:47:17 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
def _handle_presence(self, presence):
|
|
|
|
"""
|
|
|
|
Process incoming presence stanzas.
|
|
|
|
|
|
|
|
Update the roster with presence information.
|
|
|
|
"""
|
2010-10-27 03:47:17 +00:00
|
|
|
logging.debug(presence['type'])
|
2010-10-02 02:24:19 +00:00
|
|
|
self.event("presence_%s" % presence['type'], presence)
|
|
|
|
|
|
|
|
# Check for changes in subscription state.
|
|
|
|
if presence['type'] in ('subscribe', 'subscribed',
|
|
|
|
'unsubscribe', 'unsubscribed'):
|
|
|
|
self.event('changed_subscription', presence)
|
|
|
|
return
|
|
|
|
elif not presence['type'] in ('available', 'unavailable') and \
|
|
|
|
not presence['type'] in presence.showtypes:
|
|
|
|
return
|
|
|
|
self.event("changed_status", presence)
|
2010-02-25 01:12:15 +00:00
|
|
|
|
2010-10-02 02:24:19 +00:00
|
|
|
# Restore the old, lowercased name for backwards compatibility.
|
|
|
|
basexmpp = BaseXMPP
|
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.
|
|
|
|
BaseXMPP.registerPlugin = BaseXMPP.register_plugin
|
|
|
|
BaseXMPP.makeIq = BaseXMPP.make_iq
|
|
|
|
BaseXMPP.makeIqGet = BaseXMPP.make_iq_get
|
|
|
|
BaseXMPP.makeIqResult = BaseXMPP.make_iq_result
|
|
|
|
BaseXMPP.makeIqSet = BaseXMPP.make_iq_set
|
|
|
|
BaseXMPP.makeIqError = BaseXMPP.make_iq_error
|
|
|
|
BaseXMPP.makeIqQuery = BaseXMPP.make_iq_query
|
|
|
|
BaseXMPP.makeQueryRoster = BaseXMPP.make_query_roster
|
|
|
|
BaseXMPP.makeMessage = BaseXMPP.make_message
|
|
|
|
BaseXMPP.makePresence = BaseXMPP.make_presence
|
|
|
|
BaseXMPP.sendMessage = BaseXMPP.send_message
|
|
|
|
BaseXMPP.sendPresence = BaseXMPP.send_presence
|
|
|
|
BaseXMPP.sendPresenceSubscription = BaseXMPP.send_presence_subscription
|