summaryrefslogtreecommitdiff
path: root/src/smtp_service.cpp
blob: 6235e4c8cab84d0aeb123622339a59f0c633f5f0 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "smtp_service.hpp"
#include "message_sender.hpp"

namespace smtp
{
    static constexpr char HOST_PROPERTY[] = "Host";
    static constexpr char USER_PROPERTY[] = "User";
    static constexpr char PORT_PROPERTY[] = "Port";

    //
    // Constructors
    //

    SmtpService::SmtpService( ConnectionPtr connection )
    {
        FillStorageByDefault();
        CreateService( connection );
        CreateInterface( connection );
    }

    //
    // Private methods
    //

    void SmtpService::FillStorageByDefault()
    {
        mStorage.host = "";
        mStorage.username = "";
        mStorage.password = "";
        mStorage.port = "";
    }

    void SmtpService::CreateService( ConnectionPtr connection )
    {
        static constexpr char SMTP_BUS_NAME[] = "xyz.openbmc_project.SMTP";

        connection->request_name( SMTP_BUS_NAME );
    }

    void SmtpService::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<sdbusplus::asio::object_server>( connection );
        mInterface = mObjectServer->add_interface( SMTP_OBJECT_NAME, SMTP_INTERFACE_NAME );

        AddProperties();
        AddMethods();

        mInterface->initialize();
    }

    void SmtpService::AddProperties()
    {
        mInterface->register_property( HOST_PROPERTY, mStorage.host, sdbusplus::asio::PropertyPermission::readOnly );
        mInterface->register_property( USER_PROPERTY, mStorage.username, sdbusplus::asio::PropertyPermission::readOnly );
        mInterface->register_property( PORT_PROPERTY, mStorage.port, sdbusplus::asio::PropertyPermission::readOnly );
    }

    void SmtpService::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<std::string> const& cc,
                                                                           std::string const& theme,
                                                                           std::string const& text )
                                                                         { return MessageSender{ mStorage }.Send( mail_from, mail_to, cc, theme, text );});


        mInterface->register_method(SMTP_CHANGE_PARAMETERS_METHOD_NAME, [this]( std::string const& user,
                                                                           std::string const& password,
                                                                           std::string const& host,
                                                                           std::string const& port )
                                                                         { return RefreshSettings(user, password, host, port);});
    }

    bool SmtpService::RefreshSettings(std::string const& user,
                                      std::string const& password,
                                      std::string const& host,
                                      std::string const& port)
    {
        mStorage.username = user;
        mStorage.password = password;
        mStorage.host = host;
        mStorage.port = port;

        mInterface->set_property(HOST_PROPERTY, host);
        mInterface->set_property(USER_PROPERTY, user);
        mInterface->set_property(PORT_PROPERTY, port);
        return true;
    }
}