-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailUser.cpp
228 lines (188 loc) · 5.91 KB
/
MailUser.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MailUser.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TMailUser::TMailUser(TCollection* Collection)
: TCollectionItem(Collection)
{
Name = new AnsiString();
Email = new TStringList();
Email->Sorted = true;
Email->Duplicates = dupIgnore;
AddressCount = 0;
ValidEmailChars = new AnsiString();
*ValidEmailChars = "@._";
ValidNameChars = new AnsiString();
*ValidNameChars = " \'";
}
//---------------------------------------------------------------------------
__fastcall TMailUser::~TMailUser()
{
delete ValidNameChars;
delete ValidEmailChars;
delete Email;
delete Name;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::SetName(AnsiString name) {
ExtractName(name, Name);
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::AddAddress(AnsiString addr) {
AnsiString *a = new AnsiString();
ExtractAddress(addr, a);
if (*a != "")
Email->Add(*a);
AddressCount = Email->Count;
delete a;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::ExtractAddress(AnsiString s, AnsiString *addr) {
AnsiString *t = new AnsiString();
AnsiString *n1 = new AnsiString();
AnsiString *n2 = new AnsiString();
*t = s;
*t = t->Trim();
int p = t->AnsiPos("@");
if (p > 0) {
for (int i = p + 1; i <= t->Length(); i++) {
if (SelectEmailChar(t->SubString(i, 1)))
*n2 = *n2 + t->SubString(i, 1);
else
break;
}
for (int i = p; i > 0; i--) {
if (SelectEmailChar(t->SubString(i, 1)))
*n1 = t->SubString(i, 1) + *n1;
else
break;
}
}
*addr = *n1 + *n2;
delete n2;
delete n1;
delete t;
}
//---------------------------------------------------------------------------
bool __fastcall TMailUser::SelectEmailChar(AnsiString c) {
bool rv = false;
if (((c.SubString(1, 1) >= "0") && (c.SubString(1, 1) <= "9")) || ((c.SubString(1, 1) >= "a") && (c.SubString(1, 1) <= "z")) || ((c.SubString(1, 1) >= "A") && (c.SubString(1, 1) <= "Z")))
rv = true;
if (!rv) {
for (int i = 1; i <= ValidEmailChars->Length(); i++) {
rv = (c.SubString(1, 1) == ValidEmailChars->SubString(i, 1));
if (rv) break;
}
}
return rv;
}
//---------------------------------------------------------------------------
bool __fastcall TMailUser::SelectNameChar(AnsiString c) {
bool rv = false;
if (((c.SubString(1, 1) >= "0") && (c.SubString(1, 1) <= "9")) || ((c.SubString(1, 1) >= "a") && (c.SubString(1, 1) <= "z")) || ((c.SubString(1, 1) >= "A") && (c.SubString(1, 1) <= "Z")))
rv = true;
if (!rv) {
for (int i = 1; i <= ValidNameChars->Length(); i++) {
rv = (c.SubString(1, 1) == ValidNameChars->SubString(i, 1));
if (rv) break;
}
}
return rv;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::ExtractName(AnsiString s, AnsiString *n) {
AnsiString *ta = new AnsiString();
AnsiString *tn = new AnsiString();
*tn = s;
ExtractAddress(s, ta);
tn->Delete(tn->AnsiPos(*ta), ta->Length());
*tn = TrimRight(*tn);
int ms = 0;
int me = 0;
if (tn->AnsiPos("\"") > 0) { // Name is in quotes...
for (int i = tn->Length(); i > 0; i--) {
if (tn->SubString(i, 1) == "\"")
if (me == 0)
me = i;
else
ms = i;
if ((ms != 0) && (me != 0))
break;
}
if (ms == 0) ms = 1;
if (me == 0) me = tn->Length();
*tn = tn->SubString(ms, (me - ms + 1));
while (tn->AnsiPos("\"") > 0)
tn->Delete(tn->AnsiPos("\""), 1);
}
else { // Name is not quoted...
for (int i = tn->Length(); i > 0; i--) {
if (SelectNameChar(tn->SubString(i, 1))) {
me = i;
break;
}
}
*tn = TrimRight(tn->SubString(1, me));
me = tn->Length();
for (int i = tn->Length(); i > 0; i--) {
if (SelectNameChar(tn->SubString(i, 1)))
ms = i;
else
break;
}
if (ms == 0) ms = 1;
if (me == 0) me = tn->Length();
*tn = Trim(tn->SubString(ms, (me - ms + 1)));
}
*n = *tn;
delete tn;
delete ta;
}
//---------------------------------------------------------------------------
int __fastcall TMailUser::GetAddressCount() {
return AddressCount;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMailUser::GetAddress(int Num) {
AnsiString rv;
if ((Num >= 0) && (Num < GetAddressCount()))
rv = Email->Strings[Num];
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMailUser::GetName() {
AnsiString rv;
if (*Name != "")
rv = *Name;
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMailUser::GetValidEmailChars() {
return *ValidEmailChars;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::SetValidEmailChars(AnsiString chars) {
*ValidEmailChars = chars;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMailUser::GetValidNameChars() {
return *ValidNameChars;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::SetValidNameChars(AnsiString chars) {
*ValidNameChars = chars;
}
//---------------------------------------------------------------------------
void __fastcall TMailUser::Clear() {
*Name = "";
Email->Clear();
AddressCount = 0;
}
//---------------------------------------------------------------------------