114 lines
3.3 KiB
C++
114 lines
3.3 KiB
C++
#include "Protocol.h"
|
|
#include "MailServer/Message.h"
|
|
#include <Logging.h>
|
|
#include <StringAlgorithm.h>
|
|
#include <sstream>
|
|
|
|
|
|
namespace Protocol {
|
|
|
|
Protocol::Protocol(const Util::Context& context) :
|
|
m_internalState(InternalState::None),
|
|
m_context(context)
|
|
{
|
|
}
|
|
|
|
Protocol::Result Protocol::OpenConnection(const std::string& address)
|
|
{
|
|
m_address = address;
|
|
m_internalState = InternalState::Connect;
|
|
|
|
std::stringstream ss;
|
|
ss << "Connection from " << address << std::endl;
|
|
Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
|
return Answer(Protocol::State::Connected, "220 AlarmServer");
|
|
}
|
|
|
|
Protocol::Result Protocol::ProcessMessage(const std::string& message)
|
|
{
|
|
if (m_internalState == InternalState::Data)
|
|
{
|
|
AppendMessage(message);
|
|
|
|
if (StringAlgorithm::iends_with(message, ".\r\n"))
|
|
{
|
|
MailServer::Message msg(m_address, m_context, m_message);
|
|
m_internalState = InternalState::Helo;
|
|
return Answer(Protocol::State::Connected, "250 Ok");
|
|
}
|
|
|
|
return Answer(Protocol::State::Connected, "");
|
|
}
|
|
|
|
if (m_internalState == InternalState::Auth_login_user)
|
|
{
|
|
m_internalState = InternalState::Auth_login_pass;
|
|
return Answer(Protocol::State::Connected, "334 UGFzc3dvcmQ6");
|
|
}
|
|
if (m_internalState == InternalState::Auth_login_pass)
|
|
{
|
|
m_internalState = InternalState::Helo;
|
|
return Answer(Protocol::State::Connected, "235 2.7.0 Authentication successful");
|
|
}
|
|
|
|
if (StringAlgorithm::iequals(message, "quit\r\n"))
|
|
{
|
|
m_internalState = InternalState::Disconnect;
|
|
return Answer(Protocol::State::Disconnected, "221 Bye");
|
|
}
|
|
if (StringAlgorithm::istarts_with(message, "helo"))
|
|
{
|
|
m_internalState = InternalState::Helo;
|
|
return Answer(Protocol::State::Connected, "250-AlarmServer\r\n250 AUTH LOGIN");
|
|
}
|
|
if (StringAlgorithm::istarts_with(message, "ehlo"))
|
|
{
|
|
m_internalState = InternalState::Helo;
|
|
return Answer(Protocol::State::Connected, "250-AlarmServer\r\n250 AUTH LOGIN");
|
|
}
|
|
if (StringAlgorithm::istarts_with(message, "auth login"))
|
|
{
|
|
if (m_internalState != InternalState::Helo)
|
|
return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
|
|
m_internalState = InternalState::Auth_login_user;
|
|
return Answer(Protocol::State::Connected, "334 VXNlcm5hbWU6");
|
|
}
|
|
if (StringAlgorithm::istarts_with(message, "mail from:"))
|
|
{
|
|
if (m_internalState != InternalState::Helo)
|
|
return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
|
|
m_internalState = InternalState::Mail_from;
|
|
return Answer(Protocol::State::Connected, "250 Ok");
|
|
}
|
|
if (StringAlgorithm::istarts_with(message, "rcpt to:"))
|
|
{
|
|
if (m_internalState != InternalState::Mail_from)
|
|
return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
|
|
m_internalState = InternalState::Rcpt_to;
|
|
return Answer(Protocol::State::Connected, "250 Ok");
|
|
}
|
|
if (StringAlgorithm::iequals(message, "data\r\n"))
|
|
{
|
|
if (m_internalState != InternalState::Rcpt_to)
|
|
return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
|
|
m_internalState = InternalState::Data;
|
|
return Answer(Protocol::State::Connected, "354 Go ahead");
|
|
}
|
|
|
|
return Answer(Protocol::State::Connected, "502 Error");
|
|
}
|
|
|
|
Protocol::Result Protocol::Answer(Protocol::State::type state, std::string answer) const
|
|
{
|
|
return Result(state, answer);
|
|
}
|
|
|
|
void Protocol::AppendMessage(const std::string& message)
|
|
{
|
|
m_message.append(message);
|
|
m_message.append("\n");
|
|
}
|
|
|
|
} // namespace Protocol
|