ip_allow_
[ghsmtp.git] / Mailbox.hpp
blobb8f032906ce48a8ef65d355b16aee78fa85369bd
1 #ifndef MAILBOX_DOT_HPP
2 #define MAILBOX_DOT_HPP
4 #include "Domain.hpp"
6 #include <cstdint>
7 #include <optional>
8 #include <ostream>
9 #include <string>
10 #include <utility>
12 class Mailbox {
13 public:
14 Mailbox() = default;
15 Mailbox(std::string_view mailbox);
16 Mailbox(std::string_view local_part, std::string_view domain)
18 set_local(local_part);
19 set_domain(domain);
21 Mailbox(std::string_view local_part, Domain domain)
23 set_local(local_part);
24 set_domain(domain);
27 void set_local(std::string_view local_part) { local_part_ = local_part; }
28 void set_domain(std::string_view d) { domain_.set(d); }
29 void set_domain(Domain d) { domain_ = d; }
30 void clear()
32 local_part_.clear();
33 domain_.clear();
35 std::string const& local_part() const { return local_part_; }
36 Domain const& domain() const { return domain_; }
38 enum class domain_encoding : bool { ascii, utf8 };
40 size_t length(domain_encoding enc = domain_encoding::utf8) const;
42 bool empty() const { return length() == 0; }
44 std::string as_string(domain_encoding enc = domain_encoding::utf8) const;
46 operator std::string() const { return as_string(domain_encoding::utf8); }
48 bool operator==(Mailbox const& rhs) const
50 return (local_part_ == rhs.local_part_) && (domain_ == rhs.domain_);
52 bool operator!=(Mailbox const& rhs) const { return !(*this == rhs); }
54 enum class local_types : uint8_t {
55 unknown,
56 dot_string,
57 quoted_string,
60 enum class domain_types : uint8_t {
61 unknown,
62 domain,
63 address_literal,
66 struct parse_results {
67 std::string_view local;
68 std::string_view domain;
69 local_types local_type = local_types::unknown;
70 domain_types domain_type = domain_types::unknown;
73 static std::optional<parse_results> parse(std::string_view mailbox);
75 static bool validate(std::string_view mailbox);
76 static bool validate_strict_lengths(std::string_view mailbox);
78 private:
79 std::string local_part_;
80 Domain domain_;
83 inline std::ostream& operator<<(std::ostream& s, Mailbox const& mb)
85 return s << static_cast<std::string>(mb);
88 #endif // MAILBOX_DOT_HPP