1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-22 11:13:21 +00:00

Updated for Linux/MacOS

This commit is contained in:
Bryan Roe
2019-06-06 18:47:05 -07:00
parent f28ea50560
commit 67e10e0e33
4 changed files with 203 additions and 171 deletions

File diff suppressed because one or more lines are too long

View File

@@ -289,9 +289,16 @@ function macos_messageBox()
this.promise._rej('TIMEOUT');
}
else
{
if (p.button == 'Yes')
{
this.promise._res(p.button);
}
else
{
this.promise._rej('denied');
}
}
break;
}
});

View File

@@ -16,14 +16,21 @@ limitations under the License.
var promise = require('promise');
if (process.platform == 'linux')
if (process.platform == 'linux' || process.platform == 'darwin')
{
function findPath(app)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
if (process.platform == 'linux')
{
child.stdin.write("whereis " + app + " | awk '{ print $2 }'\nexit\n");
}
else
{
child.stdin.write("whereis " + app + "\nexit\n");
}
child.waitExit();
child.stdout.str = child.stdout.str.trim();
return (child.stdout.str == '' ? null : child.stdout.str);
@@ -39,25 +46,23 @@ function Toaster()
retVal.title = title;
retVal.caption = caption;
if (process.platform == 'win32')
switch (process.platform) {
case 'win32':
{
var GM = require('_GenericMarshal');
var kernel32 = GM.CreateNativeProxy('kernel32.dll');
kernel32.CreateMethod('ProcessIdToSessionId');
var psid = GM.CreateVariable(4);
var consoleUid = require('user-sessions').consoleUid();
if (kernel32.ProcessIdToSessionId(process.pid, psid).Val == 0)
{
if (kernel32.ProcessIdToSessionId(process.pid, psid).Val == 0) {
retVal._rej('internal error'); return (retVal);
}
if (consoleUid == psid.toBuffer().readUInt32LE())
{
if (consoleUid == psid.toBuffer().readUInt32LE()) {
// We are running on the physical console
retVal._child = require('ScriptContainer').Create({ processIsolation: true });
}
else
{
else {
// We need so spawn the ScriptContainer into the correct session
retVal._child = require('ScriptContainer').Create({ processIsolation: true, sessionId: consoleUid });
}
@@ -81,42 +86,35 @@ function Toaster()
retVal._child.ExecuteString(str);
return (retVal);
}
else
{
try
break;
case 'linux':
{
try {
retVal.consoleUid = require('user-sessions').consoleUid();
retVal.xinfo = require('monitor-info').getXInfo(retVal.consoleUid);
}
catch(xxe)
{
catch (xxe) {
retVal._rej(xxe);
return (retVal);
}
var util = findPath('zenity');
if (util)
{
if (util) {
// Use ZENITY
retVal.child = require('child_process').execFile(util, ['zenity', '--notification', '--title=' + title, '--text=' + caption, '--timeout=5'], { uid: retVal.consoleUid, env: { XAUTHORITY: retVal.xinfo.xauthority, DISPLAY: retVal.xinfo.display } });
retVal.child.parent = retVal;
retVal.child.stderr.str = '';
retVal.child.stderr.on('data', function (chunk) { this.str += chunk.toString(); this.parent.kill(); });
retVal.child.stdout.on('data', function (chunk) { });
retVal.child.on('exit', function (code)
{
if(this.stderr.str.trim() != '')
{
if ((util = findPath('notify-send')) && this.stderr.str.split('GLib-CRITICAL').length > 1)
{
retVal.child.on('exit', function (code) {
if (this.stderr.str.trim() != '') {
if ((util = findPath('notify-send')) && this.stderr.str.split('GLib-CRITICAL').length > 1) {
// This is a bug in zenity, so we should try notify-send
if (process.env['DISPLAY'])
{
if (process.env['DISPLAY']) {
// DISPLAY is set, so we good to go
this.parent.child = require('child_process').execFile(util, ['notify-send', this.parent.title, this.parent.caption]);
this.parent.child.parent = this.parent;
}
else
{
else {
// We need to find the DISPLAY to use
var username = require('user-sessions').getUsername(consoleUid);
this.parent.child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -128,36 +126,30 @@ function Toaster()
this.parent.child.waitExit();
// NOTIFY-SEND has a bug where timeouts don't work, so the default is 5 seconds
this.parent._timeout = setTimeout(function onFakeDismissed(obj)
{
this.parent._timeout = setTimeout(function onFakeDismissed(obj) {
obj._res('DISMISSED');
}, 10000, this.parent);
}
else
{
else {
// Fake a toast using zenity --info
util = findPath('zenity');
this.parent.child = require('child_process').execFile(util, ['zenity', '--info', '--title=' + this.parent.title, '--text=' + this.parent.caption, '--timeout=5'], { uid: this.parent.consoleUid, env: { XAUTHORITY: this.parent.xinfo.xauthority, DISPLAY: this.parent.xinfo.display } });
this.parent.child.parent = this.parent;
this.parent.child.stderr.on('data', function (chunk) { });
this.parent.child.stdout.on('data', function (chunk) { });
this.parent.child.on('exit', function (code)
{
this.parent.child.on('exit', function (code) {
this.parent._res('DISMISSED');
});
}
}
else
{
else {
this.parent._res('DISMISSED');
}
});
}
else
{
else {
util = findPath('kdialog');
if (util)
{
if (util) {
// use KDIALOG
var xdg = require('user-sessions').findEnv(retVal.consoleUid, 'XDG_RUNTIME_DIR');
if (!retVal.xinfo || !retVal.xinfo.display || !retVal.xinfo.xauthority || !xdg)
@@ -176,9 +168,16 @@ function Toaster()
retVal._rej('Zenity/KDialog not found');
}
}
}
break;
case 'darwin':
retVal._toast = require('message-box').notify(title, caption);
retVal._toast.parent = retVal;
retVal._toast.then(function (v) { this.parent._res(v); }, function (e) { this.parent._rej(e); });
break;
}
return (retVal);
}
};
}

View File

@@ -393,6 +393,26 @@ function UserSessions()
}
return (ret);
}
this.consoleUid = function consoleUid()
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('who\nexit\n');
child.waitExit();
var lines = child.stdout.str.split('\n');
var tokens, i, j;
for (i in lines) {
tokens = lines[i].split(' ');
for (j = 1; j < tokens.length; ++j) {
if (tokens[j].length > 0) {
return (parseInt(this._users()[tokens[0]]));
}
}
}
throw ('nobody logged into console');
}
this.Current = function Current(cb)
{
var retVal = {};
@@ -652,6 +672,15 @@ function UserSessions()
}
else if(process.platform == 'darwin')
{
this.getUid = function getUid(username)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("id " + username + " | awk '{ split($1, token, \"=\"); split(token[2], uid, \"(\"); print uid[1]; }'\nexit\n");
child.waitExit();
return (parseInt(child.stdout.str.trim()));
};
this.getGroupID = function getGroupID(uid)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -664,8 +693,10 @@ function UserSessions()
this.getUsername = function getUsername(uid)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stderr.str = '';
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("dscl . list /Users UniqueID | grep " + uid + " | awk '{ if($2==" + uid + "){ print $1 }}'\nexit\n");
child.waitExit();
if(child.stdout.str.trim() != '')
@@ -677,6 +708,21 @@ function UserSessions()
throw ('uid: ' + uid + ' not found');
}
};
this.consoleUid = function consoleUid()
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("who | tr '\n' '\.' | awk '{ print $1 }'\nexit\n");
child.waitExit();
var ret = child.stdout.str.trim();
if (ret != '')
{
return (this.getUid(ret));
}
throw ('nobody logged into console');
}
this.getHomeFolder = function getHomeFolder(user)
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -697,10 +743,13 @@ function UserSessions()
{
var child = require('child_process').execFile('/usr/bin/dscl', ['dscl', '.', 'list', '/Users', 'UniqueID']);
child.stdout.str = '';
child.stderr.str = '';
child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('exit\n');
child.waitExit();
var lines = child.stdout.str.split('\n');
var tokens, i;
var users = {};
@@ -803,31 +852,6 @@ function UserSessions()
{
return (this.Self() == 0);
}
this.consoleUid = function consoleUid()
{
var checkstr = process.platform == 'darwin' ? 'console' : ((process.env['DISPLAY'])?process.env['DISPLAY']:':0')
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write('who\nexit\n');
child.waitExit();
var lines = child.stdout.str.split('\n');
var tokens, i, j;
for (i in lines)
{
tokens = lines[i].split(' ');
for (j = 1; j < tokens.length; ++j)
{
if (tokens[j].length > 0)
{
return (parseInt(this._users()[tokens[0]]));
}
}
}
throw ('nobody logged into console');
}
}