public
Last active

node.js SMTP Server

  • Download Gist
gistfile1.js
JavaScript
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
/*
 
smtpd.js is SMTP server written for node.js
 
MIT License
*/
 
var tcp = require('tcp');
var sys = require('sys');
 
var enable_debug = true;
 
var server = tcp.createServer( function( socket ) {
var eol = "\r\n";
 
// patterns for commands
var command_patterns = {
helo: /^HELO\s*/i,
ehlo: /^EHLO\s*/i,
quit: /^QUIT/i,
from: /^MAIL FROM:\s*/i,
rcpt: /^RCPT TO:\s*/i,
data: /^DATA/i,
noop: /^NOOP/i,
rset: /^RSET/i,
vrfy: /^VRFY\s+/i,
expn: /^EXPN\s+/,
help: /^HELP/i,
tls: /^STARTTLS/i,
auth: /^AUTH\s+/i
}
 
// our replies
var reply = {
send: function(s) {
debug( "reply: '" + (s || "null") + "'" );
socket.send( s + eol );
},
banner: function() {
reply.send("220 <hostname> ESMTP smtpd.js");
},
error: function(s) {
reply.send("500 " + s);
},
ok: function() {
reply.send("250 OK");
}
}
 
function Command( line ) {
 
function parseCommand( line ) {
 
for( var cmd in command_patterns) {
if (command_patterns[ cmd ].test( buffer ) ) {
 
return cmd;
}
}
}
 
function extractArguments( command ) {
return this.line.replace( command, '' ).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
 
this.cmd = parseCommand( line ).toLowerCase();
this.line = line;
this.in_data = false;
this.data = [];
 
this.isRecognized = function() {
return typeof this.cmd != "undefined";
}
 
this.exec = function() {
if ( callbacks[this.cmd] ) {
callbacks[this.cmd].callback(this.line);
}
else {
reply.error("command not implemented");
}
}
 
var that = this;
 
var callbacks = {
quit: {
callback: function () {
reply.send( '221 <hostname> closing connection' );
socket.close();
}
},
ehlo: {
callback: function() {
var hostname = extractArguments( 'EHLO' );
reply.send('250-<hostname> Hello ' + socket.remoteAddress );
reply.send('250 8BITMIME');
}
},
helo: {
callback: function() {
reply.send('250 <hostname> Hello ' + socket.remoteAddress );
}
},
from: {
callback: function() {
this.from = extractArguments( 'MAIL FROM:' );
reply.ok();
}
},
rcpt: {
callback: function() {
this.recipient = extractArguments( 'RCPT TO:' );
reply.ok();
}
},
data: {
callback: function() {
that.in_data = true;
reply.send("354 Terminate with line containing only '.'");
}
}
}
 
this.appendData = function( buffer ) {
var size = buffer.length;
var line = "";
 
for( var i = 0; i < buffer.length; i++ ){
var chr = buffer[i];
 
if( chr + buffer[i + 1] == eol ) {
this.data.push( line );
line = "";
i++;
continue;
}
 
line += chr;
}
 
var size = this.data.length - 1;
 
if( this.data[size] == '.' && this.data[size - 1] == '' ) {
this.in_data = false;
this.data.pop();
this.data.pop();
}
}
 
return this;
}
 
function debug(s) {
if( enable_debug && s != null ) {
sys.print( s.toString() + eol );
sys.print('----------------------------' + eol );
}
}
 
socket.addListener('connect', function() {
reply.banner();
});
 
var buffer = "";
var cmd = {};
 
socket.addListener('receive', function(packet) {
buffer += packet;
 
while( buffer.indexOf(eol) != -1 ) {
 
if ( cmd.in_data ) {
 
cmd.appendData( buffer );
 
// we're finished
if( !cmd.in_data ) {
reply.ok();
}
}
else {
 
cmd = Command( buffer );
 
if ( cmd.isRecognized() ) {
cmd.exec();
} else {
reply.error('unrecognized command');
}
}
 
buffer = "";
}
});
 
socket.addListener('eof', function(){
socket.close();
});
});
 
exports.runServer = function() {
server.listen( 10025 );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.