#include "queue_sender.hpp" #include "sender.hpp" namespace smtp::message { QueueSender::QueueSender( manage::Settings const& settings_storage, manage::Mail& mail_to, thread::Queue& message_queue, std::shared_ptr connection ) : mSettingsStorage( settings_storage ) , mMailTo( mail_to ) , mMessageQueue( message_queue ) , mConnection( connection ) { } void QueueSender::SendMessages() { Sender message_sender; while (1) { auto message = mMessageQueue.WaitAndPop(); auto mail_from = GetMailfrom(); auto mails_to = mMailTo.GetMailToSend(); message_sender.Send( mSettingsStorage, mail_from, mails_to, message->subject, message->text ); } } std::string QueueSender::GetMailfrom() const { return mSettingsStorage.IsNeedAuth() ? mSettingsStorage.GetUserName() : GetDefaultMailfrom(); } std::string QueueSender::GetDefaultMailfrom() const { static const std::string SERVICE_NAME = "org.freedesktop.hostname1"; static const std::string PATH_NAME = "/org/freedesktop/hostname1"; static const std::string INTERFACE_NAME = "org.freedesktop.hostname1"; static const std::string FUNCTION_NAME = "Hostname"; static const std::string PROPERTIES_INTERFACE = "org.freedesktop.DBus.Properties"; static const std::string PROPERTIES_GETTER_NAME = "Get"; static const std::string DEFAULT_HOST = "cp2-5422"; std::string result = DEFAULT_HOST; mConnection->async_method_call( [&result]( const boost::system::error_code& error_code, std::variant& host_name) mutable{ if (error_code.value() == EBADR || error_code == boost::system::errc::host_unreachable) { return; } if (error_code) { return; } auto host_as_string = std::get_if(&host_name); if(host_as_string) { result = *host_as_string; } }, SERVICE_NAME, PATH_NAME, PROPERTIES_INTERFACE, PROPERTIES_GETTER_NAME, INTERFACE_NAME, FUNCTION_NAME); return result; } }