diff options
Diffstat (limited to 'src/message/sender.cpp')
-rw-r--r-- | src/message/sender.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/message/sender.cpp b/src/message/sender.cpp new file mode 100644 index 0000000..cabcb8f --- /dev/null +++ b/src/message/sender.cpp @@ -0,0 +1,127 @@ +#include <string.h> + +#include "sender.hpp" +#include "builder/date.hpp" +#include "builder/mail_to.hpp" +#include "builder/cc.hpp" +#include "builder/subject.hpp" +#include "builder/text.hpp" +#include "builder/from.hpp" +#include <iostream> + +namespace smtp::message +{ + static std::string mText = ""; + + // + // Constructors/Destructors + // + Sender::Sender( service::Settings const& settings_storage ) + : mSettingsStorage( settings_storage ) + { + + } + + // + //Public methods + // + + bool Sender::Send( std::string const& mail_from, std::string const& mail_to, std::list<std::string> const& cc, + std::string const& subject, std::string const& text ) + { + CURLcode result = CURLE_OK; + curl_slist *recipients = NULL; + WriteThis upload_ctx{}; + auto curl = curl_easy_init(); + //TODO сделать инициализацию через регистратор инициализаторов + if( !InitCurl( curl, upload_ctx, mail_from ) ) + { + return false; + } + FillRecipients( curl, mail_to, cc, recipients ); + UpdateMailText( mail_from, mail_to, cc, subject, text); + + result = curl_easy_perform(curl); + + if ( result != CURLE_OK ) + { + //TODO LOG + fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror( result )); + return false; + } + //TODO сделать деинициализацию через регистратор деинициализаторов + curl_slist_free_all( recipients ); + curl_easy_cleanup( curl ); + return true; + } + + // + //Private methods + // + bool smtp::message::Sender::InitCurl( CURL* curl, WriteThis const& upload_ctx, std::string const& mail_from ) + { + if( !curl ) + { + //TODO LOG + return false; + } + curl_easy_setopt(curl, CURLOPT_USERNAME, mSettingsStorage.GetUserName().c_str()); + curl_easy_setopt(curl, CURLOPT_PASSWORD, mSettingsStorage.GetPassword().c_str()); + curl_easy_setopt(curl, CURLOPT_URL, GetHostPortData().c_str()); + + curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, mail_from.c_str()); + + curl_easy_setopt(curl, CURLOPT_READFUNCTION, ReadCallBack); + curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + return true; + } + + void Sender::UpdateMailText( std::string const& mail_from, std::string const& mail_to, std::list<std::string> const& cc, + std::string const& subject, std::string const& text ) const + { + auto text_decorator = std::make_shared<builder::Text>( text ); + text_decorator->Apply( std::make_shared<builder::Subject>( subject ) ) + .Apply( std::make_shared<builder::Cc>( cc ) ) + .Apply( std::make_shared<builder::From>( mail_from ) ) + .Apply( std::make_shared<builder::MailTo>( mail_to ) ) + .Apply( std::make_shared<builder::Date>() ); + mText = text_decorator->Get(); + std::cout << mText << std::endl; + } + + void Sender::FillRecipients( CURL* curl, std::string const& mail_to, std::list<std::string> const& cc, curl_slist* recipients ) + { + recipients = curl_slist_append( recipients, mail_to.c_str() ); + + for( const auto& recipient : cc ) + { + recipients = curl_slist_append( recipients, recipient.c_str() ); + } + + curl_easy_setopt( curl, CURLOPT_MAIL_RCPT, recipients ); + } + + std::string Sender::GetHostPortData() const + { + auto result = "smtp://" + mSettingsStorage.GetHost(); + if( !mSettingsStorage.GetPort().empty() ) + { + result += ":" + mSettingsStorage.GetPort(); + } + return result; + } + //TODO Надо убрать этот ужас. Без статики!!! + size_t Sender::ReadCallBack( void *ptr, size_t size, size_t nmemb, void *userp ) + { + struct WriteThis *pooh = reinterpret_cast<WriteThis*>( userp ); + if( size * nmemb < 1 || pooh->counter++ > 0 ) + { + return 0; + } + memcpy( ptr, mText.c_str(), mText.size() ); + return mText.size(); + } +} |