forked from smfreegard/Haraka
/
rfc1869.js
82 lines (71 loc) · 2.47 KB
/
rfc1869.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
// RFC 1869 email address parser
// 6. MAIL FROM and RCPT TO Parameters
// [...]
//
// esmtp-cmd ::= inner-esmtp-cmd [SP esmtp-parameters] CR LF
// esmtp-parameters ::= esmtp-parameter *(SP esmtp-parameter)
// esmtp-parameter ::= esmtp-keyword ["=" esmtp-value]
// esmtp-keyword ::= (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
//
// ; syntax and values depend on esmtp-keyword
// esmtp-value ::= 1*<any CHAR excluding "=", SP, and all
// control characters (US ASCII 0-31
// inclusive)>
//
// ; The following commands are extended to
// ; accept extended parameters.
// inner-esmtp-cmd ::= ("MAIL FROM:" reverse-path) /
// ("RCPT TO:" forward-path)
var chew_regexp = /\s+([A-Za-z0-9][A-Za-z0-9\-]*(=[^= \x00-\x1f]+)?)$/;
exports.parse = function(type, line) {
var params = [];
line = (new String(line)).replace(/\s*$/, '');
if (type === "mail") {
line = line.replace(/from:/i, "");
}
else {
line = line.replace(/to:/i, "");
}
var matches;
while (matches = chew_regexp.exec(line)) {
params.push(matches[1]);
line = line.splice(matches[1].length);
}
params = params.reverse();
// the above will "fail" (i.e. all of the line in params) on
// some addresses without <> like
// MAIL FROM: user=name@example.net
// or RCPT TO: postmaster
// let's see if $line contains nothing and use the first value as address:
if (line.length) {
// parameter syntax error, i.e. not all of the arguments were
// stripped by the while() loop:
if (line.match(/\@.*\s/)) {
throw "Syntax error in parameters";
}
params.unshift(line);
return params;
}
line = params.shift();
if (type === "mail") {
if (!line.length) {
return ["<>"]; // 'MAIL FROM:' --> 'MAIL FROM:<>'
}
if (line.match(/\@.*\s/)) {
throw "Syntax error in parameters";
}
}
else {
if (line.match(/\@.*\s/)) {
throw "Syntax error in parameters";
}
else {
if (line.match(/\s/))
throw "Syntax error in parameters";
if (!line.match(/^(postmaster|abuse)$/i))
throw "Syntax error in address";
}
}
params.unshift(line);
return params;
}