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.
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'
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
,
35 self
.xmpp
.register_feature('starttls',
36 self
._handle
_starttls
,
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
):
46 Handle notification that the server supports TLS.
49 features -- The stream:features element.
51 if 'starttls' in self
.xmpp
.features
:
52 # We have already negotiated TLS, but the server is
53 # offering it again, against spec.
55 elif not self
.xmpp
.use_tls
:
58 self
.xmpp
.send(features
['starttls'], now
=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')