using System.Globalization; namespace AIStudio.Tools.Mail; public static class MailServerHosts { /// /// The host in the form certificates and the allowed hosts for root certificates use. /// /// The host as somebody wrote it, e.g., with umlauts or surrounding white space. /// The host in its ASCII form, or an IP address as written. /// True when the text is a host name or an IP address. public static bool TryGetIdnHost(string host, out string idnHost) { idnHost = host.Trim(); switch (Uri.CheckHostName(idnHost)) { case UriHostNameType.IPv4: case UriHostNameType.IPv6: return true; case UriHostNameType.Dns: try { idnHost = new IdnMapping().GetAscii(idnHost); return true; } catch (ArgumentException) { return false; } default: return false; } } /// /// Whether two hosts name the same server, however each was written. /// /// /// Case, umlauts against their ASCII form, and a closing dot make no difference. A text which /// is no host at all is the same as nothing. /// /// The one host. /// The other host. /// True when both are hosts and name the same server. public static bool AreSame(string host, string otherHost) => TryGetIdnHost(host, out var idnHost) && TryGetIdnHost(otherHost, out var otherIdnHost) && idnHost.TrimEnd('.').Equals(otherIdnHost.TrimEnd('.'), StringComparison.OrdinalIgnoreCase); }