Mercurial > libervia-pubsub
annotate idavoll/pubsub.py @ 4:ea195dc1732d
Allow publication of more than 1 item.
Use mapping between backend exceptions and jabber error replies.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sun, 27 Jun 2004 14:07:12 +0000 |
parents | 9701df89c534 |
children | a8cfb31dc50c |
rev | line source |
---|---|
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 | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
19 error_map = { |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
20 backend.NotAuthorized: 'not-authorized', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
21 backend.NodeNotFound: 'item-not-found', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
22 backend.NoPayloadAllowed: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
23 backend.EmptyPayloadExpected: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
24 backend.PayloadExpected: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
25 } |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
26 |
1 | 27 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): |
28 | |
29 def __init__(self, backend): | |
30 utility.EventDispatcher.__init__(self) | |
31 self.backend = backend | |
2 | 32 self.backend.pubsub_service = self |
1 | 33 self.addObserver(PUBSUB_PUBLISH, self.onPublish) |
34 | |
35 def componentConnected(self, xmlstream): | |
36 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) | |
37 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) | |
38 | |
39 def error(self, failure, iq): | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
40 r = failure.trap(*error_map.keys()) |
1 | 41 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
42 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg) |
1 | 43 |
44 return iq | |
45 | |
46 def success(self, result, iq): | |
47 iq.swapAttributeValues("to", "from") | |
48 iq["type"] = 'result' | |
49 iq.children = [] | |
50 return iq | |
51 | |
52 def onPubSub(self, iq): | |
53 self.dispatch(iq) | |
54 iq.handled = True | |
55 | |
56 def onPublish(self, iq): | |
57 node = iq.pubsub.publish["node"] | |
58 | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
59 items = [] |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
60 for child in iq.pubsub.publish.children: |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
61 if child.__class__ == domish.Element and child.name == 'item': |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
62 items.append(child) |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
63 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
64 print items |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
65 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
66 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), items) |
1 | 67 d.addCallback(self.success, iq) |
68 d.addErrback(self.error, iq) | |
69 d.addCallback(self.send) | |
70 | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
71 def do_notification(self, list, node): |
1 | 72 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
73 for recipient, items in list.items(): |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
74 self.notify(node, items, recipient) |
2 | 75 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
76 def notify(self, node, itemlist, recipient): |
2 | 77 message = domish.Element((NS_COMPONENT, "message")) |
78 message["from"] = self.parent.jabberId | |
79 message["to"] = recipient | |
80 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT) | |
81 items = x.addElement("items") | |
82 items["node"] = node | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
83 items.children.extend(itemlist) |
2 | 84 self.send(message) |
85 | |
1 | 86 """ |
87 def onCreateSet(self, iq): | |
88 node = iq.pubsub.create["node"] | |
89 owner = jid.JID(iq["from"]).userhost() | |
90 | |
91 try: | |
92 node = self.backend.create_node(node, owner) | |
93 | |
94 if iq.pubsub.create["node"] == None: | |
95 # also show node name | |
96 except: | |
97 pass | |
98 | |
99 iq.handled = True | |
100 """ | |
101 | |
102 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) | |
103 |