Permalink
Browse files

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

  • Loading branch information...
1 parent a56b2a1 commit 85cd29db5fa854d9f18e0784a2ae07fc6f5d2451 @miksago miksago committed Jan 25, 2010
Showing with 160 additions and 456 deletions.
  1. +2 −2 .gitignore
  2. +48 −30 examples/example-client.js
  3. +0 −30 examples/example-server.js
  4. +53 −53 lib/mock/server.js
  5. +2 −2 lib/smtp.js
  6. +55 −48 lib/smtp/client.js
  7. +0 −291 lib/smtp/server.js
View
@@ -1,2 +1,2 @@
-examples/config.js
-*.swp
+examples/config.js
+*.swp
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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();
Oops, something went wrong.

0 comments on commit 85cd29d

Please sign in to comment.