Meum/Meum.Core/UserAddress.cs
2024-10-22 13:16:20 +05:00

48 lines
1.4 KiB
C#

using System.Net;
namespace Meum.Core;
public class UserAddress
{
public readonly string Name;
public readonly DnsEndPoint RegistrationServer;
public readonly string Full;
public UserAddress(string name, DnsEndPoint registrationServer)
{
Name = name;
RegistrationServer = registrationServer;
Full = $"{name}@{registrationServer}";
}
public UserAddress(string addrstr)
{
int at_index = addrstr.IndexOf('@');
if(at_index == -1)
throw new FormatException($"Invalid user address format '{addrstr}'");
Full = addrstr;
Name = addrstr.Substring(0, at_index);
if(!ValidateName(Name))
throw new FormatException($"Invalid user name '{Name}' in address '{addrstr}'");
string serverstr = addrstr.Substring(at_index + 1);
RegistrationServer = Network.ParseDnsEndPoint(serverstr);
}
public override string ToString() => Full;
public override int GetHashCode() => ToString().GetHashCode();
private static bool ValidateName(string? name)
{
if(string.IsNullOrEmpty(name))
return false;
foreach (char c in name)
{
if(!char.IsAsciiLetterOrDigit(c) && c != '.' && c != '-' && c != '_')
return false;
}
return true;
}
}