Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A few changes, Removing the smtp server, see the future SMTPD project…

  • Loading branch information...
commit 85cd29db5fa854d9f18e0784a2ae07fc6f5d2451 1 parent a56b2a1
@miksago authored
View
4 .gitignore
@@ -1,2 +1,2 @@
-examples/config.js
-*.swp
+examples/config.js
+*.swp
View
78 examples/example-client.js
@@ -1,31 +1,49 @@
-/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil -*- */
-/*===============================================
- File: example-client.js
-
- Author: Micheil Smith
- Description:
- Demonstration of the smtp library.
-===============================================*/
-var config = require("./config");
-var smtp = require("../lib/smtp");
-
-var client = new smtp.Client();
-
-var message = "Date: "+(new Date()).toString()+"\n\
-From: Node-SMTP <"+config.from+">\n\
-To: <"+config.to+"\n\
-Subject: Node SMTP Works!\n\
-So, It looks like node-smtp all works, which is great news!\n\
-\n\
-Your's Truly,\n\
-SMTP Client.";
-
-client.connect(config.port, config.host).addCallback(function(){
- client.mail(config.from).addCallback(function(){
- client.rcpt(config.to).addCallback(function(){
- client.data(message).addCallback(function(){
- client.quit();
- });
- });
- });
+/*===============================================
+ File: example-client.js
+
+ Author: Micheil Smith
+ Description:
+ Demonstration of the smtp library.
+===============================================*/
+var sys = require("sys");
+var start_mem = process.memoryUsage();
+
+sys.puts(JSON.stringify(start_mem));
+
+var config = require("./config");
+var smtp = require("../lib/smtp");
+
+var client = new smtp.Client();
+
+var message = "Date: "+(new Date()).toString()+"\n\
+From: Node-SMTP <"+config.from+">\n\
+To: "+config.to+"\n\
+Subject: Node SMTP Works!\n\
+So, It looks like node-smtp all works, which is great news!\n\
+\n\
+Your's Truly,\n\
+SMTP Client.";
+
+client.addListener("packetSent", function(data){
+ sys.puts(">>> "+data);
+});
+
+client.connect(config.port, config.host).addCallback(function(){
+ client.mail(config.from).addCallback(function(){
+ client.rcpt(config.to).addCallback(function(){
+ client.data(message).addCallback(function(){
+ client.quit().addCallback(function(){
+ client.disconnect();
+
+ var mem = process.memoryUsage();
+ sys.puts(JSON.stringify(mem));
+ sys.puts(mem["rss"]-start_mem["rss"]);
+ sys.puts(mem["vsize"] - start_mem["vsize"]);
+ sys.puts(mem["heapTotal"] - start_mem["heapTotal"]);
+ sys.puts(mem["heapUsed"] - start_mem["heapUsed"]);
+
+ });
+ });
+ });
+ });
});
View
30 examples/example-server.js
@@ -1,30 +0,0 @@
-/*===============================================
- File: example-server.js
-
- Author: Micheil Smith
- Description:
- An example of the node-smtp server.
-
- (Not Implemented)
-===============================================*/
-
-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 +"]");
-//});
-
-server.addListener( 'ehlo', function( args ) {
- sys.puts( 'ehlo callback' );
- var hostname = args[0];
- var promise = args[1];
- // no 'true' passed as final parameter, won't close the connection
- promise.emitError("What kind of hostname is '" + hostname + "' anyway?");
-});
View
106 lib/mock/server.js
@@ -1,54 +1,54 @@
-/*===============================================
- File: server.js
-
- Author: Micheil Smith
- Description:
- A fake server, for use in testing.
-===============================================*/
-
-var sys = require("sys");
-var tcp = require("tcp");
-
-var MockServer = function(port, host){
- this.server = null;
- this.socket = null;
- this.port = port;
- this.host = host;
-
- this.connect();
-};
-
-MockServer.prototype.connect = function(){
- var self = this;
-
- this.server = tcp.createServer(function(socket){
- self.socket = socket;
-
- self.socket.setEncoding("ascii");
- self.socket.addListener("connect", function () {
- setTimeout(function(){
- self.send("hello\r\n");
- }, 10);
- });
- self.socket.addListener("receive", function (data) {
- self.send(data);
- });
- self.socket.addListener("eof", function () {
- self.send("goodbye\r\n");
- self.socket.close();
- });
- }).listen(self.port, self.host);
-};
-
-MockServer.prototype.close = function(){
- this.socket.close();
-};
-
-MockServer.prototype.send = function(data){
- sys.puts(">> "+data);
- this.socket.send(data);
-};
-
-exports.Server = function(port, host){
- return new MockServer(port, host);
+/*===============================================
+ File: server.js
+
+ Author: Micheil Smith
+ Description:
+ A fake server, for use in testing.
+===============================================*/
+
+var sys = require("sys");
+var tcp = require("tcp");
+
+var MockServer = function(port, host){
+ this.server = null;
+ this.socket = null;
+ this.port = port;
+ this.host = host;
+
+ this.connect();
+};
+
+MockServer.prototype.connect = function(){
+ var self = this;
+
+ this.server = tcp.createServer(function(socket){
+ self.socket = socket;
+
+ self.socket.setEncoding("ascii");
+ self.socket.addListener("connect", function () {
+ setTimeout(function(){
+ self.send("hello\r\n");
+ }, 10);
+ });
+ self.socket.addListener("receive", function (data) {
+ self.send(data);
+ });
+ self.socket.addListener("eof", function () {
+ self.send("goodbye\r\n");
+ self.socket.close();
+ });
+ }).listen(self.port, self.host);
+};
+
+MockServer.prototype.close = function(){
+ this.socket.close();
+};
+
+MockServer.prototype.send = function(data){
+ sys.puts(">> "+data);
+ this.socket.send(data);
+};
+
+exports.Server = function(port, host){
+ return new MockServer(port, host);
};
View
4 lib/smtp.js
@@ -21,15 +21,15 @@ var Client = function(){
/*-----------------------------------------------
SMTP Server (unimplemented, but possible)
------------------------------------------------*/
+-----------------------------------------------
var Server = function(){
var SMTPServer = require("./smtp/server").Server;
return new SMTPServer(arguments);
};
+*/
/*-----------------------------------------------
Exports
-----------------------------------------------*/
exports.Client = Client;
-exports.Server = Server;
View
103 lib/smtp/client.js
@@ -1,4 +1,3 @@
-/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil -*- */
/*===============================================
File: client.js
Author: Micheil Smith
@@ -54,61 +53,58 @@ var Client = function(){
process.inherits(Client, process.EventEmitter);
Client.prototype.connect = function(port, host){
- var client = this;
var promise = new process.Promise();
- this.port = port;
- this.host = host;
+ if(this.socket == null){
+ var client = this;
- this.socket = new tcp.createConnection(this.port, this.host);
- this.socket.setEncoding("ascii");
+ this.port = port;
+ this.host = host;
- this.packetHandler.addListener("packet", function(packet){
- client.packetHandler.removeListener("packet", arguments.callee);
- if(packet.status == "220"){
- if(/ESMTP/i.test(packet.data)){
- client.esmtp = true;
+ this.socket = new tcp.createConnection(this.port, this.host);
+ this.socket.setEncoding("ascii");
+
+ this.packetHandler.addListener("packet", function(packet){
+ client.packetHandler.removeListener("packet", arguments.callee);
+ if(packet.status == "220"){
+ if(/ESMTP/i.test(packet.data)){
+ client.esmtp = true;
+ }
+ client.handshake().addCallback(function(){
+ promise.emitSuccess(packet);
+ });
+ } else {
+ promise.emitError(packet);
}
- client.handshake().addCallback(function(){
- promise.emitSuccess(packet);
- });
- } else {
- promise.emitError(packet);
- }
- });
+ });
- this.socket.addListener("receive", function(data){
- if(client.debug){
- sys.puts("\033[0;33m>> "+data.replace(/[\r\n]/gi, "")+"\033[0m")
- }
- client.packetHandler.receive(data);
- });
+ this.socket.addListener("receive", function(data){
+ if(client.debug){
+ sys.puts("\033[0;33m>> "+data.replace(/[\r\n]/gi, "")+"\033[0m")
+ }
+ client.packetHandler.receive(data);
+ });
+ } else {
+ promise.emitCancel("Already Connected");
+ }
return promise;
};
-Client.prototype.writeline = function(line){
- if(this.debug){
- sys.puts("\033[0;32m>> "+line+"\033[0m");
+Client.prototype.disconnect = function(){
+ var promise = new process.Promise();
+ if(this.socket !== null){
+ try {
+ this.socket.close();
+ promise.emitSuccess();
+ } catch (e){
+ promise.emitError(e);
+ }
+ } else {
+ promise.emitCancel("Not Connected");
}
- this.socket.send(line+"\r\n");
};
-// Legacy Support:
-Client.prototype.get = function(data, callback){
- var client = this;
-
- this.waiting = true;
-
- this.packetHandler.addListener("packet", function(){
- client.packetHandler.removeListener("packet", arguments.callee);
- callback.apply(this, arguments);
- client.waiting = false;
- });
- this.writeline(data);
-};
-
-
// New Evented Send:
Client.prototype.send = function(){
var client = this;
@@ -121,9 +117,11 @@ Client.prototype.send = function(){
promise.emitSuccess.apply(promise, arguments);
client.waiting = false;
});
+
+ var data = Array.prototype.join.call(arguments, " ");
- this.writeline(Array.prototype.join.call(arguments, " "));
-
+ this.socket.send(data+"\r\n");
+ this.emit("packetSent", data);
return promise;
}
@@ -212,7 +210,7 @@ Client.prototype.mail = function(address){
return promise;
};
-Client.prototype.rcpt = function(address){
+Client.prototype.rcpt = function(address, name){
var client = this;
var promise = new process.Promise();
@@ -255,7 +253,17 @@ Client.prototype.rset = function(){};
Client.prototype.vrfy = function(){};
Client.prototype.expn = function(){};
Client.prototype.help = function(){};
-Client.prototype.noop = function(){};
+Client.prototype.noop = function(){
+ var promise = new process.Promise();
+
+ this.send("NOOP").addCallback(function(packet){
+ if(packet.status == "250"){
+ promise.emitsuccess();
+ } else {
+ promise.emitError(packet);
+ }
+ });
+};
/*-----------------------------------------------
Quit Command
@@ -265,7 +273,6 @@ Client.prototype.quit = function(){
var promise = new process.Promise();
this.send("QUIT").addCallback(function(packet){
if(packet.status == "221"){
- client.socket.close();
promise.emitSuccess();
} else {
promise.emitError();
View
291 lib/smtp/server.js
@@ -1,291 +0,0 @@
-/*
- File: server.js
-
- Author: Kenneth Kalmer
- Description:
- This file implements an SMTP server as defined in RFC2821.
-
- Implements:
- ESMTP: RFC2821
-
- Issues:
- Plenty, have patience.
-
-*/
-
-// Global
-var tcp = require('tcp');
-var sys = require('sys');
-
-// Local
-var enable_debug = true;
-var eol = "\r\n";
-
-/*
- RFC2821 defines an SMTP Session
-*/
-var SMTPSession = function() {
-
- this.fromAddress = null;
- this.recipientAddress = null;
- this.callbacks = [];
- 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) {
- session.receive( packet )
- });
- socket.addListener( 'eol', session.eol );
- socket.addListener( 'close', session.close );
-
- sys.puts('Connect listeners: ' + 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.quit();
- });
-
- this.emit( 'connect', socket.remoteAddress, promise );
- }
-
-}
-
-// patterns for commands
-SMTPSession.prototype.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
-}
-
-// Events emitted
-SMTPSession.prototype.events = [
- 'connect',
- 'ehlo',
- 'helo'
-]
-
-// Replies
-
-SMTPSession.prototype.send = function( s ) {
- if( this.socket.readyState == 'open' ) {
- sys.print("SENDING: " + s + eol);
- this.socket.send( s + eol );
- }
-}
-
-SMTPSession.prototype.greeting = function() {
- this.send("220 <hostname> ESMTP node.js");
-}
-
-SMTPSession.prototype.notSupported = function(){
- this.send( "500 not supported" );
-}
-
-SMTPSession.prototype.sendOk = function() {
- this.send( "250 OK" );
-}
-
-SMTPSession.prototype.error = function( message ) {
- this.send( "500 " + message );
-}
-
-// Commands
-SMTPSession.prototype.ehlo = function() {
- var session = this;
- var hostname = this.extractArguments( 'EHLO' );
-
- sys.puts('EHLO listeners: ' + sys.inspect( this.listeners('ehlo') ) );
- if ( this.listeners('ehlo').length <= 0 ) {
- session.send('250-<hostname> Hello ' + this.socket.remoteAddress );
- session.send('250 8BITMIME');
-
- } else {
- var promise = new process.Promise();
- promise.addCallback( function() {
- session.send('250-<hostname> Hello ' + session.socket.remoteAddress );
- session.send('250 8BITMIME');
- });
- promise.addErrback( function( message, quit ) {
- session.error( message );
- if( quit ) session.quit();
- });
-
- this.emit( 'ehlo', hostname, promise );
- }
-}
-
-SMTPSession.prototype.helo = function() {
- var session = this;
- var hostname = this.extractArguments( 'HELO' );
-
- sys.puts('HELO listeners: ' + sys.inspect( this.listeners('ehlo') ) );
- if ( this.listeners('helo').length <= 0 ) {
- session.send('250 <hostname> Hello ' + this.socket.remoteAddress );
-
- } else {
- var promise = new process.Promise();
- promise.addCallback( function() {
- session.send('250 <hostname> Hello ' + session.socket.remoteAddress );
- });
- promise.addErrback( function( message, quit ) {
- session.error( message );
- if( quit ) session.quit();
- });
-
- this.emit( 'helo', hostname, promise );
- }
-}
-
-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();
-}
-
-SMTPSession.prototype.startData = function() {
- this.in_data = true;
- this.send("354 Terminate with line containing only '.'");
-}
-
-SMTPSession.prototype.quit = function() {
- this.send( '221 <hostname> closing connection' );
- this.socket.close();
-}
-
-// Handlers
-SMTPSession.prototype.receive = function( packet ) {
- sys.puts('Received data: ' + packet);
-
- this.buffer += packet;
-
- if ( this.in_data ) {
- this.dataReceived();
-
- } else if ( this.buffer.indexOf( eol ) != 1 ) {
- var command = null;
-
- for( var cmd in this.command_patterns ) {
- if (this.command_patterns[ cmd ].test( this.buffer ) ) {
-
- command = cmd;
- break;
- }
- }
-
- sys.puts( 'Command: ' + sys.inspect(command) );
- switch( cmd ) {
- case 'ehlo':
- this.ehlo();
- break;
- case 'helo':
- this.helo();
- break;
- case 'rcpt':
- this.receiver();
- break;
- case 'from':
- this.sender();
- break;
- case 'data':
- this.startData();
- break;
- case 'quit':
- this.quit();
- break;
- default:
- this.notSupported();
- }
-
- this.buffer = "";
- }
-}
-
-SMTPSession.prototype.dataReceived = function(){
- var re = new RegExp( eol + "." + eol );
- if( re.test( 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");
- this.sendOk();
- }
-}
-
-// Utilities
-
-SMTPSession.prototype.extractArguments = function( command ) {
- return this.buffer.replace( command, '' ).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
-}
-
-/*
- Server class
-*/
-var Server = exports.Server = function() {
- process.EventEmitter.call( this );
-
- this.host = 'localhost';
- this.port = 10025;
- this.callbacks = [];
- this.server = null;
-
- return this;
-}
-
-process.inherits( Server, process.EventEmitter );
-
-Server.prototype.runServer = function() {
- var self = this;
- this.server = tcp.createServer( function( socket ){
-
- var session = new SMTPSession();
-
- // Only add listeners to the session if folks are listening to us.
- session.events.forEach( function( e ) {
- if( self.listeners( e ).length > 0 ) {
- var ev = e;
- session.addListener( e, function() {
- sys.puts('emit-by-proxy: ' + ev );
- self.emit( ev, 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.