Changeset 9640

Show
Ignore:
Timestamp:
05/14/08 20:30:39 (3 months ago)
Author:
asterix
Message:

fix caps hash computaion according to XEP-0115. fixes #3925

Location:
trunk/src/common
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/common/caps.py

    r9532 r9640  
    237237                node, hash = node.split('#', 1) 
    238238                computed_hash = helpers.compute_caps_hash(identities, features, 
    239                         contact.caps_hash_method) 
     239                        dataforms=dataforms, hash_method=contact.caps_hash_method) 
    240240                if computed_hash != hash: 
    241241                        # wrong hash, forget it 
  • trunk/src/common/helpers.py

    r9610 r9640  
    12461246        return 0 
    12471247 
    1248 def compute_caps_hash(identities, features, hash_method='sha-1'): 
     1248def sort_dataforms_func(d1, d2): 
     1249        f1 = d1.getField('FORM_TYPE') 
     1250        f2 = d2.getField('FORM_TYPE') 
     1251        if f1 and f2 and (f1.getValue() < f2.getValue()): 
     1252                return -1 
     1253        return 1 
     1254 
     1255def compute_caps_hash(identities, features, dataforms=[], hash_method='sha-1'): 
     1256        '''Compute caps hash according to XEP-0115, V1.5 
     1257         
     1258        dataforms are xmpp.DataForms objects as common.dataforms don't allow several 
     1259        values without a field type list-multi''' 
    12491260        S = '' 
    12501261        identities.sort(cmp=sort_identities_func) 
     
    12671278        for f in features: 
    12681279                S += '%s<' % f 
     1280        dataforms.sort(cmp=sort_dataforms_func) 
     1281        for dataform in dataforms: 
     1282                # fields indexed by var 
     1283                fields = {} 
     1284                for f in dataform.getChildren(): 
     1285                        fields[f.getVar()] = f 
     1286                form_type = fields.get('FORM_TYPE') 
     1287                if form_type: 
     1288                        S += form_type.getValue() + '<' 
     1289                        del fields['FORM_TYPE'] 
     1290                vars = fields.keys() 
     1291                vars.sort() 
     1292                for var in vars: 
     1293                        S += '%s<' % var 
     1294                        values = fields[var].getValues() 
     1295                        values.sort() 
     1296                        for value in values: 
     1297                                S += '%s<' % value 
     1298 
     1299        print S 
    12691300        if hash_method == 'sha-1': 
    12701301                hash = hash_sha1(S)