<commit_before>"use strict";

var createSMTPServer = require("./server"),
    Stream = require("stream").Stream,
    utillib = require("util"),
    oslib = require("os");

module.exports = function(options, connectionCallback){
    return new SimpleServer(options, connectionCallback);
};

function SimpleServer(options, connectionCallback){
    if(!connectionCallback && typeof options == "function"){
        connectionCallback = options;
        options = undefined;
    }

    this.connectionCallback = connectionCallback;

    this.options = options || {};
    this.initialChunk = true;

    if(!("ignoreTLS" in this.options)){
        this.options.ignoreTLS = true;
    }

    if(!("disableDNSValidation" in this.options)){
        this.options.disableDNSValidation = true;
    }

    this.server = createSMTPServer(options);
    this.listen = this.server.listen.bind(this.server);

    this.server.on("startData", this._onStartData.bind(this));
    this.server.on("data", this._onData.bind(this));
    this.server.on("dataReady", this._onDataReady.bind(this));
}

SimpleServer.prototype._onStartData = function(connection){
    connection._session = new SimpleServerConnection(connection);
    this.connectionCallback(connection._session);
};

SimpleServer.prototype._onData = function(connection, chunk){
    if(this.initialChunk){
        chunk = Buffer.concat([new Buffer(this._generateReceivedHeader(connection) + "\r\n", "utf-8"), chunk]);
        this.initialChunk = false;
    }
    connection._session.emit("data", chunk);
};

SimpleServer.prototype._onDataReady = function(connection, callback){
    connection._session._setCallback(callback);
    connection._session.emit("end");
};

SimpleServer.prototype._generateReceivedHeader = function(connection){
    var parts = [];

    if(connection.host && !connection.host.match(/^\[?\d+\.\d+\.\d+\.\d+\]?$/)){
        parts.push("from " + connection.host);
        parts.push("("+connection.remoteAddress+")");
    }else{
        parts.push("from " + connection.remoteAddress);
    }

    parts.push("by " + getHostName());

    parts.push("with SMTP;");

    parts.push(Date());

    return "Received: " + parts.join(" ");
};

function SimpleServerConnection(connection){
    Stream.call(this);

    this.accepted = false;
    this.rejected = false;

    this._callback = (function(err, code){
        if(err){
            this.rejected = err;
        }else{
            this.accepted = code || true;
        }
    });

    ["from", "to", "host", "remodeAddress"].forEach((function(key){
        if(connection[key]){
            this[key] = connection[key];
        }
    }).bind(this));
}
utillib.inherits(SimpleServerConnection, Stream);

SimpleServerConnection.prototype._setCallback = function(callback){

    if(this.rejected){
        return callback(this.rejected);
    }else if(this.accepted){
        return callback(null, this.accepted !== true ? this.accepted : undefined);
    }else{
        this._callback = callback;
    }

};

SimpleServerConnection.prototype.pause = function(){};

SimpleServerConnection.prototype.resume = function(){};

SimpleServerConnection.prototype.accept = function(code){
    this._callback(null, code);
};

SimpleServerConnection.prototype.reject = function(reason){
    this._callback(new Error(reason || "Rejected"));
};
<commit_msg>fixed: typo causing connection.remoteAddress to be undefined<commit_after>"use strict";

var createSMTPServer = require("./server"),
    Stream = require("stream").Stream,
    utillib = require("util"),
    oslib = require("os");

module.exports = function(options, connectionCallback){
    return new SimpleServer(options, connectionCallback);
};

function SimpleServer(options, connectionCallback){
    if(!connectionCallback && typeof options == "function"){
        connectionCallback = options;
        options = undefined;
    }

    this.connectionCallback = connectionCallback;

    this.options = options || {};
    this.initialChunk = true;

    if(!("ignoreTLS" in this.options)){
        this.options.ignoreTLS = true;
    }

    if(!("disableDNSValidation" in this.options)){
        this.options.disableDNSValidation = true;
    }

    this.server = createSMTPServer(options);
    this.listen = this.server.listen.bind(this.server);

    this.server.on("startData", this._onStartData.bind(this));
    this.server.on("data", this._onData.bind(this));
    this.server.on("dataReady", this._onDataReady.bind(this));
}

SimpleServer.prototype._onStartData = function(connection){
    connection._session = new SimpleServerConnection(connection);
    this.connectionCallback(connection._session);
};

SimpleServer.prototype._onData = function(connection, chunk){
    if(this.initialChunk){
        chunk = Buffer.concat([new Buffer(this._generateReceivedHeader(connection) + "\r\n", "utf-8"), chunk]);
        this.initialChunk = false;
    }
    connection._session.emit("data", chunk);
};

SimpleServer.prototype._onDataReady = function(connection, callback){
    connection._session._setCallback(callback);
    connection._session.emit("end");
};

SimpleServer.prototype._generateReceivedHeader = function(connection){
    var parts = [];

    if(connection.host && !connection.host.match(/^\[?\d+\.\d+\.\d+\.\d+\]?$/)){
        parts.push("from " + connection.host);
        parts.push("("+connection.remoteAddress+")");
    }else{
        parts.push("from " + connection.remoteAddress);
    }

    parts.push("by " + getHostName());

    parts.push("with SMTP;");

    parts.push(Date());

    return "Received: " + parts.join(" ");
};

function SimpleServerConnection(connection){
    Stream.call(this);

    this.accepted = false;
    this.rejected = false;

    this._callback = (function(err, code){
        if(err){
            this.rejected = err;
        }else{
            this.accepted = code || true;
        }
    });

    ["from", "to", "host", "remoteAddress"].forEach((function(key){
        if(connection[key]){
            this[key] = connection[key];
        }
    }).bind(this));
}
utillib.inherits(SimpleServerConnection, Stream);

SimpleServerConnection.prototype._setCallback = function(callback){

    if(this.rejected){
        return callback(this.rejected);
    }else if(this.accepted){
        return callback(null, this.accepted !== true ? this.accepted : undefined);
    }else{
        this._callback = callback;
    }

};

SimpleServerConnection.prototype.pause = function(){};

SimpleServerConnection.prototype.resume = function(){};

SimpleServerConnection.prototype.accept = function(code){
    this._callback(null, code);
};

SimpleServerConnection.prototype.reject = function(reason){
    this._callback(new Error(reason || "Rejected"));
};

{'fraction_non_alphanumeric': 0.15450898826325954, 'fraction_numerical': 0.0002971326697370376, 'mean_word_length': 3.6236263736263736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4374391', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1894, 'n_words': 318}