diff options
author | Jefry Lagrange <jefry.reyes@gmail.com> | 2012-01-16 04:37:00 +0400 |
---|---|---|
committer | Jefry Lagrange <jefry.reyes@gmail.com> | 2012-01-16 04:37:00 +0400 |
commit | 376777091e7cdc9d021595481f517ae182cae6c5 (patch) | |
tree | 0c23c3fee83f7e5e09868e37fc80258b513ba611 /src/common/xmpp | |
parent | 76bc866a98f79bd44a449a29c6184176e8a34d76 (diff) |
support for xep-300
Diffstat (limited to 'src/common/xmpp')
-rw-r--r-- | src/common/xmpp/protocol.py | 91 |
1 files changed, 88 insertions, 3 deletions
diff --git a/src/common/xmpp/protocol.py b/src/common/xmpp/protocol.py index 87bc44ec6..82b16a9c0 100644 --- a/src/common/xmpp/protocol.py +++ b/src/common/xmpp/protocol.py @@ -23,6 +23,7 @@ sub- stanzas) handling routines from simplexml import Node, NodeBuilder import time import string +import hashlib def ascii_upper(s): trans_table = string.maketrans(string.ascii_lowercase, @@ -89,7 +90,7 @@ NS_JINGLE_ERRORS = 'urn:xmpp:jingle:errors:1' # XEP-0166 NS_JINGLE_RTP = 'urn:xmpp:jingle:apps:rtp:1' # XEP-0167 NS_JINGLE_RTP_AUDIO = 'urn:xmpp:jingle:apps:rtp:audio' # XEP-0167 NS_JINGLE_RTP_VIDEO = 'urn:xmpp:jingle:apps:rtp:video' # XEP-0167 -NS_JINGLE_FILE_TRANSFER='urn:xmpp:jingle:apps:file-transfer:1' # XEP-0234 +NS_JINGLE_FILE_TRANSFER ='urn:xmpp:jingle:apps:file-transfer:1' # XEP-0234 NS_JINGLE_XTLS='urn:xmpp:jingle:security:xtls:0' # XTLS: EXPERIMENTAL security layer of jingle NS_JINGLE_RAW_UDP = 'urn:xmpp:jingle:transports:raw-udp:1' # XEP-0177 NS_JINGLE_ICE_UDP = 'urn:xmpp:jingle:transports:ice-udp:1' # XEP-0176 @@ -160,7 +161,12 @@ NS_PUBKEY_PUBKEY = 'urn:xmpp:pubkey:2' NS_PUBKEY_REVOKE = 'urn:xmpp:revoke:2' NS_PUBKEY_ATTEST = 'urn:xmpp:attest:2' NS_STREAM_MGMT = 'urn:xmpp:sm:2' # XEP-198 - +NS_HASHES = 'urn:xmpp:hashes:0' # XEP-300 +NS_HASHES_MD5 = 'urn:xmpp:hash-function-textual-names:md5' +NS_HASHES_SHA1 = 'urn:xmpp:hash-function-textual-names:sha-1' +NS_HASHES_SHA256 = 'urn:xmpp:hash-function-textual-names:sha-256' +NS_HASHES_SHA512 = 'urn:xmpp:hash-function-textual-names:sha-512' + xmpp_stream_error_conditions = ''' bad-format -- -- -- The entity has sent XML that cannot be processed. bad-namespace-prefix -- -- -- The entity has sent a namespace prefix that is unsupported, or has sent no namespace prefix on an element that requires such a prefix. @@ -1030,7 +1036,85 @@ class Iq(Protocol): attrs={'id': self.getID()}) iq.setQuery(self.getQuery().getName()).setNamespace(self.getQueryNS()) return iq - + +class Hashes(Node): + """ + Hash elements for various XEPs as defined in XEP-300 + """ + + """ + RECOMENDED HASH USE: + Algorithm Support + MD2 MUST NOT + MD4 MUST NOT + MD5 MAY + SHA-1 MUST + SHA-256 MUST + SHA-512 SHOULD + """ + + supported = ('md5', 'sha-1', 'sha-256', 'sha-512') + + def __init__(self, nsp=NS_HASHES): + Node.__init__(self, None, {}, [], None, None,False, None) + self.setNamespace(nsp) + self.setName('hashes') + + def calculateHash(self, algo, file_string): + """ + Calculate the hash and add it. It is preferable doing it here + instead of doing it all over the place in Gajim. + """ + hl = None + hash = None + + # file_string can be a string or a file + if type(file_string) == str: # if it is a string + if algo == 'md5': + hl = hashlib.md5() + elif algo == 'sha-1': + hl = hashlib.sha1() + elif algo == 'sha-256': + hl = hashlib.sha256() + elif algo == 'sha-512': + hl = hashlib.sha512() + + if hl == None: + # Raise exception + raise Exception('Hash algorithm not supported') + else: + hl.update(file_string) + hash = hl.hexdigest() + else: # if it is a file + + if algo == 'md5': + hl = hashlib.md5() + elif algo == 'sha-1': + hl = hashlib.sha1() + elif algo == 'sha-256': + hl = hashlib.sha256() + elif algo == 'sha-512': + hl = hashlib.sha512() + + if hl == None: + # Raise exception + raise Exception('Hash algorithm not supported') + else: + for line in file_string: + hl.update(line) + hash = hl.hexdigest() + + self.addHash(hash, algo) + + def addHash(self, hash, algo): + """ + More than one hash can be added. Although it is permitted, it should + not be done for big files because it could slow down Gajim. + """ + attrs = {} + attrs['algo'] = algo + self.addChild('hash', attrs, [hash]) + class Acks(Node): """ Acknowledgement elements for Stream Management @@ -1413,3 +1497,4 @@ class DataForm(Node): Simple dictionary interface for setting datafields values by their names """ return self.setField(name).setValue(val) + |