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