Mercurial > libervia-pubsub
annotate idavoll/pubsub.py @ 10:36f0ce354ce8
Initial revision
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Fri, 09 Jul 2004 15:44:23 +0000 |
parents | 52bd563b7a5d |
children | d45e921a5d2a |
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 |
4 import backend | |
5 import xmpp_error | |
6 | |
2 | 7 NS_COMPONENT = 'jabber:component:accept' |
8 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' | |
9 NS_PUBSUB_EVENT = NS_PUBSUB + '#event' | |
10 | |
1 | 11 IQ_GET = '/iq[@type="get"]' |
12 IQ_SET = '/iq[@type="set"]' | |
2 | 13 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' |
14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT | |
15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT | |
1 | 16 PUBSUB_CREATE = PUBSUB_SET + '/create' |
17 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
18 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
19 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' |
1 | 20 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
21 error_map = { |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
22 backend.NotAuthorized: 'not-authorized', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
23 backend.NodeNotFound: 'item-not-found', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
24 backend.NoPayloadAllowed: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
25 backend.PayloadExpected: 'bad-request', |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
26 } |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
27 |
1 | 28 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): |
29 | |
30 def __init__(self, backend): | |
31 utility.EventDispatcher.__init__(self) | |
32 self.backend = backend | |
2 | 33 self.backend.pubsub_service = self |
1 | 34 self.addObserver(PUBSUB_PUBLISH, self.onPublish) |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
35 self.addObserver(PUBSUB_OPTIONS_GET, self.onOptionsGet) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
36 self.addObserver(PUBSUB_CONFIGURE_GET, self.onConfigureGet) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
37 self.addObserver(PUBSUB_GET, self.notImplemented, -1) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
38 self.addObserver(PUBSUB_SET, self.notImplemented, -1) |
1 | 39 |
40 def componentConnected(self, xmlstream): | |
41 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) | |
42 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) | |
43 | |
9 | 44 def getIdentities(self, node): |
45 results = [] | |
46 if not node: | |
47 results.append({ | |
48 'category': 'pubsub', | |
49 'type': 'generic', | |
50 'name': 'Generic Pubsub Service' | |
51 }) | |
52 return results | |
53 | |
1 | 54 def error(self, failure, iq): |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
55 r = failure.trap(*error_map.keys()) |
1 | 56 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
57 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg) |
1 | 58 |
59 return iq | |
60 | |
61 def success(self, result, iq): | |
62 iq.swapAttributeValues("to", "from") | |
63 iq["type"] = 'result' | |
64 iq.children = [] | |
65 return iq | |
66 | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
67 def notImplemented(self, iq): |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
68 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented')) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
69 |
1 | 70 def onPubSub(self, iq): |
71 self.dispatch(iq) | |
72 iq.handled = True | |
73 | |
74 def onPublish(self, iq): | |
75 node = iq.pubsub.publish["node"] | |
76 | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
77 items = [] |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
78 for child in iq.pubsub.publish.children: |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
79 if child.__class__ == domish.Element and child.name == 'item': |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
80 items.append(child) |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
81 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
82 print items |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
83 |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
84 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), items) |
1 | 85 d.addCallback(self.success, iq) |
86 d.addErrback(self.error, iq) | |
87 d.addCallback(self.send) | |
88 | |
7
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
89 def onOptionsGet(self, iq): |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
90 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented', 'No subscriber options available')) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
91 |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
92 def onConfigureGet(self, iq): |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
93 self.send(xmpp_error.error_from_iq(iq, 'feature-not-implemented', 'Node can not be configured')) |
a8cfb31dc50c
Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents:
4
diff
changeset
|
94 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
95 def do_notification(self, list, node): |
1 | 96 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
97 for recipient, items in list.items(): |
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
98 self.notify(node, items, recipient) |
2 | 99 |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
100 def notify(self, node, itemlist, recipient): |
2 | 101 message = domish.Element((NS_COMPONENT, "message")) |
102 message["from"] = self.parent.jabberId | |
103 message["to"] = recipient | |
104 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT) | |
105 items = x.addElement("items") | |
106 items["node"] = node | |
4
ea195dc1732d
Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
107 items.children.extend(itemlist) |
2 | 108 self.send(message) |
109 | |
1 | 110 """ |
111 def onCreateSet(self, iq): | |
112 node = iq.pubsub.create["node"] | |
113 owner = jid.JID(iq["from"]).userhost() | |
114 | |
115 try: | |
116 node = self.backend.create_node(node, owner) | |
117 | |
118 if iq.pubsub.create["node"] == None: | |
119 # also show node name | |
120 except: | |
121 pass | |
122 | |
123 iq.handled = True | |
124 """ | |
125 | |
126 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService) | |
127 |