Cutelyst  3.1.0
validatoremail.h
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CUTELYSTVALIDATOREMAIL_H
19 #define CUTELYSTVALIDATOREMAIL_H
20 
21 #include <Cutelyst/cutelyst_global.h>
22 #include "validatorrule.h"
23 
24 namespace Cutelyst {
25 
26 class ValidatorEmailPrivate;
27 
49 class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorEmail : public ValidatorRule
50 {
51  Q_GADGET
52 public:
56  enum Category : quint8 {
57  Valid = 1,
58  DNSWarn = 7,
59  RFC5321 = 15,
60  CFWS = 31,
61  Deprecated = 63,
62  RFC5322 = 127,
63  Error = 255
64  };
65  Q_ENUM(Category)
66 
67 
70  enum Diagnose : quint8 {
71  // Address is valid
72  ValidAddress = 0,
73  // Address is valid but a DNS check was not successful
74  DnsWarnNoMxRecord = 5,
75  DnsWarnNoRecord = 6,
76  // Address is valid for SMTP but has unusual Elements
77  RFC5321TLD = 9,
78  RFC5321TLDNumberic = 10,
79  RFC5321QuotedString = 11,
80  RFC5321AddressLiteral = 12,
81  RFC5321IPv6Deprecated = 13,
82  // Address is valid within the message but cannot be used unmodified for the envelope
83  CFWSComment = 17,
84  CFWSFWS = 18,
85  // Address contains deprecated elements but may still be valid in restricted contexts
86  DeprecatedLocalpart = 33,
87  DeprecatedFWS = 34,
88  DeprecatedQText = 35,
89  DeprecatedQP = 36,
90  DeprecatedComment = 37,
91  DeprecatedCText = 38,
92  DeprecatedCFWSNearAt = 49,
93  // The address in only valid according to the broad definition of RFC 5322. It is otherwise invalid
94  RFC5322Domain = 65,
95  RFC5322TooLong = 66,
96  RFC5322LocalTooLong = 67,
97  RFC5322DomainTooLong = 68,
98  RFC5322LabelTooLong = 69,
99  RFC5322DomainLiteral = 70,
100  RFC5322DomLitOBSDText = 71,
101  RFC5322IPv6GroupCount = 72,
102  RFC5322IPv62x2xColon = 73,
103  RFC5322IPv6BadChar = 74,
104  RFC5322IPv6MaxGroups = 75,
105  RFC5322IPv6ColonStart = 76,
106  RFC5322IPv6ColonEnd = 77,
107  // Address is invalid for any purpose
108  ErrorExpectingDText = 129,
109  ErrorNoLocalPart = 130,
110  ErrorNoDomain = 131,
111  ErrorConsecutiveDots = 132,
112  ErrorATextAfterCFWS = 133,
113  ErrorATextAfterQS = 134,
114  ErrorATextAfterDomLit = 135,
115  ErrorExpectingQpair = 136,
116  ErrorExpectingAText = 137,
117  ErrorExpectingQText = 138,
118  ErrorExpectingCText = 139,
119  ErrorBackslashEnd = 140,
120  ErrorDotStart = 141,
121  ErrorDotEnd = 142,
122  ErrorDomainHyphenStart = 143,
123  ErrorDomainHyphenEnd = 144,
124  ErrorUnclosedQuotedStr = 145,
125  ErrorUnclosedComment = 146,
126  ErrorUnclosedDomLiteral = 147,
127  ErrorFWSCRLFx2 = 148,
128  ErrorFWSCRLFEnd = 149,
129  ErrorCRnoLF = 150,
130  ErrorFatal = 254
131  };
132  Q_ENUM(Diagnose)
133 
134  enum Option : quint8 {
135  NoOption = 0,
136  CheckDNS = 1,
137  UTF8Local = 2,
138  AllowIDN = 4,
139  AllowUTF8 = UTF8Local|AllowIDN
140  };
141  Q_DECLARE_FLAGS(Options, Option)
142 
143 
150  ValidatorEmail(const QString &field, Category threshold = RFC5321, Options options = NoOption, const ValidatorMessages &messages = ValidatorMessages(), const QString &defValKey = QString());
151 
155  ~ValidatorEmail() override;
156 
164  static QString diagnoseString(Context *c, Diagnose diagnose, const QString &label = QString());
165 
173  static QString categoryString(Context *c, Category category, const QString &label = QString());
174 
180  static Category category(Diagnose diagnose);
181 
189  static QString categoryString(Context *c, Diagnose diagnose, const QString &label = QString());
190 
200  static bool validate(const QString &email, Category threshold = RFC5321, Options options = NoOption, QList<Diagnose> *diagnoses = nullptr);
201 
202 protected:
210  ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override;
211 
215  QString genericValidationError(Context *c, const QVariant &errorData = QVariant()) const override;
216 
217 private:
218  Q_DECLARE_PRIVATE(ValidatorEmail)
219  Q_DISABLE_COPY(ValidatorEmail)
220 };
221 
222 }
223 
224 Q_DECLARE_OPERATORS_FOR_FLAGS(Cutelyst::ValidatorEmail::Options)
225 
226 #endif //CUTELYSTVALIDATOREMAIL_H
227 
The Cutelyst Context.
Definition: context.h:52
Checks if the value is a valid email address according to specific RFCs.
Diagnose
Single diagnose values that show why an address is not valid.
Category
Validation category, used as threshold to define valid addresses.
Base class for all validator rules.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62