snappymail/dev/Model/Email.js

273 lines
6 KiB
JavaScript
Raw Normal View History

2022-09-30 17:38:51 +08:00
import { encodeHtml } from 'Common/Html';
2016-07-07 05:03:30 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2020-08-12 07:47:24 +08:00
'use strict';
/**
* Parses structured e-mail addresses from an address field
*
* Example:
*
* "Name <address@domain>"
*
* will be converted to
*
* [{name: "Name", address: "address@domain"}]
*
* @param {String} str Address field
* @return {Array} An array of address objects
*/
export function addressparser(str) {
str = (str || '').toString();
let
endOperator = '',
node = {
type: 'text',
value: ''
},
escaped = false,
address = [],
addresses = [];
const
/*
* Operator tokens and which tokens are expected to end the sequence
*/
OPERATORS = {
'"': '"',
'(': ')',
'<': '>',
',': '',
// Groups are ended by semicolons
':': ';',
// Semicolons are not a legal delimiter per the RFC2822 grammar other
// than for terminating a group, but they are also not valid for any
// other use in this context. Given that some mail clients have
// historically allowed the semicolon as a delimiter equivalent to the
// comma in their UI, it makes sense to treat them the same as a comma
// when used outside of a group.
';': ''
},
pushToken = token => {
token.value = (token.value || '').toString().trim();
token.value.length && address.push(token);
node = {
type: 'text',
value: ''
},
escaped = false;
},
pushAddress = () => {
if (address.length) {
address = _handleAddress(address);
if (address.length) {
addresses = addresses.concat(address);
}
}
2020-08-12 07:47:24 +08:00
address = [];
};
[...str].forEach(chr => {
if (!escaped && (chr === endOperator || (!endOperator && chr in OPERATORS))) {
pushToken(node);
if (',' === chr || ';' === chr) {
pushAddress();
} else {
endOperator = endOperator ? '' : OPERATORS[chr];
if ('<' === chr) {
node.type = 'email';
} else if ('(' === chr) {
node.type = 'comment';
} else if (':' === chr) {
node.type = 'group';
}
}
2020-08-12 07:47:24 +08:00
} else {
node.value += chr;
escaped = !escaped && '\\' === chr;
2020-08-12 07:47:24 +08:00
}
});
pushToken(node);
2020-08-12 07:47:24 +08:00
pushAddress();
2020-08-12 07:47:24 +08:00
return addresses;
// return addresses.map(item => (item.name || item.email) ? new EmailModel(item.email, item.name) : null).filter(v => v);
2020-08-12 07:47:24 +08:00
}
/**
* Converts tokens for a single address into an address object
*
* @param {Array} tokens Tokens object
* @return {Object} Address object
*/
function _handleAddress(tokens) {
let
isGroup = false,
address = {},
addresses = [],
data = {
email: [],
comment: [],
group: [],
text: []
};
2020-08-12 07:47:24 +08:00
tokens.forEach(token => {
isGroup = isGroup || 'group' === token.type;
data[token.type].push(token.value);
});
2020-08-12 07:47:24 +08:00
// If there is no text but a comment, replace the two
if (!data.text.length && data.comment.length) {
data.text = data.comment;
data.comment = [];
}
if (isGroup) {
// http://tools.ietf.org/html/rfc2822#appendix-A.1.3
/*
2020-08-12 07:47:24 +08:00
addresses.push({
email: '',
name: data.text.join(' ').trim(),
group: addressparser(data.group.join(','))
// ,comment: data.comment.join(' ').trim()
2020-08-12 07:47:24 +08:00
});
*/
addresses = addresses.concat(addressparser(data.group.join(',')));
2020-08-12 07:47:24 +08:00
} else {
// If no address was found, try to detect one from regular text
if (!data.email.length && data.text.length) {
var i = data.text.length;
while (i--) {
if (data.text[i].match(/^[^@\s]+@[^@\s]+$/)) {
data.email = data.text.splice(i, 1);
2020-08-12 07:47:24 +08:00
break;
}
}
// still no address
if (!data.email.length) {
i = data.text.length;
while (i--) {
data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, address => {
if (!data.email.length) {
data.email = [address.trim()];
return '';
}
return address.trim();
});
if (data.email.length) {
2020-08-12 07:47:24 +08:00
break;
}
}
}
}
// If there's still no text but a comment exists, replace the two
2020-08-12 07:47:24 +08:00
if (!data.text.length && data.comment.length) {
data.text = data.comment;
data.comment = [];
}
// Keep only the first address occurence, push others to regular text
if (data.email.length > 1) {
data.text = data.text.concat(data.email.splice(1));
2020-08-12 07:47:24 +08:00
}
address = {
// Join values with spaces
email: data.email.join(' ').trim(),
name: data.text.join(' ').trim()
// ,comment: data.comment.join(' ').trim()
2020-08-12 07:47:24 +08:00
};
if (address.email === address.name) {
if (address.email.includes('@')) {
2020-08-12 07:47:24 +08:00
address.name = '';
} else {
address.email = '';
2020-08-12 07:47:24 +08:00
}
}
// address.email = address.email.replace(/^[<]+(.*)[>]+$/g, '$1');
2020-08-12 07:47:24 +08:00
addresses.push(address);
}
return addresses;
}
2022-02-24 21:01:41 +08:00
export class EmailModel extends AbstractModel {
2016-07-07 05:03:30 +08:00
/**
* @param {string=} email = ''
* @param {string=} name = ''
* @param {string=} dkimStatus = 'none'
*/
constructor(email, name, dkimStatus = 'none') {
super();
this.email = email || '';
this.name = name || '';
2016-07-07 05:03:30 +08:00
this.dkimStatus = dkimStatus;
2023-02-13 01:28:30 +08:00
this.cleanup();
2016-07-07 05:03:30 +08:00
}
/**
* @static
* @param {FetchJsonEmail} json
2016-07-07 05:03:30 +08:00
* @returns {?EmailModel}
*/
static reviveFromJson(json) {
const email = super.reviveFromJson(json);
2023-02-13 01:28:30 +08:00
email?.cleanup();
2023-02-14 00:05:44 +08:00
return email?.valid() ? email : null;
2016-07-07 05:03:30 +08:00
}
/**
* @returns {boolean}
*/
2023-02-14 00:05:44 +08:00
valid() {
return this.name || this.email;
2016-07-07 05:03:30 +08:00
}
/**
* @returns {void}
*/
2023-02-13 01:28:30 +08:00
cleanup() {
2019-07-05 03:19:24 +08:00
if (this.name === this.email) {
2016-07-07 05:03:30 +08:00
this.name = '';
}
}
/**
* @param {boolean} friendlyView = false
2022-09-30 17:38:51 +08:00
* @param {boolean} wrapWithLink = false
2016-07-07 05:03:30 +08:00
* @returns {string}
*/
2022-09-30 17:38:51 +08:00
toLine(friendlyView, wrapWithLink) {
2023-02-12 08:02:55 +08:00
let name = this.name,
result = this.email,
2022-09-30 17:38:51 +08:00
toLink = text =>
'<a href="mailto:'
+ encodeHtml(result) + (name ? '?to=' + encodeURIComponent('"' + name + '" <' + result + '>') : '')
+ '" target="_blank" tabindex="-1">'
+ encodeHtml(text || result)
+ '</a>';
if (result) {
if (name) {
result = friendlyView
? (wrapWithLink ? toLink(name) : name)
: (wrapWithLink
? encodeHtml('"' + name + '" <') + toLink() + encodeHtml('>')
: '"' + name + '" <' + result + '>'
);
} else if (wrapWithLink) {
result = toLink();
}
}
return result || name;
2016-07-07 05:03:30 +08:00
}
}