2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
(function () {
|
2014-09-02 08:15:31 +08:00
|
|
|
|
2014-08-25 23:49:01 +08:00
|
|
|
'use strict';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
|
|
|
var
|
2014-09-05 06:49:03 +08:00
|
|
|
Utils = require('Common/Utils')
|
2013-11-16 06:21:12 +08:00
|
|
|
;
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @param {string=} sEmail
|
|
|
|
* @param {string=} sName
|
2015-01-13 07:24:08 +08:00
|
|
|
* @param {string=} sDkimStatus
|
2015-02-01 03:00:10 +08:00
|
|
|
* @param {string=} sDkimValue
|
2014-08-20 23:03:12 +08:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-02-01 03:00:10 +08:00
|
|
|
function EmailModel(sEmail, sName, sDkimStatus, sDkimValue)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
this.email = sEmail || '';
|
|
|
|
this.name = sName || '';
|
2015-01-13 07:24:08 +08:00
|
|
|
this.dkimStatus = sDkimStatus || 'none';
|
2015-02-01 03:00:10 +08:00
|
|
|
this.dkimValue = sDkimValue || '';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param {AjaxJsonEmail} oJsonEmail
|
|
|
|
* @return {?EmailModel}
|
|
|
|
*/
|
|
|
|
EmailModel.newInstanceFromJson = function (oJsonEmail)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
var oEmailModel = new EmailModel();
|
|
|
|
return oEmailModel.initByJson(oJsonEmail) ? oEmailModel : null;
|
|
|
|
};
|
|
|
|
|
2015-06-05 02:02:31 +08:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param {string} sLine
|
|
|
|
* @param {string=} sDelimiter = ';'
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
EmailModel.splitHelper = function (sLine, sDelimiter)
|
|
|
|
{
|
|
|
|
sDelimiter = sDelimiter || ';';
|
|
|
|
|
|
|
|
sLine = sLine.replace(/[\r\n]+/g, '; ').replace(/[\s]+/g, ' ');
|
|
|
|
|
|
|
|
var
|
|
|
|
iIndex = 0,
|
|
|
|
iLen = sLine.length,
|
|
|
|
bAt = false,
|
|
|
|
sChar = '',
|
|
|
|
sResult = ''
|
|
|
|
;
|
|
|
|
|
|
|
|
for (; iIndex < iLen; iIndex++)
|
|
|
|
{
|
|
|
|
sChar = sLine.charAt(iIndex);
|
|
|
|
switch (sChar)
|
|
|
|
{
|
|
|
|
case '@':
|
|
|
|
bAt = true;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
if (bAt)
|
|
|
|
{
|
|
|
|
bAt = false;
|
|
|
|
sResult += sDelimiter;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sResult += sChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sResult.split(sDelimiter);
|
|
|
|
};
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.name = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.email = '';
|
|
|
|
|
2015-01-13 07:24:08 +08:00
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.dkimStatus = 'none';
|
|
|
|
|
2015-02-01 03:00:10 +08:00
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.dkimValue = '';
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
EmailModel.prototype.clear = function ()
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
this.email = '';
|
|
|
|
this.name = '';
|
2015-02-01 03:00:10 +08:00
|
|
|
|
2015-01-13 07:24:08 +08:00
|
|
|
this.dkimStatus = 'none';
|
2015-02-01 03:00:10 +08:00
|
|
|
this.dkimValue = '';
|
2014-08-20 23:03:12 +08:00
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
2015-04-14 02:45:09 +08:00
|
|
|
* @return {boolean}
|
2014-08-20 23:03:12 +08:00
|
|
|
*/
|
|
|
|
EmailModel.prototype.validate = function ()
|
|
|
|
{
|
|
|
|
return '' !== this.name || '' !== this.email;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} bWithoutName = false
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.hash = function (bWithoutName)
|
|
|
|
{
|
|
|
|
return '#' + (bWithoutName ? '' : this.name) + '#' + this.email + '#';
|
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
EmailModel.prototype.clearDuplicateName = function ()
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
if (this.name === this.email)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
this.name = '';
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} sQuery
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.search = function (sQuery)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
return -1 < (this.name + ' ' + this.email).toLowerCase().indexOf(sQuery.toLowerCase());
|
|
|
|
};
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @param {string} sString
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.parse = function (sString)
|
|
|
|
{
|
|
|
|
this.clear();
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
sString = Utils.trim(sString);
|
|
|
|
|
|
|
|
var
|
2014-11-13 00:46:05 +08:00
|
|
|
mRegex = /(?:"([^"]+)")? ?[<]?(.*?@[^>,]+)>?,? ?/g,
|
2014-08-20 23:03:12 +08:00
|
|
|
mMatch = mRegex.exec(sString)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (mMatch)
|
|
|
|
{
|
|
|
|
this.name = mMatch[1] || '';
|
|
|
|
this.email = mMatch[2] || '';
|
|
|
|
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
else if ((/^[^@]+@[^@]+$/).test(sString))
|
|
|
|
{
|
|
|
|
this.name = '';
|
|
|
|
this.email = sString;
|
|
|
|
}
|
|
|
|
};
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @param {AjaxJsonEmail} oJsonEmail
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.initByJson = function (oJsonEmail)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
var bResult = false;
|
|
|
|
if (oJsonEmail && 'Object/Email' === oJsonEmail['@Object'])
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
this.name = Utils.trim(oJsonEmail.Name);
|
|
|
|
this.email = Utils.trim(oJsonEmail.Email);
|
2015-01-13 07:24:08 +08:00
|
|
|
this.dkimStatus = Utils.trim(oJsonEmail.DkimStatus || '');
|
2015-02-01 03:00:10 +08:00
|
|
|
this.dkimValue = Utils.trim(oJsonEmail.DkimValue || '');
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
bResult = '' !== this.email;
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} bFriendlyView
|
|
|
|
* @param {boolean=} bWrapWithLink = false
|
|
|
|
* @param {boolean=} bEncodeHtml = false
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.toLine = function (bFriendlyView, bWrapWithLink, bEncodeHtml)
|
|
|
|
{
|
|
|
|
var sResult = '';
|
|
|
|
if ('' !== this.email)
|
|
|
|
{
|
|
|
|
bWrapWithLink = Utils.isUnd(bWrapWithLink) ? false : !!bWrapWithLink;
|
|
|
|
bEncodeHtml = Utils.isUnd(bEncodeHtml) ? false : !!bEncodeHtml;
|
|
|
|
|
|
|
|
if (bFriendlyView && '' !== this.name)
|
|
|
|
{
|
|
|
|
sResult = bWrapWithLink ? '<a href="mailto:' + Utils.encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
'" target="_blank" tabindex="-1">' + Utils.encodeHtml(this.name) + '</a>' :
|
|
|
|
(bEncodeHtml ? Utils.encodeHtml(this.name) : this.name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sResult = this.email;
|
|
|
|
if ('' !== this.name)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
if (bWrapWithLink)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
sResult = Utils.encodeHtml('"' + this.name + '" <') +
|
|
|
|
'<a href="mailto:' + Utils.encodeHtml('"' + this.name + '" <' + this.email + '>') + '" target="_blank" tabindex="-1">' + Utils.encodeHtml(sResult) + '</a>' + Utils.encodeHtml('>');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sResult = '"' + this.name + '" <' + sResult + '>';
|
|
|
|
if (bEncodeHtml)
|
|
|
|
{
|
|
|
|
sResult = Utils.encodeHtml(sResult);
|
|
|
|
}
|
2013-11-27 06:34:05 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
else if (bWrapWithLink)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
sResult = '<a href="mailto:' + Utils.encodeHtml(this.email) + '" target="_blank" tabindex="-1">' + Utils.encodeHtml(this.email) + '</a>';
|
2013-11-27 06:34:05 +08:00
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
}
|
2013-11-27 06:34:05 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
return sResult;
|
|
|
|
};
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @param {string} $sEmailAddress
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
EmailModel.prototype.mailsoParse = function ($sEmailAddress)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
$sEmailAddress = Utils.trim($sEmailAddress);
|
|
|
|
if ('' === $sEmailAddress)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var
|
|
|
|
substr = function (str, start, len) {
|
|
|
|
str += '';
|
|
|
|
var end = str.length;
|
|
|
|
|
|
|
|
if (start < 0) {
|
|
|
|
start += end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
|
|
|
|
|
|
|
|
return start >= str.length || start < 0 || start > end ? false : str.slice(start, end);
|
|
|
|
},
|
|
|
|
|
|
|
|
substr_replace = function (str, replace, start, length) {
|
|
|
|
if (start < 0) {
|
|
|
|
start = start + str.length;
|
|
|
|
}
|
|
|
|
length = length !== undefined ? length : str.length;
|
|
|
|
if (length < 0) {
|
|
|
|
length = length + str.length - start;
|
|
|
|
}
|
|
|
|
return str.slice(0, start) + replace.substr(0, length) + replace.slice(length) + str.slice(start + length);
|
|
|
|
},
|
|
|
|
|
|
|
|
$sName = '',
|
|
|
|
$sEmail = '',
|
|
|
|
$sComment = '',
|
|
|
|
|
|
|
|
$bInName = false,
|
|
|
|
$bInAddress = false,
|
|
|
|
$bInComment = false,
|
|
|
|
|
|
|
|
$aRegs = null,
|
|
|
|
|
|
|
|
$iStartIndex = 0,
|
|
|
|
$iEndIndex = 0,
|
|
|
|
$iCurrentIndex = 0
|
|
|
|
;
|
|
|
|
|
|
|
|
while ($iCurrentIndex < $sEmailAddress.length)
|
|
|
|
{
|
|
|
|
switch ($sEmailAddress.substr($iCurrentIndex, 1))
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
|
|
|
|
{
|
|
|
|
$bInName = true;
|
|
|
|
$iStartIndex = $iCurrentIndex;
|
|
|
|
}
|
|
|
|
else if ((!$bInAddress) && (!$bInComment))
|
|
|
|
{
|
|
|
|
$iEndIndex = $iCurrentIndex;
|
|
|
|
$sName = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
|
|
|
|
$sEmailAddress = substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
|
|
|
|
$iEndIndex = 0;
|
|
|
|
$iCurrentIndex = 0;
|
|
|
|
$iStartIndex = 0;
|
|
|
|
$bInName = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
|
|
|
|
{
|
|
|
|
if ($iCurrentIndex > 0 && $sName.length === 0)
|
|
|
|
{
|
|
|
|
$sName = substr($sEmailAddress, 0, $iCurrentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
$bInAddress = true;
|
|
|
|
$iStartIndex = $iCurrentIndex;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
if ($bInAddress)
|
|
|
|
{
|
|
|
|
$iEndIndex = $iCurrentIndex;
|
|
|
|
$sEmail = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
|
|
|
|
$sEmailAddress = substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
|
|
|
|
$iEndIndex = 0;
|
|
|
|
$iCurrentIndex = 0;
|
|
|
|
$iStartIndex = 0;
|
|
|
|
$bInAddress = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
|
|
|
|
{
|
|
|
|
$bInComment = true;
|
|
|
|
$iStartIndex = $iCurrentIndex;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
if ($bInComment)
|
|
|
|
{
|
|
|
|
$iEndIndex = $iCurrentIndex;
|
|
|
|
$sComment = substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
|
|
|
|
$sEmailAddress = substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
|
|
|
|
$iEndIndex = 0;
|
|
|
|
$iCurrentIndex = 0;
|
|
|
|
$iStartIndex = 0;
|
|
|
|
$bInComment = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
$iCurrentIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iCurrentIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sEmail.length === 0)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
$aRegs = $sEmailAddress.match(/[^@\s]+@\S+/i);
|
|
|
|
if ($aRegs && $aRegs[0])
|
|
|
|
{
|
|
|
|
$sEmail = $aRegs[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$sName = $sEmailAddress;
|
|
|
|
}
|
2013-11-27 06:34:05 +08:00
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
if ($sEmail.length > 0 && $sName.length === 0 && $sComment.length === 0)
|
2013-11-27 06:34:05 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
$sName = $sEmailAddress.replace($sEmail, '');
|
2013-11-27 06:34:05 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
$sEmail = Utils.trim($sEmail).replace(/^[<]+/, '').replace(/[>]+$/, '');
|
|
|
|
$sName = Utils.trim($sName).replace(/^["']+/, '').replace(/["']+$/, '');
|
|
|
|
$sComment = Utils.trim($sComment).replace(/^[(]+/, '').replace(/[)]+$/, '');
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
// Remove backslash
|
2014-08-28 19:46:18 +08:00
|
|
|
$sName = $sName.replace(/\\\\(.)/g, '$1');
|
|
|
|
$sComment = $sComment.replace(/\\\\(.)/g, '$1');
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
this.name = $sName;
|
|
|
|
this.email = $sEmail;
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
this.clearDuplicateName();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = EmailModel;
|
2013-11-27 06:34:05 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
}());
|