comparison idavoll/pubsub.py @ 16:ce3d0db64da1

Implemented basic subscribing. Reply with internal-server-error when an unhandled exception is encountered. Add special pubsub#errors elements to a few error replies. Announce supported feature pubsub#persistent-items.
author Ralph Meijer <ralphm@ik.nu>
date Sat, 17 Jul 2004 22:08:28 +0000
parents d45e921a5d2a
children 7937d6fbbe2a
comparison
equal deleted inserted replaced
15:46cd13c68ac0 16:ce3d0db64da1
5 import xmpp_error 5 import xmpp_error
6 6
7 NS_COMPONENT = 'jabber:component:accept' 7 NS_COMPONENT = 'jabber:component:accept'
8 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' 8 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
9 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' 9 NS_PUBSUB_EVENT = NS_PUBSUB + '#event'
10 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors'
10 11
11 IQ_GET = '/iq[@type="get"]' 12 IQ_GET = '/iq[@type="get"]'
12 IQ_SET = '/iq[@type="set"]' 13 IQ_SET = '/iq[@type="set"]'
13 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' 14 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]'
14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT 15 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT
15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT 16 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT
16 PUBSUB_CREATE = PUBSUB_SET + '/create' 17 PUBSUB_CREATE = PUBSUB_SET + '/create'
17 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' 18 PUBSUB_PUBLISH = PUBSUB_SET + '/publish'
19 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe'
18 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' 20 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options'
19 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' 21 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure'
20 22
21 error_map = { 23 error_map = {
22 backend.NotAuthorized: 'not-authorized', 24 backend.NotAuthorized: 'not-authorized',
30 def __init__(self, backend): 32 def __init__(self, backend):
31 utility.EventDispatcher.__init__(self) 33 utility.EventDispatcher.__init__(self)
32 self.backend = backend 34 self.backend = backend
33 self.backend.pubsub_service = self 35 self.backend.pubsub_service = self
34 self.addObserver(PUBSUB_PUBLISH, self.onPublish) 36 self.addObserver(PUBSUB_PUBLISH, self.onPublish)
37 self.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe)
35 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) 38 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet)
36 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) 39 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet)
37 self.addObserver(PUBSUB_GET, self.notImplemented, -1) 40 self.addObserver(PUBSUB_GET, self.notImplemented, -1)
38 self.addObserver(PUBSUB_SET, self.notImplemented, -1) 41 self.addObserver(PUBSUB_SET, self.notImplemented, -1)
39 42
53 56
54 def getFeatures(self, node): 57 def getFeatures(self, node):
55 return [ 58 return [
56 "http://jabber.org/protocol/pubsub#outcast-affil", 59 "http://jabber.org/protocol/pubsub#outcast-affil",
57 "http://jabber.org/protocol/pubsub#publisher-affil", 60 "http://jabber.org/protocol/pubsub#publisher-affil",
58 # We do not really store items yet 61 "http://jabber.org/protocol/pubsub#persistent-items",
59 # "http://jabber.org/protocol/pubsub#persistent-items",
60 ] 62 ]
61 63
62 def error(self, failure, iq): 64 def error(self, failure, iq):
63 r = failure.trap(*error_map.keys()) 65 try:
64 66 r = failure.trap(*error_map.keys())
65 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg) 67 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg)
66 68 return iq
67 return iq 69 except:
70 xmpp_error.error_from_iq(iq, 'internal-server-error')
71 self.send(iq)
72 raise
68 73
69 def success(self, result, iq): 74 def success(self, result, iq):
70 iq.swapAttributeValues("to", "from") 75 iq.swapAttributeValues("to", "from")
71 iq["type"] = 'result' 76 iq["type"] = 'result'
72 iq.children = [] 77 iq.children = []
93 d.addCallback(self.success, iq) 98 d.addCallback(self.success, iq)
94 d.addErrback(self.error, iq) 99 d.addErrback(self.error, iq)
95 d.addCallback(self.send) 100 d.addCallback(self.send)
96 101
97 def onOptionsGet(self, iq): 102 def onOptionsGet(self, iq):
98 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented', 'No subscriber options available')) 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)
99 106
100 def onConfigureGet(self, iq): 107 def onConfigureGet(self, iq):
101 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented', 'Node can not be configured')) 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)
117 d.addCallback(self.return_subscription, iq)
118 d.addErrback(self.error, iq)
119 d.addCallback(self.send)
120
121 def return_subscription(self, result, iq):
122 reply = self.success(result, iq)
123 entity = reply.addElement("entity")
124 entity["node"] = result["node"]
125 entity["jid"] = result["jid"].full()
126 entity["affiliation"] = result["affiliation"]
127 entity["subscription"] = result["subscription"]
128 return iq
102 129
103 def do_notification(self, list, node): 130 def do_notification(self, list, node):
104 131
105 for recipient, items in list.items(): 132 for recipient, items in list.items():
106 self.notify(node, items, recipient) 133 self.notify(node, items, recipient)