better with obs forms
[ghsmtp.git] / message.hpp
blob1b2e2391868c041b076ba19969e46be1dcb50e3b
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::string maybe_name;
68 std::vector<name_addr> name_addr_list;
71 struct parsed {
72 bool parse(std::string_view input);
73 bool parse_hdr(std::string_view input);
75 std::string as_string() const;
77 bool write(std::ostream& out) const;
79 std::vector<header> headers;
81 std::string_view get_header(std::string_view hdr) const;
83 std::string_view field_name;
84 std::string_view field_value;
86 std::string_view body;
88 // Parsing of the RFC-5322.From header
89 mailbox_name_addr_list from_parsed;
90 std::string dmarc_from;
91 std::string dmarc_from_domain;
93 // New RFC5322.From
94 std::string from_str;
96 // RFC5322.Reply
97 std::string reply_to_str;
99 // New Authentication_Results field
100 std::string ar_str;
102 // New DKIM-Signature that includes above AR
103 std::string sig_str;
105 // Added ARC headers
106 std::vector<std::string> arc_hdrs;
109 bool mailbox_list_parse(std::string_view input,
110 mailbox_name_addr_list& name_addr_list);
112 bool authentication_results_parse(std::string_view input,
113 std::string& authservid,
114 std::string& ar_results);
116 bool authentication(message::parsed& msg,
117 char const* sender,
118 char const* selector,
119 fs::path key_file);
121 void dkim_check(message::parsed& msg, char const* domain);
123 void remove_delivery_headers(message::parsed& msg);
125 void dkim_sign(message::parsed& msg,
126 char const* sender,
127 char const* selector,
128 fs::path key_file);
130 void rewrite_from_to(message::parsed& msg,
131 std::string mail_from,
132 std::string reply_to,
133 char const* sender,
134 char const* selector,
135 fs::path key_file);
137 void print_spf_envelope_froms(char const* domain, message::parsed& msg);
139 } // namespace message
141 #endif // MESSAGE_DOT_HPP_INCLUDED