/usr/share/pyshared/wokkel/client.py is in python-wokkel 0.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | # -*- test-case-name: wokkel.test.test_client -*-
#
# Copyright (c) Ralph Meijer.
# See LICENSE for details.
"""
XMPP Client support.
This module holds several improvements on top of Twisted's XMPP support
that should probably eventually move there.
"""
from twisted.application import service
from twisted.internet import reactor
from twisted.names.srvconnect import SRVConnector
from twisted.words.protocols.jabber import client, sasl, xmlstream
from wokkel import generic
from wokkel.subprotocols import StreamManager
class CheckAuthInitializer(object):
"""
Check what authentication methods are available.
"""
def __init__(self, xs):
self.xmlstream = xs
def initialize(self):
if (sasl.NS_XMPP_SASL, 'mechanisms') in self.xmlstream.features:
inits = [(sasl.SASLInitiatingInitializer, True),
(client.BindInitializer, True),
(client.SessionInitializer, False)]
for initClass, required in inits:
init = initClass(self.xmlstream)
init.required = required
self.xmlstream.initializers.append(init)
elif (client.NS_IQ_AUTH_FEATURE, 'auth') in self.xmlstream.features:
self.xmlstream.initializers.append(
client.IQAuthInitializer(self.xmlstream))
else:
raise Exception("No available authentication method found")
class HybridAuthenticator(xmlstream.ConnectAuthenticator):
"""
Initializes an XmlStream connecting to an XMPP server as a Client.
This is similar to L{client.XMPPAuthenticator}, but also tries non-SASL
autentication.
"""
namespace = 'jabber:client'
def __init__(self, jid, password):
xmlstream.ConnectAuthenticator.__init__(self, jid.host)
self.jid = jid
self.password = password
def associateWithStream(self, xs):
xmlstream.ConnectAuthenticator.associateWithStream(self, xs)
tlsInit = xmlstream.TLSInitiatingInitializer(xs)
xs.initializers = [client.CheckVersionInitializer(xs),
tlsInit,
CheckAuthInitializer(xs)]
def HybridClientFactory(jid, password):
"""
Client factory for XMPP 1.0.
This is similar to L{client.XMPPClientFactory} but also tries non-SASL
autentication.
"""
a = HybridAuthenticator(jid, password)
return xmlstream.XmlStreamFactory(a)
class XMPPClient(StreamManager, service.Service):
"""
Service that initiates an XMPP client connection.
"""
def __init__(self, jid, password, host=None, port=5222):
self.jid = jid
self.domain = generic.prepareIDNName(jid.host)
self.host = host
self.port = port
factory = HybridClientFactory(jid, password)
StreamManager.__init__(self, factory)
def startService(self):
service.Service.startService(self)
self._connection = self._getConnection()
def stopService(self):
service.Service.stopService(self)
self.factory.stopTrying()
self._connection.disconnect()
def _authd(self, xs):
"""
Called when the stream has been initialized.
Save the JID that we were assigned by the server, as the resource might
differ from the JID we asked for. This is stored on the authenticator
by its constituent initializers.
"""
self.jid = self.factory.authenticator.jid
StreamManager._authd(self, xs)
def initializationFailed(self, reason):
"""
Called when stream initialization has failed.
Stop the service (thereby disconnecting the current stream) and
raise the exception.
"""
self.stopService()
reason.raiseException()
def _getConnection(self):
if self.host:
return reactor.connectTCP(self.host, self.port, self.factory)
else:
c = XMPPClientConnector(reactor, self.domain, self.factory)
c.connect()
return c
class DeferredClientFactory(generic.DeferredXmlStreamFactory):
def __init__(self, jid, password):
authenticator = client.XMPPAuthenticator(jid, password)
generic.DeferredXmlStreamFactory.__init__(self, authenticator)
self.streamManager = StreamManager(self)
def addHandler(self, handler):
"""
Add a subprotocol handler to the stream manager.
"""
self.streamManager.addHandler(handler)
def removeHandler(self, handler):
"""
Add a subprotocol handler to the stream manager.
"""
self.streamManager.removeHandler(handler)
class XMPPClientConnector(SRVConnector):
def __init__(self, reactor, domain, factory):
SRVConnector.__init__(self, reactor, 'xmpp-client', domain, factory)
def pickServer(self):
host, port = SRVConnector.pickServer(self)
if not self.servers and not self.orderedServers:
# no SRV record, fall back..
port = 5222
return host, port
def clientCreator(factory):
domain = generic.prepareIDNName(factory.authenticator.jid.host)
c = XMPPClientConnector(reactor, domain, factory)
c.connect()
return factory.deferred
|