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:
Stephan Erb 2009-11-09 21:26:56 +01:00
parent 17af7902e4
commit cec93b6135
3 changed files with 36 additions and 27 deletions

View File

@ -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]
################################################################################
### Public API of this module
################################################################################
capscache = None
def initialize(logger):
''' Initializes the capscache global '''
''' Initializes this module '''
global capscache
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'):
'''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())
################################################################################
### Internal classes of this module
################################################################################
class AbstractClientCaps(object):
'''
Base class representing a client and its capabilities as advertised by
@ -323,6 +344,10 @@ class CapsCache(object):
discover(connection, jid)
################################################################################
### Caps network coding
################################################################################
class ConnectionCaps(object):
'''
This class highly depends on that it is a part of Connection class.

View File

@ -29,7 +29,7 @@
##
import common.gajim
from common import caps
import caps
class XMPPEntity(object):
'''Base representation of entities in XMPP'''
@ -85,21 +85,7 @@ class CommonContact(XMPPEntity):
# return caps for a contact that has no resources left.
return False
else:
return self._client_supports(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
return caps.client_supports(self.client_caps, requested_feature)
class Contact(CommonContact):

View File

@ -113,23 +113,21 @@ class TestClientCaps(CommonCapsTest):
"http://gajim.org#m3P2WeXPMGVH2tZPe7yITnfY0Dw=")
def test_client_supports(self):
contact = Contact(jid=None, account=None, client_caps=self.client_caps)
self.assertTrue(contact.supports(NS_PING),
self.assertTrue(caps.client_supports(self.client_caps, NS_PING),
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")
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")
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")
self.assertTrue(contact.supports(NS_MUC),
self.assertTrue(caps.client_supports(self.client_caps, NS_MUC),
msg="Must return True on supported feature")