Permalink
Browse files

Rearranging the filesystem a little:

- Added directory for vendor packages (will be git submodule soon)
- Moved the eventEmitter enhancements into vendor
- Moved the queue library into vendor
- Started work on a mock SMTP Server for testing purposes.
- Removed old empty files.
  • Loading branch information...
1 parent d3f1c73 commit 28829c16b9c49ee18ee9909eb08e28498a91ee46 @miksago miksago committed Jan 16, 2010
@@ -18,8 +18,11 @@ client.addOnce("idle", function(){
client.addOnce("idle", function(){
client.rcpt(config.to);
+
+ client.addOnce("idle", function(){
+ client.data("config.to");
+ });
});
-
/*client.addOnce("idle", function(){
client.quit();
});*/
@@ -0,0 +1,11 @@
+/*===============================================
+ File: example-mockserver.js
+
+ Author: Micheil Smith
+ Description:
+ Example of a mock server
+===============================================*/
+var sys = require("sys");
+var mockServer = require("../mock/server").Server;
+
+var server = mockServer(3125, "localhost");
View
No changes.
View
No changes.
View
No changes.
View
@@ -97,8 +97,8 @@ Client.prototype.get = function(data, callback){
this.waiting = true;
this.packetHandler.addOnce("packet", function(){
- client.waiting = false;
callback.apply(this, arguments);
+ client.waiting = false;
});
this.writeline(data);
};
@@ -120,7 +120,12 @@ Client.prototype.helo = function(){
this.get("HELO "+this.host, function(packet){
switch(packet.status){
case "250":
- client.capabilities = packet.data;
+ 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;
@@ -143,7 +148,11 @@ Client.prototype.ehlo = function(){
this.get("EHLO "+this.host, function(packet){
switch(packet.status){
case "250":
- client.capabilities = packet.data;
+ 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;
@@ -165,42 +174,80 @@ Client.prototype.ehlo = function(){
-----------------------------------------------*/
Client.prototype.auth = function(){};
+Client.prototype.auth_methods = {
+ plain: function(){},
+ login: function(){},
+ cram_md5: function(){}
+};
+
/*-----------------------------------------------
Sending mail
-----------------------------------------------*/
Client.prototype.mail = function(from_addr){
var client = this;
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 SMTPError["unhandled"];
+ throw new SMTPError["unhandled"];
break;
}
});
};
+
Client.prototype.rcpt = function(to_addr){
var client = this;
-
+ /*
if( ! client.mail_from){
throw SMTPError["503"];
return;
- }
+ }*/
this.get("RCPT TO:<"+to_addr+">", function(packet){
switch(packet.status){
case "250":
+ client.rcpt_to = true;
+ break;
+ default:
+ throw SMTPError["unhandled"];
+ break;
+ }
+ });
+};
+
+Client.prototype.data = function(data){
+ var client = this;
+ /*
+ if( ! client.rcpt_to ){
+ throw SMTPError["503"];
+ return;
+ }*/
+
+ this.get("DATA", function(packet){
+ switch(packet.status){
+ case "354":
+ client.writeline(data);
+ client.get(".", function(packet){
+ switch(packet.status){
+ case "250":
+ break;
+ default:
+ sys.puts(packet.status);
+ throw SMTPError["unhandled"];
+ break;
+ }
+ });
break;
default:
throw SMTPError["unhandled"];
break;
}
});
};
-Client.prototype.data = function(){};
/*-----------------------------------------------
Other Commands
View
@@ -13,7 +13,7 @@ var SMTPErrors = exports;
SMTPErrors.client = {
"unhandled": new Error("Received an unhandled status code"),
// numeric:
- "503": new Error("Bad sequence of commands"),
+ "503": Error("Bad sequence of commands"),
"504": new Error("Command parameter not implemented"),
"550": new Error("Requested action not taken: mailbox unavailable"),
"554": new Error("Transaction failed (Or, in the case of a connection-opening response, \"No SMTP service here\")")
View
@@ -1,41 +1,39 @@
/*===============================================
- File: packetHandler.js
+ File: PacketHandler.js
Author: Micheil Smith
Description:
Handles the parsing and processing of
incoming SMTP packets.
+
+ We use the vendor Queue runner,
+ although, we replace certain parts to
+ make it's use cleaner.
===============================================*/
// Global
var sys = require("sys");
// Local
-var Queue = require("../queue").Queue;
+var Queue = require("../vendor/queue").Queue;
/*-----------------------------------------------
PacketHandler
-----------------------------------------------*/
-var PacketHandler = Queue;
+exports.PacketHandler = Queue;
-PacketHandler.prototype.push = function(data){
+exports.PacketHandler.prototype.push = function(data){
var packets = data.split("\r\n");
- while(packets.length && (packet = packets.shift())){
- if(packet != ""){
- this._stack.push(packet);
-
- if( !this._processing ){
- this._processor();
- }
+ while(packets.length && (packet = packets.shift())){
+ this._stack.push(packet);
+ if( !this._processing ){
+ this._processor();
}
}
};
-PacketHandler.prototype.process = function(){
- var status = 0, data = [], continued = false, signal="other";
+exports.PacketHandler.prototype.process = function(){
+ var status = 0, data = [], continued = false;
while(this._stack.length && (packet = this._stack.shift())){
-
- sys.puts(">> "+packet);
-
if(continued == false){
// not a number, strings are easier to work with as status codes.
status = packet.substr(0,3);
@@ -49,40 +47,12 @@ PacketHandler.prototype.process = function(){
}
}
- switch(status.substr(0,1)){
- case "1":
- signal = "confirm";
- break;
- case "2":
- signal = "ok";
- break;
- case "3":
- signal = "continue";
- break;
- case "4":
- signal = "fail";
- break;
- case "5":
- signal = "error";
- break;
- }
-
this.emit("packet", {
status: status,
data: data
});
- this.emit(signal, {
- status: status,
- data: data
- });
-
if(this._stack.length == 0){
this.emit("idle");
}
-};
-
-/*-----------------------------------------------
- The final export.
------------------------------------------------*/
-exports.PacketHandler = PacketHandler;
+};
View
@@ -0,0 +1,25 @@
+/*===============================================
+ File: eventEmitter.js
+
+ Author: Micheil Smith
+ Description:
+ Various enhancements to node's
+ event.eventEmitter
+===============================================*/
+
+/*-----------------------------------------------
+ Extension to EventEmitter to allow listeners
+ to automatically be remove after being called.
+-----------------------------------------------*/
+if(process.EventEmitter.prototype.addOnce == undefined){
+ process.EventEmitter.prototype.addOnce = function(signal, callback){
+ var EventEmitter = this;
+
+ this.addListener(signal, function(){
+ EventEmitter.removeListener(signal, arguments.callee);
+ callback.apply(this, arguments);
+ });
+
+ return this;
+ };
+}
@@ -7,36 +7,17 @@
messages in a stack / queue loop.
===============================================*/
-
-/*-----------------------------------------------
- Extension to EventEmitter to allow listeners
- to automatically be remove after being called.
------------------------------------------------*/
-if(process.EventEmitter.prototype.addOnce == undefined){
- process.EventEmitter.prototype.addOnce = function(signal, callback){
- var EventEmitter = this;
-
- this.addListener(signal, function(){
- EventEmitter.removeListener(signal, arguments.callee);
- callback.apply(this, arguments);
- });
-
- return this;
- };
-}
-
-
/*-----------------------------------------------
Queue
-----------------------------------------------*/
-var Queue = function(){
+exports.Queue = function(){
this._stack = [];
this._processing = false;
};
-process.inherits(Queue, process.EventEmitter);
+process.inherits(exports.Queue, process.EventEmitter);
-Queue.prototype.push = function(item){
+exports.Queue.prototype.push = function(item){
this._stack.push(item);
this.emit("push", item);
@@ -45,14 +26,14 @@ Queue.prototype.push = function(item){
}
};
-Queue.prototype.pop = function(){
+exports.Queue.prototype.pop = function(){
var item = this._stack.pop();
this.emit("pop", item);
return item;
};
-Queue.prototype.unshift = function(item){
+exports.Queue.prototype.unshift = function(item){
this._stack.unshift(item);
this.emit("unshift", item);
@@ -61,14 +42,14 @@ Queue.prototype.unshift = function(item){
}
};
-Queue.prototype.shift = function(){
+exports.Queue.prototype.shift = function(){
var item = this._stack.shift();
this.emit("shift", item);
return item;
};
-Queue.prototype._processor = function(){
+exports.Queue.prototype._processor = function(){
this._processing = true;
this.process.call(this);
@@ -80,9 +61,7 @@ Queue.prototype._processor = function(){
}
};
-Queue.prototype.process = function(){
+exports.Queue.prototype.process = function(){
var item = this._stack.shift();
this.emit("processed", item);
};
-
-exports.Queue = Queue;
Oops, something went wrong.

0 comments on commit 28829c1

Please sign in to comment.