some work
[ghsmtp.git] / Mailbox.hpp
blobde43804e285083fd73acdb397f3bdc72691267cb
1 #ifndef MAILBOX_DOT_HPP
2 #define MAILBOX_DOT_HPP
4 #include "Domain.hpp"
6 #include <ostream>
7 #include <string>
8 #include <utility>
10 class Mailbox {
11 public:
12 Mailbox() = default;
13 Mailbox(std::string_view mailbox);
14 Mailbox(std::string_view local_part, std::string_view domain)
16 set_local(local_part);
17 set_domain(domain);
19 Mailbox(std::string_view local_part, Domain domain)
21 set_local(local_part);
22 set_domain(domain);
25 void set_local(std::string_view local_part) { local_part_ = local_part; }
26 void set_domain(std::string_view d) { domain_.set(d); }
27 void set_domain(Domain d) { domain_ = d; }
28 void clear()
30 local_part_.clear();
31 domain_.clear();
33 std::string const& local_part() const { return local_part_; }
34 Domain const& domain() const { return domain_; }
36 enum class domain_encoding : bool { ascii, utf8 };
38 size_t length(domain_encoding enc = domain_encoding::utf8) const;
40 bool empty() const { return length() == 0; }
42 std::string as_string(domain_encoding enc = domain_encoding::utf8) const;
44 operator std::string() const { return as_string(domain_encoding::utf8); }
46 bool operator==(Mailbox const& rhs) const
48 return (local_part_ == rhs.local_part_) && (domain_ == rhs.domain_);
50 bool operator!=(Mailbox const& rhs) const { return !(*this == rhs); }
52 enum class local_types : uint8_t {
53 unknown,
54 dot_string,
55 quoted_string,
58 enum class domain_types : uint8_t {
59 unknown,
60 domain,
61 address_literal,
64 struct parse_results {
65 std::string_view local;
66 std::string_view domain;
67 local_types local_type = local_types::unknown;
68 domain_types domain_type = domain_types::unknown;
71 static std::optional<parse_results> parse(std::string_view mailbox);
73 static bool validate(std::string_view mailbox);
74 static bool validate_strict_lengths(std::string_view mailbox);
76 private:
77 std::string local_part_;
78 Domain domain_;
81 inline std::ostream& operator<<(std::ostream& s, Mailbox const& mb)
83 return s << static_cast<std::string>(mb);
86 #endif // MAILBOX_DOT_HPP