add groupmessage/grouptoast to meshctrl (#5731)

Signed-off-by: si458 <simonsmith5521@gmail.com>

Simon Smith committed Jan 25, 2024 at 09:44 UTC 18af676c2281ad8aa3fd65c318674c970197ed14
1 file changed +79 -1
meshctrl.js
+79 -1
@@ -16,7 +16,7 @@ var settings = {};
16 const crypto = require('crypto');
17 const args = require('minimist')(process.argv.slice(2));
18 const path = require('path');
19 -const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'removedevice', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload', 'report'];
19 +const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'removedevice', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload', 'report', 'grouptoast', 'groupmessage'];
20 if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { console.log('Missing module "https-proxy-agent", type "npm install https-proxy-agent" to install it.'); return; } }
21
22 if (args['_'].length == 0) {
@@ -66,6 +66,8 @@ if (args['_'].length == 0) {
66 console.log(" DeviceOpenUrl - Open a URL on a remote device.");
67 console.log(" DeviceMessage - Open a message box on a remote device.");
68 console.log(" DeviceToast - Display a toast notification on a remote device.");
69 + console.log(" GroupMessage - Open a message box on remote devices in a specific device group.");
70 + console.log(" GroupToast - Display a toast notification on remote devices in a specific device group.");
71 console.log(" DevicePower - Perform wake/sleep/reset/off operations on remote devices.");
72 console.log(" DeviceSharing - View, add and remove sharing links for a given device.");
73 console.log(" AgentDownload - Download an agent of a specific type for a device group.");
@@ -276,6 +278,18 @@ if (args['_'].length == 0) {
278 else { ok = true; }
279 break;
280 }
281 + case 'groupmessage': {
282 + if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
283 + else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
284 + else { ok = true; }
285 + break;
286 + }
287 + case 'grouptoast': {
288 + if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
289 + else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
290 + else { ok = true; }
291 + break;
292 + }
293 case 'report': {
294 if (args.type == null) { console.log(winRemoveSingleQuotes("Missing report type, use --type '[reporttype]'")); }
295 else { ok = true; }
@@ -969,6 +983,38 @@ if (args['_'].length == 0) {
983 console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
984 break;
985 }
986 + case 'groupmessage': {
987 + console.log("Open a message box on remote devices in a specific device group, Example usages:\r\n");
988 + console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\""));
989 + console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\""));
990 + console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\" --timeout 120000"));
991 + console.log("\r\nRequired arguments:\r\n");
992 + if (process.platform == 'win32') {
993 + console.log(" --id [devicegroupid] - The device identifier.");
994 + } else {
995 + console.log(" --id '[devicegroupid]' - The device identifier.");
996 + }
997 + console.log(" --msg [message] - The message to display.");
998 + console.log("\r\nOptional arguments:\r\n");
999 + console.log(" --title [title] - Messagebox title, default is \"MeshCentral\".");
1000 + console.log(" --timeout [miliseconds] - After timeout messagebox vanishes, 0 keeps messagebox open until closed manually, default is 120000 (2 Minutes).");
1001 + break;
1002 + }
1003 + case 'grouptoast': {
1004 + console.log("Display a toast notification on remote devices in a specific device group, Example usages:\r\n");
1005 + console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\""));
1006 + console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\" --title \"title\""));
1007 + console.log("\r\nRequired arguments:\r\n");
1008 + if (process.platform == 'win32') {
1009 + console.log(" --id [devicegroupid] - The device identifier.");
1010 + } else {
1011 + console.log(" --id '[devicegroupid]' - The device identifier.");
1012 + }
1013 + console.log(" --msg [message] - The message to display.");
1014 + console.log("\r\nOptional arguments:\r\n");
1015 + console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
1016 + break;
1017 + }
1018 case 'report': {
1019 console.log("Generate a CSV report, Example usages:\r\n");
1020 console.log(" MeshCtrl Report --type sessions --devicegroup mesh//...");
@@ -1744,6 +1790,14 @@ function serverConnect() {
1790 ws.send(JSON.stringify({ action: 'toast', nodeids: [args.id], title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
1791 break;
1792 }
1793 + case 'groupmessage': {
1794 + ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
1795 + break;
1796 + }
1797 + case 'grouptoast': {
1798 + ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
1799 + break;
1800 + }
1801 case 'report': {
1802 var reporttype = 1;
1803 switch(args.type) {
@@ -2223,6 +2277,30 @@ function serverConnect() {
2277 }
2278 process.exit();
2279 }
2280 + if ((settings.cmd == 'groupmessage') && (data.responseid == 'meshctrl')) {
2281 + if ((data.nodes != null)) {
2282 + for (var i in data.nodes) {
2283 + for (let index = 0; index < data.nodes[i].length; index++) {
2284 + const element = data.nodes[i][index];
2285 + ws.send(JSON.stringify({ action: 'msg', type: 'messagebox', nodeid: element._id, title: args.title ? args.title : "MeshCentral", msg: args.msg, timeout: args.timeout ? args.timeout : 120000 }));
2286 + }
2287 + }
2288 + }
2289 +
2290 + setTimeout(function(){ console.log('ok'); process.exit(); }, 1000);
2291 + }
2292 + if ((settings.cmd == 'grouptoast') && (data.responseid == 'meshctrl')) {
2293 + if (data.nodes != null) {
2294 + for (var i in data.nodes) {
2295 + var nodes = [];
2296 + for (let index = 0; index < data.nodes[i].length; index++) {
2297 + const element = data.nodes[i][index];
2298 + nodes.push(element._id);
2299 + }
2300 + ws.send(JSON.stringify({ action: 'toast', nodeids: nodes, title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
2301 + }
2302 + }
2303 + }
2304 break;
2305 }
2306 case 'meshes': { // LISTDEVICEGROUPS