From aa4d426b4d3527d7e166df1a05058c9a4a0f6683 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 30 Apr 2021 00:33:56 +0200 Subject: initial/final commit --- push.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 push.cpp (limited to 'push.cpp') diff --git a/push.cpp b/push.cpp new file mode 100644 index 0000000..e304403 --- /dev/null +++ b/push.cpp @@ -0,0 +1,123 @@ +#include + +#include "vmime/vmime.hpp" +#include "utilities.h" +#include "timeout_handler.hpp" +#include "exceptions.hpp" + +extern "C" void validate_config(struct config *config) +{ + if (config->use_SSL == PARAM_NOT_GIVEN) + config->use_SSL = PARAM_NO; + if (config->use_AUTH == PARAM_NOT_GIVEN) + config->use_AUTH = PARAM_NO; + + if (is_empty_string(config->host)) + FAIL(MSG_BAD_VAR, "SERVER:host"); + if (!is_valid_port_number(config->port_SMTP)) + FAIL(MSG_BAD_PORT_VAR, "SERVER:port_smtp"); + if (!config->subject) + FAIL(MSG_BAD_VAR, "MAIL:subject"); + if (!config->from) + FAIL(MSG_BAD_VAR, "MAIL:from"); + if (!config->reply_to) + FAIL(MSG_BAD_VAR, "MAIL:replyto"); + if (config->nto == 0) + FAIL(MSG_NO_SECTION, "adresow odbiorcow", "CC"); + if (!config->msg_body) + FAIL(MSG_NO_SECTION, "tresci wiadomosci", "MSG"); + if (config->use_AUTH) { + if (!config->password) + FAIL(MSG_BAD_VAR, "SERVER:password"); + if (is_empty_string(config->login)) + FAIL(MSG_BAD_VAR, "SERVER:login"); + } + for (unsigned int i = 0; i < config->nfiles; i++) { + if (access(config->files[i], F_OK) != 0) + FAIL(MSG_ATTACH_NOT_EXIST, config->files[i]); + } +} + +void _perform_work(const struct config *config) +{ + vmime::messageBuilder mb; + mb.setSubject(vmime::text(std::string(config->subject))); + mb.setExpeditor(vmime::mailbox(std::string(config->from))); + mb. getTextPart()->setText + (vmime::make_shared + (config->msg_body)); + for (unsigned int i = 0; i < config->nto; i++) { + mb.getCopyRecipients().appendAddress + (vmime::make_shared + (std::string(config->to[i]))); + } + for (unsigned int i = 0; i < config->nfiles; i++) { + const char *file_name = + config->files[i] + strlen(config->files[i]) - 1; + + while (*file_name != '\\' && + file_name != config->files[i]) + file_name--; + + vmime::shared_ptr att = + vmime::make_shared + (std::string(config->files[i]), + vmime::mediaType("application/octet-stream")); + att->getFileInfo().setFilename(file_name); + mb.appendAttachment(att); + } + + vmime::shared_ptr msg = mb.construct(); + vmime::shared_ptr hf = + vmime::headerFieldFactory::getInstance(); + vmime::shared_ptr reply_to = + hf->create(vmime::fields::REPLY_TO); + + reply_to->setValue(vmime::make_shared + (std::string(config->reply_to))); + msg->getHeader()->appendField(reply_to); + + vmime::shared_ptr session = + vmime::net::session::create(); + vmime::shared_ptr transport; + std::string proto = config->use_SSL ? "smtps" : "smtp"; + + session->getProperties().setProperty("transport.protocol", proto); + session->getProperties().setProperty + ("transport." + proto + ".server.address", + std::string(config->host)); + session->getProperties().setProperty("server.port", config->port_SMTP); + if (config->use_AUTH) { + session->getProperties().setProperty + ("transport." + proto + ".auth.password", + std::string(config->password)); + session->getProperties().setProperty + ("transport." + proto + ".auth.username", + std::string(config->login)); + session->getProperties().setProperty + ("transport." + proto + ".options.need-authentication", + true); + } + transport = session->getTransport(); + if (config->use_SSL) + transport->setCertificateVerifier + (windows_root_certs_verifier()); + transport->setTimeoutHandlerFactory + (vmime::make_shared + (config->timeout)); + transport->connect(); + transport->send(msg); + transport->disconnect(); +} + +extern "C" void perform_work(const struct config *config) +{ + try { + _perform_work(config); + SUCCEED(); + } catch (vmime::exception &e) { + handle_vmime_exception(&e); + } catch (std::exception &e) { + handle_std_exception(&e); + } +} -- cgit v1.2.3