Skip to content

Commit

Permalink
A few changes, Removing the smtp server, see the future SMTPD project…
Browse files Browse the repository at this point in the history
  • Loading branch information
miksago committed Jan 25, 2010
1 parent a56b2a1 commit 85cd29d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 456 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,2 +1,2 @@
examples/config.js
*.swp
examples/config.js
*.swp
78 changes: 48 additions & 30 deletions 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"]);

});
});
});
});
});
30 changes: 0 additions & 30 deletions examples/example-server.js

This file was deleted.

106 changes: 53 additions & 53 deletions 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);
};
4 changes: 2 additions & 2 deletions lib/smtp.js
Expand Up @@ -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;

103 changes: 55 additions & 48 deletions lib/smtp/client.js
@@ -1,4 +1,3 @@
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil -*- */
/*===============================================
File: client.js
Author: Micheil Smith
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 85cd29d

Please sign in to comment.