-
Notifications
You must be signed in to change notification settings - Fork 5
/
smtpevent.js
276 lines (240 loc) · 8.5 KB
/
smtpevent.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
*
* Copyright (c) 2011, Euan Goddard <[email protected]>.
* All Rights Reserved.
*
* This file is part of smtpevent <https://github.com/euangoddard/node-smtpevent>,
* which is subject to the provisions of the BSD at
* <https://github.com/euangoddard/node-smtpevent/raw/master/LICENCE>. A copy of
* the license should accompany this distribution. THIS SOFTWARE IS PROVIDED "AS
* IS" AND ANY AND ALL EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST
* INFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
*
*******************************************************************************
*/
/**
* @author Euan Goddard
* @version 0.0.2
*/
var net = require('net'),
sys = require('sys'),
util = require('util');
var SMTPServer = function(hostname) {
net.Server.call(this);
var that = this;
util.log('SMTP server started on "'+ hostname + '"');
this.on('connection', function (socket) {
return new SMTPConnection(hostname, that, socket);
});
this.version = '0.0.2';
};
var SMTPConnection = function (hostname, server, socket) {
util.log('New SMTP connection from: ' + socket.remoteAddress);
// Private variables to this instance
var self = this,
EOL = '\r\n',
COMMAND = 0,
DATA = 1,
NEWLINE = '\n',
hostname = hostname || 'localhost',
state = COMMAND,
greeting = 0,
mailfrom = null,
rcpttos = [];
// Private functions:
/**
* Strip extraneous whitespace from the ends of a string
* @param {String} value
* @return {String} value stripped of all whitespace
*/
var strip = function (value) {
return value.replace(/^\s+/, '').replace(/\s+$/, '');
}
/**
* Extract the address ensuring that any <> are correctly removed
* @param {String} keyword
* @param {String} argument
* @return {String} The cleaned address
*/
var get_address = function (keyword, argument) {
var address = null,
keylen = keyword.length;
if (!argument) {
return address;
}
if (argument.substr(0, keylen).toUpperCase() === keyword) {
address = strip(argument.substr(keylen));
if (address.substr(0, 1) === '<' && address.substr(-1, 1) === '>' && address !== '<>') {
// Addresses can be in the form <[email protected]> but watch out
// for null address, e.g. <>
address = address.substr(1, (address.length - 2));
}
}
return address
}
/**
* Emit a response to the client
* @param {String} message
*/
var send_response = function (message) {
socket.write(message + EOL);
}
/**
* Functions to handle incoming SMTP commands
*/
var SMTP = {
HELO: function (argument) {
if (!argument) {
send_response('501 Syntax: HELO hostname')
return;
}
if (greeting) {
send_response('503 Duplicate HELO/EHLO');
} else {
greeting = argument;
send_response('250 ' + hostname + ' Hello ' + socket.remoteAddress);
}
},
NOOP: function (argument) {
if (argument) {
send_response('501 Syntax: NOOP');
} else {
send_response('250 Ok');
}
},
QUIT: function (argument) {
// Ignore any argument
send_response('221 ' + hostname + ' closing connection');
socket.end();
},
MAIL: function (argument) {
var address = get_address('FROM:', argument);
util.log('===> MAIL ' + argument);
if (!address) {
send_response('501 Syntax: MAIL FROM:<address>');
return;
}
if (mailfrom) {
send_response('503 Error: nested MAIL command');
return;
}
mailfrom = address;
util.log('sender: ' + mailfrom);
send_response('250 Ok');
},
RCPT: function (argument) {
util.log('===> RCPT ' + argument);
if (!mailfrom) {
send_response('503 Error: need MAIL command');
return;
}
address = get_address('TO:', argument);
if (!address) {
send_response('501 Syntax: RCPT TO: <address>');
return;
}
rcpttos.push(address);
util.log('recips: ' + rcpttos.join(', '));
send_response('250 Ok');
},
RSET: function (argument) {
if (argument) {
send_response('501 Syntax: RSET');
return;
}
// Reset the sender, recipients, and data, but not the greeting
mailfrom = null;
rcpttos = [];
state = COMMAND;
send_response('250 Ok');
},
DATA: function (argument) {
if (!rcpttos.length) {
send_response('503 Error: need RCPT command');
return;
}
if (argument) {
send_response('501 Syntax: DATA');
return;
}
state = DATA;
send_response('354 End data with <CR><LF>.<CR><LF>');
}
}
// Event listeners:
socket.on('data', function (buffer) {
var line = buffer.toString(),
method = null,
first_space_position,
command,
argument,
current_data = [],
lines;
if (state === COMMAND) {
// Handle the situation where the client is issuing SMTP commands:
if (!line) {
send_response('500 Error: bad syntax');
return;
}
first_space_position = line.indexOf(' ');
if (first_space_position < 0) {
command = strip(line.toUpperCase());
argument = null;
} else {
command = line.substr(0, first_space_position).toUpperCase();
argument = strip(line.substr(first_space_position));
}
if (!(command in SMTP)) {
send_response('502 Error: command "' + command + '" not implemented');
return;
}
SMTP[command](argument);
return;
}
else {
// Handle the case where the client is transmitting data (i.e. not a
// command)
if (state !== DATA) {
send_response('451 Internal confusion');
return;
}
// Ensure that the terminator which appears in the line is removed
// from the final message:
line = line.replace(/\r\n\.\r\n$/, '');
// Remove extraneous carriage returns and de-transparency according
// to RFC 821, Section 4.5.2.
lines = line.split('\r\n');
for (var i=0, text; i<lines.length; i++) {
text = lines[i];
if (text && text.substr(0, 1) === '.') {
current_data.push(text.substr(1));
} else {
current_data.push(text);
}
}
server.emit('incoming-mail',
socket.remoteAddress, mailfrom, rcpttos,
current_data.join(NEWLINE)
);
rcpttos = [];
mailfrom = null;
state = COMMAND;
send_response('250 Ok');
}
});
socket.on('close', function () {
util.log('Socket closed, destroying SMTPConnection instance');
delete self;
});
util.log('Socket connected from: ' + socket.remoteAddress + '. Sending welcome message.');
send_response('220 ' + hostname +' node.js smtpevent server ' + server.version);
}
sys.inherits(SMTPServer, net.Server);
// Export public API:
exports.SMTPServer = SMTPServer;
//var server = new SMTPServer('localhost');
//server.listen(1025, "127.0.0.1");
//server.on('incoming-mail', function () {
// console.log(arguments);
//});