annotate idavoll/pubsub.py @ 14:68e900b46d49

Send service-unavailable for unknown iq child namespaces
author Ralph Meijer <ralphm@ik.nu>
date Sat, 17 Jul 2004 22:03:37 +0000
parents d45e921a5d2a
children ce3d0db64da1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.protocols.jabber import component,jid
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
2 from twisted.xish import utility, domish
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.python import components
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 import backend
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 import xmpp_error
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
7 NS_COMPONENT = 'jabber:component:accept'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
8 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
9 NS_PUBSUB_EVENT = NS_PUBSUB + '#event'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
10
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 IQ_GET = '/iq[@type="get"]'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 IQ_SET = '/iq[@type="set"]'
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
13 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]'
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 PUBSUB_CREATE = PUBSUB_SET + '/create'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 def __init__(self, backend):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 utility.EventDispatcher.__init__(self)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 self.backend = backend
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
33 self.backend.pubsub_service = self
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 def componentConnected(self, xmlstream):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41 xmlstream.addObserver(PUBSUB_SET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 xmlstream.addObserver(PUBSUB_GET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43
9
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
44 def getIdentities(self, node):
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
45 results = []
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
46 if not node:
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
47 results.append({
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
48 'category': 'pubsub',
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
49 'type': 'generic',
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
50 'name': 'Generic Pubsub Service'
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
51 })
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
52 return results
52bd563b7a5d Add disco support.
Ralph Meijer <ralphm@ik.nu>
parents: 7
diff changeset
53
12
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
54 def getFeatures(self, node):
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
55 return [
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
56 "http://jabber.org/protocol/pubsub#outcast-affil",
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
57 "http://jabber.org/protocol/pubsub#publisher-affil",
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
58 # We do not really store items yet
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
59 # "http://jabber.org/protocol/pubsub#persistent-items",
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
60 ]
d45e921a5d2a Return implemented features
Ralph Meijer <ralphm@ik.nu>
parents: 9
diff changeset
61
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 def error(self, failure, iq):
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
63 r = failure.trap(*error_map.keys())
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
65 xmpp_error.error_from_iq(iq, error_map[r], failure.value.msg)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67 return iq
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 def success(self, result, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 iq.swapAttributeValues("to", "from")
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71 iq["type"] = 'result'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 iq.children = []
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73 return iq
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
75 def notImplemented(self, iq):
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
76 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
77
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 def onPubSub(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 self.dispatch(iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 iq.handled = True
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82 def onPublish(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 node = iq.pubsub.publish["node"]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
85 items = []
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
86 for child in iq.pubsub.publish.children:
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
87 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
88 items.append(child)
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
89
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
90 print items
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
91
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
92 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), items)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 d.addCallback(self.success, iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 d.addErrback(self.error, iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95 d.addCallback(self.send)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96
7
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
97 def onOptionsGet(self, iq):
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
98 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
99
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
100 def onConfigureGet(self, iq):
a8cfb31dc50c Implemented fallback for feature-not-implemented.
Ralph Meijer <ralphm@ik.nu>
parents: 4
diff changeset
101 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
102
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
103 def do_notification(self, list, node):
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
105 for recipient, items in list.items():
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
106 self.notify(node, items, recipient)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
107
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
108 def notify(self, node, itemlist, recipient):
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
109 message = domish.Element((NS_COMPONENT, "message"))
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
110 message["from"] = self.parent.jabberId
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
111 message["to"] = recipient
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
112 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
113 items = x.addElement("items")
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
114 items["node"] = node
4
ea195dc1732d Allow publication of more than 1 item.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
115 items.children.extend(itemlist)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
116 self.send(message)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
117
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 """
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 def onCreateSet(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120 node = iq.pubsub.create["node"]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121 owner = jid.JID(iq["from"]).userhost()
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 try:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 node = self.backend.create_node(node, owner)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126 if iq.pubsub.create["node"] == None:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
127 # also show node name
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
128 except:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
129 pass
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
130
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 iq.handled = True
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
132 """
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135