Mercurial > libervia-pubsub
comparison idavoll/pubsub.py @ 21:e01bbbfa8a46
Implemented node creation.
Corrected handling of subscribe/options combo.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Wed, 06 Oct 2004 21:07:11 +0000 |
parents | 7937d6fbbe2a |
children | 884268687229 |
comparison
equal
deleted
inserted
replaced
20:eddea65d1032 | 21:e01bbbfa8a46 |
---|---|
16 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | 16 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT |
17 PUBSUB_CREATE = PUBSUB_SET + '/create' | 17 PUBSUB_CREATE = PUBSUB_SET + '/create' |
18 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | 18 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' |
19 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' | 19 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' |
20 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' | 20 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
21 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' | |
21 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' | 22 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' |
23 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure' | |
22 | 24 |
23 error_map = { | 25 error_map = { |
24 backend.NotAuthorized: 'not-authorized', | 26 backend.NotAuthorized: 'not-authorized', |
25 backend.NodeNotFound: 'item-not-found', | 27 backend.NodeNotFound: 'item-not-found', |
26 backend.NoPayloadAllowed: 'bad-request', | 28 backend.NoPayloadAllowed: 'bad-request', |
27 backend.PayloadExpected: 'bad-request', | 29 backend.PayloadExpected: 'bad-request', |
30 backend.NoInstantNodes: 'not-acceptable', | |
31 backend.NodeExists: 'conflict', | |
28 } | 32 } |
29 | 33 |
30 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): | 34 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): |
31 | 35 |
32 def __init__(self, backend): | 36 def __init__(self, backend): |
33 utility.EventDispatcher.__init__(self) | 37 utility.EventDispatcher.__init__(self) |
34 self.backend = backend | 38 self.backend = backend |
35 self.backend.pubsub_service = self | 39 self.backend.pubsub_service = self |
36 self.addObserver(PUBSUB_PUBLISH, self.onPublish) | 40 self.addObserver(PUBSUB_PUBLISH, self.onPublish) |
37 self.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe) | 41 |
42 # make sure subscribe and create are handled before resp. options and | |
43 # configure | |
44 self.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe, 0) | |
45 self.addObserver(PUBSUB_OPTIONS_SET, self.onOptionsSet, 1) | |
46 self.addObserver(PUBSUB_CREATE, self.onSubscribe, 0) | |
47 self.addObserver(PUBSUB_CONFIGURE_SET, self.onConfigureSet, 1) | |
48 | |
38 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) | 49 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) |
39 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) | 50 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) |
40 self.addObserver(PUBSUB_GET, self.notImplemented, -1) | 51 self.addObserver(PUBSUB_GET, self.notImplemented, -1) |
41 self.addObserver(PUBSUB_SET, self.notImplemented, -1) | 52 self.addObserver(PUBSUB_SET, self.notImplemented, -1) |
42 | 53 |
102 def onOptionsGet(self, iq): | 113 def onOptionsGet(self, iq): |
103 xmpp_error.error_from_iq(iq, 'feature-not-implemented') | 114 xmpp_error.error_from_iq(iq, 'feature-not-implemented') |
104 iq.error.addElement((NS_PUBSUB_ERRORS, 'subscription-options-unavailable'), NS_PUBSUB_ERRORS) | 115 iq.error.addElement((NS_PUBSUB_ERRORS, 'subscription-options-unavailable'), NS_PUBSUB_ERRORS) |
105 self.send(iq) | 116 self.send(iq) |
106 | 117 |
118 def onOptionsSet(self, iq): | |
119 if iq.pubsub.subscribe: | |
120 # this should be handled by the subscribe handler | |
121 return | |
122 | |
123 xmpp_error.error_from_iq(iq, 'feature-not-implemented') | |
124 iq.error.addElement((NS_PUBSUB_ERRORS, 'subscription-options-unavailable'), NS_PUBSUB_ERRORS) | |
125 self.send(iq) | |
126 | |
107 def onConfigureGet(self, iq): | 127 def onConfigureGet(self, iq): |
108 xmpp_error.error_from_iq(iq, 'feature-not-implemented') | 128 xmpp_error.error_from_iq(iq, 'feature-not-implemented') |
109 iq.error.addElement((NS_PUBSUB_ERRORS, 'node-not-configurable'), NS_PUBSUB_ERRORS) | 129 iq.error.addElement((NS_PUBSUB_ERRORS, 'node-not-configurable'), NS_PUBSUB_ERRORS) |
110 self.send(iq) | 130 self.send(iq) |
111 | 131 |
132 def onConfigureSet(self, iq): | |
133 if iq.pubsub.create: | |
134 # this should be handled by the create handler | |
135 return | |
136 | |
137 xmpp_error.error_from_iq(iq, 'feature-not-implemented') | |
138 iq.error.addElement((NS_PUBSUB_ERRORS, 'node-not-configurable'), NS_PUBSUB_ERRORS) | |
139 self.send(iq) | |
140 | |
112 def onSubscribe(self, iq): | 141 def onSubscribe(self, iq): |
142 if iq.pubsub.options: | |
143 xmpp_error.error_from_iq(iq, 'not-acceptable') | |
144 iq.error.addElement((NS_PUBSUB_ERRORS, 'subscription-options-unavailable'), NS_PUBSUB_ERRORS) | |
145 self.send(iq) | |
146 return | |
147 | |
113 node_id = iq.pubsub.subscribe["node"] | 148 node_id = iq.pubsub.subscribe["node"] |
114 subscriber = jid.JID(iq.pubsub.subscribe["jid"]) | 149 subscriber = jid.JID(iq.pubsub.subscribe["jid"]) |
115 requestor = jid.JID(iq["from"]).userhostJID() | 150 requestor = jid.JID(iq["from"]).userhostJID() |
116 d = self.backend.do_subscribe(node_id, subscriber, requestor) | 151 d = self.backend.do_subscribe(node_id, subscriber, requestor) |
117 d.addCallback(self.return_subscription) | 152 d.addCallback(self.return_subscription) |
141 items = x.addElement("items") | 176 items = x.addElement("items") |
142 items["node"] = node | 177 items["node"] = node |
143 items.children.extend(itemlist) | 178 items.children.extend(itemlist) |
144 self.send(message) | 179 self.send(message) |
145 | 180 |
146 """ | 181 def onCreate(self, iq): |
147 def onCreateSet(self, iq): | 182 if iq.pubsub.options: |
183 xmpp_error.error_from_iq(iq, 'not-acceptable') | |
184 iq.error.addElement((NS_PUBSUB_ERRORS, 'node-not-configurable'), NS_PUBSUB_ERRORS) | |
185 self.send(iq) | |
186 return | |
187 | |
148 node = iq.pubsub.create["node"] | 188 node = iq.pubsub.create["node"] |
149 owner = jid.JID(iq["from"]).userhost() | 189 owner = jid.JID(iq["from"]).userhostJID() |
150 | 190 |
151 try: | 191 try: |
152 node = self.backend.create_node(node, owner) | 192 d = self.backend.create_node(node, owner) |
153 | 193 d.addCallback(self.return_create_response, iq) |
154 if iq.pubsub.create["node"] == None: | 194 d.addCallback(self.succeed, iq) |
155 # also show node name | 195 d.addErrback(self.error, iq) |
196 d.addCallback(self.send) | |
156 except: | 197 except: |
157 pass | 198 pass |
158 | 199 |
159 iq.handled = True | 200 def return_create_response(self, result, iq): |
160 """ | 201 if iq.pubsub.create["node"] is None: |
202 reply = domish.Element('pubsub', NS_PUBSUB) | |
203 entity = reply.addElement('create') | |
204 entity['node'] = result['node_id'] | |
205 return reply | |
161 | 206 |
162 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) | 207 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) |
163 | 208 |