root/branches/gajim_0.11.1/src/common/pubsub.py

Revision 7475, 1.5 kB (checked in by asterix, 2 years ago)

merge ad_hoc branch. so we now have ad hoc commands and pubsub (atom). A great thanks to Tomasz Melcer (liorithiel) and Google Summer Of Code. fixes #189

Line 
1import xmpp
2import gajim
3
4class ConnectionPubSub:
5        def __init__(self):
6                self.__callbacks={}
7
8        def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
9                query = xmpp.Iq('get', to=jid)
10                pb = query.addChild('pubsub', {'xmlns': xmpp.NS_PUBSUB})
11                pb.addChild('subscriptions')
12
13                id = self.connection.send(query)
14
15                self.__callbacks[id]=(cb, args, kwargs)
16
17        def send_pb_subscribe(self, jid, node, cb, *args, **kwargs):
18                our_jid = gajim.get_jid_from_account(self.name)
19                query = xmpp.Iq('set', to=jid)
20                pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
21                pb.addChild('subscribe', {'node': node, 'jid': our_jid})
22
23                id = self.connection.send(query)
24
25                self.__callbacks[id]=(cb, args, kwargs)
26
27        def send_pb_unsubscribe(self, jid, node, cb, *args, **kwargs):
28                our_jid = gajim.get_jid_from_account(self.name)
29                query = xmpp.Iq('set', to=jid)
30                pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
31                pb.addChild('unsubscribe', {'node': node, 'jid': our_jid})
32
33                id = self.connection.send(query)
34
35                self.__callbacks[id]=(cb, args, kwargs)
36
37        def send_pb_publish(self, jid, node, item, id):
38                '''Publish item to a node.'''
39                query = xmpp.Iq('set', to=jid)
40                e = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
41                e = e.addChild('publish', {'node': node})
42                e = e.addChild('item', {'id': id}, [item])      # TODO: we should generate id... or we shouldn't?
43
44                self.connection.send(query)
45
46        def _PubSubCB(self, conn, stanza):
47                try:
48                        cb, args, kwargs = self.__callbacks.pop(stanza.getID())
49                        cb(conn, stanza, *args, **kwargs)
50                except KeyError:
51                        pass
Note: See TracBrowser for help on using the browser.