You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.1 KiB
77 lines
2.1 KiB
#include <util.hpp>
|
|
|
|
#include <protocol/protocol.hpp>
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
using namespace util;
|
|
|
|
//config|smtpd-version|6.6.1
|
|
//config|smtp-session-timeout|300
|
|
//config|subsystem|smtp-in
|
|
|
|
const bool debug = true;
|
|
|
|
namespace protocol {
|
|
void protocol_t::set_handlers(const handler_map_t handlers) {
|
|
size_t length = 0;
|
|
for(auto&& [pattern, handler] : handlers) {
|
|
for(auto&& [index, token] : pattern) {
|
|
if(index + 1 > length) {
|
|
length = index + 1;
|
|
}
|
|
}
|
|
}
|
|
debug && cerr << "Maximum handler pattern length is " << length << endl;
|
|
|
|
this->handlers = handlers;
|
|
max_pattern_length = length;
|
|
}
|
|
|
|
void protocol_t::send(const token::list_t& tokens) {
|
|
this->emit(token::compose(tokens));
|
|
}
|
|
|
|
void protocol_t::absorb(const std::string& input) {
|
|
auto tokens = token::decompose(input, max_pattern_length);
|
|
auto pattern = token::pattern_t(tokens, false);
|
|
debug && cout << "Received pattern " << pattern << endl;
|
|
|
|
auto element = handlers.find(pattern);
|
|
if(element != handlers.end()) {
|
|
element->second(*this, {input, element->first});
|
|
}
|
|
}
|
|
|
|
ios_protocol_t::ios_protocol_t(std::istream& in, std::ostream& out) :
|
|
in(in), out(out) { }
|
|
|
|
void ios_protocol_t::emit(const std::string& output) {
|
|
out << output << endl;
|
|
}
|
|
|
|
void ios_protocol_t::run() {
|
|
while(!in.eof()) {
|
|
string line;
|
|
std::getline(in, line);
|
|
absorb(line);
|
|
}
|
|
}
|
|
|
|
bool operator==(const session_t& l, const session_t& r) {
|
|
return l.session == r.session && l.token == r.token;
|
|
}
|
|
|
|
session_t session_from(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) {
|
|
auto tokens = token::decompose(message.input);
|
|
return session_from(tokens);
|
|
}
|
|
}
|
|
|