Permalink
Browse files

Added the event "idle", as to allow evented blocking of the SMTP server.

  • Loading branch information...
1 parent af8730b commit c4aaa2202f9b846792d358cf582e8b21025523b1 @miksago committed Jan 15, 2010
Showing with 77 additions and 4 deletions.
  1. +2 −1 examples/config.example
  2. +9 −1 examples/example-client.js
  3. +62 −2 lib/smtp/client.js
  4. +4 −0 lib/smtp/packetHandler.js
View
3 examples/config.example
@@ -9,4 +9,5 @@
var config = exports;
config.port = 25;
-config.host = "smtp.server.com";
+config.host = "smtp.server.com";
+config.from = "test@server.com";
View
10 examples/example-client.js
@@ -10,4 +10,12 @@ var smtp = require("../lib/smtp");
var client = new smtp.Client();
-client.connect(config.port, config.host);
+client.connect(config.port, config.host);
+
+client.addOnce("idle", function(){
+ client.mail(config.from);
+});
+
+client.addOnce("idle", function(){
+ client.quit();
+});
View
64 lib/smtp/client.js
@@ -34,10 +34,21 @@ var PacketHandler = require("./packetHandler").PacketHandler;
SMTP Client
-----------------------------------------------*/
var Client = function(){
+ var client = this;
+
this.socket = null;
this.packetHandler = new PacketHandler();
this.esmtp = false;
this.started = false;
+ this.waiting = false;
+
+ this.packetHandler.addListener("idle", function(){
+ setTimeout(function(){
+ if(client.packetHandler._stack.length == 0 && !client.waiting){
+ client.emit("idle");
+ }
+ }, 500);
+ })
};
process.inherits(Client, process.EventEmitter);
@@ -50,8 +61,10 @@ Client.prototype.connect = function(port, 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){
@@ -79,10 +92,20 @@ Client.prototype.writeline = function(line){
};
Client.prototype.get = function(data, callback){
- this.packetHandler.addOnce("packet", callback);
+ var client = this;
+
+ this.waiting = true;
+
+ this.packetHandler.addOnce("packet", function(){
+ client.waiting = false;
+ callback.apply(this, arguments);
+ });
this.writeline(data);
};
+/*-----------------------------------------------
+ Handshaking
+-----------------------------------------------*/
Client.prototype.handshake = function(){
if(this.esmtp){
this.ehlo();
@@ -91,7 +114,6 @@ Client.prototype.handshake = function(){
}
};
-/* protocol implementation: */
Client.prototype.helo = function(){
var client = this;
@@ -100,6 +122,7 @@ Client.prototype.helo = function(){
case "250":
client.capabilities = packet.data;
client.started = true;
+ client.emit("started");
break;
case "504":
throw SMTPError["504"];
@@ -122,6 +145,7 @@ Client.prototype.ehlo = function(){
case "250":
client.capabilities = packet.data;
client.started = true;
+ client.emit("started");
break;
case "504":
throw SMTPError["504"];
@@ -136,6 +160,42 @@ Client.prototype.ehlo = function(){
});
};
+/*-----------------------------------------------
+ Authentication
+-----------------------------------------------*/
+Client.prototype.auth = function(){};
+
+/*-----------------------------------------------
+ Sending mail
+-----------------------------------------------*/
+Client.prototype.mail = function(from_addr){
+ this.get("MAIL FROM:<"+from_addr+">", function(packet){
+ sys.puts(packet.data.join(" "));
+ });
+};
+Client.prototype.rcpt = function(){};
+Client.prototype.data = function(){};
+
+/*-----------------------------------------------
+ Other Commands
+-----------------------------------------------*/
+Client.prototype.rset = function(){};
+Client.prototype.vrfy = function(){};
+Client.prototype.expn = function(){};
+Client.prototype.help = function(){};
+Client.prototype.noop = function(){};
+
+/*-----------------------------------------------
+ Quit Command
+-----------------------------------------------*/
+Client.prototype.quit = function(){
+ var client = this;
+ this.get("QUIT", function(packet){
+ client.socket.close();
+ });
+};
+
+
View
4 lib/smtp/packetHandler.js
@@ -76,6 +76,10 @@ PacketHandler.prototype.process = function(){
status: status,
data: data
});
+
+ if(this._stack.length == 0){
+ this.emit("idle");
+ }
};
/*-----------------------------------------------

0 comments on commit c4aaa22

Please sign in to comment.