Allow any old and crufty protocol version
[ghsmtp.git] / message.hpp
blob13c0fbd837ed8c02f09fced8295e7f40ea55c86b
1 #ifndef MESSAGE_DOT_HPP_INCLUDED
2 #define MESSAGE_DOT_HPP_INCLUDED
4 #include <iostream>
5 #include <iterator>
6 #include <optional>
7 #include <string>
8 #include <string_view>
10 #include "Mailbox.hpp"
11 #include "fs.hpp"
12 #include "iequal.hpp"
14 #include <glog/logging.h>
16 namespace message {
18 // RFC-5322 header names
19 auto constexpr ARC_Authentication_Results = "ARC-Authentication-Results";
20 auto constexpr ARC_Message_Signature = "ARC-Message-Signature";
21 auto constexpr ARC_Seal = "ARC-Seal";
23 auto constexpr Authentication_Results = "Authentication-Results";
24 auto constexpr DKIM_Signature = "DKIM-Signature";
25 auto constexpr Delivered_To = "Delivered-To";
26 auto constexpr From = "From";
27 auto constexpr In_Reply_To = "In-Reply-To";
28 auto constexpr Received_SPF = "Received-SPF";
29 auto constexpr Reply_To = "Reply-To";
30 auto constexpr Return_Path = "Return-Path";
31 auto constexpr Subject = "Subject";
33 // MIME headers
34 auto constexpr Content_Type = "Content-Type";
35 auto constexpr MIME_Version = "MIME-Version";
37 struct header {
38 header(std::string_view n, std::string_view v)
39 : name(n)
40 , value(v)
44 std::string as_string() const;
46 std::string_view as_view() const
48 // Verify that name and value views point to the same text.
49 CHECK_EQ(name.begin() + name.length() + 1, value.begin());
50 return {name.begin(),
51 static_cast<size_t>(std::distance(name.begin(), value.end()))};
54 bool operator==(std::string_view n) const { return iequal(n, name); }
56 std::string_view name;
57 std::string_view value;
60 struct name_addr {
61 std::string name;
62 std::string addr;
65 struct mailbox_name_addr_list {
66 std::string name;
67 std::vector<name_addr> name_addr_list;
70 struct parsed {
71 bool parse(std::string_view input);
72 bool parse_hdr(std::string_view input);
74 std::string as_string() const;
76 bool write(std::ostream& out) const;
78 std::vector<header> headers;
80 std::string_view get_header(std::string_view hdr) const;
82 std::string_view field_name;
83 std::string_view field_value;
85 std::string_view body;
87 // Parsing of the RFC-5322.From header
88 mailbox_name_addr_list from_parsed;
89 std::string dmarc_from;
90 std::string dmarc_from_domain;
92 // New RFC5322.From
93 std::string from_str;
95 // RFC5322.Reply
96 std::string reply_to_str;
98 // New Authentication_Results field
99 std::string ar_str;
101 // New DKIM-Signature that includes above AR
102 std::string sig_str;
104 // Added ARC headers
105 std::vector<std::string> arc_hdrs;
108 bool authentication_results_parse(std::string_view input,
109 std::string& authservid,
110 std::string& ar_results);
112 bool authentication(message::parsed& msg,
113 char const* sender,
114 char const* selector,
115 fs::path key_file);
117 void dkim_check(message::parsed& msg, char const* domain);
119 void remove_delivery_headers(message::parsed& msg);
121 void dkim_sign(message::parsed& msg,
122 char const* sender,
123 char const* selector,
124 fs::path key_file);
126 void rewrite_from_to(message::parsed& msg,
127 std::string mail_from,
128 std::string reply_to,
129 char const* sender,
130 char const* selector,
131 fs::path key_file);
133 void print_spf_envelope_froms(char const* domain, message::parsed& msg);
135 } // namespace message
137 #endif // MESSAGE_DOT_HPP_INCLUDED