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>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## 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/>.
|
|
|
|
##
|
|
|
|
|
2018-02-08 21:05:15 +01:00
|
|
|
import base64
|
|
|
|
import binascii
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2013-01-02 13:54:02 +01:00
|
|
|
#TODO: Doesn't work
|
|
|
|
#from common.connection_handlers import PEP_CONFIG
|
|
|
|
PEP_CONFIG = 'pep_config'
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import ged
|
|
|
|
from gajim.common.connection_handlers_events import PubsubReceivedEvent
|
|
|
|
from gajim.common.connection_handlers_events import PubsubBookmarksReceivedEvent
|
2018-02-08 21:05:15 +01:00
|
|
|
from gajim.common.exceptions import StanzaMalformed
|
2017-09-16 11:49:31 +02:00
|
|
|
|
2009-09-05 21:35:14 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.pubsub')
|
2006-08-20 12:18:20 +02:00
|
|
|
|
|
|
|
class ConnectionPubSub:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
2010-08-28 00:30:23 +02:00
|
|
|
self.__callbacks = {}
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.register_incoming_event(PubsubBookmarksReceivedEvent)
|
|
|
|
app.ged.register_event_handler('pubsub-bookmarks-received',
|
2010-08-27 23:53:17 +02:00
|
|
|
ged.CORE, self._nec_pubsub_bookmarks_received)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-01-06 16:50:38 +01:00
|
|
|
def cleanup(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('pubsub-bookmarks-received',
|
2011-01-06 16:50:38 +01:00
|
|
|
ged.CORE, self._nec_pubsub_bookmarks_received)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('get', to=jid)
|
|
|
|
pb = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
pb.addChild('subscriptions')
|
|
|
|
|
|
|
|
id_ = self.connection.send(query)
|
|
|
|
|
2010-08-28 00:30:23 +02:00
|
|
|
self.__callbacks[id_] = (cb, args, kwargs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def send_pb_subscribe(self, jid, node, cb, *args, **kwargs):
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
our_jid = app.get_jid_from_account(self.name)
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
pb = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
pb.addChild('subscribe', {'node': node, 'jid': our_jid})
|
|
|
|
|
|
|
|
id_ = self.connection.send(query)
|
|
|
|
|
2010-08-28 00:30:23 +02:00
|
|
|
self.__callbacks[id_] = (cb, args, kwargs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def send_pb_unsubscribe(self, jid, node, cb, *args, **kwargs):
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
our_jid = app.get_jid_from_account(self.name)
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
pb = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
pb.addChild('unsubscribe', {'node': node, 'jid': our_jid})
|
|
|
|
|
|
|
|
id_ = self.connection.send(query)
|
|
|
|
|
2010-08-28 00:30:23 +02:00
|
|
|
self.__callbacks[id_] = (cb, args, kwargs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-08-18 19:03:31 +02:00
|
|
|
def send_pb_publish(self, jid, node, item, id_=None, options=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Publish item to a node
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
e = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
p = e.addChild('publish', {'node': node})
|
2011-08-18 19:03:31 +02:00
|
|
|
attrs = {}
|
|
|
|
if id_:
|
|
|
|
attrs = {'id': id_}
|
|
|
|
p.addChild('item', attrs, [item])
|
2010-02-08 15:08:40 +01:00
|
|
|
if options:
|
|
|
|
p = e.addChild('publish-options')
|
|
|
|
p.addChild(node=options)
|
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
2018-02-08 21:05:15 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_pb_retrieve_iq(jid, node, item_id=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2018-02-08 21:05:15 +01:00
|
|
|
Get IQ to query items from a node
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('get', to=jid)
|
|
|
|
r = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
r = r.addChild('items', {'node': node})
|
2017-09-16 11:49:31 +02:00
|
|
|
if item_id is not None:
|
|
|
|
r.addChild('item', {'id': item_id})
|
2018-02-08 21:05:15 +01:00
|
|
|
return query
|
|
|
|
|
|
|
|
def send_pb_retrieve(self, jid, node, item_id=None, cb=None, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Get items from a node
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
|
|
|
query = self.get_pb_retrieve_iq(jid, node, item_id)
|
2010-02-08 15:08:40 +01:00
|
|
|
id_ = self.connection.send(query)
|
|
|
|
|
|
|
|
if cb:
|
2010-08-28 00:30:23 +02:00
|
|
|
self.__callbacks[id_] = (cb, args, kwargs)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-02-08 21:05:15 +01:00
|
|
|
def get_pubsub_avatar(self, jid, node, item_id):
|
|
|
|
query = self.get_pb_retrieve_iq(jid, node, item_id)
|
|
|
|
self.connection.SendAndCallForResponse(
|
|
|
|
query, self._nec_pubsub_avatar_received, {'jid': jid})
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_pb_retract(self, jid, node, id_):
|
|
|
|
"""
|
|
|
|
Delete item from a node
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
r = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
r = r.addChild('retract', {'node': node, 'notify': '1'})
|
|
|
|
r = r.addChild('item', {'id': id_})
|
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
2010-06-21 19:15:46 +02:00
|
|
|
def send_pb_purge(self, jid, node):
|
|
|
|
"""
|
|
|
|
Purge node: Remove all items
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
d = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB_OWNER)
|
2010-06-21 19:15:46 +02:00
|
|
|
d = d.addChild('purge', {'node': node})
|
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
|
|
|
def send_pb_delete(self, jid, node, on_ok=None, on_fail=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Delete node
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
d = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB_OWNER)
|
2010-02-08 15:08:40 +01:00
|
|
|
d = d.addChild('delete', {'node': node})
|
|
|
|
|
|
|
|
def response(con, resp, jid, node):
|
2010-06-21 19:15:46 +02:00
|
|
|
if resp.getType() == 'result' and on_ok:
|
|
|
|
on_ok(jid, node)
|
|
|
|
elif on_fail:
|
2010-02-08 15:08:40 +01:00
|
|
|
msg = resp.getErrorMsg()
|
2010-06-21 19:15:46 +02:00
|
|
|
on_fail(jid, node, msg)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.connection.SendAndCallForResponse(query, response, {'jid': jid,
|
2010-06-21 19:15:46 +02:00
|
|
|
'node': node})
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-08-28 00:30:23 +02:00
|
|
|
def send_pb_create(self, jid, node, configure=False, configure_form=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Create a new node
|
|
|
|
"""
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
c = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB)
|
2010-02-08 15:08:40 +01:00
|
|
|
c = c.addChild('create', {'node': node})
|
|
|
|
if configure:
|
|
|
|
conf = c.addChild('configure')
|
|
|
|
if configure_form is not None:
|
|
|
|
conf.addChild(node=configure_form)
|
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
|
|
|
def send_pb_configure(self, jid, node, form):
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('set', to=jid)
|
|
|
|
c = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB_OWNER)
|
2010-02-08 15:08:40 +01:00
|
|
|
c = c.addChild('configure', {'node': node})
|
|
|
|
c.addChild(node=form)
|
|
|
|
|
|
|
|
self.connection.send(query)
|
|
|
|
|
|
|
|
def _PubSubCB(self, conn, stanza):
|
|
|
|
log.debug('_PubsubCB')
|
|
|
|
try:
|
|
|
|
cb, args, kwargs = self.__callbacks.pop(stanza.getID())
|
|
|
|
cb(conn, stanza, *args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(PubsubReceivedEvent(None,
|
2010-10-18 22:05:41 +02:00
|
|
|
conn=self, stanza=stanza))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-08-27 23:53:17 +02:00
|
|
|
def _nec_pubsub_bookmarks_received(self, obj):
|
2013-07-05 17:02:07 +02:00
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2017-10-13 21:59:33 +02:00
|
|
|
app.log('bookmarks').info('Received Bookmarks (PubSub)')
|
2010-08-27 23:53:17 +02:00
|
|
|
bm_jids = [b['jid'] for b in self.bookmarks]
|
|
|
|
for bm in obj.bookmarks:
|
|
|
|
if bm['jid'] not in bm_jids:
|
|
|
|
self.bookmarks.append(bm)
|
|
|
|
# We got bookmarks from pubsub, now get those from xml to merge them
|
|
|
|
self.get_bookmarks(storage_type='xml')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-02-08 21:05:15 +01:00
|
|
|
def _validate_avatar_node(self, stanza):
|
|
|
|
jid = stanza.getFrom()
|
|
|
|
if jid is None:
|
2017-09-16 11:49:31 +02:00
|
|
|
jid = self.get_own_jid().getStripped()
|
|
|
|
else:
|
2018-02-08 21:05:15 +01:00
|
|
|
jid = jid.getStripped()
|
|
|
|
|
|
|
|
if nbxmpp.isErrorNode(stanza):
|
|
|
|
raise StanzaMalformed(stanza.getErrorMsg())
|
|
|
|
|
|
|
|
pubsub_node = stanza.getTag('pubsub')
|
|
|
|
if pubsub_node is None:
|
|
|
|
raise StanzaMalformed('No pubsub node', stanza)
|
|
|
|
|
|
|
|
items_node = pubsub_node.getTag('items')
|
|
|
|
if items_node is None:
|
|
|
|
raise StanzaMalformed('No items node', stanza)
|
|
|
|
|
|
|
|
if items_node.getAttr('node') != 'urn:xmpp:avatar:data':
|
|
|
|
raise StanzaMalformed('Wrong namespace', stanza)
|
|
|
|
|
|
|
|
item = items_node.getTag('item')
|
|
|
|
if item is None:
|
|
|
|
raise StanzaMalformed('No item node', stanza)
|
|
|
|
|
|
|
|
sha = item.getAttr('id')
|
|
|
|
data_tag = item.getTag('data', namespace='urn:xmpp:avatar:data')
|
|
|
|
if sha is None or data_tag is None:
|
|
|
|
raise StanzaMalformed('No id attr or data node found', stanza)
|
|
|
|
|
|
|
|
data = data_tag.getData()
|
|
|
|
if data is None:
|
|
|
|
raise StanzaMalformed('Data node empty', stanza)
|
|
|
|
|
|
|
|
data = base64.b64decode(data.encode('utf-8'))
|
|
|
|
|
|
|
|
return jid, sha, data
|
|
|
|
|
|
|
|
def _nec_pubsub_avatar_received(self, conn, stanza, jid):
|
|
|
|
try:
|
|
|
|
jid, sha, data = self._validate_avatar_node(stanza)
|
|
|
|
except (StanzaMalformed, binascii.Error) as error:
|
|
|
|
app.log('avatar').warning(
|
|
|
|
'Error loading Avatar (Pubsub): %s %s', jid, error)
|
|
|
|
return
|
2017-09-16 11:49:31 +02:00
|
|
|
|
|
|
|
app.log('avatar').info(
|
2018-02-08 21:05:15 +01:00
|
|
|
'Received Avatar (Pubsub): %s %s', jid, sha)
|
|
|
|
app.interface.save_avatar(data)
|
2017-09-16 11:49:31 +02:00
|
|
|
|
|
|
|
if self.get_own_jid().bareMatch(jid):
|
2018-02-08 21:05:15 +01:00
|
|
|
app.config.set_per('accounts', self.name, 'avatar_sha', sha)
|
2017-09-16 11:49:31 +02:00
|
|
|
else:
|
|
|
|
own_jid = self.get_own_jid().getStripped()
|
2018-02-08 21:05:15 +01:00
|
|
|
app.logger.set_avatar_sha(own_jid, jid, sha)
|
|
|
|
app.contacts.set_avatar(self.name, jid, sha)
|
2017-09-16 11:49:31 +02:00
|
|
|
|
|
|
|
app.interface.update_avatar(self.name, jid)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _PubSubErrorCB(self, conn, stanza):
|
|
|
|
log.debug('_PubsubErrorCB')
|
|
|
|
pubsub = stanza.getTag('pubsub')
|
|
|
|
if not pubsub:
|
|
|
|
return
|
|
|
|
items = pubsub.getTag('items')
|
|
|
|
if not items:
|
|
|
|
return
|
|
|
|
if items.getAttr('node') == 'storage:bookmarks':
|
|
|
|
# Receiving bookmarks from pubsub failed, so take them from xml
|
|
|
|
self.get_bookmarks(storage_type='xml')
|
|
|
|
|
|
|
|
def request_pb_configuration(self, jid, node):
|
|
|
|
if not self.connection or self.connected < 2:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
query = nbxmpp.Iq('get', to=jid)
|
|
|
|
e = query.addChild('pubsub', namespace=nbxmpp.NS_PUBSUB_OWNER)
|
2010-02-08 15:08:40 +01:00
|
|
|
e = e.addChild('configure', {'node': node})
|
|
|
|
id_ = self.connection.getAnID()
|
|
|
|
query.setID(id_)
|
2013-01-02 13:54:02 +01:00
|
|
|
self.awaiting_answers[id_] = (PEP_CONFIG,)
|
2011-01-06 16:50:38 +01:00
|
|
|
self.connection.send(query)
|