diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-06-29 18:24:20 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-06-29 18:37:42 +0300 |
commit | 54d8fea2057e5b301ef4ed167f0c55090ebf0fff (patch) | |
tree | d3ccf5010cfe0bcec558a18ec77d439bcc6d0d0e | |
parent | f57bd1aef78ee7ddb37045efe7fc81434a2c2114 (diff) |
Add compare argument to compute_caps_hash()
-rw-r--r-- | nbxmpp/structs.py | 17 | ||||
-rw-r--r-- | nbxmpp/util.py | 10 |
2 files changed, 23 insertions, 4 deletions
diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py index 6f7ac29..9253bee 100644 --- a/nbxmpp/structs.py +++ b/nbxmpp/structs.py @@ -112,13 +112,26 @@ AdHocCommandNote = namedtuple('AdHocCommandNote', 'text type') IBBData = namedtuple('IBBData', 'block_size sid seq type data') IBBData.__new__.__defaults__ = (None, None, None, None, None) -DiscoInfo = namedtuple('DiscoInfo', 'jid node identities features dataforms') - DiscoItems = namedtuple('DiscoItems', 'jid node items') DiscoItem = namedtuple('DiscoItem', 'jid name node') DiscoItem.__new__.__defaults__ = (None, None) +class DiscoInfo(namedtuple('DiscoInfo', 'jid node identities features dataforms')): + + __slots__ = [] + + def __new__(cls, jid, node, identities, features, dataforms): + return super(DiscoInfo, cls).__new__(cls, jid, node, identities, + features, dataforms) + + def get_caps_hash(self): + try: + return self.node.split('#')[1] + except Exception: + return None + + class DiscoIdentity(namedtuple('DiscoIdentity', 'category type name lang')): __slots__ = [] diff --git a/nbxmpp/util.py b/nbxmpp/util.py index 79b5060..8fec8ab 100644 --- a/nbxmpp/util.py +++ b/nbxmpp/util.py @@ -257,12 +257,14 @@ def text_to_color(text, background_color): return rc, gc, bc -def compute_caps_hash(info): +def compute_caps_hash(info, compare=True): """ Compute caps hash according to XEP-0115, V1.5 https://xmpp.org/extensions/xep-0115.html#ver-proc :param: info DiscoInfo + :param: compare If True an exception is raised if the hash announced in + the node attr is not equal to what is calculated """ # Initialize an empty string S. string_ = '' @@ -367,4 +369,8 @@ def compute_caps_hash(info): string_ += '%s<' % value hash_ = hashlib.sha1(string_.encode()) - return b64encode(hash_.digest()) + b64hash = b64encode(hash_.digest()) + if compare and b64hash != info.get_caps_hash(): + raise DiscoInfoMalformed('Caps hashes differ: %s != %s' % ( + b64hash, info.get_caps_hash())) + return b64hash |