Compare commits

...

4 Commits

  1. 20
      LICENSE
  2. 7
      include/mail.hpp
  3. 28
      include/protocol/protocol.hpp
  4. 37
      src/bogofilter-smtpd.cpp
  5. 31
      src/protocol/protocol.cpp

@ -0,0 +1,20 @@
Copyright (C) 2020 T.H.J. <thj@thj.no>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -14,7 +14,12 @@ namespace mail {
std::vector<std::string> body;
};
using session_map_t = std::unordered_map<protocol::session_t, message_t>;
struct session_t {
bool has_header = 0;
message_t message;
};
using session_map_t = std::unordered_map<protocol::session_key_t, session_t>;
}
#endif

@ -18,18 +18,33 @@ namespace protocol {
using handler_t = std::function<void(protocol_t&, const message_t&)>;
using handler_map_t = std::map<token::pattern_t, handler_t>;
struct session_key_t {
std::string session;
std::string token;
};
bool operator==(const session_key_t& l, const session_key_t& r);
session_key_t session_key_from(const token::list_t& tokens);
session_key_t session_key_from(const message_t& message);
class protocol_t {
public:
void set_handlers(const handler_map_t handlers);
void absorb(const std::string& input);
void send(const token::list_t& tokens);
void submit_message(const session_key_t& session_key, const std::vector<std::string>& header, const std::vector<std::string>& body);
protected:
virtual void emit(const std::string& output) { };
private:
handler_map_t handlers;
size_t max_pattern_length;
void send_data_line(const session_key_t& session_key, const std::string& data_line);
};
class ios_protocol_t : public protocol_t {
@ -45,20 +60,11 @@ namespace protocol {
std::ostream& out;
};
struct session_t {
std::string session;
std::string token;
};
bool operator==(const session_t& l, const session_t& r);
session_t session_from(token::list_t tokens);
session_t session_from(message_t message);
}
namespace std {
template<> struct hash<protocol::session_t> {
std::size_t operator()(protocol::session_t const& session) const noexcept {
template<> struct hash<protocol::session_key_t> {
std::size_t operator()(protocol::session_key_t const& session) const noexcept {
auto session_hash = std::hash<std::string>{}(session.session);
auto token_hash = std::hash<std::string>{}(session.token);
return session_hash ^ (token_hash << 1);

@ -12,7 +12,7 @@ const bool debug = true;
using namespace std;
mail::session_map_t mail_messages;
mail::session_map_t sessions;
void on_ready(protocol::protocol_t& protocol, protocol::message_t message) {
debug && cerr << "Ready" << endl;
@ -23,15 +23,40 @@ void on_ready(protocol::protocol_t& protocol, protocol::message_t message) {
}
void on_data(protocol::protocol_t& protocol, protocol::message_t message) {
mail_messages.emplace(protocol::session_from(message), mail::message_t{});
sessions.emplace(protocol::session_key_from(message), mail::session_t{});
}
void on_dataline(protocol::protocol_t& protocol, protocol::message_t message) {
debug && cerr << "Dataline" << endl;
void on_data_line(protocol::protocol_t& protocol, protocol::message_t message) {
auto tokens = protocol::token::decompose(message.input);
auto& session = sessions.at(protocol::session_key_from(tokens));
auto& data_line = tokens.at(7);
if(!session.has_header) {
if(data_line == "") {
session.has_header = true;
return;
}
session.message.header.push_back(data_line);
return;
}
if(data_line == ".") {
return;
}
if(data_line == "..") {
session.message.body.push_back(".");
return;
}
session.message.body.push_back(data_line);
}
void on_commit(protocol::protocol_t& protocol, protocol::message_t message) {
auto session_key = protocol::session_key_from(message);
auto& session = sessions.at(session_key);
protocol.submit_message(session_key, session.message.header, session.message.body);
}
int main(int argc, char** argv) {
@ -40,7 +65,7 @@ int main(int argc, char** argv) {
protocol::handler_map_t handlers;
handlers.emplace("config|ready", on_ready);
handlers.emplace("filter|*|*|smtp-in|data", on_data);
handlers.emplace("filter|*|*|smtp-in|data-line", on_dataline);
handlers.emplace("filter|*|*|smtp-in|data-line", on_data_line);
handlers.emplace("filter|*|*|smtp-in|commit", on_commit);
protocol.set_handlers(handlers);

@ -45,6 +45,29 @@ namespace protocol {
}
}
void protocol_t::send_data_line(const session_key_t& session_key, const string& data_line) {
send({"filter-dataline", session_key.session, session_key.token, session_key.token, data_line});
}
void protocol_t::submit_message(const session_key_t& session_key, const vector<string>& header, const vector<string>& body) {
for(auto& data_line : header) {
send_data_line(session_key, data_line);
}
send_data_line(session_key, "");
for(auto& data_line : body) {
if(data_line == ".") {
send_data_line(session_key, "..");
continue;
}
send_data_line(session_key, data_line);
}
send_data_line(session_key, ".");
}
ios_protocol_t::ios_protocol_t(std::istream& in, std::ostream& out) :
in(in), out(out) { }
@ -60,18 +83,18 @@ namespace protocol {
}
}
bool operator==(const session_t& l, const session_t& r) {
bool operator==(const session_key_t& l, const session_key_t& r) {
return l.session == r.session && l.token == r.token;
}
session_t session_from(token::list_t tokens) {
session_key_t session_key_from(const token::list_t& tokens) {
auto session_id = tokens.at(5);
auto session_token = tokens.at(6);
return {session_id, session_token};
}
session_t session_from(message_t message) {
session_key_t session_key_from(const message_t& message) {
auto tokens = token::decompose(message.input);
return session_from(tokens);
return session_key_from(tokens);
}
}

Loading…
Cancel
Save