<commit_before>'use strict';
// load all defined plugins

// node built-ins
var fs          = require('fs');
var path        = require('path');
var vm          = require('vm');

// npm modules
var constants   = require('haraka-constants');

// local modules
var logger      = require('./logger');
var states      = require('./connection').states;
exports.config  = require('./config');

exports.registered_hooks = {};
exports.registered_plugins = {};
exports.plugin_list = [];

var order = 0;

function Plugin (name) {
    this.name = name;
    this.base = {};
    this.timeout = get_timeout(name);
    this._get_plugin_path();
    this.config = this._get_config();
    this.hooks = {};
}

exports.shutdown_plugins = function () {
    for (var i in exports.registered_plugins) {
        if (exports.registered_plugins[i].shutdown) {
            exports.registered_plugins[i].shutdown();
        }
    }
}

process.on('message', function (msg) {
    if (msg.event && msg.event == 'plugins.shutdown') {
        logger.loginfo("[plugins] Shutting down plugins");
        exports.shutdown_plugins();
    }
});

Plugin.prototype.haraka_require = function (name) {
    return require('./' + name);
};

Plugin.prototype.core_require = Plugin.prototype.haraka_require;

function plugin_search_paths (prefix, name) {
    return [
        path.resolve(prefix, 'plugins', name + '.js'),
        path.resolve(prefix, 'node_modules', 'haraka-plugin-' + name, 'package.json'),
    ];
}

Plugin.prototype._get_plugin_path = function () {
    var plugin = this;
    /* From https://github.com/haraka/Haraka/pull/1278#issuecomment-168856528
    In Development mode, or install via a plain "git clone":

        Plain plugin in plugins/ folder
        Plugin in a folder in plugins/<name>/ folder. Contains a package.json.
        Plugin in node_modules. Contains a package.json file.

    In "installed" mode (via haraka -i <path>):
<commit_msg>add an additional plugin search path to account for the module-based plugins that live in the same node_modules folder as the Haraka installed via NPM (#1805)<commit_after>'use strict';
// load all defined plugins

// node built-ins
var fs          = require('fs');
var path        = require('path');
var vm          = require('vm');

// npm modules
var constants   = require('haraka-constants');

// local modules
var logger      = require('./logger');
var states      = require('./connection').states;
exports.config  = require('./config');

exports.registered_hooks = {};
exports.registered_plugins = {};
exports.plugin_list = [];

var order = 0;

function Plugin (name) {
    this.name = name;
    this.base = {};
    this.timeout = get_timeout(name);
    this._get_plugin_path();
    this.config = this._get_config();
    this.hooks = {};
}

exports.shutdown_plugins = function () {
    for (var i in exports.registered_plugins) {
        if (exports.registered_plugins[i].shutdown) {
            exports.registered_plugins[i].shutdown();
        }
    }
}

process.on('message', function (msg) {
    if (msg.event && msg.event == 'plugins.shutdown') {
        logger.loginfo("[plugins] Shutting down plugins");
        exports.shutdown_plugins();
    }
});

Plugin.prototype.haraka_require = function (name) {
    return require('./' + name);
};

Plugin.prototype.core_require = Plugin.prototype.haraka_require;

function plugin_search_paths (prefix, name) {
    return [
        path.resolve(prefix, 'plugins', name + '.js'),
        path.resolve(prefix, 'node_modules', 'haraka-plugin-' + name, 'package.json'),
        path.resolve(prefix, '..', 'haraka-plugin-' + name, 'package.json')
    ];
}

Plugin.prototype._get_plugin_path = function () {
    var plugin = this;
    /* From https://github.com/haraka/Haraka/pull/1278#issuecomment-168856528
    In Development mode, or install via a plain "git clone":

        Plain plugin in plugins/ folder
        Plugin in a folder in plugins/<name>/ folder. Contains a package.json.
        Plugin in node_modules. Contains a package.json file.

    In "installed" mode (via haraka -i <path>):

{'fraction_non_alphanumeric': 0.14226146676477802, 'fraction_numerical': 0.007848908511160166, 'mean_word_length': 3.0820820820820822, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2033953', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1196, 'n_words': 309}