share
[ghsmtp.git] / SPF.hpp
blobd23ac077c916f472e36a81438a82f14e4e4650eb
1 #ifndef SPF_DOT_HPP
2 #define SPF_DOT_HPP
4 #include <ostream>
6 #include <glog/logging.h>
8 // forward stuff from <spf2/spf.h>
9 typedef struct SPF_server_struct SPF_server_t;
10 typedef struct SPF_request_struct SPF_request_t;
11 typedef struct SPF_response_struct SPF_response_t;
13 namespace SPF {
15 class Result {
16 public:
17 Result() = default;
18 Result(int value); // int for SPF_result_t
20 enum class value_t {
21 INVALID,
22 NEUTRAL,
23 PASS,
24 FAIL,
25 SOFTFAIL,
26 NONE,
27 TEMPERROR,
28 PERMERROR,
31 // clang-format off
32 static constexpr auto INVALID = value_t::INVALID;
33 static constexpr auto NEUTRAL = value_t::NEUTRAL;
34 static constexpr auto PASS = value_t::PASS;
35 static constexpr auto FAIL = value_t::FAIL;
36 static constexpr auto SOFTFAIL = value_t::SOFTFAIL;
37 static constexpr auto NONE = value_t::NONE;
38 static constexpr auto TEMPERROR = value_t::TEMPERROR;
39 static constexpr auto PERMERROR = value_t::PERMERROR;
40 // clang-format on
42 static char const* c_str(value_t value);
44 char const* c_str() const { return c_str(value_); }
46 operator value_t() const { return value_; }
47 explicit operator char const*() const { return c_str(); }
49 private:
50 value_t value_{INVALID};
53 std::ostream& operator<<(std::ostream& os, Result result);
54 std::ostream& operator<<(std::ostream& os, Result::value_t result);
56 class Server {
57 public:
58 Server(Server const&) = delete;
59 Server& operator=(Server const&) = delete;
61 explicit Server(char const* fqdn);
62 ~Server();
64 static class initializer {
65 public:
66 initializer();
67 } init;
69 private:
70 SPF_server_t* srv_{nullptr};
72 // We map libspf2's levels of error, warning, info and debug to our
73 // own fatal, error, warning and info log levels.
74 static void log_error_(const char* file, int line, char const* errmsg)
75 __attribute__((noreturn))
77 LOG(FATAL) << file << ":" << line << " " << errmsg;
79 static void log_warning_(const char* file, int line, char const* errmsg)
81 LOG(WARNING) << file << ":" << line << " " << errmsg;
83 static void log_info_(const char* file, int line, char const* errmsg)
85 LOG(INFO) << file << ":" << line << " " << errmsg;
88 friend class Request;
91 class Request {
92 public:
93 Request(Request const&) = delete;
94 Request& operator=(Request const&) = delete;
96 Request()
97 : req_(nullptr)
100 explicit Request(Server const& srv);
101 ~Request();
103 void set_ip_str(char const* ip);
104 void set_ipv4_str(char const* ipv4);
105 void set_ipv6_str(char const* ipv6);
106 void set_helo_dom(char const* dom);
107 void set_env_from(char const* frm);
108 char const* get_sender_dom() const;
110 private:
111 SPF_request_t* req_{nullptr};
113 friend class Response;
116 class Response {
117 public:
118 Response(Response const&) = delete;
119 Response& operator=(Response const&) = delete;
121 Response();
122 explicit Response(Request const& req);
123 ~Response();
125 Result result() const;
126 char const* smtp_comment() const;
127 char const* header_comment() const;
128 char const* received_spf() const;
130 private:
131 SPF_response_t* res_{nullptr};
134 } // namespace SPF
136 #endif // SPF_DOT_HPP