2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/pubsub.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Tomasz Melcer <liori AT exroot.org>
|
|
|
|
## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de>
|
2008-08-15 05:20:23 +02:00
|
|
|
##
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2006-08-20 12:18:20 +02:00
|
|
|
import xmpp
|
|
|
|
import gajim
|
2007-06-03 12:30:34 +02:00
|
|
|
import connection_handlers
|
2006-08-20 12:18:20 +02:00
|
|
|
|
|
|
|
class ConnectionPubSub:
|
|
|
|
def __init__(self):
|
|
|
|
self.__callbacks={}
|
|
|
|
|
|
|
|
def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2006-08-20 12:18:20 +02:00
|
|
|
query = xmpp.Iq('get', to=jid)
|
2008-12-28 15:53:20 +01:00
|
|
|
pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
2006-08-20 12:18:20 +02:00
|
|
|
pb.addChild('subscriptions')
|
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
id_ = self.connection.send(query)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
self.__callbacks[id_]=(cb, args, kwargs)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
|
|
|
def send_pb_subscribe(self, jid, node, cb, *args, **kwargs):
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2006-08-20 12:18:20 +02:00
|
|
|
our_jid = gajim.get_jid_from_account(self.name)
|
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
pb.addChild('subscribe', {'node': node, 'jid': our_jid})
|
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
id_ = self.connection.send(query)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
self.__callbacks[id_]=(cb, args, kwargs)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
|
|
|
def send_pb_unsubscribe(self, jid, node, cb, *args, **kwargs):
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2006-08-20 12:18:20 +02:00
|
|
|
our_jid = gajim.get_jid_from_account(self.name)
|
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
pb.addChild('unsubscribe', {'node': node, 'jid': our_jid})
|
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
id_ = self.connection.send(query)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
2008-12-03 18:16:04 +01:00
|
|
|
self.__callbacks[id_]=(cb, args, kwargs)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
2009-07-31 15:50:11 +02:00
|
|
|
def send_pb_publish(self, jid, node, item, id_, options=None):
|
2006-08-20 12:18:20 +02:00
|
|
|
'''Publish item to a node.'''
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2006-08-20 12:18:20 +02:00
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
e = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
2009-07-31 15:50:11 +02:00
|
|
|
p = e.addChild('publish', {'node': node})
|
|
|
|
p.addChild('item', {'id': id_}, [item])
|
|
|
|
if options:
|
|
|
|
p = e.addChild('publish-options')
|
|
|
|
p.addChild(node=options)
|
2006-08-20 12:18:20 +02:00
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
2009-07-31 17:11:55 +02:00
|
|
|
def send_pb_retrieve(self, jid, node, cb=None, *args, **kwargs):
|
2009-07-31 14:52:01 +02:00
|
|
|
'''Get items from a node'''
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
|
|
|
query = xmpp.Iq('get', to=jid)
|
|
|
|
r = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
r = r.addChild('items', {'node': node})
|
|
|
|
id_ = self.connection.send(query)
|
|
|
|
|
2009-07-31 17:11:55 +02:00
|
|
|
if cb:
|
|
|
|
self.__callbacks[id_]=(cb, args, kwargs)
|
2009-07-31 14:52:01 +02:00
|
|
|
|
2008-10-11 12:22:04 +02:00
|
|
|
def send_pb_retract(self, jid, node, id_):
|
2008-05-05 09:48:13 +02:00
|
|
|
'''Delete item from a node'''
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2008-05-05 09:48:13 +02:00
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
r = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
r = r.addChild('retract', {'node': node, 'notify': '1'})
|
2008-10-11 12:22:04 +02:00
|
|
|
r = r.addChild('item', {'id': id_})
|
2008-05-05 09:48:13 +02:00
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
2007-03-26 00:16:48 +02:00
|
|
|
def send_pb_delete(self, jid, node):
|
|
|
|
'''Deletes node.'''
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2007-03-26 00:16:48 +02:00
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
d = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
d = d.addChild('delete', {'node': node})
|
|
|
|
|
2008-03-03 00:56:39 +01:00
|
|
|
def response(con, resp, jid, node):
|
|
|
|
if resp.getType() == 'result':
|
|
|
|
self.dispatch('PUBSUB_NODE_REMOVED', (jid, node))
|
|
|
|
else:
|
|
|
|
msg = resp.getErrorMsg()
|
|
|
|
self.dispatch('PUBSUB_NODE_NOT_REMOVED', (jid, node, msg))
|
|
|
|
|
|
|
|
self.connection.SendAndCallForResponse(query, response, {'jid': jid,
|
|
|
|
'node': node})
|
2007-03-26 00:16:48 +02:00
|
|
|
|
|
|
|
def send_pb_create(self, jid, node, configure = False, configure_form = None):
|
|
|
|
'''Creates new node.'''
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2007-03-26 00:16:48 +02:00
|
|
|
query = xmpp.Iq('set', to=jid)
|
|
|
|
c = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)
|
|
|
|
c = c.addChild('create', {'node': node})
|
|
|
|
if configure:
|
|
|
|
conf = c.addChild('configure')
|
|
|
|
if configure_form is not None:
|
2007-12-27 22:52:45 +01:00
|
|
|
conf.addChild(node=configure_form)
|
2007-03-26 00:16:48 +02:00
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
2008-02-15 23:55:21 +01:00
|
|
|
def send_pb_configure(self, jid, node, form):
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2007-03-26 00:16:48 +02:00
|
|
|
query = xmpp.Iq('set', to=jid)
|
2008-02-15 23:55:21 +01:00
|
|
|
c = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER)
|
2007-03-26 00:16:48 +02:00
|
|
|
c = c.addChild('configure', {'node': node})
|
2008-02-15 23:55:21 +01:00
|
|
|
c.addChild(node=form)
|
2007-03-26 00:16:48 +02:00
|
|
|
|
2008-02-15 23:55:21 +01:00
|
|
|
self.connection.send(query)
|
2007-03-26 00:16:48 +02:00
|
|
|
|
2006-08-20 12:18:20 +02:00
|
|
|
def _PubSubCB(self, conn, stanza):
|
2009-07-31 17:11:55 +02:00
|
|
|
gajim.log.debug('_PubsubCB')
|
2006-08-20 12:18:20 +02:00
|
|
|
try:
|
|
|
|
cb, args, kwargs = self.__callbacks.pop(stanza.getID())
|
|
|
|
cb(conn, stanza, *args, **kwargs)
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2006-08-20 12:18:20 +02:00
|
|
|
pass
|
2007-06-03 12:30:34 +02:00
|
|
|
|
2009-07-31 17:11:55 +02:00
|
|
|
pubsub = stanza.getTag('pubsub')
|
|
|
|
if not pubsub:
|
|
|
|
return
|
|
|
|
items = pubsub.getTag('items')
|
|
|
|
if not items:
|
|
|
|
return
|
|
|
|
item = items.getTag('item')
|
|
|
|
if not item:
|
|
|
|
return
|
|
|
|
storage = item.getTag('storage')
|
|
|
|
if storage:
|
|
|
|
ns = storage.getNamespace()
|
|
|
|
if ns == 'storage:bookmarks':
|
|
|
|
self._parse_bookmarks(storage, 'pubsub')
|
|
|
|
|
2007-06-03 12:30:34 +02:00
|
|
|
def request_pb_configuration(self, jid, node):
|
2008-11-28 10:59:36 +01:00
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2007-06-03 12:30:34 +02:00
|
|
|
query = xmpp.Iq('get', to=jid)
|
|
|
|
e = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER)
|
|
|
|
e = e.addChild('configure', {'node': node})
|
2008-12-03 18:16:04 +01:00
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
query.setID(id_)
|
|
|
|
self.awaiting_answers[id_] = (connection_handlers.PEP_CONFIG,)
|
2007-06-03 12:30:34 +02:00
|
|
|
self.connection.send(query)
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-15 05:20:23 +02:00
|
|
|
# vim: se ts=3:
|