-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-email-parser.js
104 lines (90 loc) · 3.39 KB
/
angular-email-parser.js
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
/*!
* angular-email-parser - v0.1.0
* https://github.com/dbtek/angular-email-parser
* (c) 2014 İsmail Demirbilek <[email protected]>
* License: MIT
*/
angular.module('emailParser', []);
angular.module('emailParser')
/**
* @ngdoc service
* @name emailParser.services:$parseEmail
* @description
* RFC822 mail parser service.
* @param {String} mail - Email source in plain text
* @param {boolean} strict - `true` for strict RFC822 compliance (don't treat `\n` without `\r` as line breaks)
* @param {boolean} parse - `false` to disable actually parsing headers (just separate the header block and the body)
* @returns {object} - Parsed result containing headers and body properties.
*/
.factory('$parseEmail', function() {
var regexes = {
strict: {
headerBlock: /^((?:\S+:(?:.*\r\n[ \t])*.*\r\n)*)\r\n/,
header: /^(\S+):(.*)$/gm,
fold: /\r\n([ \t])/g,
trim: /^\s*(.*\S)?\s*$/,
},
loose: {
headerBlock: /^((?:\S+:(?:.*\r?\n[ \t])*.*\r?\n)*)\r?\n/,
header: /^(\S+):(.*)$/gm,
fold: /\r?\n([ \t])/g,
trim: /^\s*(.*\S)?\s*$/,
},
};
/**
* Parse all headers out of the given header block data.
*
* @param {(string|Buffer)} data
* @param {boolean} strict - `true` for strict RFC822 compliance (don't treat `\n` without `\r` as line breaks)
* @returns {Object.<string, string>} parsed headers
*/
function parseHeaders(data, strict) {
data = unfold(data, strict);
var re = strict ? regexes.strict : regexes.loose;
var headers = {};
var match = re.header.exec(data);
while(match) {
headers[match[1]] = match[2].replace(re.trim, '$1');
match = re.header.exec(data);
} // end while
return headers;
}
/**
* Unfold all folded lines in the given data. (as defined by [RFC822 Section 3.1.1][])
*
* [RFC822 Section 3.1.1]: https://tools.ietf.org/html/rfc822#section-3.1.1
*
* @param {(string|Buffer)} data
* @param {boolean} strict - `true` for strict RFC822 compliance (don't treat `\n` without `\r` as line breaks)
* @returns {string} unfolded data
*/
function unfold(data, strict) {
data = data.toString();
var re = strict ? regexes.strict : regexes.loose;
return data.replace(re.fold, '$1');
}
/**
* If the given data contains a header block, separate the headers and body.
*
* @param {(string|Buffer)} data
* @param {boolean} strict - `true` for strict RFC822 compliance (don't treat `\n` without `\r` as line breaks)
* @param {boolean} parse - `false` to disable actually parsing headers (just separate the header block and the body)
* @returns {HeaderParseDocument}
*/
return function (data, strict, parse) {
data = data.toString();
var re = strict ? regexes.strict : regexes.loose;
var match = re.headerBlock.exec(data);
if(match) {
var doc = {
headerBlock: match[1],
body: data.slice(match[0].length),
};
if(parse === undefined || parse) {
doc.headers = parseHeaders(doc.headerBlock, strict);
} // end if
return doc;
} // end if
return {body: data};
};
});