Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits: 54d8fea2 by Philipp Hörist at 2019-06-29T15:37:42Z Add compare argument to compute_caps_hash()
- - - - -
2 changed files:
- nbxmpp/structs.py - nbxmpp/util.py
Changes:
===================================== 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__ = []
===================================== 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
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/54d8fea2057e5b301ef4ed167f0...