forked from andris9/simplesmtp
/
simpleserver.js
124 lines (95 loc) · 3.37 KB
/
simpleserver.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
var createSMTPServer = require("./server"),
Stream = require("stream").Stream,
utillib = require("util"),
oslib = require("os");
module.exports = function(options, connectionCallback){
return new SimpleServer(options, connectionCallback);
}
function SimpleServer(options, connectionCallback){
if(!connectionCallback && typeof options == "function"){
connectionCallback = options;
options = undefined;
}
this.connectionCallback = connectionCallback;
this.options = options || {};
this.initialChunk = true;
if(!("ignoreTLS" in this.options)){
this.options.ignoreTLS = true;
}
if(!("disableDNSValidation" in this.options)){
this.options.disableDNSValidation = true;
}
this.server = createSMTPServer(options);
this.listen = this.server.listen.bind(this.server);
this.server.on("startData", this._onStartData.bind(this));
this.server.on("data", this._onData.bind(this));
this.server.on("dataReady", this._onDataReady.bind(this));
}
SimpleServer.prototype._onStartData = function(connection){
connection._session = new SimpleServerConnection(connection);
this.connectionCallback(connection._session);
}
SimpleServer.prototype._onData = function(connection, chunk){
if(this.initialChunk){
chunk = Buffer.concat([new Buffer(this._generateReceivedHeader(connection) + "\r\n", "utf-8"), chunk])
this.initialChunk = false;
}
connection._session.emit("data", chunk);
}
SimpleServer.prototype._onDataReady = function(connection, callback){
connection._session._setCallback(callback);
connection._session.emit("end");
}
SimpleServer.prototype._generateReceivedHeader = function(connection){
var parts = [];
if(connection.host && !connection.host.match(/^\[?\d+\.\d+\.\d+\.\d+\]?$/)){
parts.push("from " + connection.host);
parts.push("("+connection.remoteAddress+")");
}else{
parts.push("from " + connection.remoteAddress);
}
parts.push("by " + getHostName());
parts.push("with SMTP;");
parts.push(Date());
return "Received: " + parts.join(" ");
}
function SimpleServerConnection(connection){
Stream.call(this);
this.accepted = false;
this.rejected = false;
this._callback = (function(err, code){
if(err){
this.rejected = err;
}else{
this.accepted = code || true;
}
});
["from", "to", "host", "remodeAddress"].forEach((function(key){
if(connection[key]){
this[key] = connection[key];
}
}).bind(this));
}
utillib.inherits(SimpleServerConnection, Stream);
SimpleServerConnection.prototype._setCallback = function(callback){
if(this.rejected){
return callback(this.rejected);
}else if(this.accepted){
return callback(null, this.accepted !== true ? this.accepted : undefined);
}else{
this._callback = callback;
}
}
SimpleServerConnection.prototype.pause = function(){}
SimpleServerConnection.prototype.resume = function(){}
SimpleServerConnection.prototype.accept = function(code){
this._callback(null, code);
}
SimpleServerConnection.prototype.reject = function(reason){
this._callback(new Error(reason || "Rejected"));
}
function getHostName(){
return (oslib.hostname && oslib.hostname()) ||
(oslib.getHostname && oslib.getHostname()) ||
"127.0.0.1";
}