1
# -*- test-case-name: twisted.words.test.test_jabbercomponent -*-
3
# Copyright (c) 2001-2006 Twisted Matrix Laboratories.
4
# See LICENSE for details.
7
External server-side components.
9
Most Jabber server implementations allow for add-on components that act as a
10
seperate entity on the Jabber network, but use the server-to-server
11
functionality of a regular Jabber IM server. These so-called 'external
12
components' are connected to the Jabber server using the Jabber Component
13
Protocol as defined in U{JEP-0114<http://www.jabber.org/jeps/jep-0114.html>}.
15
This module allows for writing external server-side component by assigning one
16
or more services implementing L{ijabber.IService} up to L{ServiceManager}. The
17
ServiceManager connects to the Jabber server and is responsible for the
18
corresponding XML stream.
21
from zope.interface import implements
23
from twisted.application import service
24
from twisted.internet import defer
25
from twisted.words.xish import domish
26
from twisted.words.protocols.jabber import ijabber, jstrports, xmlstream
28
def componentFactory(componentid, password):
30
XML stream factory for external server-side components.
32
@param componentid: JID of the component.
33
@type componentid: L{unicode}
34
@param password: password used to authenticate to the server.
35
@type password: L{str}
37
a = ConnectComponentAuthenticator(componentid, password)
38
return xmlstream.XmlStreamFactory(a)
40
class ComponentInitiatingInitializer(object):
42
External server-side component authentication initializer for the
45
@ivar xmlstream: XML stream between server and component.
46
@type xmlstream: L{xmlstream.XmlStream}
49
def __init__(self, xs):
55
hs = domish.Element((self.xmlstream.namespace, "handshake"))
56
hs.addContent(xmlstream.hashPassword(xs.sid,
57
xs.authenticator.password))
59
# Setup observer to watch for handshake result
60
xs.addOnetimeObserver("/handshake", self._cbHandshake)
62
self._deferred = defer.Deferred()
65
def _cbHandshake(self, _):
66
# we have successfully shaken hands and can now consider this
67
# entity to represent the component JID.
68
self.xmlstream.thisHost = self.xmlstream.otherHost
69
self._deferred.callback(None)
71
class ConnectComponentAuthenticator(xmlstream.ConnectAuthenticator):
73
Authenticator to permit an XmlStream to authenticate against a Jabber
74
server as an external component (where the Authenticator is initiating the
77
namespace = 'jabber:component:accept'
79
def __init__(self, componentjid, password):
81
@type componentjid: L{str}
82
@param componentjid: Jabber ID that this component wishes to bind to.
84
@type password: L{str}
85
@param password: Password/secret this component uses to authenticate.
87
# Note that we are sending 'to' our desired component JID.
88
xmlstream.ConnectAuthenticator.__init__(self, componentjid)
89
self.password = password
91
def associateWithStream(self, xs):
93
xmlstream.ConnectAuthenticator.associateWithStream(self, xs)
95
xs.initializers = [ComponentInitiatingInitializer(xs)]
97
class ListenComponentAuthenticator(xmlstream.Authenticator):
99
Placeholder for listening components.
102
class Service(service.Service):
104
External server-side component service.
107
implements(ijabber.IService)
109
def componentConnected(self, xs):
112
def componentDisconnected(self):
115
def transportConnected(self, xs):
120
Send data over service parent's XML stream.
122
@note: L{ServiceManager} maintains a queue for data sent using this
123
method when there is no current established XML stream. This data is
124
then sent as soon as a new stream has been established and initialized.
125
Subsequently, L{componentConnected} will be called again. If this
126
queueing is not desired, use C{send} on the XmlStream object (passed to
127
L{componentConnected}) directly.
129
@param obj: data to be sent over the XML stream. This is usually an
130
object providing L{domish.IElement}, or serialized XML. See
131
L{xmlstream.XmlStream} for details.
134
self.parent.send(obj)
136
class ServiceManager(service.MultiService):
138
Business logic representing a managed component connection to a Jabber
141
This service maintains a single connection to a Jabber router and provides
142
facilities for packet routing and transmission. Business logic modules are
143
services implementing L{ijabber.IService} (like subclasses of L{Service}), and
144
added as sub-service.
147
def __init__(self, jid, password):
148
service.MultiService.__init__(self)
152
self.xmlstream = None
154
# Internal buffer of packets
155
self._packetQueue = []
157
# Setup the xmlstream factory
158
self._xsFactory = componentFactory(self.jabberId, password)
160
# Register some lambda functions to keep the self.xmlstream var up to
162
self._xsFactory.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,
164
self._xsFactory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self._authd)
165
self._xsFactory.addBootstrap(xmlstream.STREAM_END_EVENT,
168
# Map addBootstrap and removeBootstrap to the underlying factory -- is
169
# this right? I have no clue...but it'll work for now, until i can
170
# think about it more.
171
self.addBootstrap = self._xsFactory.addBootstrap
172
self.removeBootstrap = self._xsFactory.removeBootstrap
174
def getFactory(self):
175
return self._xsFactory
177
def _connected(self, xs):
180
if ijabber.IService.providedBy(c):
181
c.transportConnected(xs)
183
def _authd(self, xs):
184
# Flush all pending packets
185
for p in self._packetQueue:
186
self.xmlstream.send(p)
187
self._packetQueue = []
189
# Notify all child services which implement the IService interface
191
if ijabber.IService.providedBy(c):
192
c.componentConnected(xs)
194
def _disconnected(self, _):
195
self.xmlstream = None
197
# Notify all child services which implement
198
# the IService interface
200
if ijabber.IService.providedBy(c):
201
c.componentDisconnected()
205
Send data over the XML stream.
207
When there is no established XML stream, the data is queued and sent
208
out when a new XML stream has been established and initialized.
210
@param obj: data to be sent over the XML stream. This is usually an
211
object providing L{domish.IElement}, or serialized XML. See
212
L{xmlstream.XmlStream} for details.
215
if self.xmlstream != None:
216
self.xmlstream.send(obj)
218
self._packetQueue.append(obj)
220
def buildServiceManager(jid, password, strport):
222
Constructs a pre-built L{ServiceManager}, using the specified strport
226
svc = ServiceManager(jid, password)
227
client_svc = jstrports.client(strport, svc.getFactory())
228
client_svc.setServiceParent(svc)