NAWA 0.8
Web Application Framework for C++
EmailAddress.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 <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(string address) : EmailAddress() {
62 data->address = move(address);
63}
64
65mail::EmailAddress::EmailAddress(string name, string address) : EmailAddress() {
66 data->name = move(name);
67 data->address = move(address);
68}
69
70string 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.
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:356
std::vector< std::string > splitString(std::string str, char delimiter, bool ignoreEmpty=false)
Definition: utils.cpp:418
Definition: AppInit.h:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.