NAWA  0.8
Web Application Framework for C++
SimpleEmail.cpp
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2019-2021 Tobias Flaig.
8  *
9  * This file is part of nawa.
10  *
11  * nawa is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License,
13  * version 3, as published by the Free Software Foundation.
14  *
15  * nawa is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
25 #include <nawa/util/encoding.h>
26 #include <nawa/util/utils.h>
27 #include <sstream>
28 
29 using namespace nawa;
30 using namespace std;
31 
32 struct mail::SimpleEmail::Data {
33  std::string text;
34  bool quotedPrintableEncode = true;
35 };
36 
37 NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(mail, SimpleEmail)
38 
40 
41 NAWA_COPY_CONSTRUCTOR_DERIVED_IMPL_WITH_NS(mail, SimpleEmail, Email)
42 
43 NAWA_COPY_ASSIGNMENT_OPERATOR_DERIVED_IMPL(mail::SimpleEmail, Email)
44 
45 NAWA_MOVE_CONSTRUCTOR_DERIVED_IMPL_WITH_NS(mail, SimpleEmail, Email)
46 
47 NAWA_MOVE_ASSIGNMENT_OPERATOR_DERIVED_IMPL(mail::SimpleEmail, Email)
48 
49 NAWA_COMPLEX_DATA_ACCESSORS_IMPL(mail::SimpleEmail, text, string)
50 
52 
53 string mail::SimpleEmail::getRaw(shared_ptr<ReplacementRules> const& replacementRules) const {
54  stringstream ret;
55 
56  for (auto const& e : headers()) {
57  if (e.first == "MIME-Version" || (data->quotedPrintableEncode && e.first == "Content-Transfer-Encoding"))
58  continue;
59  ret << e.first << ": " << e.second << "\r\n";
60  }
61 
62  ret << "MIME-Version: 1.0\r\n";
63  if (data->quotedPrintableEncode) {
64  ret << "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
66  replacementRules ? utils::stringReplace(data->text, *replacementRules) : data->text);
67  } else {
68  ret << "\r\n"
69  << (replacementRules ? utils::stringReplace(data->text, *replacementRules) : data->text);
70  }
71 
72  return ret.str();
73 }
Structure representing a basic email.
Namespace containing functions for text encoding and decoding.
#define NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(Class, Member, Type)
Definition: macros.h:137
#define NAWA_COMPLEX_DATA_ACCESSORS_IMPL(Class, Member, Type)
Definition: macros.h:144
#define NAWA_DEFAULT_CONSTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:42
#define NAWA_MOVE_CONSTRUCTOR_DERIVED_IMPL_WITH_NS(Namespace, Class, Parent)
Definition: macros.h:94
#define NAWA_COPY_ASSIGNMENT_OPERATOR_DERIVED_IMPL(Class, Parent)
Definition: macros.h:70
#define NAWA_MOVE_ASSIGNMENT_OPERATOR_DERIVED_IMPL(Class, Parent)
Definition: macros.h:112
#define NAWA_COPY_CONSTRUCTOR_DERIVED_IMPL_WITH_NS(Namespace, Class, Parent)
Definition: macros.h:52
#define NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:37
std::string quotedPrintableEncode(std::string const &input, std::string const &lineEnding="\r\n", bool replaceCrlf=false, bool qEncoding=false)
Definition: encoding.cpp:290
std::unordered_map< std::string, std::string > ReplacementRules
Definition: Email.h:39
std::string stringReplace(std::string input, std::unordered_map< char, char > const &patterns)
Definition: utils.cpp:514
Definition: AppInit.h:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.