def xmpp_server_error_template():

    briefing_template = "The {{disco_type}} query got an error response from"
    briefing_template += " " + bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "The XMPP server at"
    message_template += " " + bold("{{xmpp_server}}") + " returned the follo"
    message_template += "wing error: " + code("{{&error}}")
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_server_error_template():

    briefing_template = "The {{disco_type}} query got an error response from"
    briefing_template += " " + bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "The XMPP server at"
    message_template += " " + bold("{{xmpp_server}}") + " returned the follo"
    message_template += "wing error: " + code("{{&error}}")
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_connection_problem_template():

    briefing_template = "Could not establish a XMPP connection to "
    briefing_template += bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "Beware it is NOT a"
    message_template += " problem with domain " + bold("{{domain_url}}")
    message_template += " nor the XMPP server at " + bold("{{xmpp_server}}")
    message_template += "."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_connection_problem_template():

    briefing_template = "Could not establish a XMPP connection to "
    briefing_template += bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "Beware it is NOT a"
    message_template += " problem with domain " + bold("{{domain_url}}")
    message_template += " nor the XMPP server at " + bold("{{xmpp_server}}")
    message_template += "."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_disco_query_send_error_template():

    briefing_template = "Could not send " + code("{{disco_type}}") + " query"
    briefing_template += " to " + bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "Beware it may not "
    message_template += "be a problem with domain " + bold("{{domain_url}}")
    message_template += " or the XMPP server at " + bold("{{xmpp_server}}")
    message_template += "." + breakline() + "{{#error}}Problem: {{error}}{{/"
    message_template += "error}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_disco_query_send_error_template():

    briefing_template = "Could not send " + code("{{disco_type}}") + " query"
    briefing_template += " to " + bold("{{xmpp_server}}") + "."
    message_template = briefing_template + breakline() + "Beware it may not "
    message_template += "be a problem with domain " + bold("{{domain_url}}")
    message_template += " or the XMPP server at " + bold("{{xmpp_server}}")
    message_template += "." + breakline() + "{{#error}}Problem: {{error}}{{/"
    message_template += "error}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_server_error_template():

    briefing_template = "The {{disco_type}} query got an error response from"
    briefing_template += "{{^xmpp_server}}" + bold("{{domain_url}}")
    briefing_template += "{{/xmpp_server}}"
    briefing_template += "{{#xmpp_server}}" + bold("{{xmpp_server}}")
    briefing_template += "{{/xmpp_server}}."
    message_template = briefing_template
    message_template += "{{#error}}" + breakline()
    message_template += "Error response: " + code("{{&error}}") +"{{/error}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_disco_query_send_error_template():

    briefing_template = "Could not send "+code("{{disco_type}}")+" query to "
    briefing_template += "{{^xmpp_server}}" + bold("{{domain_url}}")
    briefing_template += "{{/xmpp_server}}"
    briefing_template += "{{#xmpp_server}}" + bold("{{xmpp_server}}")
    briefing_template += "{{/xmpp_server}}."
    message_template = briefing_template
    message_template += "{{#error}}" + breakline()
    message_template += "Error response: " + code("{{&error}}") +"{{/error}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def xmpp_connection_problem_template():

    briefing_template = "Could not establish a client XMPP connection."
    message_template = briefing_template + breakline() + "Beware it is NOT a"
    message_template += " problem with domain " + bold("{{domain_url}}")+ "."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def warning_template():

    briefing_template = bold("Precondition problem:") + " "
    briefing_template += code("{{warning}}")
    message_template = "This test did not even run. " + briefing_template
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def not_buddycloud_enabled_template():

    briefing_template = bold("{{domain_url}}") + " is not "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = "Congratulations! " + briefing_template
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def not_buddycloud_enabled_template():

    briefing_template = bold("{{domain_url}}") + " is not "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = "Congratulations! " + briefing_template
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def buddycloud_enabled_conflict_template():

    briefing_template = bold("{{domain_url}}") + " is "
    briefing_template += italic("buddycloud enabled") + ","
    briefing_template += " but we detected a conflict problem."
    message_template = briefing_template + breakline()
    message_template += "Using " + code("Service Discovery")
    message_template += ", we found your channel server at "
    message_template += bold("{{channel_server}}") + "!" + breakline()
    message_template += "But we also found a channel server through "
    message_template += code("TXT record lookup") + ", which is different: "
    message_template += bold("{{channel_server2}}") + "!" + breakline()
    message_template += "The channel server addresses found through "
    message_template += code("Service Discovery") + " and " + code("TXT record lookup") + " must match!"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def warning_template():

    briefing_template = bold("Precondition problem:") + " "
    briefing_template += code("{{warning}}")
    message_template = "This test did not even run. " + briefing_template
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def is_buddycloud_enabled_template():

    briefing_template = bold("{{domain_url}}") + " is "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = "Congratulations! " + briefing_template + breakline()
    message_template += "{{#discovery}}Using " + code("Service Discovery")
    message_template += ", we found your channel server at "
    message_template += bold("{{channel_server}}") + "!" + breakline()
    message_template += "{{#ptr_record}}We also found your channel server "
    message_template += code("PTR record") + ".{{/ptr_record}}{{/discovery}}"
    message_template += "{{#ptr_record}}Using " + code("PTR record") + ", we"
    message_template += " found your channel server at "
    message_template += bold("{{channel_server}}") + "!" + breakline() + "Bu"
    message_template += "t we could not find it through "
    message_template += code("Service Discovery") + ".{{/ptr_record}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def is_buddycloud_enabled_template():

    briefing_template = bold("{{domain_url}}") + " is "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = "Congratulations! " + briefing_template + breakline()
    message_template += "{{#discovery}}Using " + code("Service Discovery")
    message_template += ", we found your channel server at "
    message_template += bold("{{channel_server}}") + "!" + breakline()
    message_template += "{{#ptr_record}}We also found your channel server "
    message_template += code("PTR record") + ".{{/ptr_record}}{{/discovery}}"
    message_template += "{{#ptr_record}}Using " + code("PTR record") + ", we"
    message_template += " found your channel server at "
    message_template += bold("{{channel_server}}") + "!" + breakline() + "Bu"
    message_template += "t we could not find it through "
    message_template += code("Service Discovery") + ".{{/ptr_record}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def record_correct_template():

    briefing_template = "We could find the API server " + code("TXT record")
    briefing_template += " for " + bold("{{domain_url}}") + "!"
    message_template = "Congratulations! " + briefing_template + breakline()
    message_template += breakline() + "This is your record:" + breakline()
    message_template += code_block("{{&record}}")
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def no_record_template():

    briefing_template = "Could not find the API server " + code("TXT record")
    briefing_template += " for " + bold("{{domain_url}}") + "!"
    message_template = briefing_template + breakline()
    message_template += "Please add one in your DNS settings." + breakline()
    message_template += "Refer to the " + link("install instructions",
        "http://buddycloud.com/install#buddycloud_dns_") + " for more info."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def multiple_problems_template():

    briefing_template = "No xmpp server found is "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = briefing_template + breakline() + "Several problems w"
    message_template += "ith XMPP servers found: " + breakline() + breakline()
    message_template += "{{#xmpp_servers}}" + bold("{{name}}:") + breakline()
    message_template += "{{&error}}" + breakline() + breakline() + "{{/xmpp_"
    message_template += "servers}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def multiple_problems_template():

    briefing_template = "No xmpp server found is "
    briefing_template += italic("buddycloud enabled") + "."
    message_template = briefing_template + breakline() + "Several problems w"
    message_template += "ith XMPP servers found: " + breakline() + breakline()
    message_template += "{{#xmpp_servers}}" + bold("{{name}}:") + breakline()
    message_template += "{{&error}}" + breakline() + breakline() + "{{/xmpp_"
    message_template += "servers}}"
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def testFunction(domain_url):

    view = {"domain_url":domain_url}

    if ( domainNameLookup(domain_url)[0] != 0 ):
        view["warning"] = "%s not found!" % (domain_url)
        return warning(view)

    try:
        resolver = dns.resolver.Resolver()
        nameserver = getAuthoritativeNameserver(domain_url)
        if ( nameserver ):
            resolver.nameservers = [ nameserver ]
        answer = resolver.query("_buddycloud-api._tcp."+domain_url,
            dns.rdatatype.TXT)
    except Exception as e:
        return no_record(view)

    else:

        txt_answer = answer[0].to_text()
        view["record"] = txt_answer

        for test in record_well_formedness_tests:
            if ( not test[0](txt_answer) ):
                view["error"] = code("TXT record") + " is malformed! " + test[1]
                return record_error(view)

        domain =  txt_answer[txt_answer.find("host=")+5:
            txt_answer.find(" ", txt_answer.find("host=")+5)]
        port = txt_answer[txt_answer.find("port=")+5:
            txt_answer.find(" ", txt_answer.find("port=")+5)]
        path = txt_answer[txt_answer.find("path=")+5:
            txt_answer.find(" ", txt_answer.find("path=")+5)]
        protocol = txt_answer[txt_answer.find("protocol=")+9:
            txt_answer.find(" ", txt_answer.find("protocol=")+9)]

        if ( protocol.lower() != "https" ):
            view["error"] = code("TXT record") + "\'s " + bold("protocol") + "\
                attribute must have value set to " + code("https") + "!"
            return record_error(view)

        output = {
            'protocol' : protocol,
            'domain' : domain,
            'port' : port,
            'path' : path
        }
        return record_correct(view, output)
def record_error_template():

    briefing_template = "We could find the API server " + code("TXT record")
    briefing_template += " for " + bold("{{domain_url}}")
    briefing_template += " but it is problematic!"
    message_template = briefing_template + breakline()
    message_template += breakline() + "This is your record:" + breakline()
    message_template += code_block("{{&record}}")
    message_template += "{{&error}}" + breakline()
    message_template += "Please fix this in your DNS settings." + breakline()
    message_template += "Refer to the " + link("install instructions",
        "http://buddycloud.com/install#buddycloud_dns_") + " for more info."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
def record_error_template():

    briefing_template = "We could find the Buddycloud server " + code("TXT record")
    briefing_template += " for " + bold("{{domain_url}}")
    briefing_template += " but it is problematic!"
    message_template = briefing_template + breakline()
    message_template += breakline() + "This is your record:" + breakline()
    message_template += code_block("{{&record}}")
    message_template += breakline() + "{{&error}}" + breakline()
    message_template += "Please fix this in your DNS settings." + breakline()
    message_template += "Refer to this " + link("document",
        "http://buddycloud.github.io/buddycloud-xep/#DNS-discovery") + " for more info."
    briefing_template = parse(briefing_template)
    message_template = parse(message_template)
    return briefing_template, message_template
            check[2](check[0].search(TXT_record)) == check[1] )
        return veredict
    return checker
record_well_formedness_tests = []

def has_version():

    checks = []
    is_none = lambda match: match == None

    find_version = re.compile("(v=[0-9]+(\.{1}[0-9]+){0,1}){1}"), False, is_none
    checks.append(find_version)

    return _build_checker(checks)
record_well_formedness_tests.append((has_version(),
    bold("version") + " attribute missing!"))

def has_server():

    checks = []
    is_none = lambda match: match == None

    find_server = re.compile("(server=[^\"\']+){1}"), False, is_none
    checks.append(find_server)

    return _build_checker(checks)
record_well_formedness_tests.append((has_server(),
    bold("server") + " attribute missing!"))

def has_exactly_two_specs():
            check[2](check[0].search(TXT_record)) == check[1] )
        return veredict
    return checker
record_well_formedness_tests = []

def has_version():

    checks = []
    is_none = lambda match: match == None

    find_version = re.compile("(v=[0-9]+(\.{1}[0-9]+){0,1}){1}"), False, is_none
    checks.append(find_version)

    return _build_checker(checks)
record_well_formedness_tests.append((has_version(),
    bold("version") + " attribute missing!"))

def has_host():

    checks = []
    is_none = lambda match: match == None

    find_host = re.compile("(host=[^\"\']+){1}"), False, is_none
    checks.append(find_host)

    return _build_checker(checks)
record_well_formedness_tests.append((has_host(),
    bold("host") + " attribute missing!"))

def has_protocol():