Mercurial > libervia-pubsub
annotate idavoll/pubsub.py @ 20:eddea65d1032
Added two exceptions: NoInstantNodes and NodeExists.
Renamed 'name' attribute of Node to 'id'.
Added create_node() method.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Wed, 06 Oct 2004 21:05:42 +0000 |
parents | 7937d6fbbe2a |
children | e01bbbfa8a46 |
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' | |
16 | 10 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors' |
2 | 11 |
1 | 12 IQ_GET = '/iq[@type="get"]' |
13 IQ_SET = '/iq[@type="set"]' | |
2 | 14 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' |
15 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT | |
16 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | |
1 | 17 PUBSUB_CREATE = PUBSUB_SET + '/create' |
18 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | |
16 | 19 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
20 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
21 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' |
1 | 22 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
23 error_map = { |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
24 backend.NotAuthorized: 'not-authorized', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
25 backend.NodeNotFound: 'item-not-found', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
26 backend.NoPayloadAllowed: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
27 backend.PayloadExpected: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
28 } |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
29 |
1 | 30 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): |
31 | |
32 def __init__(self, backend): | |
33 utility.EventDispatcher.__init__(self) | |
34 self.backend = backend | |
2 | 35 self.backend.pubsub_service = self |
1 | 36 self.addObserver(PUBSUB_PUBLISH, self.onPublish) |
16 | 37 self.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
38 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
39 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
40 self.addObserver(PUBSUB_GET, self.notImplemented, -1) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
41 self.addObserver(PUBSUB_SET, self.notImplemented, -1) |
1 | 42 |
43 def componentConnected(self, xmlstream): | |
44 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) | |
45 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) | |
46 | |
9 | 47 def getIdentities(self, node): |
48 results = [] | |
49 if not node: | |
50 results.append({ | |
51 'category': 'pubsub', | |
52 'type': 'generic', | |
53 'name': 'Generic Pubsub Service' | |
54 }) | |
55 return results | |
56 | |
12 | 57 def getFeatures(self, node): |
58 return [ | |
59 "http://jabber.org/protocol/pubsub#outcast-affil", | |
60 "http://jabber.org/protocol/pubsub#publisher-affil", | |
16 | 61 "http://jabber.org/protocol/pubsub#persistent-items", |
12 | 62 ] |
63 | |
1 | 64 def error(self, failure, iq): |
16 | 65 try: |
66 r = failure.trap(*error_map.keys()) | |
67 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg) | |
68 return iq | |
69 except: | |
70 xmpp_error.error_from_iq(iq, 'internal-server-error') | |
71 self.send(iq) | |
72 raise | |
1 | 73 |
74 def success(self, result, iq): | |
75 iq.swapAttributeValues("to", "from") | |
76 iq["type"] = 'result' | |
18 | 77 iq.children = result or [] |
1 | 78 return iq |
79 | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
80 def notImplemented(self, iq): |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
81 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented')) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
82 |
1 | 83 def onPubSub(self, iq): |
84 self.dispatch(iq) | |
85 iq.handled = True | |
86 | |
87 def onPublish(self, iq): | |
88 node = iq.pubsub.publish["node"] | |
89 | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
90 items = [] |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
91 for child in iq.pubsub.publish.children: |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
92 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
|
93 items.append(child) |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
94 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
95 print items |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
96 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
97 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), items) |
1 | 98 d.addCallback(self.success, iq) |
99 d.addErrback(self.error, iq) | |
100 d.addCallback(self.send) | |
101 | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
102 def onOptionsGet(self, iq): |
16 | 103 xmpp_error.error_from_iq(iq, 'feature-not-implemented') |
104 iq.error.addElement((NS_PUBSUB_ERRORS, 'subscription-options-unavailable'), NS_PUBSUB_ERRORS) | |
105 self.send(iq) | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
106 |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
107 def onConfigureGet(self, iq): |
16 | 108 xmpp_error.error_from_iq(iq, 'feature-not-implemented') |
109 iq.error.addElement((NS_PUBSUB_ERRORS, 'node-not-configurable'), NS_PUBSUB_ERRORS) | |
110 self.send(iq) | |
111 | |
112 def onSubscribe(self, iq): | |
113 node_id = iq.pubsub.subscribe["node"] | |
114 subscriber = jid.JID(iq.pubsub.subscribe["jid"]) | |
115 requestor = jid.JID(iq["from"]).userhostJID() | |
116 d = self.backend.do_subscribe(node_id, subscriber, requestor) | |
18 | 117 d.addCallback(self.return_subscription) |
118 d.addCallback(self.succeed, iq) | |
16 | 119 d.addErrback(self.error, iq) |
120 d.addCallback(self.send) | |
121 | |
18 | 122 def return_subscription(self, result): |
123 reply = domish.Element("pubsub", NS_PUBSUB) | |
16 | 124 entity = reply.addElement("entity") |
125 entity["node"] = result["node"] | |
126 entity["jid"] = result["jid"].full() | |
127 entity["affiliation"] = result["affiliation"] | |
128 entity["subscription"] = result["subscription"] | |
18 | 129 return reply |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
130 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
131 def do_notification(self, list, node): |
1 | 132 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
133 for recipient, items in list.items(): |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
134 self.notify(node, items, recipient) |
2 | 135 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
136 def notify(self, node, itemlist, recipient): |
2 | 137 message = domish.Element((NS_COMPONENT, "message")) |
138 message["from"] = self.parent.jabberId | |
139 message["to"] = recipient | |
140 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT) | |
141 items = x.addElement("items") | |
142 items["node"] = node | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
143 items.children.extend(itemlist) |
2 | 144 self.send(message) |
145 | |
1 | 146 """ |
147 def onCreateSet(self, iq): | |
148 node = iq.pubsub.create["node"] | |
149 owner = jid.JID(iq["from"]).userhost() | |
150 | |
151 try: | |
152 node = self.backend.create_node(node, owner) | |
153 | |
154 if iq.pubsub.create["node"] == None: | |
155 # also show node name | |
156 except: | |
157 pass | |
158 | |
159 iq.handled = True | |
160 """ | |
161 | |
162 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) | |
163 |