summaryrefslogtreecommitdiff
path: root/src/service/smtp.cpp
blob: cf3dc4597ecc5eb62a1cb27fa86aa6116b885928 (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
97
98
99
100
101
102
#include "smtp.hpp"
#include "message/sender.hpp"
#include <iostream>
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, manage::Settings const& settings, manage::Mail const& mail_to )
        :mSettingsStorage(settings), mMailTo(mail_to)
    {
//        std::string from{"claiff@mail.ru"};
//        std::string to{"claiff1990@gmail.com"};
//        std::list<std::string> 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, mMailTo }.Send( "claiff@mail.ru", "subject", "text" );
        //settings.SetSettings(manage::SettingsFields{false,false,"claiff@mail.ru","nZZbXq7FbwWAqpPpy3YL","smtp.mail.ru",""});
        manage::MailsSet add_mail{"gfdgdf@fd.ru"};
        //mMailTo.AddMailsToSend( add_mail );
        mMailTo.DeleteMailToSend({"gfdgdf@fd.ru", "rwer", "fedsfsd"});

//        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<sdbusplus::asio::object_server>( 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& theme,
                                                                           std::string const& text )
                                                                         { return message::Sender{ mSettingsStorage, mMailTo }.Send( mail_from, 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.SetSettings({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;
    }
}