forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.js
93 lines (81 loc) · 2.63 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
"use strict";
// An SMTP Transaction
var config = require('./config');
var logger = require('./logger');
var Header = require('./mailheader').Header;
var body = require('./mailbody');
var utils = require('./utils');
var trans = exports;
function Transaction() {
this.mail_from = null;
this.rcpt_to = [];
this.data_lines = [];
this.banner = null;
this.data_bytes = 0;
this.header_pos = 0;
this.parse_body = false;
this.notes = {};
this.header = new Header();
}
exports.Transaction = Transaction;
exports.createTransaction = function(uuid) {
var t = new Transaction();
t.uuid = uuid || utils.uuid();
return t;
};
Transaction.prototype.add_data = function(line) {
this.data_bytes += line.length;
// check if this is the end of headers line (note the regexp isn't as strong
// as it should be - it accepts whitespace in a blank line - we've found this
// to be a good heuristic rule though).
if (this.header_pos === 0 && line.match(/^\s*$/)) {
this.header.parse(this.data_lines);
this.header_pos = this.data_lines.length;
if (this.parse_body) {
this.body = this.body || new body.Body(this.header, {"banner": this.banner});
}
}
else if (this.header_pos && this.parse_body) {
line = this.body.parse_more(line);
}
if (line.length) {
this.data_lines.push(line);
}
};
Transaction.prototype.end_data = function() {
if (this.header_pos && this.parse_body) {
var data = this.body.parse_end();
if (data.length) {
this.data_lines.push(data);
}
}
}
Transaction.prototype.add_header = function(key, value) {
this.header.add_end(key, value);
if (this.header_pos > 0) this.reset_headers();
};
Transaction.prototype.reset_headers = function () {
var header_lines = this.header.lines();
this.data_lines = header_lines.concat(this.data_lines.slice(this.header_pos));
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);
if (data)
this.body.on('attachment_data', data);
if (end)
this.body.on('attachment_end', end);
};
Transaction.prototype.set_banner = function (text, html) {
this.parse_body = true;
if (!html) {
html = text.replace(/\n/g, '<br/>\n');
}
this.banner = [text, html];
}