gajim-plural/gajim/common/modules/caps.py

139 lines
4.9 KiB
Python
Raw Normal View History

2018-07-22 19:12:52 +02:00
# Copyright (C) 2009 Stephan Erb <steve-e AT h3c.de>
2019-01-01 16:47:23 +01:00
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
2018-07-22 19:12:52 +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/>.
# XEP-0115: Entity Capabilities
import nbxmpp
2019-01-01 16:47:23 +01:00
from nbxmpp.structs import StanzaHandler
2018-07-22 19:12:52 +02:00
from gajim.common import caps_cache
from gajim.common import app
from gajim.common.nec import NetworkEvent
from gajim.common.modules.base import BaseModule
2018-07-22 19:12:52 +02:00
class Caps(BaseModule):
2018-07-22 19:12:52 +02:00
def __init__(self, con):
BaseModule.__init__(self, con)
2018-07-22 19:12:52 +02:00
self.handlers = [
2019-01-01 16:47:23 +01:00
StanzaHandler(name='presence',
callback=self._entity_caps,
ns=nbxmpp.NS_CAPS,
priority=51),
2018-07-22 19:12:52 +02:00
]
self._capscache = caps_cache.capscache
self._create_suitable_client_caps = caps_cache.create_suitable_client_caps
2019-01-01 16:47:23 +01:00
def _entity_caps(self, _con, _stanza, properties):
if properties.type.is_error or properties.type.is_unavailable:
return
2019-01-01 16:47:23 +01:00
if properties.is_self_presence:
return
2019-01-01 16:47:23 +01:00
jid = str(properties.jid)
2018-07-22 19:12:52 +02:00
2019-01-01 16:47:23 +01:00
hash_method = properties.entity_caps.hash
node = properties.entity_caps.node
caps_hash = properties.entity_caps.ver
2018-07-22 19:12:52 +02:00
self._log.info(
'Received from %s, type: %s, method: %s, node: %s, hash: %s',
jid, properties.type, hash_method, node, caps_hash)
2018-07-22 19:12:52 +02:00
client_caps = self._create_suitable_client_caps(
2019-01-01 16:47:23 +01:00
node, caps_hash, hash_method, jid)
2018-07-22 19:12:52 +02:00
# Type is None means 'available'
2019-01-01 16:47:23 +01:00
if properties.type.is_available and client_caps._hash_method == 'no':
2018-07-22 19:12:52 +02:00
self._capscache.forget_caps(client_caps)
client_caps = self._create_suitable_client_caps(
node, caps_hash, hash_method)
else:
self._capscache.query_client_of_jid_if_unknown(
2019-01-01 16:47:23 +01:00
self._con, jid, client_caps)
2018-07-22 19:12:52 +02:00
2019-01-01 16:47:23 +01:00
self._update_client_caps_of_contact(properties.jid, client_caps)
2018-07-22 19:12:52 +02:00
2018-09-11 22:25:55 +02:00
app.nec.push_incoming_event(
2019-01-01 16:47:23 +01:00
NetworkEvent('caps-update',
2018-09-11 22:25:55 +02:00
conn=self._con,
2019-01-01 16:47:23 +01:00
fjid=jid,
jid=properties.jid.getBare()))
2018-07-22 19:12:52 +02:00
def _update_client_caps_of_contact(self, from_, client_caps):
contact = self._get_contact_or_gc_contact_for_jid(from_)
if contact is not None:
contact.client_caps = client_caps
else:
self._log.info('Received Caps from unknown contact %s', from_)
2018-07-22 19:12:52 +02:00
def _get_contact_or_gc_contact_for_jid(self, from_):
contact = app.contacts.get_contact_from_full_jid(self._account,
str(from_))
if contact is None:
room_jid, resource = from_.getStripped(), from_.getResource()
contact = app.contacts.get_gc_contact(
self._account, room_jid, resource)
return contact
def contact_info_received(self, from_, identities, features, data, node):
"""
callback to update our caps cache with queried information after
we have retrieved an unknown caps hash via a disco
"""
bare_jid = from_.getStripped()
contact = self._get_contact_or_gc_contact_for_jid(from_)
if not contact:
self._log.info('Received Disco from unknown contact %s', from_)
2018-07-22 19:12:52 +02:00
return
lookup = contact.client_caps.get_cache_lookup_strategy()
cache_item = lookup(self._capscache)
if cache_item.is_valid():
# we already know that the hash is fine and have already cached
# the identities and features
return
2018-09-11 22:25:55 +02:00
validate = contact.client_caps.get_hash_validation_strategy()
hash_is_valid = validate(identities, features, data)
if hash_is_valid:
cache_item.set_and_store(identities, features)
2018-07-22 19:12:52 +02:00
else:
2018-09-11 22:25:55 +02:00
node = caps_hash = hash_method = None
contact.client_caps = self._create_suitable_client_caps(
node, caps_hash, hash_method)
self._log.warning(
2018-09-11 22:25:55 +02:00
'Computed and retrieved caps hash differ. Ignoring '
'caps of contact %s', contact.get_full_jid())
app.nec.push_incoming_event(
NetworkEvent('caps-update',
conn=self._con,
fjid=str(from_),
jid=bare_jid))
2018-07-22 19:12:52 +02:00
def get_instance(*args, **kwargs):
return Caps(*args, **kwargs), 'Caps'