1
|
1 from twisted.protocols.jabber import component,jid |
|
2 from twisted.xish import utility |
|
3 from twisted.python import components |
|
4 import backend |
|
5 import xmpp_error |
|
6 |
|
7 IQ_GET = '/iq[@type="get"]' |
|
8 IQ_SET = '/iq[@type="set"]' |
|
9 PUBSUB_GET = IQ_GET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' |
|
10 PUBSUB_SET = IQ_SET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' |
|
11 |
|
12 PUBSUB_CREATE = PUBSUB_SET + '/create' |
|
13 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' |
|
14 |
|
15 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): |
|
16 |
|
17 def __init__(self, backend): |
|
18 utility.EventDispatcher.__init__(self) |
|
19 self.backend = backend |
|
20 self.addObserver(PUBSUB_PUBLISH, self.onPublish) |
|
21 |
|
22 def componentConnected(self, xmlstream): |
|
23 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) |
|
24 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) |
|
25 |
|
26 def error(self, failure, iq): |
|
27 r = failure.trap(backend.NotAuthorized, backend.NodeNotFound) |
|
28 |
|
29 if r == backend.NotAuthorized: |
|
30 xmpp_error.error_from_iq(iq, 'not-authorized', failure.value.msg) |
|
31 |
|
32 if r == backend.NodeNotFound: |
|
33 xmpp_error.error_from_iq(iq, 'item-not-found', failure.value.msg) |
|
34 |
|
35 return iq |
|
36 |
|
37 def success(self, result, iq): |
|
38 iq.swapAttributeValues("to", "from") |
|
39 iq["type"] = 'result' |
|
40 iq.children = [] |
|
41 return iq |
|
42 |
|
43 def onPubSub(self, iq): |
|
44 self.dispatch(iq) |
|
45 iq.handled = True |
|
46 |
|
47 def onPublish(self, iq): |
|
48 node = iq.pubsub.publish["node"] |
|
49 |
|
50 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), iq.pubsub.publish.item) |
|
51 d.addCallback(self.success, iq) |
|
52 d.addErrback(self.error, iq) |
|
53 d.addCallback(self.send) |
|
54 |
|
55 |
|
56 """ |
|
57 def onCreateSet(self, iq): |
|
58 node = iq.pubsub.create["node"] |
|
59 owner = jid.JID(iq["from"]).userhost() |
|
60 |
|
61 try: |
|
62 node = self.backend.create_node(node, owner) |
|
63 |
|
64 if iq.pubsub.create["node"] == None: |
|
65 # also show node name |
|
66 except: |
|
67 pass |
|
68 |
|
69 iq.handled = True |
|
70 """ |
|
71 |
|
72 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) |
|
73 |