Permalink
Browse files

Allow responding dataReady events with custom error message

  • Loading branch information...
andris9 committed Jan 16, 2014
1 parent ac05aea commit 4a2351cea49d00d1db7a882b0125f54dfa9d65e6
Showing with 39 additions and 19 deletions.
  1. +34 −12 examples/size.js
  2. +4 −6 lib/server.js
  3. +1 −1 package.json
View
@@ -3,41 +3,63 @@ var simplesmtp = require("../index"),
// Example for http://tools.ietf.org/search/rfc1870
-var allowedRecipientDomains = ["node.ee", "neti.ee"];
+var maxMessageSize = 10;
var smtp = simplesmtp.createServer({
- maxSize: 5000, // maxSize must be set in order to support SIZE
- disableDNSValidation: true
+ maxSize: maxMessageSize, // maxSize must be set in order to support SIZE
+ disableDNSValidation: true,
+ debug: true
});
smtp.listen(25);
+// Set up sender validation function
+smtp.on("validateSender", function(connection, email, done){
+ console.log(1, connection.messageSize, maxMessageSize)
+ // SIZE value can be found from connection.messageSize
+ if(connection.messageSize > maxMessageSize){
+ var err = new Error("Max space reached");
+ err.SMTPResponse = "452 This server can only accept messages up to " + maxMessageSize + " bytes";
+ done(err);
+ }else{
+ done();
+ }
+});
+
// Set up recipient validation function
smtp.on("validateRecipient", function(connection, email, done){
-
- // SIZE value can be found from connection.messageSize
- if(typeof connection.messageSize == "number" && connection.messageSize > 100){
+ // Allow only messages up to 100 bytes
+ if(connection.messageSize > 100){
var err = new Error("Max space reached");
- // anything in the SMTPResponse will be reported back to user. If not set, default error message wil, be used
- err.SMTPResponse = "452 Insufficient channel storage: " + email;
+ err.SMTPResponse = "552 Channel size limit exceeded: " + email;
done(err);
}else{
done();
}
});
smtp.on("startData", function(connection){
+ connection.messageSize = 0;
connection.saveStream = fs.createWriteStream("/tmp/message.txt");
});
smtp.on("data", function(connection, chunk){
+ connection.messageSize += chunk.length;
connection.saveStream.write(chunk);
});
smtp.on("dataReady", function(connection, done){
connection.saveStream.end();
- done();
- console.log("Delivered message by " + connection.from +
- " to " + connection.to.join(", ") + ", sent from " + connection.host +
- " (" + connection.remoteAddress + ")");
+ // check if message
+ if(connection.messageSize > maxMessageSize){
+ // mail was too big and therefore ignored
+ var err = new Error("Max fileSize reached");
+ err.SMTPResponse = "552 message exceeds fixed maximum message size";
+ done(err);
+ }else{
+ done();
+ console.log("Delivered message by " + connection.from +
+ " to " + connection.to.join(", ") + ", sent from " + connection.host +
+ " (" + connection.remoteAddress + ")");
+ }
});
View
@@ -323,8 +323,7 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
var self = this,
match,
email,
- domain,
- messageSize;
+ domain;
if(!this.hostNameAppearsAs){
return this.client.send("503 5.5.1 Error: send HELO/EHLO first");
@@ -344,7 +343,7 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
if(this.server.options.maxSize){
mail.replace(/> size=(\d+)\b\s*/i, function(o, size){
- messageSize = Number(size) || 0;
+ self.envelope.messageSize = size;
});
}
@@ -357,7 +356,6 @@ SMTPServerConnection.prototype._onCommandMAIL = function(mail){
}
email = email.substr(0, email.length - domain.length) + domain;
self.envelope.from = email;
- self.envelope.messageSize = messageSize;
self.client.send("250 2.1.0 Ok");
});
};
@@ -588,7 +586,7 @@ SMTPServerConnection.prototype._onCommandHELO = function(host){
*/
SMTPServerConnection.prototype._onCommandEHLO = function(host){
var response = [this.server.options.name+" at your service, ["+
- this.client.remoteAddress+"]", "8BITMIME", "ENHANCEDSTATUSCODES"];
+ this.client.remoteAddress+"]", "8BITMIME", "ENHANCEDSTATUSCODES", "PIPELINING"];
if(this.server.options.maxSize){
response.push("SIZE "+this.server.options.maxSize);
@@ -718,7 +716,7 @@ SMTPServerConnection.prototype._onDataReady = function(){
this.init(true); //reset state, keep auth data
if(err){
- this.client.send("550 "+(err && err.message || "FAILED"));
+ this.client.send(err && err.SMTPResponse || ("550 " + (err && err.message || "FAILED")));
}else{
this.client.send("250 2.0.0 Ok: queued as "+(code || crypto.randomBytes(10).toString("hex")));
}
View
@@ -1,7 +1,7 @@
{
"name": "simplesmtp",
"description": "Simple SMTP server module to create custom SMTP servers",
- "version": "0.3.18",
+ "version": "0.3.18-rfc1870",
"author" : "Andris Reinman",
"maintainers":[
{

0 comments on commit 4a2351c

Please sign in to comment.