<commit_before><commit_msg>Can now look up OTR fingerprints (looking aside a bit)<commit_after>#!/usr/bin/python

import getdns, pprint
import hashlib

dnssec_status = {
    "GETDNS_DNSSEC_SECURE" : 400,
    "GETDNS_DNSSEC_BOGUS" : 401,
    "GETDNS_DNSSEC_INDETERMINATE" : 402,
    "GETDNS_DNSSEC_INSECURE" : 403,
    "GETDNS_DNSSEC_NOT_PERFORMED" : 404
}

def buildOtrFingerprintHostname(jid):
    parts = jid.split('@')
    hostname  = hashlib.sha224(parts[0]).hexdigest()
    hostname += "._otrfingerprint."
    hostname += parts[1]
    hostname += ".iain.getdnsapi.net"
    return hostname

def lookupOtrFingerprintRecords(hostname):
    ctx = getdns.context_create()
    extensions = { "dnssec_return_status" : getdns.GETDNS_EXTENSION_TRUE }
    query_results = getdns.general(ctx, hostname, getdns.GETDNS_RRTYPE_TXT, extensions=extensions)
    records = []
    for reply in query_results['replies_tree']:
        dnssec_valid = True if reply['dnssec_status'] == 400 else False
        for answer in reply['answer']:
            if 'txt_strings' in answer['rdata'].keys():
                for record in answer['rdata']['txt_strings']:
                    if record[0:7] == "v=otr1 ":
                        records.append((record, dnssec_valid))
    return records

def parseOtrFingerprintRecord(value):
    parts = value.split(" ")
    fingerprints = []
    for part in parts:
        if part[0:2] == "f:":
            fingerprints.append(part[2:])
    return fingerprints

def getOtrFingerprints(jid):
    hostname = buildOtrFingerprintHostname(jid)
    records = lookupOtrFingerprintRecords(hostname)
    fingerprints = []
    for record, dnssec_valid in records:
        for fingerprint in parseOtrFingerprintRecord(record):
            fingerprints.append((fingerprint, dnssec_valid))
    return fingerprints

pprint.pprint(getOtrFingerprints("<EMAIL>"))

{'fraction_non_alphanumeric': 0.1120042872454448, 'fraction_numerical': 0.015541264737406217, 'mean_word_length': 3.4987951807228916, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4116638', 'n_tokens_mistral': 660, 'n_tokens_neox': 632, 'n_words': 119}