Mercurial > libervia-pubsub
comparison idavoll/pubsub.py @ 95:3ad74552bbc7
Merge from RELENG_0: Implemented node configuration.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 23 Nov 2004 16:18:52 +0000 |
parents | ea3b2410c01c |
children | f289c3e1dd0a |
comparison
equal
deleted
inserted
replaced
94:bbebadd71d35 | 95:3ad74552bbc7 |
---|---|
9 | 9 |
10 NS_COMPONENT = 'jabber:component:accept' | 10 NS_COMPONENT = 'jabber:component:accept' |
11 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' | 11 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' |
12 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' | 12 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' |
13 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors' | 13 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors' |
14 NS_PUBSUB_OWNER = NS_PUBSUB + "#owner" | |
15 NS_X_DATA = 'jabber:x:data' | |
14 | 16 |
15 IQ_GET = '/iq[@type="get"]' | 17 IQ_GET = '/iq[@type="get"]' |
16 IQ_SET = '/iq[@type="set"]' | 18 IQ_SET = '/iq[@type="set"]' |
17 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' | 19 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' |
20 PUBSUB_OWNER_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB_OWNER + '"]' | |
18 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT | 21 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT |
19 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | 22 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT |
23 PUBSUB_OWNER_GET = IQ_GET + PUBSUB_OWNER_ELEMENT | |
24 PUBSUB_OWNER_SET = IQ_SET + PUBSUB_OWNER_ELEMENT | |
20 PUBSUB_CREATE = PUBSUB_SET + '/create' | 25 PUBSUB_CREATE = PUBSUB_SET + '/create' |
21 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | 26 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' |
22 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' | 27 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' |
23 PUBSUB_UNSUBSCRIBE = PUBSUB_SET + '/unsubscribe' | 28 PUBSUB_UNSUBSCRIBE = PUBSUB_SET + '/unsubscribe' |
24 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' | 29 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
25 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' | 30 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' |
26 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' | 31 PUBSUB_CONFIGURE_GET = PUBSUB_OWNER_GET + '/configure' |
27 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure' | 32 PUBSUB_CONFIGURE_SET = PUBSUB_OWNER_SET + '/configure' |
28 PUBSUB_AFFILIATIONS = PUBSUB_GET + '/affiliations' | 33 PUBSUB_AFFILIATIONS = PUBSUB_GET + '/affiliations' |
29 PUBSUB_ITEMS = PUBSUB_GET + '/items' | 34 PUBSUB_ITEMS = PUBSUB_GET + '/items' |
30 PUBSUB_RETRACT = PUBSUB_SET + '/retract' | 35 PUBSUB_RETRACT = PUBSUB_SET + '/retract' |
31 PUBSUB_PURGE = PUBSUB_SET + '/purge' | 36 PUBSUB_PURGE = PUBSUB_SET + '/purge' |
32 PUBSUB_DELETE = PUBSUB_SET + '/delete' | 37 PUBSUB_DELETE = PUBSUB_SET + '/delete' |
90 return iq | 95 return iq |
91 | 96 |
92 def success(self, result, iq): | 97 def success(self, result, iq): |
93 iq.swapAttributeValues("to", "from") | 98 iq.swapAttributeValues("to", "from") |
94 iq["type"] = 'result' | 99 iq["type"] = 'result' |
95 iq.children = result or [] | 100 iq.children = [] |
101 if result: | |
102 for child in result: | |
103 iq.addChild(child) | |
104 | |
96 return iq | 105 return iq |
97 | 106 |
98 def handler_wrapper(self, handler, iq): | 107 def handler_wrapper(self, handler, iq): |
99 try: | 108 try: |
100 d = handler(iq) | 109 d = handler(iq) |
278 def get_disco_info(self, node): | 287 def get_disco_info(self, node): |
279 info = [] | 288 info = [] |
280 | 289 |
281 if not node: | 290 if not node: |
282 info.append(disco.Feature(NS_PUBSUB + "#create-nodes")) | 291 info.append(disco.Feature(NS_PUBSUB + "#create-nodes")) |
292 info.append(disco.Feature(NS_PUBSUB + "#config-node")) | |
283 | 293 |
284 if self.backend.supports_instant_nodes(): | 294 if self.backend.supports_instant_nodes(): |
285 info.append(disco.Feature(NS_PUBSUB + "#instant-nodes")) | 295 info.append(disco.Feature(NS_PUBSUB + "#instant-nodes")) |
286 | 296 |
287 return defer.succeed(info) | 297 return defer.succeed(info) |
293 node = iq.pubsub.create.getAttribute("node") | 303 node = iq.pubsub.create.getAttribute("node") |
294 | 304 |
295 owner = jid.JID(iq["from"]).userhostJID() | 305 owner = jid.JID(iq["from"]).userhostJID() |
296 | 306 |
297 d = self.backend.create_node(node, owner) | 307 d = self.backend.create_node(node, owner) |
298 d.addCallback(self.return_create_response, iq) | 308 d.addCallback(self._return_create_response, iq) |
299 return d | 309 return d |
300 | 310 |
301 def return_create_response(self, result, iq): | 311 def _return_create_response(self, result, iq): |
302 node_id = iq.pubsub.create.getAttribute("node") | 312 node_id = iq.pubsub.create.getAttribute("node") |
303 if not node_id or node_id != result: | 313 if not node_id or node_id != result: |
304 reply = domish.Element((NS_PUBSUB, 'pubsub')) | 314 reply = domish.Element((NS_PUBSUB, 'pubsub')) |
305 entity = reply.addElement('create') | 315 entity = reply.addElement('create') |
306 entity['node'] = result | 316 entity['node'] = result |
308 | 318 |
309 def onConfigureGet(self, iq): | 319 def onConfigureGet(self, iq): |
310 self.handler_wrapper(self._onConfigureGet, iq) | 320 self.handler_wrapper(self._onConfigureGet, iq) |
311 | 321 |
312 def _onConfigureGet(self, iq): | 322 def _onConfigureGet(self, iq): |
313 raise NodeNotConfigurable | 323 try: |
324 node_id = iq.pubsub.configure["node"] | |
325 except KeyError: | |
326 raise NodeNotConfigurable | |
327 | |
328 d = self.backend.get_node_configuration(node_id) | |
329 d.addCallback(self._return_configuration_response, node_id) | |
330 return d | |
331 | |
332 def _return_configuration_response(self, options, node_id): | |
333 reply = domish.Element((NS_PUBSUB_OWNER, "pubsub")) | |
334 configure = reply.addElement("configure") | |
335 if node_id: | |
336 configure["node"] = node_id | |
337 form = configure.addElement((NS_X_DATA, "x")) | |
338 form["type"] = "form" | |
339 field = form.addElement("field") | |
340 field["var"] = "FORM_TYPE" | |
341 field["type"] = "hidden" | |
342 field.addElement("value", content=NS_PUBSUB + "#node_config") | |
343 | |
344 for option in options: | |
345 field = form.addElement("field") | |
346 field["var"] = option["var"] | |
347 field["type"] = option["type"] | |
348 field["label"] = option["label"] | |
349 field.addElement("value", content=option["value"]) | |
350 | |
351 return [reply] | |
314 | 352 |
315 def onConfigureSet(self, iq): | 353 def onConfigureSet(self, iq): |
316 self.handler_wrapper(self._onConfigureSet, iq) | 354 self.handler_wrapper(self._onConfigureSet, iq) |
317 | 355 |
318 def _onConfigureSet(self, iq): | 356 def _onConfigureSet(self, iq): |
319 raise NodeNotConfigurable | 357 try: |
358 node_id = iq.pubsub.configure["node"] | |
359 except KeyError: | |
360 raise BadRequest | |
361 | |
362 requestor = jid.JID(iq["from"]).userhostJID() | |
363 | |
364 for element in iq.pubsub.configure.elements(): | |
365 if element.name != 'x' or element.uri != NS_X_DATA: | |
366 continue | |
367 | |
368 type = element.getAttribute("type") | |
369 if type == "cancel": | |
370 return None | |
371 elif type != "submit": | |
372 continue | |
373 | |
374 try: | |
375 options = self._get_form_options(element) | |
376 except: | |
377 raise BadRequest | |
378 | |
379 if options["FORM_TYPE"] == NS_PUBSUB + "#node_config": | |
380 del options["FORM_TYPE"] | |
381 return self.backend.set_node_configuration(node_id, | |
382 options, | |
383 requestor) | |
384 | |
385 raise BadRequest | |
386 | |
387 def _get_form_options(self, form): | |
388 options = {} | |
389 | |
390 for element in form.elements(): | |
391 if element.name == 'field' and element.uri == NS_X_DATA: | |
392 options[element["var"]] = str(element.value) | |
393 | |
394 print options | |
395 return options | |
320 | 396 |
321 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService) | 397 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService) |
322 | 398 |
323 class ComponentServiceFromAffiliationsService(Service): | 399 class ComponentServiceFromAffiliationsService(Service): |
324 | 400 |