allow
[ghsmtp.git] / Mailbox.hpp
blob58a48d9731937f06f71f9a203f6c297fe35e172f
1 #ifndef MAILBOX_DOT_HPP
2 #define MAILBOX_DOT_HPP
4 #include "Domain.hpp"
6 #include <optional>
7 #include <ostream>
8 #include <string>
9 #include <utility>
11 class Mailbox {
12 public:
13 Mailbox() = default;
14 Mailbox(std::string_view mailbox);
15 Mailbox(std::string_view local_part, std::string_view domain)
17 set_local(local_part);
18 set_domain(domain);
20 Mailbox(std::string_view local_part, Domain domain)
22 set_local(local_part);
23 set_domain(domain);
26 void set_local(std::string_view local_part) { local_part_ = local_part; }
27 void set_domain(std::string_view d) { domain_.set(d); }
28 void set_domain(Domain d) { domain_ = d; }
29 void clear()
31 local_part_.clear();
32 domain_.clear();
34 std::string const& local_part() const { return local_part_; }
35 Domain const& domain() const { return domain_; }
37 enum class domain_encoding : bool { ascii, utf8 };
39 size_t length(domain_encoding enc = domain_encoding::utf8) const;
41 bool empty() const { return length() == 0; }
43 std::string as_string(domain_encoding enc = domain_encoding::utf8) const;
45 operator std::string() const { return as_string(domain_encoding::utf8); }
47 bool operator==(Mailbox const& rhs) const
49 return (local_part_ == rhs.local_part_) && (domain_ == rhs.domain_);
51 bool operator!=(Mailbox const& rhs) const { return !(*this == rhs); }
53 enum class local_types : uint8_t {
54 unknown,
55 dot_string,
56 quoted_string,
59 enum class domain_types : uint8_t {
60 unknown,
61 domain,
62 address_literal,
65 struct parse_results {
66 std::string_view local;
67 std::string_view domain;
68 local_types local_type = local_types::unknown;
69 domain_types domain_type = domain_types::unknown;
72 static std::optional<parse_results> parse(std::string_view mailbox);
74 static bool validate(std::string_view mailbox);
75 static bool validate_strict_lengths(std::string_view mailbox);
77 private:
78 std::string local_part_;
79 Domain domain_;
82 inline std::ostream& operator<<(std::ostream& s, Mailbox const& mb)
84 return s << static_cast<std::string>(mb);
87 #endif // MAILBOX_DOT_HPP