diff --git a/agents/meshcore.min.js b/agents/meshcore.min.js index b43ccd66..1bbd93e5 100644 --- a/agents/meshcore.min.js +++ b/agents/meshcore.min.js @@ -1491,6 +1491,36 @@ function createMeshCore(agent) // Unknown action, ignore it. break; } + } else if (this.httprequest.protocol == 7) { // plugin data exchange + var cmd = null; + try { cmd = JSON.parse(data); } catch (e) { }; + if (cmd == null) { return; } + if ((cmd.ctrlChannel == '102938') || ((cmd.type == 'offer') && (cmd.sdp != null))) { onTunnelControlData(cmd, this); return; } // If this is control data, handle it now. + if (cmd.action == undefined) { return; } + //sendConsoleText('CMD: ' + JSON.stringify(cmd)); + + if ((cmd.path != null) && (process.platform != 'win32') && (cmd.path[0] != '/')) { cmd.path = '/' + cmd.path; } // Add '/' to paths on non-windows + //console.log(objToString(cmd, 0, ' ')); + + switch (cmd.action) { + case 'plugin': { + try { + require(cmd.plugin).consoleaction(cmd, null, null, this); + } catch (e) { + /*var fs = require('fs'); + var logStream = fs.createWriteStream('log.txt', {'flags': 'a'}); + logStream.write('\nCouldnt load plugin '+e+e.stack); + logStream.end('\n')*/ + throw e; + } + + break; + } + default: { + // probably shouldn't happen, but just in case this feature is expanded + } + } + } //sendConsoleText("Got tunnel #" + this.httprequest.index + " data: " + data, this.httprequest.sessionid); } diff --git a/meshuser.js b/meshuser.js index 47f225f2..968248b7 100644 --- a/meshuser.js +++ b/meshuser.js @@ -2963,6 +2963,17 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } break; } + case 'plugin': { + command.userid = user._id; + + if (command.routeToNode === true) { + routeCommandToNode(command); + } else { + // TODO + } + + break; + } default: { // Unknown user action console.log('Unknown action from user ' + user.name + ': ' + command.action + '.'); diff --git a/pluginHandler.js b/pluginHandler.js index 6688e485..7a621009 100644 --- a/pluginHandler.js +++ b/pluginHandler.js @@ -33,7 +33,7 @@ module.exports.pluginHandler = function (parent) { obj.plugins[plugin] = require(obj.pluginPath + '/' + plugin + '/' + plugin + '.js')[plugin](obj); obj.exports[plugin] = obj.plugins[plugin].exports; } catch (e) { - console.log("Error loading plugin: " + plugin + " (" + e + "). It has been disabled"); + console.log("Error loading plugin: " + plugin + " (" + e + "). It has been disabled.", e.stack); } } }); @@ -49,7 +49,27 @@ module.exports.pluginHandler = function (parent) { str += ' obj.'+ p +'.'+ l + ' = '+ obj.plugins[p][l].toString()+'\r\n'; } } - str += 'return obj; };\r\n'; + + str += 'obj.enabled = '+ obj.enabled +';\r\n'; + str += `obj.onDeviceRefeshEnd = function(nodeid, panel, refresh, event) { + for (const p of Object.keys(obj)) { + if (typeof obj[p].onDeviceRefreshEnd == 'function') { + obj[p].onDeviceRefreshEnd(nodeid, panel, refresh, event); + } + } + }; + obj.registerPluginTab = function(pluginRegInfo) { + var d = pluginRegInfo(); + QA('p19headers', ''+d.tabTitle+''); + }; + obj.callPluginPage = function(id) { + var pages = Q('p19pages').querySelectorAll("#p19pages>div"); + for (const i of pages) { + i.style.display = 'none'; + } + QV(id, true); + }; + return obj; };`; return str; } diff --git a/views/default.handlebars b/views/default.handlebars index c301bd3f..56307eff 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -844,9 +844,23 @@