Mercurial > libervia-pubsub
annotate idavoll/pubsub.py @ 38:c9ddca3cce20
Change exception classes to include stanza error.
Change name of exception base class to Error.
Change backend error map to have a tuple as value: stanza-error, pubsub-error.
Remove empty componentConnected() definition in Service.
Move registering of notification from __init__ to componentConnected in
ComponentServiceFromNotificationService.
Add observers for subscribe, options and configure.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sun, 31 Oct 2004 16:03:50 +0000 |
parents | fa866793075d |
children | 31eb00734cc5 |
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 | |
8 | |
2 | 9 NS_COMPONENT = 'jabber:component:accept' |
10 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' | |
11 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' | |
16 | 12 NS_PUBSUB_ERRORS = NS_PUBSUB + '#errors' |
2 | 13 |
1 | 14 IQ_GET = '/iq[@type="get"]' |
15 IQ_SET = '/iq[@type="set"]' | |
2 | 16 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' |
17 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT | |
18 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | |
1 | 19 PUBSUB_CREATE = PUBSUB_SET + '/create' |
20 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | |
16 | 21 PUBSUB_SUBSCRIBE = PUBSUB_SET + '/subscribe' |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
22 PUBSUB_UNSUBSCRIBE = PUBSUB_SET + '/unsubscribe' |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
23 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
21 | 24 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
25 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' |
21 | 26 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure' |
1 | 27 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
28 class Error(Exception): |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
29 pubsub_error = None |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
30 stanza_error = None |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
31 msg = '' |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
32 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
33 class NotImplemented(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
34 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
|
35 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
36 class OptionsUnavailable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
37 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
|
38 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
|
39 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
40 class SubscriptionOptionsUnavailable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
41 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
|
42 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
|
43 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
44 class NodeNotConfigurable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
45 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
|
46 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
|
47 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
48 class CreateNodeNotConfigurable(Error): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
49 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
|
50 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
|
51 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
52 error_map = { |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
53 backend.NotAuthorized: ('not-authorized', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
54 backend.NodeNotFound: ('item-not-found', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
55 backend.NoPayloadAllowed: ('bad-request', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
56 backend.PayloadExpected: ('bad-request', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
57 backend.NoInstantNodes: ('not-acceptable', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
58 backend.NodeExists: ('conflict', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
59 backend.NotImplemented: ('feature-not-implemented', None), |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
60 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
|
61 } |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
62 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
63 class Service(component.Service): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
64 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
65 __implements__ = component.IService |
1 | 66 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
67 def __init__(self, backend): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
68 self.backend = backend |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
69 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
70 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
|
71 try: |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
72 e = failure.trap(Error, *error_map.keys()) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
73 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
74 if e == Error: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
75 stanza_error = failure.value.stanza_error |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
76 pubsub_error = failure.value.pubsub_error |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
77 msg = '' |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
78 else: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
79 stanza_error, pubsub_error = error_map[e] |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
80 msg = failure.value.msg |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
81 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
82 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
|
83 if pubsub_error: |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
84 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
|
85 return iq |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
86 except: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
87 xmpp_error.error_from_iq(iq, 'internal-server-error') |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
88 self.send(iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
89 raise |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
90 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
91 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
|
92 iq.swapAttributeValues("to", "from") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
93 iq["type"] = 'result' |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
94 iq.children = result or [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
95 return iq |
9 | 96 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
97 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
|
98 try: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
99 d = handler(iq) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
100 except: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
101 d = defer.fail() |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
102 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
103 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
|
104 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
|
105 d.addCallback(self.send) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
106 iq.handled = True |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
107 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
108 class ComponentServiceFromService(Service): |
12 | 109 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
110 def getIdentities(self, node): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
111 results = [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
112 if not node: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
113 results.append({ |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
114 'category': 'pubsub', |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
115 'type': 'generic', |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
116 'name': 'Generic Pubsub Service' |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
117 }) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
118 return results |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
119 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
120 def getFeatures(self, node): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
121 features = [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
122 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
123 affiliations = self.backend.get_supported_affiliations() |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
124 if 'outcast' in affiliations: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
125 features.append("http://jabber.org/protocol/pubsub#outcast-affil") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
126 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
127 if 'publisher' in affiliations: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
128 features.append("http://jabber.org/protocol/pubsub#publisher-affil") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
129 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
130 # "http://jabber.org/protocol/pubsub#persistent-items" |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
131 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
132 return features |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
133 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
134 components.registerAdapter(ComponentServiceFromService, backend.IBackendService, component.IService) |
1 | 135 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
136 class ComponentServiceFromNotificationService(Service): |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
137 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
138 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
|
139 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
|
140 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
141 def notify(self, object): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
142 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
|
143 items = object["items"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
144 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
|
145 d.addCallback(self._notify, node_id) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
146 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
147 def _notify(self, list, node_id): |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
148 print list |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
149 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
|
150 self._notify_recipient(recipient, node_id, items) |
1 | 151 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 message["to"] = recipient |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
156 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
|
157 items = event.addElement("items") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
158 items["node"] = node_id |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
159 items.children.extend(itemlist) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
160 self.send(message) |
1 | 161 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
162 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
|
163 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
164 class ComponentServiceFromPublishService(Service): |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
165 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
166 def componentConnected(self, xmlstream): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
167 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
|
168 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
169 def onPublish(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
170 self.handler_wrapper(self._onPublish, iq) |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
171 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
172 def _onPublish(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
173 node = iq.pubsub.publish["node"] |
1 | 174 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
175 items = [] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
176 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
|
177 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
|
178 items.append(child) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
179 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
180 print items |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
181 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
182 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
|
183 jid.JID(iq["from"]).userhostJID()) |
21 | 184 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
185 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
|
186 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
187 class ComponentServiceFromSubscriptionService(Service): |
16 | 188 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
189 def componentConnected(self, xmlstream): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
190 xmlstream.addObserver(PUBSUB_SUBSCRIBE, self.onSubscribe) |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
191 xmlstream.addObserver(PUBSUB_UNSUBSCRIBE, self.onUnsubscribe) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
192 xmlstream.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
193 xmlstream.addObserver(PUBSUB_OPTIONS_SET, self.onOptionsSet) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
194 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
195 def onSubscribe(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
196 self.handler_wrapper(self._onSubscribe, iq) |
21 | 197 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
198 def _onSubscribe(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
199 if iq.pubsub.options: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
200 raise SubscribeOptionsUnavailable |
21 | 201 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
202 node_id = iq.pubsub.subscribe["node"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
203 subscriber = jid.JID(iq.pubsub.subscribe["jid"]) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
204 requestor = jid.JID(iq["from"]).userhostJID() |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
205 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
|
206 d.addCallback(self.return_subscription) |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
207 return d |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
208 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
209 def return_subscription(self, result): |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
210 reply = domish.Element((NS_PUBSUB, "pubsub"), NS_PUBSUB) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
211 entity = reply.addElement("entity") |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
212 entity["node"] = result["node"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
213 entity["jid"] = result["jid"].full() |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
214 entity["affiliation"] = result["affiliation"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
215 entity["subscription"] = result["subscription"] |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
216 return [reply] |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
217 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
218 def onUnsubscribe(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
219 self.handler_wrapper(self._onUnsubscribe, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
220 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
221 def _onUnsubscribe(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
222 node_id = iq.pubsub.unsubscribe["node"] |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
223 subscriber = jid.JID(iq.pubsub.unsubscribe["jid"]) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
224 requestor = jid.JID(iq["from"]).userhostJID() |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
225 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
|
226 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
227 def onOptionsGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
228 self.handler_wrapper(self._onOptionsGet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
229 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
230 def _onOptionsGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
231 raise OptionsUnavailable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
232 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
233 def onOptionsSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
234 self.handler_wrapper(self._onOptionsSet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
235 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
236 def _onOptionsSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
237 raise OptionsUnavailable |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
238 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
239 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
|
240 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
241 class ComponentServiceFromNodeCreationService(Service): |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
242 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
243 def componentConnected(self, xmlstream): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
244 xmlstream.addObserver(PUBSUB_CREATE, self.onCreate) |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
245 xmlstream.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
246 xmlstream.addObserver(PUBSUB_CONFIGURE_SET, self.onConfigureSet) |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
247 |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
248 def onCreate(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
249 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
|
250 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
251 def _onCreate(self, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
252 if iq.pubsub.options: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
253 raise CreateNodeNotConfigurable |
23
884268687229
Simplify call chain by mapping incoming requests directly to method
Ralph Meijer <ralphm@ik.nu>
parents:
21
diff
changeset
|
254 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
255 node = iq.pubsub.create["node"] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
256 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
|
257 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
258 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
|
259 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
|
260 return d |
2 | 261 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
262 def return_create_response(self, result, iq): |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
263 if iq.pubsub.create["node"] is None: |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
264 reply = domish.Element('pubsub', NS_PUBSUB) |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
265 entity = reply.addElement('create') |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
266 entity['node'] = result['node_id'] |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
267 return reply |
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
268 |
38
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
269 def onConfigureGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
270 self.handler_wrapper(self._onConfigureGet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
271 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
272 def _onConfigureGet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
273 raise NodeNotConfigurable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
274 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
275 def onConfigureSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
276 self.handler_wrapper(self._onConfigureSet, iq) |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
277 |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
278 def _onConfigureSet(self, iq): |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
279 raise NodeNotConfigurable |
c9ddca3cce20
Change exception classes to include stanza error.
Ralph Meijer <ralphm@ik.nu>
parents:
31
diff
changeset
|
280 |
31
fa866793075d
Split up implementation in several Services that match the division
Ralph Meijer <ralphm@ik.nu>
parents:
25
diff
changeset
|
281 components.registerAdapter(ComponentServiceFromNodeCreationService, backend.INodeCreationService, component.IService) |