comparison 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
comparison
equal deleted inserted replaced
1:4cc41776b7d7 2:9701df89c534
1 from twisted.protocols.jabber import component,jid 1 from twisted.protocols.jabber import component,jid
2 from twisted.xish import utility 2 from twisted.xish import utility, domish
3 from twisted.python import components 3 from twisted.python import components
4 import backend 4 import backend
5 import xmpp_error 5 import xmpp_error
6 6
7 NS_COMPONENT = 'jabber:component:accept'
8 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
9 NS_PUBSUB_EVENT = NS_PUBSUB + '#event'
10
7 IQ_GET = '/iq[@type="get"]' 11 IQ_GET = '/iq[@type="get"]'
8 IQ_SET = '/iq[@type="set"]' 12 IQ_SET = '/iq[@type="set"]'
9 PUBSUB_GET = IQ_GET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' 13 PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]'
10 PUBSUB_SET = IQ_SET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' 14 PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT
11 15 PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT
12 PUBSUB_CREATE = PUBSUB_SET + '/create' 16 PUBSUB_CREATE = PUBSUB_SET + '/create'
13 PUBSUB_PUBLISH = PUBSUB_SET + '/publish' 17 PUBSUB_PUBLISH = PUBSUB_SET + '/publish'
14 18
15 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher): 19 class ComponentServiceFromBackend(component.Service, utility.EventDispatcher):
16 20
17 def __init__(self, backend): 21 def __init__(self, backend):
18 utility.EventDispatcher.__init__(self) 22 utility.EventDispatcher.__init__(self)
19 self.backend = backend 23 self.backend = backend
24 self.backend.pubsub_service = self
20 self.addObserver(PUBSUB_PUBLISH, self.onPublish) 25 self.addObserver(PUBSUB_PUBLISH, self.onPublish)
21 26
22 def componentConnected(self, xmlstream): 27 def componentConnected(self, xmlstream):
23 xmlstream.addObserver(PUBSUB_SET, self.onPubSub) 28 xmlstream.addObserver(PUBSUB_SET, self.onPubSub)
24 xmlstream.addObserver(PUBSUB_GET, self.onPubSub) 29 xmlstream.addObserver(PUBSUB_GET, self.onPubSub)
50 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), iq.pubsub.publish.item) 55 d = self.backend.do_publish(node, jid.JID(iq["from"]).userhost(), iq.pubsub.publish.item)
51 d.addCallback(self.success, iq) 56 d.addCallback(self.success, iq)
52 d.addErrback(self.error, iq) 57 d.addErrback(self.error, iq)
53 d.addCallback(self.send) 58 d.addCallback(self.send)
54 59
60 def do_notification(self, recipients, node, item):
55 61
62 for recipient in recipients:
63 self.notify(node, item, recipient)
64
65 def notify(self, node, item, recipient):
66 message = domish.Element((NS_COMPONENT, "message"))
67 message["from"] = self.parent.jabberId
68 message["to"] = recipient
69 x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT)
70 items = x.addElement("items")
71 items["node"] = node
72 items.children.append(item)
73 self.send(message)
74
56 """ 75 """
57 def onCreateSet(self, iq): 76 def onCreateSet(self, iq):
58 node = iq.pubsub.create["node"] 77 node = iq.pubsub.create["node"]
59 owner = jid.JID(iq["from"]).userhost() 78 owner = jid.JID(iq["from"]).userhost()
60 79