1 | # Copyright (c) 2003-2007 Ralph Meijer |
---|
2 | # See LICENSE for details. |
---|
3 | |
---|
4 | """ |
---|
5 | XMPP External Component utilities |
---|
6 | """ |
---|
7 | |
---|
8 | from twisted.application import service |
---|
9 | from twisted.internet import reactor |
---|
10 | from twisted.words.protocols.jabber import component |
---|
11 | from twisted.words.xish import domish |
---|
12 | |
---|
13 | from wokkel.subprotocols import StreamManager |
---|
14 | |
---|
15 | class Component(StreamManager, service.Service): |
---|
16 | def __init__(self, host, port, jid, password): |
---|
17 | self.host = host |
---|
18 | self.port = port |
---|
19 | |
---|
20 | factory = component.componentFactory(jid, password) |
---|
21 | |
---|
22 | StreamManager.__init__(self, factory) |
---|
23 | |
---|
24 | def _authd(self, xs): |
---|
25 | old_send = xs.send |
---|
26 | |
---|
27 | def send(obj): |
---|
28 | if domish.IElement.providedBy(obj) and \ |
---|
29 | not obj.getAttribute('from'): |
---|
30 | obj['from'] = self.xmlstream.thisEntity.full() |
---|
31 | old_send(obj) |
---|
32 | |
---|
33 | xs.send = send |
---|
34 | StreamManager._authd(self, xs) |
---|
35 | |
---|
36 | def initializationFailed(self, reason): |
---|
37 | """ |
---|
38 | Called when stream initialization has failed. |
---|
39 | |
---|
40 | Stop the service (thereby disconnecting the current stream) and |
---|
41 | raise the exception. |
---|
42 | """ |
---|
43 | self.stopService() |
---|
44 | reason.raiseException() |
---|
45 | |
---|
46 | def startService(self): |
---|
47 | service.Service.startService(self) |
---|
48 | |
---|
49 | self.factory.stopTrying() |
---|
50 | self._connection = self._getConnection() |
---|
51 | |
---|
52 | def stopService(self): |
---|
53 | service.Service.stopService(self) |
---|
54 | |
---|
55 | self._connection.disconnect() |
---|
56 | |
---|
57 | def _getConnection(self): |
---|
58 | return reactor.connectTCP(self.host, self.port, self.factory) |
---|