Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Callback proof of concept

  • Loading branch information...
commit 96137a47b92e30f72b742072279fcbf7f12c4bc2 1 parent 7826ab6
Kenneth Kalmer kennethkalmer authored committed
Showing with 64 additions and 11 deletions.
  1. +7 −0 examples/example-server.js
  2. +57 −11 lib/smtp/server.js
7 examples/example-server.js
View
@@ -13,3 +13,10 @@ var smtp = require("../lib/smtp");
var server = new smtp.Server();
server.runServer();
+var sys = require('sys');
+server.addListener('connect', function( args ) {
+ var remoteHost = args[0];
+ var promise = args[1];
+ sys.puts( 'connect callback' );
+ promise.emitError("I don't like your type [" + remoteHost +"]");
+});
68 lib/smtp/server.js
View
@@ -24,17 +24,23 @@ var eol = "\r\n";
/*
RFC2821 defines an SMTP Session
*/
-var SMTPSession = function( socket ) {
- var session = this;
+var SMTPSession = function() {
- this.from = null;
- this.recipient = null;
+ this.fromAddress = null;
+ this.recipientAddress = null;
this.callbacks = [];
- this.socket = socket;
+ this.socket = null;
this.buffer = "";
this.data = null;
this.in_data = false;
+}
+process.inherits( SMTPSession, process.EventEmitter );
+
+SMTPSession.prototype.connect = function( socket ) {
+
+ var session = this;
+ this.socket = socket;
// callbacks first
socket.addListener( 'receive', function(packet) {
@@ -43,10 +49,24 @@ var SMTPSession = function( socket ) {
socket.addListener( 'eol', session.eol );
socket.addListener( 'close', session.close );
- session.greeting();
-}
+ sys.puts( 'Listeners for connect: ' + sys.inspect( this.listeners('connect') ) );
+ if ( this.listeners('connect').length <= 0 ) {
+ session.greeting();
+
+ } else {
+ var promise = new process.Promise();
+ promise.addCallback( function() {
+ session.greeting();
+ });
+ promise.addErrback( function( message, quit ) {
+ session.error( message );
+ if( quit ) session.close();
+ });
+
+ this.emit( 'connect', socket.remoteAddress, promise );
+ }
-sys.inherits( SMTPSession, process.EventEmitter );
+}
// patterns for commands
SMTPSession.prototype.command_patterns = {
@@ -65,6 +85,11 @@ SMTPSession.prototype.command_patterns = {
auth: /^AUTH\s+/i
}
+// Events emitted
+SMTPSession.prototype.events = [
+ 'connect'
+]
+
// Replies
SMTPSession.prototype.send = function( s ) {
@@ -83,6 +108,9 @@ SMTPSession.prototype.notSupported = function(){
SMTPSession.prototype.sendOk = function() {
this.send( "250 OK" );
}
+SMTPSession.prototype.error = function( message ) {
+ this.send( "500 " + message );
+}
// Commands
SMTPSession.prototype.ehlo = function() {
@@ -97,10 +125,12 @@ SMTPSession.prototype.helo = function() {
}
SMTPSession.prototype.receiver = function() {
+ this.recipientAddress = this.extractArguments( 'RCPT TO:' ).replace(/[<>]/g, '');
this.sendOk();
}
SMTPSession.prototype.sender = function() {
+ this.fromAddress = this.extractArguments( 'MAIL FROM:' ).replace(/[<>]/g, '');
this.sendOk();
}
@@ -165,7 +195,7 @@ SMTPSession.prototype.receive = function( packet ) {
SMTPSession.prototype.dataReceived = function(){
var re = new RegExp( eol + "." + eol );
if( re.test( this.buffer ) ) {
- this.data = this.buffer;
+ this.data = this.buffer.substr( 0, this.buffer.length - 5 ); // \r\n.\r\n
this.buffer = "";
this.in_data = false;
sys.puts("End of data");
@@ -190,11 +220,27 @@ var Server = exports.Server = function() {
return this;
}
-sys.inherits( Server, process.EventEmitter );
+process.inherits( Server, process.EventEmitter );
Server.prototype.runServer = function() {
+ var self = this;
this.server = tcp.createServer( function( socket ){
- new SMTPSession( socket );
+
+ var session = new SMTPSession();
+
+ // Only add listeners to the session if folks are listening to us.
+ for( var i = 0; i < session.events.length; i++ ) {
+ var e = session.events[i];
+ if( self.listeners( e ).length > 0 ) {
+ session.addListener('connect', function() {
+ self.emit('connect', arguments);
+ });
+ }
+ }
+
+ session.connect( socket );
+
+ return session;
});
this.server.listen( this.port, this.host );
Please sign in to comment.
Something went wrong with that request. Please try again.