forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.js
105 lines (92 loc) · 2.95 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
"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;
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.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) {
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
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_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];
}