from ipaddress import ip_address
from checkdmarc.dmarc import check_dmarc
from checkdmarc.spf import check_spf
import validators
from ipwhois import IPWhois
from whois import whois
from constants import DOMAIN, EMAIL, IPV4, IPV6, URL


def determine(host):
    host_type = ""
    if validators.url(host):
        host_type = URL
    elif validators.domain(host):
        host_type = DOMAIN
    elif validators.ip_address.ipv4(host):
        host_type = IPV4
    elif validators.ip_address.ipv6(host):
        host_type = IPV6
    elif validators.email(host):
        host_type = EMAIL
    else:
        print("NO HOST TYPE")
    return host_type


def domain(host):
    result = dict(whois(host))
    if type(result["domain_name"]) is list:
        result["domain_name"] = result["domain_name"][0]
    return result


def emailsec(host):
    spf = ""
    dmarc = ""
    result_spf = check_spf(host)
    if result_spf["valid"]:
        spf = result_spf["record"]
    result_dmarc = check_dmarc(host)
    if result_dmarc["valid"]:
        dmarc = result_dmarc["record"]
    return spf, dmarc