forked from briandeheus/node-lazysmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (100 loc) · 3.96 KB
/
index.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
180
181
182
183
var net = require("net");
var EventEmitter = require("events").EventEmitter;
var util = require("util");
var Mail = function (host, debug) {
EventEmitter.call(this);
this.debug = debug;
this.host = host;
var that = this;
var server = net.createServer(function (socket) {
var data = "";
var moreData = false;
var client = socket.remoteAddress + ":" + socket.remotePort;
//Start handshake
log(that.debug, "Client connected:" + client);
that.emit("connectionIncoming", { client: { address: socket.remoteAddress, port: socket.remotePort }});
replies.connect(socket);
socket.on("data", function(d) {
log(that.debug, "Received data from client:" + client);
log(that.debug, d.toString());
log(that.debug, "End of data from client:" + client);
//In case we expect more data, for example when an e-mail is very large.
if(moreData === true) {
data += d.toString();
} else {
data = d.toString();
}
moreData = false;
//What follows is a range of steps from the SMTP handshake.
if(/^EHLO/i.test(data)) {
replies.ehlo(socket);
} else if(/^HELO/i.test(data)) {
replies.helo(socket);
} else if(/^MAIL FROM/i.test(data)) {
replies.mailFrom(socket);
} else if(/^RCPT TO/i.test(data)) {
replies.recipient(socket);
} else if(/^DATA/i.test(data)) {
replies.data(socket);
} else if(/^QUIT/i.test(data)) {
replies.quit(socket);
} else if(/^\.\r\n/m.test(data)) {
replies.end(socket, data);
} else {
//Assume we're receiving more data and that we're not ready to parse yet.
moreData = true;
}
});
socket.on("close", function() {
that.emit("connectionClosed", { client: { address: socket.remoteAddress, port: socket.remotePort }} );
log(that.debug, "Connection closed");
});
});
server.on("error", function(error) {
that.emit("error", error);
log(that.debug, "Something went wrong:" + error.code);
});
//Functions that take care of the steps in SMTP handshake and actual mail delivery.
var replies = {};
replies.connect = function (socket) {
send( socket, "220 " + host );
}
replies.helo = function (socket) {
send(socket, "250 Ok");
}
replies.quit = function (socket) {
send(socket, "221 Bye");
};
replies.end = function (socket, data) {
that.emit("mail", data);
send(socket, "250 Ok");
};
replies.data = function (socket) {
send(socket, "354 Start mail input; end with <CRLF>.<CRLF>");
}
replies.ehlo = function (socket) {
send(socket, "250-" + host );
send(socket, "250 8BITMIME");
}
replies.mailFrom = function (socket) {
send(socket, "250 Ok");
}
replies.recipient = function (socket) {
send(socket, "250 Ok");
}
function send (socket, message) {
log(that.debug, "sending:" + message);
socket.write(message + "\r\n");
};
this.start = function (port) {
log(that.debug, "Starting lazysmtp");
server.listen(port || 25);
}
};
util.inherits(Mail, EventEmitter);
function log(debug, message) {
if(debug) {
util.log(message);
}
}
exports.Mail = Mail;