Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jul 8, 2013
2 parents 96c9611 + 0ac08a4 commit 9d3bf7c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -133,6 +133,7 @@ The following connection options can be used with `simplesmtp.connect`:
* **ignoreTLS** - ignore server support for STARTTLS
* **tls** - optional options object for `tls.connect`, also applies to STARTTLS. For example `rejectUnauthorized` is set to `false` by default. You can override this option by setting `tls: {rejectUnauthorized: true}`
* **debug** - output client and server messages to console
* **logFile** - optional filename where communication with remote server has to be logged
* **instanceId** - unique instance id for debugging (will be output console with the messages)
* **localAddress** - local interface to bind to for network connections (needs Node.js >= 0.11.3 for working with tls)

Expand Down Expand Up @@ -313,6 +314,7 @@ The following connection options can be used with `simplesmtp.connect`:
* **auth** - authentication object `{user:"...", pass:"..."}` or `{XOAuthToken:"base64data"}`
* **ignoreTLS** - ignore server support for STARTTLS
* **debug** - output client and server messages to console
* **logFile** - optional filename where communication with remote server has to be logged
* **maxConnections** - how many connections to keep in the pool (defaults to 5)
* **localAddress** - local interface to bind to for network connections (needs Node.js >= 0.11.3 for working with tls)

Expand Down
36 changes: 35 additions & 1 deletion lib/client.js
Expand Up @@ -7,7 +7,8 @@ var Stream = require("stream").Stream,
tls = require("tls"),
oslib = require("os"),
xoauth2 = require("xoauth2"),
crypto = require("crypto");
crypto = require("crypto"),
fs = require('fs');

// expose to the world
module.exports = function(port, host, options){
Expand All @@ -27,6 +28,7 @@ module.exports = function(port, host, options){
* <li><b>ignoreTLS</b> - ignore server support for STARTTLS</li>
* <li><b>tls</b> - options for createCredentials</li>
* <li><b>debug</b> - output client and server messages to console</li>
* <li><b>logFile</b> - output client and server messages to file</li>
* <li><b>instanceId</b> - unique instance id for debugging</li>
* <li><b>localAddress</b> - outbound address to bind to (see: http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener)</li>
* </ul>
Expand Down Expand Up @@ -271,6 +273,11 @@ SMTPClient.prototype._onData = function(chunk){
console.log("SERVER"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+str.replace(/\r?\n/g,"\n "));
}
if(this.options.logFile){
this.log("SERVER"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+str.replace(/\r?\n/g,"\n ")
);
}

if(typeof this._currentAction == "function"){
this._currentAction.call(this, str);
Expand Down Expand Up @@ -351,6 +358,10 @@ SMTPClient.prototype.write = function(chunk){
console.log("CLIENT (DATA)"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+chunk.toString().trim().replace(/\n/g,"\n "));
}
if(this.options.logFile){
this.log("CLIENT (DATA)"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+chunk.toString().trim().replace(/\n/g,"\n "));
}

// pass the chunk to the socket
return this.socket.write(chunk);
Expand Down Expand Up @@ -401,6 +412,10 @@ SMTPClient.prototype.sendCommand = function(str){
console.log("CLIENT"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+(str || "").toString().trim().replace(/\n/g,"\n "));
}
if(this.options.logFile){
this.log("CLIENT"+(this.options.instanceId?" "+
this.options.instanceId:"")+":\n└──"+(str || "").toString().trim().replace(/\n/g,"\n "));
}
this.socket.write(new Buffer(str+"\r\n", "utf-8"));
};

Expand All @@ -419,6 +434,9 @@ SMTPClient.prototype.close = function(){
if(this.options.debug){
console.log("Closing connection to the server");
}
if(this.options.logFile){
this.log("Closing connection to the server");
}
if(this.socket && this.socket.socket && this.socket.socket.end && !this.socket.socket.destroyed){
this.socket.socket.end();
}
Expand Down Expand Up @@ -626,6 +644,9 @@ SMTPClient.prototype._actionSTARTTLS = function(str){
if(this.options.debug){
console.log("Connection secured");
}
if(this.options.logFile){
this.log("Connection secured");
}

if(secured){
// restart session
Expand Down Expand Up @@ -868,3 +889,16 @@ SMTPClient.prototype._actionStream = function(str){
this._currentAction = this._actionIdle;
process.nextTick(this.emit.bind(this, "idle"));
};

/**
* <p>Log debugs to given file</p>
*
* @param {String} str Log message
*/
SMTPClient.prototype.log = function(str) {
fs.appendFile(this.options.logFile, str + "\n", function(err) {
if (err) {
console.log('Log write failed. Data to log: ' + str);
}
});
};
1 change: 1 addition & 0 deletions lib/pool.js
Expand Up @@ -151,6 +151,7 @@ SMTPConnectionPool.prototype._createConnection = function(){
instanceId: ++this._idgen,
debug: !!this.options.debug,
localAddress: this.options.localAddress,
logFile: this.options.logFile,
ignoreTLS: !!this.options.ignoreTLS,
tls: this.options.tls || false,
auth: this.options.auth || false,
Expand Down

0 comments on commit 9d3bf7c

Please sign in to comment.