Rename to slixmpp
[slixmpp.git] / slixmpp / features / feature_starttls / starttls.py
blob4b9dd60b9be171863811d9943c2ce0cd69195f5f
1 """
2 Slixmpp: The Slick XMPP Library
3 Copyright (C) 2011 Nathanael C. Fritz
4 This file is part of Slixmpp.
6 See the file LICENSE for copying permission.
7 """
9 import logging
11 from slixmpp.stanza import StreamFeatures
12 from slixmpp.xmlstream import RestartStream, register_stanza_plugin
13 from slixmpp.plugins import BasePlugin
14 from slixmpp.xmlstream.matcher import MatchXPath
15 from slixmpp.xmlstream.handler import Callback
16 from slixmpp.features.feature_starttls import stanza
19 log = logging.getLogger(__name__)
22 class FeatureSTARTTLS(BasePlugin):
24 name = 'feature_starttls'
25 description = 'RFC 6120: Stream Feature: STARTTLS'
26 dependencies = set()
27 stanza = stanza
29 def plugin_init(self):
30 self.xmpp.register_handler(
31 Callback('STARTTLS Proceed',
32 MatchXPath(stanza.Proceed.tag_name()),
33 self._handle_starttls_proceed,
34 instream=True))
35 self.xmpp.register_feature('starttls',
36 self._handle_starttls,
37 restart=True,
38 order=self.config.get('order', 0))
40 self.xmpp.register_stanza(stanza.Proceed)
41 self.xmpp.register_stanza(stanza.Failure)
42 register_stanza_plugin(StreamFeatures, stanza.STARTTLS)
44 def _handle_starttls(self, features):
45 """
46 Handle notification that the server supports TLS.
48 Arguments:
49 features -- The stream:features element.
50 """
51 if 'starttls' in self.xmpp.features:
52 # We have already negotiated TLS, but the server is
53 # offering it again, against spec.
54 return False
55 elif not self.xmpp.use_tls:
56 return False
57 else:
58 self.xmpp.send(features['starttls'], now=True)
59 return True
61 def _handle_starttls_proceed(self, proceed):
62 """Restart the XML stream when TLS is accepted."""
63 log.debug("Starting TLS")
64 if self.xmpp.start_tls():
65 self.xmpp.features.add('starttls')
66 raise RestartStream()