comparison idavoll/pubsub.py @ 110:7043839982ba

Use storage. Fixup some small errors.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 10:17:10 +0000
parents 8d8946e67fcb
children 8527bce09862
comparison
equal deleted inserted replaced
109:9fb8f0867d02 110:7043839982ba
3 from twisted.python import components 3 from twisted.python import components
4 from twisted.internet import defer 4 from twisted.internet import defer
5 from zope.interface import implements 5 from zope.interface import implements
6 6
7 import backend 7 import backend
8 import storage
8 import xmpp_error 9 import xmpp_error
9 import disco 10 import disco
10 import data_form 11 import data_form
11 12
12 NS_COMPONENT = 'jabber:component:accept' 13 NS_COMPONENT = 'jabber:component:accept'
55 class NodeNotConfigurable(Error): 56 class NodeNotConfigurable(Error):
56 stanza_error = 'feature-not-implemented' 57 stanza_error = 'feature-not-implemented'
57 pubsub_error = 'node-not-configurable' 58 pubsub_error = 'node-not-configurable'
58 59
59 error_map = { 60 error_map = {
60 backend.NotAuthorized: ('not-authorized', None), 61 storage.NodeNotFound: ('item-not-found', None),
61 backend.NodeNotFound: ('item-not-found', None), 62 storage.NodeExists: ('conflict', None),
62 backend.NoPayloadAllowed: ('bad-request', None), 63
63 backend.PayloadExpected: ('bad-request', None), 64 backend.NotAuthorized: ('not-authorized', None),
64 backend.NoInstantNodes: ('not-acceptable', None), 65 backend.NoPayloadAllowed: ('bad-request', None),
65 backend.NodeExists: ('conflict', None), 66 backend.PayloadExpected: ('bad-request', None),
66 backend.NotImplemented: ('feature-not-implemented', None), 67 backend.NoInstantNodes: ('not-acceptable', None),
67 backend.NotSubscribed: ('not-authorized', 'requestor-not-subscribed'), 68 backend.NotImplemented: ('feature-not-implemented', None),
69 backend.NotSubscribed: ('not-authorized', 'requestor-not-subscribed'),
70 backend.InvalidConfigurationOption: ('not-acceptable', None),
71 backend.InvalidConfigurationValue: ('not-acceptable', None),
68 } 72 }
69 73
70 class Service(component.Service): 74 class Service(component.Service):
71 75
72 implements(component.IService) 76 implements(component.IService)
109 try: 113 try:
110 d = handler(iq) 114 d = handler(iq)
111 except: 115 except:
112 d = defer.fail() 116 d = defer.fail()
113 117
118
114 d.addCallback(self.success, iq) 119 d.addCallback(self.success, iq)
115 d.addErrback(self.error, iq) 120 d.addErrback(self.error, iq)
116 d.addCallback(self.send) 121 d.addCallback(self.send)
117 iq.handled = True 122 iq.handled = True
118 123
142 else: 147 else:
143 try: 148 try:
144 d = self.backend.get_node_type(node) 149 d = self.backend.get_node_type(node)
145 d.addCallback(self._add_identity, [], node) 150 d.addCallback(self._add_identity, [], node)
146 d.addErrback(lambda _: []) 151 d.addErrback(lambda _: [])
147 except backend.NodeNotFound: 152 except storage.NodeNotFound:
148 return defer.succeed([]) 153 return defer.succeed([])
149 return d 154 return d
150 155
151 def _add_identity(self, node_type, result_list, node): 156 def _add_identity(self, node_type, result_list, node):
152 result_list.append(disco.Identity('pubsub', node_type)) 157 result_list.append(disco.Identity('pubsub', node_type))
186 items = object["items"] 191 items = object["items"]
187 d = self.backend.get_notification_list(node_id, items) 192 d = self.backend.get_notification_list(node_id, items)
188 d.addCallback(self._notify, node_id) 193 d.addCallback(self._notify, node_id)
189 194
190 def _notify(self, list, node_id): 195 def _notify(self, list, node_id):
191 for recipient, items in list.items(): 196 for recipient, items in list:
192 self._notify_recipient(recipient, node_id, items) 197 self._notify_recipient(recipient, node_id, items)
193 198
194 def _notify_recipient(self, recipient, node_id, itemlist): 199 def _notify_recipient(self, recipient, node_id, itemlist):
195 message = domish.Element((NS_COMPONENT, "message")) 200 message = domish.Element((NS_COMPONENT, "message"))
196 message["from"] = self.parent.jabberId 201 message["from"] = self.parent.jabberId
197 message["to"] = recipient 202 message["to"] = recipient.full()
198 event = message.addElement((NS_PUBSUB_EVENT, "event")) 203 event = message.addElement((NS_PUBSUB_EVENT, "event"))
199 items = event.addElement("items") 204 items = event.addElement("items")
200 items["node"] = node_id 205 items["node"] = node_id
201 items.children.extend(itemlist) 206 items.children.extend(itemlist)
202 self.send(message) 207 self.send(message)
218 223
219 def onPublish(self, iq): 224 def onPublish(self, iq):
220 self.handler_wrapper(self._onPublish, iq) 225 self.handler_wrapper(self._onPublish, iq)
221 226
222 def _onPublish(self, iq): 227 def _onPublish(self, iq):
223 node = iq.pubsub.publish["node"] 228 try:
229 node = iq.pubsub.publish["node"]
230 except KeyError:
231 raise BadRequest
224 232
225 items = [] 233 items = []
226 for child in iq.pubsub.publish.children: 234 for child in iq.pubsub.publish.children:
227 if child.__class__ == domish.Element and child.name == 'item': 235 if child.__class__ == domish.Element and child.name == 'item':
228 items.append(child) 236 items.append(child)
360 form_type=NS_PUBSUB + "#node_config") 368 form_type=NS_PUBSUB + "#node_config")
361 369
362 for option in options: 370 for option in options:
363 form.add_field_single(**option) 371 form.add_field_single(**option)
364 372
373 form.parent = configure
365 configure.addChild(form) 374 configure.addChild(form)
366 375
367 return [reply] 376 return [reply]
368 377
369 def onConfigureSet(self, iq): 378 def onConfigureSet(self, iq):
376 raise BadRequest 385 raise BadRequest
377 386
378 requestor = jid.JID(iq["from"]).userhostJID() 387 requestor = jid.JID(iq["from"]).userhostJID()
379 388
380 for element in iq.pubsub.configure.elements(): 389 for element in iq.pubsub.configure.elements():
381 if element.name != 'x' or element.uri != NS_X_DATA: 390 if element.name != 'x' or element.uri != data_form.NS_X_DATA:
382 continue 391 continue
383 392
384 type = element.getAttribute("type") 393 type = element.getAttribute("type")
385 if type == "cancel": 394 if type == "cancel":
386 return None 395 return None
387 elif type != "submit": 396 elif type != "submit":
388 continue 397 continue
389 398
390 try: 399 options = self._get_form_options(element)
391 options = self._get_form_options(element)
392 except:
393 raise BadRequest
394 400
395 if options["FORM_TYPE"] == NS_PUBSUB + "#node_config": 401 if options["FORM_TYPE"] == NS_PUBSUB + "#node_config":
396 del options["FORM_TYPE"] 402 del options["FORM_TYPE"]
397 return self.backend.set_node_configuration(node_id, 403 return self.backend.set_node_configuration(node_id,
398 options, 404 options,
402 408
403 def _get_form_options(self, form): 409 def _get_form_options(self, form):
404 options = {} 410 options = {}
405 411
406 for element in form.elements(): 412 for element in form.elements():
407 if element.name == 'field' and element.uri == NS_X_DATA: 413 if element.name == 'field' and element.uri == data_form.NS_X_DATA:
408 options[element["var"]] = str(element.value) 414 try:
415 options[element["var"]] = str(element.value)
416 except (KeyError, AttributeError):
417 raise BadRequest
409 418
410 return options 419 return options
411 420
412 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService) 421 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService)
413 422