forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.js
133 lines (119 loc) · 4.11 KB
/
transaction.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";
// An SMTP Transaction
var config = require('./config');
var Header = require('./mailheader').Header;
var body = require('./mailbody');
var utils = require('./utils');
var MessageStream = require('./messagestream');
var trans = exports;
var MAX_HEADER_LINES = config.get('max_header_lines') || 1000;
function Transaction() {
this.uuid = null;
this.mail_from = null;
this.rcpt_to = [];
this.header_lines = [];
this.data_lines = [];
this.banner = null;
this.data_bytes = 0;
this.header_pos = 0;
this.body = null;
this.parse_body = false;
this.notes = {};
this.header = new Header();
this.message_stream = null;
this.discard_data = false;
this.resetting = false;
this.rcpt_count = {
accept: 0,
tempfail: 0,
reject: 0,
};
}
exports.Transaction = Transaction;
exports.createTransaction = function(uuid) {
var t = new Transaction();
t.uuid = uuid || utils.uuid();
// Initialize MessageStream here to pass in the UUID
t.message_stream = new MessageStream(config.get('smtp.ini'), t.uuid, t.header.header_list);
return t;
};
Transaction.prototype.add_data = function(line) {
if (!this.discard_data) this.message_stream.add_line(line);
if (typeof line !== 'string') {
line = line.toString('binary');
}
line = line.replace(/^\./, '').replace(/\r\n$/, '\n');
// check if this is the end of headers line
if (this.header_pos === 0 && line[0] === '\n') {
this.header.parse(this.header_lines);
this.header_pos = this.header_lines.length;
if (this.parse_body) {
this.body = this.body || new body.Body(this.header, {"banner": this.banner});
}
}
else if (this.header_pos === 0) {
// Build up headers
if (this.header_lines.length < MAX_HEADER_LINES) {
this.header_lines.push(line);
}
}
else if (this.header_pos && this.parse_body) {
this.body.parse_more(line);
}
};
Transaction.prototype.end_data = function() {
if (this.header_lines.length && this.header.header_list.length === 0) {
// Headers not parsed yet - must be a busted email
// Strategy: Find first blank line, parse up to that as headers. Rest as body.
var header_pos = 0;
for (var i = 0; i < this.header_lines.length; i++) {
header_pos = i;
if (/^\s*$/.test(this.header_lines[i])) {
this.header_lines[i] = '\n';
break;
}
}
var body_lines = this.header_lines.splice(header_pos + 1);
this.header_lines = this.header_lines.splice(0, header_pos);
this.header.parse(this.header_lines);
this.header_pos = header_pos;
if (this.parse_body) {
this.body = this.body || new body.Body(this.header, {"banner": this.banner});
for (var i = 0; i < body_lines.length; i++) {
this.body.parse_more(body_lines[i]);
}
}
}
if (this.header_pos && this.parse_body) {
var data = this.body.parse_end();
}
}
Transaction.prototype.add_header = function(key, value) {
this.header.add_end(key, value);
if (this.header_pos > 0) this.reset_headers();
};
Transaction.prototype.add_leading_header = function(key, value) {
this.header.add(key, value);
if (this.header_pos > 0) this.reset_headers();
};
Transaction.prototype.reset_headers = function () {
var header_lines = this.header.lines();
this.header_pos = header_lines.length;
};
Transaction.prototype.remove_header = function (key) {
this.header.remove(key);
if (this.header_pos > 0) this.reset_headers();
};
Transaction.prototype.attachment_hooks = function (start, data, end) {
this.parse_body = 1;
this.body = this.body || new body.Body(this.header, {"banner": this.banner});
this.body.on('attachment_start', start);
};
Transaction.prototype.set_banner = function (text, html) {
throw "transaction.set_banner is currently non-functional";
this.parse_body = true;
if (!html) {
html = text.replace(/\n/g, '<br/>\n');
}
this.banner = [text, html];
}