// Generated by CoffeeScript 1.7.1 /* Components Manager ================== > The components manager takes care of the dynamic JS modules and the rules. > Event Poller and Action Invoker modules are loaded as strings and stored in the database, > then compiled into node modules and rules and used in the engine and event poller. */ (function() { var commandFunctions, db, dynmod, events, exports, forgeModule, fs, getModuleParams, getModules, hasRequiredParams, path, vm; db = require('./persistence'); dynmod = require('./dynamic-modules'); fs = require('fs'); vm = require('vm'); path = require('path'); events = require('events'); /* Module call ----------- Initializes the Components Manager and constructs a new Event Emitter. @param {Object} args */ exports = module.exports = (function(_this) { return function(args) { _this.log = args.logger; _this.ee = new events.EventEmitter(); db(args); dynmod(args); return module.exports; }; })(this); /* Add an event handler (eh) for a certain event (evt). Current events are: - init: as soon as an event handler is added, the init events are emitted for all existing rules. - newRule: If a new rule is activated, the newRule event is emitted @public addListener ( *evt, eh* ) @param {String} evt @param {function} eh */ exports.addListener = (function(_this) { return function(evt, eh) { _this.ee.addListener(evt, eh); if (evt === 'init') { return db.getRules(function(err, obj) { var id, rule, _results; _results = []; for (id in obj) { rule = obj[id]; _results.push(_this.ee.emit('init', rule)); } return _results; }); } }; })(this); /* Processes a user request coming through the request-handler. - `user` is the user object as it comes from the DB. - `oReq` is the request object that contains: - `command` as a string - `payload` an optional stringified JSON object The callback function `callback( obj )` will receive an object containing the HTTP response code and a corresponding message. @public processRequest ( *user, oReq, callback* ) @param {Object} user @param {Object} oReq @param {function} callback */ exports.processRequest = function(user, oReq, callback) { var dat, err; if (!oReq.payload) { oReq.payload = '{}'; } try { dat = JSON.parse(oReq.payload); } catch (_error) { err = _error; return callback({ code: 404, message: 'You had a strange payload in your request!' }); } if (commandFunctions[oReq.command]) { return commandFunctions[oReq.command](user, dat, callback); } else { return callback({ code: 404, message: 'What do you want from me?' }); } }; hasRequiredParams = function(arrParams, oPayload) { var answ, param, _i, _len; answ = { code: 400, message: "Your request didn't contain all necessary fields! id and params required" }; for (_i = 0, _len = arrParams.length; _i < _len; _i++) { param = arrParams[_i]; if (!oPayload[param]) { return answ; } } answ.code = 200; answ.message = 'All required properties found'; return answ; }; getModules = function(user, oPayload, dbMod, callback) { return dbMod.getAvailableModuleIds(user.username, function(err, arrNames) { var answReq, fGetFunctions, id, oRes, sem, _i, _len, _results; oRes = {}; answReq = function() { return callback({ code: 200, message: JSON.stringify(oRes) }); }; sem = arrNames.length; if (sem === 0) { return answReq(); } else { fGetFunctions = (function(_this) { return function(id) { return dbMod.getModule(id, function(err, oModule) { if (oModule) { oRes[id] = JSON.parse(oModule.functions); } if (--sem === 0) { return answReq(); } }); }; })(this); _results = []; for (_i = 0, _len = arrNames.length; _i < _len; _i++) { id = arrNames[_i]; _results.push(fGetFunctions(id)); } return _results; } }); }; getModuleParams = function(user, oPayload, dbMod, callback) { var answ; answ = hasRequiredParams(['id'], oPayload); if (answ.code !== 200) { return callback(answ); } else { return dbMod.getModuleParams(oPayload.id, function(err, oPayload) { answ.message = oPayload; return callback(answ); }); } }; forgeModule = (function(_this) { return function(user, oPayload, dbMod, callback) { var answ; answ = hasRequiredParams(['id', 'params', 'lang', 'data'], oPayload); if (answ.code !== 200) { return callback(answ); } else { return dbMod.getModule(oPayload.id, function(err, mod) { var cm, funcs, id, name, src, _ref; if (mod) { answ.code = 409; answ.message = 'Event Poller module name already existing: ' + oPayload.id; } else { src = oPayload.data; cm = dynmod.compileString(src, user.username, oPayload.id, {}, oPayload.lang); answ = cm.answ; if (answ.code === 200) { funcs = []; _ref = cm.module; for (name in _ref) { id = _ref[name]; funcs.push(name); } _this.log.info("CM | Storing new module with functions " + (funcs.join())); answ.message = "Event Poller module successfully stored! Found following function(s): " + funcs; oPayload.functions = JSON.stringify(funcs); dbMod.storeModule(user.username, oPayload); if (oPayload["public"] === 'true') { dbMod.publish(oPayload.id); } } } return callback(answ); }); } }; })(this); commandFunctions = { get_event_pollers: function(user, oPayload, callback) { return getModules(user, oPayload, db.eventPollers, callback); }, get_action_invokers: function(user, oPayload, callback) { return getModules(user, oPayload, db.actionInvokers, callback); }, get_event_poller_params: function(user, oPayload, callback) { return getModuleParams(user, oPayload, db.eventPollers, callback); }, get_action_invoker_params: function(user, oPayload, callback) { return getModuleParams(user, oPayload, db.actionInvokers, callback); }, forge_event_poller: function(user, oPayload, callback) { return forgeModule(user, oPayload, db.eventPollers, callback); }, forge_action_invoker: function(user, oPayload, callback) { return forgeModule(user, oPayload, db.actionInvokers, callback); }, get_rules: function(user, oPayload, callback) { return console.log('CM | Implement get_rules'); }, forge_rule: (function(_this) { return function(user, oPayload, callback) { var answ; answ = hasRequiredParams(['id', 'event', 'conditions', 'actions'], oPayload); if (answ.code !== 200) { return callback(answ); } else { return db.getRule(oPayload.id, function(err, oExisting) { var arrParams, id, params, rule, strRule; if (oExisting !== null) { answ = { code: 409, message: 'Rule name already existing!' }; } else { rule = { id: oPayload.id, event: oPayload.event, conditions: oPayload.conditions, actions: oPayload.actions }; strRule = JSON.stringify(rule); db.storeRule(rule.id, strRule); db.linkRule(rule.id, user.username); db.activateRule(rule.id, user.username); if (oPayload.event_params) { db.eventPollers.storeUserParams(ep.module, user.username, oPayload.event_params); } arrParams = oPayload.action_params; for (id in arrParams) { params = arrParams[id]; db.actionInvokers.storeUserParams(id, user.username, JSON.stringify(params)); } _this.ee.emit('newRule', strRule); answ = { code: 200, message: 'Rule stored and activated!' }; } return callback(answ); }); } }; })(this) }; }).call(this);