summaryrefslogtreecommitdiff
path: root/sleekxmpp
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp')
-rw-r--r--sleekxmpp/basexmpp.py26
-rw-r--r--sleekxmpp/clientxmpp.py279
-rw-r--r--sleekxmpp/features/__init__.py11
-rw-r--r--sleekxmpp/features/feature_bind/__init__.py10
-rw-r--r--sleekxmpp/features/feature_bind/bind.py62
-rw-r--r--sleekxmpp/features/feature_bind/stanza.py22
-rw-r--r--sleekxmpp/features/feature_mechanisms/__init__.py10
-rw-r--r--sleekxmpp/features/feature_mechanisms/mechanisms.py116
-rw-r--r--sleekxmpp/features/feature_mechanisms/stanza.py104
-rw-r--r--sleekxmpp/features/feature_session/__init__.py10
-rw-r--r--sleekxmpp/features/feature_session/session.py54
-rw-r--r--sleekxmpp/features/feature_session/stanza.py21
-rw-r--r--sleekxmpp/features/feature_starttls/__init__.py10
-rw-r--r--sleekxmpp/features/feature_starttls/stanza.py47
-rw-r--r--sleekxmpp/features/feature_starttls/starttls.py66
-rw-r--r--sleekxmpp/features/sasl_anonymous.py31
-rw-r--r--sleekxmpp/features/sasl_plain.py41
-rw-r--r--sleekxmpp/plugins/base.py3
-rw-r--r--sleekxmpp/stanza/__init__.py3
-rw-r--r--sleekxmpp/stanza/stream/__init__.py8
-rw-r--r--sleekxmpp/stanza/stream_features.py52
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py7
-rw-r--r--sleekxmpp/xmlstream/tostring/tostring.py22
-rw-r--r--sleekxmpp/xmlstream/tostring/tostring26.py22
24 files changed, 812 insertions, 225 deletions
diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py
index a7d4931a..b188e767 100644
--- a/sleekxmpp/basexmpp.py
+++ b/sleekxmpp/basexmpp.py
@@ -92,6 +92,7 @@ class BaseXMPP(XMLStream):
# Deprecated method names are re-mapped for backwards compatibility.
self.default_ns = default_ns
self.stream_ns = 'http://etherx.jabber.org/streams'
+ self.namespace_map[self.stream_ns] = 'stream'
self.boundjid = JID("")
@@ -105,6 +106,8 @@ class BaseXMPP(XMLStream):
self.sentpresence = False
+ self.stanza = sleekxmpp.stanza
+
self.register_handler(
Callback('IM',
MatchXPath('{%s}message/{%s}body' % (self.default_ns,
@@ -162,9 +165,14 @@ class BaseXMPP(XMLStream):
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])
+ try:
+ module = sleekxmpp.plugins
+ module = __import__(str("%s.%s" % (module.__name__, plugin)),
+ globals(), locals(), [str(plugin)])
+ except ImportError:
+ module = sleekxmpp.features
+ module = __import__(str("%s.%s" % (module.__name__, plugin)),
+ globals(), locals(), [str(plugin)])
if isinstance(module, str):
# We probably want to load a module from outside
# the sleekxmpp package, so leave out the globals().
@@ -173,12 +181,14 @@ class BaseXMPP(XMLStream):
# 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
+ # Let XEP/RFC implementing plugins have some extra logging info.
+ spec = '(CUSTOM) '
+ if self.plugin[plugin].xep:
+ spec = "(XEP-%s) " % self.plugin[plugin].xep
+ elif self.plugin[plugin].rfc:
+ spec = "(RFC-%s) " % self.plugin[plugin].rfc
- desc = (xep, self.plugin[plugin].description)
+ desc = (spec, self.plugin[plugin].description)
log.debug("Loaded Plugin %s%s" % desc)
except:
log.exception("Unable to load plugin: %s", plugin)
diff --git a/sleekxmpp/clientxmpp.py b/sleekxmpp/clientxmpp.py
index fb5b2087..7245053f 100644
--- a/sleekxmpp/clientxmpp.py
+++ b/sleekxmpp/clientxmpp.py
@@ -15,12 +15,15 @@ import hashlib
import random
import threading
+import sleekxmpp
from sleekxmpp import plugins
from sleekxmpp import stanza
+from sleekxmpp import features
from sleekxmpp.basexmpp import BaseXMPP
-from sleekxmpp.stanza import Message, Presence, Iq
+from sleekxmpp.stanza import *
+from sleekxmpp.stanza.stream import tls, sasl
from sleekxmpp.xmlstream import XMLStream, RestartStream
-from sleekxmpp.xmlstream import StanzaBase, ET
+from sleekxmpp.xmlstream import StanzaBase, ET, register_stanza_plugin
from sleekxmpp.xmlstream.matcher import *
from sleekxmpp.xmlstream.handler import *
@@ -82,14 +85,18 @@ class ClientXMPP(BaseXMPP):
self.stream_footer = "</stream:stream>"
self.features = []
- self.registered_features = []
+ self._stream_feature_handlers = {}
+ self._stream_feature_order = []
#TODO: Use stream state here
self.authenticated = False
self.sessionstarted = False
self.bound = False
self.bindfail = False
- self.add_event_handler('connected', self.handle_connected)
+
+ self.add_event_handler('connected', self._handle_connected)
+
+ self.register_stanza(StreamFeatures)
self.register_handler(
Callback('Stream Features',
@@ -102,32 +109,17 @@ class ClientXMPP(BaseXMPP):
'jabber:iq:roster')),
self._handle_roster))
- self.register_feature(
- "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />",
- self._handle_starttls, True)
- self.register_feature(
- "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />",
- self._handle_sasl_auth, True)
- self.register_feature(
- "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind' />",
- self._handle_bind_resource)
- self.register_feature(
- "<session xmlns='urn:ietf:params:xml:ns:xmpp-session' />",
- self._handle_start_session)
-
- def handle_connected(self, event=None):
- #TODO: Use stream state here
- self.authenticated = False
- self.sessionstarted = False
- self.bound = False
- self.bindfail = False
- self.schedule("session timeout checker", 15,
- self._session_timeout_check)
+ # Setup default stream features
+ self.register_plugin('feature_starttls')
+ self.register_plugin('feature_mechanisms')
+ self.register_plugin('feature_bind')
+ self.register_plugin('feature_session')
- def _session_timeout_check(self):
- if not self.session_started_event.isSet():
- log.debug("Session start has taken more than 15 seconds")
- self.disconnect(reconnect=self.auto_reconnect)
+ # Setup default SASL mechanisms
+ self.register_plugin('sasl_plain',
+ {'priority': 1})
+ self.register_plugin('sasl_anonymous',
+ {'priority': 0})
def connect(self, address=tuple(), reattempt=True, use_tls=True):
"""
@@ -189,19 +181,48 @@ class ClientXMPP(BaseXMPP):
return XMLStream.connect(self, address[0], address[1],
use_tls=use_tls, reattempt=reattempt)
- def register_feature(self, mask, pointer, breaker=False):
+ def register_feature(self, name, handler, restart=False, order=5000):
"""
Register a stream feature.
Arguments:
- mask -- An XML string matching the feature's element.
- pointer -- The function to execute if the feature is received.
- breaker -- Indicates if feature processing should halt with
+ 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
this feature. Defaults to False.
+ 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.
+ """
+ self['feature_mechanisms'].register_mechanism(name, handler, priority)
+
+ 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)
"""
- self.registered_features.append((MatchXMLMask(mask),
- pointer,
- breaker))
+ self['feature_mechanisms'].remove_mechanism(name)
def update_roster(self, jid, name=None, subscription=None, groups=[],
block=True, timeout=None, callback=None):
@@ -273,179 +294,35 @@ class ClientXMPP(BaseXMPP):
else:
return self._handle_roster(response, request=True)
- def _handle_stream_features(self, features):
- """
- Process the received stream features.
-
- Arguments:
- features -- The features stanza.
- """
- # Record all of the features.
- self.features = []
- for sub in features.xml:
- self.features.append(sub.tag)
-
- # Process the features.
- for sub in features.xml:
- for feature in self.registered_features:
- mask, handler, halt = feature
- if mask.match(sub):
- if handler(sub) and halt:
- # Don't continue if the feature was
- # marked as a breaker.
- return True
-
- def _handle_starttls(self, xml):
- """
- Handle notification that the server supports TLS.
-
- Arguments:
- xml -- The STARTLS proceed element.
- """
- if not self.use_tls:
- return False
- elif not self.authenticated and self.ssl_support:
- tls_ns = 'urn:ietf:params:xml:ns:xmpp-tls'
- self.add_handler("<proceed xmlns='%s' />" % tls_ns,
- self._handle_tls_start,
- name='TLS Proceed',
- instream=True)
- self.send_xml(xml, now=True)
- return True
- else:
- log.warning("The module tlslite is required to log in" +\
- " to some servers, and has not been found.")
- return False
-
- def _handle_tls_start(self, xml):
- """
- Handle encrypting the stream using TLS.
-
- Restarts the stream.
- """
- log.debug("Starting TLS")
- if self.start_tls():
- raise RestartStream()
-
- def _handle_sasl_auth(self, xml):
- """
- Handle authenticating using SASL.
-
- Arguments:
- xml -- The SASL mechanisms stanza.
- """
- if self.use_tls and \
- '{urn:ietf:params:xml:ns:xmpp-tls}starttls' in self.features:
- return False
-
- log.debug("Starting SASL Auth")
- sasl_ns = 'urn:ietf:params:xml:ns:xmpp-sasl'
- self.add_handler("<success xmlns='%s' />" % sasl_ns,
- self._handle_auth_success,
- name='SASL Sucess',
- instream=True)
- self.add_handler("<failure xmlns='%s' />" % sasl_ns,
- self._handle_auth_fail,
- name='SASL Failure',
- instream=True)
-
- sasl_mechs = xml.findall('{%s}mechanism' % sasl_ns)
- if sasl_mechs:
- for sasl_mech in sasl_mechs:
- self.features.append("sasl:%s" % sasl_mech.text)
- if 'sasl:PLAIN' in self.features and self.boundjid.user:
- if sys.version_info < (3, 0):
- user = bytes(self.boundjid.user)
- password = bytes(self.password)
- else:
- user = bytes(self.boundjid.user, 'utf-8')
- password = bytes(self.password, 'utf-8')
-
- auth = base64.b64encode(b'\x00' + user + \
- b'\x00' + password).decode('utf-8')
-
- self.send("<auth xmlns='%s' mechanism='PLAIN'>%s</auth>" % (
- sasl_ns,
- auth),
- now=True)
- elif 'sasl:ANONYMOUS' in self.features and not self.boundjid.user:
- self.send("<auth xmlns='%s' mechanism='%s' />" % (
- sasl_ns,
- 'ANONYMOUS'),
- now=True)
- else:
- log.error("No appropriate login method.")
- self.disconnect()
- return True
-
- def _handle_auth_success(self, xml):
- """
- SASL authentication succeeded. Restart the stream.
-
- Arguments:
- xml -- The SASL authentication success element.
- """
- self.authenticated = True
+ 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 = []
- raise RestartStream()
- def _handle_auth_fail(self, xml):
- """
- SASL authentication failed. Disconnect and shutdown.
+ 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)
- Arguments:
- xml -- The SASL authentication failure element.
- """
- log.info("Authentication failed.")
- self.event("failed_auth", direct=True)
- self.disconnect()
+ self.schedule("session timeout checker", 15, session_timeout)
- def _handle_bind_resource(self, xml):
- """
- Handle requesting a specific resource.
-
- Arguments:
- xml -- The bind feature element.
- """
- log.debug("Requesting resource: %s" % self.boundjid.resource)
- xml.clear()
- iq = self.Iq(stype='set')
- if self.boundjid.resource:
- res = ET.Element('resource')
- res.text = self.boundjid.resource
- xml.append(res)
- iq.append(xml)
- response = iq.send(now=True)
-
- bind_ns = 'urn:ietf:params:xml:ns:xmpp-bind'
- self.set_jid(response.xml.find('{%s}bind/{%s}jid' % (bind_ns,
- bind_ns)).text)
- self.bound = True
- log.info("Node set to: %s" % self.boundjid.full)
- session_ns = 'urn:ietf:params:xml:ns:xmpp-session'
- if "{%s}session" % session_ns not in self.features or self.bindfail:
- log.debug("Established Session")
- self.sessionstarted = True
- self.session_started_event.set()
- self.event("session_start")
-
- def _handle_start_session(self, xml):
+ def _handle_stream_features(self, features):
"""
- Handle the start of the session.
+ Process the received stream features.
Arguments:
- xml -- The session feature element.
+ features -- The features stanza.
"""
- if self.authenticated and self.bound:
- iq = self.makeIqSet(xml)
- response = iq.send(now=True)
- log.debug("Established Session")
- self.sessionstarted = True
- self.session_started_event.set()
- self.event("session_start")
- else:
- # Bind probably hasn't happened yet.
- self.bindfail = True
+ 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
def _handle_roster(self, iq, request=False):
"""
diff --git a/sleekxmpp/features/__init__.py b/sleekxmpp/features/__init__.py
new file mode 100644
index 00000000..65d2bdbf
--- /dev/null
+++ b/sleekxmpp/features/__init__.py
@@ -0,0 +1,11 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+__all__ = ['feature_starttls', 'feature_mechanisms',
+ 'feature_bind', 'feature_session',
+ 'sasl_plain', 'sasl_anonymous']
diff --git a/sleekxmpp/features/feature_bind/__init__.py b/sleekxmpp/features/feature_bind/__init__.py
new file mode 100644
index 00000000..fce94dd6
--- /dev/null
+++ b/sleekxmpp/features/feature_bind/__init__.py
@@ -0,0 +1,10 @@
+"""
+ 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 sleekxmpp.features.feature_bind.bind import feature_bind
+from sleekxmpp.features.feature_bind.stanza import Bind
diff --git a/sleekxmpp/features/feature_bind/bind.py b/sleekxmpp/features/feature_bind/bind.py
new file mode 100644
index 00000000..e177d7b2
--- /dev/null
+++ b/sleekxmpp/features/feature_bind/bind.py
@@ -0,0 +1,62 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp.stanza import Iq, StreamFeatures
+from sleekxmpp.features.feature_bind import stanza
+from sleekxmpp.xmlstream import register_stanza_plugin
+from sleekxmpp.xmlstream.matcher import *
+from sleekxmpp.xmlstream.handler import *
+from sleekxmpp.plugins.base import base_plugin
+
+
+log = logging.getLogger(__name__)
+
+
+class feature_bind(base_plugin):
+
+ def plugin_init(self):
+ self.name = 'Bind Resource'
+ self.rfc = '6120'
+ self.description = 'Resource Binding Stream Feature'
+ self.stanza = stanza
+
+ self.xmpp.register_feature('bind',
+ self._handle_bind_resource,
+ restart=False,
+ order=10000)
+
+ register_stanza_plugin(Iq, stanza.Bind)
+ register_stanza_plugin(StreamFeatures, stanza.Bind)
+
+ def _handle_bind_resource(self, features):
+ """
+ Handle requesting a specific resource.
+
+ Arguments:
+ features -- The stream features stanza.
+ """
+ log.debug("Requesting resource: %s" % self.xmpp.boundjid.resource)
+ iq = self.xmpp.Iq()
+ iq['type'] = 'set'
+ iq.enable('bind')
+ if self.xmpp.boundjid.resource:
+ iq['bind']['resource'] = self.xmpp.boundjid.resource
+ response = iq.send(now=True)
+
+ self.xmpp.set_jid(response['bind']['jid'])
+ self.xmpp.bound = True
+
+ log.info("Node set to: %s" % self.xmpp.boundjid.full)
+
+ if 'session' not in features['features']:
+ log.debug("Established Session")
+ self.xmpp.sessionstarted = True
+ self.xmpp.session_started_event.set()
+ self.xmpp.event("session_start")
diff --git a/sleekxmpp/features/feature_bind/stanza.py b/sleekxmpp/features/feature_bind/stanza.py
new file mode 100644
index 00000000..f3e025fa
--- /dev/null
+++ b/sleekxmpp/features/feature_bind/stanza.py
@@ -0,0 +1,22 @@
+"""
+ 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 sleekxmpp.stanza import Iq, StreamFeatures
+from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
+
+
+class Bind(ElementBase):
+
+ """
+ """
+
+ name = 'bind'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-bind'
+ interfaces = set(('resource', 'jid'))
+ sub_interfaces = interfaces
+ plugin_attrib = 'bind'
diff --git a/sleekxmpp/features/feature_mechanisms/__init__.py b/sleekxmpp/features/feature_mechanisms/__init__.py
new file mode 100644
index 00000000..a93b2b6f
--- /dev/null
+++ b/sleekxmpp/features/feature_mechanisms/__init__.py
@@ -0,0 +1,10 @@
+"""
+ 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 sleekxmpp.features.feature_mechanisms.mechanisms import feature_mechanisms
+from sleekxmpp.features.feature_mechanisms.stanza import *
diff --git a/sleekxmpp/features/feature_mechanisms/mechanisms.py b/sleekxmpp/features/feature_mechanisms/mechanisms.py
new file mode 100644
index 00000000..994c9bed
--- /dev/null
+++ b/sleekxmpp/features/feature_mechanisms/mechanisms.py
@@ -0,0 +1,116 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp.stanza import stream
+from sleekxmpp.xmlstream import RestartStream
+from sleekxmpp.xmlstream.matcher import *
+from sleekxmpp.xmlstream.handler import *
+from sleekxmpp.plugins.base import base_plugin
+
+
+log = logging.getLogger(__name__)
+
+
+class feature_mechanisms(base_plugin):
+
+ def plugin_init(self):
+ self.name = 'SASL Mechanisms'
+ self.rfc = '6120'
+ self.description = "SASL Stream Feature"
+
+ self.xmpp.register_stanza(stream.sasl.Success)
+ self.xmpp.register_stanza(stream.sasl.Failure)
+ self.xmpp.register_stanza(stream.sasl.Auth)
+
+ self._mechanism_handlers = {}
+ self._mechanism_priorities = []
+
+ self.xmpp.register_handler(
+ Callback('SASL Success',
+ MatchXPath(stream.sasl.Success.tag_name()),
+ self._handle_success,
+ instream=True,
+ once=True))
+ self.xmpp.register_handler(
+ Callback('SASL Failure',
+ MatchXPath(stream.sasl.Failure.tag_name()),
+ self._handle_fail,
+ instream=True,
+ once=True))
+
+ self.xmpp.register_feature('mechanisms',
+ self._handle_sasl_auth,
+ restart=True,
+ order=self.config.get('order', 100))
+
+ def register_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.
+ """
+ self._mechanism_handlers[name] = handler
+ self._mechanism_priorities.append((priority, name))
+ self._mechanism_priorities.sort(reverse=True)
+
+ def remove_mechanism(self, name):
+ """
+ Remove support for a given SASL authentication mechanism.
+
+ Arguments:
+ name -- The name of the mechanism to remove (all caps)
+ """
+ if name in self._mechanism_handlers:
+ del self._mechanism_handlers[name]
+
+ p = self._mechanism_priorities
+ self._mechanism_priorities = [i for i in p if i[1] != name]
+
+ def _handle_sasl_auth(self, features):
+ """
+ Handle authenticating using SASL.
+
+ Arguments:
+ features -- The stream features stanza.
+ """
+ for priority, mech in self._mechanism_priorities:
+ if mech in features['mechanisms']:
+ log.debug('Attempt to use SASL %s' % mech)
+ if self._mechanism_handlers[mech]():
+ break
+ else:
+ log.error("No appropriate login method.")
+ self.xmpp.event("no_auth", direct=True)
+ self.xmpp.disconnect()
+
+ return True
+
+ def _handle_success(self, stanza):
+ """SASL authentication succeeded. Restart the stream."""
+ self.xmpp.authenticated = True
+ self.xmpp.features.append('mechanisms')
+ raise RestartStream()
+
+ def _handle_fail(self, stanza):
+ """SASL authentication failed. Disconnect and shutdown."""
+ log.info("Authentication failed.")
+ self.xmpp.event("failed_auth", direct=True)
+ self.xmpp.disconnect()
+ log.debug("Starting SASL Auth")
+ return True
diff --git a/sleekxmpp/features/feature_mechanisms/stanza.py b/sleekxmpp/features/feature_mechanisms/stanza.py
new file mode 100644
index 00000000..e55a72ad
--- /dev/null
+++ b/sleekxmpp/features/feature_mechanisms/stanza.py
@@ -0,0 +1,104 @@
+"""
+ 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 sleekxmpp.stanza import StreamFeatures
+from sleekxmpp.xmlstream import ElementBase, StanzaBase, ET
+from sleekxmpp.xmlstream import register_stanza_plugin
+
+
+class Mechanisms(ElementBase):
+
+ """
+ """
+
+ name = 'mechanisms'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-sasl'
+ interfaces = set(('mechanisms', 'required'))
+ plugin_attrib = name
+ is_extension = True
+
+ def get_required(self):
+ """
+ """
+ return True
+
+ def get_mechanisms(self):
+ """
+ """
+ results = []
+ mechs = self.findall('{%s}mechanism' % self.namespace)
+ if mechs:
+ for mech in mechs:
+ results.append(mech.text)
+ return results
+
+ def set_mechanisms(self, values):
+ """
+ """
+ self.del_mechanisms()
+ for val in values:
+ mech = ET.Element('{%s}mechanism' % self.namespace)
+ mech.text = val
+ self.append(mech)
+
+ def del_mechanisms(self):
+ """
+ """
+ mechs = self.findall('{%s}mechanism' % self.namespace)
+ if mechs:
+ for mech in mechs:
+ self.xml.remove(mech)
+
+
+class Success(StanzaBase):
+
+ """
+ """
+
+ name = 'success'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-sasl'
+ interfaces = set()
+ plugin_attrib = name
+
+
+class Failure(StanzaBase):
+
+ """
+ """
+
+ name = 'failure'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-sasl'
+ interfaces = set()
+ plugin_attrib = name
+
+
+class Auth(StanzaBase):
+
+ """
+ """
+
+ name = 'auth'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-sasl'
+ interfaces = set(('mechanism', 'value'))
+ plugin_attrib = name
+
+ def setup(self, xml):
+ StanzaBase.setup(self, xml)
+ self.xml.tag = self.tag_name()
+
+ def set_value(self, value):
+ self.xml.text = value
+
+ def get_value(self):
+ return self.xml.text
+
+ def del_value(self):
+ self.xml.text = ''
+
+
+register_stanza_plugin(StreamFeatures, Mechanisms)
diff --git a/sleekxmpp/features/feature_session/__init__.py b/sleekxmpp/features/feature_session/__init__.py
new file mode 100644
index 00000000..1399f73b
--- /dev/null
+++ b/sleekxmpp/features/feature_session/__init__.py
@@ -0,0 +1,10 @@
+"""
+ 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 sleekxmpp.features.feature_session.session import feature_session
+from sleekxmpp.features.feature_session.stanza import Session
diff --git a/sleekxmpp/features/feature_session/session.py b/sleekxmpp/features/feature_session/session.py
new file mode 100644
index 00000000..4d17b2d2
--- /dev/null
+++ b/sleekxmpp/features/feature_session/session.py
@@ -0,0 +1,54 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp.stanza import Iq, StreamFeatures
+from sleekxmpp.xmlstream import register_stanza_plugin
+from sleekxmpp.xmlstream.matcher import *
+from sleekxmpp.xmlstream.handler import *
+from sleekxmpp.plugins.base import base_plugin
+
+from sleekxmpp.features.feature_session import stanza
+
+
+log = logging.getLogger(__name__)
+
+
+class feature_session(base_plugin):
+
+ def plugin_init(self):
+ self.name = 'Start Session'
+ self.rfc = '3920'
+ self.description = 'Start Session Stream Feature'
+ self.stanza = stanza
+
+ self.xmpp.register_feature('session',
+ self._handle_start_session,
+ restart=False,
+ order=10001)
+
+ register_stanza_plugin(Iq, stanza.Session)
+ register_stanza_plugin(StreamFeatures, stanza.Session)
+
+ def _handle_start_session(self, features):
+ """
+ Handle the start of the session.
+
+ Arguments:
+ feature -- The stream features element.
+ """
+ iq = self.xmpp.Iq()
+ iq['type'] = 'set'
+ iq.enable('session')
+ response = iq.send(now=True)
+
+ log.debug("Established Session")
+ self.xmpp.sessionstarted = True
+ self.xmpp.session_started_event.set()
+ self.xmpp.event("session_start")
diff --git a/sleekxmpp/features/feature_session/stanza.py b/sleekxmpp/features/feature_session/stanza.py
new file mode 100644
index 00000000..2047a4f0
--- /dev/null
+++ b/sleekxmpp/features/feature_session/stanza.py
@@ -0,0 +1,21 @@
+"""
+ 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 sleekxmpp.stanza import Iq, StreamFeatures
+from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
+
+
+class Session(ElementBase):
+
+ """
+ """
+
+ name = 'session'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-session'
+ interfaces = set()
+ plugin_attrib = 'session'
diff --git a/sleekxmpp/features/feature_starttls/__init__.py b/sleekxmpp/features/feature_starttls/__init__.py
new file mode 100644
index 00000000..042e37fa
--- /dev/null
+++ b/sleekxmpp/features/feature_starttls/__init__.py
@@ -0,0 +1,10 @@
+"""
+ 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 sleekxmpp.features.feature_starttls.starttls import feature_starttls
+from sleekxmpp.features.feature_starttls.stanza import *
diff --git a/sleekxmpp/features/feature_starttls/stanza.py b/sleekxmpp/features/feature_starttls/stanza.py
new file mode 100644
index 00000000..5fdafabd
--- /dev/null
+++ b/sleekxmpp/features/feature_starttls/stanza.py
@@ -0,0 +1,47 @@
+"""
+ 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 sleekxmpp.stanza import StreamFeatures
+from sleekxmpp.xmlstream import StanzaBase, ElementBase
+from sleekxmpp.xmlstream import register_stanza_plugin
+
+
+class STARTTLS(ElementBase):
+
+ """
+ """
+
+ name = 'starttls'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-tls'
+ interfaces = set(('required',))
+ plugin_attrib = name
+
+ def get_required(self):
+ """
+ """
+ return True
+
+
+class Proceed(StanzaBase):
+
+ """
+ """
+
+ name = 'proceed'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-tls'
+ interfaces = set()
+
+
+class Failure(StanzaBase):
+
+ """
+ """
+
+ name = 'failure'
+ namespace = 'urn:ietf:params:xml:ns:xmpp-tls'
+ interfaces = set()
diff --git a/sleekxmpp/features/feature_starttls/starttls.py b/sleekxmpp/features/feature_starttls/starttls.py
new file mode 100644
index 00000000..cbb94be0
--- /dev/null
+++ b/sleekxmpp/features/feature_starttls/starttls.py
@@ -0,0 +1,66 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp.stanza import StreamFeatures
+from sleekxmpp.xmlstream import RestartStream, register_stanza_plugin
+from sleekxmpp.xmlstream.matcher import *
+from sleekxmpp.xmlstream.handler import *
+from sleekxmpp.plugins.base import base_plugin
+from sleekxmpp.features.feature_starttls import stanza
+
+
+log = logging.getLogger(__name__)
+
+
+class feature_starttls(base_plugin):
+
+ def plugin_init(self):
+ self.name = "STARTTLS"
+ self.rfc = '6120'
+ self.description = "STARTTLS Stream Feature"
+ self.stanza = stanza
+
+ self.xmpp.register_handler(
+ Callback('STARTTLS Proceed',
+ MatchXPath(stanza.Proceed.tag_name()),
+ self._handle_starttls_proceed,
+ instream=True))
+ self.xmpp.register_feature('starttls',
+ self._handle_starttls,
+ restart=True,
+ order=self.config.get('order', 0))
+
+ self.xmpp.register_stanza(stanza.Proceed)
+ self.xmpp.register_stanza(stanza.Failure)
+ register_stanza_plugin(StreamFeatures, stanza.STARTTLS)
+
+ def _handle_starttls(self, features):
+ """
+ Handle notification that the server supports TLS.
+
+ Arguments:
+ features -- The stream:features element.
+ """
+ if not self.xmpp.use_tls:
+ return False
+ elif self.xmpp.ssl_support:
+ self.xmpp.send(features['starttls'], now=True)
+ return True
+ else:
+ log.warning("The module tlslite is required to log in" +\
+ " to some servers, and has not been found.")
+ return False
+
+ def _handle_starttls_proceed(self, proceed):
+ """Restart the XML stream when TLS is accepted."""
+ log.debug("Starting TLS")
+ if self.xmpp.start_tls():
+ self.xmpp.features.append('starttls')
+ raise RestartStream()
diff --git a/sleekxmpp/features/sasl_anonymous.py b/sleekxmpp/features/sasl_anonymous.py
new file mode 100644
index 00000000..469d9d19
--- /dev/null
+++ b/sleekxmpp/features/sasl_anonymous.py
@@ -0,0 +1,31 @@
+import base64
+import sys
+import logging
+
+from sleekxmpp.stanza.stream import sasl
+from sleekxmpp.plugins.base import base_plugin
+
+
+log = logging.getLogger(__name__)
+
+
+class sasl_anonymous(base_plugin):
+
+ def plugin_init(self):
+ self.name = 'SASL ANONYMOUS'
+ self.rfc = '6120'
+ self.description = 'SASL ANONYMOUS Mechanism'
+
+ self.xmpp.register_sasl_mechanism('ANONYMOUS',
+ self._handle_anonymous,
+ priority=self.config.get('priority', 0))
+
+ def _handle_anonymous(self):
+ if self.xmpp.boundjid.user:
+ return False
+
+ resp = sasl.Auth(self.xmpp)
+ resp['mechanism'] = 'ANONYMOUS'
+ resp.send(now=True)
+
+ return True
diff --git a/sleekxmpp/features/sasl_plain.py b/sleekxmpp/features/sasl_plain.py
new file mode 100644
index 00000000..36c7d9df
--- /dev/null
+++ b/sleekxmpp/features/sasl_plain.py
@@ -0,0 +1,41 @@
+import base64
+import sys
+import logging
+
+from sleekxmpp.stanza.stream import sasl
+from sleekxmpp.plugins.base import base_plugin
+
+
+log = logging.getLogger(__name__)
+
+
+class sasl_plain(base_plugin):
+
+ def plugin_init(self):
+ self.name = 'SASL PLAIN'
+ self.rfc = '6120'
+ self.description = 'SASL PLAIN Mechanism'
+
+ self.xmpp.register_sasl_mechanism('PLAIN',
+ self._handle_plain,
+ priority=self.config.get('priority', 1))
+
+ def _handle_plain(self):
+ if not self.xmpp.boundjid.user:
+ return False
+
+ if sys.version_info < (3, 0):
+ user = bytes(self.xmpp.boundjid.user)
+ password = bytes(self.xmpp.password)
+ else:
+ user = bytes(self.xmpp.boundjid.user, 'utf-8')
+ password = bytes(self.xmpp.password, 'utf-8')
+
+ auth = base64.b64encode(b'\x00' + user + \
+ b'\x00' + password).decode('utf-8')
+
+ resp = sasl.Auth(self.xmpp)
+ resp['mechanism'] = 'PLAIN'
+ resp['value'] = auth
+ resp.send(now=True)
+ return True
diff --git a/sleekxmpp/plugins/base.py b/sleekxmpp/plugins/base.py
index 2dd68c8d..561421d8 100644
--- a/sleekxmpp/plugins/base.py
+++ b/sleekxmpp/plugins/base.py
@@ -66,7 +66,8 @@ class base_plugin(object):
"""
if config is None:
config = {}
- self.xep = 'base'
+ self.xep = None
+ self.rfc = None
self.description = 'Base Plugin'
self.xmpp = xmpp
self.config = config
diff --git a/sleekxmpp/stanza/__init__.py b/sleekxmpp/stanza/__init__.py
index dbf7b86f..4bd37dc5 100644
--- a/sleekxmpp/stanza/__init__.py
+++ b/sleekxmpp/stanza/__init__.py
@@ -8,7 +8,8 @@
from sleekxmpp.stanza.error import Error
-from sleekxmpp.stanza.stream_error import StreamError
from sleekxmpp.stanza.iq import Iq
from sleekxmpp.stanza.message import Message
from sleekxmpp.stanza.presence import Presence
+from sleekxmpp.stanza.stream_features import StreamFeatures
+from sleekxmpp.stanza.stream_error import StreamError
diff --git a/sleekxmpp/stanza/stream/__init__.py b/sleekxmpp/stanza/stream/__init__.py
new file mode 100644
index 00000000..2cb79673
--- /dev/null
+++ b/sleekxmpp/stanza/stream/__init__.py
@@ -0,0 +1,8 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2010 Nathanael C. Fritz
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
diff --git a/sleekxmpp/stanza/stream_features.py b/sleekxmpp/stanza/stream_features.py
new file mode 100644
index 00000000..5be2e55f
--- /dev/null
+++ b/sleekxmpp/stanza/stream_features.py
@@ -0,0 +1,52 @@
+"""
+ 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 sleekxmpp.xmlstream import ElementBase, StanzaBase, ET
+from sleekxmpp.xmlstream import register_stanza_plugin
+
+
+class StreamFeatures(StanzaBase):
+
+ """
+ """
+
+ name = 'features'
+ namespace = 'http://etherx.jabber.org/streams'
+ interfaces = set(('features', 'required', 'optional'))
+ sub_interfaces = interfaces
+
+ def setup(self, xml):
+ StanzaBase.setup(self, xml)
+ self.values = self.values
+
+ def get_features(self):
+ """
+ """
+ return self.plugins
+
+ def set_features(self, value):
+ """
+ """
+ pass
+
+ def del_features(self):
+ """
+ """
+ pass
+
+ def get_required(self):
+ """
+ """
+ features = self['features']
+ return [f for n, f in features.items() if f['required']]
+
+ def get_optional(self):
+ """
+ """
+ features = self['features']
+ return [f for n, f in features.items() if not f['required']]
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index d9a4636a..f1a9e1f5 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -1064,7 +1064,9 @@ class ElementBase(object):
Defaults to True.
"""
stanza_ns = '' if top_level_ns else self.namespace
- return tostring(self.xml, xmlns='', stanza_ns=stanza_ns)
+ return tostring(self.xml, xmlns='',
+ stanza_ns=stanza_ns,
+ top_level = not top_level_ns)
def __repr__(self):
"""
@@ -1282,7 +1284,8 @@ class StanzaBase(ElementBase):
stanza_ns = '' if top_level_ns else self.namespace
return tostring(self.xml, xmlns='',
stanza_ns=stanza_ns,
- stream=self.stream)
+ stream=self.stream,
+ top_level = not top_level_ns)
# To comply with PEP8, method names now use underscores.
diff --git a/sleekxmpp/xmlstream/tostring/tostring.py b/sleekxmpp/xmlstream/tostring/tostring.py
index 38b08d82..a6bb6ebc 100644
--- a/sleekxmpp/xmlstream/tostring/tostring.py
+++ b/sleekxmpp/xmlstream/tostring/tostring.py
@@ -7,7 +7,8 @@
"""
-def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
+def tostring(xml=None, xmlns='', stanza_ns='', stream=None,
+ outbuffer='', top_level=False):
"""
Serialize an XML object to a Unicode string.
@@ -26,6 +27,8 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
stream -- The XML stream that generated the XML object.
outbuffer -- Optional buffer for storing serializations during
recursive calls.
+ top_level -- Indicates that the element is the outermost
+ element.
"""
# Add previous results to the start of the output.
output = [outbuffer]
@@ -39,14 +42,21 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
else:
tag_xmlns = ''
+ default_ns = ''
+ stream_ns = ''
+ if stream:
+ default_ns = stream.default_ns
+ stream_ns = stream.stream_ns
+
# Output the tag name and derived namespace of the element.
namespace = ''
- if tag_xmlns not in ['', xmlns, stanza_ns]:
+ if top_level and tag_xmlns not in ['', default_ns, stream_ns] or \
+ tag_xmlns not in ['', xmlns, stanza_ns, stream_ns]:
namespace = ' xmlns="%s"' % tag_xmlns
- if stream and tag_xmlns in stream.namespace_map:
- mapped_namespace = stream.namespace_map[tag_xmlns]
- if mapped_namespace:
- tag_name = "%s:%s" % (mapped_namespace, tag_name)
+ if stream and tag_xmlns in stream.namespace_map:
+ mapped_namespace = stream.namespace_map[tag_xmlns]
+ if mapped_namespace:
+ tag_name = "%s:%s" % (mapped_namespace, tag_name)
output.append("<%s" % tag_name)
output.append(namespace)
diff --git a/sleekxmpp/xmlstream/tostring/tostring26.py b/sleekxmpp/xmlstream/tostring/tostring26.py
index 11501780..3d1ca3d7 100644
--- a/sleekxmpp/xmlstream/tostring/tostring26.py
+++ b/sleekxmpp/xmlstream/tostring/tostring26.py
@@ -10,7 +10,8 @@ from __future__ import unicode_literals
import types
-def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
+def tostring(xml=None, xmlns='', stanza_ns='', stream=None,
+ outbuffer='', top_level=False):
"""
Serialize an XML object to a Unicode string.
@@ -29,6 +30,8 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
stream -- The XML stream that generated the XML object.
outbuffer -- Optional buffer for storing serializations during
recursive calls.
+ top_level -- Indicates that the element is the outermost
+ element.
"""
# Add previous results to the start of the output.
output = [outbuffer]
@@ -42,14 +45,21 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, outbuffer=''):
else:
tag_xmlns = u''
+ default_ns = ''
+ stream_ns = ''
+ if stream:
+ default_ns = stream.default_ns
+ stream_ns = stream.stream_ns
+
# Output the tag name and derived namespace of the element.
namespace = u''
- if tag_xmlns not in ['', xmlns, stanza_ns]:
+ if top_level and tag_xmlns not in ['', default_ns, stream_ns] or \
+ tag_xmlns not in ['', xmlns, stanza_ns, stream_ns]:
namespace = u' xmlns="%s"' % tag_xmlns
- if stream and tag_xmlns in stream.namespace_map:
- mapped_namespace = stream.namespace_map[tag_xmlns]
- if mapped_namespace:
- tag_name = u"%s:%s" % (mapped_namespace, tag_name)
+ if stream and tag_xmlns in stream.namespace_map:
+ mapped_namespace = stream.namespace_map[tag_xmlns]
+ if mapped_namespace:
+ tag_name = u"%s:%s" % (mapped_namespace, tag_name)
output.append(u"<%s" % tag_name)
output.append(namespace)