<|start_filename|>plugin/rules.js<|end_filename|>
/**
 * Created by Nicolas on 10/25/15.
 */
(function (Rules) {
    'use strict';

    var database = require('./database'),
        logger   = require('./logger'),
        nodebb   = require('./nodebb');

    var cache = nodebb.cache;

    var rulesList = [];

    Rules.invalidate = function (done) {
        database.getRules(function (error, rules) {
            if (error) {
                return done(error);
            }

            logger.log('verbose', 'Updating rules...');

            // Re-compile regular expressions
            var i, len = rules.length, rule, ruleEntity;
            rulesList.length = 0;
            for (i = 0; i < len; ++i) {
                rule = rules[i];
                try {
                    ruleEntity = {
                        match      : new RegExp(rule.regex, "g"),
                        replacement: rule.replacement
                    };
                    rulesList.push(ruleEntity);
                } catch (e) {
                    console.error('Rule is skipped', e);
                }
            }

            cache.reset();

            logger.log('verbose', 'Updating rule list, total rules: %d', rulesList.length);

            done();
        });
    };

    Rules.parse = function (content, done) {
        if (content) {
            var i = 0, len = rulesList.length, rule;

            for (i; i < len; ++i) {
                rule = rulesList[i];
                content = content.replace(rule.match, rule.replacement);
            }

            done(null, content);
        } else {
            done(null, content);
        }
    };

})(module.exports);


<|start_filename|>plugin/index.js<|end_filename|>
(function (Plugin) {
    'use strict';

    var async   = require('async'),

        filters = require('./filters'),
        rules   = require('./rules'),
        sockets = require('./sockets');

    //NodeBB list of Hooks: https://github.com/NodeBB/NodeBB/wiki/Hooks
    Plugin.hooks = {
        filters: filters,
        statics: {
            load: function (params, callback) {
                var router      = params.router,
                    middleware  = params.middleware,
                    controllers = params.controllers,
                    pluginUri   = '/admin/plugins/embed',
                    apiUri      = '/api' + pluginUri,
                    renderAdmin = function (req, res, next) {
                        res.render(
                            'admin/plugins/embed', {}
                        );
                    };

                router.get(pluginUri, middleware.admin.buildHeader, renderAdmin);
                router.get(apiUri, renderAdmin);

                async.series([
                    async.apply(sockets.init),
                    async.apply(rules.invalidate)
                ], callback);
            }
        }
    };

})(module.exports);

<|start_filename|>plugin/sockets.js<|end_filename|>
(function (Sockets) {
    'use strict';

    var constants  = require('./constants'),
        controller = require('./controller'),
        nodebb     = require('./nodebb');

    var adminSockets  = nodebb.adminSockets,
        serverSockets = nodebb.serverSockets,
        emitNamespace = 'admin.plugins.' + constants.SOCKET_NAMESPACE + '.';

    Sockets.init = function (callback) {
        adminSockets[constants.SOCKET_NAMESPACE] = {};

        //Acknowledgements
        adminSockets[constants.SOCKET_NAMESPACE].defaultRulesInstall = Sockets.defaultRulesInstall;
        adminSockets[constants.SOCKET_NAMESPACE].embedRulesGet = Sockets.embedRulesGet;
        adminSockets[constants.SOCKET_NAMESPACE].ruleCreate = Sockets.ruleCreate;
        adminSockets[constants.SOCKET_NAMESPACE].ruleDelete = Sockets.ruleDelete;
        adminSockets[constants.SOCKET_NAMESPACE].ruleSave = Sockets.ruleSave;

        callback();
    };

    Sockets.defaultRulesInstall = function (socket, payload, callback) {
        controller.installDefaultRules(callback);
    };

    Sockets.embedRulesGet = function (socket, payload, callback) {
        controller.getAllRules(callback);
    };

    Sockets.emit = function (eventName, payload) {
        serverSockets.emit(emitNamespace + eventName, payload);
    };

    Sockets.ruleCreate = function (socket, payload, callback) {
        controller.createRule(payload, callback);
    };

    Sockets.ruleDelete = function (socket, payload, callback) {
        controller.deleteRule(payload, callback);
    };

    Sockets.ruleSave = function (socket, payload, callback) {
        controller.saveRule(payload, callback);
    };

})(module.exports);

<|start_filename|>plugin/logger.js<|end_filename|>
/**
 * Created by Nicolas on 10/25/15.
 */
(function (Module) {
    'use strict';

    var winston = require('winston');

    Module.exports = new (winston.Logger)({
        transports: [
            new (winston.transports.Console)({
                colorize : true,
                timestamp: function () {
                    var date = new Date();
                    return date.getDate() + '/' + (date.getMonth() + 1) + ' ' + date.toTimeString().substr(0, 5) + ' [' + global.process.pid + ']';
                },
                level    : global.env === 'production' ? 'info' : 'verbose',
                label    : 'plugins/embed'
            })
        ]
    });

})(module);