forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.js
179 lines (157 loc) · 5.57 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"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.attachment_start_hooks = [];
this.banner = null;
this.body_filters = [];
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.ensure_body = function() {
var self = this;
if (this.body) {
return;
}
this.body = new body.Body(this.header);
this.attachment_start_hooks.forEach(function (h) {
self.body.on('attachment_start', h);
});
if (this.banner) {
this.body.set_banner(this.banner);
}
this.body_filters.forEach(function(o) {
self.body.add_filter(function(ct, enc, buf) {
if ((o.ct_match instanceof RegExp && o.ct_match.test(ct.toLowerCase()))
|| ct.toLowerCase().indexOf(String(o.ct_match).toLowerCase()) === 0) {
return o.filter(ct, enc, buf);
}
});
});
};
Transaction.prototype.add_data = function(line) {
if (typeof line === 'string') { // This shouldn't ever really happen...
line = new Buffer(line);
}
// check if this is the end of headers line
if (this.header_pos === 0 && (line[0] === 0x0A || (line[0] === 0x0D && line[1] === 0x0A)) ) {
this.header.parse(this.header_lines);
this.header_pos = this.header_lines.length;
if (this.parse_body) {
this.ensure_body();
}
}
else if (this.header_pos === 0) {
// Build up headers
if (this.header_lines.length < MAX_HEADER_LINES) {
if (line[0] === 0x2E) line = line.slice(1); // Strip leading "."
this.header_lines.push(line.toString('binary').replace(/\r\n$/, '\n'));
}
}
else if (this.header_pos && this.parse_body) {
if (line[0] === 0x2E) line = line.slice(1); // Strip leading "."
var new_line = this.body.parse_more(line.toString('binary').replace(/\r\n$/, '\n'));
if (!new_line.length) {
return; // buffering for banners
}
new_line = new_line.replace(/^\./gm, '..').replace(/\r?\n/gm, '\r\n');
line = new Buffer(new_line);
}
if (!this.discard_data) this.message_stream.add_line(line);
};
Transaction.prototype.end_data = function(cb) {
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.ensure_body();
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();
if (data.length) {
data = data.replace(/^\./gm, '..').replace(/\r?\n/gm, '\r\n');
var line = new Buffer(data);
if (!this.discard_data) this.message_stream.add_line(line);
}
}
this.message_stream.add_line_end(cb);
}
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.attachment_start_hooks.push(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];
}
Transaction.prototype.add_body_filter = function (ct_match, filter) {
this.parse_body = true;
this.body_filters.push({'ct_match': ct_match, 'filter': filter});
}