annotate idavoll/pubsub.py @ 23:884268687229

Simplify call chain by mapping incoming requests directly to method calls in pubsub.py, and providing a exception handler for all backend calls.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 07 Oct 2004 15:57:05 +0000
parents e01bbbfa8a46
children 256dcda26752
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.protocols.jabber import component,jid
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
2 from twisted.xish import utility, domish
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.python import components
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
4 from twisted.internet import defer
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
5
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 import backend
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 import xmpp_error
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
9 NS_COMPONENT = 'jabber:component:accept'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
10 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
11 NS_PUBSUB_EVENT = NS_PUBSUB + '#event'
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
12 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors'
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
13
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14 IQ_GET = '/iq[@type="get"]'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 IQ_SET = '/iq[@type="set"]'
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
16 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
17 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
18 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 PUBSUB_CREATE = PUBSUB_SET + '/create'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 PUBSUB_PUBLISH = PUBSUB_SET + '/publish'
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
21 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe'
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
22 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options'
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
23 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options'
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
24 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure'
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
25 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure'
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
27 class PubSubError(Exception):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
28 pubsub_error = None
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
29 msg = ''
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
30
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
31 class NotImplemented(PubSubError):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
32 pass
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
33
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
34 class OptionsUnavailable(PubSubError):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
35 pubsub_error = 'subscription-options-unavailable'
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
36
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
37 class SubscriptionOptionsUnavailable(PubSubError):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
38 pubsub_error = 'subscription-options-unavailable'
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
39
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
40 class NodeNotConfigurable(PubSubError):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
41 pubsub_error = 'node-not-configurable'
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
42
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
43 class CreateNodeNotConfigurable(PubSubError):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
44 pubsub_error = 'node-not-configurable'
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
45
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
46 error_map = {
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
47 backend.NotAuthorized: 'not-authorized',
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
48 backend.NodeNotFound: 'item-not-found',
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
49 backend.NoPayloadAllowed: 'bad-request',
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
50 backend.PayloadExpected: 'bad-request',
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
51 backend.NoInstantNodes: 'not-acceptable',
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
52 backend.NodeExists: 'conflict',
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
53 NotImplemented: 'feature-not-implemented',
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
54 OptionsUnavailable: 'feature-not-implemented',
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
55 SubscriptionOptionsUnavailable: 'not-acceptable',
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
56 NodeNotConfigurable: 'feature-not-implemented',
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
57 CreateNodeNotConfigurable: 'not-acceptable',
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
58 }
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
59
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
60 class ComponentServiceFromBackend(component.Service):
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 def __init__(self, backend):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 self.backend = backend
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
64 self.backend.pubsub_service = self
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
65
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 def componentConnected(self, xmlstream):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67 xmlstream.addObserver(PUBSUB_SET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68 xmlstream.addObserver(PUBSUB_GET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69
9
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
70 def getIdentities(self, node):
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
71 results = []
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
72 if not node:
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
73 results.append({
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
74 'category': 'pubsub',
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
75 'type': 'generic',
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
76 'name': 'Generic Pubsub Service'
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
77 })
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
78 return results
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
79
12
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
80 def getFeatures(self, node):
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
81 return [
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
82 "http://jabber.org/protocol/pubsub#outcast-affil",
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
83 "http://jabber.org/protocol/pubsub#publisher-affil",
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
84 "http://jabber.org/protocol/pubsub#persistent-items",
12
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
85 ]
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
86
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 def error(self, failure, iq):
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
88 try:
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
89 r = failure.trap(*error_map.keys())
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
90 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg)
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
91 if isinstance(failure.value, PubSubError) and \
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
92 failure.value.pubsub_error is not None:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
93 iq.error.addElement((NS_PUBSUB_ERRORS,
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
94 failure.value.pubsub_error),
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
95 NS_PUBSUB_ERRORS)
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
96 return iq
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
97 except:
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
98 xmpp_error.error_from_iq(iq, 'internal-server-error')
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
99 self.send(iq)
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
100 raise
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 def success(self, result, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 iq.swapAttributeValues("to", "from")
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104 iq["type"] = 'result'
18
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
105 iq.children = result or []
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 return iq
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
108 def onPubSub(self, iq):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
109 for elem in iq.pubsub.elements():
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
110 if not elem.hasAttribute('xmlns'):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
111 action = elem.name
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
112 break
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
113
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
114 if not action:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
115 return
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
116
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
117 try:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
118 try:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
119 handler = getattr(self, 'on%s%s' % (action.capitalize(),
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
120 iq["type"].capitalize()))
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
121 except KeyError:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
122 raise NotImplemented
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
123 else:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
124 d = handler(iq)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
125 except:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
126 d = defer.fail()
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
127
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
128 d.addCallback(self.success, iq)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
129 d.addErrback(self.error, iq)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
130 d.addCallback(self.send)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 iq.handled = True
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
132
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
133 # action handlers
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
134
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
135 def onPublishSet(self, iq):
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136 node = iq.pubsub.publish["node"]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
138 items = []
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
139 for child in iq.pubsub.publish.children:
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
140 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
141 items.append(child)
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
142
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
143 print items
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
144
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
145 return self.backend.do_publish(node,
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
146 jid.JID(iq["from"]).userhost(),
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
147 items)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
148
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
149 def onOptionsGet(self, iq):
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
150 raise OptionsUnavailable
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
151
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
152 def onOptionsSet(self, iq):
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
153 raise OptionsUnavailable
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
154
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
155 def onConfigureGet(self, iq):
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
156 raise NodeNotConfigurable
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
157
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
158 def onConfigureSet(self, iq):
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
159 raise NodeNotConfigurable
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
160
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
161 def onSubscribeSet(self, iq):
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
162 if iq.pubsub.options:
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
163 raise SubscribeOptionsUnavailable
21
e01bbbfa8a46 Implemented node creation.
Ralph Meijer <ralphm@ik.nu>
parents: 18
diff changeset
164
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
165 node_id = iq.pubsub.subscribe["node"]
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
166 subscriber = jid.JID(iq.pubsub.subscribe["jid"])
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
167 requestor = jid.JID(iq["from"]).userhostJID()
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
168 d = self.backend.do_subscribe(node_id, subscriber, requestor)
18
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
169 d.addCallback(self.return_subscription)
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
170 d.addCallback(self.succeed, iq)
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
171 d.addErrback(self.error, iq)
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
172 d.addCallback(self.send)
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
173
18
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
174 def return_subscription(self, result):
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
175 reply = domish.Element("pubsub", NS_PUBSUB)
16
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
176 entity = reply.addElement("entity")
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
177 entity["node"] = result["node"]
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
178 entity["jid"] = result["jid"].full()
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
179 entity["affiliation"] = result["affiliation"]
ce3d0db64da1 Implemented basic subscribing.
Ralph Meijer <ralphm@ik.nu>
parents: 12
diff changeset
180 entity["subscription"] = result["subscription"]
18
7937d6fbbe2a Small code cleanups
Ralph Meijer <ralphm@ik.nu>
parents: 16
diff changeset
181 return reply
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
182
23
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
183 def onCreateSet(self, iq):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
184 if iq.pubsub.options:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
185 raise CreateNodeNotConfigurable
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
186
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
187 node = iq.pubsub.create["node"]
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
188 owner = jid.JID(iq["from"]).userhostJID()
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
189
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
190 d = self.backend.create_node(node, owner)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
191 d.addCallback(self.return_create_response, iq)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
192 return d
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
193
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
194 def return_create_response(self, result, iq):
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
195 if iq.pubsub.create["node"] is None:
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
196 reply = domish.Element('pubsub', NS_PUBSUB)
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
197 entity = reply.addElement('create')
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
198 entity['node'] = result['node_id']
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
199 return reply
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
200
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
201 # other methods
884268687229 Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents: 21
diff changeset
202
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
203 def do_notification(self, list, node):
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
204 for recipient, items in list.items():
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
205 self.notify(node, items, recipient)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
206
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
207 def notify(self, node, itemlist, recipient):
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
208 message = domish.Element((NS_COMPONENT, "message"))
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
209 message["from"] = self.parent.jabberId
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
210 message["to"] = recipient
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
211 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
212 items = x.addElement("items")
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
213 items["node"] = node
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
214 items.children.extend(itemlist)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
215 self.send(message)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
216
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
217 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
218