Skip to content

Commit

Permalink
Don't require user/recipients config values if intraprocess (#296)
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe Bedard <[email protected]>
  • Loading branch information
christophebedard authored Sep 13, 2021
1 parent d862490 commit d54704d
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ As for the values:
* `intraprocess`: enable intraprocess mode by setting to `true`
* optional; by default, intraprocess is disabled
* this makes `email` act as if it was sending emails to itself and entirely bypasses actually sending and receiving emails
* all other options have no effect in practice if intraprocess is enabled
* all other options are optional and have no effect in practice if intraprocess is enabled

Using the same configuration file with the same email for the `username` and `to` fields (i.e., same email address for sending & receiving) for all your executables will work.
Alternatively, you can use two different configuration files for two different executables, e.g., if they're sending emails to each other.
Expand Down
7 changes: 1 addition & 6 deletions email/include/email/email/intra_receiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#define EMAIL__EMAIL__INTRA_RECEIVER_HPP_

#include <chrono>
#include <memory>
#include <optional> // NOLINT cpplint mistakes <optional> for a C system header
#include <string>

#include "email/email/info.hpp"
#include "email/email/receiver.hpp"
Expand All @@ -38,11 +36,8 @@ class IntraEmailReceiver : public EmailReceiver
{
public:
/// Constructor.
/**
* \param user_info the user information for receiving emails
*/
EMAIL_PUBLIC
explicit IntraEmailReceiver(UserInfo::SharedPtrConst user_info);
IntraEmailReceiver();

EMAIL_PUBLIC
virtual ~IntraEmailReceiver();
Expand Down
10 changes: 4 additions & 6 deletions email/include/email/email/intra_sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@ class IntraEmailSender : public EmailSender
public:
/// Constructor.
/**
* \param user_info the user information for sending emails
* \param recipients the email recipients
* \param receiver the intraprocess email receiver
*/
EMAIL_PUBLIC
explicit IntraEmailSender(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients,
std::shared_ptr<IntraEmailReceiver> receiver);
explicit IntraEmailSender(std::shared_ptr<IntraEmailReceiver> receiver);

EMAIL_PUBLIC
virtual ~IntraEmailSender();
Expand Down Expand Up @@ -71,6 +67,8 @@ class IntraEmailSender : public EmailSender
send_email_data(const struct EmailData & data);

std::shared_ptr<IntraEmailReceiver> receiver_;
const std::string from_;
const struct EmailRecipients recipients_;
};

} // namespace email
Expand Down
6 changes: 1 addition & 5 deletions email/include/email/email/receiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ class EmailReceiver
{
public:
/// Constructor.
/**
* \param user_info the user information for receiving emails
*/
EMAIL_PUBLIC
explicit EmailReceiver(UserInfo::SharedPtrConst user_info);
EmailReceiver();

EMAIL_PUBLIC
virtual ~EmailReceiver();
Expand Down Expand Up @@ -75,7 +72,6 @@ class EmailReceiver

protected:
std::shared_ptr<Logger> logger_;
UserInfo::SharedPtrConst user_info_;
std::atomic_bool do_shutdown_;

private:
Expand Down
13 changes: 1 addition & 12 deletions email/include/email/email/sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <memory>
#include <optional> // NOLINT cpplint mistakes <optional> for a C system header
#include <string>

#include "email/email/info.hpp"
#include "email/log.hpp"
Expand All @@ -30,21 +29,14 @@ namespace email
/// Abstract email sender.
/**
* Sends emails.
* The recipients are always the same.
* Only the email subject & body can change from one sent email to another.
*/
class EmailSender
{
public:
/// Constructor.
/**
* \param user_info the user information for sending emails
* \param recipients the email recipients
*/
EMAIL_PUBLIC
explicit EmailSender(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients);
EmailSender();

EMAIL_PUBLIC
virtual ~EmailSender();
Expand Down Expand Up @@ -83,9 +75,6 @@ class EmailSender
std::shared_ptr<Logger>
logger();

UserInfo::SharedPtrConst user_info_;
EmailRecipients::SharedPtrConst recipients_;

private:
EMAIL_DISABLE_COPY(EmailSender)
};
Expand Down
23 changes: 13 additions & 10 deletions email/include/email/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ class Options
public:
/// Constructor.
/**
* The user info, recipients, and polling period values are optional if intraprocess is enabled.
* The curl verbose option is meaningless if intraprocess is enabled.
*
* Not to be called directly: use `parse_options_from_*()` instead.
*
* \param user_info the user info
* \param recipients the recipients
* \param user_info the user info, or `std::nullopt` if intraprocess
* \param recipients the recipients, or `std::nullopt` if intraprocess
* \param curl_verbose the curl verbose status
* \param intraprocess the intraprocess status
* \param polling_period the polling period
*/
EMAIL_PUBLIC
Options(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients,
std::optional<UserInfo::SharedPtrConst> user_info,
std::optional<EmailRecipients::SharedPtrConst> recipients,
const bool curl_verbose,
const bool intraprocess,
const std::optional<std::chrono::nanoseconds> polling_period);
Expand All @@ -61,18 +64,18 @@ class Options

/// Get user information data.
/**
* \return the `UserInfo` object
* \return the `UserInfo` object, or `std::nullopt`
*/
EMAIL_PUBLIC
UserInfo::SharedPtrConst
std::optional<UserInfo::SharedPtrConst>
get_user_info() const;

/// Get email recipient data.
/**
* \return the `EmailRecipients` object
* \return the `EmailRecipients` object, or `std::nullopt`
*/
EMAIL_PUBLIC
EmailRecipients::SharedPtrConst
std::optional<EmailRecipients::SharedPtrConst>
get_recipients() const;

/// Get the curl verbose status.
Expand Down Expand Up @@ -152,8 +155,8 @@ class Options
std::shared_ptr<Logger>
logger();

UserInfo::SharedPtrConst user_info_;
EmailRecipients::SharedPtrConst recipients_;
std::optional<UserInfo::SharedPtrConst> user_info_;
std::optional<EmailRecipients::SharedPtrConst> recipients_;
const bool curl_verbose_;
const bool intraprocess_;
const std::optional<std::chrono::nanoseconds> polling_period_;
Expand Down
13 changes: 7 additions & 6 deletions email/src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,26 @@ Context::init_common()
// other objects from the context on creation or initialization
assert(!receiver_);
if (!options_->intraprocess()) {
assert(options_->get_user_info().has_value());
receiver_ = std::make_shared<CurlEmailReceiver>(
options_->get_user_info(),
options_->get_user_info().value(),
options_->curl_verbose());
std::dynamic_pointer_cast<CurlEmailReceiver>(receiver_)->init();
} else {
receiver_ = std::make_shared<IntraEmailReceiver>(options_->get_user_info());
receiver_ = std::make_shared<IntraEmailReceiver>();
}

assert(!sender_);
if (!options_->intraprocess()) {
assert(options_->get_user_info().has_value());
assert(options_->get_recipients().has_value());
sender_ = std::make_shared<CurlEmailSender>(
options_->get_user_info(),
options_->get_recipients(),
options_->get_user_info().value(),
options_->get_recipients().value(),
options_->curl_verbose());
std::dynamic_pointer_cast<CurlEmailSender>(sender_)->init();
} else {
sender_ = std::make_shared<IntraEmailSender>(
options_->get_user_info(),
options_->get_recipients(),
std::dynamic_pointer_cast<IntraEmailReceiver>(receiver_));
}

Expand Down
2 changes: 1 addition & 1 deletion email/src/email/curl_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace email
CurlEmailReceiver::CurlEmailReceiver(
UserInfo::SharedPtrConst user_info,
const bool curl_verbose)
: EmailReceiver(user_info),
: EmailReceiver(),
CurlExecutor(
{user_info->host_imap, user_info->username, user_info->password},
{"imaps", 993},
Expand Down
2 changes: 1 addition & 1 deletion email/src/email/curl_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CurlEmailSender::CurlEmailSender(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients,
const bool curl_verbose)
: EmailSender(user_info, recipients),
: EmailSender(),
CurlExecutor(
{user_info->host_smtp, user_info->username, user_info->password},
{"smtps", 465},
Expand Down
6 changes: 2 additions & 4 deletions email/src/email/intra_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
// limitations under the License.

#include <chrono>
#include <memory>
#include <optional> // NOLINT cpplint mistakes <optional> for a C system header
#include <string>

#include "email/email/intra_receiver.hpp"
#include "email/email/info.hpp"
Expand All @@ -27,8 +25,8 @@
namespace email
{

IntraEmailReceiver::IntraEmailReceiver(UserInfo::SharedPtrConst user_info)
: EmailReceiver(user_info),
IntraEmailReceiver::IntraEmailReceiver()
: EmailReceiver(),
emails_()
{}

Expand Down
17 changes: 8 additions & 9 deletions email/src/email/intra_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
namespace email
{

IntraEmailSender::IntraEmailSender(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients,
std::shared_ptr<IntraEmailReceiver> receiver)
: EmailSender(user_info, recipients),
receiver_(receiver)
IntraEmailSender::IntraEmailSender(std::shared_ptr<IntraEmailReceiver> receiver)
: EmailSender(),
receiver_(receiver),
from_("[email protected]"),
recipients_("[email protected]")
{}

IntraEmailSender::~IntraEmailSender()
Expand All @@ -49,8 +48,8 @@ IntraEmailSender::send(
struct EmailData data(
"",
"",
user_info_->username,
*recipients_,
from_,
recipients_,
content,
additional_headers);
return send_email_data(data);
Expand All @@ -67,7 +66,7 @@ IntraEmailSender::reply(
struct EmailData data(
"",
email.message_id,
user_info_->username,
from_,
recipients,
content,
additional_headers);
Expand Down
6 changes: 1 addition & 5 deletions email/src/email/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
// limitations under the License.

#include <atomic>
#include <memory>
#include <optional> // NOLINT cpplint mistakes <optional> for a C system header

#include "email/email/info.hpp"
#include "email/email/receiver.hpp"
#include "email/log.hpp"

namespace email
{

EmailReceiver::EmailReceiver(UserInfo::SharedPtrConst user_info)
EmailReceiver::EmailReceiver()
: logger_(log::create("EmailReceiver")),
user_info_(user_info),
do_shutdown_(false)
{}

Expand Down
8 changes: 1 addition & 7 deletions email/src/email/sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@

#include <memory>

#include "email/email/info.hpp"
#include "email/email/sender.hpp"
#include "email/log.hpp"

namespace email
{

EmailSender::EmailSender(
UserInfo::SharedPtrConst user_info,
EmailRecipients::SharedPtrConst recipients)
: user_info_(user_info),
recipients_(recipients)
{}
EmailSender::EmailSender() {}

EmailSender::~EmailSender()
{
Expand Down
Loading

0 comments on commit d54704d

Please sign in to comment.