2011-12-06 04:37:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2011-12-06 04:37:47 +00:00
|
|
|
sleekxmpp.clientxmpp
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
This module provides XMPP functionality that
|
|
|
|
is specific to external server component connections.
|
|
|
|
|
|
|
|
Part of SleekXMPP: The Sleek XMPP Library
|
|
|
|
|
|
|
|
:copyright: (c) 2011 Nathanael C. Fritz
|
|
|
|
:license: MIT, see LICENSE for more details
|
2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
from __future__ import absolute_import
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
2009-08-31 23:02:19 +00:00
|
|
|
import hashlib
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2010-11-08 01:14:17 +00:00
|
|
|
from sleekxmpp.basexmpp import BaseXMPP
|
2012-02-17 22:41:31 +00:00
|
|
|
from sleekxmpp.xmlstream import XMLStream
|
|
|
|
from sleekxmpp.xmlstream import ET
|
|
|
|
from sleekxmpp.xmlstream.matcher import MatchXPath
|
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
2010-10-06 14:47:05 +00:00
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-10-06 14:47:05 +00:00
|
|
|
class ComponentXMPP(BaseXMPP):
|
|
|
|
|
|
|
|
"""
|
|
|
|
SleekXMPP's basic XMPP server component.
|
|
|
|
|
|
|
|
Use only for good, not for evil.
|
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
:param jid: The JID of the component.
|
|
|
|
:param secret: The secret or password for the component.
|
|
|
|
:param host: The server accepting the component.
|
|
|
|
:param port: The port used to connect to the server.
|
|
|
|
:param plugin_config: A dictionary of plugin configurations.
|
2012-06-19 08:29:48 +00:00
|
|
|
:param plugin_whitelist: A list of approved plugins that
|
|
|
|
will be loaded when calling
|
2011-12-06 04:37:47 +00:00
|
|
|
:meth:`~sleekxmpp.basexmpp.BaseXMPP.register_plugins()`.
|
|
|
|
:param use_jc_ns: Indicates if the ``'jabber:client'`` namespace
|
|
|
|
should be used instead of the standard
|
|
|
|
``'jabber:component:accept'`` namespace.
|
|
|
|
Defaults to ``False``.
|
2010-10-06 14:47:05 +00:00
|
|
|
"""
|
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
def __init__(self, jid, secret, host=None, port=None,
|
2010-10-06 14:47:05 +00:00
|
|
|
plugin_config={}, plugin_whitelist=[], use_jc_ns=False):
|
|
|
|
if use_jc_ns:
|
|
|
|
default_ns = 'jabber:client'
|
|
|
|
else:
|
|
|
|
default_ns = 'jabber:component:accept'
|
2010-10-27 03:47:17 +00:00
|
|
|
BaseXMPP.__init__(self, jid, default_ns)
|
2010-10-06 14:47:05 +00:00
|
|
|
|
|
|
|
self.auto_authorize = None
|
|
|
|
self.stream_header = "<stream:stream %s %s to='%s'>" % (
|
|
|
|
'xmlns="jabber:component:accept"',
|
2010-10-06 18:03:19 +00:00
|
|
|
'xmlns:stream="%s"' % self.stream_ns,
|
2010-10-06 14:47:05 +00:00
|
|
|
jid)
|
|
|
|
self.stream_footer = "</stream:stream>"
|
|
|
|
self.server_host = host
|
|
|
|
self.server_port = port
|
|
|
|
self.secret = secret
|
2010-10-27 03:47:17 +00:00
|
|
|
|
2010-10-24 21:33:11 +00:00
|
|
|
self.plugin_config = plugin_config
|
|
|
|
self.plugin_whitelist = plugin_whitelist
|
2010-10-06 14:47:05 +00:00
|
|
|
self.is_component = True
|
|
|
|
|
|
|
|
self.register_handler(
|
|
|
|
Callback('Handshake',
|
|
|
|
MatchXPath('{jabber:component:accept}handshake'),
|
|
|
|
self._handle_handshake))
|
2011-04-14 23:27:27 +00:00
|
|
|
self.add_event_handler('presence_probe',
|
|
|
|
self._handle_probe)
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2012-06-19 08:29:48 +00:00
|
|
|
def connect(self, host=None, port=None, use_ssl=False,
|
2012-06-09 17:43:57 +00:00
|
|
|
use_tls=False, reattempt=True):
|
2011-12-06 04:37:47 +00:00
|
|
|
"""Connect to the server.
|
|
|
|
|
|
|
|
Setting ``reattempt`` to ``True`` will cause connection attempts to
|
|
|
|
be made every second until a successful connection is established.
|
|
|
|
|
|
|
|
:param host: The name of the desired server for the connection.
|
|
|
|
Defaults to :attr:`server_host`.
|
|
|
|
:param port: Port to connect to on the server.
|
|
|
|
Defauts to :attr:`server_port`.
|
|
|
|
:param use_ssl: Flag indicating if SSL should be used by connecting
|
|
|
|
directly to a port using SSL.
|
|
|
|
:param use_tls: Flag indicating if TLS should be used, allowing for
|
|
|
|
connecting to a port without using SSL immediately and
|
|
|
|
later upgrading the connection.
|
|
|
|
:param reattempt: Flag indicating if the socket should reconnect
|
|
|
|
after disconnections.
|
2010-10-06 14:47:05 +00:00
|
|
|
"""
|
2011-12-06 04:37:47 +00:00
|
|
|
if host is None:
|
|
|
|
host = self.server_host
|
|
|
|
if port is None:
|
|
|
|
port = self.server_port
|
2012-05-22 10:56:06 +00:00
|
|
|
|
|
|
|
self.server_name = self.boundjid.host
|
|
|
|
|
2012-06-09 17:47:27 +00:00
|
|
|
if use_tls:
|
|
|
|
log.info("XEP-0114 components can not use TLS")
|
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
log.debug("Connecting to %s:%s", host, port)
|
|
|
|
return XMLStream.connect(self, host=host, port=port,
|
|
|
|
use_ssl=use_ssl,
|
2012-06-09 17:47:27 +00:00
|
|
|
use_tls=False,
|
2011-12-06 04:37:47 +00:00
|
|
|
reattempt=reattempt)
|
2010-10-06 14:47:05 +00:00
|
|
|
|
|
|
|
def incoming_filter(self, xml):
|
|
|
|
"""
|
2011-12-06 04:37:47 +00:00
|
|
|
Pre-process incoming XML stanzas by converting any
|
|
|
|
``'jabber:client'`` namespaced elements to the component's
|
|
|
|
default namespace.
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
:param xml: The XML stanza to pre-process.
|
2010-10-06 14:47:05 +00:00
|
|
|
"""
|
|
|
|
if xml.tag.startswith('{jabber:client}'):
|
|
|
|
xml.tag = xml.tag.replace('jabber:client', self.default_ns)
|
|
|
|
|
|
|
|
# The incoming_filter call is only made on top level stanza
|
|
|
|
# elements. So we manually continue filtering on sub-elements.
|
|
|
|
for sub in xml:
|
|
|
|
self.incoming_filter(sub)
|
|
|
|
|
|
|
|
return xml
|
|
|
|
|
|
|
|
def start_stream_handler(self, xml):
|
|
|
|
"""
|
|
|
|
Once the streams are established, attempt to handshake
|
|
|
|
with the server to be accepted as a component.
|
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
:param xml: The incoming stream's root element.
|
2010-10-06 14:47:05 +00:00
|
|
|
"""
|
2011-08-06 07:44:32 +00:00
|
|
|
BaseXMPP.start_stream_handler(self, xml)
|
|
|
|
|
2010-10-06 14:47:05 +00:00
|
|
|
# Construct a hash of the stream ID and the component secret.
|
|
|
|
sid = xml.get('id', '')
|
|
|
|
pre_hash = '%s%s' % (sid, self.secret)
|
|
|
|
if sys.version_info >= (3, 0):
|
|
|
|
# Handle Unicode byte encoding in Python 3.
|
|
|
|
pre_hash = bytes(pre_hash, 'utf-8')
|
|
|
|
|
|
|
|
handshake = ET.Element('{jabber:component:accept}handshake')
|
|
|
|
handshake.text = hashlib.sha1(pre_hash).hexdigest().lower()
|
2011-06-08 17:00:01 +00:00
|
|
|
self.send_xml(handshake, now=True)
|
2010-10-06 14:47:05 +00:00
|
|
|
|
|
|
|
def _handle_handshake(self, xml):
|
2011-12-06 04:37:47 +00:00
|
|
|
"""The handshake has been accepted.
|
2010-10-06 14:47:05 +00:00
|
|
|
|
2011-12-06 04:37:47 +00:00
|
|
|
:param xml: The reply handshake stanza.
|
2010-10-06 14:47:05 +00:00
|
|
|
"""
|
2011-05-27 23:59:52 +00:00
|
|
|
self.session_started_event.set()
|
2010-10-06 14:47:05 +00:00
|
|
|
self.event("session_start")
|
2011-04-14 23:27:27 +00:00
|
|
|
|
|
|
|
def _handle_probe(self, presence):
|
|
|
|
pto = presence['to'].bare
|
|
|
|
pfrom = presence['from'].bare
|
|
|
|
self.roster[pto][pfrom].handle_probe(presence)
|