Mercurial > libervia-pubsub
annotate idavoll/pubsub.py @ 83:f3f31aa491df
Fix handling of node creation.
Announce pubsub#retrieve-items disco feature in
ComponentServiceFromItemRetrievalService.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 09 Nov 2004 15:57:23 +0000 |
parents | 995ba223a43b |
children | ec557449d1aa |
rev | line source |
---|---|
1 | 1 from twisted.protocols.jabber import component,jid |
2 | 2 from twisted.xish import utility, domish |
1 | 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 | 6 import backend |
7 import xmpp_error | |
73 | 8 import disco |
1 | 9 |
2 | 10 NS_COMPONENT = 'jabber:component:accept' |
11 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' | |
12 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' | |
16 | 13 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors' |
2 | 14 |
1 | 15 IQ_GET = '/iq[@type="get"]' |
16 IQ_SET = '/iq[@type="set"]' | |
2 | 17 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' |
18 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT | |
19 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | |
1 | 20 PUBSUB_CREATE = PUBSUB_SET + '/create' |
21 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | |
16 | 22 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
23 PUBSUB_UNSUBSCRIBE = PUBSUB_SET + '/unsubscribe' |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
24 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
21 | 25 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
26 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' |
21 | 27 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure' |
60
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
28 PUBSUB_AFFILIATIONS = PUBSUB_GET + '/affiliations' |
81
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
29 PUBSUB_ITEMS = PUBSUB_GET + '/items' |
1 | 30 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
31 class Error(Exception): |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
32 pubsub_error = None |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
33 stanza_error = None |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
34 msg = '' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
35 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
36 class NotImplemented(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
37 stanza_error = 'feature-not-implemented' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
38 |
47
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
39 class BadRequest(Error): |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
40 stanza_error = 'bad-request' |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
41 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
42 class OptionsUnavailable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
43 stanza_error = 'feature-not-implemented' |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
44 pubsub_error = 'subscription-options-unavailable' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
45 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
46 class SubscriptionOptionsUnavailable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
47 stanza_error = 'not-acceptable' |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
48 pubsub_error = 'subscription-options-unavailable' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
49 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
50 class NodeNotConfigurable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
51 stanza_error = 'feature-not-implemented' |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
52 pubsub_error = 'node-not-configurable' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
53 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
54 class CreateNodeNotConfigurable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
55 stanza_error = 'not-acceptable' |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
56 pubsub_error = 'node-not-configurable' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
57 |
47
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
58 |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
59 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
60 error_map = { |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
61 backend.NotAuthorized: ('not-authorized', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
62 backend.NodeNotFound: ('item-not-found', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
63 backend.NoPayloadAllowed: ('bad-request', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
64 backend.PayloadExpected: ('bad-request', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
65 backend.NoInstantNodes: ('not-acceptable', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
66 backend.NodeExists: ('conflict', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
67 backend.NotImplemented: ('feature-not-implemented', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
68 backend.NotSubscribed: ('not-authorized', 'requestor-not-subscribed'), |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
69 } |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
70 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
71 class Service(component.Service): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
72 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
73 __implements__ = component.IService |
1 | 74 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
75 def __init__(self, backend): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
76 self.backend = backend |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
77 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
78 def error(self, failure, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
79 try: |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
80 e = failure.trap(Error, *error_map.keys()) |
81
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
81 except: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
82 failure.printBriefTraceback() |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
83 xmpp_error.error_from_iq(iq, 'internal-server-error') |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
84 return iq |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
85 else: |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
86 if e == Error: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
87 stanza_error = failure.value.stanza_error |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
88 pubsub_error = failure.value.pubsub_error |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
89 msg = '' |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
90 else: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
91 stanza_error, pubsub_error = error_map[e] |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
92 msg = failure.value.msg |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
93 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
94 xmpp_error.error_from_iq(iq, stanza_error, msg) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
95 if pubsub_error: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
96 iq.error.addElement((NS_PUBSUB_ERRORS, pubsub_error)) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
97 return iq |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
98 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
99 def success(self, result, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
100 iq.swapAttributeValues("to", "from") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
101 iq["type"] = 'result' |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
102 iq.children = result or [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
103 return iq |
9 | 104 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
105 def handler_wrapper(self, handler, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
106 try: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
107 d = handler(iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
108 except: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
109 d = defer.fail() |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
110 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
111 d.addCallback(self.success, iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
112 d.addErrback(self.error, iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
113 d.addCallback(self.send) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
114 iq.handled = True |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
115 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
116 class ComponentServiceFromService(Service): |
12 | 117 |
73 | 118 def get_disco_info(self, node): |
119 info = [] | |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
120 |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
121 if not node: |
73 | 122 info.append(disco.Identity('pubsub', 'generic', |
123 'Generic Pubsub Service')) | |
124 | |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
125 if self.backend.supports_publisher_affiliation(): |
73 | 126 info.append(disco.Feature(NS_PUBSUB + "#publisher-affiliation")) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
127 |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
128 if self.backend.supports_outcast_affiliation(): |
73 | 129 info.append(disco.Feature(NS_PUBSUB + "#outcast-affiliation")) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
130 |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
131 if self.backend.supports_persistent_items(): |
73 | 132 info.append(disco.Feature(NS_PUBSUB + "#persistent-items")) |
133 | |
134 return defer.succeed(info) | |
135 else: | |
136 d = self.backend.get_node_type(node) | |
137 d.addCallback(lambda x: [disco.Identity('pubsub', x)]) | |
138 d.addErrback(lambda _: []) | |
139 return d | |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
140 |
73 | 141 def get_disco_items(self, node): |
142 if node: | |
143 return defer.succeed([]) | |
144 | |
145 d = self.backend.get_nodes() | |
146 d.addCallback(lambda nodes: [disco.Item(self.parent.jabberId, node) | |
147 for node in nodes]) | |
148 return d | |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
149 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
150 components.registerAdapter(ComponentServiceFromService, backend.IBackendService, component.IService) |
1 | 151 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
152 class ComponentServiceFromNotificationService(Service): |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
153 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
154 def componentConnected(self, xmlstream): |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
155 self.backend.register_notifier(self.notify) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
156 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
157 def notify(self, object): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
158 node_id = object["node_id"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
159 items = object["items"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
160 d = self.backend.get_notification_list(node_id, items) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
161 d.addCallback(self._notify, node_id) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
162 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
163 def _notify(self, list, node_id): |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
164 print list |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
165 for recipient, items in list.items(): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
166 self._notify_recipient(recipient, node_id, items) |
1 | 167 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
168 def _notify_recipient(self, recipient, node_id, itemlist): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
169 message = domish.Element((NS_COMPONENT, "message")) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
170 message["from"] = self.parent.jabberId |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
171 message["to"] = recipient |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
172 event = message.addElement((NS_PUBSUB_EVENT, "event")) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
173 items = event.addElement("items") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
174 items["node"] = node_id |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
175 items.children.extend(itemlist) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
176 self.send(message) |
1 | 177 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
178 components.registerAdapter(ComponentServiceFromNotificationService, backend.INotificationService, component.IService) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
179 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
180 class ComponentServiceFromPublishService(Service): |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
181 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
182 def componentConnected(self, xmlstream): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
183 xmlstream.addObserver(PUBSUB_PUBLISH, self.onPublish) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
184 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
185 def onPublish(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
186 self.handler_wrapper(self._onPublish, iq) |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
187 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
188 def _onPublish(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
189 node = iq.pubsub.publish["node"] |
1 | 190 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
191 items = [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
192 for child in iq.pubsub.publish.children: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
193 if child.__class__ == domish.Element and child.name == 'item': |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
194 items.append(child) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
195 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
196 print items |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
197 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
198 return self.backend.publish(node, items, |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
199 jid.JID(iq["from"]).userhostJID()) |
21 | 200 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
201 components.registerAdapter(ComponentServiceFromPublishService, backend.IPublishService, component.IService) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
202 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
203 class ComponentServiceFromSubscriptionService(Service): |
16 | 204 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
205 def componentConnected(self, xmlstream): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
206 xmlstream.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe) |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
207 xmlstream.addObserver(PUBSUB_UNSUBSCRIBE, self.onUnsubscribe) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
208 xmlstream.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
209 xmlstream.addObserver(PUBSUB_OPTIONS_SET, self.onOptionsSet) |
58
3e2e0040e3e0
Return support for the pubsub#subscribe feature.
Ralph Meijer <ralphm@ik.nu>
parents:
56
diff
changeset
|
210 |
73 | 211 def get_disco_info(self, node): |
212 info = [] | |
58
3e2e0040e3e0
Return support for the pubsub#subscribe feature.
Ralph Meijer <ralphm@ik.nu>
parents:
56
diff
changeset
|
213 |
3e2e0040e3e0
Return support for the pubsub#subscribe feature.
Ralph Meijer <ralphm@ik.nu>
parents:
56
diff
changeset
|
214 if not node: |
73 | 215 info.append(disco.Feature(NS_PUBSUB + '#subscribe')) |
58
3e2e0040e3e0
Return support for the pubsub#subscribe feature.
Ralph Meijer <ralphm@ik.nu>
parents:
56
diff
changeset
|
216 |
73 | 217 return defer.succeed(info) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
218 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
219 def onSubscribe(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
220 self.handler_wrapper(self._onSubscribe, iq) |
21 | 221 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
222 def _onSubscribe(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
223 if iq.pubsub.options: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
224 raise SubscribeOptionsUnavailable |
21 | 225 |
48
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
226 try: |
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
227 node_id = iq.pubsub.subscribe["node"] |
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
228 subscriber = jid.JID(iq.pubsub.subscribe["jid"]) |
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
229 except KeyError: |
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
230 raise BadRequest |
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
231 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
232 requestor = jid.JID(iq["from"]).userhostJID() |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
233 d = self.backend.subscribe(node_id, subscriber, requestor) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
234 d.addCallback(self.return_subscription) |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
235 return d |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
236 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
237 def return_subscription(self, result): |
60
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
238 reply = domish.Element((NS_PUBSUB, "pubsub")) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
239 entity = reply.addElement("entity") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
240 entity["node"] = result["node"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
241 entity["jid"] = result["jid"].full() |
48
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
242 entity["affiliation"] = result["affiliation"] or 'none' |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
243 entity["subscription"] = result["subscription"] |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
244 return [reply] |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
245 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
246 def onUnsubscribe(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
247 self.handler_wrapper(self._onUnsubscribe, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
248 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
249 def _onUnsubscribe(self, iq): |
47
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
250 try: |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
251 node_id = iq.pubsub.unsubscribe["node"] |
48
671ead538758
Check for malformed subscription request.
Ralph Meijer <ralphm@ik.nu>
parents:
47
diff
changeset
|
252 subscriber = jid.JID(iq.pubsub.unsubscribe["jid"]) |
47
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
253 except KeyError: |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
254 raise BadRequest |
31eb00734cc5
Check for malformed unsubscribe request.
Ralph Meijer <ralphm@ik.nu>
parents:
38
diff
changeset
|
255 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
256 requestor = jid.JID(iq["from"]).userhostJID() |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
257 return self.backend.unsubscribe(node_id, subscriber, requestor) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
258 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
259 def onOptionsGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
260 self.handler_wrapper(self._onOptionsGet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
261 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
262 def _onOptionsGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
263 raise OptionsUnavailable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
264 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
265 def onOptionsSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
266 self.handler_wrapper(self._onOptionsSet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
267 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
268 def _onOptionsSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
269 raise OptionsUnavailable |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
270 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
271 components.registerAdapter(ComponentServiceFromSubscriptionService, backend.ISubscriptionService, component.IService) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
272 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
273 class ComponentServiceFromNodeCreationService(Service): |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
274 |
68
a3d67cbab9c4
Return deferreds from getFeatures() and getIdentities().
Ralph Meijer <ralphm@ik.nu>
parents:
60
diff
changeset
|
275 def componentConnected(self, xmlstream): |
a3d67cbab9c4
Return deferreds from getFeatures() and getIdentities().
Ralph Meijer <ralphm@ik.nu>
parents:
60
diff
changeset
|
276 xmlstream.addObserver(PUBSUB_CREATE, self.onCreate) |
a3d67cbab9c4
Return deferreds from getFeatures() and getIdentities().
Ralph Meijer <ralphm@ik.nu>
parents:
60
diff
changeset
|
277 xmlstream.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) |
a3d67cbab9c4
Return deferreds from getFeatures() and getIdentities().
Ralph Meijer <ralphm@ik.nu>
parents:
60
diff
changeset
|
278 xmlstream.addObserver(PUBSUB_CONFIGURE_SET, self.onConfigureSet) |
a3d67cbab9c4
Return deferreds from getFeatures() and getIdentities().
Ralph Meijer <ralphm@ik.nu>
parents:
60
diff
changeset
|
279 |
73 | 280 def get_disco_info(self, node): |
281 info = [] | |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
282 |
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
283 if not node: |
73 | 284 info.append(disco.Feature(NS_PUBSUB + "#create-nodes")) |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
285 |
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
286 if self.backend.supports_instant_nodes(): |
73 | 287 info.append(disco.Feature(NS_PUBSUB + "#instant-nodes")) |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
288 |
73 | 289 return defer.succeed(info) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
290 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
291 def onCreate(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
292 self.handler_wrapper(self._onCreate, iq) |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
293 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
294 def _onCreate(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
295 if iq.pubsub.options: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
296 raise CreateNodeNotConfigurable |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
297 |
56
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
298 node = iq.pubsub.create.getAttribute("node") |
55fa890ef60b
Match backend's supported_*() methods in getFeatures() of ComponentServiceFromService.
Ralph Meijer <ralphm@ik.nu>
parents:
48
diff
changeset
|
299 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
300 owner = jid.JID(iq["from"]).userhostJID() |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
301 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
302 d = self.backend.create_node(node, owner) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
303 d.addCallback(self.return_create_response, iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
304 return d |
2 | 305 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
306 def return_create_response(self, result, iq): |
83 | 307 node_id = iq.pubsub.create.getAttribute("node") |
308 if not node_id or node_id != result: | |
60
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
309 reply = domish.Element((NS_PUBSUB, 'pubsub')) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
310 entity = reply.addElement('create') |
83 | 311 entity['node'] = result |
60
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
312 return [reply] |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
313 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
314 def onConfigureGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
315 self.handler_wrapper(self._onConfigureGet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
316 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
317 def _onConfigureGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
318 raise NodeNotConfigurable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
319 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
320 def onConfigureSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
321 self.handler_wrapper(self._onConfigureSet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
322 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
323 def _onConfigureSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
324 raise NodeNotConfigurable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
325 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
326 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService) |
60
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
327 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
328 class ComponentServiceFromAffiliationsService(Service): |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
329 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
330 def componentConnected(self, xmlstream): |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
331 xmlstream.addObserver(PUBSUB_AFFILIATIONS, self.onAffiliations) |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
332 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
333 def onAffiliations(self, iq): |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
334 self.handler_wrapper(self._onAffiliations, iq) |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
335 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
336 def _onAffiliations(self, iq): |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
337 d = self.backend.get_affiliations(jid.JID(iq["from"]).userhostJID()) |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
338 d.addCallback(self._return_affiliations_response, iq) |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
339 return d |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
340 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
341 def _return_affiliations_response(self, result, iq): |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
342 reply = domish.Element((NS_PUBSUB, 'pubsub')) |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
343 affiliations = reply.addElement('affiliations') |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
344 for r in result: |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
345 entity = affiliations.addElement('entity') |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
346 entity['node'] = r['node'] |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
347 entity['jid'] = r['jid'].full() |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
348 entity['affiliation'] = r['affiliation'] or 'none' |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
349 entity['subscription'] = r['subscription'] or 'none' |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
350 return [reply] |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
351 |
f6b7a06b8870
Implement retrieving affiliations.
Ralph Meijer <ralphm@ik.nu>
parents:
58
diff
changeset
|
352 components.registerAdapter(ComponentServiceFromAffiliationsService, backend.IAffiliationsService, component.IService) |
81
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
353 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
354 class ComponentServiceFromItemRetrievalService(Service): |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
355 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
356 def componentConnected(self, xmlstream): |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
357 xmlstream.addObserver(PUBSUB_ITEMS, self.onItems) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
358 |
83 | 359 def get_disco_info(self, node): |
360 info = [] | |
361 | |
362 if not node: | |
363 info.append(disco.Feature(NS_PUBSUB + "#retrieve-items")) | |
364 | |
365 return defer.succeed(info) | |
366 | |
81
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
367 def onItems(self, iq): |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
368 self.handler_wrapper(self._onItems, iq) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
369 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
370 def _onItems(self, iq): |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
371 try: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
372 node_id = iq.pubsub.items["node"] |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
373 except KeyError: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
374 raise BadRequest |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
375 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
376 max_items = iq.pubsub.items.getAttribute('max_items') |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
377 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
378 if max_items: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
379 try: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
380 max_items = int(max_items) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
381 except ValueError: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
382 raise BadRequest |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
383 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
384 item_ids = [] |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
385 for child in iq.pubsub.items.children: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
386 if child.name == 'item' and child.uri == NS_PUBSUB: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
387 try: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
388 item_ids.append(child["id"]) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
389 except KeyError: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
390 raise BadRequest |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
391 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
392 d = self.backend.get_items(node_id, jid.JID(iq["from"]), max_items, |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
393 item_ids) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
394 d.addCallback(self._return_items_response, node_id) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
395 return d |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
396 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
397 def _return_items_response(self, result, node_id): |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
398 reply = domish.Element((NS_PUBSUB, 'pubsub')) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
399 items = reply.addElement('items') |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
400 items["node"] = node_id |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
401 try: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
402 for r in result: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
403 items.addRawXml(r) |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
404 except Exception, e: |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
405 print e |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
406 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
407 return [reply] |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
408 |
995ba223a43b
Implemented ComponentServiceToItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
73
diff
changeset
|
409 components.registerAdapter(ComponentServiceFromItemRetrievalService, backend.IItemRetrievalService, component.IService) |