Push method to check if a specific ClientCaps supports a feature down to the caps module.
Public interfaces stay the same.
This commit is contained in:
parent
17af7902e4
commit
cec93b6135
|
@ -41,12 +41,29 @@ from common.xmpp import NS_XHTML_IM, NS_RECEIPTS, NS_ESESSION, NS_CHATSTATES
|
||||||
FEATURE_BLACKLIST = [NS_CHATSTATES, NS_XHTML_IM, NS_RECEIPTS, NS_ESESSION]
|
FEATURE_BLACKLIST = [NS_CHATSTATES, NS_XHTML_IM, NS_RECEIPTS, NS_ESESSION]
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### Public API of this module
|
||||||
|
################################################################################
|
||||||
|
|
||||||
capscache = None
|
capscache = None
|
||||||
def initialize(logger):
|
def initialize(logger):
|
||||||
''' Initializes the capscache global '''
|
''' Initializes this module '''
|
||||||
global capscache
|
global capscache
|
||||||
capscache = CapsCache(logger)
|
capscache = CapsCache(logger)
|
||||||
|
|
||||||
|
def client_supports(client_caps, requested_feature):
|
||||||
|
lookup_item = client_caps.get_cache_lookup_strategy()
|
||||||
|
cache_item = lookup_item(capscache)
|
||||||
|
|
||||||
|
supported_features = cache_item.features
|
||||||
|
if requested_feature in supported_features:
|
||||||
|
return True
|
||||||
|
elif supported_features == [] and cache_item.queried in (0, 1):
|
||||||
|
# assume feature is supported, if we don't know yet, what the client
|
||||||
|
# is capable of
|
||||||
|
return requested_feature not in FEATURE_BLACKLIST
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def compute_caps_hash(identities, features, dataforms=[], hash_method='sha-1'):
|
def compute_caps_hash(identities, features, dataforms=[], hash_method='sha-1'):
|
||||||
'''Compute caps hash according to XEP-0115, V1.5
|
'''Compute caps hash according to XEP-0115, V1.5
|
||||||
|
@ -118,6 +135,10 @@ def compute_caps_hash(identities, features, dataforms=[], hash_method='sha-1'):
|
||||||
return base64.b64encode(hash_.digest())
|
return base64.b64encode(hash_.digest())
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### Internal classes of this module
|
||||||
|
################################################################################
|
||||||
|
|
||||||
class AbstractClientCaps(object):
|
class AbstractClientCaps(object):
|
||||||
'''
|
'''
|
||||||
Base class representing a client and its capabilities as advertised by
|
Base class representing a client and its capabilities as advertised by
|
||||||
|
@ -323,6 +344,10 @@ class CapsCache(object):
|
||||||
discover(connection, jid)
|
discover(connection, jid)
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### Caps network coding
|
||||||
|
################################################################################
|
||||||
|
|
||||||
class ConnectionCaps(object):
|
class ConnectionCaps(object):
|
||||||
'''
|
'''
|
||||||
This class highly depends on that it is a part of Connection class.
|
This class highly depends on that it is a part of Connection class.
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
##
|
##
|
||||||
|
|
||||||
import common.gajim
|
import common.gajim
|
||||||
from common import caps
|
import caps
|
||||||
|
|
||||||
class XMPPEntity(object):
|
class XMPPEntity(object):
|
||||||
'''Base representation of entities in XMPP'''
|
'''Base representation of entities in XMPP'''
|
||||||
|
@ -85,21 +85,7 @@ class CommonContact(XMPPEntity):
|
||||||
# return caps for a contact that has no resources left.
|
# return caps for a contact that has no resources left.
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return self._client_supports(requested_feature)
|
return caps.client_supports(self.client_caps, requested_feature)
|
||||||
|
|
||||||
def _client_supports(self, requested_feature):
|
|
||||||
lookup_item = self.client_caps.get_cache_lookup_strategy()
|
|
||||||
cache_item = lookup_item(caps.capscache)
|
|
||||||
|
|
||||||
supported_features = cache_item.features
|
|
||||||
if requested_feature in supported_features:
|
|
||||||
return True
|
|
||||||
elif supported_features == [] and cache_item.queried in (0, 1):
|
|
||||||
# assume feature is supported, if we don't know yet, what the client
|
|
||||||
# is capable of
|
|
||||||
return requested_feature not in caps.FEATURE_BLACKLIST
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class Contact(CommonContact):
|
class Contact(CommonContact):
|
||||||
|
|
|
@ -113,23 +113,21 @@ class TestClientCaps(CommonCapsTest):
|
||||||
"http://gajim.org#m3P2WeXPMGVH2tZPe7yITnfY0Dw=")
|
"http://gajim.org#m3P2WeXPMGVH2tZPe7yITnfY0Dw=")
|
||||||
|
|
||||||
def test_client_supports(self):
|
def test_client_supports(self):
|
||||||
contact = Contact(jid=None, account=None, client_caps=self.client_caps)
|
self.assertTrue(caps.client_supports(self.client_caps, NS_PING),
|
||||||
|
|
||||||
self.assertTrue(contact.supports(NS_PING),
|
|
||||||
msg="Assume supported, if we don't have caps")
|
msg="Assume supported, if we don't have caps")
|
||||||
|
|
||||||
self.assertFalse(contact.supports(NS_XHTML_IM),
|
self.assertFalse(caps.client_supports(self.client_caps, NS_XHTML_IM),
|
||||||
msg="Must not assume blacklisted feature is supported on default")
|
msg="Must not assume blacklisted feature is supported on default")
|
||||||
|
|
||||||
self.cc.initialize_from_db()
|
self.cc.initialize_from_db()
|
||||||
|
|
||||||
self.assertFalse(contact.supports(NS_PING),
|
self.assertFalse(caps.client_supports(self.client_caps, NS_PING),
|
||||||
msg="Must return false on unsupported feature")
|
msg="Must return false on unsupported feature")
|
||||||
|
|
||||||
self.assertTrue(contact.supports(NS_XHTML_IM),
|
self.assertTrue(caps.client_supports(self.client_caps, NS_XHTML_IM),
|
||||||
msg="Must return True on supported feature")
|
msg="Must return True on supported feature")
|
||||||
|
|
||||||
self.assertTrue(contact.supports(NS_MUC),
|
self.assertTrue(caps.client_supports(self.client_caps, NS_MUC),
|
||||||
msg="Must return True on supported feature")
|
msg="Must return True on supported feature")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue