<commit_before>// rcpt_to.access plugin

exports.register = function() {
    var i;
    var config = this.config.get('rcpt_to.access.ini');
    this.wl = this.config.get('rcpt_to.access.whitelist', 'list');
    this.bl = this.config.get('rcpt_to.access.blacklist', 'list');
    this.deny_msg = config.general && (config.general['deny_msg'] ||
        'Connection rejected.');
    var white_regex =
        this.config.get('rcpt_to.access.whitelist_regex', 'list');
    var black_regex =
        this.config.get('rcpt_to.access.blacklist_regex', 'list');

    if (white_regex.length) {
        this.wlregex = new RegExp('^(?:' + white_regex.join('|') + ')$', 'i');
    }

    if (black_regex.length) {
        this.blregex = new RegExp('^(?:' + black_regex.join('|') + ')$', 'i');
    }

    this.register_hook('rcpt', 'rcpt_to_access');
}

exports.rcpt_to_access = function(next, connection, params) {
    var plugin = this;
    var rcpt_to = params[0].address();

    // address whitelist checks
    if (rcpt_to) {
        connection.logdebug(plugin, 'checking ' + rcpt_to +
            ' against rcpt_to.access.whitelist');

        if (_in_whitelist(connection, plugin, rcpt_to)) {
            connection.logdebug(plugin, "Allowing " + rcpt_to);
            return next();
        }
    }

    // address blacklist checks
    if (rcpt_to) {
        connection.logdebug(plugin, 'checking ' + rcpt_to +
            ' against rcpt_to.access.blacklist');

        if (_in_blacklist(connection, plugin, rcpt_to)) {
            connection.logdebug(plugin, "Rejecting, matched: " + rcpt_to);
            return next(DENY, rcpt_to + ' ' + plugin.deny_msg);
        }
    }

    return next();
}

function _in_whitelist(connection, plugin, address) {
    var i;
    for (i in plugin.wl) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.wl[i]);

        if (plugin.wl[i] === address) {
            return 1;
        }
    }

    if (plugin.wlregex) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.wlregex.source);

        if (address.match(plugin.wlregex)) {
            return 1;
        }
    }

    return 0;
}

function _in_blacklist(connection, plugin, address) {
    var i;
    for (i in plugin.bl) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.bl[i]);

        if (plugin.bl[i] === address) {
            return 1;
        }
    }

    if (plugin.blregex) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.blregex.source);

        if (address.match(plugin.blregex)) {
            return 1;
        }
    }

    return 0;
}<commit_msg>rt_access: added results storage<commit_after>// rcpt_to.access plugin

exports.register = function() {
    var i;
    var config = this.config.get('rcpt_to.access.ini');
    this.wl = this.config.get('rcpt_to.access.whitelist', 'list');
    this.bl = this.config.get('rcpt_to.access.blacklist', 'list');
    this.deny_msg = config.general && (config.general['deny_msg'] ||
        'Connection rejected.');
    var white_regex =
        this.config.get('rcpt_to.access.whitelist_regex', 'list');
    var black_regex =
        this.config.get('rcpt_to.access.blacklist_regex', 'list');

    if (white_regex.length) {
        this.wlregex = new RegExp('^(?:' + white_regex.join('|') + ')$', 'i');
    }

    if (black_regex.length) {
        this.blregex = new RegExp('^(?:' + black_regex.join('|') + ')$', 'i');
    }

    this.register_hook('rcpt', 'rcpt_to_access');
}

exports.rcpt_to_access = function(next, connection, params) {
    var plugin = this;
    var rcpt_to = params[0].address();

    // address whitelist checks
    if (!rcpt_to) {
        connection.transaction.results.add(plugin, {skip: 'null rcpt', emit: true});
        return next();
    }

    connection.logdebug(plugin, 'checking ' + rcpt_to +
        ' against rcpt_to.access.whitelist');

    if (_in_whitelist(connection, plugin, rcpt_to)) {
        connection.logdebug(plugin, "Allowing " + rcpt_to);
        connection.transaction.results.add(plugin, {pass: 'whitelisted', emit: true});
        return next();
    }

    // address blacklist checks
    connection.logdebug(plugin, 'checking ' + rcpt_to +
        ' against rcpt_to.access.blacklist');

    if (_in_blacklist(connection, plugin, rcpt_to)) {
        connection.logdebug(plugin, "Rejecting, matched: " + rcpt_to);
        connection.transaction.results.add(plugin, {fail: 'blacklisted', emit: true});
        return next(DENY, rcpt_to + ' ' + plugin.deny_msg);
    }

    connection.transaction.results.add(plugin, {pass: 'unlisted', emit: true});
    return next();
};

function _in_whitelist(connection, plugin, address) {
    var i;
    for (i in plugin.wl) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.wl[i]);

        if (plugin.wl[i] === address) {
            return true;
        }
    }

    if (plugin.wlregex) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.wlregex.source);

        if (address.match(plugin.wlregex)) {
            return true;
        }
    }

    return false;
}

function _in_blacklist(connection, plugin, address) {
    var i;
    for (i in plugin.bl) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.bl[i]);

        if (plugin.bl[i] === address) {
            return true;
        }
    }

    if (plugin.blregex) {
        connection.logdebug(plugin, 'checking ' + address + ' against ' +
            plugin.blregex.source);

        if (address.match(plugin.blregex)) {
            return true;
        }
    }

    return false;
}
{'fraction_non_alphanumeric': 0.14990479487623334, 'fraction_numerical': 0.0013848018002423402, 'mean_word_length': 2.41086186540732, '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': '3711660', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1849, 'n_words': 352}