summaryrefslogtreecommitdiff
path: root/src/message/queue_sender.cpp
blob: 9dbc8eb78886d7082d07d1fe5e468474af618016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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<sdbusplus::asio::connection> 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<std::monostate, std::string>& 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<std::string>(&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;
    }

}