NAWA 0.9
Web Application Framework for C++
EmailAddress.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25#include <nawa/util/encoding.h>
26#include <nawa/util/utils.h>
27#include <regex>
28#include <sstream>
29
30using namespace nawa;
31using namespace std;
32
33namespace {
34 string applyPunycodeToDomain(string const& address) {
35 auto partsOfAddr = utils::splitString(address, '@', false);
36 if (partsOfAddr.size() != 2) {
37 return address;
38 }
39 partsOfAddr[1] = encoding::punycodeEncode(partsOfAddr[1]);
40 return partsOfAddr[0] + "@" + partsOfAddr[1];
41 }
42}// namespace
43
44struct mail::EmailAddress::Data {
45 string name;
46 string address;
47};
48
50
52
53NAWA_COPY_CONSTRUCTOR_IMPL_WITH_NS(mail, EmailAddress)
54
55NAWA_COPY_ASSIGNMENT_OPERATOR_IMPL(mail::EmailAddress)
56
57NAWA_MOVE_CONSTRUCTOR_IMPL_WITH_NS(mail, EmailAddress)
58
59NAWA_MOVE_ASSIGNMENT_OPERATOR_IMPL(mail::EmailAddress)
60
61mail::EmailAddress::EmailAddress(std::string address) : EmailAddress() {
62 data->address = std::move(address);
63}
64
65mail::EmailAddress::EmailAddress(std::string name, std::string address) : EmailAddress() {
66 data->name = std::move(name);
67 data->address = std::move(address);
68}
69
70std::string mail::EmailAddress::get(bool includeName, bool applyPunycode) const {
71 stringstream ret;
72 if (includeName && !data->name.empty()) {
73 ret << data->name << " ";
74 }
75 string address = data->address;
76 if (applyPunycode) {
77 address = applyPunycodeToDomain(address);
78 }
79 ret << '<' << address << '>';
80 return ret.str();
81}
82
83bool mail::EmailAddress::isValid() const {
84 string address = applyPunycodeToDomain(data->address);
85 if (address.empty()) {
86 return false;
87 }
88 regex emCheck(R"([a-z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-z0-9.-]+)", regex::icase);
89 return regex_match(address, emCheck);
90}
91
92NAWA_COMPLEX_DATA_ACCESSORS_IMPL(mail::EmailAddress, name, string)
93
94NAWA_COMPLEX_DATA_ACCESSORS_IMPL(mail::EmailAddress, address, string)
Structure representing an email address.
std::string get(bool includeName=true, bool applyPunycode=true) const
std::string & address() noexcept
std::string & name() noexcept
Namespace containing functions for text encoding and decoding.
#define NAWA_MOVE_CONSTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:90
#define NAWA_DEFAULT_CONSTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:42
#define NAWA_MOVE_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:98
#define NAWA_COPY_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:56
#define NAWA_COPY_CONSTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:48
#define NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:37
std::string punycodeEncode(std::string const &input)
Definition: encoding.cpp:355
std::vector< std::string > splitString(std::string str, char delimiter, bool ignoreEmpty=false)
Definition: utils.cpp:448
Definition: AppInit.h:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.