Browse files

Implementing promise based callbacks to all SMTP Command Methods

  • Loading branch information...
1 parent af2183d commit f69d18e193e493dc1ea810cf9a311882bd09c278 @miksago committed Jan 17, 2010
Showing with 78 additions and 97 deletions.
  1. +4 −15 examples/example-client.js
  2. +74 −82 lib/smtp/client.js
View
19 examples/example-client.js
@@ -11,19 +11,8 @@ var smtp = require("../lib/smtp");
var client = new smtp.Client();
-client.connect(config.port, config.host);
-
-client.addOnce("idle", function(){
- client.mail(config.from);
-});
-
-client.addOnce("idle", function(){
- client.rcpt(config.to);
-
- client.addOnce("idle", function(){
- client.data("config.to");
+client.connect(config.port, config.host).addCallback(function(){
+ client.mail(config.from).addCallback(function(){
+ client.rcpt(config.to);
});
-});
-/*client.addOnce("idle", function(){
- client.quit();
-});*/
+});
View
156 lib/smtp/client.js
@@ -21,7 +21,6 @@
negotiation within node.js
===============================================*/
-
// Global
var sys = require("sys");
var tcp = require("tcp");
@@ -38,57 +37,68 @@ var Client = function(){
var client = this;
this.socket = null;
this.packetHandler = new PacketHandler();
+
this.esmtp = false;
+ this.capabilities = {};
+
this.started = false;
this.waiting = false;
+ this.debug = true;
+
this.packetHandler.addListener("idle", function(){
setTimeout(function(){
if(client.packetHandler._stack.length == 0 && !client.waiting){
client.emit("idle");
}
}, 1000);
});
-
+ this.packetHandler.addListener("packet", function(packet){
+ if(client.debug){
+ sys.puts("\033[0;31m>> "+packet.status+" "+packet.data.join("\n")+"\033[0m")
+ }
+ });
};
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;
this.socket = new tcp.createConnection(this.port, this.host);
this.socket.setEncoding("ascii");
- this.waiting = true;
this.packetHandler.addOnce("packet", function(packet){
- client.waiting = false;
- switch(packet.status){
- case "220":
- if(packet.data[0].indexOf("ESMTP") > -1){
- client.esmtp = true;
- }
- client.handshake();
- break;
- case "554":
- throw SMTPError["554"];
- break;
- default:
- throw SMTPError["unhandled"];
- break;
+ if(packet.status == "220"){
+ if(/ESMTP/i.test(packet.data)){
+ client.esmtp = true;
+ }
+ 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.push(data);
});
+
+ return promise;
};
Client.prototype.writeline = function(line){
- sys.puts("\033[0;32m"+line+"\033[0m")
+ if(this.debug){
+ sys.puts("\033[0;32m>> "+line+"\033[0m");
+ }
this.socket.send(line+"\r\n");
};
@@ -109,64 +119,56 @@ Client.prototype.get = function(data, callback){
-----------------------------------------------*/
Client.prototype.handshake = function(){
if(this.esmtp){
- this.ehlo();
+ return this.ehlo();
} else {
- this.helo();
+ return this.helo();
}
};
Client.prototype.helo = function(){
var client = this;
+ var promise = new process.Promise();
this.get("HELO "+this.host, function(packet){
- switch(packet.status){
- case "250":
- for(var i=1, dl=packet.data.length, item; i<dl; ++i){
- item = packet.data[i].substr(4).split(" ");
- client.capabilities[item.shift()] = item;
- }
-
- client.started = true;
- client.emit("started");
- break;
- case "504":
- throw SMTPError["504"];
- break;
- case "550":
- throw SMTPError["550"];
- break;
- default:
- throw SMTPError["unhandled"];
- break;
+ if(packet.status == "250"){
+ for(var i=1, j=packet.data.length, item; i<j; ++i){
+ item = packet.data[i].substr(4).split(" ");
+ client.capabilities[item.shift()] = item;
+ }
+ client.connected = true;
+
+ promise.emitSuccess(packet);
+ } else {
+ promise.emitError(packet);
}
});
+
+ return promise;
};
Client.prototype.ehlo = function(){
var client = this;
+ var promise = new process.Promise();
this.get("EHLO "+this.host, function(packet){
- switch(packet.status){
- case "250":
- for(var i=1, dl=packet.data.length, item; i<dl; ++i){
- item = packet.data[i].substr(4).split(" ");
-
- client.capabilities[item.shift()] = item;
+ if(packet.status == "250"){
+ for(var i=1, j=packet.data.length, item; i<j; ++i){
+ item = packet.data[i].split(" ");
+ if(item.length > 1){
+ client.capabilities[item[0]] = item.join(" ");
+ } else {
+ client.capabilities[item[0]] = true;
}
- client.started = true;
- client.emit("started");
- break;
- case "504":
- throw SMTPError["504"];
- break;
- case "550":
- throw SMTPError["550"];
- break;
- default:
- throw SMTPError["unhandled"];
- break;
+ }
+ client.connected = true;
+
+ promise.emitSuccess(packet);
+ } else {
+ promise.emitError(packet);
}
});
+
+ return promise;
};
/*-----------------------------------------------
@@ -187,40 +189,30 @@ Client.prototype.mail = function(from_addr){
var client = this;
var promise = new process.Promise();
- this.get("MAIL FROM:<"+from_address+">", function(packet){
+ this.get("MAIL FROM:<"+from_addr+">", function(packet){
if(packet.status == "250"){
promise.emitSuccess(packet);
} else {
promise.emitError(packet);
}
});
- /*
- this.get("MAIL FROM:<"+from_addr+">", function(packet){
- sys.debug(JSON.stringify(packet));
- switch(packet.status){
- case "250":
- client.mail_from = true;
- break;
- default:
- throw new SMTPError["unhandled"];
- break;
- }
- });*/
+
+ return promise;
};
Client.prototype.rcpt = function(to_addr){
- var client = this;
-
- this.get("RCPT TO:<"+to_addr+">", function(packet){
- switch(packet.status){
- case "250":
- client.rcpt_to = true;
- break;
- default:
- throw SMTPError["unhandled"];
- break;
- }
- });
+ var client = this;
+ var promise = new process.Promise();
+
+ this.get("RCPT TO:<"+to_addr+">", function(packet){
+ if(packet.status == "250"){
+ promise.emitSuccess(packet);
+ } else {
+ promise.emitError(packet);
+ }
+ });
+
+ return promise;
};
Client.prototype.data = function(data){

0 comments on commit f69d18e

Please sign in to comment.