annotate idavoll/pubsub.py @ 2:9701df89c534

First take at notifications
author Ralph Meijer <ralphm@ik.nu>
date Fri, 25 Jun 2004 20:31:14 +0000
parents 4cc41776b7d7
children ea195dc1732d
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'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 def __init__(self, backend):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 utility.EventDispatcher.__init__(self)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 self.backend = backend
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
24 self.backend.pubsub_service = self
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 self.addObserver(PUBSUB_PUBLISH, self.onPublish)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 def componentConnected(self, xmlstream):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 xmlstream.addObserver(PUBSUB_SET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 xmlstream.addObserver(PUBSUB_GET, self.onPubSub)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 def error(self, failure, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 r = failure.trap(backend.NotAuthorized, backend.NodeNotFound)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 if r == backend.NotAuthorized:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 xmpp_error.error_from_iq(iq, 'not-authorized', failure.value.msg)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 if r == backend.NodeNotFound:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 xmpp_error.error_from_iq(iq, 'item-not-found', failure.value.msg)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 return iq
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 def success(self, result, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 iq.swapAttributeValues("to", "from")
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 iq["type"] = 'result'
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 iq.children = []
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46 return iq
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 def onPubSub(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 self.dispatch(iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 iq.handled = True
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52 def onPublish(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
53 node = iq.pubsub.publish["node"]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), iq.pubsub.publish.item)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 d.addCallback(self.success, iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 d.addErrback(self.error, iq)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 d.addCallback(self.send)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
60 def do_notification(self, recipients, node, item):
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
62 for recipient in recipients:
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
63 self.notify(node, item, recipient)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
64
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
65 def notify(self, node, item, recipient):
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
66 message = domish.Element((NS_COMPONENT, "message"))
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
67 message["from"] = self.parent.jabberId
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
68 message["to"] = recipient
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
69 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
70 items = x.addElement("items")
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
71 items["node"] = node
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
72 items.children.append(item)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
73 self.send(message)
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
74
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 """
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 def onCreateSet(self, iq):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 node = iq.pubsub.create["node"]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 owner = jid.JID(iq["from"]).userhost()
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 try:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81 node = self.backend.create_node(node, owner)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 if iq.pubsub.create["node"] == None:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84 # also show node name
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 except:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86 pass
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88 iq.handled = True
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89 """
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
90
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91 components.registerAdapter(ComponentServiceFromBackend, backend.IBackendService, component.IService)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92