Permalink
Browse files

Started the modularisation of node-smtp:

- Moved the errors to be standalone, this file will include both client and server error code translations.
- Moved the client code into a separate file, all client related code should be there, same will happen if/when I implement the server.
- Refactored smtp module to provide simple wrappers over the submodules.
  • Loading branch information...
1 parent 3fe4756 commit 2e565f3c4bed9946edf47b14c680191db4239af5 @miksago miksago committed Jan 15, 2010
Showing with 131 additions and 109 deletions.
  1. +5 −109 lib/smtp.js
  2. +111 −0 lib/smtp/client.js
  3. +15 −0 lib/smtp/errors.js
View
@@ -20,129 +20,25 @@
negotiation within node.js
===============================================*/
-// Global
-var sys = require("sys");
-var tcp = require("tcp")
-// Local
-var PacketHandler = require("./smtp/packetHandler").PacketHandler;
-
-/*-----------------------------------------------
- SMTP Error Definitions
------------------------------------------------*/
-var SMTPError = {
- auth: new Error("SMTP Authentication Error"),
- server: new Error("SMTP Server Error (code: 420, 450 or temporary error)"),
- syntax: new Error("SMTP Syntax Error (code: 500)"),
- fatal: new Error("SMTP Fatal Error (code: all 5xx except for 500)"),
- unknown: new Error("SMTP Unknown reply code returned from server"),
- unsupported: new Error("SMTP Command Unsupported on server"),
-
-
- "504": new Error("Command parameter not implemented"),
- "550": new Error("Requested action not taken: mailbox unavailable")
-};
-
/*-----------------------------------------------
SMTP Client
-----------------------------------------------*/
var Client = function(){
- this.socket = null;
- this.packetHandler = new PacketHandler();
- this.esmtp = false;
- this.started = false;
-};
-
-process.inherits(Client, process.EventEmitter);
-
-Client.prototype.connect = function(port, host){
- var client = this;
-
- this.port = port;
- this.host = host;
-
- this.socket = new tcp.createConnection(this.port, this.host);
- this.socket.setEncoding("ascii");
-
- this.packetHandler.addOnce("ok", function(packet){
- if(packet.data[0].indexOf("ESMTP") > -1){
- client.esmtp = true;
- }
-
- client.handshake();
- });
-
- this.socket.addListener("receive", function(data){
- client.packetHandler.push(data);
- });
+ var SMTPClient = require("./smtp/client").Client;
+ return new SMTPClient(arguments);
};
-Client.prototype.writeline = function(line){
- sys.puts("\033[0;32m"+line+"\033[0m")
- this.socket.send(line+"\r\n");
-};
-
-Client.prototype.get = function(data, callback){
- this.packetHandler.addOnce("packet", callback);
- this.writeline(data);
-};
-
-Client.prototype.handshake = function(){
- if(this.esmtp){
- this.ehlo();
- } else {
- this.helo();
- }
-};
-
-/* protocol implementation: */
-Client.prototype.helo = function(){
- var client = this;
-
- this.get("HELO "+this.host, function(packet){
- switch(packet.status){
- case "250":
- client.capabilities = packet.data;
- client.started = true;
- break;
- case "504":
- throw SMTPError["504"];
- break;
- case "550":
- throw SMTPError["550"];
- break;
- }
- });
-};
-
-Client.prototype.ehlo = function(){
- var client = this;
-
- this.get("EHLO "+this.host, function(packet){
- switch(packet.status){
- case "250":
- client.capabilities = packet.data;
- client.started = true;
- break;
- case "504":
- throw SMTPError["504"];
- break;
- case "550":
- throw SMTPError["550"];
- break;
- }
- });
-};
-
-
/*-----------------------------------------------
SMTP Server (unimplemented, but possible)
-----------------------------------------------*/
var Server = function(){
throw new Error("SMTP Server Not Implemented");
- return;
};
+/*-----------------------------------------------
+ Exports
+-----------------------------------------------*/
exports.Client = Client;
exports.Server = Server;
View
@@ -0,0 +1,111 @@
+/*===============================================
+ File: client.js
+
+ Author: Micheil Smith
+ Description:
+ The SMTP Client
+===============================================*/
+
+
+// Global
+var sys = require("sys");
+var tcp = require("tcp");
+
+// Local
+var SMTPError = require("./errors").client;
+var PacketHandler = require("./packetHandler").PacketHandler;
+
+
+/*-----------------------------------------------
+ SMTP Client
+-----------------------------------------------*/
+var Client = function(){
+ this.socket = null;
+ this.packetHandler = new PacketHandler();
+ this.esmtp = false;
+ this.started = false;
+};
+
+process.inherits(Client, process.EventEmitter);
+
+Client.prototype.connect = function(port, host){
+ var client = this;
+
+ this.port = port;
+ this.host = host;
+
+ this.socket = new tcp.createConnection(this.port, this.host);
+ this.socket.setEncoding("ascii");
+
+ this.packetHandler.addOnce("ok", function(packet){
+ if(packet.data[0].indexOf("ESMTP") > -1){
+ client.esmtp = true;
+ }
+
+ client.handshake();
+ });
+
+ this.socket.addListener("receive", function(data){
+ client.packetHandler.push(data);
+ });
+};
+
+Client.prototype.writeline = function(line){
+ sys.puts("\033[0;32m"+line+"\033[0m")
+ this.socket.send(line+"\r\n");
+};
+
+Client.prototype.get = function(data, callback){
+ this.packetHandler.addOnce("packet", callback);
+ this.writeline(data);
+};
+
+Client.prototype.handshake = function(){
+ if(this.esmtp){
+ this.ehlo();
+ } else {
+ this.helo();
+ }
+};
+
+/* protocol implementation: */
+Client.prototype.helo = function(){
+ var client = this;
+
+ this.get("HELO "+this.host, function(packet){
+ switch(packet.status){
+ case "250":
+ client.capabilities = packet.data;
+ client.started = true;
+ break;
+ case "504":
+ throw SMTPError["504"];
+ break;
+ case "550":
+ throw SMTPError["550"];
+ break;
+ }
+ });
+};
+
+Client.prototype.ehlo = function(){
+ var client = this;
+
+ this.get("EHLO "+this.host, function(packet){
+ switch(packet.status){
+ case "250":
+ client.capabilities = packet.data;
+ client.started = true;
+ break;
+ case "504":
+ throw SMTPError["504"];
+ break;
+ case "550":
+ throw SMTPError["550"];
+ break;
+ }
+ });
+};
+
+
+exports.Client = Client;
View
@@ -0,0 +1,15 @@
+/*===============================================
+ File: errors.js
+
+ Author: Micheil Smith
+ Description:
+ All the different client (and server)
+ error codes to translate to human text.
+===============================================*/
+
+var SMTPErrors = exports;
+
+SMTPErrors.client = {
+ "504": new Error("Command parameter not implemented"),
+ "550": new Error("Requested action not taken: mailbox unavailable")
+};

0 comments on commit 2e565f3

Please sign in to comment.