Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic: Translate end-of-lines in email emitter by DNF #2010

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions dnf5-plugins/automatic_plugin/email_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,25 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5/utils/format.hpp>

#include <chrono>
#include <string>

namespace dnf5 {

// TODO(mblaha): use some library to create an email instead of this template
constexpr const char * MESSAGE_TEMPLATE =
constexpr const char * EMAIL_HEADER_TEMPLATE =
"Date: {date}\r\n"
"To: {to}\r\n"
"From: {from}\r\n"
"Subject: {subject}\r\n"
"X-Mailer: dnf5-automatic\r\n"
"\r\n"
"{body}";
"\r\n";

void EmailMessage::set_body(std::stringstream & body) {
this->body.clear();
for (std::string line; std::getline(body, line);) {
this->body.push_back(line);
}
}

std::string EmailMessage::str() {
const auto now = std::chrono::system_clock::now();
Expand All @@ -50,12 +57,15 @@ std::string EmailMessage::str() {

std::string msg;
msg = libdnf5::utils::sformat(
MESSAGE_TEMPLATE,
EMAIL_HEADER_TEMPLATE,
fmt::arg("date", date),
fmt::arg("to", to_str),
fmt::arg("from", from),
fmt::arg("subject", subject),
fmt::arg("body", body));
fmt::arg("subject", subject));
for (const auto & line : body) {
msg.append(line).append("\r\n");
}

return msg;
}

Expand Down
5 changes: 3 additions & 2 deletions dnf5-plugins/automatic_plugin/email_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#ifndef DNF5_PLUGINS_AUTOMATIC_PLUGIN_EMAIL_MESSAGE_HPP
#define DNF5_PLUGINS_AUTOMATIC_PLUGIN_EMAIL_MESSAGE_HPP

#include <sstream>
#include <string>
#include <vector>

Expand All @@ -38,7 +39,7 @@ class EmailMessage {
/// Set the To header value
void set_to(const std::vector<std::string> & to) { this->to = to; };
/// Set the message body
void set_body(std::string_view body) { this->body = body; };
void set_body(std::stringstream & body);

/// Return string representation of the message
std::string str();
Expand All @@ -47,7 +48,7 @@ class EmailMessage {
std::string subject;
std::string from;
std::vector<std::string> to;
std::string body;
std::vector<std::string> body;
};

} // namespace dnf5
Expand Down
4 changes: 1 addition & 3 deletions dnf5-plugins/automatic_plugin/emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void EmitterEmail::notify() {
message.set_to(to);
message.set_from(from);
message.set_subject(subject);
message.set_body(output_stream.str());
message.set_body(output_stream);

{
// use curl to send the message
Expand Down Expand Up @@ -222,8 +222,6 @@ void EmitterEmail::notify() {

curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

curl_easy_setopt(curl, CURLOPT_CRLF, 1L);

res = curl_easy_perform(curl);
fclose(payload_file);
if (res != CURLE_OK) {
Expand Down
4 changes: 2 additions & 2 deletions dnf5-plugins/automatic_plugin/emitters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Emitter {
Emitter(
const ConfigAutomatic & config_automatic,
const libdnf5::base::Transaction & transaction,
const std::stringstream & output_stream,
std::stringstream & output_stream,
const bool success)
: config_automatic(config_automatic),
transaction(transaction),
Expand All @@ -56,7 +56,7 @@ class Emitter {
// resolved upgrade transaction
const libdnf5::base::Transaction & transaction;
// stream with captured upgrade outputs
const std::stringstream & output_stream;
std::stringstream & output_stream;
const bool success;

/// Return number of available upgrades.
Expand Down
Loading