diff --git a/src/common/caps.py b/src/common/caps.py index b3fe72e9c..bb52bdcf4 100644 --- a/src/common/caps.py +++ b/src/common/caps.py @@ -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. diff --git a/src/common/contacts.py b/src/common/contacts.py index c423fe275..34f45c914 100644 --- a/src/common/contacts.py +++ b/src/common/contacts.py @@ -29,7 +29,7 @@ ## import common.gajim -from common import caps +import caps class XMPPEntity(object): '''Base representation of entities in XMPP''' @@ -72,7 +72,7 @@ class CommonContact(XMPPEntity): def get_shown_name(self): raise NotImplementedError - + def supports(self, requested_feature): ''' Returns True if the contact has advertised to support the feature @@ -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): diff --git a/test/unit/test_caps.py b/test/unit/test_caps.py index 9ac466c47..160996b6a 100644 --- a/test/unit/test_caps.py +++ b/test/unit/test_caps.py @@ -113,25 +113,23 @@ 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), - msg="Must return True on supported feature") - + self.assertTrue(caps.client_supports(self.client_caps, NS_MUC), + msg="Must return True on supported feature") + class TestOldClientCaps(TestClientCaps):