Move to new Build System
This commit is contained in:
Symlink
+1
@@ -0,0 +1 @@
|
||||
../Makefile
|
||||
@@ -0,0 +1,113 @@
|
||||
#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
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef PROTOCOL_H
|
||||
#define PROTOCOL_H
|
||||
|
||||
#include "Util/Util.h"
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace Protocol {
|
||||
|
||||
class Protocol
|
||||
{
|
||||
public:
|
||||
class State
|
||||
{
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
Connected,
|
||||
Disconnected
|
||||
};
|
||||
};
|
||||
|
||||
typedef std::pair<State::type, std::string> Result;
|
||||
|
||||
public:
|
||||
explicit Protocol(const Util::Context& context);
|
||||
|
||||
Result OpenConnection(const std::string& address);
|
||||
Result ProcessMessage(const std::string& message);
|
||||
|
||||
private:
|
||||
class InternalState
|
||||
{
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
None,
|
||||
Connect,
|
||||
Helo,
|
||||
Auth_login_user,
|
||||
Auth_login_pass,
|
||||
Mail_from,
|
||||
Rcpt_to,
|
||||
Data,
|
||||
Mime_boundary,
|
||||
Image_data,
|
||||
Disconnect
|
||||
};
|
||||
};
|
||||
|
||||
private:
|
||||
Result Answer(State::type state, std::string answer) const;
|
||||
void AppendMessage(const std::string& message);
|
||||
|
||||
private:
|
||||
InternalState::type m_internalState;
|
||||
std::string m_address;
|
||||
std::string m_message;
|
||||
|
||||
const Util::Context& m_context;
|
||||
};
|
||||
|
||||
} // namespace Protocol
|
||||
|
||||
#endif // PROTOCOL_H
|
||||
Reference in New Issue
Block a user