From cdb7ae4c729b6a6c51b57eeefd21574952a4b767 Mon Sep 17 00:00:00 2001 From: eportnov Date: Mon, 12 Sep 2022 16:46:44 +0300 Subject: refactoring --- src/service/settings.cpp | 54 ++++++++++++++++++++++++++ src/service/settings.hpp | 36 ++++++++++++++++++ src/service/smtp.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ src/service/smtp.hpp | 35 +++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 src/service/settings.cpp create mode 100644 src/service/settings.hpp create mode 100644 src/service/smtp.cpp create mode 100644 src/service/smtp.hpp (limited to 'src/service') diff --git a/src/service/settings.cpp b/src/service/settings.cpp new file mode 100644 index 0000000..075aafa --- /dev/null +++ b/src/service/settings.cpp @@ -0,0 +1,54 @@ +#include "settings.hpp" + +namespace smtp::service +{ + + Settings::Settings() + : mSettingsFields{} + { + + } + + Settings::Settings( SettingsFields data ) + : mSettingsFields(data) + { + + } + + bool Settings::CheckAndSetSettings(SettingsFields data) + { + mSettingsFields = data; + return true; + } + + bool Settings::IsNeedAuth() const noexcept + { + return mSettingsFields.is_need_auth; + } + + bool Settings::IsNeedSsl() const noexcept + { + return mSettingsFields.is_need_ssl; + } + + std::string Settings::GetUserName() const + { + return mSettingsFields.username; + } + + std::string Settings::GetPassword() const + { + return mSettingsFields.password; + } + + std::string Settings::GetHost() const + { + return mSettingsFields.host; + } + + std::string Settings::GetPort() const + { + return mSettingsFields.port; + } + +} diff --git a/src/service/settings.hpp b/src/service/settings.hpp new file mode 100644 index 0000000..b6319f6 --- /dev/null +++ b/src/service/settings.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +namespace smtp::service +{ + struct SettingsFields + { + bool is_need_auth; + bool is_need_ssl; + std::string username; + std::string password; + std::string host; + std::string port; + }; + + class Settings + { + public: + Settings(); + explicit Settings( SettingsFields data ); + ~Settings() = default; + + bool CheckAndSetSettings( SettingsFields data ); + + bool IsNeedAuth() const noexcept; + bool IsNeedSsl() const noexcept; + + std::string GetUserName() const; + std::string GetPassword() const; + std::string GetHost() const; + std::string GetPort() const; + private: + SettingsFields mSettingsFields; + }; +} diff --git a/src/service/smtp.cpp b/src/service/smtp.cpp new file mode 100644 index 0000000..818a4de --- /dev/null +++ b/src/service/smtp.cpp @@ -0,0 +1,99 @@ +#include "smtp.hpp" +#include "message/sender.hpp" + +namespace smtp::service +{ + static constexpr char HOST_PROPERTY[] = "Host"; + static constexpr char USER_PROPERTY[] = "User"; + static constexpr char PORT_PROPERTY[] = "Port"; + + // + // Constructors + // + + Smtp::Smtp( ConnectionPtr connection ) + { + std::string from{"claiff@mail.ru"}; + std::string to{"claiff1990@gmail.com"}; + std::list cc{"claiff@mail.ru"}; + std::string subject{"subject"}; + std::string text{"text"}; + + mSettingsStorage.CheckAndSetSettings({true, true, "claiff@mail.ru","nZZbXq7FbwWAqpPpy3YL", "smtp.mail.ru", "" }); + message::Sender{ mSettingsStorage }.Send( "claiff@mail.ru", "claiff1990@gmail.com", {"claiff@mail.ru"}, "subject", "text" ); + +// CreateService( connection ); +// CreateInterface( connection ); + } + + // + // Private methods + // + + void Smtp::CreateService( ConnectionPtr connection ) + { + static constexpr char SMTP_BUS_NAME[] = "xyz.openbmc_project.SMTP"; + + connection->request_name( SMTP_BUS_NAME ); + } + + void Smtp::CreateInterface( ConnectionPtr connection ) + { + static constexpr char SMTP_OBJECT_NAME[] = "/xyz/openbmc_project/SMTP"; + static constexpr char SMTP_INTERFACE_NAME[] = "xyz.openbmc_project.SMTP"; + + mObjectServer = std::make_shared( connection ); + mInterface = mObjectServer->add_interface( SMTP_OBJECT_NAME, SMTP_INTERFACE_NAME ); + + AddProperties(); + AddMethods(); + + mInterface->initialize(); + } + + void Smtp::AddProperties() + { + mInterface->register_property( HOST_PROPERTY, mSettingsStorage.GetHost(), sdbusplus::asio::PropertyPermission::readOnly ); + mInterface->register_property( USER_PROPERTY, mSettingsStorage.GetUserName(), sdbusplus::asio::PropertyPermission::readOnly ); + mInterface->register_property( PORT_PROPERTY, mSettingsStorage.GetPort(), sdbusplus::asio::PropertyPermission::readOnly ); + } + + void Smtp::AddMethods() + { + static constexpr char SMTP_SEND_MESSAGE_METHOD_NAME[] = "SendMail"; + static constexpr char SMTP_CHANGE_PARAMETERS_METHOD_NAME[] = "ChangeParameters"; + + mInterface->register_method(SMTP_SEND_MESSAGE_METHOD_NAME, [this]( std::string const& mail_from, + std::string const& mail_to, + std::list const& cc, + std::string const& theme, + std::string const& text ) + { return message::Sender{ mSettingsStorage }.Send( mail_from, mail_to, cc, theme, text );}); + + + mInterface->register_method(SMTP_CHANGE_PARAMETERS_METHOD_NAME, [this]( bool is_need_auth, + bool is_need_ssl, + std::string const& user, + std::string const& password, + std::string const& host, + std::string const& port ) + { return RefreshSettings( is_need_auth, is_need_ssl, user, password, host, port);}); + } + + bool Smtp::RefreshSettings( bool is_need_auth, bool is_need_ssl, + std::string const& user, std::string const& password, + std::string const& host, std::string const& port ) + { + auto is_settings_norm = mSettingsStorage.CheckAndSetSettings({is_need_auth, is_need_ssl, + user, password, host, port}); + if(!is_settings_norm) + { + return false; + } + + mInterface->set_property(HOST_PROPERTY, host); + mInterface->set_property(USER_PROPERTY, user); + mInterface->set_property(PORT_PROPERTY, port); + return true; + } +} diff --git a/src/service/smtp.hpp b/src/service/smtp.hpp new file mode 100644 index 0000000..c311645 --- /dev/null +++ b/src/service/smtp.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include + +#include +#include + +#include "settings.hpp" + +namespace smtp::service +{ + using ConnectionPtr = std::shared_ptr; + using InterfacePtr = std::shared_ptr; + using ObjectServerPtr = std::shared_ptr; + + class Smtp + { + public: + Smtp( ConnectionPtr connection ); + ~Smtp() = default; + private: + void FillStorageByDefault(); + void CreateService( ConnectionPtr bus ); + void CreateInterface( ConnectionPtr connection ); + void AddProperties(); + void AddMethods(); + bool RefreshSettings( bool is_need_auth, bool is_need_ssl, + std::string const& user, std::string const& password, + std::string const& host, std::string const& port ); + + InterfacePtr mInterface; + ObjectServerPtr mObjectServer; + Settings mSettingsStorage; + }; +} -- cgit v1.2.3