mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.RegularExpressions;
|
|
using MimeKit;
|
|
|
|
namespace BTCPayServer
|
|
{
|
|
/// <summary>
|
|
/// Validate address in the format "Firstname Lastname <blah@example.com>" See rfc822
|
|
/// </summary>
|
|
public class MailboxAddressValidator
|
|
{
|
|
static ParserOptions _options;
|
|
static MailboxAddressValidator()
|
|
{
|
|
_options = ParserOptions.Default.Clone();
|
|
_options.AllowAddressesWithoutDomain = false;
|
|
}
|
|
public static bool IsMailboxAddress(string? str)
|
|
{
|
|
return TryParse(str, out _);
|
|
}
|
|
public static MailboxAddress Parse(string? str)
|
|
{
|
|
if (!TryParse(str, out var mb))
|
|
throw new FormatException("Invalid mailbox address (rfc822)");
|
|
return mb;
|
|
}
|
|
public static bool TryParse(string? str, [MaybeNullWhen(false)] out MailboxAddress mailboxAddress)
|
|
{
|
|
mailboxAddress = null;
|
|
if (String.IsNullOrWhiteSpace(str))
|
|
return false;
|
|
return MailboxAddress.TryParse(_options, str, out mailboxAddress) && mailboxAddress is not null;
|
|
}
|
|
}
|
|
}
|