|
1
|
|
|
|
|
2
|
|
|
import {trim, pString, encodeHtml} from 'Common/Utils'; |
|
3
|
|
|
|
|
4
|
|
|
class EmailModel |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @param {string=} email = '' |
|
8
|
|
|
* @param {string=} name = '' |
|
9
|
|
|
* @param {string=} dkimStatus = 'none' |
|
10
|
|
|
* @param {string=} dkimValue = '' |
|
11
|
|
|
*/ |
|
12
|
|
|
constructor(email = '', name = '', dkimStatus = 'none', dkimValue = '') |
|
13
|
|
|
{ |
|
14
|
|
|
this.email = email; |
|
15
|
|
|
this.name = name; |
|
16
|
|
|
this.dkimStatus = dkimStatus; |
|
17
|
|
|
this.dkimValue = dkimValue; |
|
18
|
|
|
|
|
19
|
|
|
this.clearDuplicateName(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @static |
|
24
|
|
|
* @param {AjaxJsonEmail} json |
|
25
|
|
|
* @returns {?EmailModel} |
|
26
|
|
|
*/ |
|
27
|
|
|
static newInstanceFromJson(json) { |
|
28
|
|
|
const email = new EmailModel(); |
|
29
|
|
|
return email.initByJson(json) ? email : null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @static |
|
34
|
|
|
* @param {string} line |
|
35
|
|
|
* @param {string=} delimiter = ';' |
|
36
|
|
|
* @returns {Array} |
|
37
|
|
|
*/ |
|
38
|
|
|
static splitHelper(line, delimiter = ';') { |
|
39
|
|
|
line = line.replace(/[\r\n]+/g, '; ').replace(/[\s]+/g, ' '); |
|
40
|
|
|
|
|
41
|
|
|
let |
|
42
|
|
|
index = 0, |
|
43
|
|
|
len = 0, |
|
44
|
|
|
at = false, |
|
|
|
|
|
|
45
|
|
|
char = '', |
|
46
|
|
|
result = ''; |
|
47
|
|
|
|
|
48
|
|
|
for (len = line.length; index < len; index++) |
|
49
|
|
|
{ |
|
50
|
|
|
char = line.charAt(index); |
|
51
|
|
|
switch (char) |
|
52
|
|
|
{ |
|
53
|
|
|
case '@': |
|
54
|
|
|
at = true; |
|
55
|
|
|
break; |
|
56
|
|
|
case ' ': |
|
57
|
|
|
if (at) |
|
58
|
|
|
{ |
|
59
|
|
|
at = false; |
|
60
|
|
|
result += delimiter; |
|
61
|
|
|
} |
|
62
|
|
|
break; |
|
63
|
|
|
// no default |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
result += char; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return result.split(delimiter); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @returns {void} |
|
74
|
|
|
*/ |
|
75
|
|
|
clear() { |
|
76
|
|
|
this.email = ''; |
|
77
|
|
|
this.name = ''; |
|
78
|
|
|
|
|
79
|
|
|
this.dkimStatus = 'none'; |
|
80
|
|
|
this.dkimValue = ''; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @returns {boolean} |
|
85
|
|
|
*/ |
|
86
|
|
|
validate() { |
|
87
|
|
|
return '' !== this.name || '' !== this.email; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param {boolean} withoutName = false |
|
92
|
|
|
* @returns {string} |
|
93
|
|
|
*/ |
|
94
|
|
|
hash(withoutName = false) { |
|
95
|
|
|
return '#' + (withoutName ? '' : this.name) + '#' + this.email + '#'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @returns {void} |
|
100
|
|
|
*/ |
|
101
|
|
|
clearDuplicateName() { |
|
102
|
|
|
if (this.name === this.email) |
|
103
|
|
|
{ |
|
104
|
|
|
this.name = ''; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param {string} query |
|
110
|
|
|
* @returns {boolean} |
|
111
|
|
|
*/ |
|
112
|
|
|
search(query) { |
|
113
|
|
|
return -1 < (this.name + ' ' + this.email).toLowerCase().indexOf(query.toLowerCase()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param {string} str |
|
118
|
|
|
*/ |
|
119
|
|
|
parse(str) { |
|
120
|
|
|
this.clear(); |
|
121
|
|
|
|
|
122
|
|
|
str = trim(str); |
|
123
|
|
|
|
|
124
|
|
|
const |
|
125
|
|
|
regex = /(?:"([^"]+)")? ?[<]?(.*?@[^>,]+)>?,? ?/g, |
|
126
|
|
|
match = regex.exec(str); |
|
127
|
|
|
|
|
128
|
|
|
if (match) |
|
129
|
|
|
{ |
|
130
|
|
|
this.name = match[1] || ''; |
|
131
|
|
|
this.email = match[2] || ''; |
|
132
|
|
|
|
|
133
|
|
|
this.clearDuplicateName(); |
|
134
|
|
|
} |
|
135
|
|
|
else if ((/^[^@]+@[^@]+$/).test(str)) |
|
136
|
|
|
{ |
|
137
|
|
|
this.name = ''; |
|
138
|
|
|
this.email = str; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param {AjaxJsonEmail} oJsonEmail |
|
|
|
|
|
|
144
|
|
|
* @returns {boolean} |
|
145
|
|
|
*/ |
|
146
|
|
|
initByJson(json) { |
|
147
|
|
|
let result = false; |
|
148
|
|
|
if (json && 'Object/Email' === json['@Object']) |
|
149
|
|
|
{ |
|
150
|
|
|
this.name = trim(json.Name); |
|
151
|
|
|
this.email = trim(json.Email); |
|
152
|
|
|
this.dkimStatus = trim(json.DkimStatus || ''); |
|
153
|
|
|
this.dkimValue = trim(json.DkimValue || ''); |
|
154
|
|
|
|
|
155
|
|
|
result = '' !== this.email; |
|
156
|
|
|
this.clearDuplicateName(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return result; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param {boolean} friendlyView |
|
164
|
|
|
* @param {boolean=} wrapWithLink = false |
|
165
|
|
|
* @param {boolean=} useEncodeHtml = false |
|
166
|
|
|
* @returns {string} |
|
167
|
|
|
*/ |
|
168
|
|
|
toLine(friendlyView, wrapWithLink = false, useEncodeHtml = false) { |
|
169
|
|
|
let result = ''; |
|
170
|
|
|
if ('' !== this.email) |
|
171
|
|
|
{ |
|
172
|
|
|
if (friendlyView && '' !== this.name) |
|
173
|
|
|
{ |
|
174
|
|
|
result = wrapWithLink ? '<a href="mailto:' + encodeHtml('"' + this.name + '" <' + this.email + '>') + |
|
175
|
|
|
'" target="_blank" tabindex="-1">' + encodeHtml(this.name) + '</a>' : (useEncodeHtml ? encodeHtml(this.name) : this.name); |
|
176
|
|
|
} |
|
177
|
|
|
else |
|
178
|
|
|
{ |
|
179
|
|
|
result = this.email; |
|
180
|
|
|
if ('' !== this.name) |
|
181
|
|
|
{ |
|
182
|
|
|
if (wrapWithLink) |
|
183
|
|
|
{ |
|
184
|
|
|
result = encodeHtml('"' + this.name + '" <') + '<a href="mailto:' + |
|
185
|
|
|
encodeHtml('"' + this.name + '" <' + this.email + '>') + |
|
186
|
|
|
'" target="_blank" tabindex="-1">' + |
|
187
|
|
|
encodeHtml(result) + |
|
188
|
|
|
'</a>' + |
|
189
|
|
|
encodeHtml('>'); |
|
190
|
|
|
} |
|
191
|
|
|
else |
|
192
|
|
|
{ |
|
193
|
|
|
result = '"' + this.name + '" <' + result + '>'; |
|
194
|
|
|
if (useEncodeHtml) |
|
195
|
|
|
{ |
|
196
|
|
|
result = encodeHtml(result); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
else if (wrapWithLink) |
|
201
|
|
|
{ |
|
202
|
|
|
result = '<a href="mailto:' + encodeHtml(this.email) + '" target="_blank" tabindex="-1">' + encodeHtml(this.email) + '</a>'; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return result; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param {string} $sEmailAddress |
|
212
|
|
|
* @returns {boolean} |
|
213
|
|
|
*/ |
|
214
|
|
|
mailsoParse($sEmailAddress) { |
|
215
|
|
|
$sEmailAddress = trim($sEmailAddress); |
|
216
|
|
|
if ('' === $sEmailAddress) |
|
217
|
|
|
{ |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
var |
|
222
|
|
|
substr = function(str, start, len) { |
|
223
|
|
|
str = pString(str); |
|
224
|
|
|
var end = str.length; |
|
225
|
|
|
|
|
226
|
|
|
if (0 > start) |
|
227
|
|
|
{ |
|
228
|
|
|
start += end; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
end = 'undefined' === typeof len ? end : (0 > len ? len + end : len + start); |
|
232
|
|
|
|
|
233
|
|
|
return start >= str.length || 0 > start || start > end ? false : str.slice(start, end); |
|
234
|
|
|
}, |
|
235
|
|
|
|
|
236
|
|
|
substrReplace = function(str, replace, start, length) { |
|
237
|
|
|
str = pString(str); |
|
238
|
|
|
if (0 > start) |
|
239
|
|
|
{ |
|
240
|
|
|
start += str.length; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
length = 'undefined' !== typeof length ? length : str.length; |
|
244
|
|
|
if (0 > length) |
|
245
|
|
|
{ |
|
246
|
|
|
length = length + str.length - start; |
|
247
|
|
|
} |
|
248
|
|
|
return str.slice(0, start) + replace.substr(0, length) + replace.slice(length) + str.slice(start + length); |
|
249
|
|
|
}, |
|
250
|
|
|
|
|
251
|
|
|
$sName = '', |
|
252
|
|
|
$sEmail = '', |
|
253
|
|
|
$sComment = '', |
|
254
|
|
|
|
|
255
|
|
|
$bInName = false, |
|
256
|
|
|
$bInAddress = false, |
|
257
|
|
|
$bInComment = false, |
|
258
|
|
|
|
|
259
|
|
|
$aRegs = null, |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
$iStartIndex = 0, |
|
262
|
|
|
$iEndIndex = 0, |
|
263
|
|
|
$iCurrentIndex = 0; |
|
264
|
|
|
|
|
265
|
|
|
while ($iCurrentIndex < $sEmailAddress.length) |
|
266
|
|
|
{ |
|
267
|
|
|
switch ($sEmailAddress.substr($iCurrentIndex, 1)) |
|
268
|
|
|
{ |
|
269
|
|
|
case '"': |
|
270
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment)) |
|
271
|
|
|
{ |
|
272
|
|
|
$bInName = true; |
|
273
|
|
|
$iStartIndex = $iCurrentIndex; |
|
274
|
|
|
} |
|
275
|
|
|
else if ((!$bInAddress) && (!$bInComment)) |
|
276
|
|
|
{ |
|
277
|
|
|
$iEndIndex = $iCurrentIndex; |
|
278
|
|
|
$sName = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); |
|
279
|
|
|
$sEmailAddress = substrReplace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); |
|
280
|
|
|
$iEndIndex = 0; |
|
|
|
|
|
|
281
|
|
|
$iCurrentIndex = 0; |
|
282
|
|
|
$iStartIndex = 0; |
|
283
|
|
|
$bInName = false; |
|
284
|
|
|
} |
|
285
|
|
|
break; |
|
286
|
|
|
case '<': |
|
287
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment)) |
|
288
|
|
|
{ |
|
289
|
|
|
if (0 < $iCurrentIndex && 0 === $sName.length) |
|
290
|
|
|
{ |
|
291
|
|
|
$sName = substr($sEmailAddress, 0, $iCurrentIndex); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
$bInAddress = true; |
|
295
|
|
|
$iStartIndex = $iCurrentIndex; |
|
296
|
|
|
} |
|
297
|
|
|
break; |
|
298
|
|
|
case '>': |
|
299
|
|
|
if ($bInAddress) |
|
300
|
|
|
{ |
|
301
|
|
|
$iEndIndex = $iCurrentIndex; |
|
302
|
|
|
$sEmail = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); |
|
303
|
|
|
$sEmailAddress = substrReplace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); |
|
304
|
|
|
$iEndIndex = 0; |
|
305
|
|
|
$iCurrentIndex = 0; |
|
306
|
|
|
$iStartIndex = 0; |
|
307
|
|
|
$bInAddress = false; |
|
308
|
|
|
} |
|
309
|
|
|
break; |
|
310
|
|
|
case '(': |
|
311
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment)) |
|
312
|
|
|
{ |
|
313
|
|
|
$bInComment = true; |
|
314
|
|
|
$iStartIndex = $iCurrentIndex; |
|
315
|
|
|
} |
|
316
|
|
|
break; |
|
317
|
|
|
case ')': |
|
318
|
|
|
if ($bInComment) |
|
319
|
|
|
{ |
|
320
|
|
|
$iEndIndex = $iCurrentIndex; |
|
321
|
|
|
$sComment = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1); |
|
322
|
|
|
$sEmailAddress = substrReplace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1); |
|
323
|
|
|
$iEndIndex = 0; |
|
324
|
|
|
$iCurrentIndex = 0; |
|
325
|
|
|
$iStartIndex = 0; |
|
326
|
|
|
$bInComment = false; |
|
327
|
|
|
} |
|
328
|
|
|
break; |
|
329
|
|
|
case '\\': |
|
330
|
|
|
$iCurrentIndex += 1; |
|
331
|
|
|
break; |
|
332
|
|
|
// no default |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$iCurrentIndex += 1; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
if (0 === $sEmail.length) |
|
339
|
|
|
{ |
|
340
|
|
|
$aRegs = $sEmailAddress.match(/[^@\s]+@\S+/i); |
|
341
|
|
|
if ($aRegs && $aRegs[0]) |
|
342
|
|
|
{ |
|
343
|
|
|
$sEmail = $aRegs[0]; |
|
344
|
|
|
} |
|
345
|
|
|
else |
|
346
|
|
|
{ |
|
347
|
|
|
$sName = $sEmailAddress; |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if (0 < $sEmail.length && 0 === $sName.length && 0 === $sComment.length) |
|
352
|
|
|
{ |
|
353
|
|
|
$sName = $sEmailAddress.replace($sEmail, ''); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
$sEmail = trim($sEmail).replace(/^[<]+/, '').replace(/[>]+$/, ''); |
|
357
|
|
|
$sName = trim($sName).replace(/^["']+/, '').replace(/["']+$/, ''); |
|
358
|
|
|
$sComment = trim($sComment).replace(/^[(]+/, '').replace(/[)]+$/, ''); |
|
359
|
|
|
|
|
360
|
|
|
// Remove backslash |
|
361
|
|
|
$sName = $sName.replace(/\\\\(.)/g, '$1'); |
|
362
|
|
|
$sComment = $sComment.replace(/\\\\(.)/g, '$1'); |
|
|
|
|
|
|
363
|
|
|
|
|
364
|
|
|
this.name = $sName; |
|
365
|
|
|
this.email = $sEmail; |
|
366
|
|
|
|
|
367
|
|
|
this.clearDuplicateName(); |
|
368
|
|
|
return true; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
export {EmailModel, EmailModel as default}; |
|
373
|
|
|
|