-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCivil.Utils.Types.Email.pas
327 lines (254 loc) · 8.7 KB
/
Civil.Utils.Types.Email.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
(***
* Civil.Utils.Types.Email.pas;
*
* v1.0.0 (Alpha)
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Eng.º Anderson Marques Ribeiro ([email protected]).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
unit Civil.Utils.Types.Email;
interface
uses
SysUtils, Classes, Math, System.Variants, Generics.Collections, NativeXml,
RegularExpressions, AnsiStrings, Winapi.Windows, Civil.Utils.Types;
type
EEmail = class(Exception)
public
type
TEmailErroEnum =
(
eeStringEmailInvalida
);
constructor Create(ACodigoErro: TEmailErroEnum);
strict private
var
FCodigoErro: TEmailErroEnum;
public
property CodigoErro: TEmailErroEnum read FCodigoErro;
end; { EEmail }
TEmail = packed record
public
constructor Create(AEmail: AnsiString);
strict private
class constructor CreateClass;
class var
FDiag,
FDiagExtracao,
FDiagParte1,
FDiagParte2,
FDiagParte3,
FDiagNomeConta: TRegEx;
type
TParte1Type = packed array[0..248] of AnsiChar;
TParte2Type = packed array[0..2] of AnsiChar;
TParte3Type = packed array[0..1] of AnsiChar;
TNomeContaType = packed array[0..63] of AnsiChar;
var
FNomeConta: TNomeContaType;
FParte1: TParte1Type;
FParte2: TParte2Type;
FParte3: TParte3Type;
function GetNomeConta: AnsiString; inline;
procedure SetNomeConta(AValor: AnsiString); inline;
function GetParte1: AnsiString; inline;
procedure SetParte1(AValor: AnsiString); inline;
function GetParte2: AnsiString; inline;
procedure SetParte2(AValor: AnsiString); inline;
function GetParte3: AnsiString; inline;
procedure SetParte3(AValor: AnsiString); inline;
public
property NomeConta: AnsiString read GetNomeConta write SetNomeConta;
property Parte1: AnsiString read GetParte1 write SetParte1;
property Parte2: AnsiString read GetParte2 write SetParte2;
property Parte3: AnsiString read GetParte3 write SetParte3;
function Comprimento: Integer; inline;
procedure LerDe(ANode: TXmlNode); inline;
procedure GravarPara(ANode: TXmlNode); inline;
class function ValidarEmail(AEmail: AnsiString): Boolean; static;
class function Extrair(ATexto: AnsiString; out AResultado: TDadosExtraidosArray): Integer; static;
class operator Implicit(AEmail: AnsiString): TEmail; overload;
class operator Implicit(AEmail: TEmail): AnsiString; overload; inline;
class operator Equal(AEmail1, AEmail2: TEmail): Boolean; inline;
class operator NotEqual(AEmail1, AEmail2: TEmail): Boolean; inline;
end; { TEmail }
const
NULL_MAIL: TEMail =
(
FNomeConta: ''; FParte1: ''; FParte2: ''; FParte3: '';
);
implementation
uses
Civil.Utils.VariantIds;
(*
* EEmail.
* -----------------------------------------------------------------------------
*)
constructor EEmail.Create(ACodigoErro: TEmailErroEnum);
const
MENSAGENS: array[TEmailErroEnum] of String =
(
'A string não contém um e-mail válido'
);
begin
FCodigoErro := ACodigoErro;
inherited Create(MENSAGENS[ACodigoErro]);
end;
(*
* TEmail.
* -----------------------------------------------------------------------------
*)
constructor TEmail.Create(AEmail: AnsiString);
begin
Self := AEmail;
end;
class constructor TEmail.CreateClass;
const
PRODUCAO = '(?<NomeConta>[a-z0-9\._-]{1,64})@(?<Parte1>[a-z0-9_\-]{1,249})\.(?<Parte2>[a-z0-9-]{1,3})(\.(?<Parte3>[a-z0-9]{2}))?';
begin
FDiag := TRegEx.Create('^' + PRODUCAO + '$', [roExplicitCapture, roIgnoreCase, roCompiled]);
FDiagExtracao := TRegEx.Create(PRODUCAO, [roExplicitCapture, roIgnoreCase, roCompiled]);
FDiagNomeConta := TRegEx.Create('^[a-z0-9\._\-]{1,64}$', [roIgnoreCase, roExplicitCapture, roCompiled]);
FDiagParte1 := TRegEx.Create('^[a-z0-9_\-]{1,249}$', [roIgnoreCase, roExplicitCapture, roCompiled]);
FDiagParte2 := TRegEx.Create('^[a-z0-9]{1,3}$', [roIgnoreCase, roExplicitCapture, roCompiled]);
FDiagParte3 := TRegEx.Create('^[a-z0-9]{2}$', [roIgnoreCase, roExplicitCapture, roCompiled]);
end;
function TEmail.GetNomeConta: AnsiString;
begin
Result := AnsiString(FNomeConta);
end;
procedure TEmail.SetNomeConta(AValor: AnsiString);
begin
if not FDiagNomeConta.IsMatch(AValor) then
raise EEmail.Create(eeStringEmailInvalida);
AnsiStrings.StrPCopy(FNomeConta, AValor);
end;
function TEmail.GetParte1: AnsiString;
begin
Result := AnsiString(FParte1);
end;
procedure TEmail.SetParte1(AValor: AnsiString);
begin
if not FDiagParte1.IsMatch(AValor) then
raise EEmail.Create(eeStringEmailInvalida);
AnsiStrings.StrPCopy(FParte1, AValor);
end;
function TEmail.GetParte2: AnsiString;
begin
Result := AnsiString(FParte2);
end;
procedure TEmail.SetParte2(AValor: AnsiString);
begin
if not FDiagParte2.IsMatch(AValor) then
raise EEmail.Create(eeStringEmailInvalida);
AnsiStrings.StrPCopy(FParte2, AValor);
end;
function TEmail.GetParte3: AnsiString;
begin
Result := AnsiString(FParte3);
end;
procedure TEmail.SetParte3(AValor: AnsiString);
begin
if not FDiagParte3.IsMatch(AValor) then
raise EEmail.Create(eeStringEmailInvalida);
AnsiStrings.StrPCopy(FParte3, AValor);
end;
function TEmail.Comprimento: Integer;
var
intComp3: Integer;
begin
intComp3 := Length(AnsiString(FParte3));
Result := Length(AnsiString(FNomeConta)) + Length(AnsiString(FParte1)) +
Length(AnsiString(FParte2)) + intComp3;
Inc(Result, 2);
if intComp3 > 0 then
Inc(Result);
end;
procedure TEmail.LerDe(ANode: TXmlNode);
begin
Self := ANode.NodeByName('EMAIL').Value;
end;
procedure TEmail.GravarPara(ANode: TXmlNode);
var
node: TXmlNode;
begin
node := ANode.FindNode('EMAIL');
if Assigned(node) then
node.Value := AnsiString(Self)
else
ANode.NodeNew('EMAIL').Value := AnsiString(Self);
end;
class function TEmail.ValidarEmail(AEmail: AnsiString): Boolean;
begin
Result := FDiag.IsMatch(AEmail);
end;
class function TEmail.Extrair(ATexto: AnsiString; out AResultado: TDadosExtraidosArray): Integer;
var
matches: TMatchCollection;
i: Integer;
begin
matches := FDiagExtracao.Matches(ATexto);
SetLength(AResultado, matches.Count);
for i := 0 to matches.Count - 1 do
with AResultado[i] do
begin
Dados := matches.Item[i].Value;
Posicao := matches.Item[i].Index;
Valido := ValidarEmail(Dados);
end;
Result := matches.Count;
end;
class operator TEmail.Implicit(AEmail: AnsiString): TEmail;
var
match: TMatch;
begin
match := FDiag.Match(AEmail);
if not match.Success then
raise EEmail.Create(eeStringEmailInvalida);
with Result do
begin
AnsiStrings.StrPCopy(FNomeConta, match.Groups.Item[1].Value);
AnsiStrings.StrPCopy(FParte1, match.Groups.Item[2].Value);
AnsiStrings.StrPCopy(FParte2, match.Groups.Item[3].Value);
if match.Groups.Count = 5 then
AnsiStrings.StrPCopy(FParte3, match.Groups.Item[4].Value)
else
FillChar(FParte3, SizeOf(FParte3), 0);
end;
end;
class operator TEmail.Implicit(AEmail: TEmail): AnsiString;
begin
with AEmail do
begin
Result := AnsiString(FNomeConta) + '@' + AnsiString(FParte1) + '.' +
AnsiString(FParte2);
if Length(AnsiString(FParte3)) > 0 then
Result := Result + '.' + AnsiString(FParte3);
end;
end;
class operator TEmail.Equal(AEmail1, AEmail2: TEmail): Boolean;
begin
Result := AnsiCompareStr(AnsiString(AEmail1), AnsiString(AEmail2)) = 0;
end;
class operator TEmail.NotEqual(AEmail1, AEmail2: TEmail): Boolean;
begin
Result := AnsiCompareStr(AnsiString(AEmail1), AnsiString(AEmail2)) <> 0;
end;
end.