Protocol.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Protocol.h"
  2. #include "MailServer/Message.h"
  3. #include <Logging.h>
  4. #include <StringAlgorithm.h>
  5. #include <sstream>
  6. namespace Protocol {
  7. Protocol::Protocol(const Util::Context& context) :
  8. m_internalState(InternalState::None),
  9. m_context(context)
  10. {
  11. }
  12. Protocol::Result Protocol::OpenConnection(const std::string& address)
  13. {
  14. m_address = address;
  15. m_internalState = InternalState::Connect;
  16. std::stringstream ss;
  17. ss << "Connection from " << address << std::endl;
  18. Logging::Log(Logging::Severity::Info, ss.str());
  19. return Answer(Protocol::State::Connected, "220 AlarmServer");
  20. }
  21. Protocol::Result Protocol::ProcessMessage(const std::string& message)
  22. {
  23. if (m_internalState == InternalState::Data)
  24. {
  25. AppendMessage(message);
  26. if (StringAlgorithm::iends_with(message, ".\r\n"))
  27. {
  28. MailServer::Message msg(m_address, m_context, m_message);
  29. m_internalState = InternalState::Helo;
  30. return Answer(Protocol::State::Connected, "250 Ok");
  31. }
  32. return Answer(Protocol::State::Connected, "");
  33. }
  34. if (m_internalState == InternalState::Auth_login_user)
  35. {
  36. m_internalState = InternalState::Auth_login_pass;
  37. return Answer(Protocol::State::Connected, "334 UGFzc3dvcmQ6");
  38. }
  39. if (m_internalState == InternalState::Auth_login_pass)
  40. {
  41. m_internalState = InternalState::Helo;
  42. return Answer(Protocol::State::Connected, "235 2.7.0 Authentication successful");
  43. }
  44. if (StringAlgorithm::iequals(message, "quit\r\n"))
  45. {
  46. m_internalState = InternalState::Disconnect;
  47. return Answer(Protocol::State::Disconnected, "221 Bye");
  48. }
  49. if (StringAlgorithm::istarts_with(message, "helo"))
  50. {
  51. m_internalState = InternalState::Helo;
  52. return Answer(Protocol::State::Connected, "250-AlarmServer\r\n250 AUTH LOGIN");
  53. }
  54. if (StringAlgorithm::istarts_with(message, "ehlo"))
  55. {
  56. m_internalState = InternalState::Helo;
  57. return Answer(Protocol::State::Connected, "250-AlarmServer\r\n250 AUTH LOGIN");
  58. }
  59. if (StringAlgorithm::istarts_with(message, "auth login"))
  60. {
  61. if (m_internalState != InternalState::Helo)
  62. return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
  63. m_internalState = InternalState::Auth_login_user;
  64. return Answer(Protocol::State::Connected, "334 VXNlcm5hbWU6");
  65. }
  66. if (StringAlgorithm::istarts_with(message, "mail from:"))
  67. {
  68. if (m_internalState != InternalState::Helo)
  69. return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
  70. m_internalState = InternalState::Mail_from;
  71. return Answer(Protocol::State::Connected, "250 Ok");
  72. }
  73. if (StringAlgorithm::istarts_with(message, "rcpt to:"))
  74. {
  75. if (m_internalState != InternalState::Mail_from)
  76. return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
  77. m_internalState = InternalState::Rcpt_to;
  78. return Answer(Protocol::State::Connected, "250 Ok");
  79. }
  80. if (StringAlgorithm::iequals(message, "data\r\n"))
  81. {
  82. if (m_internalState != InternalState::Rcpt_to)
  83. return Answer(Protocol::State::Connected, "503 Bad sequence of commands");
  84. m_internalState = InternalState::Data;
  85. return Answer(Protocol::State::Connected, "354 Go ahead");
  86. }
  87. return Answer(Protocol::State::Connected, "502 Error");
  88. }
  89. Protocol::Result Protocol::Answer(Protocol::State::type state, std::string answer) const
  90. {
  91. return Result(state, answer);
  92. }
  93. void Protocol::AppendMessage(const std::string& message)
  94. {
  95. m_message.append(message);
  96. m_message.append("\n");
  97. }
  98. } // namespace Protocol