1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-14 07:13:37 +00:00
Files
MeshAgent/modules/linux-gnome-helpers.js
2020-06-09 14:25:28 -07:00

134 lines
6.4 KiB
JavaScript

function gnome_getProxySettings(uid)
{
var child = require('child_process').execFile('/bin/sh', ['sh'], { env: { HOME: require('user-sessions').getHomeFolder(uid) }});
child.stderr.str = ''; child.stderr.on('data', function (c) { });
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
child.stdin.write('gsettings list-recursively org.gnome.system.proxy | tr "\\n" "\\|" | awk \'');
child.stdin.write('{');
child.stdin.write(' count=split($0, res, "|");')
child.stdin.write(' exc="[]"; auth=""; pwd=""; username=""; enabled="";');
child.stdin.write(' for(a=0;a<count;++a)');
child.stdin.write(' {');
child.stdin.write(' split(res[a], modecheck, " ");');
child.stdin.write(' if(modecheck[2] == "mode")');
child.stdin.write(' {');
child.stdin.write(' split(modecheck[3], prx, "\\047"); mode = prx[2];');
child.stdin.write(' }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="host") { split(modecheck[3], hst, "\\047"); host = hst[2]; }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="port") { port = modecheck[3]; }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="use-authentication") { auth=modecheck[3]; }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy" && modecheck[2]=="ignore-hosts") { exc = substr(res[a], 36); gsub("\\047", "\\"", exc); }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="enabled") { enabled = modecheck[3]; }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="authentication-user")');
child.stdin.write(' {');
child.stdin.write(' split(res[a],dummy,"\\047"); username=dummy[2];');
child.stdin.write(' }');
child.stdin.write(' if(modecheck[1]=="org.gnome.system.proxy.http" && modecheck[2]=="authentication-password")');
child.stdin.write(' {');
child.stdin.write(' pwd=substr(res[a],53);');
child.stdin.write(' }');
child.stdin.write(' }');
child.stdin.write(' if(pwd~/^\\047/) { gsub("\\"", "\\\\\\"", pwd); gsub("\\047", "\\"", pwd); }');
child.stdin.write(' printf "{\\"mode\\": \\"%s\\", \\"enabled\\": %s, \\"host\\": \\"%s\\", \\"port\\": %s, \\"authEnabled\\": %s, \\"username\\": \\"%s\\", \\"password\\": %s, \\"exceptions\\": %s}", mode, enabled, host, port, auth, username, pwd, exc;');
child.stdin.write("}'\nexit\n");
child.waitExit();
try
{
return (JSON.parse(child.stdout.str.trim()));
}
catch(e)
{
return ({});
}
}
function gnome_getDesktopWallpaper(uid)
{
var child = require('child_process').execFile('/bin/sh', ['sh'], { env: { HOME: require('user-sessions').getHomeFolder(uid) } });
child.stderr.str = ''; child.stderr.on('data', function (c) { });
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
child.stdin.write('gsettings get org.gnome.desktop.background picture-uri\nexit\n');
child.waitExit();
child.stdout.str = child.stdout.str.trim().split('file://').pop();
if (child.stdout.str.endsWith('"') || child.stdout.str.endsWith("'"))
{
return (child.stdout.str.substring(0, child.stdout.str.length - 1));
}
else
{
return (child.stdout.str);
}
}
function gnome_setDesktopWallpaper(uid, filePath)
{
if (!filePath) { filePath = '/dev/null'; }
var v = { HOME: require('user-sessions').getHomeFolder(uid) };
var pids = require('process-manager').getProcess('gnome-session');
for (var i in pids)
{
var e = require('user-sessions').getEnvFromPid(pids[i]);
if (e.USER && require('user-sessions').getUid(e.USER)!=uid)
{
continue;
}
v.DBUS_SESSION_BUS_ADDRESS = e.DBUS_SESSION_BUS_ADDRESS;
if (v.DBUS_SESSION_BUS_ADDRESS) { break; }
}
var child = require('child_process').execFile('/bin/sh', ['sh'], { uid: uid, env: v });
child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); });
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
child.stdin.write('gsettings set org.gnome.desktop.background picture-uri file://' + filePath + '\nexit\n');
child.waitExit();
}
switch(process.platform)
{
case 'linux':
module.exports =
{
getProxySettings: gnome_getProxySettings,
getDesktopWallpaper: gnome_getDesktopWallpaper,
setDesktopWallpaper: gnome_setDesktopWallpaper
};
Object.defineProperty(module.exports, '_location', {
value: (function ()
{
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("whereis gsettings | awk '{ print $2 }'\nexit\n");
child.waitExit();
return (child.stdout.str.trim());
})()
});
Object.defineProperty(module.exports, 'available', { get: function () { return (this._location != '' ? true : false); } });
Object.defineProperty(module.exports, 'scriptVersion',
{
value: (function()
{
var ret = { major: 0, minor: 0 };
if(require('fs').existsSync('/usr/bin/script'))
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
child.stderr.on('data', function () { });
child.stdin.write('script -V | awk \'{ split($NF, T, "."); printf "{\\"major\\":%s, \\"minor\\":%s}",T[1],T[2]; }\'\nexit\n');
child.waitExit();
try
{
ret = JSON.parse(child.stdout.str.trim());
}
catch (x)
{ }
}
return (ret);
})()
});
break;
}