summaryrefslogtreecommitdiff
path: root/src/message
diff options
context:
space:
mode:
Diffstat (limited to 'src/message')
-rw-r--r--src/message/builder/cc.cpp31
-rw-r--r--src/message/builder/cc.hpp21
-rw-r--r--src/message/builder/date.cpp16
-rw-r--r--src/message/builder/date.hpp17
-rw-r--r--src/message/builder/from.cpp22
-rw-r--r--src/message/builder/from.hpp19
-rw-r--r--src/message/builder/mail_to.cpp23
-rw-r--r--src/message/builder/mail_to.hpp21
-rw-r--r--src/message/builder/subject.cpp22
-rw-r--r--src/message/builder/subject.hpp19
-rw-r--r--src/message/builder/text.cpp24
-rw-r--r--src/message/builder/text.hpp19
-rw-r--r--src/message/builder/types/idecorator.hpp82
-rw-r--r--src/message/builder/types/imessage_builder.hpp14
-rw-r--r--src/message/sender.cpp127
-rw-r--r--src/message/sender.hpp38
16 files changed, 515 insertions, 0 deletions
diff --git a/src/message/builder/cc.cpp b/src/message/builder/cc.cpp
new file mode 100644
index 0000000..55d70ff
--- /dev/null
+++ b/src/message/builder/cc.cpp
@@ -0,0 +1,31 @@
+#include "cc.hpp"
+
+namespace smtp::message::builder
+{
+ Cc::Cc( std::list<std::string> const& mail_to )
+ : mMailTo( mail_to )
+ {
+
+ }
+
+ std::string Cc::Get() const
+ {
+ std::string result;
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+
+ if( mMailTo.empty() )
+ {
+ return result;
+ }
+
+ for(const auto& cc : mMailTo)
+ {
+ result += "Cc: " + cc + "\r\n";
+ }
+
+ return result;
+ }
+}
diff --git a/src/message/builder/cc.hpp b/src/message/builder/cc.hpp
new file mode 100644
index 0000000..340c7cd
--- /dev/null
+++ b/src/message/builder/cc.hpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#include <list>
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class Cc : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ explicit Cc( std::list<std::string> const& mail_to );
+ ~Cc() override = default;
+
+ std::string Get() const override;
+ private:
+ std::list<std::string> const& mMailTo;
+ };
+}
+
diff --git a/src/message/builder/date.cpp b/src/message/builder/date.cpp
new file mode 100644
index 0000000..3268935
--- /dev/null
+++ b/src/message/builder/date.cpp
@@ -0,0 +1,16 @@
+#include "date.hpp"
+
+namespace smtp::message::builder
+{
+ std::string Date::Get() const
+ {
+ std::string result;
+
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+ result += "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n";
+ return result;
+ }
+}
diff --git a/src/message/builder/date.hpp b/src/message/builder/date.hpp
new file mode 100644
index 0000000..31908ac
--- /dev/null
+++ b/src/message/builder/date.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class Date : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ Date() = default;
+ ~Date() override = default;
+
+ std::string Get() const override;
+ };
+}
+
diff --git a/src/message/builder/from.cpp b/src/message/builder/from.cpp
new file mode 100644
index 0000000..b19515a
--- /dev/null
+++ b/src/message/builder/from.cpp
@@ -0,0 +1,22 @@
+#include "from.hpp"
+
+namespace smtp::message::builder
+{
+
+ From::From( std::string const& mail_to )
+ :mMailTo( mail_to )
+ {
+
+ }
+
+ std::string From::Get() const
+ {
+ std::string result;
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+ result += "From: " + mMailTo + "\r\n";
+ return result;
+ }
+}
diff --git a/src/message/builder/from.hpp b/src/message/builder/from.hpp
new file mode 100644
index 0000000..d1f0b21
--- /dev/null
+++ b/src/message/builder/from.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class From : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ explicit From( std::string const& mail_to );
+ ~From() override = default;
+
+ std::string Get() const override;
+ private:
+ std::string const& mMailTo;
+ };
+}
+
diff --git a/src/message/builder/mail_to.cpp b/src/message/builder/mail_to.cpp
new file mode 100644
index 0000000..dab1b7f
--- /dev/null
+++ b/src/message/builder/mail_to.cpp
@@ -0,0 +1,23 @@
+#include "mail_to.hpp"
+
+namespace smtp::message::builder
+{
+ MailTo::MailTo( std::string const& mail_to)
+ : mMailTo( mail_to )
+ {
+
+ }
+
+ std::string MailTo::Get() const
+ {
+ std::string result;
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+
+ result += "To: " + mMailTo +"\r\n";
+
+ return result;
+ }
+}
diff --git a/src/message/builder/mail_to.hpp b/src/message/builder/mail_to.hpp
new file mode 100644
index 0000000..5d60d97
--- /dev/null
+++ b/src/message/builder/mail_to.hpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#include <list>
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class MailTo : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ explicit MailTo( std::string const& mail_to );
+ ~MailTo() override = default;
+
+ std::string Get() const override;
+ private:
+ std::string const& mMailTo;
+ };
+}
+
diff --git a/src/message/builder/subject.cpp b/src/message/builder/subject.cpp
new file mode 100644
index 0000000..166bca4
--- /dev/null
+++ b/src/message/builder/subject.cpp
@@ -0,0 +1,22 @@
+#include "subject.hpp"
+
+namespace smtp::message::builder
+{
+ Subject::Subject( std::string const& subject )
+ : mSubject(subject)
+ {
+
+ }
+
+ std::string Subject::Get() const
+ {
+ std::string result;
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+
+ result += "Subject: " + mSubject + "\r\n";
+ return result;
+ }
+}
diff --git a/src/message/builder/subject.hpp b/src/message/builder/subject.hpp
new file mode 100644
index 0000000..8153bc8
--- /dev/null
+++ b/src/message/builder/subject.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class Subject : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ explicit Subject( std::string const& subject );
+ ~Subject() override = default;
+
+ std::string Get() const override;
+ private:
+ std::string const& mSubject;
+ };
+}
+
diff --git a/src/message/builder/text.cpp b/src/message/builder/text.cpp
new file mode 100644
index 0000000..c478b7d
--- /dev/null
+++ b/src/message/builder/text.cpp
@@ -0,0 +1,24 @@
+#include "text.hpp"
+
+namespace smtp::message::builder
+{
+ Text::Text( std::string const& text)
+ : mText( text )
+ {
+
+ }
+
+ std::string Text::Get() const
+ {
+ std::string result;
+ if( mBase )
+ {
+ result = mBase->Get();
+ }
+
+ result += "\r\n";
+ result += mText + "\r\n";
+
+ return result;
+ }
+}
diff --git a/src/message/builder/text.hpp b/src/message/builder/text.hpp
new file mode 100644
index 0000000..9715656
--- /dev/null
+++ b/src/message/builder/text.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "types/idecorator.hpp"
+#include "types/imessage_builder.hpp"
+
+namespace smtp::message::builder
+{
+ class Text : public types::IDecorator<types::IMessageBuilder>
+ {
+ public:
+ explicit Text( std::string const& text );
+ ~Text() override = default;
+
+ std::string Get() const override;
+ private:
+ std::string const& mText;
+ };
+}
+
diff --git a/src/message/builder/types/idecorator.hpp b/src/message/builder/types/idecorator.hpp
new file mode 100644
index 0000000..19284ac
--- /dev/null
+++ b/src/message/builder/types/idecorator.hpp
@@ -0,0 +1,82 @@
+#pragma once
+
+#include <memory>
+
+namespace smtp::message::builder::types
+{
+
+/**
+ * @brief Базовый интерфейс описания декоратора
+ * @details См. https://refactoring.guru/ru/design-patterns/decorator
+ * @tparam T Тип декорируемого класса, T должен обладать возможностью наследования (в том числе виртуальный деструктор)
+ */
+ template < typename T, typename TPointerType = std::shared_ptr<T> > class IDecorator : public T
+ {
+ public:
+ using PointerType = TPointerType;
+ using Type = IDecorator<T, TPointerType>;
+
+ ~IDecorator() override = default;
+
+ /**
+ * @brief Установить указатель на декорируемый класс
+ * @details По возможности используйте метод Apply() как наиболее универсальный
+ * @param base Указатель на декорируемый класс
+ */
+ void SetBase( PointerType const& base ) noexcept
+ {
+ mBase = base;
+ }
+
+ void SetBase( PointerType&& base ) noexcept
+ {
+ mBase = std::move( base );
+ }
+
+ /**
+ * @brief Установить указатель на декорируемый класс (цепочка обязанностей)
+ * @details См. https://refactoring.guru/ru/design-patterns/chain-of-responsibility/cpp/example
+ * @details Цепь вызовов: декоратор1 -> декоратор2 -> базовый класс
+ * @param base_or_decorator Указатель на декорируемый класс или целевой декоратор
+ */
+ Type& Apply( PointerType const& base_or_decorator ) noexcept
+ {
+ if( !base_or_decorator )
+ {
+ return *this;
+ }
+
+ SetBase( base_or_decorator );
+ return GetApplyResult();
+ }
+
+ Type& Apply( PointerType&& base_or_decorator ) noexcept
+ {
+ if( !base_or_decorator )
+ {
+ return *this;
+ }
+
+ SetBase( std::move( base_or_decorator ) );
+ return GetApplyResult();
+ }
+
+ private:
+ Type& GetApplyResult() noexcept
+ {
+ auto as_decorator = dynamic_cast< Type * >( mBase.get() );
+ if( as_decorator )
+ {
+ return *as_decorator;
+ }
+
+ return *this;
+ }
+
+ protected:
+ PointerType mBase;
+ };
+
+ template < typename T, typename TDeleter = std::default_delete<T> > using IDecoratorUnique = IDecorator<T, std::unique_ptr<T, TDeleter> >;
+
+} // namespace sbis::devices::generic::types
diff --git a/src/message/builder/types/imessage_builder.hpp b/src/message/builder/types/imessage_builder.hpp
new file mode 100644
index 0000000..701f5f9
--- /dev/null
+++ b/src/message/builder/types/imessage_builder.hpp
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <string>
+
+namespace smtp::message::builder::types
+{
+ class IMessageBuilder
+ {
+ public:
+ virtual ~IMessageBuilder() = default;
+
+ virtual std::string Get() const = 0;
+ };
+}
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();
+ }
+}
diff --git a/src/message/sender.hpp b/src/message/sender.hpp
new file mode 100644
index 0000000..e75faa8
--- /dev/null
+++ b/src/message/sender.hpp
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <list>
+#include <memory>
+
+#include <curl/curl.h>
+
+#include "service/settings.hpp"
+
+namespace smtp::message
+{
+ struct WriteThis
+ {
+ int counter;
+ };
+
+ class Sender
+ {
+ public:
+ Sender( service::Settings const& settings_storage );
+ ~Sender() = default;
+
+ bool 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 );
+ private:
+ void UpdateMailText( std::string const& mail_from, std::string const& mail_to, std::list<std::string> const& cc,
+ std::string const& subject, std::string const& textt ) const;
+ void InitSenders( std::string const& mail_from, std::list<std::string> const& mail_to );
+ void FillRecipients( CURL* curl, std::string const& mail_to, std::list<std::string> const& cc, curl_slist* recipients );
+ std::string GetHostPortData() const;
+ static size_t ReadCallBack( void *ptr, size_t size, size_t nmemb, void *userp );
+
+ service::Settings const& mSettingsStorage;
+ bool InitCurl( CURL* curl, WriteThis const& upload_ctx, std::string const& mail_from );
+ };
+}
+
+